00001
00002
00003
00004
00005
00006 #ifndef NEWMAT_LIB
00007 #define NEWMAT_LIB 0
00008
00009 #include "include.h"
00010
00011
00012 #include "myexcept.h"
00013
00014 #ifdef use_namespace
00015 namespace NEWMAT { using namespace RBD_COMMON; }
00016 namespace RBD_LIBRARIES { using namespace NEWMAT; }
00017 namespace NEWMAT {
00018 #endif
00019
00020
00021
00022 #ifdef NO_LONG_NAMES
00023 #define UpperTriangularMatrix UTMatrix
00024 #define LowerTriangularMatrix LTMatrix
00025 #define SymmetricMatrix SMatrix
00026 #define DiagonalMatrix DMatrix
00027 #define BandMatrix BMatrix
00028 #define UpperBandMatrix UBMatrix
00029 #define LowerBandMatrix LBMatrix
00030 #define SymmetricBandMatrix SBMatrix
00031 #define BandLUMatrix BLUMatrix
00032 #endif
00033
00034 #ifndef TEMPS_DESTROYED_QUICKLY_R
00035 #define ReturnMatrix ReturnMatrixX
00036 #else
00037 #define ReturnMatrix ReturnMatrixX&
00038 #endif
00039
00040
00041
00042 class GeneralMatrix;
00043 void MatrixErrorNoSpace(void*);
00044
00047 class LogAndSign {
00048
00049
00050 Real log_value;
00051 int sign;
00052 public:
00053 LogAndSign() { log_value=0.0; sign=1; }
00054 LogAndSign(Real);
00055 void operator*=(Real);
00056 void ChangeSign() { sign = -sign; }
00057 Real LogValue() const { return log_value; }
00058 int Sign() const { return sign; }
00059 Real Value() const;
00060 FREE_CHECK(LogAndSign)
00061 };
00062
00063
00064
00065
00066
00067
00068
00069
00070 #ifdef DO_REPORT
00071
00073 class ExeCounter {
00074 int line;
00075 int fileid;
00076 long nexe;
00077 static int nreports;
00078 public:
00079 ExeCounter(int,int);
00080 void operator++() { nexe++; }
00081 ~ExeCounter();
00082 };
00083 #endif
00084
00085
00086
00089 class MatrixConversionCheck {
00090 static bool DoCheck;
00091 public:
00092 MatrixConversionCheck() {
00093 DoCheck=true;
00094 }
00095 ~MatrixConversionCheck() {
00096 DoCheck=false;
00097 }
00098 void CleanUp() { DoCheck=false; }
00099 static bool IsOn() { return DoCheck; }
00100 static void DataLoss();
00101 friend class SkipConversionCheck;
00102 };
00103
00106 class SkipConversionCheck {
00107
00108 bool LastValue;
00109 public:
00110 SkipConversionCheck() : LastValue(MatrixConversionCheck::DoCheck)
00111 { MatrixConversionCheck::DoCheck = false; }
00112 ~SkipConversionCheck() { MatrixConversionCheck::DoCheck = LastValue; }
00113 void CleanUp() { MatrixConversionCheck::DoCheck = LastValue; }
00114 };
00115
00116
00117
00118
00119
00120
00121
00122 class GeneralMatrix;
00123 class BaseMatrix;
00124 class MatrixInput;
00125
00129 class MatrixType {
00130 public:
00131 enum Attribute {
00132 Valid = 1,
00133 Diagonal = 2,
00134 Symmetric = 4,
00135 Band = 8,
00136 Lower = 16,
00137 Upper = 32,
00138 LUDeco = 64
00139 };
00140
00141 enum {
00142 US = 0,
00143 UT = Valid + Upper,
00144 LT = Valid + Lower,
00145 Rt = Valid,
00146 Sm = Valid + Symmetric,
00147 Dg = Valid + Diagonal + Band + Lower + Upper + Symmetric,
00148 RV = Valid,
00149 CV = Valid,
00150 BM = Valid + Band,
00151 UB = Valid + Band + Upper,
00152 LB = Valid + Band + Lower,
00153 SB = Valid + Band + Symmetric,
00154 Ct = Valid + LUDeco,
00155 BC = Valid + Band + LUDeco
00156 };
00157
00158 static nTypes() {
00159 return 9;
00160 }
00161
00162 public:
00163 int attribute;
00164 public:
00165 MatrixType () {}
00166 MatrixType (int i) : attribute(i) {}
00167 int operator+() const { return attribute; }
00168 MatrixType operator+(MatrixType mt) const
00169 { return MatrixType(attribute & mt.attribute); }
00170 MatrixType operator*(const MatrixType&) const;
00171 MatrixType SP(const MatrixType&) const;
00172 MatrixType operator|(const MatrixType& mt) const
00173 { return MatrixType(attribute & mt.attribute & Valid); }
00174 MatrixType operator&(const MatrixType& mt) const
00175 { return MatrixType(attribute & mt.attribute & Valid); }
00176 bool operator>=(MatrixType mt) const
00177 { return ( attribute & mt.attribute ) == attribute; }
00178 bool operator<(MatrixType mt) const
00179 { return ( attribute & mt.attribute ) != attribute; }
00180 bool operator==(MatrixType t) const
00181 { return (attribute == t.attribute); }
00182 bool operator!=(MatrixType t) const
00183 { return (attribute != t.attribute); }
00184 bool operator!() const { return (attribute & Valid) == 0; }
00185 MatrixType i() const;
00186 MatrixType t() const;
00187 MatrixType AddEqualEl() const
00188 { return MatrixType(attribute & (Valid + Symmetric)); }
00189 MatrixType MultRHS() const;
00190 MatrixType sub() const
00191 { return MatrixType(attribute & Valid); }
00192 MatrixType ssub() const {
00193
00194 return MatrixType(attribute);
00195 }
00196 GeneralMatrix* New() const;
00197 GeneralMatrix* New(int,int,BaseMatrix*) const;
00198
00199 char* Value() const;
00200 friend bool Rectangular(MatrixType a, MatrixType b, MatrixType c);
00201 friend bool Compare(const MatrixType&, MatrixType&);
00202
00203 bool IsBand() const { return (attribute & Band) != 0; }
00204 bool IsDiagonal() const { return (attribute & Diagonal) != 0; }
00205 bool CannotConvert() const { return (attribute & LUDeco) != 0; }
00206
00207 FREE_CHECK(MatrixType)
00208 };
00209
00210
00211
00214 class MatrixBandWidth {
00215 public:
00216 int lower;
00217 int upper;
00218 MatrixBandWidth(const int l, const int u) : lower(l), upper (u) {}
00219 MatrixBandWidth(const int i) : lower(i), upper(i) {}
00220 MatrixBandWidth operator+(const MatrixBandWidth&) const;
00221 MatrixBandWidth operator*(const MatrixBandWidth&) const;
00222 MatrixBandWidth minimum(const MatrixBandWidth&) const;
00223 MatrixBandWidth t() const { return MatrixBandWidth(upper,lower); }
00224 bool operator==(const MatrixBandWidth& bw) const
00225 { return (lower == bw.lower) && (upper == bw.upper); }
00226 int Upper() const { return upper; }
00227 int Lower() const { return lower; }
00228 FREE_CHECK(MatrixBandWidth)
00229 };
00230
00231
00232
00233
00234
00235
00236
00239 class ArrayLengthSpecifier {
00240 int value;
00241 public:
00242 int Value() const { return value; }
00243 ArrayLengthSpecifier(int l) : value(l) {}
00244 };
00245
00246
00247
00248 class MatrixRowCol;
00249 class MatrixRow;
00250 class MatrixCol;
00251 class MatrixColX;
00252
00253 class GeneralMatrix;
00254 class AddedMatrix;
00255 class MultipliedMatrix;
00256 class SubtractedMatrix;
00257 class SPMatrix;
00258 class ConcatenatedMatrix;
00259 class StackedMatrix;
00260 class SolvedMatrix;
00261 class ShiftedMatrix;
00262 class NegShiftedMatrix;
00263 class ScaledMatrix;
00264 class TransposedMatrix;
00265 class ReversedMatrix;
00266 class NegatedMatrix;
00267 class InvertedMatrix;
00268 class RowedMatrix;
00269 class ColedMatrix;
00270 class DiagedMatrix;
00271 class MatedMatrix;
00272 class GetSubMatrix;
00273 class ReturnMatrixX;
00274 class Matrix;
00275 class nricMatrix;
00276 class RowVector;
00277 class ColumnVector;
00278 class SymmetricMatrix;
00279 class UpperTriangularMatrix;
00280 class LowerTriangularMatrix;
00281 class DiagonalMatrix;
00282 class CroutMatrix;
00283 class BandMatrix;
00284 class LowerBandMatrix;
00285 class UpperBandMatrix;
00286 class SymmetricBandMatrix;
00287 class LinearEquationSolver;
00288 class GenericMatrix;
00289
00290 static MatrixType MatrixTypeUnSp(MatrixType::US);
00291
00292
00294 class BaseMatrix : public Janitor {
00295 protected:
00296 virtual int search(const BaseMatrix*) const = 0;
00297
00298
00299
00300 public:
00301 virtual GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp) = 0;
00302
00303
00304
00305
00306 #ifndef TEMPS_DESTROYED_QUICKLY
00307
00308 AddedMatrix operator+(const BaseMatrix&) const;
00309 MultipliedMatrix operator*(const BaseMatrix&) const;
00310 SubtractedMatrix operator-(const BaseMatrix&) const;
00311 ConcatenatedMatrix operator|(const BaseMatrix&) const;
00312 StackedMatrix operator&(const BaseMatrix&) const;
00313 ShiftedMatrix operator+(Real) const;
00314 ScaledMatrix operator*(Real) const;
00315 ScaledMatrix operator/(Real) const;
00316 ShiftedMatrix operator-(Real) const;
00317 TransposedMatrix t() const;
00318
00319 NegatedMatrix operator-() const;
00320 ReversedMatrix Reverse() const;
00321 InvertedMatrix i() const;
00322
00323 RowedMatrix AsRow() const;
00324 ColedMatrix AsColumn() const;
00325 DiagedMatrix AsDiagonal() const;
00326 MatedMatrix AsMatrix(int,int) const;
00327 GetSubMatrix SubMatrix(int,int,int,int) const;
00328 GetSubMatrix SymSubMatrix(int,int) const;
00329 GetSubMatrix Row(int) const;
00330 GetSubMatrix Rows(int,int) const;
00331 GetSubMatrix Column(int) const;
00332 GetSubMatrix Columns(int,int) const;
00333 #else
00334
00335 AddedMatrix& operator+(const BaseMatrix&) const;
00336 MultipliedMatrix& operator*(const BaseMatrix&) const;
00337 SubtractedMatrix& operator-(const BaseMatrix&) const;
00338 ConcatenatedMatrix& operator|(const BaseMatrix&) const;
00339 StackedMatrix& operator&(const BaseMatrix&) const;
00340 ShiftedMatrix& operator+(Real) const;
00341 ScaledMatrix& operator*(Real) const;
00342 ScaledMatrix& operator/(Real) const;
00343 ShiftedMatrix& operator-(Real) const;
00344 TransposedMatrix& t() const;
00345
00346 NegatedMatrix& operator-() const;
00347 ReversedMatrix& Reverse() const;
00348 InvertedMatrix& i() const;
00349
00350 RowedMatrix& AsRow() const;
00351 ColedMatrix& AsColumn() const;
00352 DiagedMatrix& AsDiagonal() const;
00353 MatedMatrix& AsMatrix(int,int) const;
00354 GetSubMatrix& SubMatrix(int,int,int,int) const;
00355 GetSubMatrix& SymSubMatrix(int,int) const;
00356 GetSubMatrix& Row(int) const;
00357 GetSubMatrix& Rows(int,int) const;
00358 GetSubMatrix& Column(int) const;
00359 GetSubMatrix& Columns(int,int) const;
00360 #endif
00361 Real AsScalar() const;
00362 virtual LogAndSign LogDeterminant() const;
00363 virtual Real SumSquare() const;
00364 virtual Real SumAbsoluteValue() const;
00365 virtual Real Sum() const;
00366 virtual Real MaximumAbsoluteValue() const;
00367 virtual Real MaximumValue() const;
00368 virtual Real MinimumValue() const;
00369 virtual Real Trace() const;
00370 Real Norm1() const;
00371 Real NormInfinity() const;
00372 virtual MatrixBandWidth BandWidth() const;
00373 virtual void CleanUp() {
00374 }
00375 void IEQND() const;
00376
00377
00378
00379
00380
00381 friend class GeneralMatrix;
00382 friend class Matrix;
00383 friend class nricMatrix;
00384 friend class RowVector;
00385 friend class ColumnVector;
00386 friend class SymmetricMatrix;
00387 friend class UpperTriangularMatrix;
00388 friend class LowerTriangularMatrix;
00389 friend class DiagonalMatrix;
00390 friend class CroutMatrix;
00391 friend class BandMatrix;
00392 friend class LowerBandMatrix;
00393 friend class UpperBandMatrix;
00394 friend class SymmetricBandMatrix;
00395 friend class AddedMatrix;
00396 friend class MultipliedMatrix;
00397 friend class SubtractedMatrix;
00398 friend class SPMatrix;
00399 friend class ConcatenatedMatrix;
00400 friend class StackedMatrix;
00401 friend class SolvedMatrix;
00402 friend class ShiftedMatrix;
00403 friend class NegShiftedMatrix;
00404 friend class ScaledMatrix;
00405 friend class TransposedMatrix;
00406 friend class ReversedMatrix;
00407 friend class NegatedMatrix;
00408 friend class InvertedMatrix;
00409 friend class RowedMatrix;
00410 friend class ColedMatrix;
00411 friend class DiagedMatrix;
00412 friend class MatedMatrix;
00413 friend class GetSubMatrix;
00414 friend class ReturnMatrixX;
00415 friend class LinearEquationSolver;
00416 friend class GenericMatrix;
00417 NEW_DELETE(BaseMatrix)
00418 };
00419
00420
00421
00423 class GeneralMatrix : public BaseMatrix {
00424 virtual GeneralMatrix* Image() const;
00425 protected:
00426 int tag;
00427 int nrows, ncols;
00428 int storage;
00429 Real* store;
00430 GeneralMatrix();
00431 GeneralMatrix(ArrayLengthSpecifier);
00432 void Add(GeneralMatrix*, Real);
00433 void Add(Real);
00434 void NegAdd(GeneralMatrix*, Real);
00435 void NegAdd(Real);
00436 void Multiply(GeneralMatrix*, Real);
00437 void Multiply(Real);
00438 void Negate(GeneralMatrix*);
00439 void Negate();
00440 void ReverseElements();
00441 void ReverseElements(GeneralMatrix*);
00442 void operator=(Real);
00443 Real* GetStore();
00444 GeneralMatrix* BorrowStore(GeneralMatrix*, MatrixType);
00445
00446 void GetMatrix(const GeneralMatrix*);
00447 void Eq(const BaseMatrix&, MatrixType);
00448 void Eq2(const BaseMatrix&, MatrixType);
00449 int search(const BaseMatrix*) const;
00450 virtual GeneralMatrix* Transpose(TransposedMatrix*, MatrixType);
00451 void CheckConversion(const BaseMatrix&);
00452 void ReSize(int, int, int);
00453 public:
00454 GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
00455 virtual MatrixType Type() const = 0;
00456 int Nrows() const {
00457 return nrows;
00458 }
00459 int Ncols() const { return ncols; }
00460 int Storage() const { return storage; }
00461 Real* Store() const { return store; }
00462 virtual ~GeneralMatrix();
00463 void tDelete();
00464 bool reuse();
00465 void Protect() {
00466 tag=-1;
00467 }
00468 int Tag() const { return tag; }
00469 bool IsZero() const;
00470 void Release() {
00471 tag=1;
00472 }
00473 void Release(int t) {
00474 tag=t;
00475 }
00476 void ReleaseAndDelete() {
00477 tag=0;
00478 }
00479 void operator<<(const Real*);
00480 void operator<<(const BaseMatrix& X) { Eq(X,this->Type()); }
00481
00482 void Inject(const GeneralMatrix&);
00483 void operator+=(const BaseMatrix&);
00484 void operator-=(const BaseMatrix&);
00485 void operator*=(const BaseMatrix&);
00486 void operator|=(const BaseMatrix&);
00487 void operator&=(const BaseMatrix&);
00488 void operator+=(Real);
00489 void operator-=(Real r) { operator+=(-r); }
00490 void operator*=(Real);
00491 void operator/=(Real r) { operator*=(1.0/r); }
00492
00493 virtual GeneralMatrix* MakeSolver();
00494 virtual void Solver(MatrixColX&, const MatrixColX&) {}
00495 virtual void GetRow(MatrixRowCol&) = 0;
00496 virtual void RestoreRow(MatrixRowCol&) {
00497 }
00498 virtual void NextRow(MatrixRowCol&);
00499 virtual void GetCol(MatrixRowCol&) = 0;
00500 virtual void GetCol(MatrixColX&) = 0;
00501 virtual void RestoreCol(MatrixRowCol&) {
00502 }
00503 virtual void RestoreCol(MatrixColX&) {
00504 }
00505 virtual void NextCol(MatrixRowCol&);
00506 virtual void NextCol(MatrixColX&);
00507 Real SumSquare() const;
00508 Real SumAbsoluteValue() const;
00509 Real Sum() const;
00510 Real MaximumAbsoluteValue() const;
00511 Real MaximumValue() const;
00512 Real MinimumValue() const;
00513 LogAndSign LogDeterminant() const;
00514 virtual bool IsEqual(const GeneralMatrix&) const;
00515
00516 void CheckStore() const;
00517 virtual void SetParameters(const GeneralMatrix*) {}
00518
00519 operator ReturnMatrix() const;
00520 ReturnMatrix ForReturn() const;
00521 MatrixInput operator<<(Real);
00522
00523 void CleanUp();
00524
00525 friend class Matrix;
00526 friend class nricMatrix;
00527 friend class SymmetricMatrix;
00528 friend class UpperTriangularMatrix;
00529 friend class LowerTriangularMatrix;
00530 friend class DiagonalMatrix;
00531 friend class CroutMatrix;
00532 friend class RowVector;
00533 friend class ColumnVector;
00534 friend class BandMatrix;
00535 friend class LowerBandMatrix;
00536 friend class UpperBandMatrix;
00537 friend class SymmetricBandMatrix;
00538 friend class BaseMatrix;
00539 friend class AddedMatrix;
00540 friend class MultipliedMatrix;
00541 friend class SubtractedMatrix;
00542 friend class SPMatrix;
00543 friend class ConcatenatedMatrix;
00544 friend class StackedMatrix;
00545 friend class SolvedMatrix;
00546 friend class ShiftedMatrix;
00547 friend class NegShiftedMatrix;
00548 friend class ScaledMatrix;
00549 friend class TransposedMatrix;
00550 friend class ReversedMatrix;
00551 friend class NegatedMatrix;
00552 friend class InvertedMatrix;
00553 friend class RowedMatrix;
00554 friend class ColedMatrix;
00555 friend class DiagedMatrix;
00556 friend class MatedMatrix;
00557 friend class GetSubMatrix;
00558 friend class ReturnMatrixX;
00559 friend class LinearEquationSolver;
00560 friend class GenericMatrix;
00561 NEW_DELETE(GeneralMatrix)
00562 };
00563
00566 class Matrix : public GeneralMatrix {
00567 GeneralMatrix* Image() const;
00568 public:
00569 Matrix() {}
00570 Matrix(int, int);
00571 Matrix(const BaseMatrix&);
00572 void operator=(const BaseMatrix&);
00573 void operator=(Real f) { GeneralMatrix::operator=(f); }
00574 void operator=(const Matrix& m) { operator=((const BaseMatrix&)m); }
00575 MatrixType Type() const;
00576 Real& operator()(int, int);
00577 Real& element(int, int);
00578 #ifdef SETUP_C_SUBSCRIPTS
00579 Real* operator[](int m) { return store+m*ncols; }
00580 const Real* operator[](int m) const { return store+m*ncols; }
00581 #endif
00582 Matrix(const Matrix& gm) { GetMatrix(&gm); }
00583 Real operator()(int, int) const;
00584 GeneralMatrix* MakeSolver();
00585 Real Trace() const;
00586 void GetRow(MatrixRowCol&);
00587 void GetCol(MatrixRowCol&);
00588 void GetCol(MatrixColX&);
00589 void RestoreCol(MatrixRowCol&);
00590 void RestoreCol(MatrixColX&);
00591 void NextRow(MatrixRowCol&);
00592 void NextCol(MatrixRowCol&);
00593 void NextCol(MatrixColX&);
00594 virtual void ReSize(int,int);
00595
00596 NEW_DELETE(Matrix)
00597 };
00598
00600 class nricMatrix : public Matrix {
00601
00602 GeneralMatrix* Image() const;
00603 Real** row_pointer;
00604 void MakeRowPointer();
00605 void DeleteRowPointer();
00606 public:
00607 nricMatrix() {}
00608 nricMatrix(int m, int n)
00609 : Matrix(m,n) { MakeRowPointer(); }
00610 nricMatrix(const BaseMatrix& bm)
00611 : Matrix(bm) { MakeRowPointer(); }
00612 void operator=(const BaseMatrix& bm)
00613 { DeleteRowPointer(); Matrix::operator=(bm); MakeRowPointer(); }
00614 void operator=(Real f) { GeneralMatrix::operator=(f); }
00615 void operator=(const nricMatrix& m) { operator=((const BaseMatrix&)m); }
00616 void operator<<(const BaseMatrix& X)
00617 { DeleteRowPointer(); Eq(X,this->Type()); MakeRowPointer(); }
00618 nricMatrix(const nricMatrix& gm) { GetMatrix(&gm); MakeRowPointer(); }
00619 void ReSize(int m, int n)
00620 { DeleteRowPointer(); Matrix::ReSize(m,n); MakeRowPointer(); }
00621 ~nricMatrix() { DeleteRowPointer(); }
00622 Real** nric() const { CheckStore(); return row_pointer-1; }
00623 void CleanUp();
00624 NEW_DELETE(nricMatrix)
00625 };
00626
00628 class SymmetricMatrix : public GeneralMatrix {
00629 GeneralMatrix* Image() const;
00630 public:
00631 SymmetricMatrix() {}
00632 SymmetricMatrix(ArrayLengthSpecifier);
00633 SymmetricMatrix(const BaseMatrix&);
00634 void operator=(const BaseMatrix&);
00635 void operator=(Real f) { GeneralMatrix::operator=(f); }
00636 void operator=(const SymmetricMatrix& m) { operator=((const BaseMatrix&)m); }
00637 Real& operator()(int, int);
00638 Real& element(int, int);
00639 #ifdef SETUP_C_SUBSCRIPTS
00640 Real* operator[](int m) { return store+(m*(m+1))/2; }
00641 const Real* operator[](int m) const { return store+(m*(m+1))/2; }
00642 #endif
00643 MatrixType Type() const;
00644 SymmetricMatrix(const SymmetricMatrix& gm) { GetMatrix(&gm); }
00645 Real operator()(int, int) const;
00646 Real SumSquare() const;
00647 Real SumAbsoluteValue() const;
00648 Real Sum() const;
00649 Real Trace() const;
00650 void GetRow(MatrixRowCol&);
00651 void GetCol(MatrixRowCol&);
00652 void GetCol(MatrixColX&);
00653 void RestoreCol(MatrixRowCol&) {}
00654 void RestoreCol(MatrixColX&);
00655 GeneralMatrix* Transpose(TransposedMatrix*, MatrixType);
00656 void ReSize(int);
00657 NEW_DELETE(SymmetricMatrix)
006