00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __UT_Matrix_H__
00023 #define __UT_Matrix_H__
00024
00025 #include "UT_API.h"
00026 #include <stdlib.h>
00027 #include <iostream.h>
00028 #include <SYS/SYS_Types.h>
00029 #include "UT_Assert.h"
00030 #include "UT_Math.h"
00031 #include "UT_Matrix2.h"
00032 #include "UT_Vector.h"
00033
00034 class UT_DMatrix3;
00035 class UT_DMatrix4;
00036
00037 class UT_API UT_Matrix {
00038 public:
00039 UT_Matrix();
00040
00041
00042
00043
00044
00045
00046 UT_Matrix(int nrl, int nrh, int ncl, int nch);
00047 UT_Matrix(int nrl, int nrh, int ncl, int nch, float *array);
00048 UT_Matrix(int nrl, int ncl, UT_Matrix &mat);
00049 explicit UT_Matrix(const UT_Matrix &mat);
00050 ~UT_Matrix();
00051
00052
00053 void init(int nrl, int nrh, int ncl, int nch);
00054
00055
00056
00057
00058 void resize(int nrows, int ncols);
00059
00060
00061
00062 void appendCol( float *new_col=0 );
00063 void appendRow( float *new_row=0 );
00064
00065
00066
00067
00068
00069
00070
00071 int isInit() const { return myMatrix ? 1 : 0; }
00072
00073
00074
00075 void submatrix(const UT_Matrix &A,
00076 int nrl, int nrh, int ncl, int nch);
00077
00078
00079 void submatrix(float *array,
00080 int nrl, int nrh, int ncl, int nch, int stride=-1);
00081
00082
00083
00084 void zero(int nrl, int nrh, int ncl, int nch);
00085 void zero()
00086 { zero(myNRL, myNRH, myNCL, myNCH); }
00087
00088
00089
00090
00091
00092
00093
00094
00095 void getSubmatrix2(UT_Matrix2 &mat, int row, int col) const;
00096 void setSubmatrix2(int row, int col, const UT_Matrix2 &mat);
00097 void addSubmatrix2(int row, int col, const UT_Matrix2 &mat);
00098
00099 void getSubmatrix3(UT_DMatrix3 &mat, int row, int col) const;
00100 void setSubmatrix3(int row, int col, const UT_DMatrix3 &mat);
00101 void addSubmatrix3(int row, int col, const UT_DMatrix3 &mat);
00102
00103 void getSubmatrix4(UT_DMatrix4 &mat, int row, int col) const;
00104 void setSubmatrix4(int row, int col, const UT_DMatrix4 &mat);
00105 void addSubmatrix4(int row, int col, const UT_DMatrix4 &mat);
00106
00107
00108 void makeIdentity();
00109
00110
00111 void negate();
00112
00113
00114 int getNRL() const { return myNRL; }
00115
00116
00117 int getNRH() const { return myNRH; }
00118
00119
00120 int getNCL() const { return myNCL; }
00121
00122
00123 int getNCH() const { return myNCH; }
00124
00125
00126 int rows() const { return myNRH - myNRL + 1; }
00127
00128
00129 int columns() const { return myNCH - myNCL + 1; }
00130
00131
00132
00133 void changeNRLAndNCL(int nrl, int ncl);
00134
00135
00136
00137
00138 void setShallowNRL(int nrl) { myNRL = nrl; }
00139 void setShallowNRH(int nrh) { myNRH = nrh; }
00140 void setShallowNCL(int ncl) { myNCL = ncl; }
00141 void setShallowNCH(int nch) { myNCH = nch; }
00142
00143
00144 float &operator()(int row, int col)
00145 {
00146 UT_ASSERT_P(row >= myNRL && row <= myNRH);
00147 UT_ASSERT_P(col >= myNCL && col <= myNCH);
00148 return myMatrix[row*myStride + col];
00149 }
00150 float operator()(int row, int col) const
00151 {
00152 UT_ASSERT_P(row >= myNRL && row <= myNRH);
00153 UT_ASSERT_P(col >= myNCL && col <= myNCH);
00154 return myMatrix[row*myStride + col];
00155 }
00156
00157 UT_Matrix &operator=(const UT_Matrix &m);
00158
00159
00160 UT_Matrix &operator+=(const UT_Matrix &m);
00161 UT_Matrix &operator-=(const UT_Matrix &m);
00162
00163
00164
00165
00166
00167 void preMult( const UT_Matrix &A, UT_Matrix &result ) const;
00168
00169
00170
00171
00172
00173 void postMult( const UT_Matrix &A, UT_Matrix &result ) const;
00174
00175
00176
00177 void preMult(const UT_Vector &x, UT_Vector &result) const;
00178
00179 void postMult(const UT_Vector &x, UT_Vector &result) const
00180 { multVec(x, result); }
00181
00182
00183 void upperNormalUpdate(const UT_Matrix &A);
00184
00185
00186
00187 void outerproductUpdate(float b,
00188 const UT_Vector &x, const UT_Vector &y);
00189
00190
00191 void addScaledMatrix(const UT_Matrix &A, float scale);
00192
00193 void setAndScale(const UT_Matrix &A, float scale);
00194
00195
00196
00197 void preMultGivensInPlace(float c, float s);
00198 void postMultGivensInPlace(float c, float s);
00199
00200 void multVec(const UT_Vector &x, UT_Vector &result) const;
00201
00202
00203 float rowsL2dist(int r1, int r2, int cl=-1, int ch=-1) const;
00204
00205
00206 float normFrobenius() const;
00207 float norm1() const;
00208 float normInfinite() const;
00209
00210
00211 void transpose( UT_Matrix &result ) const;
00212
00213
00214 bool isSymmetric(fpreal64 tolerance = UT_FTOLERANCE) const;
00215
00216
00217
00218 float *row(int i) const
00219 {
00220 UT_ASSERT_P(i >= myNRL && i <= myNRH);
00221 return &myMatrix[i * myStride];
00222 }
00223
00224 void clearAndDestroy();
00225
00226 int save(ostream &os, int binary) const;
00227
00228
00229 friend ostream &operator<<(ostream &os, const UT_Matrix &m )
00230 {
00231 m.outTo(os);
00232 return os;
00233 }
00234 protected:
00235
00236 void outTo( ostream &os ) const;
00237
00238 private:
00239 int myNRL, myNRH, myNCL, myNCH;
00240 float *myMatrix;
00241 int myStride, myOffset;
00242 char myOwnData;
00243 };
00244
00245 #endif