]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/math/FGMatrix33.h
Merge branch 'jmt/units-fix' into maint
[flightgear.git] / src / FDM / JSBSim / math / FGMatrix33.h
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Header: FGMatrix33.h
4 Author: Tony Peden, Jon Berndt, Mathias Frolich
5 Date started: Unknown
6
7  ------------- Copyright (C) 2001  Jon S. Berndt (jon@jsbsim.org) -------------
8
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
17  details.
18
19  You should have received a copy of the GNU Lesser General Public License along with
20  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21  Place - Suite 330, Boston, MA  02111-1307, USA.
22
23  Further information about the GNU Lesser General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25
26 HISTORY
27 --------------------------------------------------------------------------------
28 ??/??/??   TP   Created
29 03/16/2000 JSB  Added exception throwing
30 03/06/2004 MF   Rework of the code to make it a bit compiler friendlier
31
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 SENTRY
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
35
36 #ifndef FGMATRIX33_H
37 #define FGMATRIX33_H
38
39 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 INCLUDES
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
42
43 #include <string>
44 #include <iosfwd>
45
46 #include "FGColumnVector3.h"
47 #include "FGJSBBase.h"
48
49 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50 DEFINITIONS
51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
52
53 #define ID_MATRIX33 "$Id$"
54
55 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 FORWARD DECLARATIONS
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
58
59 namespace JSBSim {
60
61 class FGColumnVector3;
62
63 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 CLASS DOCUMENTATION
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
66
67 /** Exception convenience class.
68   */
69
70 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 DECLARATION: MatrixException
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
73
74 class MatrixException : public FGJSBBase
75 {
76 public:
77   std::string Message;
78 };
79
80 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 CLASS DOCUMENTATION
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
83
84   /** Handles matrix math operations.
85       @author Tony Peden, Jon Berndt, Mathias Froelich
86   */
87
88 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 DECLARATION: FGMatrix33
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
91
92 class FGMatrix33 : public FGJSBBase
93 {
94 public:
95
96   enum {
97     eRows = 3,
98     eColumns = 3
99   };
100
101   /** Default initializer.
102
103       Create a zero matrix.
104    */
105   FGMatrix33(void);
106
107   /** Copy constructor.
108
109       @param M Matrix which is used for initialization.
110
111       Create copy of the matrix given in the argument.
112    */
113   FGMatrix33(const FGMatrix33& M) {
114     Entry(1,1) = M.Entry(1,1);
115     Entry(2,1) = M.Entry(2,1);
116     Entry(3,1) = M.Entry(3,1);
117     Entry(1,2) = M.Entry(1,2);
118     Entry(2,2) = M.Entry(2,2);
119     Entry(3,2) = M.Entry(3,2);
120     Entry(1,3) = M.Entry(1,3);
121     Entry(2,3) = M.Entry(2,3);
122     Entry(3,3) = M.Entry(3,3);
123
124     Debug(0);
125   }
126
127   /** Initialization by given values.
128
129       @param m11 value of the 1,1 Matrix element.
130       @param m12 value of the 1,2 Matrix element.
131       @param m13 value of the 1,3 Matrix element.
132       @param m21 value of the 2,1 Matrix element.
133       @param m22 value of the 2,2 Matrix element.
134       @param m23 value of the 2,3 Matrix element.
135       @param m31 value of the 3,1 Matrix element.
136       @param m32 value of the 3,2 Matrix element.
137       @param m33 value of the 3,3 Matrix element.
138
139       Create a matrix from the doubles given in the arguments.
140    */
141   FGMatrix33(double m11, double m12, double m13,
142              double m21, double m22, double m23,
143              double m31, double m32, double m33) {
144     Entry(1,1) = m11;
145     Entry(2,1) = m21;
146     Entry(3,1) = m31;
147     Entry(1,2) = m12;
148     Entry(2,2) = m22;
149     Entry(3,2) = m32;
150     Entry(1,3) = m13;
151     Entry(2,3) = m23;
152     Entry(3,3) = m33;
153
154     Debug(0);
155   }
156
157   /** Destructor.
158    */
159   ~FGMatrix33(void) { Debug(1); }
160
161   /** Prints the contents of the matrix.
162       @param delimeter the item separator (tab or comma)
163       @return a string with the delimeter-separated contents of the matrix  */
164   std::string Dump(const std::string& delimeter) const;
165
166   /** Read access the entries of the matrix.
167       @param row Row index.
168       @param col Column index.
169
170       @return the value of the matrix entry at the given row and
171       column indices. Indices are counted starting with 1.
172    */
173   double operator()(unsigned int row, unsigned int col) const {
174     return Entry(row, col);
175   }
176
177   /** Write access the entries of the matrix.
178       Note that the indices given in the arguments are unchecked.
179
180       @param row Row index.
181       @param col Column index.
182
183       @return a reference to the matrix entry at the given row and
184       column indices. Indices are counted starting with 1.
185    */
186   double& operator()(unsigned int row, unsigned int col) {
187     return Entry(row, col);
188   }
189
190   /** Read access the entries of the matrix.
191       This function is just a shortcut for the <tt>double&
192       operator()(unsigned int row, unsigned int col)</tt> function. It is
193       used internally to access the elements in a more convenient way.
194
195       Note that the indices given in the arguments are unchecked.
196
197       @param row Row index.
198       @param col Column index.
199
200       @return the value of the matrix entry at the given row and
201       column indices. Indices are counted starting with 1.
202    */
203   double Entry(unsigned int row, unsigned int col) const {
204     return data[(col-1)*eRows+row-1];
205   }
206
207   /** Write access the entries of the matrix.
208       This function is just a shortcut for the <tt>double&
209       operator()(unsigned int row, unsigned int col)</tt> function. It is
210       used internally to access the elements in a more convenient way.
211
212       Note that the indices given in the arguments are unchecked.
213
214       @param row Row index.
215       @param col Column index.
216
217       @return a reference to the matrix entry at the given row and
218       column indices. Indices are counted starting with 1.
219    */
220    double& Entry(unsigned int row, unsigned int col) {
221      return data[(col-1)*eRows+row-1];
222    }
223
224   /** Number of rows in the matrix.
225       @return the number of rows in the matrix.
226    */
227    unsigned int Rows(void) const { return eRows; }
228
229   /** Number of cloumns in the matrix.
230       @return the number of columns in the matrix.
231    */
232    unsigned int Cols(void) const { return eColumns; }
233
234   /** Transposed matrix.
235       This function only returns the transpose of this matrix. This matrix itself
236       remains unchanged.
237       @return the transposed matrix.
238    */
239   FGMatrix33 Transposed(void) const {
240     return FGMatrix33( Entry(1,1), Entry(2,1), Entry(3,1),
241                        Entry(1,2), Entry(2,2), Entry(3,2),
242                        Entry(1,3), Entry(2,3), Entry(3,3) );
243   }
244
245   /** Transposes this matrix.
246       This function only transposes this matrix. Nothing is returned.
247    */
248   void T(void);
249
250 /** Initialize the matrix.
251     This function initializes a matrix to all 0.0.
252  */
253   void InitMatrix(void);
254
255 /** Initialize the matrix.
256     This function initializes a matrix to user specified values.
257  */
258   void InitMatrix(double m11, double m12, double m13,
259                   double m21, double m22, double m23,
260                   double m31, double m32, double m33) {
261     Entry(1,1) = m11;
262     Entry(2,1) = m21;
263     Entry(3,1) = m31;
264     Entry(1,2) = m12;
265     Entry(2,2) = m22;
266     Entry(3,2) = m32;
267     Entry(1,3) = m13;
268     Entry(2,3) = m23;
269     Entry(3,3) = m33;
270   }
271
272   /** Determinant of the matrix.
273       @return the determinant of the matrix.
274    */
275   double Determinant(void) const;
276
277   /** Return if the matrix is invertible.
278       Checks and returns if the matrix is nonsingular and thus
279       invertible. This is done by simply computing the determinant and
280       check if it is zero. Note that this test does not cover any
281       instabilities caused by nearly singular matirces using finite
282       arithmetics. It only checks exact singularity.
283    */
284   bool Invertible(void) const { return 0.0 != Determinant(); }
285
286   /** Return the inverse of the matrix.
287       Computes and returns if the inverse of the matrix. It is computed
288       by Cramers Rule. Also there are no checks performed if the matrix
289       is invertible. If you are not sure that it really is check this
290       with the @ref Invertible() call before.
291    */
292   FGMatrix33 Inverse(void) const;
293
294   /** Assignment operator.
295
296       @param A source matrix.
297
298       Copy the content of the matrix given in the argument into *this.
299    */
300   FGMatrix33& operator=(const FGMatrix33& A) {
301     data[0] = A.data[0];
302     data[1] = A.data[1];
303     data[2] = A.data[2];
304     data[3] = A.data[3];
305     data[4] = A.data[4];
306     data[5] = A.data[5];
307     data[6] = A.data[6];
308     data[7] = A.data[7];
309     data[8] = A.data[8];
310     return *this;
311   }
312
313   /** Matrix vector multiplication.
314
315       @param v vector to multiply with.
316       @return matric vector product.
317
318       Compute and return the product of the current matrix with the
319       vector given in the argument.
320    */
321   FGColumnVector3 operator*(const FGColumnVector3& v) const;
322
323   /** Matrix subtraction.
324
325       @param B matrix to add to.
326       @return difference of the matrices.
327
328       Compute and return the sum of the current matrix and the matrix
329       B given in the argument.
330   */
331   FGMatrix33 operator-(const FGMatrix33& B) const;
332
333   /** Matrix addition.
334
335       @param B matrix to add to.
336       @return sum of the matrices.
337
338       Compute and return the sum of the current matrix and the matrix
339       B given in the argument.
340   */
341   FGMatrix33 operator+(const FGMatrix33& B) const;
342
343   /** Matrix product.
344
345       @param B matrix to add to.
346       @return product of the matrices.
347
348       Compute and return the product of the current matrix and the matrix
349       B given in the argument.
350   */
351   FGMatrix33 operator*(const FGMatrix33& B) const;
352
353   /** Multiply the matrix with a scalar.
354
355       @param scalar scalar factor to multiply with.
356       @return scaled matrix.
357
358       Compute and return the product of the current matrix with the
359       scalar value scalar given in the argument.
360   */
361   FGMatrix33 operator*(const double scalar) const;
362
363   /** Multiply the matrix with 1.0/scalar.
364
365       @param scalar scalar factor to divide through.
366       @return scaled matrix.
367
368       Compute and return the product of the current matrix with the
369       scalar value 1.0/scalar, where scalar is given in the argument.
370   */
371   FGMatrix33 operator/(const double scalar) const;
372
373   /** In place matrix subtraction.
374
375       @param B matrix to subtract.
376       @return reference to the current matrix.
377
378       Compute the diffence from the current matrix and the matrix B
379       given in the argument.
380   */
381   FGMatrix33& operator-=(const FGMatrix33 &B);
382
383   /** In place matrix addition.
384
385       @param B matrix to add.
386       @return reference to the current matrix.
387
388       Compute the sum of the current matrix and the matrix B
389       given in the argument.
390   */
391   FGMatrix33& operator+=(const FGMatrix33 &B);
392
393   /** In place matrix multiplication.
394
395       @param B matrix to multiply with.
396       @return reference to the current matrix.
397
398       Compute the product of the current matrix and the matrix B
399       given in the argument.
400   */
401   FGMatrix33& operator*=(const FGMatrix33 &B);
402
403   /** In place matrix scale.
404
405       @param scalar scalar value to multiply with.
406       @return reference to the current matrix.
407
408       Compute the product of the current matrix and the scalar value scalar
409       given in the argument.
410   */
411   FGMatrix33& operator*=(const double scalar);
412
413   /** In place matrix scale.
414
415       @param scalar scalar value to divide through.
416       @return reference to the current matrix.
417
418       Compute the product of the current matrix and the scalar value
419       1.0/scalar, where scalar is given in the argument.
420   */
421   FGMatrix33& operator/=(const double scalar);
422
423 private:
424   double data[eRows*eColumns];
425
426   void Debug(int from);
427 };
428
429 /** Scalar multiplication.
430
431     @param scalar scalar value to multiply with.
432     @param A Matrix to multiply.
433
434     Multiply the Matrix with a scalar value.
435 */
436 inline FGMatrix33 operator*(double scalar, const FGMatrix33& A) {
437   // use already defined operation.
438   return A*scalar;
439 }
440
441 /** Write matrix to a stream.
442
443     @param os Stream to write to.
444     @param M Matrix to write.
445
446     Write the matrix to a stream.
447 */
448 std::ostream& operator<<(std::ostream& os, const FGMatrix33& M);
449
450 /** Read matrix from a stream.
451
452     @param os Stream to read from.
453     @param M Matrix to initialize with the values from the stream.
454
455     Read matrix from a stream.
456 */
457 std::istream& operator>>(std::istream& is, FGMatrix33& M);
458
459 } // namespace JSBSim
460
461 #endif