fmatvec  0.0.0
Fast Matrix/Vector Library
var_general_matrix.h
1 /* Copyright (C) 2003-2012 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_general_matrix_h
23 #define var_general_matrix_h
24 
25 #include "types.h"
26 #include <cstdlib>
27 #include <stdexcept>
28 
29 namespace fmatvec {
30 
39  template <class AT> class Matrix<fmatvec::General,fmatvec::Var,fmatvec::Var,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  int N{0};
52 
53  AT *ele;
54 
55  template <class Type, class Row, class Col> inline Matrix<General,Var,Var,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
56 
58 
59  public:
60 
65  explicit Matrix() : ele(nullptr) { }
66 
67 // Works with -std=gnu++0x only
68 // template<class Ini=All<AT>>
69 // Matrix(int m, int n, Ini ini=All<AT>()) : M(m), N(n), ele(new AT[M*N]) {
70 // init(ini);
71 // }
72 
73  explicit Matrix(int m, int n, Noinit) : M(m), N(n), ele(new AT[M*N]) { }
74  explicit Matrix(int m, int n, Init ini=INIT, const AT &a=AT()) : M(m), N(n), ele(new AT[M*N]) { init(a); }
75  explicit Matrix(int m, int n, Eye ini, const AT &a=1) : M(m), N(n), ele(new AT[M*N]) { init(ini,a); }
76 
82  Matrix(const Matrix<General,Var,Var,AT> &A) : M(A.M), N(A.N), ele(new AT[M*N]) {
83  copy(A);
84  }
85 
91  template<class Row, class Col>
92  Matrix(const Matrix<General,Row,Col,AT> &A) : M(A.rows()), N(A.cols()), ele(new AT[M*N]) {
93  copy(A);
94  }
95 
101  template<class Type, class Row, class Col>
102  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), N(A.cols()), ele(new AT[M*N]) {
103  copy(A);
104  }
105 
118  Matrix(const std::string &strs);
119  Matrix(const char *strs);
120 
124  delete[] ele;
125  }
126 
127  typedef AT* iterator;
128  typedef const AT* const_iterator;
129  iterator begin() { return &ele[0]; }
130  iterator end() { return &ele[M*N]; }
131  const_iterator begin() const { return &ele[0]; }
132  const_iterator end() const { return &ele[M*N]; }
133 
134  Matrix<General,Var,Var,AT>& resize(int m, int n, Noinit) {
135  delete[] ele;
136  M = m;
137  N = n;
138  ele = new AT[M*N];
139  return *this;
140  }
141 
142  Matrix<General,Var,Var,AT>& resize(int m, int n, Init ini=INIT, const AT &a=AT()) { return resize(m,n,Noinit()).init(a); }
143 
144  Matrix<General,Var,Var,AT>& resize(int m, int n, Eye ini, const AT &a=1) { return resize(m,n,Noinit()).init(ini,a); }
145 
153  FMATVEC_ASSERT(M == A.rows(), AT);
154  FMATVEC_ASSERT(N == A.cols(), AT);
155  return copy(A);
156  }
157 
164  template <class Type, class Row, class Col>
166  FMATVEC_ASSERT(M == A.rows(), AT);
167  FMATVEC_ASSERT(N == A.cols(), AT);
168  return copy(A);
169  }
170 
177  template<class Type, class Row, class Col>
178  inline Matrix<General,Var,Var,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
179  if(M!=A.rows() || N!=A.cols()) resize(A.rows(),A.cols(),NONINIT);
180  return copy(A);
181  }
182 
183  template <class AT2>
184  operator Matrix<General,Var,Var,AT2>() const {
186  for(size_t r=0; r<rows(); ++r)
187  for(size_t c=0; c<cols(); ++c)
188  ret(r,c) = (*this)(r,c);
189  return ret;
190  }
191 
201  AT& operator()(int i, int j) {
202  FMATVEC_ASSERT(i>=0, AT);
203  FMATVEC_ASSERT(j>=0, AT);
204  FMATVEC_ASSERT(i<M, AT);
205  FMATVEC_ASSERT(j<N, AT);
206 
207  return e(i,j);
208  }
209 
214  const AT& operator()(int i, int j) const {
215  FMATVEC_ASSERT(i>=0, AT);
216  FMATVEC_ASSERT(j>=0, AT);
217  FMATVEC_ASSERT(i<M, AT);
218  FMATVEC_ASSERT(j<N, AT);
219 
220  return e(i,j);
221  }
222 
223  AT& e(int i, int j) {
224  return ele[i*N+j];
225  }
226 
231  const AT& e(int i, int j) const {
232  return ele[i*N+j];
233  }
234 
235  AT& e(int i) {
236  return ele[i];
237  }
238 
243  const AT& e(int i) const {
244  return ele[i];
245  }
246 
252  AT* operator()() {return ele;}
253 
258  const AT* operator()() const {return ele;}
259 
264  constexpr int rows() const {return M;}
265 
270  constexpr int cols() const {return N;}
271 
276  int ldim() const {return N;}
277 
284  const CBLAS_TRANSPOSE blasTrans() const {
285  return CblasNoTrans;
286  }
287 
295  const CBLAS_ORDER blasOrder() const {
296  return CblasRowMajor;
297  }
298 
304 
305  template <int M1, int M2, int N1, int N2>
306  inline const Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> operator()(const fmatvec::Range<Fixed<M1>,Fixed<M2>> &I, const fmatvec::Range<Fixed<N1>,Fixed<N2>> &J) const;
307 
308  template <int M1, int M2>
309  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> operator()(const fmatvec::Range<Fixed<M1>,Fixed<M2>> &I, const fmatvec::Range<Var,Var > &J) const;
310 
311  template <int N1, int N2>
312  inline const Matrix<General,Var,Fixed<N2-N1+1>,AT> operator()(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Fixed<N1>,Fixed<N2>> &J) const;
313 
314  inline const RowVector<Var,AT> row(int i) const;
315  inline const Vector<Var,AT> col(int j) const;
316 
324  inline Matrix<General,Var,Var,AT>& init(const AT &val=AT());
325  inline Matrix<General,Var,Var,AT>& init(Init all, const AT &a=AT()) { return init(a); }
326  inline Matrix<General,Var,Var,AT>& init(Eye eye, const AT &val=1);
327  inline Matrix<General,Var,Var,AT>& init(Noinit, const AT &a=AT()) { return *this; }
328 
333  explicit inline operator std::vector<std::vector<AT>>() const;
334 
340  explicit inline Matrix(const std::vector<std::vector<AT>> &m);
341 
342 // /*! \brief Cast to AT.
343 // *
344 // * \return The AT representation of the matrix
345 // * */
346 // explicit operator AT() const {
347 // FMATVEC_ASSERT(M==1, AT);
348 // FMATVEC_ASSERT(N==1, AT);
349 // return ele[0];
350 // }
351 //
352 // /*! \brief AT Constructor.
353 // * Constructs and initializes a matrix with a AT object.
354 // * \param x The AT the matrix will be initialized with.
355 // * */
356 // explicit Matrix(const AT &x) : M(1), N(1), ele(new AT[1]) { ele[0] = x; }
357 
358  inline const Matrix<General,Var,Var,AT> T() const;
359 
360  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
361 
362  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
363 
364  template<class Type, class Row, class Col> inline void set(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A);
365 
366  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
367 
368  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
369 
370  template<class Type, class Row, class Col> inline void add(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A);
371  };
372 
373  template <class AT>
374  Matrix<General,Var,Var,AT>::Matrix(const std::string &strs) : ele(nullptr) {
375  std::istringstream iss(strs);
376  iss>>*this;
377 
378  // check end of stream
379  iss>>std::ws;
380  if(!iss.eof())
381  throw std::runtime_error("Input not fully read.");
382  }
383  template <class AT> Matrix<General,Var,Var,AT>::Matrix(const char *strs) : Matrix<General,Var,Var,AT>::Matrix(std::string(strs)) {}
384 
385  template <class AT>
386  inline Matrix<General,Var,Var,AT>& Matrix<General,Var,Var,AT>::init(const AT &val) {
387  for(int i=0; i<M*N; i++)
388  e(i) = val;
389  return *this;
390  }
391 
392  template <class AT>
393  inline Matrix<General,Var,Var,AT>& Matrix<General,Var,Var,AT>::init(Eye eye, const AT &val) {
394  for(int i=0; i<M; i++)
395  for(int j=0; j<N; j++)
396  e(i,j) = (i==j) ? val : 0;
397  return *this;
398  }
399 
400  template <class AT>
401  inline const Matrix<General,Var,Var,AT> Matrix<General,Var,Var,AT>::operator()(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J) const {
402  FMATVEC_ASSERT(I.end()<M, AT);
403  FMATVEC_ASSERT(J.end()<N, AT);
404  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
405 
406  for(int i=0; i<A.rows(); i++)
407  for(int j=0; j<A.cols(); j++)
408  A.e(i,j) = e(I.start()+i,J.start()+j);
409 
410  return A;
411  }
412 
413  template <class AT> template <int M1, int M2, int N1, int N2>
414  inline const Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> Matrix<General,Var,Var,AT>::operator()(const fmatvec::Range<Fixed<M1>,Fixed<M2>> &I, const fmatvec::Range<Fixed<N1>,Fixed<N2>> &J) const {
415  FMATVEC_ASSERT(M2<M, AT);
416  FMATVEC_ASSERT(N2<N, AT);
417  Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> A(NONINIT);
418 
419  for(int i=0; i<A.rows(); i++)
420  for(int j=0; j<A.cols(); j++)
421  A.e(i,j) = e(I.start()+i,J.start()+j);
422 
423  return A;
424  }
425 
426  template <class AT> template <int M1, int M2>
427  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> Matrix<General,Var,Var,AT>::operator()(const fmatvec::Range<Fixed<M1>,Fixed<M2>> &I, const fmatvec::Range<Var,Var> &J) const {
428  FMATVEC_ASSERT(M2<M, AT);
429  FMATVEC_ASSERT(J.end()<N, AT);
430  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
431 
432  for(int i=0; i<A.rows(); i++)
433  for(int j=0; j<A.cols(); j++)
434  A.e(i,j) = e(I.start()+i,J.start()+j);
435 
436  return A;
437  }
438 
439  template <class AT> template <int N1, int N2>
440  inline const Matrix<General,Var,Fixed<N2-N1+1>,AT> Matrix<General,Var,Var,AT>::operator()(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Fixed<N1>,Fixed<N2>> &J) const {
441  FMATVEC_ASSERT(I.end()<M, AT);
442  FMATVEC_ASSERT(N2<N, AT);
443  Matrix<General,Var,Fixed<N2-N1+1>,AT> A(I.end()-I.start()+1,NONINIT);
444 
445  for(int i=0; i<A.rows(); i++)
446  for(int j=0; j<A.cols(); j++)
447  A.e(i,j) = e(I.start()+i,J.start()+j);
448 
449  return A;
450  }
451 
452  template <class AT>
453  inline const RowVector<Var,AT> Matrix<General,Var,Var,AT>::row(int i) const {
454 
455  FMATVEC_ASSERT(i>=0, AT);
456  FMATVEC_ASSERT(i<M, AT);
457 
458  RowVector<Var,AT> x(N,NONINIT);
459 
460  for(int j=0; j<N; j++)
461  x.e(j) = e(i,j);
462 
463  return x;
464 
465  }
466 
467  template <class AT>
468  inline const Vector<Var,AT> Matrix<General,Var,Var,AT>::col(int j) const {
469 
470  FMATVEC_ASSERT(j>=0, AT);
471  FMATVEC_ASSERT(j<N, AT);
472 
473  Vector<Var,AT> x(M,NONINIT);
474 
475  for(int i=0; i<M; i++)
476  x.e(i) = e(i,j);
477 
478  return x;
479 
480  }
481 
482  template <class AT>
483  inline const Matrix<General,Var,Var,AT> Matrix<General,Var,Var,AT>::T() const {
484  Matrix<General,Var,Var,AT> A(N,M,NONINIT);
485  for(int i=0; i<N; i++)
486  for(int j=0; j<M; j++)
487  A.e(i,j) = e(j,i);
488  return A;
489  }
490 
491  template <class AT> template <class Row>
492  inline void Matrix<General,Var,Var,AT>::set(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 <class AT> template <class Col>
500  inline void Matrix<General,Var,Var,AT>::set(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 <class AT> template<class Type, class Row, class Col>
508  inline void Matrix<General,Var,Var,AT>::set(const fmatvec::Range<Var,Var> &I, const fmatvec::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 <class AT> template <class Row>
521  inline void Matrix<General,Var,Var,AT>::add(int j, const Vector<Row,AT> &x) {
522  FMATVEC_ASSERT(j<cols(), AT);
523  FMATVEC_ASSERT(rows()==x.size(), AT);
524  for(int i=0; i<rows(); i++)
525  e(i,j) += x.e(i);
526  }
527 
528  template <class AT> template <class Col>
529  inline void Matrix<General,Var,Var,AT>::add(int i, const RowVector<Col,AT> &x) {
530  FMATVEC_ASSERT(i<rows(), AT);
531  FMATVEC_ASSERT(cols()==x.size(), AT);
532  for(int j=0; j<cols(); j++)
533  e(i,j) += x.e(j);
534  }
535 
536  template <class AT> template<class Type, class Row, class Col>
537  inline void Matrix<General,Var,Var,AT>::add(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
538 
539  FMATVEC_ASSERT(I.end()<rows(), AT);
540  FMATVEC_ASSERT(J.end()<cols(), AT);
541  FMATVEC_ASSERT(I.size()==A.rows(), AT);
542  FMATVEC_ASSERT(J.size()==A.cols(), AT);
543 
544  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
545  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
546  e(i,j) += A.e(ii,jj);
547  }
548 
549  template <class AT>
550  inline Matrix<General,Var,Var,AT>::operator std::vector<std::vector<AT>>() const {
551  std::vector<std::vector<AT>> ret(rows());
552  for(int r=0; r<rows(); r++) {
553  ret[r].resize(cols());
554  for(int c=0; c<cols(); c++)
555  ret[r][c]=e(r,c);
556  }
557  return ret;
558  }
559 
560  template <class AT>
561  inline Matrix<General,Var,Var,AT>::Matrix(const std::vector<std::vector<AT>> &m) : M(static_cast<int>(m.size())), N(static_cast<int>(m[0].size())), ele(new AT[M*N]) {
562  for(int r=0; r<rows(); r++) {
563  if(static_cast<int>(m[r].size())!=cols())
564  throw std::runtime_error("The rows of the input have different length.");
565  for(int c=0; c<cols(); c++)
566  e(r,c)=m[r][c];
567  }
568  }
569 
571 
572  template <class AT> template <class Type, class Row, class Col>
573  inline Matrix<General,Var,Var,AT>& Matrix<General,Var,Var,AT>::copy(const Matrix<Type,Row,Col,AT> &A) {
574  for(int i=0; i<M; i++)
575  for(int j=0; j<N; j++)
576  e(i,j) = A.e(i,j);
577  return *this;
578  }
579 
581 
582 }
583 
584 #endif
int end() const
Last element.
Definition: range.h:91
Definition: fmatvec.h:38
constexpr int cols() const
Number of columns.
Definition: var_general_matrix.h:270
Matrix< General, Var, Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_general_matrix.h:165
constexpr int rows() const
Number of rows.
Definition: var_general_matrix.h:264
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
Definition: types.h:85
Definition: types.h:97
const AT & operator()(int i, int j) const
Element operator.
Definition: var_general_matrix.h:214
int ldim() const
Leading dimension.
Definition: var_general_matrix.h:276
Matrix< General, Var, Var, AT > & operator=(const Matrix< General, Var, Var, AT > &A)
Assignment operator.
Definition: var_general_matrix.h:152
Matrix(const Matrix< General, Var, Var, AT > &A)
Copy Constructor.
Definition: var_general_matrix.h:82
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_general_matrix.h:284
int start() const
First element.
Definition: range.h:85
Definition: fmatvec.h:47
AT * operator()()
Pointer operator.
Definition: var_general_matrix.h:252
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: var_general_matrix.h:92
~Matrix()
Destructor.
Definition: var_general_matrix.h:123
const AT & e(int i) const
Element operator.
Definition: var_general_matrix.h:243
int cols() const
Number of columns.
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_general_matrix.h:102
This is an index class for creating submatrices.
Definition: range.h:35
Namespace fmatvec.
Definition: _memory.cc:28
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_general_matrix.h:295
int size() const
Size.
Definition: range.h:97
Matrix()
Standard constructor.
Definition: var_general_matrix.h:65
const AT & e(int i, int j) const
Element operator.
Definition: var_general_matrix.h:231
Definition: types.h:84
Basic shape class for matrices.
Definition: types.h:132
AT & operator()(int i, int j)
Element operator.
Definition: var_general_matrix.h:201
const AT * operator()() const
Pointer operator.
Definition: var_general_matrix.h:258
int rows() const
Number of rows.
This is an index class for creating submatrices.
Definition: range.h:44
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39