xc
FunctionFromPointsR_T.h
1 // -*-c++-*-
2 //----------------------------------------------------------------------------
3 // xc utils library; general purpose classes and functions.
4 //
5 // Copyright (C) Luis C. PĂ©rez Tato
6 //
7 // XC utils is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // This software is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program.
19 // If not, see <http://www.gnu.org/licenses/>.
20 //----------------------------------------------------------------------------
21 //FunctionFromPointsR_T.h
22 
23 #ifndef FUNCTIONFROMPOINTSR_T_H
24 #define FUNCTIONFROMPOINTSR_T_H
25 
26 #include <map>
27 #include <iostream>
28 #include <vector>
29 #include <boost/python/list.hpp>
30 
36 template <class T>
38  {
39  public:
40  typedef std::map<double,T> value_map;
41  typedef typename value_map::iterator iterator;
42  typedef typename value_map::const_iterator const_iterator;
43  typedef typename value_map::const_reverse_iterator const_reverse_iterator;
44  private:
45  value_map points;
46  public:
47  T Valor(const double &) const;
48  std::vector<T> Valores(const std::vector<double> &) const;
49  T operator()(const double &z) const
50  { return Valor(z); }
51  std::vector<T> operator()(const std::vector<double> &zs) const
52  { return Valores(zs); }
54  const_iterator begin(void) const
55  { return points.begin(); }
57  const_reverse_iterator rbegin(void) const
58  { return points.rbegin(); }
60  const_iterator end(void) const
61  { return points.end(); }
63  const_reverse_iterator rend(void) const
64  { return points.rend(); }
66  size_t size(void) const
67  { return points.size(); }
69  size_t empty(void) const
70  { return points.empty(); }
71  void insert(const double &x,const T &y);
72  void clear(void)
73  { points.clear(); }
74  };
75 
77 template <class T>
78 T FunctionFromPointsR_T<T>::Valor(const double &z) const
79  {
80  T retval= T();
81  if(points.empty()) return retval; //If empty return default value.
82  const_iterator j= points.upper_bound(z);
83  if(j==begin()) //Abscissae z is smaller that all the rest.
84  j++;//retval= (*j).second/(*j).first*z;
85  if(j==end()) //Abscissae z is greater that all the rest.
86  j--;//retval= (*j).second/(*j).first*z;
87  const_iterator i= j;
88  i--;
89  const double x0= (*i).first;
90  const T y0= (*i).second;
91  const double x1= (*j).first;
92  const T y1= (*j).second;
93  const T a= (y1-y0)/(x1-x0);
94  const T b= y1-a*x1;
95  retval= a*z+b;
96  return retval;
97  }
98 
100 template <class T>
101 std::vector<T> FunctionFromPointsR_T<T>::Valores(const std::vector<double> &zs) const
102  {
103  const size_t sz= zs.size();
104  std::vector<T> retval(sz);
105  if(points.empty()) return retval; //If empty return default value.
106  for(size_t i= 0;i<sz;i++)
107  retval[i]= Valor(zs[i]);
108  return retval;
109  }
110 
112 template <class T>
113 void FunctionFromPointsR_T<T>::insert(const double &x,const T &y)
114  {
115  points[x]= y;
116  }
117 template <class T>
118 std::ostream &operator<<(std::ostream &os, const FunctionFromPointsR_T<T> &lt)
119  {
120  if(!lt.empty())
121  {
122  typename FunctionFromPointsR_T<T>::const_iterator i= lt.begin();
123  os << "x= " << (*i).first
124  << " y= " << (*i).second;
125  i++;
126  for(;i!=lt.end();i++)
127  os << std::endl << "x= " << (*i).first
128  << " y= " << (*i).second;
129  }
130  return os;
131  }
132 
133 
134 #endif
const_reverse_iterator rend(void) const
Return an iterator to the beginning of the point container.
Definition: FunctionFromPointsR_T.h:63
std::vector< T > Valores(const std::vector< double > &) const
Return los valores que corresponden a las abcisas z.
Definition: FunctionFromPointsR_T.h:101
size_t size(void) const
Return the number of points that define the function.
Definition: FunctionFromPointsR_T.h:66
function from R to T defined on a set of points.
Definition: FunctionFromPointsR_T.h:37
const_iterator end(void) const
Return an iterator to the end of the point container.
Definition: FunctionFromPointsR_T.h:60
void insert(const double &x, const T &y)
Inserts the pair (x,y) as a point of the function.
Definition: FunctionFromPointsR_T.h:113
const_reverse_iterator rbegin(void) const
Return an reverse iterator to the end of the point container.
Definition: FunctionFromPointsR_T.h:57
const_iterator begin(void) const
Return an iterator to the beginning of the point container.
Definition: FunctionFromPointsR_T.h:54
T Valor(const double &) const
Return el valor que corresponde a la abcisa z.
Definition: FunctionFromPointsR_T.h:78
size_t empty(void) const
Return true if there are no points.
Definition: FunctionFromPointsR_T.h:69