]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/math/FGMatrix33.h
Sync. w. JSBSim cvs
[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   /** Read access the entries of the matrix.
162       @param row Row index.
163       @param col Column index.
164
165       @return the value of the matrix entry at the given row and
166       column indices. Indices are counted starting with 1.
167    */
168   double operator()(unsigned int row, unsigned int col) const {
169     return Entry(row, col);
170   }
171
172   /** Write access the entries of the matrix.
173       Note that the indices given in the arguments are unchecked.
174
175       @param row Row index.
176       @param col Column index.
177
178       @return a reference to the matrix entry at the given row and
179       column indices. Indices are counted starting with 1.
180    */
181   double& operator()(unsigned int row, unsigned int col) {
182     return Entry(row, col);
183   }
184
185   /** Read access the entries of the matrix.
186       This function is just a shortcut for the <tt>double&
187       operator()(unsigned int row, unsigned int col)</tt> function. It is
188       used internally to access the elements in a more convenient way.
189
190       Note that the indices given in the arguments are unchecked.
191
192       @param row Row index.
193       @param col Column index.
194
195       @return the value of the matrix entry at the given row and
196       column indices. Indices are counted starting with 1.
197    */
198   double Entry(unsigned int row, unsigned int col) const {
199     return data[(col-1)*eRows+row-1];
200   }
201
202   /** Write access the entries of the matrix.
203       This function is just a shortcut for the <tt>double&
204       operator()(unsigned int row, unsigned int col)</tt> function. It is
205       used internally to access the elements in a more convenient way.
206
207       Note that the indices given in the arguments are unchecked.
208
209       @param row Row index.
210       @param col Column index.
211
212       @return a reference to the matrix entry at the given row and
213       column indices. Indices are counted starting with 1.
214    */
215    double& Entry(unsigned int row, unsigned int col) {
216      return data[(col-1)*eRows+row-1];
217    }
218
219   /** Number of rows in the matrix.
220       @return the number of rows in the matrix.
221    */
222    unsigned int Rows(void) const { return eRows; }
223
224   /** Number of cloumns in the matrix.
225       @return the number of columns in the matrix.
226    */
227    unsigned int Cols(void) const { return eColumns; }
228
229   /** Transposed matrix.
230       This function only returns the transpose of this matrix. This matrix itself
231       remains unchanged.
232       @return the transposed matrix.
233    */
234   FGMatrix33 Transposed(void) const {
235     return FGMatrix33( Entry(1,1), Entry(2,1), Entry(3,1),
236                        Entry(1,2), Entry(2,2), Entry(3,2),
237                        Entry(1,3), Entry(2,3), Entry(3,3) );
238   }
239
240   /** Transposes this matrix.
241       This function only transposes this matrix. Nothing is returned.
242    */
243   void T(void);
244
245 /** Initialize the matrix.
246     This function initializes a matrix to all 0.0.
247  */
248   void InitMatrix(void);
249
250 /** Initialize the matrix.
251     This function initializes a matrix to user specified values.
252  */
253   void InitMatrix(double m11, double m12, double m13,
254                   double m21, double m22, double m23,
255                   double m31, double m32, double m33) {
256     Entry(1,1) = m11;
257     Entry(2,1) = m21;
258     Entry(3,1) = m31;
259     Entry(1,2) = m12;
260     Entry(2,2) = m22;
261     Entry(3,2) = m32;
262     Entry(1,3) = m13;
263     Entry(2,3) = m23;
264     Entry(3,3) = m33;
265   }
266
267   /** Determinant of the matrix.
268       @return the determinant of the matrix.
269    */
270   double Determinant(void) const;
271
272   /** Return if the matrix is invertible.
273       Checks and returns if the matrix is nonsingular and thus
274       invertible. This is done by simply computing the determinant and
275       check if it is zero. Note that this test does not cover any
276       instabilities caused by nearly singular matirces using finite
277       arithmetics. It only checks exact singularity.
278    */
279   bool Invertible(void) const { return 0.0 != Determinant(); }
280
281   /** Return the inverse of the matrix.
282       Computes and returns if the inverse of the matrix. It is computed
283       by Cramers Rule. Also there are no checks performed if the matrix
284       is invertible. If you are not sure that it really is check this
285       with the @ref Invertible() call before.
286    */
287   FGMatrix33 Inverse(void) const;
288
289   /** Assignment operator.
290
291       @param A source matrix.
292
293       Copy the content of the matrix given in the argument into *this.
294    */
295   FGMatrix33& operator=(const FGMatrix33& A) {
296     data[0] = A.data[0];
297     data[1] = A.data[1];
298     data[2] = A.data[2];
299     data[3] = A.data[3];
300     data[4] = A.data[4];
301     data[5] = A.data[5];
302     data[6] = A.data[6];
303     data[7] = A.data[7];
304     data[8] = A.data[8];
305     return *this;
306   }
307
308   /** Matrix vector multiplication.
309
310       @param v vector to multiply with.
311       @return matric vector product.
312
313       Compute and return the product of the current matrix with the
314       vector given in the argument.
315    */
316   FGColumnVector3 operator*(const FGColumnVector3& v) const;
317
318   /** Matrix subtraction.
319
320       @param B matrix to add to.
321       @return difference of the matrices.
322
323       Compute and return the sum of the current matrix and the matrix
324       B given in the argument.
325   */
326   FGMatrix33 operator-(const FGMatrix33& B) const;
327
328   /** Matrix addition.
329
330       @param B matrix to add to.
331       @return sum of the matrices.
332
333       Compute and return the sum of the current matrix and the matrix
334       B given in the argument.
335   */
336   FGMatrix33 operator+(const FGMatrix33& B) const;
337
338   /** Matrix product.
339
340       @param B matrix to add to.
341       @return product of the matrices.
342
343       Compute and return the product of the current matrix and the matrix
344       B given in the argument.
345   */
346   FGMatrix33 operator*(const FGMatrix33& B) const;
347
348   /** Multiply the matrix with a scalar.
349
350       @param scalar scalar factor to multiply with.
351       @return scaled matrix.
352
353       Compute and return the product of the current matrix with the
354       scalar value scalar given in the argument.
355   */
356   FGMatrix33 operator*(const double scalar) const;
357
358   /** Multiply the matrix with 1.0/scalar.
359
360       @param scalar scalar factor to divide through.
361       @return scaled matrix.
362
363       Compute and return the product of the current matrix with the
364       scalar value 1.0/scalar, where scalar is given in the argument.
365   */
366   FGMatrix33 operator/(const double scalar) const;
367
368   /** In place matrix subtraction.
369
370       @param B matrix to subtract.
371       @return reference to the current matrix.
372
373       Compute the diffence from the current matrix and the matrix B
374       given in the argument.
375   */
376   FGMatrix33& operator-=(const FGMatrix33 &B);
377
378   /** In place matrix addition.
379
380       @param B matrix to add.
381       @return reference to the current matrix.
382
383       Compute the sum of the current matrix and the matrix B
384       given in the argument.
385   */
386   FGMatrix33& operator+=(const FGMatrix33 &B);
387
388   /** In place matrix multiplication.
389
390       @param B matrix to multiply with.
391       @return reference to the current matrix.
392
393       Compute the product of the current matrix and the matrix B
394       given in the argument.
395   */
396   FGMatrix33& operator*=(const FGMatrix33 &B);
397
398   /** In place matrix scale.
399
400       @param scalar scalar value to multiply with.
401       @return reference to the current matrix.
402
403       Compute the product of the current matrix and the scalar value scalar
404       given in the argument.
405   */
406   FGMatrix33& operator*=(const double scalar);
407
408   /** In place matrix scale.
409
410       @param scalar scalar value to divide through.
411       @return reference to the current matrix.
412
413       Compute the product of the current matrix and the scalar value
414       1.0/scalar, where scalar is given in the argument.
415   */
416   FGMatrix33& operator/=(const double scalar);
417
418 private:
419   double data[eRows*eColumns];
420
421   void Debug(int from);
422 };
423
424 /** Scalar multiplication.
425
426     @param scalar scalar value to multiply with.
427     @param A Matrix to multiply.
428
429     Multiply the Matrix with a scalar value.
430 */
431 inline FGMatrix33 operator*(double scalar, const FGMatrix33& A) {
432   // use already defined operation.
433   return A*scalar;
434 }
435
436 /** Write matrix to a stream.
437
438     @param os Stream to write to.
439     @param M Matrix to write.
440
441     Write the matrix to a stream.
442 */
443 std::ostream& operator<<(std::ostream& os, const FGMatrix33& M);
444
445 /** Read matrix from a stream.
446
447     @param os Stream to read from.
448     @param M Matrix to initialize with the values from the stream.
449
450     Read matrix from a stream.
451 */
452 std::istream& operator>>(std::istream& is, FGMatrix33& M);
453
454 } // namespace JSBSim
455
456 #endif