Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JMath/JMatrix2D.hh
Go to the documentation of this file.
1#ifndef __JMATRIX2D__
2#define __JMATRIX2D__
3
4#include <limits>
5#include <cmath>
6#include <ostream>
7#include <iomanip>
8
10#include "JLang/JEquals.hh"
11#include "JLang/JManip.hh"
12#include "JMath/JMath.hh"
13
14
15/**
16 * \author mdejong
17 */
18
19namespace JMATH {}
20namespace JPP { using namespace JMATH; }
21
22namespace JMATH {
23
24 using JIO::JReader;
25 using JIO::JWriter;
26 using JLANG::JEquals;
27
28
29 /**
30 * 2 x 2 matrix
31 */
32 class JMatrix2D :
33 public JMath <JMatrix2D>,
34 public JEquals<JMatrix2D>
35 {
36 public:
37
38 using JMath<JMatrix2D>::mul;
39
40
41 /**
42 * Default constructor.
43 */
45 a00(0.0), a01(0.0),
46 a10(0.0), a11(0.0)
47 {}
48
49
50 /**
51 * Contructor.
52 *
53 * \param __a00 (0,0)
54 * \param __a01 (0,1)
55 * \param __a10 (1,0)
56 * \param __a11 (1,1)
57 */
58 JMatrix2D(const double __a00, const double __a01,
59 const double __a10, const double __a11) :
60 a00(__a00), a01(__a01),
61 a10(__a10), a11(__a11)
62 {}
63
64
65 /**
66 * Get reference to unique instance of this class object.
67 *
68 * \return zero matrix
69 */
70 static const JMatrix2D& getInstance()
71 {
72 static JMatrix2D matrix;
73
74 return matrix;
75 }
76
77
78 /**
79 * Set to identity matrix.
80 *
81 * \return this matrix
82 */
84 {
85 a00 = 1.0; a01 = 0.0;
86 a10 = 0.0; a11 = 1.0;
87
88 return *this;
89 }
90
91
92 /**
93 * Get reference to unique instance of this class object.
94 *
95 * \return identity matrix
96 */
97 static const JMatrix2D& getIdentity()
98 {
99 static JMatrix2D matrix(JMatrix2D().setIdentity());
100
101 return matrix;
102 }
103
104
105 /**
106 * Set matrix.
107 *
108 * \param A matrix
109 */
110 void set(const JMatrix2D& A)
111 {
112 static_cast<JMatrix2D&>(*this) = A;
113 }
114
115
116 /**
117 * Set matrix to the null matrix.
118 *
119 * \return this matrix
120 */
122 {
123 *this = JMatrix2D();
124
125 return *this;
126 }
127
128
129 /**
130 * Transpose.
131 *
132 * \return this matrix
133 */
135 {
136 using std::swap;
137
138 swap(a10, a01);
139
140 return *this;
141 }
142
143
144 /**
145 * Negate matrix.
146 *
147 * \return -this matrix
148 */
150 {
151 a00 = -a00; a01 = -a01;
152 a10 = -a10; a11 = -a11;
153
154 return *this;
155 }
156
157
158 /**
159 * Matrix addition.
160 *
161 * \param A matrix
162 * \return this matrix + A
163 */
165 {
166 a00 += A.a00; a01 += A.a01;
167 a10 += A.a10; a11 += A.a11;
168
169 return *this;
170 }
171
172
173 /**
174 * Matrix subtraction.
175 *
176 * \param A matrix
177 * \return this matrix - A
178 */
180 {
181 a00 -= A.a00; a01 -= A.a01;
182 a10 -= A.a10; a11 -= A.a11;
183
184 return *this;
185 }
186
187
188 /**
189 * Scale matrix.
190 *
191 * \param factor factor
192 * \return this matrix * factor
193 */
194 JMatrix2D& mul(const double factor)
195 {
196 a00 *= factor; a01 *= factor;
197 a10 *= factor; a11 *= factor;
198
199 return *this;
200 }
201
202
203 /**
204 * Scale matrix.
205 *
206 * \param factor factor
207 * \return this matrix / factor
208 */
209 JMatrix2D& div(const double factor)
210 {
211 a00 /= factor; a01 /= factor;
212 a10 /= factor; a11 /= factor;
213
214 return *this;
215 }
216
217
218 /**
219 * Matrix multiplication.
220 *
221 * \param A matrix
222 * \param B matrix
223 * \return this matrix
224 */
226 const JMatrix2D& B)
227 {
228 a00 = A.a00 * B.a00 + A.a01 * B.a10;
229 a01 = A.a00 * B.a01 + A.a01 * B.a11;
230
231 a10 = A.a10 * B.a00 + A.a11 * B.a10;
232 a11 = A.a10 * B.a01 + A.a11 * B.a11;
233
234 return *this;
235 }
236
237
238 /**
239 * Equality.
240 *
241 * \param A matrix
242 * \param eps numerical precision
243 * \return true if matrices identical; else false
244 */
245 bool equals(const JMatrix2D& A,
246 const double eps = std::numeric_limits<double>::min()) const
247 {
248 return (fabs(a00 - A.a00) <= eps &&
249 fabs(a01 - A.a01) <= eps &&
250 fabs(a10 - A.a10) <= eps &&
251 fabs(a11 - A.a11) <= eps);
252 }
253
254
255 /**
256 * Test identity.
257 *
258 * \param eps numerical precision
259 * \return true if identity matrix; else false
260 */
261 bool isIdentity(const double eps = std::numeric_limits<double>::min()) const
262 {
263 return equals(getIdentity(), eps);
264 }
265
266
267 /**
268 * Get determinant of matrix.
269 *
270 * \return determinant of matrix
271 */
272 double getDeterminant() const
273 {
274 return a00 * a11 - a01 * a10;
275 }
276
277
278 /**
279 * Transform.
280 *
281 * \param __x x value
282 * \param __y y value
283 */
284 void transform(double& __x, double& __y) const
285 {
286 const double x = a00 * __x + a01 * __y;
287 const double y = a10 * __x + a11 * __y;
288
289 __x = x;
290 __y = y;
291 }
292
293
294 /**
295 * Read matrix from input.
296 *
297 * \param in reader
298 * \param matrix matrix
299 * \return reader
300 */
301 friend inline JReader& operator>>(JReader& in, JMatrix2D& matrix)
302 {
303 in >> matrix.a00; in >> matrix.a01;
304 in >> matrix.a10; in >> matrix.a11;
305
306 return in;
307 }
308
309
310 /**
311 * Write matrix to output.
312 *
313 * \param out writer
314 * \param matrix matrix
315 * \return writer
316 */
317 friend inline JWriter& operator<<(JWriter& out, const JMatrix2D& matrix)
318 {
319 out << matrix.a00; out << matrix.a01;
320 out << matrix.a10; out << matrix.a11;
321
322 return out;
323 }
324
325
326 /**
327 * Print ASCII formatted output.
328 *
329 * \param out output stream
330 * \param A matrix
331 * \return output stream
332 */
333 friend inline std::ostream& operator<<(std::ostream& out, const JMatrix2D& A)
334 {
335 using namespace std;
336
337 const JFormat format(out, getFormat<JMatrix2D>(JFormat_t(10, 3, std::ios::fixed | std::ios::showpos)));
338
339 out << format << A.a00 << ' ' << format << A.a01 << endl;
340 out << format << A.a10 << ' ' << format << A.a11 << endl;
341
342 return out;
343 }
344
345
346 double a00, a01;
347 double a10, a11;
348 };
349}
350
351#endif
I/O manipulators.
JFormat_t & getFormat()
Get format for given type.
Definition JManip.hh:682
Base class for data structures with artithmetic capabilities.
Interface for binary input.
Interface for binary output.
JMatrix2D & setIdentity()
Set to identity matrix.
static const JMatrix2D & getInstance()
Get reference to unique instance of this class object.
JMatrix2D & div(const double factor)
Scale matrix.
friend JWriter & operator<<(JWriter &out, const JMatrix2D &matrix)
Write matrix to output.
JMatrix2D(const double __a00, const double __a01, const double __a10, const double __a11)
Contructor.
JMatrix2D()
Default constructor.
JMatrix2D & mul(const double factor)
Scale matrix.
JMatrix2D & add(const JMatrix2D &A)
Matrix addition.
void set(const JMatrix2D &A)
Set matrix.
bool isIdentity(const double eps=std::numeric_limits< double >::min()) const
Test identity.
JMatrix2D & sub(const JMatrix2D &A)
Matrix subtraction.
JMatrix2D & reset()
Set matrix to the null matrix.
void transform(double &__x, double &__y) const
Transform.
JMatrix2D & mul(const JMatrix2D &A, const JMatrix2D &B)
Matrix multiplication.
JMatrix2D & negate()
Negate matrix.
static const JMatrix2D & getIdentity()
Get reference to unique instance of this class object.
bool equals(const JMatrix2D &A, const double eps=std::numeric_limits< double >::min()) const
Equality.
friend JReader & operator>>(JReader &in, JMatrix2D &matrix)
Read matrix from input.
double getDeterminant() const
Get determinant of matrix.
friend std::ostream & operator<<(std::ostream &out, const JMatrix2D &A)
Print ASCII formatted output.
JMatrix2D & transpose()
Transpose.
Auxiliary classes and methods for mathematical operations.
Definition JEigen3D.hh:88
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Data structure for format specifications.
Definition JManip.hh:524
Auxiliary class to temporarily define format specifications.
Definition JManip.hh:636
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