00001 #ifndef SimTK_SIMMATRIX_MATRIX_HELPER_H_
00002 #define SimTK_SIMMATRIX_MATRIX_HELPER_H_
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
00061 #include "SimTKcommon/Scalar.h"
00062
00063 #include <iostream>
00064 #include <cassert>
00065 #include <complex>
00066 #include <cstddef>
00067
00068 namespace SimTK {
00069
00070
00071 template <class S> class MatrixHelperRep;
00072
00077 class MatrixStructures {
00078 public:
00079 enum Structure {
00080 Uncommitted = 0,
00081 Full = 1,
00082 Diagonal = 2,
00083 Symmetric = 3,
00084 Triangular = 4,
00085 QuasiTriangular = 5,
00086 Hessenberg = 6,
00087 Permutation = 7,
00088 TriDiagonal = 8
00089 };
00090 };
00091
00092 class MatrixShapes {
00093 public:
00094 enum Shape {
00095 Uncommitted = 0,
00096 General = 1,
00097 Square = 2,
00098 Vector = 3,
00099 RowVector = 4
00100 };
00101 };
00102
00103 class MatrixSparseFormats {
00104 public:
00105 enum Sparsity {
00106 Uncommitted = 0,
00107 Full = 1,
00108 Banded = 2
00109 };
00110 };
00111 class MatrixStorageFormats {
00112 public:
00113 enum Storage {
00114 Uncommitted = 0,
00115 Conventional = 1,
00116 Packed = 2,
00117 HouseholderProduct = 3,
00118 PivotArray = 4
00119 };
00120 };
00121 class MatrixConditions {
00122 public:
00123 enum Condition {
00124 Uncommitted = 0,
00125 Unknown = 1,
00126 Orthogonal = 2,
00127 PositiveDefinite = 3,
00128 WellConditioned = 4,
00129 FullRank = 5,
00130 Singular = 6
00131 };
00132 };
00133
00134 template <class S>
00135 class SimTK_SimTKCOMMON_EXPORT MatrixHelper {
00136 typedef typename CNT<S>::Number Number;
00137 typedef typename CNT<S>::StdNumber StdNumber;
00138 typedef typename CNT<S>::Precision Precision;
00139 public:
00140
00141
00142
00143 ~MatrixHelper();
00144
00145
00146 class ShallowCopy { };
00147 class DeepCopy { };
00148 class TransposeView { };
00149 class DiagonalView { };
00150
00151
00152
00153
00154 explicit MatrixHelper(int esz, int cppEsz);
00155
00156
00157
00158 void clear();
00159
00160
00161 MatrixHelper(int esz, int cppEsz, int m, int n, bool lockNrow=false, bool lockNcol=false);
00162
00163
00164 MatrixHelper(int esz, int cppEsz, int m, int n, int leadingDim, const S*);
00165 MatrixHelper(int esz, int cppEsz, int m, int n, int leadingDim, S*);
00166
00167
00168
00169
00170 MatrixHelper(const MatrixHelper&, int i, int j, int nrow, int ncol);
00171 MatrixHelper(MatrixHelper&, int i, int j, int nrow, int ncol);
00172
00173 MatrixHelper(const MatrixHelper<typename CNT<S>::THerm>&,
00174 const TransposeView&);
00175 MatrixHelper(MatrixHelper<typename CNT<S>::THerm>&,
00176 const TransposeView&);
00177
00178
00179 MatrixHelper(const MatrixHelper&, const DiagonalView&);
00180 MatrixHelper(MatrixHelper&, const DiagonalView&);
00181
00182
00183
00184
00185 MatrixHelper(const MatrixHelper&, const ShallowCopy&);
00186 MatrixHelper(MatrixHelper&, const ShallowCopy&);
00187 MatrixHelper(const MatrixHelper&, const DeepCopy&);
00188
00189
00190 MatrixHelper(const MatrixHelper<typename CNT<S>::TNeg>&, const DeepCopy&);
00191
00192
00193
00194
00195
00196 MatrixHelper& copyAssign(const MatrixHelper&);
00197 MatrixHelper& negatedCopyAssign(const MatrixHelper<typename CNT<S>::TNeg>&);
00198
00199
00200
00201
00202
00203 MatrixHelper& readOnlyViewAssign(const MatrixHelper&);
00204 MatrixHelper& writableViewAssign(MatrixHelper&);
00205
00206
00207
00208
00209 const S* getElt(int i, int j) const;
00210 S* updElt(int i, int j);
00211
00212
00213
00214
00215 void sum(S* eltp) const;
00216 void colSum(int j, S* eltp) const;
00217 void rowSum(int i, S* eltp) const;
00218
00219
00220 void addIn(const MatrixHelper&);
00221 void addIn(const MatrixHelper<typename CNT<S>::TNeg>&);
00222 void subIn(const MatrixHelper&);
00223 void subIn(const MatrixHelper<typename CNT<S>::TNeg>&);
00224
00225 void fillWith(const S* eltp);
00226 void copyInByRows(const S* elts);
00227
00228
00229
00230
00231 void fillWithScalar(const StdNumber&);
00232
00233
00234
00235 void scaleBy(const StdNumber&);
00236
00237
00238
00239 S scalarSum() const;
00240 S scalarColSum(int j) const;
00241 S scalarRowSum(int i) const;
00242
00243
00244
00245
00246 void invertInPlace();
00247
00248 void dump(const char* msg=0) const;
00249
00250
00251 template <class SA, class SB>
00252 void matmul(const StdNumber& beta,
00253 const StdNumber& alpha, const MatrixHelper<SA>& A, const MatrixHelper<SB>& B);
00254
00255
00256
00257 const S& getScalar(int i, int j) const;
00258 S& updScalar(int i, int j);
00259
00260
00261
00262 int nrow() const;
00263 int ncol() const;
00264 long size() const;
00265
00266 void resize(int m, int n);
00267 void resizeKeep(int m, int n);
00268
00269 void lockNRows();
00270 void lockNCols();
00271 void lockShape();
00272 void unlockNRows();
00273 void unlockNCols();
00274 void unlockShape();
00275
00276 void setMatrixStructure(MatrixStructures::Structure );
00277 MatrixStructures::Structure getMatrixStructure() const;
00278 void setMatrixShape(MatrixShapes::Shape );
00279 MatrixShapes::Shape getMatrixShape() const;
00280 void setMatrixSparsity(MatrixSparseFormats::Sparsity );
00281 MatrixSparseFormats::Sparsity getMatrixSparsity() const;
00282 void setMatrixStorage(MatrixStorageFormats::Storage );
00283 MatrixStorageFormats::Storage getMatrixStorage() const;
00284 void setMatrixCondition(MatrixConditions::Condition );
00285 MatrixConditions::Condition getMatrixCondition() const;
00286
00287
00288
00289 bool hasContiguousData() const;
00290 long getContiguousDataLength() const;
00291 const S* getContiguousData() const;
00292 S* updContiguousData();
00293 void replaceContiguousData(S* newData, long length, bool takeOwnership);
00294 void replaceContiguousData(const S* newData, long length);
00295 void swapOwnedContiguousData(S* newData, int length, S*& oldData);
00296
00297
00298 class MatrixHelperRep<S>* rep;
00299
00300 private:
00301
00302 MatrixHelper(const MatrixHelper&);
00303 };
00304
00305 }
00306
00307 #endif // SimTK_SIMMATRIX_MATRIX_HELPER_H_