Weibull.cxx
Go to the documentation of this file.
1 
12 #ifdef _MSC_VER
13 #include "msdevstudio/MSconfig.h"
14 #endif
15 
16 #include "Weibull.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 Weibull::Weibull ( )
33 {
34  initialize ();
35 }
36 
37 Weibull::Weibull ( double prefactor, double scale, double shape )
38 {
39  initialize ();
40 
41  m_parms[0] = prefactor;
42  m_parms[1] = scale;
43  m_parms[2] = shape;
44 }
45 
46 void Weibull::initialize ()
47 {
48  m_name = "Weibull";
49  m_parm_names.push_back ( "Prefactor" );
50  m_parm_names.push_back ( "Scale" );
51  m_parm_names.push_back ( "Shape" );
52 
53  resize ();
54 }
55 
57 {
58  return new Weibull ( *this );
59 }
60 
61 double Weibull::operator () ( double x ) const
62 {
63  return m_parms[0]*exp( -pow(x/m_parms[1],m_parms[2]) )*pow(x,m_parms[2] - 1.0);
64 }
65 
66 /* virtual */
67 void
68 Weibull::
69 initialParameters ( const FunctionHelper * helper )
70 {
71  double min_x = helper->minCoord ();
72  double max_x = helper->maxCoord ();
73  max_x = (min_x + max_x)/2.;
74 
75  m_parms[2] = 1.0;
76 
77  try {
78  double min_y = helper->valueAt (min_x);
79  double max_y = helper->valueAt (max_x);
80  if (min_y != 0 && max_y != 0) { // success!
81  m_parms[1] = ( min_x - max_x ) / log( max_y/min_y );
82  m_parms[0] = max_y / exp( -max_x/m_parms[1] );
83  return;
84  }
85  } catch (std::string &message) {
86  std::cerr << message << std::endl;
87  }
88 
89 // All cleverness fails, so use default values....
90  m_parms[0] = 1.;
91  m_parms[1] = 1.;
92 }
93 
94 double Weibull::derivByParm ( int i, double x ) const
95 {
96  switch ( i ) {
97  case 0 :
98  return operator()(x)/m_parms[0];
99  break;
100 
101  case 1 :
102  return operator()(x)*pow(x/m_parms[1],m_parms[2]) * m_parms[2]/m_parms[1];
103  break;
104 
105  case 2 :
106  return operator()(x)*(log(x) - pow(x/m_parms[1],m_parms[2]) * log(x/m_parms[1]));
107  break;
108 
109  default:
110  assert ( false );
111  break;
112  }
113  return 0.0;
114 }
115 
116 } // namespace hippodraw
117 
unsigned int i
FunctionHelper class interface.
std::vector< intptr_t > shape(numeric::array arr)
Definition: num_util.cpp:317
A function can be used with a fitter.
Definition: Weibull.h:25
virtual double minCoord() const =0
Returns the smallest coordinate value along the X axis in the data set.
numeric::array clone(numeric::array arr)
Definition: num_util.cpp:412
A function that can be added to a DataRep and used in a fitter.
Definition: FunctionBase.h:90
An abstract base class to help FunctionBase objects perform some operations.
virtual double maxCoord() const =0
Returns the largest coordinate value along the X axis in the data set.
Weibull class interface.
virtual double valueAt(double x) const =0
Returns the value at a given coordinate.

Generated for HippoDraw Class Library by doxygen