proj home

Files   Classes   Functions   Hierarchy  

partialderivative.h

Go to the documentation of this file.
00001 #ifndef PARTIALDERIVATIVE_H
00002 #define PARTIALDERIVATIVE_H
00003 
00004 #include <cmath>
00005 using namespace std;
00006 
00007 #include <typeop.h>
00008 
00016 template< typename EXP >
00017 class partialderivative
00018 {
00019 public:
00020 
00022   typename typeop<EXP>::Tref exp;
00023 
00024   typedef typename typeop<EXP>::Tbare::FNtype FN;
00025   typedef typename typeop<EXP>::Tbare::Ttype T;
00026   
00028   T * dxi;
00029 
00031   uintc N;
00032 
00034   partialderivative
00035   (
00036     typename typeop<EXP>::Tref exp_
00037   );
00038 
00040   ~partialderivative();
00041 
00043   void operator()();
00044 
00046   T const squared() const
00047   {
00048     T val(0);
00049     for (uint i=0; i<N; ++i)
00050       val += dxi[i]*dxi[i];
00051 
00052     return val;
00053   }
00054  
00056   T const absvalue() const
00057   {
00058     T val(0);
00059     for (uint i=0; i<N; ++i)
00060       val += abs(dxi[i]);
00061 
00062     return val;
00063   }
00064 };
00065 
00066 //----------------------------------------------------
00067 //  Implementation
00068 
00069 
00070 template< typename EXP >
00071 void partialderivative<EXP>::operator()()
00072 {
00073   T x;
00074   T fnb;
00075   T fna;
00076 
00077   T h;
00078 
00079   // O(N).O(fn) complexity
00080   for (uint i=0; i<N; ++i)
00081   {
00082     h = exp.hi[i];
00083     x = exp.fn.xi[i];
00084     exp.fn.xi[i] -= h;
00085     exp.fn(fna);
00086     exp.fn.xi[i] = x + h;
00087     exp.fn(fnb);
00088     // Restore xi
00089     exp.fn.xi[i] = x;
00090     
00091     // Compute the partial derivative.
00092     assert(h!=0.0);
00093     dxi[i] = (fnb-fna)/h*0.5;
00094   }
00095 
00096   // Restore the current state.
00097   exp.fn(fna);
00098 }
00099 
00100 template< typename EXP >
00101 partialderivative<EXP>::partialderivative
00102 (
00103   typename typeop<EXP>::Tref exp_
00104 )
00105   : exp(exp_), N(exp_.N)
00106 {
00107   dxi = new T[N];
00108 }
00109 
00110 template< typename EXP >
00111 partialderivative<EXP>::~partialderivative()
00112 {
00113   delete[] dxi;
00114 }
00115 
00116 
00117 
00118 
00119 #endif
00120 
00121 

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