| 1 | #ifndef _MATRIX_H | 
|---|
| 2 | #define _MATRIX_H | 
|---|
| 3 |  | 
|---|
| 4 | #include <math.h> | 
|---|
| 5 | #include <stdio.h> | 
|---|
| 6 | #include <stdlib.h> | 
|---|
| 7 | #include <iostream> | 
|---|
| 8 |  | 
|---|
| 9 | using namespace std; | 
|---|
| 10 |  | 
|---|
| 11 | class Matrix | 
|---|
| 12 | { | 
|---|
| 13 | public: | 
|---|
| 14 |   // Constructors | 
|---|
| 15 |   Matrix (const Matrix& m); | 
|---|
| 16 |   Matrix (size_t row, size_t col); | 
|---|
| 17 |    | 
|---|
| 18 |   // Destructor | 
|---|
| 19 |   ~Matrix (); | 
|---|
| 20 |    | 
|---|
| 21 |   // Assignment operators | 
|---|
| 22 |   Matrix& operator = (const Matrix& m) ; | 
|---|
| 23 |    | 
|---|
| 24 |   // Value extraction method | 
|---|
| 25 |   size_t RowNo () const { return _m->Row; } | 
|---|
| 26 |   size_t ColNo () const { return _m->Col; } | 
|---|
| 27 |    | 
|---|
| 28 |   // Subscript operator | 
|---|
| 29 |   float& operator () (size_t row, size_t col); | 
|---|
| 30 |    float  operator () (size_t row, size_t col) const; | 
|---|
| 31 |  | 
|---|
| 32 |    // Unary operators | 
|---|
| 33 |    Matrix operator + ()  { return *this; } | 
|---|
| 34 |    Matrix operator - () ; | 
|---|
| 35 |  | 
|---|
| 36 |    // Combined assignment - calculation operators | 
|---|
| 37 |    Matrix& operator += (const Matrix& m); | 
|---|
| 38 |    Matrix& operator -= (const Matrix& m); | 
|---|
| 39 |    Matrix& operator *= (const Matrix& m); | 
|---|
| 40 |    Matrix& operator *= (const float& c) ; | 
|---|
| 41 |    Matrix& operator /= (const float& c) ; | 
|---|
| 42 |    Matrix& operator ^= (const size_t& pow); | 
|---|
| 43 |  | 
|---|
| 44 |    // Miscellaneous -methods | 
|---|
| 45 |    void Null (const size_t& row, const size_t& col) ; | 
|---|
| 46 |    void Null () ; | 
|---|
| 47 |    void Unit (const size_t& row) ; | 
|---|
| 48 |    void Unit () ; | 
|---|
| 49 |    void SetSize (size_t row, size_t col) ; | 
|---|
| 50 |  | 
|---|
| 51 |    // Utility methods | 
|---|
| 52 |    Matrix Solve (const Matrix& v) const; | 
|---|
| 53 |    Matrix Adj (); | 
|---|
| 54 |    Matrix Inv (); | 
|---|
| 55 |    float Det () const; | 
|---|
| 56 |    float Norm () ; | 
|---|
| 57 |    float Cofact (size_t row, size_t col); | 
|---|
| 58 |    float Cond () ; | 
|---|
| 59 |  | 
|---|
| 60 |    // Type of matrices | 
|---|
| 61 |    bool IsSquare ()  { return (_m->Row == _m->Col); }  | 
|---|
| 62 |    bool IsSingular (); | 
|---|
| 63 |    bool IsDiagonal (); | 
|---|
| 64 |    bool IsScalar (); | 
|---|
| 65 |    bool IsUnit (); | 
|---|
| 66 |    bool IsNull (); | 
|---|
| 67 |    bool IsSymmetric (); | 
|---|
| 68 |    bool IsSkewSymmetric (); | 
|---|
| 69 |    bool IsUpperTriangular (); | 
|---|
| 70 |    bool IsLowerTriangular (); | 
|---|
| 71 |  | 
|---|
| 72 | private: | 
|---|
| 73 |    struct base_mat | 
|---|
| 74 |     { | 
|---|
| 75 |         float **Val; | 
|---|
| 76 |         size_t Row, Col, RowSiz, ColSiz; | 
|---|
| 77 |         int Refcnt; | 
|---|
| 78 |  | 
|---|
| 79 |         base_mat (size_t row, size_t col, float** v) | 
|---|
| 80 |         { | 
|---|
| 81 |             Row = row; RowSiz = row; | 
|---|
| 82 |             Col = col; ColSiz = col; | 
|---|
| 83 |             Refcnt = 1; | 
|---|
| 84 |  | 
|---|
| 85 |             Val = new float* [row]; | 
|---|
| 86 |             size_t rowlen = col * sizeof(float); | 
|---|
| 87 |  | 
|---|
| 88 |             for (size_t i=0; i < row; i++) | 
|---|
| 89 |             { | 
|---|
| 90 |                 Val[i] = new float [col]; | 
|---|
| 91 |                 if (v) memcpy( Val[i], v[i], rowlen); | 
|---|
| 92 |             } | 
|---|
| 93 |         } | 
|---|
| 94 |         ~base_mat () | 
|---|
| 95 |         { | 
|---|
| 96 |             for (size_t i=0; i < RowSiz; i++) | 
|---|
| 97 |                 delete [] Val[i]; | 
|---|
| 98 |             delete [] Val; | 
|---|
| 99 |         } | 
|---|
| 100 |     }; | 
|---|
| 101 |     base_mat *_m; | 
|---|
| 102 |  | 
|---|
| 103 |     void clone (); | 
|---|
| 104 |     void realloc (size_t row, size_t col); | 
|---|
| 105 |     int pivot (size_t row); | 
|---|
| 106 | }; | 
|---|
| 107 |  | 
|---|
| 108 | Matrix operator ! (const Matrix m); | 
|---|
| 109 |  | 
|---|
| 110 | #endif /*_MATRIX_H */ | 
|---|