]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/JSBSim/FGMatrix.cpp
Initial revision.
[flightgear.git] / src / FDM / JSBSim / FGMatrix.cpp
index 6e51fa17d633cb70852e6a56066f1fe3f0f9f2d6..6edc4f3f66e26428feb10fe5e6d81c63eb839271 100644 (file)
@@ -1,4 +1,4 @@
-/*******************************************************************************
+/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 Module: FGMatrix.cpp
 Author: Originally by Tony Peden [formatted here (and broken??) by JSB]
@@ -14,15 +14,20 @@ HISTORY
 ??/??/??   TP   Created
 03/16/2000 JSB  Added exception throwing
 
-********************************************************************************
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 INCLUDES
-*******************************************************************************/
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
 #include "FGMatrix.h"
 
-/*******************************************************************************
-************************************ CODE **************************************
-*******************************************************************************/
+static const char *IdSrc = "$Id$";
+static const char *IdHdr = ID_MATRIX;
+
+extern short debug_lvl;
+
+/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+CLASS IMPLEMENTATION
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
 
 double** FGalloc(int rows, int cols)
 {
@@ -38,42 +43,48 @@ double** FGalloc(int rows, int cols)
   return A;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 void dealloc(double **A, int rows)
 {
-  for(int i=0;i<= rows;i++) delete[] A[i];
+  for (int i=0; i <= rows; i++) delete[] A[i];
   delete[] A;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGMatrix::FGMatrix(const unsigned int r, const unsigned int c) : rows(r), cols(c)
 {
   data = FGalloc(rows,cols);
   InitMatrix();
   rowCtr = colCtr = 1;
+  
+  if (debug_lvl & 2) cout << "Instantiated: FGMatrix" << endl;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGMatrix::FGMatrix(const FGMatrix& M)
 {
   data  = NULL;
   rowCtr = colCtr = 1;
   *this = M;
+
+  if (debug_lvl & 2) cout << "Instantiated: FGMatrix" << endl;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGMatrix::~FGMatrix(void)
 {
   dealloc(data,rows);
   rowCtr = colCtr = 1;
   rows = cols = 0;
+
+  if (debug_lvl & 2) cout << "Destroyed:    FGMatrix" << endl;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 ostream& operator<<(ostream& os, const FGMatrix& M)
 {
@@ -88,7 +99,7 @@ ostream& operator<<(ostream& os, const FGMatrix& M)
   return os;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGMatrix& FGMatrix::operator<<(const float ff)
 {
@@ -101,7 +112,7 @@ FGMatrix& FGMatrix::operator<<(const float ff)
   return *this;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 istream& operator>>(istream& is, FGMatrix& M)
 {
@@ -113,23 +124,21 @@ istream& operator>>(istream& is, FGMatrix& M)
   return is;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGMatrix& FGMatrix::operator=(const FGMatrix& M)
 {
   if (&M != this) {
-    if (data != NULL) dealloc(data,rows);
-
-    width  = M.width;
-    prec   = M.prec;
-    delim  = M.delim;
-    origin = M.origin;
-    rows   = M.rows;
-    cols   = M.cols;
-
-    data = FGalloc(rows,cols);
-    for (unsigned int i=0; i<=rows; i++) {
-      for (unsigned int j=0; j<=cols; j++) {
+    if (M.rows != rows || M.cols != cols) {
+      if (data != NULL) dealloc(data,rows);
+
+      rows   = M.rows;
+      cols   = M.cols;
+
+      data = FGalloc(rows,cols);
+    }
+    for (unsigned int i=1; i<=rows; i++) {
+      for (unsigned int j=1; j<=cols; j++) {
         data[i][j] = M.data[i][j];
       }
     }
@@ -137,31 +146,27 @@ FGMatrix& FGMatrix::operator=(const FGMatrix& M)
   return *this;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 unsigned int FGMatrix::Rows(void) const
 {
   return rows;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 unsigned int FGMatrix::Cols(void) const
 {
   return cols;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 void FGMatrix::SetOParams(char delim,int width,int prec,int origin)
 {
-  FGMatrix::delim  = delim;
-  FGMatrix::width  = width;
-  FGMatrix::prec   = prec;
-  FGMatrix::origin = origin;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 void FGMatrix::InitMatrix(double value)
 {
@@ -174,7 +179,7 @@ void FGMatrix::InitMatrix(double value)
   }
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 void FGMatrix::InitMatrix(void)
 {
@@ -187,14 +192,14 @@ void FGMatrix::InitMatrix(void)
 
 FGMatrix FGMatrix::operator-(const FGMatrix& M)
 {
+  FGMatrix Diff(Rows(), Cols());
+
   if ((Rows() != M.Rows()) || (Cols() != M.Cols())) {
     MatrixException mE;
     mE.Message = "Invalid row/column match in Matrix operator -";
     throw mE;
   }
 
-  FGMatrix Diff(Rows(), Cols());
-
   for (unsigned int i=1; i<=Rows(); i++) {
     for (unsigned int j=1; j<=Cols(); j++) {
       Diff(i,j) = data[i][j] - M(i,j);
@@ -203,7 +208,7 @@ FGMatrix FGMatrix::operator-(const FGMatrix& M)
   return Diff;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 void FGMatrix::operator-=(const FGMatrix &M)
 {
@@ -220,18 +225,18 @@ void FGMatrix::operator-=(const FGMatrix &M)
   }
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGMatrix FGMatrix::operator+(const FGMatrix& M)
 {
+  FGMatrix Sum(Rows(), Cols());
+
   if ((Rows() != M.Rows()) || (Cols() != M.Cols())) {
     MatrixException mE;
     mE.Message = "Invalid row/column match in Matrix operator +";
     throw mE;
   }
 
-  FGMatrix Sum(Rows(), Cols());
-
   for (unsigned int i=1; i<=Rows(); i++) {
     for (unsigned int j=1; j<=Cols(); j++) {
       Sum(i,j) = data[i][j] + M(i,j);
@@ -240,7 +245,7 @@ FGMatrix FGMatrix::operator+(const FGMatrix& M)
   return Sum;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 void FGMatrix::operator+=(const FGMatrix &M)
 {
@@ -257,7 +262,7 @@ void FGMatrix::operator+=(const FGMatrix &M)
   }
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGMatrix operator*(double scalar, FGMatrix &M)
 {
@@ -271,7 +276,7 @@ FGMatrix operator*(double scalar, FGMatrix &M)
   return Product;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 void FGMatrix::operator*=(const double scalar)
 {
@@ -282,18 +287,18 @@ void FGMatrix::operator*=(const double scalar)
   }
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGMatrix FGMatrix::operator*(const FGMatrix& M)
 {
+  FGMatrix Product(Rows(), M.Cols());
+
   if (Cols() != M.Rows()) {
     MatrixException mE;
     mE.Message = "Invalid row/column match in Matrix operator *";
     throw mE;
   }
 
-  FGMatrix Product(Rows(), M.Cols());
-
   for (unsigned int i=1; i<=Rows(); i++) {
     for (unsigned int j=1; j<=M.Cols(); j++)  {
       Product(i,j) = 0;
@@ -305,7 +310,7 @@ FGMatrix FGMatrix::operator*(const FGMatrix& M)
   return Product;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 void FGMatrix::operator*=(const FGMatrix& M)
 {
@@ -331,38 +336,41 @@ void FGMatrix::operator*=(const FGMatrix& M)
   cols = M.cols;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGMatrix FGMatrix::operator/(const double scalar)
 {
   FGMatrix Quot(Rows(), Cols());
 
-  if(scalar != 0) {
+  if (scalar != 0) {
     for (unsigned int i=1; i<=Rows(); i++) {
       for (unsigned int j=1; j<=Cols(); j++)  {
          Quot(i,j) = data[i][j]/scalar;
       }
     }
     
+  } else {
+    cerr << "Attempt to divide by zero in method FGMatrix::operator/(const double scalar), object at " << this << endl; 
   }
-  return Quot;
+  return Quot;  
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 void FGMatrix::operator/=(const double scalar)
 {
-
+  
   if(scalar != 0) {
     for (unsigned int i=1; i<=Rows(); i++)  {
       for (unsigned int j=1; j<=Cols(); j++) {
         data[i][j]/=scalar;
       }
     }
-  }
+  } else 
+    cerr << "Attempt to divide by zero in method FGMatrix::operator/=(const double scalar), object " << this << endl; 
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 void FGMatrix::T(void)
 {
@@ -372,7 +380,7 @@ void FGMatrix::T(void)
     TransposeNonSquare();
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGColumnVector FGMatrix::operator*(const FGColumnVector& Col)
 {
@@ -393,7 +401,7 @@ FGColumnVector FGMatrix::operator*(const FGColumnVector& Col)
   return Product;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 void FGMatrix::TransposeSquare(void)
 {
@@ -406,7 +414,7 @@ void FGMatrix::TransposeSquare(void)
   }
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 void FGMatrix::TransposeNonSquare(void)
 {
@@ -428,21 +436,37 @@ void FGMatrix::TransposeNonSquare(void)
   cols       = m;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+void FGMatrix::Debug(void)
+{
+    //TODO: Add your source code here
+}
+
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+FGColumnVector::FGColumnVector(void):FGMatrix(3,1)
+{
+  if (debug_lvl & 2) cout << "Instantiated: FGColumnVector" << endl;
+}
 
-FGColumnVector::FGColumnVector(void):FGMatrix(3,1) { }
 FGColumnVector::FGColumnVector(int m):FGMatrix(m,1) { }
 FGColumnVector::FGColumnVector(const FGColumnVector& b):FGMatrix(b) { }
-FGColumnVector::~FGColumnVector() { }
 
-/******************************************************************************/
+FGColumnVector::~FGColumnVector(void)
+{
+//  dealloc(data,rows);
+  if (debug_lvl & 2) cout << "Destroyed:    FGColumnVector" << endl;
+}
+
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 double& FGColumnVector::operator()(int m) const
 {
   return FGMatrix::operator()(m,1);
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGColumnVector operator*(const FGMatrix& Mat, const FGColumnVector& Col)
 {
@@ -464,26 +488,25 @@ FGColumnVector operator*(const FGMatrix& Mat, const FGColumnVector& Col)
   return Product;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGColumnVector FGColumnVector::operator+(const FGColumnVector& C)
 {
+  FGColumnVector Sum(C.Rows()); // This must be created dynamically
+                                // because we don't know the size of "C",
+                                // it could be 3 or 4 or ...
   if (Rows() != C.Rows()) {
     MatrixException mE;
     mE.Message = "Invalid row/column match in Column Vector operator *";
     throw mE;
   }
 
-  FGColumnVector Sum(C.Rows());
-
-  for (unsigned int i=1; i<=C.Rows(); i++) {
-    Sum(i) = C(i) + data[i][1];
-  }
+  for (unsigned int i=1; i<=C.Rows(); i++) Sum(i) = C(i) + data[i][1];
 
   return Sum;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGColumnVector FGColumnVector::operator*(const double scalar)
 {
@@ -494,18 +517,18 @@ FGColumnVector FGColumnVector::operator*(const double scalar)
   return Product;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGColumnVector FGColumnVector::operator-(const FGColumnVector& V)
 {
+  FGColumnVector Diff(Rows());
+
   if ((Rows() != V.Rows()) || (Cols() != V.Cols())) {
     MatrixException mE;
     mE.Message = "Invalid row/column match in Column Vector operator -";
     throw mE;
   }
 
-  FGColumnVector Diff(Rows());
-
   for (unsigned int i=1; i<=Rows(); i++) {
     Diff(i) = data[i][1] - V(i);
   }
@@ -513,18 +536,21 @@ FGColumnVector FGColumnVector::operator-(const FGColumnVector& V)
   return Diff;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGColumnVector FGColumnVector::operator/(const double scalar)
 {
   FGColumnVector Quotient(Rows());
-  if(scalar != 0) {
+
+  if (scalar != 0) {
     for (unsigned int i=1; i<=Rows(); i++) Quotient(i) = data[i][1] / scalar;
+  } else {
+    cerr << "Attempt to divide by zero in method FGColumnVector::operator/(const double scalar), object " << this << endl; 
   }
   return Quotient;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGColumnVector operator*(const double scalar, const FGColumnVector& C)
 {
@@ -537,7 +563,7 @@ FGColumnVector operator*(const double scalar, const FGColumnVector& C)
   return Product;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 float FGColumnVector::Magnitude(void)
 {
@@ -554,7 +580,7 @@ float FGColumnVector::Magnitude(void)
   }
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGColumnVector FGColumnVector::Normalize(void)
 {
@@ -569,18 +595,18 @@ FGColumnVector FGColumnVector::Normalize(void)
   return *this;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGColumnVector FGColumnVector::operator*(const FGColumnVector& V)
 {
+  FGColumnVector Product(3);
+
   if (Rows() != 3 || V.Rows() != 3) {
     MatrixException mE;
     mE.Message = "Invalid row count in vector cross product function";
     throw mE;
   }
 
-  FGColumnVector Product(3);
-
   Product(1) = data[2][1] * V(3) - data[3][1] * V(2);
   Product(2) = data[3][1] * V(1) - data[1][1] * V(3);
   Product(3) = data[1][1] * V(2) - data[2][1] * V(1);
@@ -588,18 +614,18 @@ FGColumnVector FGColumnVector::operator*(const FGColumnVector& V)
   return Product;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 FGColumnVector FGColumnVector::multElementWise(const FGColumnVector& V)
 {
+  FGColumnVector Product(3);
+
   if (Rows() != 3 || V.Rows() != 3) {
     MatrixException mE;
     mE.Message = "Invalid row count in vector cross product function";
     throw mE;
   }
 
-  FGColumnVector Product(3);
-
   Product(1) = data[1][1] * V(1);
   Product(2) = data[2][1] * V(2);
   Product(3) = data[3][1] * V(3);
@@ -607,4 +633,12 @@ FGColumnVector FGColumnVector::multElementWise(const FGColumnVector& V)
   return Product;
 }
 
-/******************************************************************************/
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+void FGColumnVector::Debug(void)
+{
+    //TODO: Add your source code here
+}
+
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+