#include <HxMatrix.h>
Operators | |
| HxTMatrix< C > | setRow (long r, HxVector v, bool makeCopy=false) |
| replaces row r with v; if(makeCopy) it does not change the original. More... | |
| HxTMatrix< C > | setCol (long c, HxVector v, bool makeCopy=false) |
| replaces colomn c with v; if(makeCopy) it does not change the original. More... | |
| HxTMatrix< C > & | operator= (C a) |
| Assign constant value. More... | |
| HxTMatrix< C > & | operator= (const HxTMatrix< C > &m) |
| Normal assigment. More... | |
| C * | operator[] (int i) const |
| Subscripting, start with 0. More... | |
| HxTMatrix< C > | operator- () const |
| Unary minus. More... | |
| HxTMatrix< C > | operator * (double val) const |
| Multiplication. More... | |
| HxTMatrix< C > | operator * (const HxTMatrix< C > &a) const |
| Multiplication. More... | |
| HxTMatrix< C > | operator/ (double val) const |
| Division. More... | |
| HxTMatrix< C > | operator+ (const HxTMatrix< C > &a) const |
| Addition. More... | |
| HxTMatrix< C > | operator+ (double val) const |
| Addition. More... | |
| HxTMatrix< C > | operator- (const HxTMatrix< C > &a) const |
| Subtraction. More... | |
| HxTMatrix< C > | operator- (double val) const |
| Subtraction. More... | |
| int | operator== (const HxTMatrix< C > &a) const |
| Equal. More... | |
| int | operator!= (const HxTMatrix< C > &a) const |
| Not equal. More... | |
| HxVec3Double | operator * (const HxVec3Double &b) const |
| Multiplication. More... | |
| HxVector | operator * (const HxVector &, const HxTMatrix< C > &) |
| Multiplication. More... | |
| HxVector | operator * (const HxTMatrix< C > &, const HxVector &) |
| Multiplication. More... | |
| HxTMatrix< C > | operator/ (double a, const HxTMatrix< C > &b) |
| Division. More... | |
Public Methods | |
| ~HxTMatrix () | |
| convert HxTMatrix of type U to type C. More... | |
| std::ostream & | put (std::ostream &os) const |
Constructors | |
| HxTMatrix () | |
| Empty matrix of type C. More... | |
| HxTMatrix (int nrow, int ncol) | |
| Empty matrix of type C, with given number of rows and columns. More... | |
| HxTMatrix (int nrow, int ncol, C a) | |
| HxTMatrix of type C, with constant value. More... | |
| HxTMatrix (int nrow, int ncol, C *data) | |
| HxTMatrix of type C, with given data. More... | |
| HxTMatrix (const HxString fileName, bool bin=true) | |
| construct HxTMatrix from [binary|text] file. More... | |
| HxTMatrix (const HxTMatrix &m) | |
| Copy constructor. More... | |
| HxTMatrix (const HxVector &v) | |
| Copy from vector constructor. More... | |
Inquiry | |
| int | nRow () const |
| Number of rows. More... | |
| int | nCol () const |
| Number of columns. More... | |
| int | nElem () const |
| Number of elements. More... | |
| int | valid () const |
| Indicates whether the matrix is valid. More... | |
| HxVector | getRow (long r) const |
| returns row r. More... | |
| HxVector | getCol (long c) const |
| returns colomn c. More... | |
Operations | |
| HxTMatrix< C > | delRow (long r) const |
| deletes row r from the matrix. More... | |
| HxTMatrix< C > | delCol (long c) const |
| deletes colomn c from the matrix. More... | |
| void | write2disk (const HxString fileName, bool bin=true) const |
| write matrix to [binary|text] file. More... | |
| HxTMatrix< C > | i () const |
| Inverse. More... | |
| HxTMatrix< C > | t () const |
| Transpose. More... | |
| HxTMatrix< C > | covariance (HxVector &mean=HxVector()) const |
| Calculate covariance matrix. More... | |
| HxTMatrix< C > | svd (HxVector &W, HxTMatrix< C > &V) const |
| Singular Value Decomposition. More... | |
| HxTMatrix< C > | klm (int newDim) const |
| Karhunen Loeve mapping (PCA) Principal Componant Analysis to reduce matrix to newDim dimensions. More... | |
| void | kmeans (HxTMatrix< C > &c, HxVector &imap, int numloop, bool cIsInitialized) const |
| K-means clustering. More... | |
| HxTMatrix< C > | add (const HxTMatrix< C > &b) const |
| Addition. More... | |
| HxTMatrix< C > | add (const double val) const |
| Addition. More... | |
| HxTMatrix< C > | sub (const HxTMatrix< C > &b) const |
| Subtraction. More... | |
| HxTMatrix< C > | sub (const double val) const |
| Subtraction. More... | |
| HxTMatrix< C > | mul (const HxTMatrix< C > &b) const |
| Multiplication. More... | |
| HxTMatrix< C > | mul (const HxVector &v) const |
| Multiplication. More... | |
| HxTMatrix< C > | mul (const double val) const |
| Multiplication. More... | |
| HxTMatrix< C > | div (const double val) const |
| Division. More... | |
| HxTMatrix< C > | sin () const |
| Apply sin to each element. More... | |
| HxTMatrix< C > | cos () const |
| Apply cos to each element. More... | |
| HxTMatrix< C > | tan () const |
| Apply tan to each element. More... | |
| HxTMatrix< C > | sinh () const |
| Apply sinh to each element. More... | |
| HxTMatrix< C > | cosh () const |
| Apply cosh to each element. More... | |
| HxTMatrix< C > | tanh () const |
| Apply tanh to each element. More... | |
| HxTMatrix< C > | exp () const |
| Apply exp to each element. More... | |
| HxTMatrix< C > | log () const |
| Apply log to each element. More... | |
| HxTMatrix< C > | sqrt () const |
| Apply sqrt to each element. More... | |
| HxTMatrix< C > | abs () const |
| Apply abs to each element. More... | |
| HxTMatrix< C > | sgn () const |
| Apply sgn to each element. More... | |
| HxTMatrix< C > | map (C(*f)(C)) const |
| Map f to each element of this. More... | |
| HxTMatrix< C > | app (const HxTMatrix< C > &b) const |
| append V at the bottom of this. More... | |
Static Public Methods | |
HxTMatrix generation | |
Generate coordinate transformation matrices for postfix vector multiplication | |
| HxTMatrix< C > | translate2d (C x, C y) |
| Translation in 2D. More... | |
| HxTMatrix< C > | scale2d (double sx, double sy) |
| Scaling in 2D. More... | |
| HxTMatrix< C > | rotate2d (double alpha) |
| Rotation in 2D (alpha in rad). More... | |
| HxTMatrix< C > | rotate2dDeg (double alpha) |
| Rotation in 2D (alpha in deg). More... | |
| HxTMatrix< C > | reflect2d (int doX, int doY) |
| Reflection in 2D (if (doX != 0) reflect X), etc. More... | |
| HxTMatrix< C > | shear2d (double sx, double sy) |
| Shearing in 2D. More... | |
| HxTMatrix< C > | translate3d (double x, double y, double z) |
| Translation in 3D. More... | |
| HxTMatrix< C > | scale3d (double sx, double sy, double sz) |
| Scaling in 3D. More... | |
| HxTMatrix< C > | rotateX3d (double alpha) |
| Rotation around X-axis in 3D (alpha in rad). More... | |
| HxTMatrix< C > | rotateX3dDeg (double alpha) |
| Rotation around X-axis in 3D (alpha in deg). More... | |
| HxTMatrix< C > | rotateY3d (double alpha) |
| Rotation around Y-axis in 3D (alpha in rad). More... | |
| HxTMatrix< C > | rotateY3dDeg (double alpha) |
| Rotation around Y-axis in 3D (alpha in deg). More... | |
| HxTMatrix< C > | rotateZ3d (double alpha) |
| Rotation around Z-axis in 3D (alpha in rad). More... | |
| HxTMatrix< C > | rotateZ3dDeg (double alpha) |
| Rotation around Z-axis in 3D (alpha in deg). More... | |
| HxTMatrix< C > | reflect3d (int doX, int doY, int doZ) |
| Reflection in 3D (if (doX != 0) reflect X), etc. More... | |
| HxTMatrix< C > | projection (double f) |
| Projection matrix. More... | |
| HxTMatrix< C > | camera (double f) |
| Camera transformation. More... | |
| HxTMatrix< C > | lift2dTo3dXY () |
| Lift 2D plane to 3D XY-plane. More... | |
Friends | |
| class | HxVector |
The dimensions are of arbitrary size.
|
|||||||||
|
Empty matrix of type C.
|
|
||||||||||||||||
|
Empty matrix of type C, with given number of rows and columns.
|
|
||||||||||||||||||||
|
HxTMatrix of type C, with constant value.
|
|
||||||||||||||||||||
|
HxTMatrix of type C, with given data.
|
|
||||||||||||||||
|
construct HxTMatrix from [binary|text] file.
|
|
||||||||||
|
Copy constructor.
|
|
||||||||||
|
Copy from vector constructor.
|
|
|||||||||
|
convert HxTMatrix of type U to type C.
|
|
||||||||||||||||
|
Translation in 2D.
00963 {
00964 HxTMatrix<C> m(3,3);
00965 m[0][0] = 1; m[0][1] = 0; m[0][2] = x;
00966 m[1][0] = 0; m[1][1] = 1; m[1][2] = y;
00967 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1;
00968 /* prefix:
00969 m[0][0] = 1; m[0][1] = 0; m[0][2] = 0;
00970 m[1][0] = 0; m[1][1] = 1; m[1][2] = 0;
00971 m[2][0] = x; m[2][1] = y; m[2][2] = 1;
00972 */
00973 return m;
00974 }
|
|
||||||||||||||||
|
Scaling in 2D.
00980 {
00981 HxTMatrix<C> m(3,3);
00982 m[0][0] = sx; m[0][1] = 0; m[0][2] = 0;
00983 m[1][0] = 0; m[1][1] = sy; m[1][2] = 0;
00984 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1;
00985 return m;
00986 }
|
|
||||||||||
|
Rotation in 2D (alpha in rad).
00991 {
00992 HxTMatrix<C> m(3,3);
00993 m[0][0] = ::cos(alpha); m[0][1] = -::sin(alpha); m[0][2] = 0;
00994 m[1][0] = ::sin(alpha); m[1][1] = ::cos(alpha); m[1][2] = 0;
00995 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1;
00996 /* prefix:
00997 m[0][0] = ::cos(alpha); m[0][1] = ::sin(alpha); m[0][2] = 0;
00998 m[1][0] = -::sin(alpha); m[1][1] = ::cos(alpha); m[1][2] = 0;
00999 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1;
01000 */
01001 return m;
01002 }
|
|
||||||||||
|
Rotation in 2D (alpha in deg).
01006 {
01007 return rotate2d(M_PI*alpha/180.0);
01008 }
|
|
||||||||||||||||
|
Reflection in 2D (if (doX != 0) reflect X), etc.
01012 {
01013 double rx = (doX) ? -1 : 1;
01014 double ry = (doY) ? -1 : 1;
01015 HxTMatrix<C> m(3,3);
01016 m[0][0] = rx; m[0][1] = 0; m[0][2] = 0;
01017 m[1][0] = 0; m[1][1] = ry; m[1][2] = 0;
01018 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1;
01019 return m;
01020 }
|
|
||||||||||||||||
|
Shearing in 2D.
01024 {
01025 HxTMatrix<C> m(3,3);
01026 m[0][0] = 1; m[0][1] = sx; m[0][2] = 0;
01027 m[1][0] = sy; m[1][1] = 1; m[1][2] = 0;
01028 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1;
01029 /* prefix:
01030 m[0][0] = 1; m[0][1] = sy; m[0][2] = 0;
01031 m[1][0] = sx; m[1][1] = 1; m[1][2] = 0;
01032 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1;
01033 */
01034 return m;
01035 }
|
|
||||||||||||||||||||
|
Translation in 3D.
01039 {
01040 HxTMatrix<C> m(4,4);
01041 m[0][0] = 1; m[0][1] = 0; m[0][2] = 0; m[0][3] = x;
01042 m[1][0] = 0; m[1][1] = 1; m[1][2] = 0; m[1][3] = y;
01043 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1; m[2][3] = z;
01044 m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
01045 /* prefix:
01046 m[0][0] = 1; m[0][1] = 0; m[0][2] = 0; m[0][3] = 0;
01047 m[1][0] = 0; m[1][1] = 1; m[1][2] = 0; m[1][3] = 0;
01048 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1; m[2][3] = 0;
01049 m[3][0] = x; m[3][1] = y; m[3][2] = z; m[3][3] = 1;
01050 */
01051 return m;
01052 }
|
|
||||||||||||||||||||
|
Scaling in 3D.
01056 {
01057 HxTMatrix<C> m(4,4);
01058 m[0][0] = sx; m[0][1] = 0; m[0][2] = 0; m[0][3] = 0;
01059 m[1][0] = 0; m[1][1] = sy; m[1][2] = 0; m[1][3] = 0;
01060 m[2][0] = 0; m[2][1] = 0; m[2][2] = sz; m[2][3] = 0;
01061 m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
01062 return m;
01063 }
|
|
||||||||||
|
Rotation around X-axis in 3D (alpha in rad).
01067 {
01068 HxTMatrix<C> m(4,4);
01069 m[0][0] = 1; m[0][1] = 0; m[0][2] = 0; m[0][3] = 0;
01070 m[1][0] = 0; m[1][1] = ::cos(alpha); m[1][2] = -::sin(alpha); m[1][3] = 0;
01071 m[2][0] = 0; m[2][1] = ::sin(alpha); m[2][2] = ::cos(alpha); m[2][3] = 0;
01072 m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
01073 /* prefix
01074 m[0][0] = 1; m[0][1] = 0; m[0][2] = 0; m[0][3] = 0;
01075 m[1][0] = 0; m[1][1] = ::cos(alpha); m[1][2] = ::sin(alpha); m[1][3] = 0;
01076 m[2][0] = 0; m[2][1] = -::sin(alpha); m[2][2] = ::cos(alpha); m[2][3] = 0;
01077 m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
01078 */
01079 return m;
01080 }
|
|
||||||||||
|
Rotation around X-axis in 3D (alpha in deg).
01084 {
01085 return rotateX3d(M_PI*alpha/180.0);
01086 }
|
|
||||||||||
|
Rotation around Y-axis in 3D (alpha in rad).
01090 {
01091 HxTMatrix<C> m(4,4);
01092 alpha = M_PI*alpha/180.0;
01093 m[0][0] = ::cos(alpha); m[0][1] = 0; m[0][2] = ::sin(alpha); m[0][3] = 0;
01094 m[1][0] = 0; m[1][1] = 1; m[1][2] = 0; m[1][3] = 0;
01095 m[2][0] = -::sin(alpha); m[2][1] = 0; m[2][2] = ::cos(alpha); m[2][3] = 0;
01096 m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
01097 /* prefix:
01098 m[0][0] = ::cos(alpha); m[0][1] = 0; m[0][2] = -::sin(alpha); m[0][3] = 0;
01099 m[1][0] = 0; m[1][1] = 1; m[1][2] = 0; m[1][3] = 0;
01100 m[2][0] = ::sin(alpha); m[2][1] = 0; m[2][2] = ::cos(alpha); m[2][3] = 0;
01101 m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
01102 */
01103 return m;
01104 }
|
|
||||||||||
|
Rotation around Y-axis in 3D (alpha in deg).
01108 {
01109 return rotateY3d(M_PI*alpha/180.0);
01110 }
|
|
||||||||||
|
Rotation around Z-axis in 3D (alpha in rad).
01114 {
01115 HxTMatrix<C> m(4,4);
01116 m[0][0] = ::cos(alpha); m[0][1] = -::sin(alpha); m[0][2] = 0; m[0][3] = 0;
01117 m[1][0] = ::sin(alpha); m[1][1] = ::cos(alpha); m[1][2] = 0; m[1][3] = 0;
01118 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1; m[2][3] = 0;
01119 m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
01120 /* prefix:
01121 m[0][0] = ::cos(alpha); m[0][1] = ::sin(alpha); m[0][2] = 0; m[0][3] = 0;
01122 m[1][0] = -::sin(alpha); m[1][1] = ::cos(alpha); m[1][2] = 0; m[1][3] = 0;
01123 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1; m[2][3] = 0;
01124 m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
01125 */
01126 return m;
01127 }
|
|
||||||||||
|
Rotation around Z-axis in 3D (alpha in deg).
01131 {
01132 return rotateZ3d(M_PI*alpha/180.0);
01133 }
|
|
||||||||||||||||||||
|
Reflection in 3D (if (doX != 0) reflect X), etc.
01137 {
01138 double rx = (doX) ? -1 : 1;
01139 double ry = (doY) ? -1 : 1;
01140 double rz = (doZ) ? -1 : 1;
01141 HxTMatrix<C> m(4,4);
01142 m[0][0] = rx; m[0][1] = 0; m[0][2] = 0; m[0][3] = 0;
01143 m[1][0] = 0; m[1][1] = ry; m[1][2] = 0; m[1][3] = 0;
01144 m[2][0] = 0; m[2][1] = 0; m[2][2] = rz; m[2][3] = 0;
01145 m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
01146 return m;
01147 }
|
|
||||||||||
|
Projection matrix.
01151 {
01152 HxTMatrix<C> m(4,4);
01153 m[0][0] = 1; m[0][1] = 0; m[0][2] = 0; m[0][3] = 0;
01154 m[1][0] = 0; m[1][1] = 1; m[1][2] = 0; m[1][3] = 0;
01155 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1; m[2][3] = 1/f;
01156 m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
01157 /* prefix:
01158 m[0][0] = 1; m[0][1] = 0; m[0][2] = 0; m[0][3] = 0;
01159 m[1][0] = 0; m[1][1] = 1; m[1][2] = 0; m[1][3] = 0;
01160 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1; m[2][3] = 0;
01161 m[3][0] = 0; m[3][1] = 0; m[3][2] = 1/f; m[3][3] = 1;
01162 */
01163 return m;
01164 }
|
|
||||||||||
|
Camera transformation.
01168 {
01169 HxTMatrix<C> m(3,4);
01170 m[0][0] = 1; m[0][1] = 0; m[0][2] = 0; m[0][3] = 0;
01171 m[1][0] = 0; m[1][1] = 1; m[1][2] = 0; m[1][3] = 0;
01172 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1/f; m[2][3] = 1;
01173 /* prefix:
01174 HxTMatrix<C> m(4,3);
01175 m[0][0] = 1; m[0][1] = 0; m[0][2] = 0;
01176 m[1][0] = 0; m[1][1] = 1; m[1][2] = 0;
01177 m[2][0] = 0; m[2][1] = 0; m[2][2] = 1/f;
01178 m[3][0] = 0; m[3][1] = 0; m[3][2] = 1;
01179 */
01180 return m;
01181 }
|
|
|||||||||
|
Lift 2D plane to 3D XY-plane.
01185 {
01186 HxTMatrix<C> m(4,3);
01187 m[0][0] = 1; m[0][1] = 0; m[0][2] = 0;
01188 m[1][0] = 0; m[1][1] = 1; m[1][2] = 0;
01189 m[2][0] = 0; m[2][1] = 0; m[2][2] = 0;
01190 m[3][0] = 0; m[3][1] = 0; m[3][2] = 1;
01191 /* prefix:
01192 HxTMatrix<C> m(3,4);
01193 m[0][0] = 1; m[0][1] = 0; m[0][2] = 0; m[0][3] = 0;
01194 m[1][0] = 0; m[1][1] = 1; m[1][2] = 0; m[1][3] = 0;
01195 m[2][0] = 0; m[2][1] = 0; m[2][2] = 0; m[2][3] = 1;
01196 */
01197 return m;
01198 }
|
|
|||||||||
|
Number of rows.
00582 {
00583 return _nr;
00584 }
|
|
|||||||||
|
Number of columns.
00588 {
00589 return _nc;
00590 }
|
|
|||||||||
|
Number of elements.
00594 {
00595 return _nr * _nc;
00596 }
|
|
|||||||||
|
Indicates whether the matrix is valid.
00600 {
00601 return ((_nc != 0) && (_nr != 0));
00602 }
|
|
||||||||||
|
returns row r.
|
|
||||||||||
|
returns colomn c.
|
|
||||||||||||||||||||
|
replaces row r with v; if(makeCopy) it does not change the original.
00642 {
00643
00644 if(r>= _nr || r<0){
00645 error("setRow: rownr exceeded the number of rows in HxTMatrix<C>.");
00646 return HxTMatrix<C>(0,0) ;
00647 }
00648 if(_nc != v.nElem()){
00649 error("setRow: v.nElem not equal to matrix dimension");
00650 return HxTMatrix<C>(0,0) ;
00651 }
00652
00653 HxTMatrix<C> tmp(0,0);
00654
00655 if(makeCopy) {
00656
00657 tmp = HxTMatrix<C>(_nr,_nc);
00658
00659 for(int row = 0; row<_nr; row++)
00660 for(int col = 0; col<_nc; col++)
00661 if(row!=r) tmp[row][col] = (*this)[row][col] ;
00662 else tmp[row][col] = v[col] ;
00663 }
00664 else { // only change the original, faster
00665
00666 for(int col = 0; col<_nc; col++)
00667 (*this)[r][col] = v[col] ;
00668 }
00669
00670
00671 return tmp;
00672
00673 }
|
|
||||||||||||||||||||
|
replaces colomn c with v; if(makeCopy) it does not change the original.
00677 {
00678
00679 if(c>= _nc || c<0){
00680 error("setCol: Colnr exceeded number of columns in HxTMatrix<C>.");
00681 return HxTMatrix<C>(0,0) ;
00682 }
00683 if(_nr != v.nElem()){
00684 error("setCol: v.nElem not equal to matrix dimension");
00685 return HxTMatrix<C>(0,0) ;
00686 }
00687
00688 HxTMatrix<C> tmp(0,0);
00689
00690 if(makeCopy) {
00691
00692 tmp = HxTMatrix<C>(_nr,_nc);
00693
00694 for(int row = 0; row<_nr; row++)
00695 for(int col = 0; col<_nc; col++)
00696 if(col!=c) tmp[row][col] = (*this)[row][col] ;
00697 else tmp[row][col] = v[row] ;
00698 }
00699 else { // only change the original, faster
00700
00701 for(int row = 0; row<_nr; row++)
00702 (*this)[row][c] = v[row] ;
00703 }
00704
00705 return tmp;
00706
00707 }
|
|
||||||||||
|
Assign constant value.
|
|
||||||||||
|
Normal assigment.
|
|
||||||||||
|
Subscripting, start with 0.
00786 {
00787 return &_data[i*_nc];
00788 }
|
|
|||||||||
|
Unary minus.
|
|
||||||||||
|
Multiplication.
|
|
||||||||||
|
Multiplication.
01258 {
01259 if (nCol() != a.nRow()) {
01260 error("nonconformant HxTMatrix<C> * HxTMatrix<C> operands.");
01261 return HxTMatrix<C>(0,0);
01262 }
01263 HxTMatrix<C> m((*this).nRow(), a.nCol(), 0.0);
01264 double sum;
01265 int i, j, k;
01266 for (i=0 ; i<nRow() ; i++) {
01267 for (j=0 ; j<a.nCol() ; j++) {
01268 sum = 0;
01269 for (k=0 ; k<nCol() ; k++)
01270 sum += (*this)[i][k] * a[k][j];
01271 m[i][j] = sum;
01272 }
01273 }
01274 return m;
01275 }
|
|
||||||||||
|
Division.
|
|
||||||||||
|
Addition.
01224 {
01225 if ((a.nCol() != nCol()) || (a.nRow() != nRow()) ) {
01226 error("nonconformant HxTMatrix<C> + HxTMatrix<C> operands.");
01227 return HxTMatrix<C>(0,0);
01228 }
01229 HxTMatrix<C> m(nRow(), nCol());
01230 int i, j;
01231 for (i=0 ; i<nRow() ; i++) {
01232 for (j=0 ; j<nCol() ; j++)
01233 m[i][j] = a[i][j] + (*this)[i][j];
01234 }
01235 return m;
01236 }
|
|
||||||||||
|
Addition.
|
|
||||||||||
|
Subtraction.
01241 {
01242 if ((a.nCol() != nCol()) || (a.nRow() != nRow()) ) {
01243 error("nonconformant HxTMatrix<C> + HxTMatrix<C> operands.");
01244 return HxTMatrix<C>(0,0);
01245 }
01246 HxTMatrix<C> m(nRow(), nCol());
01247 int i, j;
01248 for (i=0 ; i<nRow() ; i++) {
01249 for (j=0 ; j<nCol() ; j++)
01250 m[i][j] = (*this)[i][j] - a[i][j] ;
01251 }
01252 return m;
01253 }
|
|
||||||||||
|
Subtraction.
|
|
||||||||||
|
Equal.
|
|
||||||||||
|
Not equal.
00885 {
00886 return !(a == *this);
00887 }
|
|
||||||||||
|
Multiplication. HxTMatrix must have matching dimensions.
01371 {
01372 if ((nRow() != 3) || (nCol() != 3)) {
01373 error("nonconformant HxTMatrix<C> * HxVec3Double operands.");
01374 return HxVec3Double();
01375 }
01376 double v[3];
01377 for (int i=0 ; i<nRow() ; i++) {
01378 v[i] = (*this)[i][0]*b.x() + (*this)[i][1]*b.y() + (*this)[i][2]*b.z();
01379 }
01380 return HxVec3Double(v[0], v[1], v[2]);
01381 }
|
|
||||||||||
|
deletes row r from the matrix.
00710 {
00711
00712 if(r>= _nr || r <0){
00713 error("delRow: rownr exceeded the number of rows in HxTMatrix<C>.");
00714 return HxTMatrix<C>(0,0) ;
00715 }
00716
00717 HxTMatrix<C> tmp(_nr-1,_nc);
00718
00719 for(int row = 0; row<_nr; row++)
00720 for(int col = 0; col<_nc; col++)
00721 if(row < r) tmp[row][col] = (*this)[row][col] ;
00722 else if(row > r) tmp[row-1][col] = (*this)[row][col] ;
00723
00724 return tmp ;
00725
00726 }
|
|
||||||||||
|
deletes colomn c from the matrix.
00731 {
00732
00733 if(c>= _nc || c<0){
00734 error("delCol: colnr exceeded the number of cols in HxTMatrix<C>.");
00735 return HxTMatrix<C>(0,0) ;
00736 }
00737
00738 HxTMatrix<C> tmp(_nr,_nc-1);
00739
00740 for(int row = 0; row<_nr; row++)
00741 for(int col = 0; col<_nc; col++)
00742 if(col < c) tmp[row][col] = (*this)[row][col] ;
00743 else if(col > c) tmp[row][col-1] = (*this)[row][col] ;
00744
00745 return tmp ;
00746
00747 }
|
|
||||||||||||||||
|
write matrix to [binary|text] file.
01391 {
01392 FILE *fp;
01393
01394 if(bin) { // write to binary file
01395
01396 fp = fopen(fileName.c_str(), "wb") ;
01397
01398 //write the header of the matrix
01399 fwrite(&_nr, sizeof(int), 1, fp) ;
01400 fwrite(&_nc, sizeof(int), 1, fp) ;
01401
01402 // write data
01403 fwrite(_data,sizeof(C),_nr*_nc,fp) ;
01404
01405 }
01406 else { // write to text file
01407
01408 fp = fopen(fileName.c_str(), "w") ;
01409
01410 //write the header of the matrix
01411 fprintf(fp,"%d \t %d \n", _nr, _nc) ;
01412 for(int i=0; i<_nr;i++) {
01413 for(int j=0; j<_nc;j++)
01414 fprintf(fp," %f",(double)(*this)[i][j] );
01415 fprintf(fp,"\n");
01416 }
01417
01418 }
01419
01420 fclose(fp) ;
01421 }
|
|
|||||||||
|
Inverse.
01425 {
01426 if (nRow() != nCol()) {
01427 error("Inverse: matrix is not square!.");
01428 return HxTMatrix<C>(0, 0);
01429 }
01430
01431 int size = nRow();
01432 HxTMatrix<C> m(size,size), tmp(*this);
01433 short* idx = new short [size];
01434 double d;
01435
01436 if (!ludcmp(tmp._data, size, idx, &d)) {
01437 error( "Inverse: singular matrix can't be inverted." );
01438 delete [] idx;
01439 return HxTMatrix<C>(0,0);
01440 }
01441
01442 double * res = new double [size];
01443
01444 for (int j = 0; j < size; j++) {
01445 for (int i = 0; i < size; i++)
01446 res[i] = 0.0;
01447 res[j] = 1.0;
01448 lubksb(tmp._data, size, idx, res);
01449 for (i = 0; i < size; i++)
01450 m[i][j] = res[i];
01451 }
01452
01453 delete [] res;
01454 delete [] idx;
01455
01456 return m;
01457 }
|
|
|||||||||
|
Transpose.
|
|
||||||||||
|
Calculate covariance matrix.
02211 {
02212 // printf("Calculating the covariance matrix...");
02213 int i,j,k;
02214
02215 HxTMatrix<C> COVxy(_nc,_nc, 0.0) ;
02216
02217 mean = HxVector(_nc) ; //means
02218 for (i=0; i<_nc; i++) mean[i] = 0;
02219
02220 for (k=0; k<_nr; k++)
02221 for (i=0; i<_nc; i++) {
02222 mean[i] += (*this)[k][i];
02223 for (j=i; j<_nc; j++)
02224 COVxy[i][j] += ((*this)[k][i] * (*this)[k][j]) ;
02225 }
02226
02227 for (i=0; i<_nc; i++) mean[i] /= _nr;
02228
02229 for (i=0; i<_nc; i++)
02230 for (j=i; j<_nc; j++) {
02231 COVxy[i][j] = COVxy[i][j]/_nr - mean[i]*mean[j];
02232 COVxy[j][i] = COVxy[i][j];
02233 }
02234
02235 // printf("...done\n");
02236 return COVxy;
02237 }
|
|
||||||||||||||||
|
Singular Value Decomposition. m = m.svd(W,V) * W.diag() * V.t()
01463 {
01464 int row = nRow();
01465 int col = nCol();
01466
01467 if (col > row) {
01468 error( "Svd: HxTMatrix must be augmented with extra rows of zeros." );
01469 W = HxVector(0);
01470 V = HxTMatrix<C>(0,0);
01471 return HxTMatrix<C>(0,0);
01472 }
01473
01474 HxTMatrix<C> U(*this);
01475 W = HxVector(col);
01476 V = HxTMatrix<C>(col,col);
01477
01478 svdcmp(U._data, W, V._data, col, row);
01479
01480 /* sort on eigenvalue */
01481 for (int i = 0; i < col; i++) {
01482 int idx = i;
01483 double val = W[idx];
01484
01485 for (int j = i+1; j < col; j++)
01486 if( W[j] > val ) {
01487 idx = j;
01488 val = W[idx];
01489 }
01490
01491 if (idx != i) {
01492 val = W[idx]; W[idx] = W[i]; W[i] = val;
01493 for (int j = 0; j < col; j++) {
01494 val = V[j][idx];
01495 V[j][idx] = V[j][i];
01496 V[j][i] = val;
01497 }
01498 for (j = 0; j < row; j++) {
01499 val = U[j][idx];
01500 U[j][idx] = U[j][i];
01501 U[j][i] = val;
01502 }
01503 }
01504 }
01505
01506 return U;
01507 }
|
|
||||||||||
|
Karhunen Loeve mapping (PCA) Principal Componant Analysis to reduce matrix to newDim dimensions.
02432 {
02433 if (newDim<1 || newDim>_nc)
02434 {
02435 printf("klm: new dimensionality is invalid, dimsize is set as 1...\n");
02436 newDim = 1;
02437 }
02438
02439 HxTMatrix<C> newfvec(_nr,newDim);
02440
02441 HxTMatrix<C> a = this->covariance() ;
02442
02443 HxVector d(_nc) ;
02444
02445 HxVector e(_nc) ;
02446
02447 tred2(a,d,e);
02448
02449 tqli(d,e,a);
02450
02451 // d[k] is an eigenvalue and a[][k] is a corresponding eigenvector
02452
02453 for (int k=0; k<_nr; k++)
02454 for (int i=0; i<newDim; i++) {
02455 double vl = 0.0;
02456 for (int j=0; j<_nc; j++) vl += ((*this)[k][j]*a[j][i]);
02457 newfvec[k][i] = vl;
02458 }
02459
02460 return newfvec;
02461 }
|
|
||||||||||||||||||||||||
|
K-means clustering. matrix is clustered, where each row is a vector. Input: if(cIsInitialized) c contains the starting points for kmeans else c is assigned some datavectors. c must contain a number of rows indicating the nr. of desired clusters. numloop: max number of loops. OUTPUT: imap: index mapping of the vectors in the matrix to their clusternumbers the matrix c contains the cluster vectors, empty clusters are removed, so c could contain less nrRows as started with
02067 {
02068 int i,j;
02069
02070 int nrVectors = _nr ;
02071 int nrDimensions = _nc ;
02072 int nrClusters = c.nRow() ;
02073 //printf("K-means, nrClusters= %d, nr=%d, nc=%d\n", nrClusters, _nr, _nc );
02074
02075 imap = HxVector(nrVectors) ;
02076 if(!cIsInitialized) {
02077 // the cluster matrix is not initialized, initialize it with some vectors.
02078 c = HxTMatrix<C>(nrClusters,nrDimensions) ;
02079 int steps = nrVectors / nrClusters ;
02080 for(i=0;i<nrClusters;i++) {
02081 for(j=0;j<nrDimensions;j++) {
02082 c[i][j] = (*this)[i*steps][j] ;
02083 }
02084
02085 // printf("cluster %d in data %d \n", i, i*steps);
02086
02087 /* start of code to check if this vector did not occur already
02088 int k=0;
02089 bool occurs = false ;
02090 int index = i * _nc ;
02091 while(k<i && !occurs) {
02092
02093 double d = vec_distance(&c._data[k*_nc], &c._data[index], _nc, 0.01);
02094 if(d < 0.01) occurs = true ;
02095 else k++ ;
02096 }
02097
02098 if(occurs) printf("dist between %d and %d Small !\n",i,k) ;
02099 //*/
02100 }
02101 }
02102
02103 // check c matrix dimensions
02104 if(c.nCol() != nrDimensions) {
02105 error( "error kmeans: c matrix does not have similar dimensionality." );
02106 exit(1);
02107 }
02108
02109 std::list<int> *mylist;
02110 std::list<int>::const_iterator listIter;
02111 bool contin = true;
02112 double err = 0, errOld=0;
02113
02114 int emptyclust =0 ;
02115 while(contin)
02116 {
02117 emptyclust =0 ;
02118 err=0 ;
02119 contin = false;
02120 // create a list for each cluster vector, and keep the data vectors who
02121 // belong to a cluster in their corresponding list.
02122 mylist = new std::list<int>[nrClusters];
02123 for(i=0;i<nrVectors;i++)
02124 {
02125 int jm=0;
02126 double d = 0;
02127 int indexI = i*_nc ;
02128 double dm = vec_distance(&_data[indexI],c._data, _nc);
02129 for(j=1;j<nrClusters;j++) {
02130 d = vec_distance(&_data[indexI], &c._data[j*_nc], _nc, dm);
02131 if( d<dm ) { dm = d;jm=j; }
02132 }
02133 // std::cout << i << " in " << jm << std::endl ;
02134 mylist[jm].push_back(i);
02135 err += dm;
02136 }
02137
02138 numloop--;
02139 // printf("recalculate\n");
02140 // recalculate the position of the cluster vectors,
02141 // based on the datavectors who belong to the cluster
02142 for(i=0;i<nrClusters;i++) {
02143 if( (mylist[i].size()) > 0 )
02144 {
02145 for(j=0;j<nrDimensions;j++) c[i][j] = 0;
02146
02147 std::list<int>::const_iterator myListEnd = mylist[i].end() ;
02148 for(listIter = mylist[i].begin(); listIter!=myListEnd;listIter++)
02149 for(j=0;j<nrDimensions;j++)
02150 c[i][j] += (*this)[*listIter][j];
02151
02152 for(j=0;j<nrDimensions;j++) c[i][j] /= mylist[i].size();
02153 }
02154 else {
02155 emptyclust++ ;
02156 printf("empty cluster: %d\n",i);
02157 }
02158 }
02159
02160 //printf("loop %d dist=%f, distOld=%f, emptyClusters: %d\n",numloop, err, errOld, emptyclust);
02161 // printf("%d, ",numloop);
02162 // quit if the numLoops is reached or if the error stays constant
02163 if(err != errOld && numloop>0) contin = true;
02164
02165 errOld = err;
02166
02167 } // end while(contin)
02168
02169 printf("\n");
02170 // for each data vector, store the cluster it belongs to in vector imap
02171 int filledClusters = 0 ;
02172 for(i=0;i<nrClusters;i++)
02173 if(!mylist[i].empty()) {
02174 for(listIter= mylist[i].begin();listIter!=mylist[i].end();listIter++)
02175 imap[*listIter] = filledClusters;
02176 filledClusters++ ;
02177 }
02178
02179 // if there are empty clusters, remove them.
02180 if(emptyclust > 0 ) {
02181 printf("removing empty clusters: new clustersize = %d = %d",nrClusters-emptyclust,filledClusters);
02182 int counter=0 ;
02183 HxTMatrix<C> tmpC(nrClusters-emptyclust, nrDimensions) ;
02184
02185 // copy filled clusters to a temp. matrix
02186 for(i=0;i<nrClusters;i++)
02187 if(!mylist[i].empty()) {
02188 for(j=0;j<nrDimensions;j++) tmpC[counter][j] = c[i][j];
02189 counter++ ;
02190 }
02191 c = tmpC ;
02192 }
02193
02194 delete[] mylist;
02195
02196 }
|
|
||||||||||
|
Addition. Equivalent to : a+b
01534 {
01535 return *this+b;
01536 }
|
|
||||||||||
|
Addition. Equivalent to : a+val
01540 {
01541 return *this+val;
01542 }
|
|
||||||||||
|
Subtraction. Equivalent to : a-b
01546 {
01547 return *this-b;
01548 }
|
|
||||||||||
|
Subtraction. Equivalent to : a-val
01552 {
01553 return *this-val;
01554 }
|
|
||||||||||
|
Multiplication. Equivalent to : a*b
01558 {
01559 return *this*b;
01560 }
|
|
||||||||||
|
Multiplication. Equivalent to : a*v
01571 {
01572 return *this*v;
01573 }
|
|
||||||||||
|
Multiplication. Equivalent to : a*val
01564 {
01565 return *this*val;
01566 }
|
|
||||||||||
|
Division. Equivalent to : a/val
01578 {
01579 return *this/val;
01580 }
|
|
|||||||||
|
Apply sin to each element.
01584 {
01585 return conv<C>(conv<double>(*this).map(::sin));
01586 }
|
|
|||||||||
|
Apply cos to each element.
01590 {
01591 return conv<C>(conv<double>(*this).map(::cos));
01592 //return map(::cos);
01593 }
|
|
|||||||||
|
Apply tan to each element.
01597 {
01598 return conv<C>(conv<double>(*this).map(::tan));
01599 // return map(::tan);
01600 }
|
|
|||||||||
|
Apply sinh to each element.
01604 {
01605 return conv<C>(conv<double>(*this).map(::sinh));
01606 // return map(::sinh);
01607 }
|
|
|||||||||
|
Apply cosh to each element.
01611 {
01612 return conv<C>(conv<double>(*this).map(::cosh));
01613 // return map(::cosh);
01614 }
|
|
|||||||||
|
Apply tanh to each element.
01618 {
01619 return conv<C>(conv<double>(*this).map(::tanh));
01620 // return map(::tanh);
01621 }
|
|
|||||||||
|
Apply exp to each element.
01625 {
01626 return conv<C>(conv<double>(*this).map(::exp));
01627 // return map(::exp);
01628 }
|
|
|||||||||
|
Apply log to each element.
01632 {
01633 return conv<C>(conv<double>(*this).map(::log));
01634 // return map(::log);
01635 }
|
|
|||||||||
|
Apply sqrt to each element.
01639 {
01640 return conv<C>(conv<double>(*this).map(::sqrt));
01641 // return map(::sqrt);
01642 }
|
|
|||||||||
|
Apply abs to each element.
01646 {
01647 return conv<C>(conv<double>(*this).map(::fabs));
01648 // return map(::fabs);
01649 }
|
|
|||||||||
|
Apply sgn to each element.
|
|
||||||||||
|
Map f to each element of this.
|
|
||||||||||
|
append V at the bottom of this.
01512 {
01513 int newRows = b.nRow() + nRow() ;
01514 int newCols = b.nCol()>nCol() ? b.nCol() : nCol() ;
01515 int i,j ;
01516
01517 HxTMatrix<C> m( newRows, newCols, (C) 0.0) ;
01518
01519 for (i=0; i<nRow(); i++)
01520 for (j=0; j<nCol(); j++)
01521 m[i][j] = (*this)[i][j] ;
01522
01523 for (i=nRow(); i<newRows; i++)
01524 for (j=0; j<b.nCol(); j++)
01525 m[i][j] = b[i-nRow()][j] ;
01526
01527 return m;
01528 }
|
|
||||||||||||||||
|
Multiplication.
01300 {
01301 if (a.nElem() != b.nRow()) {
01302 error("nonconformant HxVector * HxTMatrix<C> operands.");
01303 return HxVector(0);
01304 }
01305 HxVector v(b.nCol());
01306 double sum;
01307 int i, j;
01308 for (i=0 ; i<b.nCol() ; i++) {
01309 sum = 0;
01310 for (j=0 ; j<b.nRow() ; j++)
01311 sum += a[j] * b[j][i];
01312 v[i] = sum;
01313 }
01314 return v;
01315 }
|
|
||||||||||||||||
|
Multiplication.
01320 {
01321 if (b.nElem() != a.nCol()) {
01322 error("nonconformant HxTMatrix<C> * HxVector operands.");
01323 return HxVector(0);
01324 }
01325 HxVector v(a.nRow());
01326 double sum;
01327 int i, j;
01328 for (i=0 ; i<a.nRow() ; i++) {
01329 sum = 0;
01330 for (j=0 ; j<a.nCol() ; j++)
01331 sum += a[i][j] * b[j];
01332 v[i] = sum;
01333 }
01334 return v;
01335 }
|
|
||||||||||||||||
|
Division.
|
1.2.12 written by Dimitri van Heesch,
© 1997-2001