1 /*******************************************************************************
4 Author: Tony Peden [formatted here by JSB]
6 Purpose: FGMatrix class
10 --------------------------------------------------------------------------------
14 --------------------------------------------------------------------------------
18 --------------------------------------------------------------------------------
21 ********************************************************************************
23 *******************************************************************************/
31 /*******************************************************************************
33 *******************************************************************************/
37 /*******************************************************************************
39 *******************************************************************************/
42 /*******************************************************************************
44 *******************************************************************************/
47 /*******************************************************************************
49 *******************************************************************************/
52 /*******************************************************************************
53 ************************************ CODE **************************************
54 *******************************************************************************/
56 double** alloc(int rows,int cols)
60 A = new double *[rows+1];
63 for (int i=0;i<=rows;i++){
64 A[i]=new double[cols+1];
65 if (!A[i]) return NULL;
71 void dealloc(double **A, int rows, int cols)
73 for(int i=0;i<=rows;i++){
81 FGMatrix::FGMatrix(unsigned rows, unsigned cols)
86 data=alloc(rows,cols);
90 FGMatrix::FGMatrix(const FGMatrix& A)
97 FGMatrix::~FGMatrix(void)
100 dealloc(data,rows,cols);
106 FGMatrix& FGMatrix::operator=(const FGMatrix& A)
109 if (data != NULL) dealloc(data,rows,cols);
119 if (A.keep == true) {
122 data = alloc(rows,cols);
123 for (unsigned int i=0; i<=rows; i++) {
124 for (unsigned int j=0; j<=cols; j++) {
125 data[i][j] = A.data[i][j];
134 double& FGMatrix::operator()(unsigned row, unsigned col)
136 return data[row][col];
140 unsigned FGMatrix::Rows(void) const
146 unsigned FGMatrix::Cols(void) const
152 void FGMatrix::SetOParams(char delim,int width,int prec,int origin)
154 FGMatrix::delim=delim;
155 FGMatrix::width=width;
157 FGMatrix::origin=origin;
161 void FGMatrix::InitMatrix(double value)
164 for (unsigned int i=0;i<=rows;i++) {
165 for (unsigned int j=0;j<=cols;j++) {
166 operator()(i,j) = value;
173 void FGMatrix::InitMatrix(void)
179 FGMatrix operator-(FGMatrix& A, FGMatrix& B)
181 if ((A.Rows() != B.Rows()) || (A.Cols() != B.Cols())) {
182 cout << endl << "FGMatrix::operator-" << endl << '\t';
183 cout << "Subtraction not defined for matrices of different sizes";
188 FGMatrix Diff(A.Rows(),A.Cols());
190 for (unsigned int i=1;i<=A.Rows();i++) {
191 for (unsigned int j=1;j<=A.Cols();j++) {
192 Diff(i,j)=A(i,j)-B(i,j);
199 void operator-=(FGMatrix &A,FGMatrix &B)
201 if ((A.Rows() != B.Rows()) || (A.Cols() != B.Cols())) {
202 cout << endl << "FGMatrix::operator-" << endl << '\t';
203 cout << "Subtraction not defined for matrices of different sizes";
208 for (unsigned int i=1;i<=A.Rows();i++) {
209 for (unsigned int j=1;j<=A.Cols();j++) {
216 FGMatrix operator+(FGMatrix& A, FGMatrix& B)
218 if ((A.Rows() != B.Rows()) || (A.Cols() != B.Cols())) {
219 cout << endl << "FGMatrix::operator+" << endl << '\t';
220 cout << "Addition not defined for matrices of different sizes";
225 FGMatrix Sum(A.Rows(),A.Cols());
227 for (unsigned int i=1;i<=A.Rows();i++) {
228 for (unsigned int j=1;j<=A.Cols();j++) {
229 Sum(i,j)=A(i,j)+B(i,j);
236 void operator+=(FGMatrix &A,FGMatrix &B)
238 if ((A.Rows() != B.Rows()) || (A.Cols() != B.Cols())) {
239 cout << endl << "FGMatrix::operator+" << endl << '\t';
240 cout << "Addition not defined for matrices of different sizes";
244 for (unsigned int i=1;i<=A.Rows();i++) {
245 for (unsigned int j=1;j<=A.Cols();j++) {
252 FGMatrix operator*(double scalar,FGMatrix &A)
254 FGMatrix Product(A.Rows(),A.Cols());
256 for (unsigned int i=1;i<=A.Rows();i++) {
257 for (unsigned int j=1;j<=A.Cols();j++) {
258 Product(i,j) = scalar*A(i,j);
265 void operator*=(FGMatrix &A,double scalar)
267 for (unsigned int i=1;i<=A.Rows();i++) {
268 for (unsigned int j=1;j<=A.Cols();j++) {
275 FGMatrix operator*(FGMatrix &Left, FGMatrix &Right)
277 if (Left.Cols() != Right.Rows()) {
278 cout << endl << "FGMatrix::operator*" << endl << '\t';
279 cout << "The number of rows in the right matrix must match the number";
280 cout << endl << '\t' << "of columns in the left." << endl;
281 cout << '\t' << "Multiplication not defined." << endl;
285 FGMatrix Product(Left.Rows(),Right.Cols());
287 for (unsigned int i=1;i<=Left.Rows();i++) {
288 for (unsigned int j=1;j<=Right.Cols();j++) {
290 for (unsigned int k=1;k<=Left.Cols();k++) {
291 Product(i,j)+=Left(i,k)*Right(k,j);
299 void operator*=(FGMatrix &Left,FGMatrix &Right)
301 if (Left.Cols() != Right.Rows()) {
302 cout << endl << "FGMatrix::operator*" << endl << '\t';
303 cout << "The number of rows in the right matrix must match the number";
304 cout << endl << '\t' << "of columns in the left." << endl;
305 cout << '\t' << "Multiplication not defined." << endl;
311 prod=alloc(Left.Rows(),Right.Cols());
312 for (unsigned int i=1;i<=Left.Rows();i++) {
313 for (unsigned int j=1;j<=Right.Cols();j++) {
315 for (unsigned int k=1;k<=Left.Cols();k++) {
316 prod[i][j]+=Left(i,k)*Right(k,j);
320 dealloc(Left.data,Left.Rows(),Left.Cols());
322 Left.cols=Right.cols;
326 FGMatrix operator/(FGMatrix& A, double scalar)
328 FGMatrix Quot(A.Rows(),A.Cols());
330 for (unsigned int i=1;i<=A.Rows();i++) {
331 for (unsigned int j=1;j<=A.Cols();j++) {
332 Quot(i,j)=A(i,j)/scalar;
339 void operator/=(FGMatrix &A,double scalar)
341 for (unsigned int i=1;i<=A.Rows();i++) {
342 for (unsigned int j=1;j<=A.Cols();j++) {
349 void FGMatrix::T(void)
354 TransposeNonSquare();
358 void FGMatrix::TransposeSquare(void)
360 for (unsigned int i=1;i<=rows;i++) {
361 for (unsigned int j=i+1;j<=cols;j++) {
362 double tmp=data[i][j];
363 data[i][j]=data[j][i];
370 void FGMatrix::TransposeNonSquare(void)
374 tran=alloc(rows,cols);
375 for (unsigned int i=1;i<=rows;i++) {
376 for (unsigned int j=1;j<=cols;j++) {
377 tran[j][i]=data[i][j];
380 dealloc(data,rows,cols);
389 FGColumnVector::FGColumnVector(void):FGMatrix(3,1) { }
390 FGColumnVector::FGColumnVector(int m):FGMatrix(m,1) { }
391 FGColumnVector::FGColumnVector(FGColumnVector& b):FGMatrix(b) { }
392 FGColumnVector::~FGColumnVector() { }
393 double& FGColumnVector::operator()(int m)
395 return FGMatrix::operator()(m,1);