1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4 Author: Tony Peden, Jon Berndt, Mathias Frolich
7 ------------- Copyright (C) 2001 Jon S. Berndt (jon@jsbsim.org) -------------
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
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
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.
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.
27 --------------------------------------------------------------------------------
29 03/16/2000 JSB Added exception throwing
30 03/06/2004 MF Rework of the code to make it a bit compiler friendlier
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
46 #include "FGColumnVector3.h"
47 #include "FGJSBBase.h"
49 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
53 #define ID_MATRIX33 "$Id$"
55 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
61 class FGColumnVector3;
63 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
67 /** Exception convenience class.
70 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 DECLARATION: MatrixException
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
74 class MatrixException : public FGJSBBase
80 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
84 /** Handles matrix math operations.
85 @author Tony Peden, Jon Berndt, Mathias Froelich
88 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 DECLARATION: FGMatrix33
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
92 class FGMatrix33 : public FGJSBBase
101 /** Default initializer.
103 Create a zero matrix.
107 /** Copy constructor.
109 @param M Matrix which is used for initialization.
111 Create copy of the matrix given in the argument.
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);
127 /** Initialization by given values.
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.
139 Create a matrix from the doubles given in the arguments.
141 FGMatrix33(double m11, double m12, double m13,
142 double m21, double m22, double m23,
143 double m31, double m32, double m33) {
159 ~FGMatrix33(void) { Debug(1); }
161 /** Read access the entries of the matrix.
162 @param row Row index.
163 @param col Column index.
165 @return the value of the matrix entry at the given row and
166 column indices. Indices are counted starting with 1.
168 double operator()(unsigned int row, unsigned int col) const {
169 return Entry(row, col);
172 /** Write access the entries of the matrix.
173 Note that the indices given in the arguments are unchecked.
175 @param row Row index.
176 @param col Column index.
178 @return a reference to the matrix entry at the given row and
179 column indices. Indices are counted starting with 1.
181 double& operator()(unsigned int row, unsigned int col) {
182 return Entry(row, col);
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.
190 Note that the indices given in the arguments are unchecked.
192 @param row Row index.
193 @param col Column index.
195 @return the value of the matrix entry at the given row and
196 column indices. Indices are counted starting with 1.
198 double Entry(unsigned int row, unsigned int col) const {
199 return data[(col-1)*eRows+row-1];
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.
207 Note that the indices given in the arguments are unchecked.
209 @param row Row index.
210 @param col Column index.
212 @return a reference to the matrix entry at the given row and
213 column indices. Indices are counted starting with 1.
215 double& Entry(unsigned int row, unsigned int col) {
216 return data[(col-1)*eRows+row-1];
219 /** Number of rows in the matrix.
220 @return the number of rows in the matrix.
222 unsigned int Rows(void) const { return eRows; }
224 /** Number of cloumns in the matrix.
225 @return the number of columns in the matrix.
227 unsigned int Cols(void) const { return eColumns; }
229 /** Transposed matrix.
230 This function only returns the transpose of this matrix. This matrix itself
232 @return the transposed matrix.
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) );
240 /** Transposes this matrix.
241 This function only transposes this matrix. Nothing is returned.
245 /** Initialize the matrix.
246 This function initializes a matrix to all 0.0.
248 void InitMatrix(void);
250 /** Initialize the matrix.
251 This function initializes a matrix to user specified values.
253 void InitMatrix(double m11, double m12, double m13,
254 double m21, double m22, double m23,
255 double m31, double m32, double m33) {
267 /** Determinant of the matrix.
268 @return the determinant of the matrix.
270 double Determinant(void) const;
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.
279 bool Invertible(void) const { return 0.0 != Determinant(); }
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.
287 FGMatrix33 Inverse(void) const;
289 /** Assignment operator.
291 @param A source matrix.
293 Copy the content of the matrix given in the argument into *this.
295 FGMatrix33& operator=(const FGMatrix33& A) {
308 /** Matrix vector multiplication.
310 @param v vector to multiply with.
311 @return matric vector product.
313 Compute and return the product of the current matrix with the
314 vector given in the argument.
316 FGColumnVector3 operator*(const FGColumnVector3& v) const;
318 /** Matrix subtraction.
320 @param B matrix to add to.
321 @return difference of the matrices.
323 Compute and return the sum of the current matrix and the matrix
324 B given in the argument.
326 FGMatrix33 operator-(const FGMatrix33& B) const;
330 @param B matrix to add to.
331 @return sum of the matrices.
333 Compute and return the sum of the current matrix and the matrix
334 B given in the argument.
336 FGMatrix33 operator+(const FGMatrix33& B) const;
340 @param B matrix to add to.
341 @return product of the matrices.
343 Compute and return the product of the current matrix and the matrix
344 B given in the argument.
346 FGMatrix33 operator*(const FGMatrix33& B) const;
348 /** Multiply the matrix with a scalar.
350 @param scalar scalar factor to multiply with.
351 @return scaled matrix.
353 Compute and return the product of the current matrix with the
354 scalar value scalar given in the argument.
356 FGMatrix33 operator*(const double scalar) const;
358 /** Multiply the matrix with 1.0/scalar.
360 @param scalar scalar factor to divide through.
361 @return scaled matrix.
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.
366 FGMatrix33 operator/(const double scalar) const;
368 /** In place matrix subtraction.
370 @param B matrix to subtract.
371 @return reference to the current matrix.
373 Compute the diffence from the current matrix and the matrix B
374 given in the argument.
376 FGMatrix33& operator-=(const FGMatrix33 &B);
378 /** In place matrix addition.
380 @param B matrix to add.
381 @return reference to the current matrix.
383 Compute the sum of the current matrix and the matrix B
384 given in the argument.
386 FGMatrix33& operator+=(const FGMatrix33 &B);
388 /** In place matrix multiplication.
390 @param B matrix to multiply with.
391 @return reference to the current matrix.
393 Compute the product of the current matrix and the matrix B
394 given in the argument.
396 FGMatrix33& operator*=(const FGMatrix33 &B);
398 /** In place matrix scale.
400 @param scalar scalar value to multiply with.
401 @return reference to the current matrix.
403 Compute the product of the current matrix and the scalar value scalar
404 given in the argument.
406 FGMatrix33& operator*=(const double scalar);
408 /** In place matrix scale.
410 @param scalar scalar value to divide through.
411 @return reference to the current matrix.
413 Compute the product of the current matrix and the scalar value
414 1.0/scalar, where scalar is given in the argument.
416 FGMatrix33& operator/=(const double scalar);
419 double data[eRows*eColumns];
421 void Debug(int from);
424 /** Scalar multiplication.
426 @param scalar scalar value to multiply with.
427 @param A Matrix to multiply.
429 Multiply the Matrix with a scalar value.
431 inline FGMatrix33 operator*(double scalar, const FGMatrix33& A) {
432 // use already defined operation.
436 /** Write matrix to a stream.
438 @param os Stream to write to.
439 @param M Matrix to write.
441 Write the matrix to a stream.
443 std::ostream& operator<<(std::ostream& os, const FGMatrix33& M);
445 /** Read matrix from a stream.
447 @param os Stream to read from.
448 @param M Matrix to initialize with the values from the stream.
450 Read matrix from a stream.
452 std::istream& operator>>(std::istream& is, FGMatrix33& M);
454 } // namespace JSBSim