00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "FGMatrix33.h"
00041 #include "FGColumnVector3.h"
00042
00043 namespace JSBSim {
00044
00045 static const char *IdSrc = "$Id: FGMatrix33.cpp,v 1.4 2008/12/12 05:05:50 jberndt Exp $";
00046 static const char *IdHdr = ID_MATRIX33;
00047
00048
00049
00050
00051
00052
00053
00054 FGMatrix33::FGMatrix33(void)
00055 {
00056 data[0] = data[1] = data[2] = data[3] = data[4] = data[5] =
00057 data[6] = data[7] = data[8] = 0.0;
00058
00059
00060 }
00061
00062
00063
00064 ostream& operator<<(ostream& os, const FGMatrix33& M)
00065 {
00066 for (unsigned int i=1; i<=M.Rows(); i++) {
00067 for (unsigned int j=1; j<=M.Cols(); j++) {
00068 if (i == M.Rows() && j == M.Cols())
00069 os << M(i,j);
00070 else
00071 os << M(i,j) << ", ";
00072 }
00073 }
00074 return os;
00075 }
00076
00077
00078
00079 istream& operator>>(istream& is, FGMatrix33& M)
00080 {
00081 for (unsigned int i=1; i<=M.Rows(); i++) {
00082 for (unsigned int j=1; j<=M.Cols(); j++) {
00083 is >> M(i,j);
00084 }
00085 }
00086 return is;
00087 }
00088
00089
00090
00091 double FGMatrix33::Determinant(void) const {
00092 return Entry(1,1)*Entry(2,2)*Entry(3,3) + Entry(1,2)*Entry(2,3)*Entry(3,1)
00093 + Entry(1,3)*Entry(2,1)*Entry(3,2) - Entry(1,3)*Entry(2,2)*Entry(3,1)
00094 - Entry(1,2)*Entry(2,1)*Entry(3,3) - Entry(2,3)*Entry(3,2)*Entry(1,1);
00095 }
00096
00097
00098
00099 FGMatrix33 FGMatrix33::Inverse(void) const {
00100
00101
00102
00103 double rdet = 1.0/Determinant();
00104
00105 double i11 = rdet*(Entry(2,2)*Entry(3,3)-Entry(2,3)*Entry(3,2));
00106 double i21 = rdet*(Entry(2,3)*Entry(3,1)-Entry(2,1)*Entry(3,3));
00107 double i31 = rdet*(Entry(2,1)*Entry(3,2)-Entry(2,2)*Entry(3,1));
00108 double i12 = rdet*(Entry(1,3)*Entry(3,2)-Entry(1,2)*Entry(3,3));
00109 double i22 = rdet*(Entry(1,1)*Entry(3,3)-Entry(1,3)*Entry(3,1));
00110 double i32 = rdet*(Entry(1,2)*Entry(3,1)-Entry(1,1)*Entry(3,2));
00111 double i13 = rdet*(Entry(1,2)*Entry(2,3)-Entry(1,3)*Entry(2,2));
00112 double i23 = rdet*(Entry(1,3)*Entry(2,1)-Entry(1,1)*Entry(2,3));
00113 double i33 = rdet*(Entry(1,1)*Entry(2,2)-Entry(1,2)*Entry(2,1));
00114
00115 return FGMatrix33( i11, i12, i13,
00116 i21, i22, i23,
00117 i31, i32, i33 );
00118 }
00119
00120
00121
00122 void FGMatrix33::InitMatrix(void)
00123 {
00124 data[0] = data[1] = data[2] = data[3] = data[4] = data[5] =
00125 data[6] = data[7] = data[8] = 0.0;
00126 }
00127
00128
00129
00130
00131
00132 FGMatrix33 FGMatrix33::operator-(const FGMatrix33& M) const
00133 {
00134 return FGMatrix33( Entry(1,1) - M(1,1),
00135 Entry(1,2) - M(1,2),
00136 Entry(1,3) - M(1,3),
00137 Entry(2,1) - M(2,1),
00138 Entry(2,2) - M(2,2),
00139 Entry(2,3) - M(2,3),
00140 Entry(3,1) - M(3,1),
00141 Entry(3,2) - M(3,2),
00142 Entry(3,3) - M(3,3) );
00143 }
00144
00145
00146
00147 FGMatrix33& FGMatrix33::operator-=(const FGMatrix33 &M)
00148 {
00149 data[0] -= M.data[0];
00150 data[1] -= M.data[1];
00151 data[2] -= M.data[2];
00152 data[3] -= M.data[3];
00153 data[4] -= M.data[4];
00154 data[5] -= M.data[5];
00155 data[6] -= M.data[6];
00156 data[7] -= M.data[7];
00157 data[8] -= M.data[8];
00158
00159 return *this;
00160 }
00161
00162
00163
00164 FGMatrix33 FGMatrix33::operator+(const FGMatrix33& M) const
00165 {
00166 return FGMatrix33( Entry(1,1) + M(1,1),
00167 Entry(1,2) + M(1,2),
00168 Entry(1,3) + M(1,3),
00169 Entry(2,1) + M(2,1),
00170 Entry(2,2) + M(2,2),
00171 Entry(2,3) + M(2,3),
00172 Entry(3,1) + M(3,1),
00173 Entry(3,2) + M(3,2),
00174 Entry(3,3) + M(3,3) );
00175 }
00176
00177
00178
00179 FGMatrix33& FGMatrix33::operator+=(const FGMatrix33 &M)
00180 {
00181 Entry(1,1) += M(1,1);
00182 Entry(1,2) += M(1,2);
00183 Entry(1,3) += M(1,3);
00184 Entry(2,1) += M(2,1);
00185 Entry(2,2) += M(2,2);
00186 Entry(2,3) += M(2,3);
00187 Entry(3,1) += M(3,1);
00188 Entry(3,2) += M(3,2);
00189 Entry(3,3) += M(3,3);
00190
00191 return *this;
00192 }
00193
00194
00195
00196 FGMatrix33 FGMatrix33::operator*(const double scalar) const
00197 {
00198 return FGMatrix33( scalar * Entry(1,1),
00199 scalar * Entry(1,2),
00200 scalar * Entry(1,3),
00201 scalar * Entry(2,1),
00202 scalar * Entry(2,2),
00203 scalar * Entry(2,3),
00204 scalar * Entry(3,1),
00205 scalar * Entry(3,2),
00206 scalar * Entry(3,3) );
00207 }
00208
00209
00210
00211 FGMatrix33 operator*(double scalar, FGMatrix33 &M)
00212 {
00213 return FGMatrix33( scalar * M(1,1),
00214 scalar * M(1,2),
00215 scalar * M(1,3),
00216 scalar * M(2,1),
00217 scalar * M(2,2),
00218 scalar * M(2,3),
00219 scalar * M(3,1),
00220 scalar * M(3,2),
00221 scalar * M(3,3) );
00222 }
00223
00224
00225
00226 FGMatrix33& FGMatrix33::operator*=(const double scalar)
00227 {
00228 Entry(1,1) *= scalar;
00229 Entry(1,2) *= scalar;
00230 Entry(1,3) *= scalar;
00231 Entry(2,1) *= scalar;
00232 Entry(2,2) *= scalar;
00233 Entry(2,3) *= scalar;
00234 Entry(3,1) *= scalar;
00235 Entry(3,2) *= scalar;
00236 Entry(3,3) *= scalar;
00237
00238 return *this;
00239 }
00240
00241
00242
00243 FGMatrix33 FGMatrix33::operator*(const FGMatrix33& M) const
00244 {
00245
00246 FGMatrix33 Product;
00247
00248 Product(1,1) = Entry(1,1)*M(1,1) + Entry(1,2)*M(2,1) + Entry(1,3)*M(3,1);
00249 Product(1,2) = Entry(1,1)*M(1,2) + Entry(1,2)*M(2,2) + Entry(1,3)*M(3,2);
00250 Product(1,3) = Entry(1,1)*M(1,3) + Entry(1,2)*M(2,3) + Entry(1,3)*M(3,3);
00251 Product(2,1) = Entry(2,1)*M(1,1) + Entry(2,2)*M(2,1) + Entry(2,3)*M(3,1);
00252 Product(2,2) = Entry(2,1)*M(1,2) + Entry(2,2)*M(2,2) + Entry(2,3)*M(3,2);
00253 Product(2,3) = Entry(2,1)*M(1,3) + Entry(2,2)*M(2,3) + Entry(2,3)*M(3,3);
00254 Product(3,1) = Entry(3,1)*M(1,1) + Entry(3,2)*M(2,1) + Entry(3,3)*M(3,1);
00255 Product(3,2) = Entry(3,1)*M(1,2) + Entry(3,2)*M(2,2) + Entry(3,3)*M(3,2);
00256 Product(3,3) = Entry(3,1)*M(1,3) + Entry(3,2)*M(2,3) + Entry(3,3)*M(3,3);
00257
00258 return Product;
00259 }
00260
00261
00262
00263 FGMatrix33& FGMatrix33::operator*=(const FGMatrix33& M)
00264 {
00265
00266 double a,b,c;
00267
00268 a = Entry(1,1); b=Entry(1,2); c=Entry(1,3);
00269 Entry(1,1) = a*M(1,1) + b*M(2,1) + c*M(3,1);
00270 Entry(1,2) = a*M(1,2) + b*M(2,2) + c*M(3,2);
00271 Entry(1,3) = a*M(1,3) + b*M(2,3) + c*M(3,3);
00272
00273 a = Entry(2,1); b=Entry(2,2); c=Entry(2,3);
00274 Entry(2,1) = a*M(1,1) + b*M(2,1) + c*M(3,1);
00275 Entry(2,2) = a*M(1,2) + b*M(2,2) + c*M(3,2);
00276 Entry(2,3) = a*M(1,3) + b*M(2,3) + c*M(3,3);
00277
00278 a = Entry(3,1); b=Entry(3,2); c=Entry(3,3);
00279 Entry(3,1) = a*M(1,1) + b*M(2,1) + c*M(3,1);
00280 Entry(3,2) = a*M(1,2) + b*M(2,2) + c*M(3,2);
00281 Entry(3,3) = a*M(1,3) + b*M(2,3) + c*M(3,3);
00282
00283 return *this;
00284 }
00285
00286
00287
00288 FGMatrix33 FGMatrix33::operator/(const double scalar) const
00289 {
00290 FGMatrix33 Quot;
00291
00292 if ( scalar != 0 ) {
00293 double tmp = 1.0/scalar;
00294 Quot(1,1) = Entry(1,1) * tmp;
00295 Quot(1,2) = Entry(1,2) * tmp;
00296 Quot(1,3) = Entry(1,3) * tmp;
00297 Quot(2,1) = Entry(2,1) * tmp;
00298 Quot(2,2) = Entry(2,2) * tmp;
00299 Quot(2,3) = Entry(2,3) * tmp;
00300 Quot(3,1) = Entry(3,1) * tmp;
00301 Quot(3,2) = Entry(3,2) * tmp;
00302 Quot(3,3) = Entry(3,3) * tmp;
00303 } else {
00304 MatrixException mE;
00305 mE.Message = "Attempt to divide by zero in method FGMatrix33::operator/(const double scalar)";
00306 throw mE;
00307 }
00308 return Quot;
00309 }
00310
00311
00312
00313 FGMatrix33& FGMatrix33::operator/=(const double scalar)
00314 {
00315 if ( scalar != 0 ) {
00316 double tmp = 1.0/scalar;
00317 Entry(1,1) *= tmp;
00318 Entry(1,2) *= tmp;
00319 Entry(1,3) *= tmp;
00320 Entry(2,1) *= tmp;
00321 Entry(2,2) *= tmp;
00322 Entry(2,3) *= tmp;
00323 Entry(3,1) *= tmp;
00324 Entry(3,2) *= tmp;
00325 Entry(3,3) *= tmp;
00326 } else {
00327 MatrixException mE;
00328 mE.Message = "Attempt to divide by zero in method FGMatrix33::operator/=(const double scalar)";
00329 throw mE;
00330 }
00331 return *this;
00332 }
00333
00334
00335
00336 void FGMatrix33::T(void)
00337 {
00338 for (unsigned int i=1; i<=3; i++) {
00339 for (unsigned int j=i+1; j<=3; j++) {
00340 double tmp = Entry(i,j);
00341 Entry(i,j) = Entry(j,i);
00342 Entry(j,i) = tmp;
00343 }
00344 }
00345 }
00346
00347
00348
00349 FGColumnVector3 FGMatrix33::operator*(const FGColumnVector3& v) const {
00350 double tmp1 = v(1)*Entry(1,1);
00351 double tmp2 = v(1)*Entry(2,1);
00352 double tmp3 = v(1)*Entry(3,1);
00353
00354 tmp1 += v(2)*Entry(1,2);
00355 tmp2 += v(2)*Entry(2,2);
00356 tmp3 += v(2)*Entry(3,2);
00357
00358 tmp1 += v(3)*Entry(1,3);
00359 tmp2 += v(3)*Entry(2,3);
00360 tmp3 += v(3)*Entry(3,3);
00361
00362 return FGColumnVector3( tmp1, tmp2, tmp3 );
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384 void FGMatrix33::Debug(int from)
00385 {
00386 if (debug_lvl <= 0) return;
00387
00388 if (debug_lvl & 1) {
00389 if (from == 0) {
00390
00391 }
00392 }
00393 if (debug_lvl & 2 ) {
00394 if (from == 0) cout << "Instantiated: FGMatrix33" << endl;
00395 if (from == 1) cout << "Destroyed: FGMatrix33" << endl;
00396 }
00397 if (debug_lvl & 4 ) {
00398 }
00399 if (debug_lvl & 8 ) {
00400 }
00401 if (debug_lvl & 16) {
00402 }
00403 if (debug_lvl & 64) {
00404 if (from == 0) {
00405 cout << IdSrc << endl;
00406 cout << IdHdr << endl;
00407 }
00408 }
00409 }
00410 }