Jpp  18.0.0-rc.3
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JGauss.hh
Go to the documentation of this file.
1 #ifndef __JMATH__JGAUSS__
2 #define __JMATH__JGAUSS__
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/JConstants.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  * Gauss model.
28  */
29  struct JGauss_t :
30  public JMath<JGauss_t>,
31  public JEquals<JGauss_t>
32  {
33  /**
34  * Default constructor.
35  */
37  mean (0.0),
38  sigma (0.0),
39  signal (0.0),
40  background(0.0)
41  {}
42 
43 
44  /**
45  * Constructor.
46  *
47  * \param mean mean
48  * \param sigma sigma
49  * \param signal signal
50  * \param background background
51  */
52  JGauss_t(const double mean,
53  const double sigma,
54  const double signal,
55  const double background) :
56  mean (mean),
57  sigma (sigma),
58  signal (signal),
59  background(background)
60  {}
61 
62 
63  /**
64  * Equality.
65  *
66  * \param gauss gauss
67  * \param eps numerical precision
68  * \return true if gauss's identical; else false
69  */
70  bool equals(const JGauss_t& gauss,
71  const double eps = std::numeric_limits<double>::min()) const
72  {
73  return (fabs(mean - gauss.mean) <= eps &&
74  fabs(sigma - gauss.sigma) <= eps &&
75  fabs(signal - gauss.signal) <= eps &&
76  fabs(background - gauss.background) <= eps);
77  }
78 
79 
80  /**
81  * Add gauss.
82  *
83  * \param gauss gauss
84  * \return this gauss
85  */
86  JGauss_t& add(const JGauss_t& gauss)
87  {
88  mean += gauss.mean;
89  sigma += gauss.sigma;
90  signal += gauss.signal;
91  background += gauss.background;
92 
93  return *this;
94  }
95 
96 
97  /**
98  * Subtract gauss.
99  *
100  * \param gauss gauss
101  * \return this gauss
102  */
103  JGauss_t& sub(const JGauss_t& gauss)
104  {
105  mean -= gauss.mean;
106  sigma -= gauss.sigma;
107  signal -= gauss.signal;
108  background -= gauss.background;
109 
110  return *this;
111  }
112 
113 
114  /**
115  * Scale gauss.
116  *
117  * \param factor multiplication factor
118  * \return this gauss
119  */
120  JGauss_t& mul(const double factor)
121  {
122  mean *= factor;
123  sigma *= factor;
124  signal *= factor;
125  background *= factor;
126 
127  return *this;
128  }
129 
130 
131  /**
132  * Write Gauss to input stream.
133  *
134  * \param in input stream
135  * \param gauss gauss
136  * \return input stream
137  */
138  friend inline std::istream& operator>>(std::istream& in, JGauss_t& gauss)
139  {
140  return in >> gauss.mean >> gauss.sigma >> gauss.signal >> gauss.background;
141  }
142 
143 
144  /**
145  * Write Gauss to output stream.
146  *
147  * \param out output stream
148  * \param gauss gauss
149  * \return output stream
150  */
151  friend inline std::ostream& operator<<(std::ostream& out, const JGauss_t& gauss)
152  {
153  using namespace std;
154 
155  return out << FIXED(7,3) << gauss.mean << ' '
156  << FIXED(7,3) << gauss.sigma << ' '
157  << FIXED(9,3) << gauss.signal << ' '
158  << FIXED(9,3) << gauss.background;
159  }
160 
161  double mean;
162  double sigma;
163  double signal;
164  double background;
165  };
166 
167 
168  /**
169  * Gauss function object.
170  *
171  * Evaluates function, derivative and gradient values.
172  */
173  struct JGauss :
174  public JGauss_t
175  {
176  /**
177  * Type definition of fit parameter.
178  */
179  typedef double JGauss::*parameter_type;
180 
181 
182  /**
183  * Default constructor.
184  */
185  JGauss() :
186  JGauss_t()
187  {}
188 
189 
190  /**
191  * Copy constructor.
192  *
193  * \param gauss gauss
194  */
196  JGauss_t(gauss)
197  {}
198 
199 
200  /**
201  * Constructor.
202  *
203  * \param mean mean
204  * \param sigma sigma
205  * \param signal signal
206  * \param background background
207  */
208  JGauss(const double mean,
209  const double sigma,
210  const double signal = 1.0,
211  const double background = 0.0) :
212  JGauss_t(mean, sigma, signal, background)
213  {}
214 
215 
216  /**
217  * Function value.
218  *
219  * \param x abscissa value
220  * \return function value
221  */
222  double getValue(const double x) const
223  {
224  const double u = (x - mean) / sigma;
225 
226  return get_signal(u) + background;
227  }
228 
229 
230  /**
231  * Derivative value.
232  *
233  * \param x abscissa value
234  * \return derivative value
235  */
236  double getDerivative(const double x) const
237  {
238  const double u = (x - mean) / sigma;
239 
240  return get_signal(u) * -u;
241  }
242 
243 
244  /**
245  * Function value.
246  *
247  * \param x abscissa value
248  * \return function value
249  */
250  double operator()(const double x) const
251  {
252  return getValue(x);
253  }
254 
255 
256  /**
257  * Get gradient.
258  *
259  * \param x abscissa value
260  * \return gradient
261  */
262  JGauss_t getGradient(const double x) const
263  {
264  JGauss_t gradient;
265 
266  const double u = (x - mean) / sigma;
267  const double fs = get_signal(u);
268 
269  gradient.mean = fs * u / sigma; // d(f)/d(mean)
270  gradient.sigma = fs * u*u / sigma - fs / sigma; // d(f)/d(sigma)
271  gradient.signal = fs / signal; // d(f)/d(signal)
272  gradient.background = 1.0; // d(f)/d(background)
273 
274  return gradient;
275  }
276 
277  private:
278  /**
279  * Get signal.
280  *
281  * \param u value
282  * \return signal
283  */
284  inline double get_signal(const double u) const
285  {
286  return signal * exp(-0.5*u*u) / (sqrt(2.0*PI) * sigma);
287  }
288  };
289 }
290 
291 #endif
bool equals(const JGauss_t &gauss, const double eps=std::numeric_limits< double >::min()) const
Equality.
Definition: JGauss.hh:70
friend std::istream & operator>>(std::istream &in, JGauss_t &gauss)
Write Gauss to input stream.
Definition: JGauss.hh:138
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Definition: JScale.hh:47
double mean
Definition: JGauss.hh:161
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:109
JGauss_t getGradient(const double x) const
Get gradient.
Definition: JGauss.hh:262
double operator()(const double x) const
Function value.
Definition: JGauss.hh:250
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
Gauss function object.
Definition: JGauss.hh:173
JGauss_t & sub(const JGauss_t &gauss)
Subtract gauss.
Definition: JGauss.hh:103
double get_signal(const double u) const
Get signal.
Definition: JGauss.hh:284
JGauss_t & mul(const double factor)
Scale gauss.
Definition: JGauss.hh:120
friend std::ostream & operator<<(std::ostream &out, const JGauss_t &gauss)
Write Gauss to output stream.
Definition: JGauss.hh:151
JGauss(const double mean, const double sigma, const double signal=1.0, const double background=0.0)
Constructor.
Definition: JGauss.hh:208
Mathematical constants.
double JGauss::* parameter_type
Type definition of fit parameter.
Definition: JGauss.hh:179
Template definition of auxiliary base class for comparison of data structures.
Definition: JEquals.hh:24
JGauss_t & add(const JGauss_t &gauss)
Add gauss.
Definition: JGauss.hh:86
JGauss()
Default constructor.
Definition: JGauss.hh:185
Gauss model.
Definition: JGauss.hh:29
static const double PI
Mathematical constants.
JGauss_t()
Default constructor.
Definition: JGauss.hh:36
JGauss(const JGauss_t &gauss)
Copy constructor.
Definition: JGauss.hh:195
JGauss_t(const double mean, const double sigma, const double signal, const double background)
Constructor.
Definition: JGauss.hh:52
I/O manipulators.
double signal
Definition: JGauss.hh:163
double background
Definition: JGauss.hh:164
then set_variable NUMBER_OF_TESTS else set_variable NUMBER_OF_TESTS fi function gauss()
double sigma
Definition: JGauss.hh:162
Base class for data structures with artithmetic capabilities.
double getValue(const double x) const
Function value.
Definition: JGauss.hh:222
double getDerivative(const double x) const
Derivative value.
Definition: JGauss.hh:236
double u[N+1]
Definition: JPolint.hh:776
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable STRING $argv[2] set_array QUANTILES set_variable FORMULA *[0] exp(-0.5 *(x-[1])*(x-[1])/([2]*[2]))" set_variable MODULE `getModule -a $DETECTOR -L "$STRING 0"` source JAcousticsToolkit.sh typeset -A TRIPODS get_tripods $WORKDIR/tripod.txt TRIPODS XMEAN