cubic_spline.h

Go to the documentation of this file.
00001 #ifndef N_SPLINE_H
00002 #define N_SPLINE_H
00003 
00006 
00007 //
00008 // Copyright 2002, Lowell Boggs Jr.
00009 //
00010 // This file or directory, containing source code for a computer program,
00011 // is Copyrighted by Lowell Boggs, Jr.  987 Regency Drive, Lewisville
00012 // TX (USA), 75067.  You may use, copy, modify, and distribute this
00013 // source file without charge or obligation so long as you agree to
00014 // the following:
00015 //
00016 //  1.  You must indemnify Lowell Boggs against any and all financial
00017 //      obligations caused by its use, misuse, function, or malfunction.
00018 //      Further, you acknowledge that there is no warranty of any kind,
00019 //      whatsoever.
00020 //
00021 //  2.  You agree not to attempt to patent any portion of this original
00022 //      work -- though you may attempt to patent your own extensions to
00023 //      it if you so choose.
00024 //
00025 //  3.  You keep this copyright notice with the file and all copies
00026 //      of the file and do not change it anyway except language translation.
00027 //
00028 // You are responsible for enforcing your own compliance with these
00029 // conditions and may not use this source file if you cannot agree to the
00030 // above terms and conditions.
00031 //
00032 // Warning:  not all files in this directory structure are covered by the
00033 // same copyright.  Some of them are part of the GNU source distribution
00034 // and you must obey the GPL copyright for those files.
00035 //
00036 // Further warning, the algorithms container here are c++ transmogriphications
00037 // of code from a college textbook -- originally fortran.  See below.
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 } // namespace cxxtls
00206 #endif
Generated on Wed Feb 29 22:50:03 2012 for CXXUtilities by  doxygen 1.6.3