fmatvec  2.0.0
Fast Matrix/Vector Library
var_fixed_general_matrix.h
1 /* Copyright (C) 2003-2005 Martin Förg
2 
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Contact:
18  * martin.o.foerg@googlemail.com
19  *
20  */
21 
22 #ifndef var_fixed_general_matrix_h
23 #define var_fixed_general_matrix_h
24 
25 #include "types.h"
26 #include <cstdlib>
27 #include <stdexcept>
28 
29 namespace fmatvec {
30 
39  template <int N, class AT> class Matrix<fmatvec::General,fmatvec::Var,fmatvec::Fixed<N>,AT> {
40 
41  public:
42  typedef AT value_type;
43 
45 
46  protected:
47 
48  int M{0};
49 
50  AT *ele;
51 
52  template <class Type, class Row, class Col> inline Matrix<General,Var,Fixed<N>,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
53 
55 
56  public:
57 
58  explicit Matrix() : ele(nullptr) { }
59 
60  explicit Matrix(int m, Noinit) : M(m), ele(new AT[M*N]) { }
61  explicit Matrix(int m, Init ini=INIT, const AT &a=AT()) : M(m), ele(new AT[M*N]) { init(a); }
62  explicit Matrix(int m, Eye ini, const AT &a=1) : M(m), ele(new AT[M*N]) { init(ini,a); }
63  explicit Matrix(int m, int n, Noinit) : M(m), ele(new AT[M*N]) { FMATVEC_ASSERT(n==N, AT); }
64  explicit Matrix(int m, int n, Init ini=INIT, const AT &a=AT()) : M(m), ele(new AT[M*N]) { FMATVEC_ASSERT(n==N, AT); init(a); }
65  explicit Matrix(int m, int n, Eye ini, const AT &a=1) : M(m), ele(new AT[M*N]) { FMATVEC_ASSERT(n==N, AT); init(ini,a); }
66 
72  Matrix(const Matrix<General,Var,Fixed<N>,AT> &A) : M(A.M), ele(new AT[M*N]) {
73  copy(A);
74  }
75 
81  template<class Row, class Col>
82  Matrix(const Matrix<General,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*N]) {
83  FMATVEC_ASSERT(A.cols() == N, AT);
84  copy(A);
85  }
86 
92  template<class Type, class Row, class Col>
93  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*N]) {
94  FMATVEC_ASSERT(A.cols() == N, AT);
95  copy(A);
96  }
97 
110  Matrix(const std::string &strs);
111  Matrix(const char *strs);
112 
116  delete[] ele;
117  }
118 
119  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Noinit) {
120  delete[] ele;
121  M = m;
122  ele = new AT[M*N];
123  return *this;
124  }
125 
126  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Init ini=INIT, const AT &a=AT()) { return resize(m,Noinit()).init(a); }
127 
128  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Eye ini, const AT &a=1) { return resize(m,Noinit()).init(ini,a); }
129 
132  void resize(int m, int n) {
133  if(n!=N)
134  throw std::runtime_error("A var-fixed matrix can only be resized in the first dimension.");
135  resize(m);
136  }
137 
145  FMATVEC_ASSERT(M == A.rows(), AT);
146  return copy(A);
147  }
148 
155  template <class Type, class Row, class Col>
157  FMATVEC_ASSERT(N == A.cols(), AT);
158  FMATVEC_ASSERT(M == A.rows(), AT);
159  return copy(A);
160  }
161 
168  template <class Type, class Row, class Col>
169  inline Matrix<General,Var,Fixed<N>,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
170  FMATVEC_ASSERT(N == A.cols(), AT);
171  if(M!=A.rows()) resize(A.rows(),NONINIT);
172  return copy(A);
173  }
174 
184  AT& operator()(int i, int j) {
185  FMATVEC_ASSERT(i>=0, AT);
186  FMATVEC_ASSERT(j>=0, AT);
187  FMATVEC_ASSERT(i<M, AT);
188  FMATVEC_ASSERT(j<N, AT);
189 
190  return e(i,j);
191  }
192 
197  const AT& operator()(int i, int j) const {
198  FMATVEC_ASSERT(i>=0, AT);
199  FMATVEC_ASSERT(j>=0, AT);
200  FMATVEC_ASSERT(i<M, AT);
201  FMATVEC_ASSERT(j<N, AT);
202 
203  return e(i,j);
204  }
205 
206  AT& e(int i, int j) {
207  return ele[i*N+j];
208  }
209 
214  const AT& e(int i, int j) const {
215  return ele[i*N+j];
216  }
217 
218  AT& e(int i) {
219  return ele[i];
220  }
221 
226  const AT& e(int i) const {
227  return ele[i];
228  }
229 
235  AT* operator()() {return ele;}
236 
241  const AT* operator()() const {return ele;}
242 
247  int rows() const {return M;}
248 
253  int cols() const {return N;}
254 
259  int ldim() const {return N;}
260 
267  const CBLAS_TRANSPOSE blasTrans() const {
268  return CblasNoTrans;
269  }
270 
278  const CBLAS_ORDER blasOrder() const {
279  return CblasRowMajor;
280  }
281 
282  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
283 
284  inline const RowVector<Fixed<N>,AT> row(int i) const;
285  inline const Vector<Var,AT> col(int j) const;
286 
294  inline Matrix<General,Var,Fixed<N>,AT>& init(const AT &val=AT());
295  inline Matrix<General,Var,Fixed<N>,AT>& init(Init, const AT &a=AT()) { return init(a); }
296  inline Matrix<General,Var,Fixed<N>,AT>& init(Eye, const AT &val=1);
297  inline Matrix<General,Var,Fixed<N>,AT>& init(Noinit, const AT &a=AT()) { return *this; }
298 
303  explicit inline operator std::vector<std::vector<AT>>() const;
304 
310  explicit inline Matrix(const std::vector<std::vector<AT>> &m);
311 
312 // /*! \brief Cast to AT.
313 // *
314 // * \return The AT representation of the matrix
315 // * */
316 // explicit operator AT() const {
317 // FMATVEC_ASSERT(M==1, AT);
318 // FMATVEC_ASSERT(N==1, AT);
319 // return ele[0];
320 // }
321 //
322 // /*! \brief AT Constructor.
323 // * Constructs and initializes a matrix with a AT object.
324 // * \param x The AT the matrix will be initialized with.
325 // * */
326 // explicit Matrix(const AT &x) : M(1), ele(new AT[1]) {
327 // FMATVEC_ASSERT(N==1, AT);
328 // ele[0] = x;
329 // }
330 
331  inline const Matrix<General,Fixed<N>,Var,AT> T() const;
332 
333  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
334 
335  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
336 
337  template<class Type, class Row, class Col> inline void set(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A);
338 
339  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
340 
341  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
342 
343  template<class Type, class Row, class Col> inline void add(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A);
344  };
345 
346  template <int N, class AT>
347  Matrix<General,Var,Fixed<N>,AT>::Matrix(const std::string &strs) : ele(0) {
348  std::istringstream iss(strs);
349  iss>>*this;
350 
351  // check end of stream
352  iss>>std::ws;
353  if(!iss.eof())
354  throw std::runtime_error("Input not fully read.");
355  }
356  template <int N, class AT> Matrix<General,Var,Fixed<N>,AT>::Matrix(const char * strs) :
357  Matrix<General,Var,Fixed<N>,AT>::Matrix(std::string(strs)) {}
358 
359  template <int N, class AT>
360  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(const AT &val) {
361  for(int i=0; i<M*N; i++)
362  e(i) = val;
363  return *this;
364  }
365 
366  template <int N, class AT>
367  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(Eye eye, const AT &val) {
368  for(int i=0; i<M; i++)
369  for(int j=0; j<N; j++)
370  e(i,j) = (i==j) ? val : 0;
371  return *this;
372  }
373 
374  template <int N, class AT>
375  inline const Matrix<General,Var,Var,AT> Matrix<General,Var,Fixed<N>,AT>::operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const {
376  FMATVEC_ASSERT(I.end()<M, AT);
377  FMATVEC_ASSERT(J.end()<N, AT);
378  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
379 
380  for(int i=0; i<A.rows(); i++)
381  for(int j=0; j<A.cols(); j++)
382  A.e(i,j) = e(I.start()+i,J.start()+j);
383 
384  return A;
385  }
386 
387  template <int N, class AT>
388  inline const RowVector<Fixed<N>,AT> Matrix<General,Var,Fixed<N>,AT>::row(int i) const {
389 
390  FMATVEC_ASSERT(i>=0, AT);
391  FMATVEC_ASSERT(i<M, AT);
392 
393  RowVector<Fixed<N>,AT> x(NONINIT);
394 
395  for(int j=0; j<N; j++)
396  x.e(j) = e(i,j);
397 
398  return x;
399 
400  }
401 
402  template <int N, class AT>
403  inline const Vector<Var,AT> Matrix<General,Var,Fixed<N>,AT>::col(int j) const {
404 
405  FMATVEC_ASSERT(j>=0, AT);
406  FMATVEC_ASSERT(j<N, AT);
407 
408  Vector<Var,AT> x(M,NONINIT);
409 
410  for(int i=0; i<M; i++)
411  x.e(i) = e(i,j);
412 
413  return x;
414 
415  }
416 
417  template <int N, class AT>
418  inline const Matrix<General,Fixed<N>,Var,AT> Matrix<General,Var,Fixed<N>,AT>::T() const {
419  Matrix<General,Fixed<N>,Var,AT> A(rows(),NONINIT);
420  for(int i=0; i<N; i++)
421  for(int j=0; j<M; j++)
422  A.e(i,j) = e(j,i);
423  return A;
424  }
425 
426  template <int N, class AT> template <class Row>
427  inline void Matrix<General,Var,Fixed<N>,AT>::set(int j, const Vector<Row,AT> &x) {
428  FMATVEC_ASSERT(j<cols(), AT);
429  FMATVEC_ASSERT(rows()==x.size(), AT);
430  for(int i=0; i<rows(); i++)
431  e(i,j) = x.e(i);
432  }
433 
434  template <int N, class AT> template <class Col>
435  inline void Matrix<General,Var,Fixed<N>,AT>::set(int i, const RowVector<Col,AT> &x) {
436  FMATVEC_ASSERT(i<rows(), AT);
437  FMATVEC_ASSERT(cols()==x.size(), AT);
438  for(int j=0; j<cols(); j++)
439  e(i,j) = x.e(j);
440  }
441 
442  template <int N, class AT> template<class Type, class Row, class Col>
443  inline void Matrix<General,Var,Fixed<N>,AT>::set(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
444 
445  FMATVEC_ASSERT(I.end()<rows(), AT);
446  FMATVEC_ASSERT(J.end()<cols(), AT);
447  FMATVEC_ASSERT(I.size()==A.rows(), AT);
448  FMATVEC_ASSERT(J.size()==A.cols(), AT);
449 
450  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
451  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
452  e(i,j) = A.e(ii,jj);
453  }
454 
455  template <int N, class AT> template <class Row>
456  inline void Matrix<General,Var,Fixed<N>,AT>::add(int j, const Vector<Row,AT> &x) {
457  FMATVEC_ASSERT(j<cols(), AT);
458  FMATVEC_ASSERT(rows()==x.size(), AT);
459  for(int i=0; i<rows(); i++)
460  e(i,j) += x.e(i);
461  }
462 
463  template <int N, class AT> template <class Col>
464  inline void Matrix<General,Var,Fixed<N>,AT>::add(int i, const RowVector<Col,AT> &x) {
465  FMATVEC_ASSERT(i<rows(), AT);
466  FMATVEC_ASSERT(cols()==x.size(), AT);
467  for(int j=0; j<cols(); j++)
468  e(i,j) += x.e(j);
469  }
470 
471  template <int N, class AT> template<class Type, class Row, class Col>
472  inline void Matrix<General,Var,Fixed<N>,AT>::add(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
473 
474  FMATVEC_ASSERT(I.end()<rows(), AT);
475  FMATVEC_ASSERT(J.end()<cols(), AT);
476  FMATVEC_ASSERT(I.size()==A.rows(), AT);
477  FMATVEC_ASSERT(J.size()==A.cols(), AT);
478 
479  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
480  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
481  e(i,j) += A.e(ii,jj);
482  }
483 
484  template <int N, class AT>
485  inline Matrix<General,Var,Fixed<N>,AT>::operator std::vector<std::vector<AT>>() const {
486  std::vector<std::vector<AT>> ret(rows());
487  for(int r=0; r<rows(); r++) {
488  ret[r].resize(cols());
489  for(int c=0; c<cols(); c++)
490  ret[r][c]=e(r,c);
491  }
492  return ret;
493  }
494 
495  template <int N, class AT>
496  inline Matrix<General,Var,Fixed<N>,AT>::Matrix(const std::vector<std::vector<AT>> &m) : M(static_cast<int>(m.size())), ele(new AT[M*N]) {
497  if(m[0].size() != N)
498  throw std::runtime_error("The input has "+std::to_string(m[0].size())+" columns but "+std::to_string(N)+" columns are required.");
499  for(int r=0; r<rows(); r++) {
500  if(static_cast<int>(m[r].size())!=cols())
501  throw std::runtime_error("The rows of the input have different length.");
502  for(int c=0; c<cols(); c++)
503  e(r,c)=m[r][c];
504  }
505  }
506 
508 
509  template <int N, class AT> template <class Type, class Row, class Col>
510  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::copy(const Matrix<Type,Row,Col,AT> &A) {
511  for(int i=0; i<M; i++)
512  for(int j=0; j<N; j++)
513  e(i,j) = A.e(i,j);
514  return *this;
515  }
516 
518 
519 }
520 
521 #endif
Definition: fmatvec.h:38
int rows() const
Number of rows.
Definition: var_fixed_general_matrix.h:247
Matrix< General, Var, Fixed< N >, AT > & operator=(const Matrix< General, Var, Fixed< N >, AT > &A)
Assignment operator.
Definition: var_fixed_general_matrix.h:144
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
const AT & e(int i) const
Element operator.
Definition: var_fixed_general_matrix.h:226
const AT * operator()() const
Pointer operator.
Definition: var_fixed_general_matrix.h:241
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:82
Definition: types.h:85
Definition: types.h:97
Definition: fmatvec.h:50
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:82
AT * operator()()
Pointer operator.
Definition: var_fixed_general_matrix.h:235
AT & operator()(int i, int j)
Element operator.
Definition: var_fixed_general_matrix.h:184
Definition: fmatvec.h:47
int ldim() const
Leading dimension.
Definition: var_fixed_general_matrix.h:259
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_fixed_general_matrix.h:267
int cols() const
Number of columns.
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_fixed_general_matrix.h:278
int cols() const
Number of columns.
Definition: var_fixed_general_matrix.h:253
Namespace fmatvec.
Definition: _memory.cc:28
const AT & operator()(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:197
~Matrix()
Destructor.
Definition: var_fixed_general_matrix.h:115
Matrix< General, Var, Fixed< N >, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_fixed_general_matrix.h:156
const AT & e(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:214
Definition: types.h:84
void resize(int m, int n)
Definition: var_fixed_general_matrix.h:132
Basic shape class for matrices.
Definition: types.h:132
Matrix(const Matrix< General, Var, Fixed< N >, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:72
int rows() const
Number of rows.
This is an index class for creating submatrices.
Definition: range.h:44
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:93
Definition: types.h:86