00001
00002
00003
00004
00005 #ifndef NEWMAT_LIB
00006 #define NEWMAT_LIB 0
00007
00008 #include "include.h"
00009
00010 #include "myexcept.h"
00011
00012
00013 #ifdef use_namespace
00014 namespace NEWMAT { using namespace RBD_COMMON; }
00015 namespace RBD_LIBRARIES { using namespace NEWMAT; }
00016 namespace NEWMAT {
00017 #endif
00018
00019
00020
00021 #ifdef NO_LONG_NAMES
00022 #define UpperTriangularMatrix UTMatrix
00023 #define LowerTriangularMatrix LTMatrix
00024 #define SymmetricMatrix SMatrix
00025 #define DiagonalMatrix DMatrix
00026 #define BandMatrix BMatrix
00027 #define UpperBandMatrix UBMatrix
00028 #define LowerBandMatrix LBMatrix
00029 #define SymmetricBandMatrix SBMatrix
00030 #define BandLUMatrix BLUMatrix
00031 #endif
00032
00033
00034
00035 class GeneralMatrix;
00036
00037 void MatrixErrorNoSpace(const void*);
00038
00039 class LogAndSign
00040
00041
00042 {
00043 Real log_value;
00044 int sign;
00045 public:
00046 LogAndSign() { log_value=0.0; sign=1; }
00047 LogAndSign(Real);
00048 void operator*=(Real);
00049 void PowEq(int k);
00050 void ChangeSign() { sign = -sign; }
00051 Real LogValue() const { return log_value; }
00052 int Sign() const { return sign; }
00053 Real Value() const;
00054 FREE_CHECK(LogAndSign)
00055 };
00056
00057
00058
00059
00060
00061
00062
00063
00064 #ifdef DO_REPORT
00065
00066 class ExeCounter
00067 {
00068 int line;
00069 int fileid;
00070 long nexe;
00071 static int nreports;
00072 public:
00073 ExeCounter(int,int);
00074 void operator++() { nexe++; }
00075 ~ExeCounter();
00076 };
00077
00078 #endif
00079
00080
00081
00082
00083
00084
00085
00086
00087 class GeneralMatrix;
00088 class BaseMatrix;
00089 class MatrixInput;
00090
00091 class MatrixType
00092 {
00093 public:
00094 enum Attribute { Valid = 1,
00095 Diagonal = 2,
00096 Symmetric = 4,
00097 Band = 8,
00098 Lower = 16,
00099 Upper = 32,
00100 Square = 64,
00101 Skew = 128,
00102 LUDeco = 256,
00103 Ones = 512 };
00104
00105 enum { US = 0,
00106 UT = Valid + Upper + Square,
00107 LT = Valid + Lower + Square,
00108 Rt = Valid,
00109 Sq = Valid + Square,
00110 Sm = Valid + Symmetric + Square,
00111 Sk = Valid + Skew + Square,
00112 Dg = Valid + Diagonal + Band + Lower + Upper + Symmetric
00113 + Square,
00114 Id = Valid + Diagonal + Band + Lower + Upper + Symmetric
00115 + Square + Ones,
00116 RV = Valid,
00117 CV = Valid,
00118 BM = Valid + Band + Square,
00119 UB = Valid + Band + Upper + Square,
00120 LB = Valid + Band + Lower + Square,
00121 SB = Valid + Band + Symmetric + Square,
00122 Ct = Valid + LUDeco + Square,
00123 BC = Valid + Band + LUDeco + Square,
00124 Mask = ~Square
00125 };
00126
00127
00128 static int nTypes() { return 12; }
00129
00130 public:
00131 int attribute;
00132 bool DataLossOK;
00133
00134 public:
00135 MatrixType () : DataLossOK(false) {}
00136 MatrixType (int i) : attribute(i), DataLossOK(false) {}
00137 MatrixType (int i, bool dlok) : attribute(i), DataLossOK(dlok) {}
00138 MatrixType (const MatrixType& mt)
00139 : attribute(mt.attribute), DataLossOK(mt.DataLossOK) {}
00140 void operator=(const MatrixType& mt)
00141 { attribute = mt.attribute; DataLossOK = mt.DataLossOK; }
00142 void SetDataLossOK() { DataLossOK = true; }
00143 int operator+() const { return attribute; }
00144 MatrixType operator+(MatrixType mt) const
00145 { return MatrixType(attribute & mt.attribute); }
00146 MatrixType operator*(const MatrixType&) const;
00147 MatrixType SP(const MatrixType&) const;
00148 MatrixType KP(const MatrixType&) const;
00149 MatrixType operator|(const MatrixType& mt) const
00150 { return MatrixType(attribute & mt.attribute & Valid); }
00151 MatrixType operator&(const MatrixType& mt) const
00152 { return MatrixType(attribute & mt.attribute & Valid); }
00153 bool operator>=(MatrixType mt) const
00154 { return ( attribute & ~mt.attribute & Mask ) == 0; }
00155 bool operator<(MatrixType mt) const
00156 { return ( attribute & ~mt.attribute & Mask ) != 0; }
00157 bool operator==(MatrixType t) const
00158 { return (attribute == t.attribute); }
00159 bool operator!=(MatrixType t) const
00160 { return (attribute != t.attribute); }
00161 bool operator!() const { return (attribute & Valid) == 0; }
00162 MatrixType i() const;
00163 MatrixType t() const;
00164 MatrixType AddEqualEl() const
00165 { return MatrixType(attribute & (Valid + Symmetric + Square)); }
00166 MatrixType MultRHS() const;
00167 MatrixType sub() const
00168 { return MatrixType(attribute & Valid); }
00169 MatrixType ssub() const
00170 { return MatrixType(attribute); }
00171 GeneralMatrix* New() const;
00172 GeneralMatrix* New(int,int,BaseMatrix*) const;
00173
00174 const char* Value() const;
00175 friend bool Rectangular(MatrixType a, MatrixType b, MatrixType c);
00176 friend bool Compare(const MatrixType&, MatrixType&);
00177
00178 bool IsBand() const { return (attribute & Band) != 0; }
00179 bool IsDiagonal() const { return (attribute & Diagonal) != 0; }
00180 bool IsSymmetric() const { return (attribute & Symmetric) != 0; }
00181 bool CannotConvert() const { return (attribute & LUDeco) != 0; }
00182
00183 FREE_CHECK(MatrixType)
00184 };
00185
00186
00187
00188
00189 class MatrixBandWidth
00190 {
00191 public:
00192 int lower;
00193 int upper;
00194 MatrixBandWidth(const int l, const int u) : lower(l), upper (u) {}
00195 MatrixBandWidth(const int i) : lower(i), upper(i) {}
00196 MatrixBandWidth operator+(const MatrixBandWidth&) const;
00197 MatrixBandWidth operator*(const MatrixBandWidth&) const;
00198 MatrixBandWidth minimum(const MatrixBandWidth&) const;
00199 MatrixBandWidth t() const { return MatrixBandWidth(upper,lower); }
00200 bool operator==(const MatrixBandWidth& bw) const
00201 { return (lower == bw.lower) && (upper == bw.upper); }
00202 bool operator!=(const MatrixBandWidth& bw) const { return !operator==(bw); }
00203 int Upper() const { return upper; }
00204 int Lower() const { return lower; }
00205 FREE_CHECK(MatrixBandWidth)
00206 };
00207
00208
00209
00210
00211
00212
00213
00214
00215 class ArrayLengthSpecifier
00216 {
00217 int value;
00218 public:
00219 int Value() const { return value; }
00220 ArrayLengthSpecifier(int l) : value(l) {}
00221 };
00222
00223
00224
00225
00226 class MatrixRowCol;
00227 class MatrixRow;
00228 class MatrixCol;
00229 class MatrixColX;
00230
00231 class GeneralMatrix;
00232 class AddedMatrix;
00233 class MultipliedMatrix;
00234 class SubtractedMatrix;
00235 class SPMatrix;
00236 class KPMatrix;
00237 class ConcatenatedMatrix;
00238 class StackedMatrix;
00239 class SolvedMatrix;
00240 class ShiftedMatrix;
00241 class NegShiftedMatrix;
00242 class ScaledMatrix;
00243 class TransposedMatrix;
00244 class ReversedMatrix;
00245 class NegatedMatrix;
00246 class InvertedMatrix;
00247 class RowedMatrix;
00248 class ColedMatrix;
00249 class DiagedMatrix;
00250 class MatedMatrix;
00251 class GetSubMatrix;
00252 class ReturnMatrix;
00253 class Matrix;
00254 class SquareMatrix;
00255 class nricMatrix;
00256 class RowVector;
00257 class ColumnVector;
00258 class SymmetricMatrix;
00259 class UpperTriangularMatrix;
00260 class LowerTriangularMatrix;
00261 class DiagonalMatrix;
00262 class CroutMatrix;
00263 class BandMatrix;
00264 class LowerBandMatrix;
00265 class UpperBandMatrix;
00266 class SymmetricBandMatrix;
00267 class LinearEquationSolver;
00268 class GenericMatrix;
00269
00270
00271 #define MatrixTypeUnSp 0
00272
00273
00274
00275 class BaseMatrix : public Janitor
00276 {
00277 protected:
00278 virtual int search(const BaseMatrix*) const = 0;
00279
00280
00281
00282 public:
00283 virtual GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp) = 0;
00284
00285
00286
00287
00288 AddedMatrix operator+(const BaseMatrix&) const;
00289 MultipliedMatrix operator*(const BaseMatrix&) const;
00290 SubtractedMatrix operator-(const BaseMatrix&) const;
00291 ConcatenatedMatrix operator|(const BaseMatrix&) const;
00292 StackedMatrix operator&(const BaseMatrix&) const;
00293 ShiftedMatrix operator+(Real) const;
00294 ScaledMatrix operator*(Real) const;
00295 ScaledMatrix operator/(Real) const;
00296 ShiftedMatrix operator-(Real) const;
00297 TransposedMatrix t() const;
00298
00299 NegatedMatrix operator-() const;
00300 ReversedMatrix Reverse() const;
00301 InvertedMatrix i() const;
00302
00303 RowedMatrix AsRow() const;
00304 ColedMatrix AsColumn() const;
00305 DiagedMatrix AsDiagonal() const;
00306 MatedMatrix AsMatrix(int,int) const;
00307 GetSubMatrix SubMatrix(int,int,int,int) const;
00308 GetSubMatrix SymSubMatrix(int,int) const;
00309 GetSubMatrix Row(int) const;
00310 GetSubMatrix Rows(int,int) const;
00311 GetSubMatrix Column(int) const;
00312 GetSubMatrix Columns(int,int) const;
00313 Real AsScalar() const;
00314 virtual LogAndSign LogDeterminant() const;
00315 Real Determinant() const;
00316 virtual Real SumSquare() const;
00317 Real NormFrobenius() const;
00318 virtual Real SumAbsoluteValue() const;
00319 virtual Real Sum() const;
00320 virtual Real MaximumAbsoluteValue() const;
00321 virtual Real MaximumAbsoluteValue1(int& i) const;
00322 virtual Real MaximumAbsoluteValue2(int& i, int& j) const;
00323 virtual Real MinimumAbsoluteValue() const;
00324 virtual Real MinimumAbsoluteValue1(int& i) const;
00325 virtual Real MinimumAbsoluteValue2(int& i, int& j) const;
00326 virtual Real Maximum() const;
00327 virtual Real Maximum1(int& i) const;
00328 virtual Real Maximum2(int& i, int& j) const;
00329 virtual Real Minimum() const;
00330 virtual Real Minimum1(int& i) const;
00331 virtual Real Minimum2(int& i, int& j) const;
00332 virtual Real Trace() const;
00333 Real Norm1() const;
00334 Real NormInfinity() const;
00335 virtual MatrixBandWidth BandWidth() const;
00336 virtual void CleanUp() {}
00337 void IEQND() const;
00338
00339
00340
00341
00342 friend class GeneralMatrix;
00343 friend class Matrix;
00344 friend class SquareMatrix;
00345 friend class nricMatrix;
00346 friend class RowVector;
00347 friend class ColumnVector;
00348 friend class SymmetricMatrix;
00349 friend class UpperTriangularMatrix;
00350 friend class LowerTriangularMatrix;
00351 friend class DiagonalMatrix;
00352 friend class CroutMatrix;
00353 friend class BandMatrix;
00354 friend class LowerBandMatrix;
00355 friend class UpperBandMatrix;
00356 friend class SymmetricBandMatrix;
00357 friend class AddedMatrix;
00358 friend class MultipliedMatrix;
00359 friend class SubtractedMatrix;
00360 friend class SPMatrix;
00361 friend class KPMatrix;
00362 friend class ConcatenatedMatrix;
00363 friend class StackedMatrix;
00364 friend class SolvedMatrix;
00365 friend class ShiftedMatrix;
00366 friend class NegShiftedMatrix;
00367 friend class ScaledMatrix;
00368 friend class TransposedMatrix;
00369 friend class ReversedMatrix;
00370 friend class NegatedMatrix;
00371 friend class InvertedMatrix;
00372 friend class RowedMatrix;
00373 friend class ColedMatrix;
00374 friend class DiagedMatrix;
00375 friend class MatedMatrix;
00376 friend class GetSubMatrix;
00377 friend class ReturnMatrix;
00378 friend class LinearEquationSolver;
00379 friend class GenericMatrix;
00380 NEW_DELETE(BaseMatrix)
00381 };
00382
00383
00384
00385
00386 class GeneralMatrix : public BaseMatrix
00387 {
00388 virtual GeneralMatrix* Image() const;
00389 protected:
00390 int tag;
00391 int nrows_value, ncols_value;
00392 int storage;
00393 Real* store;
00394 GeneralMatrix();
00395 GeneralMatrix(ArrayLengthSpecifier);
00396 void Add(GeneralMatrix*, Real);
00397 void Add(Real);
00398 void NegAdd(GeneralMatrix*, Real);
00399 void NegAdd(Real);
00400 void Multiply(GeneralMatrix*, Real);
00401 void Multiply(Real);
00402 void Negate(GeneralMatrix*);
00403 void Negate();
00404 void ReverseElements();
00405 void ReverseElements(GeneralMatrix*);
00406 void operator=(Real);
00407 Real* GetStore();
00408 GeneralMatrix* BorrowStore(GeneralMatrix*, MatrixType);
00409
00410 void GetMatrix(const GeneralMatrix*);
00411 void Eq(const BaseMatrix&, MatrixType);
00412 void Eq(const GeneralMatrix&);
00413 void Eq(const BaseMatrix&, MatrixType, bool);
00414 void Eq2(const BaseMatrix&, MatrixType);
00415 int search(const BaseMatrix*) const;
00416 virtual GeneralMatrix* Transpose(TransposedMatrix*, MatrixType);
00417 void CheckConversion(const BaseMatrix&);
00418 void ReSize(int, int, int);
00419 virtual short SimpleAddOK(const GeneralMatrix* ) { return 0; }
00420
00421 virtual void MiniCleanUp()
00422 { store = 0; storage = 0; nrows_value = 0; ncols_value = 0; tag = -1;}
00423
00424 void PlusEqual(const GeneralMatrix& gm);
00425 void MinusEqual(const GeneralMatrix& gm);
00426 void PlusEqual(Real f);
00427 void MinusEqual(Real f);
00428 void swap(GeneralMatrix& gm);
00429 public:
00430 GeneralMatrix* Evaluate(MatrixType mt=MatrixTypeUnSp);
00431 virtual MatrixType Type() const = 0;
00432 int Nrows() const { return nrows_value; }
00433 int Ncols() const { return ncols_value; }
00434 int Storage() const { return storage; }
00435 Real* Store() const { return store; }
00436
00437 int nrows() const { return nrows_value; }
00438 int ncols() const { return ncols_value; }
00439 int size() const { return storage; }
00440 Real* data() { return store; }
00441 const Real* data() const { return store; }
00442 const Real* const_data() const { return store; }
00443 virtual ~GeneralMatrix();
00444 void tDelete();
00445 bool reuse();
00446 void Protect() { tag=-1; }
00447 int Tag() const { return tag; }
00448 bool IsZero() const;
00449 void Release() { tag=1; }
00450 void Release(int t) { tag=t; }
00451 void ReleaseAndDelete() { tag=0; }
00452 void operator<<(const Real*);
00453 void operator<<(const int*);
00454 void operator<<(const BaseMatrix& X)
00455 { Eq(X,this->Type(),true); }
00456 void Inject(const GeneralMatrix&);
00457 void operator+=(const BaseMatrix&);
00458 void operator-=(const BaseMatrix&);
00459 void operator*=(const BaseMatrix&);
00460 void operator|=(const BaseMatrix&);
00461 void operator&=(const BaseMatrix&);
00462 void operator+=(Real);
00463 void operator-=(Real r) { operator+=(-r); }
00464 void operator*=(Real);
00465 void operator/=(Real r) { operator*=(1.0/r); }
00466 virtual GeneralMatrix* MakeSolver();
00467 virtual void Solver(MatrixColX&, const MatrixColX&) {}
00468 virtual void GetRow(MatrixRowCol&) = 0;
00469 virtual void RestoreRow(MatrixRowCol&) {}
00470 virtual void NextRow(MatrixRowCol&);
00471 virtual void GetCol(MatrixRowCol&) = 0;
00472 virtual void GetCol(MatrixColX&) = 0;
00473 virtual void RestoreCol(MatrixRowCol&) {}
00474 virtual void RestoreCol(MatrixColX&) {}
00475 virtual void NextCol(MatrixRowCol&);
00476 virtual void NextCol(MatrixColX&);
00477 Real SumSquare() const;
00478 Real SumAbsoluteValue() const;
00479 Real Sum() const;
00480 Real MaximumAbsoluteValue1(int& i) const;
00481 Real MinimumAbsoluteValue1(int& i) const;
00482 Real Maximum1(int& i) const;
00483 Real Minimum1(int& i) const;
00484 Real MaximumAbsoluteValue() const;
00485 Real MaximumAbsoluteValue2(int& i, int& j) const;
00486 Real MinimumAbsoluteValue() const;
00487 Real MinimumAbsoluteValue2(int& i, int& j) const;
00488 Real Maximum() const;
00489 Real Maximum2(int& i, int& j) const;
00490 Real Minimum() const;
00491 Real Minimum2(int& i, int& j) const;
00492 LogAndSign LogDeterminant() const;
00493 virtual bool IsEqual(const GeneralMatrix&) const;
00494
00495 void CheckStore() const;
00496 virtual void SetParameters(const GeneralMatrix*) {}
00497
00498 operator ReturnMatrix() const;