fmatvec  0.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  static constexpr bool isVector {false};
43  typedef AT value_type;
44  typedef General shape_type;
45 
47 
48  protected:
49 
50  int M{0};
51 
52  AT *ele;
53 
54  template <class Type, class Row, class Col> inline Matrix<General,Var,Fixed<N>,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
55 
57 
58  public:
59 
60  explicit Matrix() : ele(nullptr) { }
61 
62  explicit Matrix(int m, Noinit) : M(m), ele(new AT[M*N]) { }
63  explicit Matrix(int m, Init ini=INIT, const AT &a=AT()) : M(m), ele(new AT[M*N]) { init(a); }
64  explicit Matrix(int m, Eye ini, const AT &a=1) : M(m), ele(new AT[M*N]) { init(ini,a); }
65  explicit Matrix(int m, int n, Noinit) : M(m), ele(new AT[M*N]) { FMATVEC_ASSERT(n==N, AT); }
66  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); }
67  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); }
68 
74  Matrix(const Matrix<General,Var,Fixed<N>,AT> &A) : M(A.M), ele(new AT[M*N]) {
75  copy(A);
76  }
77 
83  template<class Row, class Col>
84  Matrix(const Matrix<General,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*N]) {
85  FMATVEC_ASSERT(A.cols() == N, AT);
86  copy(A);
87  }
88 
94  template<class Type, class Row, class Col>
95  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*N]) {
96  FMATVEC_ASSERT(A.cols() == N, AT);
97  copy(A);
98  }
99 
112  Matrix(const std::string &strs);
113  Matrix(const char *strs);
114 
115  typedef AT* iterator;
116  typedef const AT* const_iterator;
117  iterator begin() { return &ele[0]; }
118  iterator end() { return &ele[M*N]; }
119  const_iterator begin() const { return &ele[0]; }
120  const_iterator end() const { return &ele[M*N]; }
121 
125  delete[] ele;
126  }
127 
128  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Noinit) {
129  delete[] ele;
130  M = m;
131  ele = new AT[M*N];
132  return *this;
133  }
134 
135  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Init ini=INIT, const AT &a=AT()) { return resize(m,Noinit()).init(a); }
136 
137  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Eye ini, const AT &a=1) { return resize(m,Noinit()).init(ini,a); }
138 
141  void resize(int m, int n) {
142  if(n!=N)
143  throw std::runtime_error("A var-fixed matrix can only be resized in the first dimension.");
144  resize(m);
145  }
146 
154  FMATVEC_ASSERT(M == A.rows(), AT);
155  return copy(A);
156  }
157 
164  template <class Type, class Row, class Col>
166  FMATVEC_ASSERT(N == A.cols(), AT);
167  FMATVEC_ASSERT(M == A.rows(), AT);
168  return copy(A);
169  }
170 
177  template <class Type, class Row, class Col>
178  inline Matrix<General,Var,Fixed<N>,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
179  FMATVEC_ASSERT(N == A.cols(), AT);
180  if(M!=A.rows()) resize(A.rows(),NONINIT);
181  return copy(A);
182  }
183 
193  AT& operator()(int i, int j) {
194  FMATVEC_ASSERT(i>=0, AT);
195  FMATVEC_ASSERT(j>=0, AT);
196  FMATVEC_ASSERT(i<M, AT);
197  FMATVEC_ASSERT(j<N, AT);
198 
199  return e(i,j);
200  }
201 
206  const AT& operator()(int i, int j) const {
207  FMATVEC_ASSERT(i>=0, AT);
208  FMATVEC_ASSERT(j>=0, AT);
209  FMATVEC_ASSERT(i<M, AT);
210  FMATVEC_ASSERT(j<N, AT);
211 
212  return e(i,j);
213  }
214 
215  AT& e(int i, int j) {
216  return ele[i*N+j];
217  }
218 
223  const AT& e(int i, int j) const {
224  return ele[i*N+j];
225  }
226 
227  AT& e(int i) {
228  return ele[i];
229  }
230 
235  const AT& e(int i) const {
236  return ele[i];
237  }
238 
244  AT* operator()() {return ele;}
245 
250  const AT* operator()() const {return ele;}
251 
256  constexpr int rows() const {return M;}
257 
262  constexpr int cols() const {return N;}
263 
268  int ldim() const {return N;}
269 
276  const CBLAS_TRANSPOSE blasTrans() const {
277  return CblasNoTrans;
278  }
279 
287  const CBLAS_ORDER blasOrder() const {
288  return CblasRowMajor;
289  }
290 
291  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
292 
293  inline const RowVector<Fixed<N>,AT> row(int i) const;
294  inline const Vector<Var,AT> col(int j) const;
295 
303  inline Matrix<General,Var,Fixed<N>,AT>& init(const AT &val=AT());
304  inline Matrix<General,Var,Fixed<N>,AT>& init(Init, const AT &a=AT()) { return init(a); }
305  inline Matrix<General,Var,Fixed<N>,AT>& init(Eye, const AT &val=1);
306  inline Matrix<General,Var,Fixed<N>,AT>& init(Noinit, const AT &a=AT()) { return *this; }
307 
312  explicit inline operator std::vector<std::vector<AT>>() const;
313 
319  explicit inline Matrix(const std::vector<std::vector<AT>> &m);
320 
321 // /*! \brief Cast to AT.
322 // *
323 // * \return The AT representation of the matrix
324 // * */
325 // explicit operator AT() const {
326 // FMATVEC_ASSERT(M==1, AT);
327 // FMATVEC_ASSERT(N==1, AT);
328 // return ele[0];
329 // }
330 //
331 // /*! \brief AT Constructor.
332 // * Constructs and initializes a matrix with a AT object.
333 // * \param x The AT the matrix will be initialized with.
334 // * */
335 // explicit Matrix(const AT &x) : M(1), ele(new AT[1]) {
336 // FMATVEC_ASSERT(N==1, AT);
337 // ele[0] = x;
338 // }
339 
340  inline const Matrix<General,Fixed<N>,Var,AT> T() const;
341 
342  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
343 
344  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
345 
346  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);
347 
348  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
349 
350  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
351 
352  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);
353  };
354 
355  template <int N, class AT>
356  Matrix<General,Var,Fixed<N>,AT>::Matrix(const std::string &strs) : ele(0) {
357  std::istringstream iss(strs);
358  iss>>*this;
359 
360  // check end of stream
361  iss>>std::ws;
362  if(!iss.eof())
363  throw std::runtime_error("Input not fully read.");
364  }
365  template <int N, class AT> Matrix<General,Var,Fixed<N>,AT>::Matrix(const char * strs) :
366  Matrix<General,Var,Fixed<N>,AT>::Matrix(std::string(strs)) {}
367 
368  template <int N, class AT>
369  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(const AT &val) {
370  for(int i=0; i<M*N; i++)
371  e(i) = val;
372  return *this;
373  }
374 
375  template <int N, class AT>
376  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(Eye eye, const AT &val) {
377  for(int i=0; i<M; i++)
378  for(int j=0; j<N; j++)
379  e(i,j) = (i==j) ? val : 0;
380  return *this;
381  }
382 
383  template <int N, class AT>
384  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 {
385  FMATVEC_ASSERT(I.end()<M, AT);
386  FMATVEC_ASSERT(J.end()<N, AT);
387  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
388 
389  for(int i=0; i<A.rows(); i++)
390  for(int j=0; j<A.cols(); j++)
391  A.e(i,j) = e(I.start()+i,J.start()+j);
392 
393  return A;
394  }
395 
396  template <int N, class AT>
397  inline const RowVector<Fixed<N>,AT> Matrix<General,Var,Fixed<N>,AT>::row(int i) const {
398 
399  FMATVEC_ASSERT(i>=0, AT);
400  FMATVEC_ASSERT(i<M, AT);
401 
402  RowVector<Fixed<N>,AT> x(NONINIT);
403 
404  for(int j=0; j<N; j++)
405  x.e(j) = e(i,j);
406 
407  return x;
408 
409  }
410 
411  template <int N, class AT>
412  inline const Vector<Var,AT> Matrix<General,Var,Fixed<N>,AT>::col(int j) const {
413 
414  FMATVEC_ASSERT(j>=0, AT);
415  FMATVEC_ASSERT(j<N, AT);
416 
417  Vector<Var,AT> x(M,NONINIT);
418 
419  for(int i=0; i<M; i++)
420  x.e(i) = e(i,j);
421 
422  return x;
423 
424  }
425 
426  template <int N, class AT>
427  inline const Matrix<General,Fixed<N>,Var,AT> Matrix<General,Var,Fixed<N>,AT>::T() const {
428  Matrix<General,Fixed<N>,Var,AT> A(rows(),NONINIT);
429  for(int i=0; i<N; i++)
430  for(int j=0; j<M; j++)
431  A.e(i,j) = e(j,i);
432  return A;
433  }
434 
435  template <int N, class AT> template <class Row>
436  inline void Matrix<General,Var,Fixed<N>,AT>::set(int j, const Vector<Row,AT> &x) {
437  FMATVEC_ASSERT(j<cols(), AT);
438  FMATVEC_ASSERT(rows()==x.size(), AT);
439  for(int i=0; i<rows(); i++)
440  e(i,j) = x.e(i);
441  }
442 
443  template <int N, class AT> template <class Col>
444  inline void Matrix<General,Var,Fixed<N>,AT>::set(int i, const RowVector<Col,AT> &x) {
445  FMATVEC_ASSERT(i<rows(), AT);
446  FMATVEC_ASSERT(cols()==x.size(), AT);
447  for(int j=0; j<cols(); j++)
448  e(i,j) = x.e(j);
449  }
450 
451  template <int N, class AT> template<class Type, class Row, class Col>
452  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) {
453 
454  FMATVEC_ASSERT(I.end()<rows(), AT);
455  FMATVEC_ASSERT(J.end()<cols(), AT);
456  FMATVEC_ASSERT(I.size()==A.rows(), AT);
457  FMATVEC_ASSERT(J.size()==A.cols(), AT);
458 
459  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
460  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
461  e(i,j) = A.e(ii,jj);
462  }
463 
464  template <int N, class AT> template <class Row>
465  inline void Matrix<General,Var,Fixed<N>,AT>::add(int j, const Vector<Row,AT> &x) {
466  FMATVEC_ASSERT(j<cols(), AT);
467  FMATVEC_ASSERT(rows()==x.size(), AT);
468  for(int i=0; i<rows(); i++)
469  e(i,j) += x.e(i);
470  }
471 
472  template <int N, class AT> template <class Col>
473  inline void Matrix<General,Var,Fixed<N>,AT>::add(int i, const RowVector<Col,AT> &x) {
474  FMATVEC_ASSERT(i<rows(), AT);
475  FMATVEC_ASSERT(cols()==x.size(), AT);
476  for(int j=0; j<cols(); j++)
477  e(i,j) += x.e(j);
478  }
479 
480  template <int N, class AT> template<class Type, class Row, class Col>
481  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) {
482 
483  FMATVEC_ASSERT(I.end()<rows(), AT);
484  FMATVEC_ASSERT(J.end()<cols(), AT);
485  FMATVEC_ASSERT(I.size()==A.rows(), AT);
486  FMATVEC_ASSERT(J.size()==A.cols(), AT);
487 
488  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
489  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
490  e(i,j) += A.e(ii,jj);
491  }
492 
493  template <int N, class AT>
494  inline Matrix<General,Var,Fixed<N>,AT>::operator std::vector<std::vector<AT>>() const {
495  std::vector<std::vector<AT>> ret(rows());
496  for(int r=0; r<rows(); r++) {
497  ret[r].resize(cols());
498  for(int c=0; c<cols(); c++)
499  ret[r][c]=e(r,c);
500  }
501  return ret;
502  }
503 
504  template <int N, class AT>
505  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]) {
506  if(m[0].size() != N)
507  throw std::runtime_error("The input has "+std::to_string(m[0].size())+" columns but "+std::to_string(N)+" columns are required.");
508  for(int r=0; r<rows(); r++) {
509  if(static_cast<int>(m[r].size())!=cols())
510  throw std::runtime_error("The rows of the input have different length.");
511  for(int c=0; c<cols(); c++)
512  e(r,c)=m[r][c];
513  }
514  }
515 
517 
518  template <int N, class AT> template <class Type, class Row, class Col>
519  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::copy(const Matrix<Type,Row,Col,AT> &A) {
520  for(int i=0; i<M; i++)
521  for(int j=0; j<N; j++)
522  e(i,j) = A.e(i,j);
523  return *this;
524  }
525 
527 
528 }
529 
530 #endif
Definition: fmatvec.h:38
Matrix< General, Var, Fixed< N >, AT > & operator=(const Matrix< General, Var, Fixed< N >, AT > &A)
Assignment operator.
Definition: var_fixed_general_matrix.h:153
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:235
const AT * operator()() const
Pointer operator.
Definition: var_fixed_general_matrix.h:250
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
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:84
constexpr int rows() const
Number of rows.
Definition: var_fixed_general_matrix.h:256
AT * operator()()
Pointer operator.
Definition: var_fixed_general_matrix.h:244
AT & operator()(int i, int j)
Element operator.
Definition: var_fixed_general_matrix.h:193
Definition: fmatvec.h:47
int ldim() const
Leading dimension.
Definition: var_fixed_general_matrix.h:268
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_fixed_general_matrix.h:276
int cols() const
Number of columns.
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_fixed_general_matrix.h:287
Namespace fmatvec.
Definition: _memory.cc:28
const AT & operator()(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:206
~Matrix()
Destructor.
Definition: var_fixed_general_matrix.h:124
Matrix< General, Var, Fixed< N >, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_fixed_general_matrix.h:165
const AT & e(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:223
Definition: types.h:84
void resize(int m, int n)
Definition: var_fixed_general_matrix.h:141
Basic shape class for matrices.
Definition: types.h:132
constexpr int cols() const
Number of columns.
Definition: var_fixed_general_matrix.h:262
Matrix(const Matrix< General, Var, Fixed< N >, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:74
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:95
Definition: types.h:86