Jpp
JTrigonometric.hh
Go to the documentation of this file.
1 #ifndef __JMATH__JTRIGONOMETRIC__
2 #define __JMATH__JTRIGONOMETRIC__
3 
4 #include <istream>
5 #include <ostream>
6 #include <cmath>
7 
8 #include "JLang/JException.hh"
9 
10 
11 /**
12  * \author mdejong
13  */
14 
15 namespace JMATH {}
16 namespace JPP { using namespace JMATH; }
17 
18 namespace JMATH {
19 
20  using JLANG::JException;
21 
22 
23  /**
24  * Trigonometric function object for <tt>sin</tt> and <tt>cos</tt>.
25  *
26  * Evaluation of function, derivative and integral values.
27  */
29  public:
30  /**
31  * Type definition of pointer to trigonometric function.
32  */
33  typedef double (*pF)(double);
34 
35 
36  /**
37  * Constructor.
38  *
39  * \param f1 pointer to function
40  * \param factor multiplication factor
41  */
42  JTrigonometric(pF f1, const double factor = 1.0)
43  {
44  if (f1 != sin && f1 != cos) {
45  throw JException("Invalid trigonometric function.");
46  }
47 
48  this->f1 = f1;
49  this->factor = factor;
50  }
51 
52 
53  /**
54  * Function value.
55  *
56  * \param x abscissa value
57  * \return function value
58  */
59  double getValue(const double x) const
60  {
61  return factor * f1(x);
62  }
63 
64 
65  /**
66  * Derivative value.
67  *
68  * \param x abscissa value
69  * \return derivative value
70  */
71  double getDerivative(const double x) const
72  {
73  return getDerivative().getValue(x);
74  }
75 
76 
77  /**
78  * Integral value.
79  *
80  * \param x abscissa value
81  * \return integral value
82  */
83  double getIntegral(const double x) const
84  {
85  return getIntegral().getValue(x);
86  }
87 
88 
89  /**
90  * Function value.
91  *
92  * \param x abscissa value
93  * \return function value
94  */
95  double operator()(const double x) const
96  {
97  return getValue(x);
98  }
99 
100 
101  /**
102  * Derivative function.
103  *
104  * \return derivative function
105  */
107  {
108  if (f1 == sin)
109  return JTrigonometric(cos, +factor);
110  else if (f1 == cos)
111  return JTrigonometric(sin, -factor);
112  else
113  throw JException("Invalid trigonometric function.");
114  }
115 
116 
117  /**
118  * Integral function.
119  *
120  * \return integral function
121  */
123  {
124  if (f1 == sin)
125  return JTrigonometric(cos, -factor);
126  else if (f1 == cos)
127  return JTrigonometric(sin, +factor);
128  else
129  throw JException("Invalid trigonometric function.");
130  }
131 
132 
133  /**
134  * Read trigonometric from input.
135  *
136  * \param in input stream
137  * \param object trigonometric
138  * \return input stream
139  */
140  friend inline std::istream& operator>>(std::istream& in, JTrigonometric& object)
141  {
142  std::string buffer;
143 
144  if (in >> object.factor >> buffer) {
145  if (buffer == "sin")
146  object.f1 = sin;
147  else if (buffer == "cos")
148  object.f1 = cos;
149  else
150  throw JException("Invalid trigonometric function.");
151  }
152 
153  return in;
154  }
155 
156 
157  /**
158  * Write trigonometric to output.
159  *
160  * \param out output stream
161  * \param object trigonometric
162  * \return output stream
163  */
164  friend inline std::ostream& operator<<(std::ostream& out, const JTrigonometric& object)
165  {
166  out << object.factor;
167 
168  if (object.f1 == sin)
169  out << ' ' << "sin";
170  else if (object.f1 == cos)
171  out << ' ' << "cos";
172  else
173  throw JException("Invalid trigonometric function.");
174 
175  return out;
176  }
177 
178  protected:
180  double factor;
181  };
182 }
183 
184 #endif
JException.hh
JMATH::JTrigonometric::f1
pF f1
Definition: JTrigonometric.hh:179
JMATH::JTrigonometric::factor
double factor
Definition: JTrigonometric.hh:180
JMATH::JTrigonometric::JTrigonometric
JTrigonometric(pF f1, const double factor=1.0)
Constructor.
Definition: JTrigonometric.hh:42
JMATH::JTrigonometric::pF
double(* pF)(double)
Type definition of pointer to trigonometric function.
Definition: JTrigonometric.hh:33
JMATH::JTrigonometric::operator()
double operator()(const double x) const
Function value.
Definition: JTrigonometric.hh:95
JMATH::JTrigonometric::getIntegral
double getIntegral(const double x) const
Integral value.
Definition: JTrigonometric.hh:83
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JMATH::JTrigonometric::getDerivative
double getDerivative(const double x) const
Derivative value.
Definition: JTrigonometric.hh:71
JMATH::JTrigonometric::getDerivative
JTrigonometric getDerivative() const
Derivative function.
Definition: JTrigonometric.hh:106
JMATH::JTrigonometric::operator>>
friend std::istream & operator>>(std::istream &in, JTrigonometric &object)
Read trigonometric from input.
Definition: JTrigonometric.hh:140
JMATH
Auxiliary classes and methods for mathematical operations.
Definition: JCalculator.hh:9
JMATH::JTrigonometric::operator<<
friend std::ostream & operator<<(std::ostream &out, const JTrigonometric &object)
Write trigonometric to output.
Definition: JTrigonometric.hh:164
JMATH::JTrigonometric::getValue
double getValue(const double x) const
Function value.
Definition: JTrigonometric.hh:59
JMATH::JTrigonometric::getIntegral
JTrigonometric getIntegral() const
Integral function.
Definition: JTrigonometric.hh:122
JMATH::JTrigonometric
Trigonometric function object for sin and cos.
Definition: JTrigonometric.hh:28
JLANG::JException
General exception.
Definition: JException.hh:23