Exponential.cxx
Go to the documentation of this file.
1 
12 #ifdef _MSC_VER
13 #include "msdevstudio/MSconfig.h"
14 #endif
15 
16 #include "Exponential.h"
17 
18 #include "FunctionHelper.h"
19 
20 #include <cmath>
21 #include <cassert>
22 #include <iostream>
23 
24 #ifdef ITERATOR_MEMBER_DEFECT
25 using namespace std;
26 #else
27 using std::vector;
28 #endif
29 
30 namespace hippodraw {
31 
32 Exponential::Exponential ( )
33 {
34  initialize ();
35 }
36 
37 Exponential::Exponential ( double prefactor, double scale )
38 {
39  initialize ();
40 
41  m_parms[0] = prefactor;
42  m_parms[1] = scale;
43 }
44 
45 void Exponential::initialize ()
46 {
47  m_name = "Exponential";
48  m_parm_names.push_back ( "Prefactor" );
49  m_parm_names.push_back ( "Scale" );
50 
51  resize ();
52 }
53 
55 {
56  return new Exponential ( *this );
57 }
58 
59 double Exponential::operator () ( double x ) const
60 {
61  return m_parms[0]*exp( -x/m_parms[1] );
62 }
63 
64 /* virtual */
65 void
66 Exponential::
67 initialParameters ( const FunctionHelper * helper )
68 {
69  double min_x = helper->minCoord ();
70  double max_x = helper->maxCoord ();
71  max_x = (min_x + max_x)/2.;
72 
73  try {
74  double min_y = helper->valueAt (min_x);
75  double max_y = helper->valueAt (max_x);
76  if (min_y != 0 && max_y != 0) { // success!
77  m_parms[1] = ( min_x - max_x ) / log( max_y/min_y );
78  m_parms[0] = max_y / exp( -max_x/m_parms[1] );
79  return;
80  }
81  } catch (std::string &message) {
82  std::cerr << message << std::endl;
83  }
84 
85 // All cleverness fails, so use default values....
86  m_parms[0] = 1.;
87  m_parms[1] = 1.;
88 }
89 
90 double Exponential::derivByParm ( int i, double x ) const
91 {
92  switch ( i ) {
93  case 0 :
94  return exp( -x/m_parms[1] );
95  break;
96 
97  case 1 :
98  return operator()(x)*(x/m_parms[1]/m_parms[1]);
99  break;
100 
101  default:
102  assert ( false );
103  break;
104  }
105  return 0.0;
106 }
107 
108 } // namespace hippodraw
109 

Generated for HippoDraw Class Library by doxygen