proj home

Files   Classes   Functions   Hierarchy  

fixedpoint.h

Go to the documentation of this file.
00001 #ifndef FIXEDPOINT_H
00002 #define FIXEDPOINT_H
00003 
00004 #include <cmath>
00005 using namespace std;
00006 
00007 
00019 template< typename F, unsigned int N >
00020 class fixedpoint
00021 {
00022 public:
00023 
00024   F f;
00025 
00027   void inc(double x[N], double const x0[N]) const; 
00028 
00030   double const norm
00031   (
00032     double const x[N], 
00033     double const x0[N]
00034   ) const;
00035 };
00036 
00037 
00038 /*
00039  *  Example Code 
00040  *
00041  *  Since this is a little non-standard I have supplied
00042  *  example code because I do not know how to communicate
00043  *  the interface expectations other than as given.
00044  *
00045  *  Solve
00046  *  3x + 2y = 7
00047  * -5x + 4y = 3
00048  *
00049  * x=1,y=2
00050 
00051 class f2
00052 {
00053 public:
00054 
00055   // f is the system of equations as a vector function = 0.
00056   void f(double fx[2], double const x[2]) const; 
00057 
00058   // Inverse Derivative of vector equ's f or inverse grad f. 
00059   void IDf(double idf[2][2], double const x[2]) const; 
00060 
00061 };
00062 
00063 void f2::f(double fx[2], double const x[2]) const
00064 {
00065   fx[0] = 3.0*x[0]+2.0*x[1]-7.0;
00066   fx[1] = -5.0*x[0]+4*x[1]-3.0;
00067 }  
00068 
00069 
00070 void f2::IDf(double idf[2][2], double const x[2]) const 
00071 {
00072   double det = 22.0;
00073 
00074   idf[0][0] = 4.0/det;
00075   idf[0][1] = -2.0/det;
00076   idf[1][0] = 5.0/det;
00077   idf[1][1] = 3.0/det;
00078 } 
00079 
00080 */
00081 
00082 
00083 
00084 
00085 //----------------------------------------------------------
00086 // Implementation
00087 
00088 
00089 
00090 template < typename F, unsigned int N >
00091 void fixedpoint<F,N>::inc
00092 (
00093   double x[N], 
00094   double const x0[N]
00095 ) const
00096 {
00097   double fx[N];
00098   f.f(fx,x0);
00099 
00100   for (unsigned int i=0; i<N; ++i)
00101     x[i] = x0[i];
00102 
00103   double IDF[N][N];
00104   f.IDf(IDF,x0);
00105 
00106   for (unsigned int i=0; i<N; ++i)
00107   {
00108     for (unsigned int k=0; k<N; ++k)
00109     {
00110       x[i] -= IDF[i][k]*fx[k];
00111     }
00112   }
00113 }
00114 
00115 template < typename F, unsigned int N >
00116 double const fixedpoint<F,N>::norm
00117 (
00118   double const x[N], 
00119   double const x0[N]
00120 ) const
00121 {
00122   double s = 0.0;
00123   for ( unsigned int i=0; i<N; ++i )
00124     s += abs(x[i]-x0[i]);
00125 
00126   return s;
00127 }
00128   
00129 
00130 
00131 #endif
00132 
00133 

Generated on Fri Mar 4 00:49:28 2011 for Chelton Evans Source by  doxygen 1.5.8