00001 00012 #ifdef _MSC_VER 00013 #include "msdevstudio/MSconfig.h" 00014 #endif 00015 00016 #include "Exponential.h" 00017 00018 #include "FunctionHelper.h" 00019 00020 #include <cmath> 00021 #include <cassert> 00022 #include <iostream> 00023 00024 #ifdef ITERATOR_MEMBER_DEFECT 00025 using namespace std; 00026 #else 00027 using std::vector; 00028 #endif 00029 00030 namespace hippodraw { 00031 00032 Exponential::Exponential ( ) 00033 { 00034 initialize (); 00035 } 00036 00037 Exponential::Exponential ( double prefactor, double scale ) 00038 { 00039 initialize (); 00040 00041 m_parms[0] = prefactor; 00042 m_parms[1] = scale; 00043 } 00044 00045 void Exponential::initialize () 00046 { 00047 m_name = "Exponential"; 00048 m_parm_names.push_back ( "Prefactor" ); 00049 m_parm_names.push_back ( "Scale" ); 00050 00051 resize (); 00052 } 00053 00054 FunctionBase * Exponential::clone () const 00055 { 00056 return new Exponential ( *this ); 00057 } 00058 00059 double Exponential::operator () ( double x ) const 00060 { 00061 return m_parms[0]*exp( -x/m_parms[1] ); 00062 } 00063 00064 /* virtual */ 00065 void 00066 Exponential:: 00067 initialParameters ( const FunctionHelper * helper ) 00068 { 00069 double min_x = helper->minCoord (); 00070 double max_x = helper->maxCoord (); 00071 max_x = (min_x + max_x)/2.; 00072 00073 try { 00074 double min_y = helper->valueAt (min_x); 00075 double max_y = helper->valueAt (max_x); 00076 if (min_y != 0 && max_y != 0) { // success! 00077 m_parms[1] = ( min_x - max_x ) / log( max_y/min_y ); 00078 m_parms[0] = max_y / exp( -max_x/m_parms[1] ); 00079 return; 00080 } 00081 } catch (std::string &message) { 00082 std::cerr << message << std::endl; 00083 } 00084 00085 // All cleverness fails, so use default values.... 00086 m_parms[0] = 1.; 00087 m_parms[1] = 1.; 00088 } 00089 00090 double Exponential::derivByParm ( int i, double x ) const 00091 { 00092 switch ( i ) { 00093 case 0 : 00094 return exp( -x/m_parms[1] ); 00095 break; 00096 00097 case 1 : 00098 return operator()(x)*(x/m_parms[1]/m_parms[1]); 00099 break; 00100 00101 default: 00102 assert ( false ); 00103 break; 00104 } 00105 return 0.0; 00106 } 00107 00108 } // namespace hippodraw 00109