#include "FGMatrix.h"
-static const char *IdSrc = "$Header$";
+static const char *IdSrc = "$Id$";
static const char *IdHdr = ID_MATRIX;
+extern short debug_lvl;
+
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
CLASS IMPLEMENTATION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
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;
}
data = FGalloc(rows,cols);
InitMatrix();
rowCtr = colCtr = 1;
+
+ if (debug_lvl & 2) cout << "Instantiated: FGMatrix" << endl;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data = NULL;
rowCtr = colCtr = 1;
*this = M;
+
+ if (debug_lvl & 2) cout << "Instantiated: FGMatrix" << endl;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dealloc(data,rows);
rowCtr = colCtr = 1;
rows = cols = 0;
+
+ if (debug_lvl & 2) cout << "Destroyed: FGMatrix" << endl;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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];
}
}
void FGMatrix::SetOParams(char delim,int width,int prec,int origin)
{
- FGMatrix::delim = delim;
- FGMatrix::width = width;
- FGMatrix::prec = prec;
- FGMatrix::origin = origin;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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);
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);
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;
{
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
+ } else {
cerr << "Attempt to divide by zero in method FGMatrix::operator/(const double scalar), object at " << this << endl;
+ }
return Quot;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-FGColumnVector::FGColumnVector(void):FGMatrix(3,1) { }
+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(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;
+}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 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);
}
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
+ } else {
cerr << "Attempt to divide by zero in method FGColumnVector::operator/(const double scalar), object " << this << endl;
+ }
return Quotient;
-
-
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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);
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);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+void FGColumnVector::Debug(void)
+{
+ //TODO: Add your source code here
+}
+
+//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+