General Purpose Geodetic Library
Sg3dMatrix.h
Go to the documentation of this file.
1 /*
2  *
3  * This file is a part of Space Geodetic Library. The library is used by
4  * nuSolve, a part of CALC/SOLVE system, and designed to make analysis of
5  * geodetic VLBI observations.
6  * Copyright (C) 2010-2020 Sergei Bolotin.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef SG_3D_MATRIX
24 #define SG_3D_MATRIX
25 
26 
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30 
31 
32 #include <ostream>
33 #include <SgMathSupport.h>
34 #include <Sg3dVector.h>
35 
36 
37 /***===================================================================================================*/
43 /*=====================================================================================================*/
45 {
46 protected:
47  double mat[3][3];
48 
49 public:
50  //
51  // constructors/destructors:
52  //
56  inline Sg3dMatrix();
57 
61  inline Sg3dMatrix(const Sg3dMatrix&);
62 
66  inline Sg3dMatrix(const Sg3dVector& col0, const Sg3dVector& col1, const Sg3dVector& col2);
67 
71  inline Sg3dMatrix(double a00, double a01, double a02,
72  double a10, double a11, double a12,
73  double a20, double a21, double a22);
74 
78  inline ~Sg3dMatrix(){};
79 
80 
81  //
82  // Interfaces:
83  //
88  inline double& operator()(DIRECTION i, DIRECTION j) {return mat[i][j];};
89 
93  inline double at (DIRECTION i, DIRECTION j) const {return mat[i][j];};
94 
95  //
96  // Functions:
97  //
100  inline double module() const; /* determinant */
101 
104  inline void unify() {operator/=(module());};
105 
108  inline Sg3dMatrix T() const; /* transpose: */
109 
112  inline Sg3dMatrix& operator = (const Sg3dMatrix&);
113 
116  inline Sg3dMatrix& operator+= (const Sg3dMatrix&);
117 
120  inline Sg3dMatrix& operator-= (const Sg3dMatrix&);
121 
124  inline Sg3dMatrix& operator*= (double);
125 
128  inline Sg3dMatrix& operator/= (double);
129 
132  friend inline Sg3dMatrix operator+ (const Sg3dMatrix&, const Sg3dMatrix&);
133 
136  friend inline Sg3dMatrix operator- (const Sg3dMatrix&, const Sg3dMatrix&);
137 
140  friend inline Sg3dMatrix operator/ (const Sg3dMatrix&, double);
141 
144  friend inline Sg3dMatrix operator* (const Sg3dMatrix&, double);
145 
148  friend inline Sg3dMatrix operator* (double, const Sg3dMatrix&);
149 
152  friend inline Sg3dMatrix operator- (const Sg3dMatrix&);
153 
156  friend inline Sg3dVector operator* (const Sg3dMatrix&, const Sg3dVector&);
157 
160  friend Sg3dMatrix operator* (const Sg3dMatrix&, const Sg3dMatrix&);
161 
164  friend Sg3dMatrix operator~ (const Sg3dMatrix&);
165 
166 
167  //
168  // I/O:
169  //
172  friend std::ostream &operator<<(std::ostream& s, const Sg3dMatrix& M);
173 };
174 /*=====================================================================================================*/
175 
176 
177 
178 /*=====================================================================================================*/
179 /* */
180 /* Sg3dMatrix's inline members: */
181 /* */
182 /*=====================================================================================================*/
184 {
185  mat[0][0]=1.0; mat[0][1]=0.0; mat[0][2]=0.0;
186  mat[1][0]=0.0; mat[1][1]=1.0; mat[1][2]=0.0;
187  mat[2][0]=0.0; mat[2][1]=0.0; mat[2][2]=1.0;
188 };
189 
190 inline Sg3dMatrix::Sg3dMatrix(const Sg3dVector& col0, const Sg3dVector& col1, const Sg3dVector& col2)
191 {
192  mat[0][0]=col0.at(X_AXIS); mat[0][1]=col1.at(X_AXIS); mat[0][2]=col2.at(X_AXIS);
193  mat[1][0]=col0.at(Y_AXIS); mat[1][1]=col1.at(Y_AXIS); mat[1][2]=col2.at(Y_AXIS);
194  mat[2][0]=col0.at(Z_AXIS); mat[2][1]=col1.at(Z_AXIS); mat[2][2]=col2.at(Z_AXIS);
195 };
196 
197 inline Sg3dMatrix::Sg3dMatrix(double a00, double a01, double a02,
198  double a10, double a11, double a12,
199  double a20, double a21, double a22)
200 {
201  mat[0][0]=a00; mat[0][1]=a01; mat[0][2]=a02;
202  mat[1][0]=a10; mat[1][1]=a11; mat[1][2]=a12;
203  mat[2][0]=a20; mat[2][1]=a21; mat[2][2]=a22;
204 };
205 
207 {
208  mat[0][0]=M.mat[0][0]; mat[0][1]=M.mat[0][1]; mat[0][2]=M.mat[0][2];
209  mat[1][0]=M.mat[1][0]; mat[1][1]=M.mat[1][1]; mat[1][2]=M.mat[1][2];
210  mat[2][0]=M.mat[2][0]; mat[2][1]=M.mat[2][1]; mat[2][2]=M.mat[2][2];
211 };
212 
214 {
215  mat[0][0]=M.mat[0][0]; mat[0][1]=M.mat[0][1]; mat[0][2]=M.mat[0][2];
216  mat[1][0]=M.mat[1][0]; mat[1][1]=M.mat[1][1]; mat[1][2]=M.mat[1][2];
217  mat[2][0]=M.mat[2][0]; mat[2][1]=M.mat[2][1]; mat[2][2]=M.mat[2][2];
218  return *this;
219 };
220 
222 {
223  mat[0][0]+=M.mat[0][0]; mat[0][1]+=M.mat[0][1]; mat[0][2]+=M.mat[0][2];
224  mat[1][0]+=M.mat[1][0]; mat[1][1]+=M.mat[1][1]; mat[1][2]+=M.mat[1][2];
225  mat[2][0]+=M.mat[2][0]; mat[2][1]+=M.mat[2][1]; mat[2][2]+=M.mat[2][2];
226  return *this;
227 };
228 
230 {
231  mat[0][0]-=M.mat[0][0]; mat[0][1]-=M.mat[0][1]; mat[0][2]-=M.mat[0][2];
232  mat[1][0]-=M.mat[1][0]; mat[1][1]-=M.mat[1][1]; mat[1][2]-=M.mat[1][2];
233  mat[2][0]-=M.mat[2][0]; mat[2][1]-=M.mat[2][1]; mat[2][2]-=M.mat[2][2];
234  return *this;
235 };
236 
238 {
239  mat[0][0]*=v; mat[0][1]*=v; mat[0][2]*=v;
240  mat[1][0]*=v; mat[1][1]*=v; mat[1][2]*=v;
241  mat[2][0]*=v; mat[2][1]*=v; mat[2][2]*=v;
242  return *this;
243 };
244 
246 {
247  mat[0][0]/=v; mat[0][1]/=v; mat[0][2]/=v;
248  mat[1][0]/=v; mat[1][1]/=v; mat[1][2]/=v;
249  mat[2][0]/=v; mat[2][1]/=v; mat[2][2]/=v;
250  return *this;
251 };
252 
253 inline double Sg3dMatrix::module() const
254 {
255  return
256  mat[0][0]*(mat[1][1]*mat[2][2] - mat[1][2]*mat[2][1]) -
257  mat[0][1]*(mat[1][0]*mat[2][2] - mat[1][2]*mat[2][0]) +
258  mat[0][2]*(mat[1][0]*mat[2][1] - mat[1][1]*mat[2][0]) ;
259 };
260 
261 inline Sg3dMatrix Sg3dMatrix::T() const
262 {
263  return Sg3dMatrix(mat[0][0], mat[1][0], mat[2][0],
264  mat[0][1], mat[1][1], mat[2][1],
265  mat[0][2], mat[1][2], mat[2][2]);
266 };
267 /*=====================================================================================================*/
268 
269 
270 
271 /*=====================================================================================================*/
272 /* */
273 /* Sg3dMatrix's inline friends: */
274 /* */
275 /*=====================================================================================================*/
277 {
278  Sg3dMatrix M;
279  M.mat[0][0] = -M1.mat[0][0];
280  M.mat[0][1] = -M1.mat[0][1];
281  M.mat[0][2] = -M1.mat[0][2];
282 
283  M.mat[1][0] = -M1.mat[1][0];
284  M.mat[1][1] = -M1.mat[1][1];
285  M.mat[1][2] = -M1.mat[1][2];
286 
287  M.mat[2][0] = -M1.mat[2][0];
288  M.mat[2][1] = -M1.mat[2][1];
289  M.mat[2][2] = -M1.mat[2][2];
290  return M;
291 };
292 inline Sg3dMatrix operator+ (const Sg3dMatrix& M1, const Sg3dMatrix& M2)
293 {
294  Sg3dMatrix M(M1);
295  M.mat[0][0]+= M2.mat[0][0];
296  M.mat[0][1]+= M2.mat[0][1];
297  M.mat[0][2]+= M2.mat[0][2];
298 
299  M.mat[1][0]+= M2.mat[1][0];
300  M.mat[1][1]+= M2.mat[1][1];
301  M.mat[1][2]+= M2.mat[1][2];
302 
303  M.mat[2][0]+= M2.mat[2][0];
304  M.mat[2][1]+= M2.mat[2][1];
305  M.mat[2][2]+= M2.mat[2][2];
306  return M;
307 };
308 
309 inline Sg3dMatrix operator- (const Sg3dMatrix& M1, const Sg3dMatrix& M2)
310 {
311  Sg3dMatrix M(M1);
312  M.mat[0][0]-= M2.mat[0][0];
313  M.mat[0][1]-= M2.mat[0][1];
314  M.mat[0][2]-= M2.mat[0][2];
315 
316  M.mat[1][0]-= M2.mat[1][0];
317  M.mat[1][1]-= M2.mat[1][1];
318  M.mat[1][2]-= M2.mat[1][2];
319 
320  M.mat[2][0]-= M2.mat[2][0];
321  M.mat[2][1]-= M2.mat[2][1];
322  M.mat[2][2]-= M2.mat[2][2];
323  return M;
324 };
325 
326 inline Sg3dMatrix operator/ (const Sg3dMatrix& M1, double v2)
327 {
328  Sg3dMatrix M(M1);
329  M.mat[0][0]/=v2; M.mat[0][1]/=v2; M.mat[0][2]/=v2;
330  M.mat[1][0]/=v2; M.mat[1][1]/=v2; M.mat[1][2]/=v2;
331  M.mat[2][0]/=v2; M.mat[2][1]/=v2; M.mat[2][2]/=v2;
332  return M;
333 };
334 
335 inline Sg3dMatrix operator* (double v1, const Sg3dMatrix& M2)
336 {
337  Sg3dMatrix M(M2);
338  M.mat[0][0]*=v1; M.mat[0][1]*=v1; M.mat[0][2]*=v1;
339  M.mat[1][0]*=v1; M.mat[1][1]*=v1; M.mat[1][2]*=v1;
340  M.mat[2][0]*=v1; M.mat[2][1]*=v1; M.mat[2][2]*=v1;
341  return M;
342 };
343 
344 inline Sg3dMatrix operator* (const Sg3dMatrix& M1, double v2)
345 {
346  Sg3dMatrix M(v2*M1);
347  return M;
348 };
349 
350 inline Sg3dVector operator*(const Sg3dMatrix& M, const Sg3dVector& V)
351 {
352  return
353  Sg3dVector(M.mat[0][0]*V.vec[0] + M.mat[0][1]*V.vec[1] + M.mat[0][2]*V.vec[2],
354  M.mat[1][0]*V.vec[0] + M.mat[1][1]*V.vec[1] + M.mat[1][2]*V.vec[2],
355  M.mat[2][0]*V.vec[0] + M.mat[2][1]*V.vec[1] + M.mat[2][2]*V.vec[2]);
356 };
357 
358 inline std::ostream &operator<<(std::ostream& s, const Sg3dMatrix& M)
359 {
360  s << "|" << M.mat[0][0] << ", " << M.mat[0][1] << ", " << M.mat[0][2] << "|" << std::endl;
361  s << "|" << M.mat[1][0] << ", " << M.mat[1][1] << ", " << M.mat[1][2] << "|" << std::endl;
362  s << "|" << M.mat[2][0] << ", " << M.mat[2][1] << ", " << M.mat[2][2] << "|" << std::endl;
363  return s;
364 };
365 //*====================================================================================================*/
366 
367 #endif //SG_3D_MATRIX
Sg3dMatrix operator+(const Sg3dMatrix &M1, const Sg3dMatrix &M2)
Definition: Sg3dMatrix.h:292
Sg3dMatrix operator-(const Sg3dMatrix &M1)
Definition: Sg3dMatrix.h:276
Sg3dMatrix operator*(double v1, const Sg3dMatrix &M2)
Definition: Sg3dMatrix.h:335
std::ostream & operator<<(std::ostream &s, const Sg3dMatrix &M)
Definition: Sg3dMatrix.h:358
Sg3dMatrix operator/(const Sg3dMatrix &M1, double v2)
Definition: Sg3dMatrix.h:326
DIRECTION
Definition: SgMathSupport.h:81
@ Z_AXIS
Definition: SgMathSupport.h:81
@ X_AXIS
Definition: SgMathSupport.h:81
@ Y_AXIS
Definition: SgMathSupport.h:81
Sg3dMatrix & operator-=(const Sg3dMatrix &)
Definition: Sg3dMatrix.h:229
double mat[3][3]
The 3x3 array that is storing elements of a matrix.
Definition: Sg3dMatrix.h:47
double module() const
Definition: Sg3dMatrix.h:253
friend Sg3dMatrix operator~(const Sg3dMatrix &)
Definition: Sg3dMatrix.cpp:83
friend std::ostream & operator<<(std::ostream &s, const Sg3dMatrix &M)
Definition: Sg3dMatrix.h:358
Sg3dMatrix & operator*=(double)
Definition: Sg3dMatrix.h:237
friend Sg3dMatrix operator*(const Sg3dMatrix &, double)
Definition: Sg3dMatrix.h:344
Sg3dMatrix & operator+=(const Sg3dMatrix &)
Definition: Sg3dMatrix.h:221
double & operator()(DIRECTION i, DIRECTION j)
Definition: Sg3dMatrix.h:88
double at(DIRECTION i, DIRECTION j) const
Definition: Sg3dMatrix.h:93
Sg3dMatrix T() const
Definition: Sg3dMatrix.h:261
Sg3dMatrix & operator/=(double)
Definition: Sg3dMatrix.h:245
Sg3dMatrix & operator=(const Sg3dMatrix &)
Definition: Sg3dMatrix.h:213
friend Sg3dMatrix operator+(const Sg3dMatrix &, const Sg3dMatrix &)
Definition: Sg3dMatrix.h:292
friend Sg3dMatrix operator-(const Sg3dMatrix &, const Sg3dMatrix &)
Definition: Sg3dMatrix.h:309
void unify()
Definition: Sg3dMatrix.h:104
friend Sg3dMatrix operator/(const Sg3dMatrix &, double)
Definition: Sg3dMatrix.h:326
double vec[3]
An array of 3 elements, stores elements of a vector.
Definition: Sg3dVector.h:51
double at(DIRECTION i) const
Definition: Sg3dVector.h:91