Jpp 20.0.0-27-g39925593c-D
the software that should make you happy
Loading...
Searching...
No Matches
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
15namespace JMATH {}
16namespace JPP { using namespace JMATH; }
17
18namespace JMATH {
19
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 != (double (*)(double)) sin && f1 != (double (*)(double)) 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 == (double (*)(double)) sin)
109 return JTrigonometric(cos, +factor);
110 else if (f1 == (double (*)(double)) 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 == (double (*)(double)) sin)
125 return JTrigonometric(cos, -factor);
126 else if (f1 == (double (*)(double)) 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 == (double (*)(double)) sin)
169 out << ' ' << "sin";
170 else if (object.f1 == (double (*)(double)) 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
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
General exception.
Definition JException.hh:24
Trigonometric function object for sin and cos.
JTrigonometric getIntegral() const
Integral function.
JTrigonometric(pF f1, const double factor=1.0)
Constructor.
double getDerivative(const double x) const
Derivative value.
double operator()(const double x) const
Function value.
JTrigonometric getDerivative() const
Derivative function.
double getIntegral(const double x) const
Integral value.
double(* pF)(double)
Type definition of pointer to trigonometric function.
double getValue(const double x) const
Function value.
friend std::istream & operator>>(std::istream &in, JTrigonometric &object)
Read trigonometric from input.
friend std::ostream & operator<<(std::ostream &out, const JTrigonometric &object)
Write trigonometric to output.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).