Jpp  master_rocky-37-gf0c5bc59d
the software that should make you happy
JMath/JModel.hh
Go to the documentation of this file.
1 #ifndef __JMATH__JMODEL__
2 #define __JMATH__JMODEL__
3 
4 #include <istream>
5 #include <ostream>
6 #include <iomanip>
7 #include <cmath>
8 #include <limits>
9 
10 #include "JLang/JEquals.hh"
11 #include "JLang/JManip.hh"
12 #include "JMath/JMath.hh"
13 #include "JMath/JZero.hh"
14 
15 /**
16  * \author mdejong
17  */
18 
19 namespace JMATH {}
20 namespace JPP { using namespace JMATH; }
21 
22 namespace JMATH {
23 
24  using JLANG::JEquals;
25 
26  /**
27  * Fit model.
28  */
29  struct JModel_t :
30  public std::vector<double>,
31  JMath<JModel_t>
32  {
34 
35  typedef size_t parameter_type;
36 
37  /**
38  * Reset.
39  *
40  * \param zero zero
41  * \return this model
42  */
44  {
45  for (size_t i = 0; i != this->size(); ++i) {
46  (*this)[i] = 0.0;
47  }
48 
49  return *this;
50  }
51 
52 
53  /**
54  * Get value at given index.
55  *
56  * \param index index
57  * \return value
58  */
59  double& operator[](const size_t index)
60  {
61  if (index >= this->size()) {
62  this->resize(index + 1, 0.0);
63  }
64 
65  return std::vector<double>::operator[](index);
66  }
67 
68 
69  /**
70  * Equality.
71  *
72  * \param model model
73  * \param eps numerical precision
74  * \return true if model's identical; else false
75  */
76  bool equals(const JModel_t& model,
77  const double eps = std::numeric_limits<double>::min()) const
78  {
79  for (size_t i = 0; i != this->size(); ++i) {
80  if (fabs((*this)[i] - model[i]) >= eps) {
81  return false;
82  }
83  }
84 
85  return true;
86  }
87 
88 
89  /**
90  * Negate model.
91  *
92  * \return this model
93  */
95  {
96  for (size_t i = 0; i != this->size(); ++i) {
97  (*this)[i] = -(*this)[i];
98  }
99 
100  return *this;
101  }
102 
103 
104  /**
105  * Add model.
106  *
107  * \param model model
108  * \return this model
109  */
111  {
112  for (size_t i = 0; i != model.size(); ++i) {
113  (*this)[i] += model[i];
114  }
115 
116  return *this;
117  }
118 
119 
120  /**
121  * Subtract model.
122  *
123  * \param model model
124  * \return this model
125  */
127  {
128  for (size_t i = 0; i != model.size(); ++i) {
129  (*this)[i] -= model[i];
130  }
131 
132  return *this;
133  }
134 
135 
136  /**
137  * Scale model.
138  *
139  * \param factor multiplication factor
140  * \return this model
141  */
142  JModel_t& mul(const double factor)
143  {
144  for (size_t i = 0; i != this->size(); ++i) {
145  (*this)[i] *= factor;
146  }
147 
148  return *this;
149  }
150 
151 
152  /**
153  * Scale model.
154  *
155  * \param factor division factor
156  * \return this model
157  */
158  JModel_t& div(const double factor)
159  {
160  for (size_t i = 0; i != this->size(); ++i) {
161  (*this)[i] /= factor;
162  }
163 
164  return *this;
165  }
166 
167 
168  /**
169  * Write model to input stream.
170  *
171  * \param in input stream
172  * \param model model
173  * \return input stream
174  */
175  friend inline std::istream& operator>>(std::istream& in, JModel_t& model)
176  {
177  model.clear();
178 
179  for (double value; in >> value; ) {
180  model.push_back(value);
181  }
182 
183  return in;
184  }
185 
186 
187  /**
188  * Write model to output stream.
189  *
190  * \param out output stream
191  * \param model model
192  * \return output stream
193  */
194  friend inline std::ostream& operator<<(std::ostream& out, const JModel_t& model)
195  {
196  using namespace std;
197 
198  for (JModel_t::const_iterator i = model.begin(); i != model.end(); ++i) {
199  out << FIXED(7,3) << *i << ' ';
200  }
201 
202  return out;
203  }
204  };
205 }
206 
207 #endif
I/O manipulators.
Base class for data structures with artithmetic capabilities.
Definition of zero value for any class.
Auxiliary classes and methods for mathematical operations.
Definition: JEigen3D.hh:88
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:105
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JSTDTypes.hh:14
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:448
Template definition of auxiliary base class for comparison of data structures.
Definition: JEquals.hh:84
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:347
Fit model.
Definition: JMath/JModel.hh:32
JModel_t & sub(const JModel_t &model)
Subtract model.
double & operator[](const size_t index)
Get value at given index.
Definition: JMath/JModel.hh:59
JModel_t & add(const JModel_t &model)
Add model.
JModel_t & div(const double factor)
Scale model.
friend std::ostream & operator<<(std::ostream &out, const JModel_t &model)
Write model to output stream.
friend std::istream & operator>>(std::istream &in, JModel_t &model)
Write model to input stream.
size_t parameter_type
Definition: JMath/JModel.hh:35
bool equals(const JModel_t &model, const double eps=std::numeric_limits< double >::min()) const
Equality.
Definition: JMath/JModel.hh:76
JModel_t & negate()
Negate model.
Definition: JMath/JModel.hh:94
JModel_t & mul(const double factor)
Scale model.
JModel_t & operator=(const JMATH::JZero &zero)
Reset.
Definition: JMath/JModel.hh:43
Auxiliary class to assign zero value.
Definition: JZero.hh:81