fmatvec  0.0.0
Fast Matrix/Vector Library
fixed_var_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 fixed_var_general_matrix_h
23 #define fixed_var_general_matrix_h
24 
25 #include "types.h"
26 #include "range.h"
27 #include <cstdlib>
28 #include <stdexcept>
29 
30 namespace fmatvec {
31 
40  template <int M, class AT> class Matrix<General,Fixed<M>,Var,AT> {
41 
42  public:
43  static constexpr bool isVector {false};
44 
45  typedef AT value_type;
46  typedef General shape_type;
47 
49 
50  protected:
51 
52  int N{0};
53 
54  AT *ele;
55 
56  template <class Type, class Row, class Col> inline Matrix<General,Fixed<M>,Var,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
57 
59 
60  public:
61 
66  explicit Matrix() : ele(nullptr) { }
67 
68 // template<class Ini=All<AT>>
69 // Matrix(int n, Ini ini=All<AT>()) : N(n), ele(new AT[M*N]) {
70 // init(ini);
71 // }
72 // template<class Ini=All<AT>>
73 // Matrix(int m, int n, Ini ini=All<AT>()) : N(n), ele(new AT[M*N]) {
74 // init(ini);
75 // }
76 
77  explicit Matrix(int n, Noinit) : N(n), ele(new AT[M*N]) { }
78  explicit Matrix(int n, Init ini=INIT, const AT &a=AT()) : N(n), ele(new AT[M*N]) { init(a); }
79  explicit Matrix(int n, Eye ini, const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
80  explicit Matrix(int m, int n, Noinit) : N(n), ele(new AT[M*N]) { FMATVEC_ASSERT(m==M, AT); }
81  explicit Matrix(int m, int n, Init ini=INIT, const AT &a=AT()) : N(n), ele(new AT[M*N]) { FMATVEC_ASSERT(m==M, AT); init(a); }
82  explicit Matrix(int m, int n, Eye ini, const AT &a=1) : N(n), ele(new AT[M*N]) { FMATVEC_ASSERT(m==M, AT); init(ini,a); }
83 
89  Matrix(const Matrix<General,Fixed<M>,Var,AT> &A) : N(A.N), ele(new AT[M*N]) {
90  copy(A);
91  }
92 
98  template<class Row, class Col>
99  Matrix(const Matrix<General,Row,Col,AT> &A) : N(A.cols()), ele(new AT[M*N]) {
100  FMATVEC_ASSERT(A.rows() == M, AT);
101  copy(A);
102  }
103 
109  template<class Type, class Row, class Col>
110  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : N(A.cols()), ele(new AT[M*N]) {
111  FMATVEC_ASSERT(A.rows() == M, AT);
112  copy(A);
113  }
114 
127  Matrix(const std::string &strs);
128  Matrix(const char *strs);
129 
130  typedef AT* iterator;
131  typedef const AT* const_iterator;
132  iterator begin() { return &ele[0]; }
133  iterator end() { return &ele[M*N]; }
134  const_iterator begin() const { return &ele[0]; }
135  const_iterator end() const { return &ele[M*N]; }
136 
140  delete[] ele;
141  }
142 
143  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Noinit) {
144  delete[] ele;
145  N = n;
146  ele = new AT[M*N];
147  return *this;
148  }
149 
150  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Init ini=INIT, const AT &a=AT()) { return resize(n,Noinit()).init(a); }
151 
152  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Eye ini, const AT &a=1) { return resize(n,Noinit()).init(ini,a); }
153 
156  void resize(int m, int n) {
157  if(m!=M)
158  throw std::runtime_error("A fixed-var matrix can only be resized in the second dimension.");
159  resize(n);
160  }
161 
169  FMATVEC_ASSERT(N == A.cols(), AT);
170  return copy(A);
171  }
172 
179  template <class Type, class Row, class Col>
181  FMATVEC_ASSERT(M == A.rows(), AT);
182  FMATVEC_ASSERT(N == A.cols(), AT);
183  return copy(A);
184  }
185 
192  template<class Type, class Row, class Col>
193  inline Matrix<General,Fixed<M>,Var,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
194  FMATVEC_ASSERT(M == A.rows(), AT);
195  if(N!=A.cols()) resize(A.cols(),NONINIT);
196  return copy(A);
197  }
198 
208  AT& operator()(int i, int j) {
209  FMATVEC_ASSERT(i>=0, AT);
210  FMATVEC_ASSERT(j>=0, AT);
211  FMATVEC_ASSERT(i<M, AT);
212  FMATVEC_ASSERT(j<N, AT);
213 
214  return e(i,j);
215  }
216 
221  const AT& operator()(int i, int j) const {
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 
230  AT& e(int i, int j) {
231  return ele[i*N+j];
232  }
233 
238  const AT& e(int i, int j) const {
239  return ele[i*N+j];
240  }
241 
242  AT& e(int i) {
243  return ele[i];
244  }
245 
250  const AT& e(int i) const {
251  return ele[i];
252  }
253 
259  AT* operator()() {return ele;}
260 
265  const AT* operator()() const {return ele;}
266 
271  constexpr int rows() const {return M;}
272 
277  constexpr int cols() const {return N;}
278 
283  int ldim() const {return N;}
284 
291  const CBLAS_TRANSPOSE blasTrans() const {
292  return CblasNoTrans;
293  }
294 
302  const CBLAS_ORDER blasOrder() const {
303  return CblasRowMajor;
304  }
305 
332  //inline Matrix<GeneralVar, AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J);
333 
338  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
339 
340  template <int M1, int M2>
341  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> operator()(const Range<Fixed<M1>,Fixed<M2>> &I, const Range<Var,Var> &J) const;
342 
343  inline const RowVector<Var,AT> row(int i) const;
344  inline const Vector<Fixed<M>,AT> col(int j) const;
345 
353  inline Matrix<General,Fixed<M>,Var,AT>& init(const AT &val=AT());
354  inline Matrix<General,Fixed<M>,Var,AT>& init(Init, const AT &a=AT()) { return init(a); }
355  inline Matrix<General,Fixed<M>,Var,AT>& init(Eye, const AT &val=1);
356  inline Matrix<General,Fixed<M>,Var,AT>& init(Noinit, const AT &a=AT()) { return *this; }
357 
362  explicit inline operator std::vector<std::vector<AT>>() const;
363 
369  explicit inline Matrix(const std::vector<std::vector<AT>> &m);
370 
371 // /*! \brief Cast to AT.
372 // *
373 // * \return The AT representation of the matrix
374 // * */
375 // explicit operator AT() const {
376 // FMATVEC_ASSERT(M==1, AT);
377 // FMATVEC_ASSERT(N==1, AT);
378 // return ele[0];
379 // }
380 //
381 // /*! \brief AT Constructor.
382 // * Constructs and initializes a matrix with a AT object.
383 // * \param v The AT the matrix will be initialized with.
384 // * */
385 // explicit Matrix(const AT &x) : N(1), ele(new AT[1]) {
386 // FMATVEC_ASSERT(M==1, AT);
387 // ele[0] = x;
388 // }
389 
390  inline const Matrix<General,Var,Fixed<M>,AT> T() const;
391 
392  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
393 
394  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
395 
396  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);
397 
398  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
399 
400  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
401 
402  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);
403  };
404 
405  template <int M, class AT>
406  Matrix<General,Fixed<M>,Var,AT>::Matrix(const std::string &strs) : ele(nullptr) {
407  std::istringstream iss(strs);
408  iss>>*this;
409 
410  // check end of stream
411  iss>>std::ws;
412  if(!iss.eof())
413  throw std::runtime_error("Input not fully read.");
414  }
415  template <int M, class AT> Matrix<General,Fixed<M>,Var,AT>::Matrix(const char *strs) :
416  Matrix<General,Fixed<M>,Var,AT>::Matrix(std::string(strs)) {}
417 
418  template <int M, class AT>
419  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::init(const AT &val) {
420  for(int i=0; i<M*N; i++)
421  e(i) = val;
422  return *this;
423  }
424 
425  template <int M, class AT>
426  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::init(Eye eye, const AT &val) {
427  for(int i=0; i<M; i++)
428  for(int j=0; j<N; j++)
429  e(i,j) = (i==j) ? val : 0;
430  return *this;
431  }
432 
433  template <int M, class AT> template <int M1, int M2>
434  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> Matrix<General,Fixed<M>,Var,AT>::operator()(const Range<Fixed<M1>,Fixed<M2>> &I, const Range<Var,Var> &J) const {
435  FMATVEC_ASSERT(M2<M, AT);
436  FMATVEC_ASSERT(J.end()<N, AT);
437  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
438 
439  for(int i=0; i<A.rows(); i++)
440  for(int j=0; j<A.cols(); j++)
441  A.e(i,j) = e(M1+i,J.start()+j);
442 
443  return A;
444  }
445 
446  template <int M, class AT>
447  inline const Matrix<General,Var,Var,AT> Matrix<General,Fixed<M>,Var,AT>::operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const {
448  FMATVEC_ASSERT(I.end()<M, AT);
449  FMATVEC_ASSERT(J.end()<N, AT);
450  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
451 
452  for(int i=0; i<A.rows(); i++)
453  for(int j=0; j<A.cols(); j++)
454  A.e(i,j) = e(I.start()+i,J.start()+j);
455 
456  return A;
457  }
458 
459  template <int M, class AT>
460  inline const RowVector<Var,AT> Matrix<General,Fixed<M>,Var,AT>::row(int i) const {
461 
462  FMATVEC_ASSERT(i>=0, AT);
463  FMATVEC_ASSERT(i<M, AT);
464 
465  RowVector<Var,AT> x(N,NONINIT);
466 
467  for(int j=0; j<N; j++)
468  x.e(j) = e(i,j);
469 
470  return x;
471 
472  }
473 
474  template <int M, class AT>
475  inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::col(int j) const {
476 
477  FMATVEC_ASSERT(j>=0, AT);
478  FMATVEC_ASSERT(j<N, AT);
479 
480  Vector<Fixed<M>,AT> x(NONINIT);
481 
482  for(int i=0; i<M; i++)
483  x.e(i) = e(i,j);
484 
485  return x;
486 
487  }
488 
489  template <int M, class AT>
490  inline const Matrix<General,Var,Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::T() const {
491  Matrix<General,Var,Fixed<M>,AT> A(cols(),NONINIT);
492  for(int i=0; i<N; i++)
493  for(int j=0; j<M; j++)
494  A.e(i,j) = e(j,i);
495  return A;
496  }
497 
498  template <int M, class AT> template <class Row>
499  inline void Matrix<General,Fixed<M>,Var,AT>::set(int j, const Vector<Row,AT> &x) {
500  FMATVEC_ASSERT(j<cols(), AT);
501  FMATVEC_ASSERT(rows()==x.size(), AT);
502  for(int i=0; i<rows(); i++)
503  e(i,j) = x.e(i);
504  }
505 
506  template <int M, class AT> template <class Col>
507  inline void Matrix<General,Fixed<M>,Var,AT>::set(int i, const RowVector<Col,AT> &x) {
508  FMATVEC_ASSERT(i<rows(), AT);
509  FMATVEC_ASSERT(cols()==x.size(), AT);
510  for(int j=0; j<cols(); j++)
511  e(i,j) = x.e(j);
512  }
513 
514  template <int M, class AT> template<class Type, class Row, class Col>
515  inline void Matrix<General,Fixed<M>,Var,AT>::set(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
516 
517  FMATVEC_ASSERT(I.end()<rows(), AT);
518  FMATVEC_ASSERT(J.end()<cols(), AT);
519  FMATVEC_ASSERT(I.size()==A.rows(), AT);
520  FMATVEC_ASSERT(J.size()==A.cols(), AT);
521 
522  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
523  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
524  e(i,j) = A.e(ii,jj);
525  }
526 
527  template <int M, class AT> template <class Row>
528  inline void Matrix<General,Fixed<M>,Var,AT>::add(int j, const Vector<Row,AT> &x) {
529  FMATVEC_ASSERT(j<cols(), AT);
530  FMATVEC_ASSERT(rows()==x.size(), AT);
531  for(int i=0; i<rows(); i++)
532  e(i,j) += x.e(i);
533  }
534 
535  template <int M, class AT> template <class Col>
536  inline void Matrix<General,Fixed<M>,Var,AT>::add(int i, const RowVector<Col,AT> &x) {
537  FMATVEC_ASSERT(i<rows(), AT);
538  FMATVEC_ASSERT(cols()==x.size(), AT);
539  for(int j=0; j<cols(); j++)
540  e(i,j) += x.e(j);
541  }
542 
543  template <int M, class AT> template<class Type, class Row, class Col>
544  inline void Matrix<General,Fixed<M>,Var,AT>::add(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
545 
546  FMATVEC_ASSERT(I.end()<rows(), AT);
547  FMATVEC_ASSERT(J.end()<cols(), AT);
548  FMATVEC_ASSERT(I.size()==A.rows(), AT);
549  FMATVEC_ASSERT(J.size()==A.cols(), AT);
550 
551  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
552  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
553  e(i,j) += A.e(ii,jj);
554  }
555 
556  template <int M, class AT>
557  inline Matrix<General,Fixed<M>,Var,AT>::operator std::vector<std::vector<AT>>() const {
558  std::vector<std::vector<AT>> ret(rows());
559  for(int r=0; r<rows(); r++) {
560  ret[r].resize(cols());
561  for(int c=0; c<cols(); c++)
562  ret[r][c]=e(r,c);
563  }
564  return ret;
565  }
566 
567  template <int M, class AT>
568  inline Matrix<General,Fixed<M>,Var,AT>::Matrix(const std::vector<std::vector<AT>> &m) : N(!m.empty()? static_cast<int>(m[0].size()):0), ele(new AT[M*N]) {
569  if(m.size() != M)
570  throw std::runtime_error("The input has "+std::to_string(m.size())+" rows but "+std::to_string(M)+" rows are required.");
571  for(int r=0; r<rows(); r++) {
572  if(static_cast<int>(m[r].size())!=cols())
573  throw std::runtime_error("The rows of the input have different length.");
574  for(int c=0; c<cols(); c++)
575  e(r,c)=m[r][c];
576  }
577  }
578 
580 
581  template <int M, class AT> template <class Type, class Row, class Col>
583  for(int i=0; i<M; i++)
584  for(int j=0; j<N; j++)
585  e(i,j) = A.e(i,j);
586  return *this;
587  }
588 
590 
591 }
592 
593 #endif
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:221
Definition: fmatvec.h:38
void resize(int m, int n)
Definition: fixed_var_general_matrix.h:156
AT & operator()(int i, int j)
Element operator.
Definition: fixed_var_general_matrix.h:208
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Matrix()
Standard constructor.
Definition: fixed_var_general_matrix.h:66
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
Definition: types.h:85
Definition: types.h:97
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_var_general_matrix.h:291
const AT & e(int i) const
Element operator.
Definition: fixed_var_general_matrix.h:250
const AT * operator()() const
Pointer operator.
Definition: fixed_var_general_matrix.h:265
AT * operator()()
Pointer operator.
Definition: fixed_var_general_matrix.h:259
constexpr int rows() const
Number of rows.
Definition: fixed_var_general_matrix.h:271
Definition: fmatvec.h:47
int ldim() const
Leading dimension.
Definition: fixed_var_general_matrix.h:283
int cols() const
Number of columns.
This is an index class for creating submatrices.
Definition: range.h:35
const AT & e(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:238
Namespace fmatvec.
Definition: _memory.cc:28
~Matrix()
Destructor.
Definition: fixed_var_general_matrix.h:139
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:99
Definition: types.h:84
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:110
Basic shape class for matrices.
Definition: types.h:132
Matrix< General, Fixed< M >, Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: fixed_var_general_matrix.h:180
int rows() const
Number of rows.
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_var_general_matrix.h:302
constexpr int cols() const
Number of columns.
Definition: fixed_var_general_matrix.h:277
This is an index class for creating submatrices.
Definition: range.h:44
Matrix(const Matrix< General, Fixed< M >, Var, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:89
Matrix< General, Fixed< M >, Var, AT > & operator=(const Matrix< General, Fixed< M >, Var, AT > &A)
Assignment operator.
Definition: fixed_var_general_matrix.h:168
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39
Definition: types.h:86