cubic_spline.h
Go to the documentation of this file.00001 #ifndef N_SPLINE_H
00002 #define N_SPLINE_H
00003
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include <map>
00040 namespace cxxtls
00041 {
00042
00043
00044 class cubic_spline
00045
00074 {
00075 public:
00076
00077 struct info
00078 {
00079 double y_;
00080 double s_;
00081
00082 info(double y, double s=0)
00083 : y_(y),
00084 s_(s)
00085 {
00086 }
00087 };
00088
00089 typedef std::multimap<double,info> rep_t;
00090 typedef rep_t::value_type value_t;
00091 typedef rep_t::iterator iterator;
00092 typedef rep_t::const_iterator const_iterator;
00093
00094 void add_sample(double x, double y)
00097 {
00098 dirty_ = true;
00099
00100 iterator where = rep_.find(x);
00101
00102 if(where != rep_.end())
00103 where->second.y_ = y;
00104 else
00105 rep_.insert(value_t(x,y));
00106
00107 }
00108
00109 void erase()
00111 {
00112 rep_.erase(rep_.begin(), rep_.end());
00113 dirty_ = true;
00114 }
00115
00116 double operator() (double x) const { return evaluate(x); }
00118
00119 rep_t const& samples() const { return rep_; }
00121
00122 cubic_spline()
00123 : dirty_(true)
00124 {
00125 }
00126
00127 cubic_spline(cubic_spline const& rhs)
00128 : rep_(rhs.rep_),
00129 dirty_(rhs.dirty_)
00130 {
00131 }
00132
00133 cubic_spline& operator=(cubic_spline const& rhs)
00134 {
00135 this->cubic_spline::~cubic_spline();
00136 new(this) cubic_spline(rhs);
00137 return *this;
00138 }
00139
00140 template<class InputIterator>
00141 cubic_spline(InputIterator first, InputIterator last)
00142 : dirty_(true)
00143 {
00146
00147 while(first != last)
00148 {
00149 add_sample(first->first, first->second);
00150
00151 ++first;
00152 }
00153 }
00154
00155 template<class OutputIterator>
00156 void evaluate(double start_x,
00157 double delta_x,
00158 OutputIterator begin,
00159 OutputIterator end
00160 )
00161
00168 {
00169 while(begin != end)
00170 {
00171 begin->first = start_x;
00172 begin->second = evaluate(start_x);
00173
00174 ++begin;
00175
00176 start_x += delta_x;
00177 }
00178 }
00179
00180 const_iterator ceil(double const& x) const
00183 {
00184 return rep_.upper_bound(x);
00185 }
00186
00187 const_iterator floor(double const x) const;
00194
00195 double evaluate(double) const;
00196 void compute_coefficients();
00197
00198
00199 private:
00200
00201 rep_t rep_;
00202 bool dirty_;
00203
00204 };
00205 }
00206 #endif