12 #ifndef SPLINEFUNCTION_H 13 #define SPLINEFUNCTION_H 30 for (
unsigned int i = 0; i < x.size(); ++i)
32 m_vals.push_back(std::make_pair(x[i], y[i]));
34 sort(m_vals.begin(), m_vals.end());
40 m_vals.push_back(std::make_pair(x, val));
41 sort(m_vals.begin(), m_vals.end());
45 double f(
double arg)
const 48 std::pair<double, double> op = std::make_pair(arg, 0.);
49 std::vector< std::pair<double, double> >::const_iterator it = std::lower_bound(m_vals.begin(), m_vals.end(), op);
50 ind = distance(m_vals.begin(), it);
52 if (ind == 0)
return m_vals[0].second +
53 (arg - m_vals[0].first) *
54 (m_vals[1].second - m_vals[0].second) / (m_vals[1].first - m_vals[0].first);
56 if (ind == m_vals.size())
return m_vals[ind - 2].second +
57 (arg - m_vals[ind - 2].first) *
58 (m_vals[ind - 1].second - m_vals[ind - 2].second) / (m_vals[ind - 1].first - m_vals[ind - 2].first);
60 return m_vals[ind - 1].second +
61 (arg - m_vals[ind - 1].first) *
62 (m_vals[ind].second - m_vals[ind - 1].second) / (m_vals[ind].first - m_vals[ind - 1].first);
66 double df(
double arg)
const {
68 std::pair<double, double> op = std::make_pair(arg, 0.);
69 std::vector< std::pair<double, double> >::const_iterator it = std::lower_bound(m_vals.begin(), m_vals.end(), op);
70 ind = std::distance(m_vals.begin(), it);
72 return (m_vals[1].second - m_vals[0].second) / (m_vals[1].first - m_vals[0].first);
73 if (ind == m_vals.size())
74 return (m_vals[ind - 1].second - m_vals[ind - 2].second) / (m_vals[ind - 1].first - m_vals[ind - 2].first);
75 return (m_vals[ind].second - m_vals[ind - 1].second) / (m_vals[ind].first - m_vals[ind - 1].first);
88 m_vals[0].second = 0.;
90 m_vals[1].second = 0.;
99 void fill(std::vector<double> x, std::vector<double> y) {
101 for (
unsigned int i = 0; i < x.size(); ++i)
103 m_vals.push_back(std::make_pair(x[i], y[i]));
105 sort(m_vals.begin(), m_vals.end());
111 m_vals.push_back(std::make_pair(0., val));
112 m_vals.push_back(std::make_pair(1., val));
119 std::vector< std::pair<double, double> > m_vals;
124 #endif // SPLINEFUNCTION_H
Class implementing a simple linear spline.
void clear()
Clear all data and refill with zero function.
double fsquare(double arg)
Evaluates f(arg)^2.
void add_val(double x, double val)
Adds a new pair of x,y values.
double f(double arg) const
Evaluates interpolated function at x = arg.
SplineFunction(std::vector< double > x, std::vector< double > y)
double df(double arg) const
Evaluates slope (derivative) at x = arg.
void setConstant(double val)
Models constnat f(x) == val function.
void fill(std::vector< double > x, std::vector< double > y)
Fill (x,y) pairs from provided vectors.
The main namespace where all classes and functions of the Thermal-FIST library reside.
void clearall()
Just clear all data.