Thermal-FIST  1.3
Package for hadron resonance gas model applications
SplineFunction.h
Go to the documentation of this file.
1 /*
2 /*
3 /*
4 /*
5 /*
6 /*
7 /*
8 /*
9 /*
10 /* * GNU General Public License (GPLv3 or later)
11  */
12 #ifndef SPLINEFUNCTION_H
13 #define SPLINEFUNCTION_H
14 
15 #include <vector>
16 #include <algorithm>
17 #include <cmath>
18 
19 namespace thermalfist {
20 
23  {
24  public:
25  SplineFunction() : m_vals(0) {
26  m_vals.resize(0);
27  }
28 
29  SplineFunction(std::vector<double> x, std::vector<double> y) : m_vals(0) {
30  for (unsigned int i = 0; i < x.size(); ++i)
31  {
32  m_vals.push_back(std::make_pair(x[i], y[i]));
33  }
34  sort(m_vals.begin(), m_vals.end());
35  }
36 
38  void add_val(double x, double val)
39  {
40  m_vals.push_back(std::make_pair(x, val));
41  sort(m_vals.begin(), m_vals.end());
42  }
43 
45  double f(double arg) const
46  {
47  unsigned int ind = 0;
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);
51 
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);
55 
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);
59 
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);
63  }
64 
66  double df(double arg) const {
67  unsigned int ind = 0;
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);
71  if (ind == 0)
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);
76  }
77 
79  double fsquare(double arg) {
80  double ret = f(arg);
81  return ret * ret;
82  }
83 
85  void clear() {
86  m_vals.resize(2);
87  m_vals[0].first = 0.;
88  m_vals[0].second = 0.;
89  m_vals[1].first = 1.;
90  m_vals[1].second = 0.;
91  }
92 
94  void clearall() {
95  m_vals.resize(0);
96  }
97 
99  void fill(std::vector<double> x, std::vector<double> y) {
100  m_vals.resize(0);
101  for (unsigned int i = 0; i < x.size(); ++i)
102  {
103  m_vals.push_back(std::make_pair(x[i], y[i]));
104  }
105  sort(m_vals.begin(), m_vals.end());
106  }
107 
109  void setConstant(double val) {
110  m_vals.resize(0);
111  m_vals.push_back(std::make_pair(0., val));
112  m_vals.push_back(std::make_pair(1., val));
113  }
114 
115  // TODO: Read (x,y) pairs from file.
116  //void loadFromFile(const char *file);
117 
118  private:
119  std::vector< std::pair<double, double> > m_vals;
120  };
121 
122 } // namespace thermalfist
123 
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.