TooN
matrix.hh
1 // -*- c++ -*-
2 
3 // Copyright (C) 2009 Tom Drummond (twd20@cam.ac.uk),
4 // Ed Rosten (er258@cam.ac.uk)
5 
6 //All rights reserved.
7 //
8 //Redistribution and use in source and binary forms, with or without
9 //modification, are permitted provided that the following conditions
10 //are met:
11 //1. Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //2. Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 //
17 //THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
18 //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 //IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 //ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
21 //LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 //CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 //SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 //INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 //CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 //POSSIBILITY OF SUCH DAMAGE.
28 
29 namespace TooN {
30 
104 template <int Rows=Dynamic, int Cols=Rows, class Precision=DefaultPrecision, class Layout = RowMajor>
105 struct Matrix : public Layout::template MLayout<Rows, Cols, Precision>
106 {
107 public:
108 
109  using Layout::template MLayout<Rows, Cols, Precision>::my_data;
110  using Layout::template MLayout<Rows, Cols, Precision>::num_rows;
111  using Layout::template MLayout<Rows, Cols, Precision>::num_cols;
112 
113  //Use Tom's sneaky constructor hack...
114 
117 
119  Matrix(){}
120 
122  Matrix(int rows, int cols) :
123  Layout::template MLayout<Rows,Cols,Precision>(rows, cols)
124  {}
125 
127  Matrix(Precision* p) :
128  Layout::template MLayout<Rows, Cols, Precision>(p)
129  {}
130 
132  Matrix(Precision* p, int r, int c) :
133  Layout::template MLayout<Rows, Cols, Precision>(p, r, c)
134  {}
135 
138  Matrix(Precision* data, int rows, int cols, int rowstride, int colstride, Internal::Slicing)
139  :Layout::template MLayout<Rows, Cols, Precision>(data, rows, cols, rowstride, colstride){}
140 
141 
142  //See vector.hh and allocator.hh for details about why the
143  //copy constructor should be default.
145  template <class Op>
146  inline Matrix(const Operator<Op>& op)
147  :Layout::template MLayout<Rows,Cols,Precision>(op)
148  {
149  op.eval(*this);
150  }
151 
153  template<int Rows2, int Cols2, typename Precision2, typename Base2>
155  :Layout::template MLayout<Rows,Cols,Precision>(from.num_rows(), from.num_cols())
156  {
157  operator=(from);
158  }
160 
164  inline Matrix& operator= (const Matrix& from)
165  {
166  SizeMismatch<Rows, Rows>::test(num_rows(), from.num_rows());
167  SizeMismatch<Cols, Cols>::test(num_cols(), from.num_cols());
168 
169  for(int r=0; r < num_rows(); r++)
170  for(int c=0; c < num_cols(); c++)
171  (*this)[r][c] = from[r][c];
172 
173  return *this;
174  }
175 
176  // operator = 0-ary operator
177  template<class Op> inline Matrix& operator= (const Operator<Op>& op)
178  {
179  op.eval(*this);
180  return *this;
181  }
182 
183  // operator =
184  template<int Rows2, int Cols2, typename Precision2, typename Base2>
185  Matrix& operator= (const Matrix<Rows2, Cols2, Precision2, Base2>& from)
186  {
187  SizeMismatch<Rows, Rows2>::test(num_rows(), from.num_rows());
188  SizeMismatch<Cols, Cols2>::test(num_cols(), from.num_cols());
189 
190  for(int r=0; r < num_rows(); r++)
191  for(int c=0; c < num_cols(); c++)
192  (*this)[r][c] = from[r][c];
193 
194  return *this;
195  }
197 
200 
201  Matrix& operator*=(const Precision rhs)
202  {
203  for(int r=0; r < num_rows(); r++)
204  for(int c=0; c < num_cols(); c++)
205  (*this)[r][c] *= rhs;
206 
207  return *this;
208  }
209 
210  Matrix& operator/=(const Precision rhs)
211  {
212  for(int r=0; r < num_rows(); r++)
213  for(int c=0; c < num_cols(); c++)
214  (*this)[r][c] /= rhs;
215 
216  return *this;
217  }
218 
219  template<int Rows2, int Cols2, typename Precision2, typename Base2>
220  Matrix& operator+= (const Matrix<Rows2, Cols2, Precision2, Base2>& from)
221  {
222  SizeMismatch<Rows, Rows2>::test(num_rows(), from.num_rows());
223  SizeMismatch<Cols, Cols2>::test(num_cols(), from.num_cols());
224 
225  for(int r=0; r < num_rows(); r++)
226  for(int c=0; c < num_cols(); c++)
227  (*this)[r][c] += from[r][c];
228 
229  return *this;
230  }
231 
232  template<class Op>
233  Matrix& operator+=(const Operator<Op>& op)
234  {
235  op.plusequals(*this);
236  return *this;
237  }
238 
239  template<class Op>
240  Matrix& operator-=(const Operator<Op>& op)
241  {
242  op.minusequals(*this);
243  return *this;
244  }
245 
246  template<int Rows2, int Cols2, typename Precision2, typename Base2>
247  Matrix& operator-= (const Matrix<Rows2, Cols2, Precision2, Base2>& from)
248  {
249  SizeMismatch<Rows, Rows2>::test(num_rows(), from.num_rows());
250  SizeMismatch<Cols, Cols2>::test(num_cols(), from.num_cols());
251 
252  for(int r=0; r < num_rows(); r++)
253  for(int c=0; c < num_cols(); c++)
254  (*this)[r][c] -= from[r][c];
255 
256  return *this;
257  }
258 
259  template<int Rows2, int Cols2, typename Precision2, typename Base2>
260  bool operator== (const Matrix<Rows2, Cols2, Precision2, Base2>& rhs) const
261  {
262  SizeMismatch<Rows, Rows2>::test(num_rows(), rhs.num_rows());
263  SizeMismatch<Cols, Cols2>::test(num_cols(), rhs.num_cols());
264 
265  for(int r=0; r < num_rows(); r++)
266  for(int c=0; c < num_cols(); c++)
267  if((*this)[r][c] != rhs[r][c])
268  return 0;
269  return 1;
270  }
271 
272  template<int Rows2, int Cols2, typename Precision2, typename Base2>
273  bool operator!= (const Matrix<Rows2, Cols2, Precision2, Base2>& rhs) const
274  {
275  SizeMismatch<Rows, Rows2>::test(num_rows(), rhs.num_rows());
276  SizeMismatch<Cols, Cols2>::test(num_cols(), rhs.num_cols());
277 
278  for(int r=0; r < num_rows(); r++)
279  for(int c=0; c < num_cols(); c++)
280  if((*this)[r][c] != rhs[r][c])
281  return 1;
282  return 0;
283  }
284 
285  template<class Op>
286  bool operator!=(const Operator<Op>& op)
287  {
288  return op.notequal(*this);
289  }
290 
291 
293 
296 
299  {
300  return *this;
301  }
303 
304  #ifdef DOXYGEN_INCLUDE_ONLY_FOR_DOCS
305 
318  const double& operator() (int r, int c) const;
319 
326  const double& operator[](const std::pair<int,int>& row_col) const;
330  double& operator[](const std::pair<int,int>& row_col);
331 
347  double& operator() (int r, int c);
348 
367  const Vector& operator[] (int r) const;
368 
389  Vector& operator[] (int r);
390 
394  int num_rows() const;
395 
399  int num_cols() const;
400 
402 
403 
420  const Matrix<Cols, Rows>& T() const;
421 
443  Matrix<Cols, Rows>& T();
444 
461  template<Rstart, Cstart, Rsize, Csize>
462  const Matrix<Rsize, Csize>& slice() const;
463 
479  template<Rstart, Cstart, Rsize, Csize>
480  Matrix<Rsize, Csize>& slice();
481 
494  const Matrix<>& slice(int rstart, int cstart, int rsize, int csize) const;
495 
507  Matrix<>& slice(int rstart, int cstart, int rsize, int csize);
508 
510 
511 
512  #endif
513 };
514 
515 }
Matrix(int rows, int cols)
Construction of dynamic matrices. Values are not initialized.
Definition: matrix.hh:122
Matrix(Precision *data, int rows, int cols, int rowstride, int colstride, Internal::Slicing)
Advanced construction of dynamically sized slice matrices.
Definition: matrix.hh:138
Matrix & ref()
return me as a non const reference - useful for temporaries
Definition: matrix.hh:298
Pretty generic SFINAE introspection generator.
Definition: vec_test.cc:21
A vector.
Definition: vector.hh:126
A matrix.
Definition: matrix.hh:105
Matrix(const Operator< Op > &op)
Construction from an operator.
Definition: matrix.hh:146
Definition: operators.hh:119
Matrix(const Matrix< Rows2, Cols2, Precision2, Base2 > &from)
constructor from arbitrary matrix
Definition: matrix.hh:154
Matrix(Precision *p, int r, int c)
Construction of dynamically sized slice matrices.
Definition: matrix.hh:132
Matrix(Precision *p)
Construction of statically sized slice matrices.
Definition: matrix.hh:127
Matrix()
Construction of static matrices. Values are not initialized.
Definition: matrix.hh:119
Definition: size_mismatch.hh:103
Definition: TooN.h:145