00001 #ifndef CACHEDFUNCTION1D_H
00002 #define CACHEDFUNCTION1D_H
00003
00004 #include <iostream>
00005 #include <cmath>
00006 using namespace std;
00007
00008
00009 namespace SctData{
00010
00017 class CachedFunction1D{
00018 public:
00028 CachedFunction1D(double min, double max, double delta, double (*function)(double) ) throw() ;
00032 ~CachedFunction1D() throw() ;
00040 inline double eval(double x) const throw() ;
00041 private:
00043 CachedFunction1D() throw() ;
00045 CachedFunction1D& operator=(const CachedFunction1D&) throw() ;
00046 void makeTable() throw();
00047 double min;
00048 double max;
00049 double delta;
00050 double invdelta;
00051 int nvalue;
00052 double * values;
00053 double (*function) (double);
00054 };
00055
00056
00057 double CachedFunction1D::eval(double x) const throw() {
00058
00059
00060
00061
00062
00063
00064 double pos = (x-min)*invdelta;
00065 int ipos=(int)(pos);
00066 if (ipos<0) { return values[0]; }
00067 else if (ipos>=nvalue-1) { return values[nvalue-1]; }
00068
00069 double frac = pos - ipos;
00070 double y1 = values[ipos];
00071 double y2 = values[ipos+1];
00072
00073
00074
00075 return y1 + (y2-y1) * frac;
00076 }
00077
00078 }
00079
00080
00081
00082 #endif // #ifndef CACHEDFUNCTION_H