fmatvec  0.0.0
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 "range.h"
27 #include <vector>
28 #include <cstdlib>
29 #include <stdexcept>
30 
31 namespace fmatvec {
32 
41  template <int N, class AT> class Matrix<fmatvec::General,fmatvec::Var,fmatvec::Fixed<N>,AT> {
42 
43  public:
44  static constexpr bool isVector {false};
45  using value_type = AT;
46  using shape_type = General;
47 
49 
50  protected:
51 
52  int M{0};
53 
54  AT *ele;
55 
56  template <class Type, class Row, class Col> inline Matrix<General,Var,Fixed<N>,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
57 
59 
60  public:
61 
62  explicit Matrix() : ele(nullptr) { }
63 
64  explicit Matrix(int m, Noinit) : M(m), ele(new AT[M*N]) { }
65  explicit Matrix(int m, Init ini=INIT, const AT &a=AT()) : M(m), ele(new AT[M*N]) { init(a); }
66  explicit Matrix(int m, Eye ini, const AT &a=1) : M(m), ele(new AT[M*N]) { init(ini,a); }
67  explicit Matrix(int m, int n, Noinit) : M(m), ele(new AT[M*N]) { FMATVEC_ASSERT(n==N, AT); }
68  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); }
69  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); }
70 
71  // move
72  Matrix(Matrix<General,Var,Fixed<N>,AT> &&src) noexcept {
73  M=src.M;
74  src.M=0;
75  ele=src.ele;
76  src.ele=nullptr;
77  }
78  Matrix<General,Var,Fixed<N>,AT>& operator=(Matrix<General,Var,Fixed<N>,AT> &&src) noexcept {
79  FMATVEC_ASSERT(M == src.rows(), AT);
80  src.M=0;
81  delete[]ele;
82  ele=src.ele;
83  src.ele=nullptr;
84  return *this;
85  }
86 
92  Matrix(const Matrix<General,Var,Fixed<N>,AT> &A) : M(A.M), ele(new AT[M*N]) {
93  copy(A);
94  }
95 
101  template<class Row, class Col>
102  Matrix(const Matrix<General,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*N]) {
103  FMATVEC_ASSERT(A.cols() == N, AT);
104  copy(A);
105  }
106 
112  template<class Type, class Row, class Col>
113  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*N]) {
114  FMATVEC_ASSERT(A.cols() == N, AT);
115  copy(A);
116  }
117 
130  Matrix(const std::string &strs);
131  Matrix(const char *strs);
132 
133  using iterator = AT *;
134  using const_iterator = const AT *;
135  iterator begin() { return &ele[0]; }
136  iterator end() { return &ele[M*N]; }
137  const_iterator begin() const { return &ele[0]; }
138  const_iterator end() const { return &ele[M*N]; }
139 
143  delete[] ele;
144  }
145 
146  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Noinit) {
147  delete[] ele;
148  M = m;
149  ele = new AT[M*N];
150  return *this;
151  }
152 
153  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Init ini=INIT, const AT &a=AT()) { return resize(m,Noinit()).init(a); }
154 
155  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Eye ini, const AT &a=1) { return resize(m,Noinit()).init(ini,a); }
156 
159  void resize(int m, int n) {
160  if(n!=N)
161  throw std::runtime_error("A var-fixed matrix can only be resized in the first dimension.");
162  resize(m);
163  }
164 
172  FMATVEC_ASSERT(M == A.rows(), AT);
173  return copy(A);
174  }
175 
182  template <class Type, class Row, class Col>
184  FMATVEC_ASSERT(N == A.cols(), AT);
185  FMATVEC_ASSERT(M == A.rows(), AT);
186  return copy(A);
187  }
188 
195  template <class Type, class Row, class Col>
196  inline Matrix<General,Var,Fixed<N>,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
197  FMATVEC_ASSERT(N == A.cols(), AT);
198  if(M!=A.rows()) resize(A.rows(),NONINIT);
199  return copy(A);
200  }
201  // move
202  inline Matrix<General,Var,Fixed<N>,AT>& operator<<=(Matrix<General,Var,Fixed<N>,AT> &&src) {
203  FMATVEC_ASSERT(N == src.cols(), AT);
204  M=src.M;
205  src.M=0;
206  delete[]ele;
207  ele=src.ele;
208  src.ele=nullptr;
209  return *this;
210  }
211 
221  AT& operator()(int i, int j) {
222  FMATVEC_ASSERT(i>=0, AT);
223  FMATVEC_ASSERT(j>=0, AT);
224  FMATVEC_ASSERT(i<M, AT);
225  FMATVEC_ASSERT(j<N, AT);
226 
227  return e(i,j);
228  }
229 
234  const AT& operator()(int i, int j) const {
235  FMATVEC_ASSERT(i>=0, AT);
236  FMATVEC_ASSERT(j>=0, AT);
237  FMATVEC_ASSERT(i<M, AT);
238  FMATVEC_ASSERT(j<N, AT);
239 
240  return e(i,j);
241  }
242 
243  AT& e(int i, int j) {
244  return ele[i*N+j];
245  }
246 
251  const AT& e(int i, int j) const {
252  return ele[i*N+j];
253  }
254 
255  AT& e(int i) {
256  return ele[i];
257  }
258 
263  const AT& e(int i) const {
264  return ele[i];
265  }
266 
272  AT* operator()() {return ele;}
273 
278  const AT* operator()() const {return ele;}
279 
284  constexpr int rows() const {return M;}
285 
290  constexpr int cols() const {return N;}
291 
296  int ldim() const {return N;}
297 
304  const CBLAS_TRANSPOSE blasTrans() const {
305  return CblasNoTrans;
306  }
307 
315  const CBLAS_ORDER blasOrder() const {
316  return CblasRowMajor;
317  }
318 
319  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
320 
321  inline const RowVector<Fixed<N>,AT> row(int i) const;
322  inline const Vector<Var,AT> col(int j) const;
323 
331  inline Matrix<General,Var,Fixed<N>,AT>& init(const AT &val=AT());
332  inline Matrix<General,Var,Fixed<N>,AT>& init(Init, const AT &a=AT()) { return init(a); }
333  inline Matrix<General,Var,Fixed<N>,AT>& init(Eye, const AT &val=1);
334  inline Matrix<General,Var,Fixed<N>,AT>& init(Noinit, const AT &a=AT()) { return *this; }
335 
340  explicit inline operator std::vector<std::vector<AT>>() const;
341 
347  explicit inline Matrix(const std::vector<std::vector<AT>> &m);
348 
349 // /*! \brief Cast to AT.
350 // *
351 // * \return The AT representation of the matrix
352 // * */
353 // explicit operator AT() const {
354 // FMATVEC_ASSERT(M==1, AT);
355 // FMATVEC_ASSERT(N==1, AT);
356 // return ele[0];
357 // }
358 //
359 // /*! \brief AT Constructor.
360 // * Constructs and initializes a matrix with a AT object.
361 // * \param x The AT the matrix will be initialized with.
362 // * */
363 // explicit Matrix(const AT &x) : M(1), ele(new AT[1]) {
364 // FMATVEC_ASSERT(N==1, AT);
365 // ele[0] = x;
366 // }
367 
368  inline const Matrix<General,Fixed<N>,Var,AT> T() const;
369 
370  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
371 
372  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
373 
374  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);
375 
376  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
377 
378  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
379 
380  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);
381  };
382 
383  template <int N, class AT>
384  Matrix<General,Var,Fixed<N>,AT>::Matrix(const std::string &strs) : ele(0) {
385  std::istringstream iss(strs);
386  iss>>*this;
387 
388  // check end of stream
389  iss>>std::ws;
390  if(!iss.eof())
391  throw std::runtime_error("Input not fully read.");
392  }
393  template <int N, class AT> Matrix<General,Var,Fixed<N>,AT>::Matrix(const char * strs) :
394  Matrix<General,Var,Fixed<N>,AT>::Matrix(std::string(strs)) {}
395 
396  template <int N, class AT>
397  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(const AT &val) {
398  for(int i=0; i<M*N; i++)
399  e(i) = val;
400  return *this;
401  }
402 
403  template <int N, class AT>
404  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(Eye eye, const AT &val) {
405  for(int i=0; i<M; i++)
406  for(int j=0; j<N; j++)
407  e(i,j) = (i==j) ? val : 0;
408  return *this;
409  }
410 
411  template <int N, class AT>
412  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 {
413  FMATVEC_ASSERT(I.end()<M, AT);
414  FMATVEC_ASSERT(J.end()<N, AT);
415  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
416 
417  for(int i=0; i<A.rows(); i++)
418  for(int j=0; j<A.cols(); j++)
419  A.e(i,j) = e(I.start()+i,J.start()+j);
420 
421  return A;
422  }
423 
424  template <int N, class AT>
425  inline const RowVector<Fixed<N>,AT> Matrix<General,Var,Fixed<N>,AT>::row(int i) const {
426 
427  FMATVEC_ASSERT(i>=0, AT);
428  FMATVEC_ASSERT(i<M, AT);
429 
430  RowVector<Fixed<N>,AT> x(NONINIT);
431 
432  for(int j=0; j<N; j++)
433  x.e(j) = e(i,j);
434 
435  return x;
436 
437  }
438 
439  template <int N, class AT>
440  inline const Vector<Var,AT> Matrix<General,Var,Fixed<N>,AT>::col(int j) const {
441 
442  FMATVEC_ASSERT(j>=0, AT);
443  FMATVEC_ASSERT(j<N, AT);
444 
445  Vector<Var,AT> x(M,NONINIT);
446 
447  for(int i=0; i<M; i++)
448  x.e(i) = e(i,j);
449 
450  return x;
451 
452  }
453 
454  template <int N, class AT>
455  inline const Matrix<General,Fixed<N>,Var,AT> Matrix<General,Var,Fixed<N>,AT>::T() const {
456  Matrix<General,Fixed<N>,Var,AT> A(rows(),NONINIT);
457  for(int i=0; i<N; i++)
458  for(int j=0; j<M; j++)
459  A.e(i,j) = e(j,i);
460  return A;
461  }
462 
463  template <int N, class AT> template <class Row>
464  inline void Matrix<General,Var,Fixed<N>,AT>::set(int j, const Vector<Row,AT> &x) {
465  FMATVEC_ASSERT(j<cols(), AT);
466  FMATVEC_ASSERT(rows()==x.size(), AT);
467  for(int i=0; i<rows(); i++)
468  e(i,j) = x.e(i);
469  }
470 
471  template <int N, class AT> template <class Col>
472  inline void Matrix<General,Var,Fixed<N>,AT>::set(int i, const RowVector<Col,AT> &x) {
473  FMATVEC_ASSERT(i<rows(), AT);
474  FMATVEC_ASSERT(cols()==x.size(), AT);
475  for(int j=0; j<cols(); j++)
476  e(i,j) = x.e(j);
477  }
478 
479  template <int N, class AT> template<class Type, class Row, class Col>
480  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) {
481 
482  FMATVEC_ASSERT(I.end()<rows(), AT);
483  FMATVEC_ASSERT(J.end()<cols(), AT);
484  FMATVEC_ASSERT(I.size()==A.rows(), AT);
485  FMATVEC_ASSERT(J.size()==A.cols(), AT);
486 
487  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
488  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
489  e(i,j) = A.e(ii,jj);
490  }
491 
492  template <int N, class AT> template <class Row>
493  inline void Matrix<General,Var,Fixed<N>,AT>::add(int j, const Vector<Row,AT> &x) {
494  FMATVEC_ASSERT(j<cols(), AT);
495  FMATVEC_ASSERT(rows()==x.size(), AT);
496  for(int i=0; i<rows(); i++)
497  e(i,j) += x.e(i);
498  }
499 
500  template <int N, class AT> template <class Col>
501  inline void Matrix<General,Var,Fixed<N>,AT>::add(int i, const RowVector<Col,AT> &x) {
502  FMATVEC_ASSERT(i<rows(), AT);
503  FMATVEC_ASSERT(cols()==x.size(), AT);
504  for(int j=0; j<cols(); j++)
505  e(i,j) += x.e(j);
506  }
507 
508  template <int N, class AT> template<class Type, class Row, class Col>
509  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) {
510 
511  FMATVEC_ASSERT(I.end()<rows(), AT);
512  FMATVEC_ASSERT(J.end()<cols(), AT);
513  FMATVEC_ASSERT(I.size()==A.rows(), AT);
514  FMATVEC_ASSERT(J.size()==A.cols(), AT);
515 
516  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
517  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
518  e(i,j) += A.e(ii,jj);
519  }
520 
521  template <int N, class AT>
522  inline Matrix<General,Var,Fixed<N>,AT>::operator std::vector<std::vector<AT>>() const {
523  std::vector<std::vector<AT>> ret(rows(),std::vector<AT>(cols()));
524  for(int r=0; r<rows(); r++) {
525  for(int c=0; c<cols(); c++)
526  ret[r][c]=e(r,c);
527  }
528  return ret;
529  }
530 
531  template <int N, class AT>
532  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]) {
533  if(m[0].size() != N)
534  throw std::runtime_error("The input has "+std::to_string(m[0].size())+" columns but "+std::to_string(N)+" columns are required.");
535  for(int r=0; r<rows(); r++) {
536  if(static_cast<int>(m[r].size())!=cols())
537  throw std::runtime_error("The rows of the input have different length.");
538  for(int c=0; c<cols(); c++)
539  e(r,c)=m[r][c];
540  }
541  }
542 
544 
545  template <int N, class AT> template <class Type, class Row, class Col>
546  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::copy(const Matrix<Type,Row,Col,AT> &A) {
547  for(int i=0; i<M; i++)
548  for(int j=0; j<N; j++)
549  e(i,j) = A.e(i,j);
550  return *this;
551  }
552 
554 
555 }
556 
557 #endif
Definition: fmatvec.h:39
Matrix< General, Var, Fixed< N >, AT > & operator=(const Matrix< General, Var, Fixed< N >, AT > &A)
Assignment operator.
Definition: var_fixed_general_matrix.h:171
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:42
const AT & e(int i) const
Element operator.
Definition: var_fixed_general_matrix.h:263
const AT * operator()() const
Pointer operator.
Definition: var_fixed_general_matrix.h:278
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
Definition: types.h:89
Definition: types.h:101
Definition: fmatvec.h:51
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:102
constexpr int rows() const
Number of rows.
Definition: var_fixed_general_matrix.h:284
AT * operator()()
Pointer operator.
Definition: var_fixed_general_matrix.h:272
AT & operator()(int i, int j)
Element operator.
Definition: var_fixed_general_matrix.h:221
Definition: fmatvec.h:48
int ldim() const
Leading dimension.
Definition: var_fixed_general_matrix.h:296
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_fixed_general_matrix.h:304
int cols() const
Number of columns.
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_fixed_general_matrix.h:315
Namespace fmatvec.
Definition: _memory.cc:28
const AT & operator()(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:234
~Matrix()
Destructor.
Definition: var_fixed_general_matrix.h:142
Matrix< General, Var, Fixed< N >, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_fixed_general_matrix.h:183
const AT & e(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:251
Definition: types.h:88
void resize(int m, int n)
Definition: var_fixed_general_matrix.h:159
Shape class for general matrices.
Definition: types.h:112
constexpr int cols() const
Number of columns.
Definition: var_fixed_general_matrix.h:290
Matrix(const Matrix< General, Var, Fixed< N >, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:92
int rows() const
Number of rows.
This is an index class for creating submatrices.
Definition: range.h:45
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:113
Definition: types.h:90