Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Mat.h
00001 /* 00002 File: Mat.h 00003 00004 Function: Defines a generic resizeable matrix. 00005 00006 Author(s): Andrew Willmott 00007 00008 Copyright: (c) 1995-2001, Andrew Willmott 00009 */ 00010 00011 #ifndef __Mat__ 00012 #define __Mat__ 00013 00014 #include "Vec.h" 00015 #include "Mat2.h" 00016 #include "Mat3.h" 00017 #include "Mat4.h" 00018 00019 class Mat2; 00020 class Mat3; 00021 class Mat4; 00022 00023 00024 // --- Mat Class -------------------------------------------------------------- 00025 00026 class Mat 00027 { 00028 public: 00029 00030 // Constructors 00031 00032 Mat(); // Null matrix 00033 Mat(Int rows, Int cols); // Uninitialised matrix 00034 Mat(Int rows, Int cols, Double elt0 ...); // Mat(2, 2, 1.0, 2.0, 3.0, 4.0) 00035 Mat(Int nrows, Int ncols, Real *ndata); // Create reference matrix 00036 Mat(const Mat &m); // Copy constructor 00037 Mat(const Mat2 &m); // reference to a Mat2 00038 Mat(const Mat3 &m); // reference to a Mat3 00039 Mat(const Mat4 &m); // reference to a Mat4 00040 Mat(Int rows, Int cols, ZeroOrOne k); // I * k 00041 Mat(Int rows, Int cols, Block k); // block matrix (m[i][j] = k) 00042 00043 ~Mat(); 00044 00045 // Accessor methods 00046 00047 Int Rows() const { return(rows & VL_REF_MASK); }; 00048 Int Cols() const { return(cols); }; 00049 00050 Vec operator [] (Int i); // Indexing by row 00051 Vec operator [] (Int i) const; // Indexing by row 00052 00053 Real &Elt(Int i, Int j); // Indexing by elt 00054 Real Elt(Int i, Int j) const; 00055 00056 Void SetSize(Int nrows, Int ncols); 00057 Void SetSize(const Mat &m); 00058 Bool IsSquare() const 00059 { return((rows & VL_REF_MASK) == cols); }; 00060 00061 Real *Ref() const; // Return pointer to data 00062 00063 // Assignment operators 00064 00065 Mat &operator = (const Mat &m); // Assignment of a matrix 00066 Mat &operator = (ZeroOrOne k); // Set to k * I... 00067 Mat &operator = (Block k); // Set to a block matrix... 00068 Mat &operator = (const Mat2 &m); 00069 Mat &operator = (const Mat3 &m); 00070 Mat &operator = (const Mat4 &m); 00071 00072 // In-Place Operators 00073 00074 Mat &operator += (const Mat &m); 00075 Mat &operator -= (const Mat &m); 00076 Mat &operator *= (const Mat &m); 00077 Mat &operator *= (Real s); 00078 Mat &operator /= (Real s); 00079 00080 // Matrix initialisers 00081 00082 Void MakeZero(); 00083 Void MakeDiag(Real k); 00084 Void MakeDiag(); 00085 Void MakeBlock(Real k); 00086 Void MakeBlock(); 00087 00088 Mat &Clamp(Real fuzz); 00089 Mat &Clamp(); 00090 00091 // Private... 00092 00093 protected: 00094 00095 Real *data; 00096 UInt rows; 00097 UInt cols; 00098 00099 Bool IsRef() { return((rows & VL_REF_FLAG) != 0); }; 00100 }; 00101 00102 00103 // --- Mat Comparison Operators ----------------------------------------------- 00104 00105 Bool operator == (const Mat &m, const Mat &n); 00106 Bool operator != (const Mat &m, const Mat &n); 00107 00108 00109 // --- Mat Arithmetic Operators ----------------------------------------------- 00110 00111 Mat operator + (const Mat &m, const Mat &n); 00112 Mat operator - (const Mat &m, const Mat &n); 00113 Mat operator - (const Mat &m); 00114 Mat operator * (const Mat &m, const Mat &n); 00115 Mat operator * (const Mat &m, Real s); 00116 inline Mat operator * (Real s, const Mat &m); 00117 Mat operator / (const Mat &m, Real s); 00118 00119 Vec operator * (const Mat &m, const Vec &v); 00120 Vec operator * (const Vec &v, const Mat &m); 00121 00122 Mat trans(const Mat &m); // Transpose 00123 Real trace(const Mat &m); // Trace 00124 Mat inv(const Mat &m, Real *determinant = 0, Real pEps = 1e-20); 00125 // Inverse 00126 Mat oprod(const Vec &a, const Vec &b); // Outer product 00127 00128 Mat clamped(const Mat &m, Real fuzz); 00129 Mat clamped(const Mat &m); 00130 00131 00132 // --- Mat Input & Output ----------------------------------------------------- 00133 00134 //std::ostream &operator << (std::ostream &s, const Mat &m); 00135 //std::istream &operator >> (std::istream &s, Mat &m); 00136 00137 void printMat(const Mat &m); 00138 00139 00140 // --- Mat Inlines ------------------------------------------------------------ 00141 00142 inline Mat::Mat() : data(0), rows(0), cols(0) 00143 { 00144 } 00145 00146 inline Mat::Mat(Int rows, Int cols) : rows(rows), cols(cols) 00147 { 00148 Assert(rows > 0 && cols > 0, "(Mat) illegal matrix size"); 00149 00150 data = new Real[rows * cols]; 00151 } 00152 00153 inline Mat::Mat(Int nrows, Int ncols, Real *ndata) : 00154 data(ndata), rows(nrows | VL_REF_FLAG), cols(ncols) 00155 { 00156 } 00157 00158 inline Vec Mat::operator [] (Int i) 00159 { 00160 CheckRange(i, 0, Rows(), "(Mat::[i]) i index out of range"); 00161 00162 return(Vec(cols, data + i * cols)); 00163 } 00164 00165 inline Vec Mat::operator [] (Int i) const 00166 { 00167 CheckRange(i, 0, Rows(), "(Mat::[i]) i index out of range"); 00168 00169 return(Vec(cols, data + i * cols)); 00170 } 00171 00172 inline Real &Mat::Elt(Int i, Int j) 00173 { 00174 CheckRange(i, 0, Rows(), "(Mat::e(i,j)) i index out of range"); 00175 CheckRange(j, 0, Cols(), "(Mat::e(i,j)) j index out of range"); 00176 00177 return(data[i * cols + j]); 00178 } 00179 00180 inline Real Mat::Elt(Int i, Int j) const 00181 { 00182 CheckRange(i, 0, Rows(), "(Mat::e(i,j)) i index out of range"); 00183 CheckRange(j, 0, Cols(), "(Mat::e(i,j)) j index out of range"); 00184 00185 return(data[i * cols + j]); 00186 } 00187 00188 inline Real *Mat::Ref() const 00189 { 00190 return(data); 00191 } 00192 00193 inline Mat operator * (Real s, const Mat &m) 00194 { 00195 return(m * s); 00196 } 00197 00198 inline Mat &Mat::operator = (ZeroOrOne k) 00199 { 00200 MakeDiag(k); 00201 00202 return(SELF); 00203 } 00204 00205 inline Mat &Mat::operator = (Block k) 00206 { 00207 MakeBlock((ZeroOrOne) k); 00208 00209 return(SELF); 00210 } 00211 00212 inline Mat::~Mat() 00213 { 00214 if (!IsRef()) 00215 delete[] data; 00216 } 00217 00218 #endif
Generated on Wed Jul 13 2022 19:31:42 by
1.7.2