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 
69  // move
70  Matrix(Matrix<General,Var,Fixed<N>,AT> &&src) {
71  M=src.M;
72  src.M=0;
73  ele=src.ele;
74  src.ele=nullptr;
75  }
76  Matrix<General,Var,Fixed<N>,AT>& operator=(Matrix<General,Var,Fixed<N>,AT> &&src) {
77  FMATVEC_ASSERT(M == src.rows(), AT);
78  src.M=0;
79  delete[]ele;
80  ele=src.ele;
81  src.ele=nullptr;
82  return *this;
83  }
84 
90  Matrix(const Matrix<General,Var,Fixed<N>,AT> &A) : M(A.M), ele(new AT[M*N]) {
91  copy(A);
92  }
93 
99  template<class Row, class Col>
100  Matrix(const Matrix<General,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*N]) {
101  FMATVEC_ASSERT(A.cols() == N, AT);
102  copy(A);
103  }
104 
110  template<class Type, class Row, class Col>
111  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*N]) {
112  FMATVEC_ASSERT(A.cols() == N, AT);
113  copy(A);
114  }
115 
128  Matrix(const std::string &strs);
129  Matrix(const char *strs);
130 
131  typedef AT* iterator;
132  typedef const AT* const_iterator;
133  iterator begin() { return &ele[0]; }
134  iterator end() { return &ele[M*N]; }
135  const_iterator begin() const { return &ele[0]; }
136  const_iterator end() const { return &ele[M*N]; }
137 
141  delete[] ele;
142  }
143 
144  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Noinit) {
145  delete[] ele;
146  M = m;
147  ele = new AT[M*N];
148  return *this;
149  }
150 
151  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Init ini=INIT, const AT &a=AT()) { return resize(m,Noinit()).init(a); }
152 
153  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Eye ini, const AT &a=1) { return resize(m,Noinit()).init(ini,a); }
154 
157  void resize(int m, int n) {
158  if(n!=N)
159  throw std::runtime_error("A var-fixed matrix can only be resized in the first dimension.");
160  resize(m);
161  }
162 
170  FMATVEC_ASSERT(M == A.rows(), AT);
171  return copy(A);
172  }
173 
180  template <class Type, class Row, class Col>
182  FMATVEC_ASSERT(N == A.cols(), AT);
183  FMATVEC_ASSERT(M == A.rows(), AT);
184  return copy(A);
185  }
186 
193  template <class Type, class Row, class Col>
194  inline Matrix<General,Var,Fixed<N>,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
195  FMATVEC_ASSERT(N == A.cols(), AT);
196  if(M!=A.rows()) resize(A.rows(),NONINIT);
197  return copy(A);
198  }
199  // move
200  template <class Type, class Row, class Col>
201  inline Matrix<General,Var,Fixed<N>,AT>& operator<<=(Matrix<General,Var,Fixed<N>,AT> &&src) {
202  FMATVEC_ASSERT(N == src.cols(), AT);
203  M=src.M;
204  src.M=0;
205  delete[]ele;
206  ele=src.ele;
207  src.ele=nullptr;
208  return *this;
209  }
210 
220  AT& operator()(int i, int j) {
221  FMATVEC_ASSERT(i>=0, AT);
222  FMATVEC_ASSERT(j>=0, AT);
223  FMATVEC_ASSERT(i<M, AT);
224  FMATVEC_ASSERT(j<N, AT);
225 
226  return e(i,j);
227  }
228 
233  const AT& operator()(int i, int j) const {
234  FMATVEC_ASSERT(i>=0, AT);
235  FMATVEC_ASSERT(j>=0, AT);
236  FMATVEC_ASSERT(i<M, AT);
237  FMATVEC_ASSERT(j<N, AT);
238 
239  return e(i,j);
240  }
241 
242  AT& e(int i, int j) {
243  return ele[i*N+j];
244  }
245 
250  const AT& e(int i, int j) const {
251  return ele[i*N+j];
252  }
253 
254  AT& e(int i) {
255  return ele[i];
256  }
257 
262  const AT& e(int i) const {
263  return ele[i];
264  }
265 
271  AT* operator()() {return ele;}
272 
277  const AT* operator()() const {return ele;}
278 
283  constexpr int rows() const {return M;}
284 
289  constexpr int cols() const {return N;}
290 
295  int ldim() const {return N;}
296 
303  const CBLAS_TRANSPOSE blasTrans() const {
304  return CblasNoTrans;
305  }
306 
314  const CBLAS_ORDER blasOrder() const {
315  return CblasRowMajor;
316  }
317 
318  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
319 
320  inline const RowVector<Fixed<N>,AT> row(int i) const;
321  inline const Vector<Var,AT> col(int j) const;
322 
330  inline Matrix<General,Var,Fixed<N>,AT>& init(const AT &val=AT());
331  inline Matrix<General,Var,Fixed<N>,AT>& init(Init, const AT &a=AT()) { return init(a); }
332  inline Matrix<General,Var,Fixed<N>,AT>& init(Eye, const AT &val=1);
333  inline Matrix<General,Var,Fixed<N>,AT>& init(Noinit, const AT &a=AT()) { return *this; }
334 
339  explicit inline operator std::vector<std::vector<AT>>() const;
340 
346  explicit inline Matrix(const std::vector<std::vector<AT>> &m);
347 
348 // /*! \brief Cast to AT.
349 // *
350 // * \return The AT representation of the matrix
351 // * */
352 // explicit operator AT() const {
353 // FMATVEC_ASSERT(M==1, AT);
354 // FMATVEC_ASSERT(N==1, AT);
355 // return ele[0];
356 // }
357 //
358 // /*! \brief AT Constructor.
359 // * Constructs and initializes a matrix with a AT object.
360 // * \param x The AT the matrix will be initialized with.
361 // * */
362 // explicit Matrix(const AT &x) : M(1), ele(new AT[1]) {
363 // FMATVEC_ASSERT(N==1, AT);
364 // ele[0] = x;
365 // }
366 
367  inline const Matrix<General,Fixed<N>,Var,AT> T() const;
368 
369  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
370 
371  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
372 
373  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);
374 
375  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
376 
377  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
378 
379  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);
380  };
381 
382  template <int N, class AT>
383  Matrix<General,Var,Fixed<N>,AT>::Matrix(const std::string &strs) : ele(0) {
384  std::istringstream iss(strs);
385  iss>>*this;
386 
387  // check end of stream
388  iss>>std::ws;
389  if(!iss.eof())
390  throw std::runtime_error("Input not fully read.");
391  }
392  template <int N, class AT> Matrix<General,Var,Fixed<N>,AT>::Matrix(const char * strs) :
393  Matrix<General,Var,Fixed<N>,AT>::Matrix(std::string(strs)) {}
394 
395  template <int N, class AT>
396  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(const AT &val) {
397  for(int i=0; i<M*N; i++)
398  e(i) = val;
399  return *this;
400  }
401 
402  template <int N, class AT>
403  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(Eye eye, const AT &val) {
404  for(int i=0; i<M; i++)
405  for(int j=0; j<N; j++)
406  e(i,j) = (i==j) ? val : 0;
407  return *this;
408  }
409 
410  template <int N, class AT>
411  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 {
412  FMATVEC_ASSERT(I.end()<M, AT);
413  FMATVEC_ASSERT(J.end()<N, AT);
414  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
415 
416  for(int i=0; i<A.rows(); i++)
417  for(int j=0; j<A.cols(); j++)
418  A.e(i,j) = e(I.start()+i,J.start()+j);
419 
420  return A;
421  }
422 
423  template <int N, class AT>
424  inline const RowVector<Fixed<N>,AT> Matrix<General,Var,Fixed<N>,AT>::row(int i) const {
425 
426  FMATVEC_ASSERT(i>=0, AT);
427  FMATVEC_ASSERT(i<M, AT);
428 
429  RowVector<Fixed<N>,AT> x(NONINIT);
430 
431  for(int j=0; j<N; j++)
432  x.e(j) = e(i,j);
433 
434  return x;
435 
436  }
437 
438  template <int N, class AT>
439  inline const Vector<Var,AT> Matrix<General,Var,Fixed<N>,AT>::col(int j) const {
440 
441  FMATVEC_ASSERT(j>=0, AT);
442  FMATVEC_ASSERT(j<N, AT);
443 
444  Vector<Var,AT> x(M,NONINIT);
445 
446  for(int i=0; i<M; i++)
447  x.e(i) = e(i,j);
448 
449  return x;
450 
451  }
452 
453  template <int N, class AT>
454  inline const Matrix<General,Fixed<N>,Var,AT> Matrix<General,Var,Fixed<N>,AT>::T() const {
455  Matrix<General,Fixed<N>,Var,AT> A(rows(),NONINIT);
456  for(int i=0; i<N; i++)
457  for(int j=0; j<M; j++)
458  A.e(i,j) = e(j,i);
459  return A;
460  }
461 
462  template <int N, class AT> template <class Row>
463  inline void Matrix<General,Var,Fixed<N>,AT>::set(int j, const Vector<Row,AT> &x) {
464  FMATVEC_ASSERT(j<cols(), AT);
465  FMATVEC_ASSERT(rows()==x.size(), AT);
466  for(int i=0; i<rows(); i++)
467  e(i,j) = x.e(i);
468  }
469 
470  template <int N, class AT> template <class Col>
471  inline void Matrix<General,Var,Fixed<N>,AT>::set(int i, const RowVector<Col,AT> &x) {
472  FMATVEC_ASSERT(i<rows(), AT);
473  FMATVEC_ASSERT(cols()==x.size(), AT);
474  for(int j=0; j<cols(); j++)
475  e(i,j) = x.e(j);
476  }
477 
478  template <int N, class AT> template<class Type, class Row, class Col>
479  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) {
480 
481  FMATVEC_ASSERT(I.end()<rows(), AT);
482  FMATVEC_ASSERT(J.end()<cols(), AT);
483  FMATVEC_ASSERT(I.size()==A.rows(), AT);
484  FMATVEC_ASSERT(J.size()==A.cols(), AT);
485 
486  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
487  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
488  e(i,j) = A.e(ii,jj);
489  }
490 
491  template <int N, class AT> template <class Row>
492  inline void Matrix<General,Var,Fixed<N>,AT>::add(int j, const Vector<Row,AT> &x) {
493  FMATVEC_ASSERT(j<cols(), AT);
494  FMATVEC_ASSERT(rows()==x.size(), AT);
495  for(int i=0; i<rows(); i++)
496  e(i,j) += x.e(i);
497  }
498 
499  template <int N, class AT> template <class Col>
500  inline void Matrix<General,Var,Fixed<N>,AT>::add(int i, const RowVector<Col,AT> &x) {
501  FMATVEC_ASSERT(i<rows(), AT);
502  FMATVEC_ASSERT(cols()==x.size(), AT);
503  for(int j=0; j<cols(); j++)
504  e(i,j) += x.e(j);
505  }
506 
507  template <int N, class AT> template<class Type, class Row, class Col>
508  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) {
509 
510  FMATVEC_ASSERT(I.end()<rows(), AT);
511  FMATVEC_ASSERT(J.end()<cols(), AT);
512  FMATVEC_ASSERT(I.size()==A.rows(), AT);
513  FMATVEC_ASSERT(J.size()==A.cols(), AT);
514 
515  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
516  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
517  e(i,j) += A.e(ii,jj);
518  }
519 
520  template <int N, class AT>
521  inline Matrix<General,Var,Fixed<N>,AT>::operator std::vector<std::vector<AT>>() const {
522  std::vector<std::vector<AT>> ret(rows());
523  for(int r=0; r<rows(); r++) {
524  ret[r].resize(cols());
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:38
Matrix< General, Var, Fixed< N >, AT > & operator=(const Matrix< General, Var, Fixed< N >, AT > &A)
Assignment operator.
Definition: var_fixed_general_matrix.h:169
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:262
const AT * operator()() const
Pointer operator.
Definition: var_fixed_general_matrix.h:277
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
Definition: types.h:89
Definition: types.h:101
Definition: fmatvec.h:50
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:100
constexpr int rows() const
Number of rows.
Definition: var_fixed_general_matrix.h:283
AT * operator()()
Pointer operator.
Definition: var_fixed_general_matrix.h:271
AT & operator()(int i, int j)
Element operator.
Definition: var_fixed_general_matrix.h:220
Definition: fmatvec.h:47
int ldim() const
Leading dimension.
Definition: var_fixed_general_matrix.h:295
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_fixed_general_matrix.h:303
int cols() const
Number of columns.
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_fixed_general_matrix.h:314
Namespace fmatvec.
Definition: _memory.cc:28
const AT & operator()(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:233
~Matrix()
Destructor.
Definition: var_fixed_general_matrix.h:140
Matrix< General, Var, Fixed< N >, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_fixed_general_matrix.h:181
const AT & e(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:250
Definition: types.h:88
void resize(int m, int n)
Definition: var_fixed_general_matrix.h:157
Basic shape class for matrices.
Definition: types.h:136
constexpr int cols() const
Number of columns.
Definition: var_fixed_general_matrix.h:289
Matrix(const Matrix< General, Var, Fixed< N >, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:90
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:111
Definition: types.h:90