1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4 Author: Tony Peden, Jon Berndt, Mathias Frolich
8 --------------------------------------------------------------------------------
10 03/16/2000 JSB Added exception throwing
11 03/06/2004 MF Rework of the code to make it a bit compiler friendlier
13 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
20 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
22 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
27 # include <simgear/compiler.h>
30 # include STL_IOSTREAM
32 SG_USING_STD(ostream);
33 SG_USING_STD(istream);
39 # if defined(sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
46 # if defined(sgi) && !defined(__GNUC__)
60 #include "FGColumnVector3.h"
61 #include "FGJSBBase.h"
63 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
67 #define ID_MATRIX33 "$Id$"
69 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
75 class FGColumnVector3;
77 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
81 /** Exception convenience class.
84 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 DECLARATION: MatrixException
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
88 class MatrixException : public FGJSBBase
94 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
98 /** Handles matrix math operations.
99 @author Tony Peden, Jon Berndt, Mathias Froelich
102 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 DECLARATION: FGMatrix33
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
106 class FGMatrix33 : public FGJSBBase
115 /** Default initializer.
117 Create a zero matrix.
121 /** Copy constructor.
123 @param M Matrix which is used for initialization.
125 Create copy of the matrix given in the argument.
127 FGMatrix33(const FGMatrix33& M) {
128 Entry(1,1) = M.Entry(1,1);
129 Entry(2,1) = M.Entry(2,1);
130 Entry(3,1) = M.Entry(3,1);
131 Entry(1,2) = M.Entry(1,2);
132 Entry(2,2) = M.Entry(2,2);
133 Entry(3,2) = M.Entry(3,2);
134 Entry(1,3) = M.Entry(1,3);
135 Entry(2,3) = M.Entry(2,3);
136 Entry(3,3) = M.Entry(3,3);
141 /** Initialization by given values.
143 @param m11 value of the 1,1 Matrix element.
144 @param m12 value of the 1,2 Matrix element.
145 @param m13 value of the 1,3 Matrix element.
146 @param m21 value of the 2,1 Matrix element.
147 @param m22 value of the 2,2 Matrix element.
148 @param m23 value of the 2,3 Matrix element.
149 @param m31 value of the 3,1 Matrix element.
150 @param m32 value of the 3,2 Matrix element.
151 @param m33 value of the 3,3 Matrix element.
153 Create a matrix from the doubles given in the arguments.
155 FGMatrix33(double m11, double m12, double m13,
156 double m21, double m22, double m23,
157 double m31, double m32, double m33) {
173 ~FGMatrix33(void) { Debug(1); }
175 /** Read access the entries of the matrix.
176 @param row Row index.
177 @param col Column index.
179 @return the value of the matrix entry at the given row and
180 column indices. Indices are counted starting with 1.
182 double operator()(unsigned int row, unsigned int col) const {
183 return Entry(row, col);
186 /** Write access the entries of the matrix.
187 Note that the indices given in the arguments are unchecked.
189 @param row Row index.
190 @param col Column index.
192 @return a reference to the matrix entry at the given row and
193 column indices. Indices are counted starting with 1.
195 double& operator()(unsigned int row, unsigned int col) {
196 return Entry(row, col);
199 /** Read access the entries of the matrix.
200 This function is just a shortcut for the @ref double&
201 operator()(unsigned int row, unsigned int col) function. It is
202 used internally to access the elements in a more convenient way.
204 Note that the indices given in the arguments are unchecked.
206 @param row Row index.
207 @param col Column index.
209 @return the value of the matrix entry at the given row and
210 column indices. Indices are counted starting with 1.
212 double Entry(unsigned int row, unsigned int col) const {
213 return data[(col-1)*eRows+row-1];
216 /** Write access the entries of the matrix.
217 This function is just a shortcut for the @ref double&
218 operator()(unsigned int row, unsigned int col) function. It is
219 used internally to access the elements in a more convenient way.
221 Note that the indices given in the arguments are unchecked.
223 @param row Row index.
224 @param col Column index.
226 @return a reference to the matrix entry at the given row and
227 column indices. Indices are counted starting with 1.
229 double& Entry(unsigned int row, unsigned int col) {
230 return data[(col-1)*eRows+row-1];
233 /** Number of rows in the matrix.
234 @return the number of rows in the matrix.
236 unsigned int Rows(void) const { return eRows; }
238 /** Number of cloumns in the matrix.
239 @return the number of columns in the matrix.
241 unsigned int Cols(void) const { return eColumns; }
243 /** Transposed matrix.
244 This function only returns the transpose of this matrix. This matrix itself
246 @return the transposed matrix.
248 FGMatrix33 Transposed(void) const {
249 return FGMatrix33( Entry(1,1), Entry(2,1), Entry(3,1),
250 Entry(1,2), Entry(2,2), Entry(3,2),
251 Entry(1,3), Entry(2,3), Entry(3,3) );
254 /** Transposes this matrix.
255 This function only transposes this matrix. Nothing is returned.
259 /** Initialize the matrix.
260 This function initializes a matrix to all 0.0.
262 void InitMatrix(void);
264 /** Initialize the matrix.
265 This function initializes a matrix to user specified values.
267 void InitMatrix(double m11, double m12, double m13,
268 double m21, double m22, double m23,
269 double m31, double m32, double m33) {
281 /** Determinant of the matrix.
282 @return the determinant of the matrix.
284 double Determinant(void) const;
286 /** Return if the matrix is invertible.
287 Checks and returns if the matrix is nonsingular and thus
288 invertible. This is done by simply computing the determinant and
289 check if it is zero. Note that this test does not cover any
290 instabilities caused by nearly singular matirces using finite
291 arithmetics. It only checks exact singularity.
293 bool Invertible(void) const { return 0.0 != Determinant(); }
295 /** Return the inverse of the matrix.
296 Computes and returns if the inverse of the matrix. It is computed
297 by Cramers Rule. Also there are no checks performed if the matrix
298 is invertible. If you are not sure that it really is check this
299 with the @ref Invertible() call before.
301 FGMatrix33 Inverse(void) const;
303 /** Assignment operator.
305 @param A source matrix.
307 Copy the content of the matrix given in the argument into *this.
309 FGMatrix33& operator=(const FGMatrix33& A) {
322 /** Matrix vector multiplication.
324 @param v vector to multiply with.
325 @return matric vector product.
327 Compute and return the product of the current matrix with the
328 vector given in the argument.
330 FGColumnVector3 operator*(const FGColumnVector3& v) const;
332 /** Matrix subtraction.
334 @param B matrix to add to.
335 @return difference of the matrices.
337 Compute and return the sum of the current matrix and the matrix
338 B given in the argument.
340 FGMatrix33 operator-(const FGMatrix33& B) const;
344 @param B matrix to add to.
345 @return sum of the matrices.
347 Compute and return the sum of the current matrix and the matrix
348 B given in the argument.
350 FGMatrix33 operator+(const FGMatrix33& B) const;
354 @param B matrix to add to.
355 @return product of the matrices.
357 Compute and return the product of the current matrix and the matrix
358 B given in the argument.
360 FGMatrix33 operator*(const FGMatrix33& B) const;
362 /** Multiply the matrix with a scalar.
364 @param scalar scalar factor to multiply with.
365 @return scaled matrix.
367 Compute and return the product of the current matrix with the
368 scalar value scalar given in the argument.
370 FGMatrix33 operator*(const double scalar) const;
372 /** Multiply the matrix with 1.0/scalar.
374 @param scalar scalar factor to divide through.
375 @return scaled matrix.
377 Compute and return the product of the current matrix with the
378 scalar value 1.0/scalar, where scalar is given in the argument.
380 FGMatrix33 operator/(const double scalar) const;
382 /** In place matrix subtraction.
384 @param B matrix to subtract.
385 @return reference to the current matrix.
387 Compute the diffence from the current matrix and the matrix B
388 given in the argument.
390 FGMatrix33& operator-=(const FGMatrix33 &B);
392 /** In place matrix addition.
394 @param B matrix to add.
395 @return reference to the current matrix.
397 Compute the sum of the current matrix and the matrix B
398 given in the argument.
400 FGMatrix33& operator+=(const FGMatrix33 &B);
402 /** In place matrix multiplication.
404 @param B matrix to multiply with.
405 @return reference to the current matrix.
407 Compute the product of the current matrix and the matrix B
408 given in the argument.
410 FGMatrix33& operator*=(const FGMatrix33 &B);
412 /** In place matrix scale.
414 @param scalar scalar value to multiply with.
415 @return reference to the current matrix.
417 Compute the product of the current matrix and the scalar value scalar
418 given in the argument.
420 FGMatrix33& operator*=(const double scalar);
422 /** In place matrix scale.
424 @param scalar scalar value to divide through.
425 @return reference to the current matrix.
427 Compute the product of the current matrix and the scalar value
428 1.0/scalar, where scalar is given in the argument.
430 FGMatrix33& operator/=(const double scalar);
433 double data[eRows*eColumns];
435 void Debug(int from);
438 /** Scalar multiplication.
440 @param scalar scalar value to multiply with.
441 @param A Matrix to multiply.
443 Multiply the Matrix with a scalar value.
445 inline FGMatrix33 operator*(double scalar, const FGMatrix33& A) {
446 // use already defined operation.
450 /** Write matrix to a stream.
452 @param os Stream to write to.
453 @param M Matrix to write.
455 Write the matrix to a stream.
457 ostream& operator<<(ostream& os, const FGMatrix33& M);
459 /** Read matrix from a stream.
461 @param os Stream to read from.
462 @param M Matrix to initialize with the values from the stream.
464 Read matrix from a stream.
466 istream& operator>>(istream& is, FGMatrix33& M);
468 } // namespace JSBSim