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: FGMatrix33.h,v 1.12 2010/08/21 17:13:47 jberndt Exp $"
55 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
61 class FGColumnVector3;
64 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
68 /** Exception convenience class.
71 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 DECLARATION: MatrixException
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
75 class MatrixException : public FGJSBBase
81 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
85 /** Handles matrix math operations.
86 @author Tony Peden, Jon Berndt, Mathias Froelich
89 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 DECLARATION: FGMatrix33
91 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
93 class FGMatrix33 : public FGJSBBase
102 /** Default initializer.
104 Create a zero matrix.
108 /** Copy constructor.
110 @param M Matrix which is used for initialization.
112 Create copy of the matrix given in the argument.
114 FGMatrix33(const FGMatrix33& M)
129 /** Initialization by given values.
131 @param m11 value of the 1,1 Matrix element.
132 @param m12 value of the 1,2 Matrix element.
133 @param m13 value of the 1,3 Matrix element.
134 @param m21 value of the 2,1 Matrix element.
135 @param m22 value of the 2,2 Matrix element.
136 @param m23 value of the 2,3 Matrix element.
137 @param m31 value of the 3,1 Matrix element.
138 @param m32 value of the 3,2 Matrix element.
139 @param m33 value of the 3,3 Matrix element.
141 Create a matrix from the doubles given in the arguments.
143 FGMatrix33(double m11, double m12, double m13,
144 double m21, double m22, double m23,
145 double m31, double m32, double m33) {
161 ~FGMatrix33(void) { /* Debug(1); */ }
163 /** Prints the contents of the matrix.
164 @param delimeter the item separator (tab or comma)
165 @return a string with the delimeter-separated contents of the matrix */
166 std::string Dump(const std::string& delimeter) const;
168 /** Prints the contents of the matrix.
169 @param delimeter the item separator (tab or comma, etc.)
170 @param prefix an additional prefix that is used to indent the 3X3 matrix printout
171 @return a string with the delimeter-separated contents of the matrix */
172 std::string Dump(const std::string& delimiter, const std::string& prefix) const;
174 /** Read access the entries of the matrix.
175 @param row Row index.
176 @param col Column index.
178 @return the value of 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) const {
182 return data[(col-1)*eRows+row-1];
185 /** Write access the entries of the matrix.
186 Note that the indices given in the arguments are unchecked.
188 @param row Row index.
189 @param col Column index.
191 @return a reference to the matrix entry at the given row and
192 column indices. Indices are counted starting with 1.
194 double& operator()(unsigned int row, unsigned int col) {
195 return data[(col-1)*eRows+row-1];
198 /** Read access the entries of the matrix.
199 This function is just a shortcut for the <tt>double&
200 operator()(unsigned int row, unsigned int col)</tt> function. It is
201 used internally to access the elements in a more convenient way.
203 Note that the indices given in the arguments are unchecked.
205 @param row Row index.
206 @param col Column index.
208 @return the value of the matrix entry at the given row and
209 column indices. Indices are counted starting with 1.
211 double Entry(unsigned int row, unsigned int col) const {
212 return data[(col-1)*eRows+row-1];
215 /** Write access the entries of the matrix.
216 This function is just a shortcut for the <tt>double&
217 operator()(unsigned int row, unsigned int col)</tt> function. It is
218 used internally to access the elements in a more convenient way.
220 Note that the indices given in the arguments are unchecked.
222 @param row Row index.
223 @param col Column index.
225 @return a reference to the matrix entry at the given row and
226 column indices. Indices are counted starting with 1.
228 double& Entry(unsigned int row, unsigned int col) {
229 return data[(col-1)*eRows+row-1];
232 /** Number of rows in the matrix.
233 @return the number of rows in the matrix.
235 unsigned int Rows(void) const { return eRows; }
237 /** Number of cloumns in the matrix.
238 @return the number of columns in the matrix.
240 unsigned int Cols(void) const { return eColumns; }
242 /** Transposed matrix.
243 This function only returns the transpose of this matrix. This matrix itself
245 @return the transposed matrix.
247 FGMatrix33 Transposed(void) const {
248 return FGMatrix33( data[0], data[1], data[2],
249 data[3], data[4], data[5],
250 data[6], data[7], data[8] );
253 /** Transposes this matrix.
254 This function only transposes this matrix. Nothing is returned.
258 /** Initialize the matrix.
259 This function initializes a matrix to all 0.0.
261 void InitMatrix(void);
263 /** Initialize the matrix.
264 This function initializes a matrix to user specified values.
266 void InitMatrix(double m11, double m12, double m13,
267 double m21, double m22, double m23,
268 double m31, double m32, double m33) {
280 /** Returns the quaternion associated with this direction cosine (rotation) matrix.
282 FGQuaternion GetQuaternion(void);
284 /** Determinant of the matrix.
285 @return the determinant of the matrix.
287 double Determinant(void) const;
289 /** Return if the matrix is invertible.
290 Checks and returns if the matrix is nonsingular and thus
291 invertible. This is done by simply computing the determinant and
292 check if it is zero. Note that this test does not cover any
293 instabilities caused by nearly singular matirces using finite
294 arithmetics. It only checks exact singularity.
296 bool Invertible(void) const { return 0.0 != Determinant(); }
298 /** Return the inverse of the matrix.
299 Computes and returns if the inverse of the matrix. It is computed
300 by Cramers Rule. Also there are no checks performed if the matrix
301 is invertible. If you are not sure that it really is check this
302 with the @ref Invertible() call before.
304 FGMatrix33 Inverse(void) const;
306 /** Assignment operator.
308 @param A source matrix.
310 Copy the content of the matrix given in the argument into *this.
312 FGMatrix33& operator=(const FGMatrix33& A) {
325 /** Matrix vector multiplication.
327 @param v vector to multiply with.
328 @return matric vector product.
330 Compute and return the product of the current matrix with the
331 vector given in the argument.
333 FGColumnVector3 operator*(const FGColumnVector3& v) const;
335 /** Matrix subtraction.
337 @param B matrix to add to.
338 @return difference of the matrices.
340 Compute and return the sum of the current matrix and the matrix
341 B given in the argument.
343 FGMatrix33 operator-(const FGMatrix33& B) const;
347 @param B matrix to add to.
348 @return sum of the matrices.
350 Compute and return the sum of the current matrix and the matrix
351 B given in the argument.
353 FGMatrix33 operator+(const FGMatrix33& B) const;
357 @param B matrix to add to.
358 @return product of the matrices.
360 Compute and return the product of the current matrix and the matrix
361 B given in the argument.
363 FGMatrix33 operator*(const FGMatrix33& B) const;
365 /** Multiply the matrix with a scalar.
367 @param scalar scalar factor to multiply with.
368 @return scaled matrix.
370 Compute and return the product of the current matrix with the
371 scalar value scalar given in the argument.
373 FGMatrix33 operator*(const double scalar) const;
375 /** Multiply the matrix with 1.0/scalar.
377 @param scalar scalar factor to divide through.
378 @return scaled matrix.
380 Compute and return the product of the current matrix with the
381 scalar value 1.0/scalar, where scalar is given in the argument.
383 FGMatrix33 operator/(const double scalar) const;
385 /** In place matrix subtraction.
387 @param B matrix to subtract.
388 @return reference to the current matrix.
390 Compute the diffence from the current matrix and the matrix B
391 given in the argument.
393 FGMatrix33& operator-=(const FGMatrix33 &B);
395 /** In place matrix addition.
397 @param B matrix to add.
398 @return reference to the current matrix.
400 Compute the sum of the current matrix and the matrix B
401 given in the argument.
403 FGMatrix33& operator+=(const FGMatrix33 &B);
405 /** In place matrix multiplication.
407 @param B matrix to multiply with.
408 @return reference to the current matrix.
410 Compute the product of the current matrix and the matrix B
411 given in the argument.
413 FGMatrix33& operator*=(const FGMatrix33 &B);
415 /** In place matrix scale.
417 @param scalar scalar value to multiply with.
418 @return reference to the current matrix.
420 Compute the product of the current matrix and the scalar value scalar
421 given in the argument.
423 FGMatrix33& operator*=(const double scalar);
425 /** In place matrix scale.
427 @param scalar scalar value to divide through.
428 @return reference to the current matrix.
430 Compute the product of the current matrix and the scalar value
431 1.0/scalar, where scalar is given in the argument.
433 FGMatrix33& operator/=(const double scalar);
436 double data[eRows*eColumns];
438 void Debug(int from);
441 /** Scalar multiplication.
443 @param scalar scalar value to multiply with.
444 @param A Matrix to multiply.
446 Multiply the Matrix with a scalar value.
448 inline FGMatrix33 operator*(double scalar, const FGMatrix33& A) {
449 // use already defined operation.
453 /** Write matrix to a stream.
455 @param os Stream to write to.
456 @param M Matrix to write.
458 Write the matrix to a stream.
460 std::ostream& operator<<(std::ostream& os, const FGMatrix33& M);
462 /** Read matrix from a stream.
464 @param os Stream to read from.
465 @param M Matrix to initialize with the values from the stream.
467 Read matrix from a stream.
469 std::istream& operator>>(std::istream& is, FGMatrix33& M);
471 } // namespace JSBSim
473 #include "FGQuaternion.h"