00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 namespace TooN {
00032
00109 template <int Rows=-1, int Cols=Rows, class Precision=DefaultPrecision, class Layout = RowMajor>
00110 struct Matrix : public Layout::template MLayout<Rows, Cols, Precision>
00111 {
00112 public:
00113
00114 using Layout::template MLayout<Rows, Cols, Precision>::my_data;
00115 using Layout::template MLayout<Rows, Cols, Precision>::num_rows;
00116 using Layout::template MLayout<Rows, Cols, Precision>::num_cols;
00117
00118
00119
00122
00124 Matrix(){}
00125
00127 Matrix(int rows, int cols) :
00128 Layout::template MLayout<Rows,Cols,Precision>(rows, cols)
00129 {}
00130
00132 Matrix(Precision* p) :
00133 Layout::template MLayout<Rows, Cols, Precision>(p)
00134 {}
00135
00137 Matrix(Precision* p, int r, int c) :
00138 Layout::template MLayout<Rows, Cols, Precision>(p, r, c)
00139 {}
00140
00143 Matrix(Precision* data, int rows, int cols, int rowstride, int colstride, Internal::Slicing)
00144 :Layout::template MLayout<Rows, Cols, Precision>(data, rows, cols, rowstride, colstride){}
00145
00146
00147
00148
00150 template <class Op>
00151 inline Matrix(const Operator<Op>& op)
00152 :Layout::template MLayout<Rows,Cols,Precision>(op)
00153 {
00154 op.eval(*this);
00155 }
00156
00158 template<int Rows2, int Cols2, typename Precision2, typename Base2>
00159 inline Matrix(const Matrix<Rows2, Cols2,Precision2,Base2>& from)
00160 :Layout::template MLayout<Rows,Cols,Precision>(from.num_rows(), from.num_cols())
00161 {
00162 operator=(from);
00163 }
00165
00169 inline Matrix& operator= (const Matrix& from)
00170 {
00171 SizeMismatch<Rows, Rows>::test(num_rows(), from.num_rows());
00172 SizeMismatch<Cols, Cols>::test(num_cols(), from.num_cols());
00173
00174 for(int r=0; r < num_rows(); r++)
00175 for(int c=0; c < num_cols(); c++)
00176 (*this)[r][c] = from[r][c];
00177
00178 return *this;
00179 }
00180
00181
00182 template<class Op> inline Matrix& operator= (const Operator<Op>& op)
00183 {
00184 op.eval(*this);
00185 return *this;
00186 }
00187
00188
00189 template<int Rows2, int Cols2, typename Precision2, typename Base2>
00190 Matrix& operator= (const Matrix<Rows2, Cols2, Precision2, Base2>& from)
00191 {
00192 SizeMismatch<Rows, Rows2>::test(num_rows(), from.num_rows());
00193 SizeMismatch<Cols, Cols2>::test(num_cols(), from.num_cols());
00194
00195 for(int r=0; r < num_rows(); r++)
00196 for(int c=0; c < num_cols(); c++)
00197 (*this)[r][c] = from[r][c];
00198
00199 return *this;
00200 }
00202
00205
00206 Matrix& operator*=(const Precision& rhs)
00207 {
00208 for(int r=0; r < num_rows(); r++)
00209 for(int c=0; c < num_cols(); c++)
00210 (*this)[r][c] *= rhs;
00211
00212 return *this;
00213 }
00214
00215 Matrix& operator/=(const Precision& rhs)
00216 {
00217 for(int r=0; r < num_rows(); r++)
00218 for(int c=0; c < num_cols(); c++)
00219 (*this)[r][c] /= rhs;
00220
00221 return *this;
00222 }
00223
00224 template<int Rows2, int Cols2, typename Precision2, typename Base2>
00225 Matrix& operator+= (const Matrix<Rows2, Cols2, Precision2, Base2>& from)
00226 {
00227 SizeMismatch<Rows, Rows2>::test(num_rows(), from.num_rows());
00228 SizeMismatch<Cols, Cols2>::test(num_cols(), from.num_cols());
00229
00230 for(int r=0; r < num_rows(); r++)
00231 for(int c=0; c < num_cols(); c++)
00232 (*this)[r][c] += from[r][c];
00233
00234 return *this;
00235 }
00236
00237 template<class Op>
00238 Matrix& operator+=(const Operator<Op>& op)
00239 {
00240 op.plusequals(*this);
00241 return *this;
00242 }
00243
00244 template<class Op>
00245 Matrix& operator-=(const Operator<Op>& op)
00246 {
00247 op.minusequals(*this);
00248 return *this;
00249 }
00250
00251 template<int Rows2, int Cols2, typename Precision2, typename Base2>
00252 Matrix& operator-= (const Matrix<Rows2, Cols2, Precision2, Base2>& from)
00253 {
00254 SizeMismatch<Rows, Rows2>::test(num_rows(), from.num_rows());
00255 SizeMismatch<Cols, Cols2>::test(num_cols(), from.num_cols());
00256
00257 for(int r=0; r < num_rows(); r++)
00258 for(int c=0; c < num_cols(); c++)
00259 (*this)[r][c] -= from[r][c];
00260
00261 return *this;
00262 }
00263
00264 template<int Rows2, int Cols2, typename Precision2, typename Base2>
00265 bool operator== (const Matrix<Rows2, Cols2, Precision2, Base2>& rhs)
00266 {
00267 SizeMismatch<Rows, Rows2>::test(num_rows(), rhs.num_rows());
00268 SizeMismatch<Cols, Cols2>::test(num_cols(), rhs.num_cols());
00269
00270 for(int r=0; r < num_rows(); r++)
00271 for(int c=0; c < num_cols(); c++)
00272 if((*this)[r][c] != rhs[r][c])
00273 return 0;
00274 return 1;
00275 }
00276
00277 template<int Rows2, int Cols2, typename Precision2, typename Base2>
00278 bool operator!= (const Matrix<Rows2, Cols2, Precision2, Base2>& rhs)
00279 {
00280 SizeMismatch<Rows, Rows2>::test(num_rows(), rhs.num_rows());
00281 SizeMismatch<Cols, Cols2>::test(num_cols(), rhs.num_cols());
00282
00283 for(int r=0; r < num_rows(); r++)
00284 for(int c=0; c < num_cols(); c++)
00285 if((*this)[r][c] != rhs[r][c])
00286 return 1;
00287 return 0;
00288 }
00289
00291
00294
00296 Matrix& ref()
00297 {
00298 return *this;
00299 }
00301
00302 #ifdef DOXYGEN_INCLUDE_ONLY_FOR_DOCS
00303
00315 const double& operator() (int r, int c) const;
00316
00323 const double& operator[](const std::pair<int,int>& row_col) const;
00327 double& operator[](const std::pair<int,int>& row_col);
00328
00342 double& operator() (int r, int c);
00343
00360 const Vector& operator[] (int r) const;
00361
00380 Vector& operator[] (int r);
00381
00385 int num_rows() const;
00386
00390 int num_cols() const;
00391
00393
00394
00409 const Matrix<Cols, Rows>& T() const;
00410
00430 Matrix<Cols, Rows>& T();
00431
00445 template<Rstart, Cstart, Rsize, Csize>
00446 const Matrix<Rsize, Csize>& slice() const;
00447
00461 template<Rstart, Cstart, Rsize, Csize>
00462 Matrix<Rsize, Csize>& slice();
00463
00476 const Matrix<>& slice(int rstart, int cstart, int rsize, int csize) const;
00477
00489 Matrix<>& slice(int rstart, int cstart, int rsize, int csize);
00490
00492
00493
00494 #endif
00495 };
00496
00497 }