Matrix.h
00001 #ifndef gridripper_math_Matrix_h
00002 #define gridripper_math_Matrix_h
00003
00004
00005 #include <iostream>
00006
00007 namespace gridripper { namespace math {
00008
00015 template <class T>
00016 class MVector
00017 {
00018 protected:
00020 T *x;
00021
00023 int n;
00024
00025 public:
00027 MVector();
00028
00030 MVector(int d);
00031
00033 MVector(const MVector<T>& v);
00034
00035 virtual ~MVector();
00036
00038 virtual void setSize(int n);
00039
00041 int size() const { return n; }
00042
00044 T& operator [](int i) const {
00045 return x[i];
00046 }
00047
00049 MVector<T>& operator =(const MVector<T>& b);
00050
00056 MVector<T>& operator =(T s);
00057
00059 bool operator ==(const MVector<T>& b) const;
00060
00062 bool operator !=(const MVector<T>& b) const {
00063 return !(*this == b);
00064 }
00065
00067 MVector<T> operator +() const {return *this;}
00068
00070 MVector<T> operator -() const;
00071
00073 MVector<T> operator +(const MVector<T>& b) const;
00074
00076 MVector<T> operator -(const MVector<T>& b) const;
00077
00079 MVector<T> operator *(T s) const;
00080
00082 MVector<T> operator /(T s) const;
00083
00085 MVector<T>& operator +=(const MVector<T>& b);
00086
00088 MVector<T>& operator -=(const MVector<T>& b);
00089
00091 template <class T2>
00092 friend MVector<T2> operator *(T2 s, const MVector<T2>& v) {
00093 return v*s;
00094 }
00095
00097 T operator *(const MVector<T>& v) const;
00098
00100 MVector<T>& operator *=(T s);
00101
00103 MVector<T>& operator /=(T s);
00104
00106 T square();
00107
00109 template <class T2>
00110 friend std::istream& operator >>(std::istream& is, MVector<T2>& v);
00111
00113 template <class T2>
00114 friend std::ostream& operator <<(std::ostream& os, const MVector<T2>& v);
00115
00116 private:
00117 void init(int n);
00118 };
00119
00126 template <class T>
00127 class Matrix
00128 {
00129 protected:
00131 T *x;
00132
00134 int numRows;
00135
00137 int numColumns;
00138
00139 public:
00141 Matrix() {
00142 x = NULL;
00143 numRows = numColumns = 0;
00144 }
00145
00147 Matrix(int rows, int columns);
00148
00150 Matrix(const Matrix<T>&);
00151
00152 virtual ~Matrix();
00153
00155 void setZero();
00156
00158 void setIdentity();
00159
00161 virtual void setSize(int rows, int columns);
00162
00164 int getNumRows() const { return numRows; }
00165
00167 int getNumColumns() const { return numColumns; }
00168
00170 T& operator ()(int i, int j) {
00171 return x[i*numColumns + j];
00172 }
00173
00175 T operator ()(int i, int j) const {
00176 return x[i*numColumns + j];
00177 }
00178
00184 T& operator [](int i) {
00185 return x[i];
00186 }
00187
00193 T operator [](int i) const {
00194 return x[i];
00195 }
00196
00198 Matrix<T>& operator =(const Matrix<T>&);
00199
00201 bool operator ==(const Matrix<T>&) const;
00202
00204 int operator !=(const Matrix<T>& B) const {return !(*this==B);}
00205
00207 Matrix<T> operator +() const {return *this;}
00208
00210 Matrix<T> operator -() const;
00211
00213 Matrix<T> operator +(const Matrix<T>&) const;
00214
00216 Matrix<T> operator -(const Matrix<T>&) const;
00217
00219 MVector<T> operator *(const MVector<T>&) const;
00220
00222 Matrix<T> operator *(T) const;
00223
00225 Matrix<T> operator /(T) const;
00226
00228 Matrix<T> operator ^(int) const;
00229
00231 Matrix<T> operator *(const Matrix<T>&) const;
00232
00234 template <class T2>
00235 friend Matrix<T2> operator *(T2 s, const Matrix<T2>& A) {return A*s;}
00236
00238 template <class T2>
00239 friend MVector<T2> operator *(const MVector<T2>&, const Matrix<T2>&);
00240
00242 Matrix<T>& operator +=(const Matrix<T>&);
00243
00245 Matrix<T>& operator -=(const Matrix<T>&);
00246
00248 Matrix<T>& operator *=(T);
00249
00251 Matrix<T>& operator /=(T);
00252
00254 Matrix<T>& operator *=(const Matrix<T>& B) {
00255 return *this = *this * B;
00256 }
00257
00259 Matrix<T>& operator ^=(int);
00260
00262 Matrix<T> operator ~() const;
00263
00268 Matrix<T> minorMatrix(int row, int column) const;
00269
00271 template <class T2>
00272 friend MVector<T2> operator /(const MVector<T2>&, const Matrix<T2>&);
00273
00275 T trace() const;
00276
00278 T det() const;
00279
00281 void invert() { invert(*this); }
00282
00284 void invert(const Matrix<T>&);
00285
00287 void swapRows(int i, int j);
00288
00290 template <class T2>
00291 friend std::istream& operator >>(std::istream&, Matrix<T2>&);
00292
00294 template <class T2>
00295 friend std::ostream& operator <<(std::ostream&, const Matrix<T2>&);
00296
00297 private:
00298 void init(int rows, int columns);
00299 };
00300
00301 #include "Matrix.tcc"
00302
00303 } }
00304
00305 #endif