All Classes Namespaces Functions Variables Typedefs Enumerations Pages
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<General,Var,Var,AT> {
40 
41  public:
42  typedef AT value_type;
43 
45 
46  protected:
47 
48  int M{0}, N{0};
49 
50  AT *ele;
51 
52  template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A);
53 
55 
56  public:
57 
62  Matrix() : ele(nullptr) { }
63 
64 // Works with -std=gnu++0x only
65 // template<class Ini=All<AT> >
66 // Matrix(int m, int n, Ini ini=All<AT>()) : M(m), N(n), ele(new AT[M*N]) {
67 // init(ini);
68 // }
69 
70  Matrix(int m, int n, Noinit) : M(m), N(n), ele(new AT[M*N]) { }
71  Matrix(int m, int n, Init ini=INIT, const AT &a=0) : M(m), N(n), ele(new AT[M*N]) { init(a); }
72  Matrix(int m, int n, Eye ini, const AT &a=1) : M(m), N(n), ele(new AT[M*N]) { init(ini,a); }
73 
81  Matrix(const Matrix<General,Var,Var,AT> &A) : M(A.M), N(A.N), ele(new AT[M*N]) {
82  deepCopy(A);
83  }
84 
85  template<class Row, class Col>
86  Matrix(const Matrix<General,Row,Col,AT> &A) : M(A.rows()), N(A.cols()), ele(new AT[M*N]) {
87 
88  deepCopy(A);
89  }
90 
91  template<class Type, class Row, class Col>
92  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), N(A.cols()), ele(new AT[M*N]) {
93 
94  deepCopy(A);
95  }
96 
109  Matrix(const std::string &strs);
110  Matrix(const char *strs);
111 
115  delete[] ele;
116  }
117 
118  Matrix<General,Var,Var,AT>& resize() {
119  delete[] ele;
120  M = N = 0;
121  ele = nullptr;
122  return *this;
123  }
124 
125  Matrix<General,Var,Var,AT>& resize(int m, int n, Noinit) {
126  delete[] ele;
127  M = m;
128  N = n;
129  ele = new AT[M*N];
130  return *this;
131  }
132 
133  Matrix<General,Var,Var,AT>& resize(int m, int n, Init ini=INIT, const AT &a=0) { return resize(m,n,Noinit()).init(a); }
134 
135  Matrix<General,Var,Var,AT>& resize(int m, int n, Eye ini, const AT &a=1) { return resize(m,n,Noinit()).init(ini,a); }
136 
143  inline Matrix<General,Var,Var,AT>& operator=(const Matrix<General,Var,Var,AT> &A);
144 
145  template <class Type, class Row, class Col>
146  inline Matrix<General,Var,Var,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
147 
154  template<class Type, class Row, class Col>
155  inline Matrix<General,Var,Var,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
156 
168  AT& operator()(int i, int j) {
169 #ifndef FMATVEC_NO_BOUNDS_CHECK
170  assert(i>=0);
171  assert(j>=0);
172  assert(i<M);
173  assert(j<N);
174 #endif
175 
176  return e(i,j);
177  };
178 
183  const AT& operator()(int i, int j) const {
184 #ifndef FMATVEC_NO_BOUNDS_CHECK
185  assert(i>=0);
186  assert(j>=0);
187  assert(i<M);
188  assert(j<N);
189 #endif
190 
191  return e(i,j);
192  };
193 
194  AT& e(int i, int j) {
195  return ele[i*N+j];
196  };
197 
202  const AT& e(int i, int j) const {
203  return ele[i*N+j];
204  };
205 
206  AT& e(int i) {
207  return ele[i];
208  };
209 
214  const AT& e(int i) const {
215  return ele[i];
216  };
217 
223  AT* operator()() {return ele;};
224 
229  const AT* operator()() const {return ele;};
230 
235  int rows() const {return M;};
236 
241  int cols() const {return N;};
242 
247  int ldim() const {return M;};
248 
250  bool transposed() const {
251  return false;
252  };
253 
260  const CBLAS_TRANSPOSE blasTrans() const {
261  return CblasNoTrans;
262  };
263 
271  const CBLAS_ORDER blasOrder() const {
272  return CblasColMajor;
273  };
274 
301  //inline Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J);
302 
307  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
308 
309  template <int M1, int M2, int N1, int N2>
310  inline const Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Fixed<N1>,Fixed<N2> > &J) const;
311 
312  template <int M1, int M2>
313  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Var,Var > &J) const;
314 
315  template <int N1, int N2>
316  inline const Matrix<General,Var,Fixed<N2-N1+1>,AT> operator()(const Range<Var,Var> &I, const Range<Fixed<N1>,Fixed<N2> > &J) const;
317 
318  inline const RowVector<Var,AT> row(int i) const;
319  inline const Vector<Var,AT> col(int j) const;
320 
328  inline Matrix<General,Var,Var,AT>& init(const AT &val=0);
329  inline Matrix<General,Var,Var,AT>& init(Init all, const AT &a=0) { return init(a); }
330  inline Matrix<General,Var,Var,AT>& init(Eye eye, const AT &val=1);
331  inline Matrix<General,Var,Var,AT>& init(Noinit, const AT &a=0) { return *this; }
332 
337  inline operator std::vector<std::vector<AT> >() const;
338 
344  inline Matrix(std::vector<std::vector<AT> > m);
345 
346  inline const Matrix<General,Var,Var,AT> T() const;
347 
348  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
349 
350  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
351 
352  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);
353 
354  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
355 
356  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
357 
358  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);
359 
360  };
361 
362  template <class AT>
363  Matrix<General,Var,Var,AT>::Matrix(const std::string &strs) : ele(nullptr) {
364  std::istringstream iss(strs);
365  iss>>*this;
366 
367  // check end of stream
368  iss>>std::ws;
369  if(!iss.eof())
370  throw std::runtime_error("Input not fully read.");
371  }
372  template <class AT> Matrix<General,Var,Var,AT>::Matrix(const char *strs) : Matrix<General,Var,Var,AT>::Matrix(std::string(strs)) {}
373 
374  template <class AT> template< class Type, class Row, class Col>
375  inline Matrix<General,Var,Var,AT>& Matrix<General,Var,Var,AT>::operator=(const Matrix<Type,Row,Col,AT> &A) {
376 
377  if(!ele) {
378  delete[] ele;
379  M = A.rows();
380  N = A.cols();
381  ele = new AT[M*N];
382  } else {
383 #ifndef FMATVEC_NO_SIZE_CHECK
384  assert(M == A.rows());
385  assert(N == A.cols());
386 #endif
387  }
388 
389  deepCopy(A);
390 
391  return *this;
392  }
393 
394  template <class AT>
396 
397  if(!ele) {
398  delete[] ele;
399  M = A.rows();
400  N = A.cols();
401  ele = new AT[M*N];
402  } else {
403 #ifndef FMATVEC_NO_SIZE_CHECK
404  assert(M == A.rows());
405  assert(N == A.cols());
406 #endif
407  }
408 
409  deepCopy(A);
410 
411  return *this;
412  }
413 
414  template <class AT>
416  for(int i=0; i<M*N; i++)
417  e(i) = val;
418  return *this;
419  }
420 
421  template <class AT>
423  for(int i=0; i<M; i++)
424  for(int j=0; j<N; j++)
425  e(i,j) = (i==j) ? val : 0;
426  return *this;
427  }
428 
429  template <class AT> template< class Type, class Row, class Col>
431 
432  if(M!=A.rows() || N!=A.cols()) {
433  delete[] ele;
434  M = A.rows();
435  N = A.cols();
436  ele = new AT[M*N];
437  }
438 
439  deepCopy(A);
440 
441  return *this;
442  }
443 
444  template <class AT>
446 #ifndef FMATVEC_NO_BOUNDS_CHECK
447  assert(I.end()<M);
448  assert(J.end()<N);
449 #endif
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 <class AT> template <int M1, int M2, int N1, int N2>
460  inline const Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> Matrix<General,Var,Var,AT>::operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Fixed<N1>,Fixed<N2> > &J) const {
461 #ifndef FMATVEC_NO_BOUNDS_CHECK
462  assert(M2<M);
463  assert(N2<N);
464 #endif
465  Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> A(NONINIT);
466 
467  for(int i=0; i<A.rows(); i++)
468  for(int j=0; j<A.cols(); j++)
469  A.e(i,j) = e(I.start()+i,J.start()+j);
470 
471  return A;
472  }
473 
474  template <class AT> template <int M1, int M2>
475  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> Matrix<General,Var,Var,AT>::operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Var,Var> &J) const {
476 #ifndef FMATVEC_NO_BOUNDS_CHECK
477  assert(M2<M);
478  assert(J.end()<N);
479 #endif
480  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
481 
482  for(int i=0; i<A.rows(); i++)
483  for(int j=0; j<A.cols(); j++)
484  A.e(i,j) = e(I.start()+i,J.start()+j);
485 
486  return A;
487  }
488 
489  template <class AT> template <int N1, int N2>
490  inline const Matrix<General,Var,Fixed<N2-N1+1>,AT> Matrix<General,Var,Var,AT>::operator()(const Range<Var,Var> &I, const Range<Fixed<N1>,Fixed<N2> > &J) const {
491 #ifndef FMATVEC_NO_BOUNDS_CHECK
492  assert(I.end()<M);
493  assert(N2<N);
494 #endif
495  Matrix<General,Var,Fixed<N2-N1+1>,AT> A(I.end()-I.start()+1,NONINIT);
496 
497  for(int i=0; i<A.rows(); i++)
498  for(int j=0; j<A.cols(); j++)
499  A.e(i,j) = e(I.start()+i,J.start()+j);
500 
501  return A;
502  }
503 
504  template <class AT>
505  inline const RowVector<Var,AT> Matrix<General,Var,Var,AT>::row(int i) const {
506 
507 #ifndef FMATVEC_NO_BOUNDS_CHECK
508  assert(i>=0);
509  assert(i<M);
510 #endif
511 
512  RowVector<Var,AT> x(N,NONINIT);
513 
514  for(int j=0; j<N; j++)
515  x.e(j) = e(i,j);
516 
517  return x;
518 
519  }
520 
521  template <class AT>
522  inline const Vector<Var,AT> Matrix<General,Var,Var,AT>::col(int j) const {
523 
524 #ifndef FMATVEC_NO_BOUNDS_CHECK
525  assert(j>=0);
526  assert(j<N);
527 #endif
528 
529  Vector<Var,AT> x(M,NONINIT);
530 
531  for(int i=0; i<M; i++)
532  x.e(i) = e(i,j);
533 
534  return x;
535 
536  }
537 
538  template <class AT>
539  inline const Matrix<General,Var,Var,AT> Matrix<General,Var,Var,AT>::T() const {
540  Matrix<General,Var,Var,AT> A(N,M,NONINIT);
541  for(int i=0; i<N; i++)
542  for(int j=0; j<M; j++)
543  A.e(i,j) = e(j,i);
544  return A;
545  }
546 
547  template <class AT> template <class Row>
548  inline void Matrix<General,Var,Var,AT>::set(int j, const Vector<Row,AT> &x) {
549 #ifndef FMATVEC_NO_BOUNDS_CHECK
550  assert(j<cols());
551  assert(rows()==x.size());
552 #endif
553  for(int i=0; i<rows(); i++)
554  e(i,j) = x.e(i);
555  }
556 
557  template <class AT> template <class Col>
558  inline void Matrix<General,Var,Var,AT>::set(int i, const RowVector<Col,AT> &x) {
559 #ifndef FMATVEC_NO_BOUNDS_CHECK
560  assert(i<rows());
561  assert(cols()==x.size());
562 #endif
563  for(int j=0; j<cols(); j++)
564  e(i,j) = x.e(j);
565  }
566 
567  template <class AT> template<class Type, class Row, class Col>
568  inline void Matrix<General,Var,Var,AT>::set(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
569 
570 #ifndef FMATVEC_NO_BOUNDS_CHECK
571  assert(I.end()<rows());
572  assert(J.end()<cols());
573  assert(I.size()==A.rows());
574  assert(J.size()==A.cols());
575 #endif
576 
577  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
578  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
579  e(i,j) = A.e(ii,jj);
580  }
581 
582  template <class AT> template <class Row>
583  inline void Matrix<General,Var,Var,AT>::add(int j, const Vector<Row,AT> &x) {
584 #ifndef FMATVEC_NO_BOUNDS_CHECK
585  assert(j<cols());
586  assert(rows()==x.size());
587 #endif
588  for(int i=0; i<rows(); i++)
589  e(i,j) += x.e(i);
590  }
591 
592  template <class AT> template <class Col>
593  inline void Matrix<General,Var,Var,AT>::add(int i, const RowVector<Col,AT> &x) {
594 #ifndef FMATVEC_NO_BOUNDS_CHECK
595  assert(i<rows());
596  assert(cols()==x.size());
597 #endif
598  for(int j=0; j<cols(); j++)
599  e(i,j) += x.e(j);
600  }
601 
602  template <class AT> template<class Type, class Row, class Col>
603  inline void Matrix<General,Var,Var,AT>::add(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
604 
605 #ifndef FMATVEC_NO_BOUNDS_CHECK
606  assert(I.end()<rows());
607  assert(J.end()<cols());
608  assert(I.size()==A.rows());
609  assert(J.size()==A.cols());
610 #endif
611 
612  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
613  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
614  e(i,j) += A.e(ii,jj);
615  }
616 
617  template <class AT>
618  inline Matrix<General,Var,Var,AT>::operator std::vector<std::vector<AT> >() const {
619  std::vector<std::vector<AT> > ret(rows());
620  for(int r=0; r<rows(); r++) {
621  ret[r].resize(cols());
622  for(int c=0; c<cols(); c++)
623  ret[r][c]=e(r,c);
624  }
625  return ret;
626  }
627 
628  template <class AT>
629  inline Matrix<General,Var,Var,AT>::Matrix(std::vector<std::vector<AT> > m) : M(m.size()), N(m[0].size()), ele(new AT[M*N]) {
630  for(int r=0; r<rows(); r++) {
631  if(static_cast<int>(m[r].size())!=cols())
632  throw std::runtime_error("The rows of the input have different length.");
633  for(int c=0; c<cols(); c++)
634  e(r,c)=m[r][c];
635  }
636  }
637 
639 
640  template <class AT> template <class Type, class Row, class Col>
642  for(int i=0; i<M; i++)
643  for(int j=0; j<N; j++)
644  e(i,j) = A.e(i,j);
645  }
646 
648 
649 }
650 
651 #endif
Definition: fmatvec.h:38
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
~Matrix()
Destructor.
Definition: var_general_matrix.h:114
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_general_matrix.h:271
This is a vector class of general shape in dense storage format.
Definition: var_vector.h:39
This is a matrix class for general matrices.
Definition: var_general_matrix.h:39
AT & operator()(int i, int j)
Element operator.
Definition: var_general_matrix.h:168
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:90
Definition: matrix.h:43
Definition: types.h:65
const AT & operator()(int i, int j) const
Element operator.
Definition: var_general_matrix.h:183
bool transposed() const
The storage format of a var matrix is fortran-storage order -&gt; return always false.
Definition: var_general_matrix.h:250
int rows() const
Number of rows.
Definition: var_general_matrix.h:235
const AT * operator()() const
Pointer operator.
Definition: var_general_matrix.h:229
int rows() const
Number of rows.
int cols() const
Number of columns.
Matrix< General, Var, Var, AT > & init(const AT &val=0)
Initialization.
Definition: var_general_matrix.h:415
Matrix()
Standard constructor.
Definition: var_general_matrix.h:62
int cols() const
Number of columns.
Definition: var_general_matrix.h:241
Matrix< General, Var, Var, AT > & operator=(const Matrix< General, Var, Var, AT > &A)
Assignment operator.
Definition: var_general_matrix.h:395
Matrix(const Matrix< General, Var, Var, AT > &A)
Copy Constructor.
Definition: var_general_matrix.h:81
const AT & e(int i, int j) const
Element operator.
Definition: var_general_matrix.h:202
This is an index class for creating submatrices.
Definition: range.h:35
int ldim() const
Leading dimension.
Definition: var_general_matrix.h:247
AT * operator()()
Pointer operator.
Definition: var_general_matrix.h:223
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_general_matrix.h:260
Definition: matrix.h:42
Basic shape class for matrices.
Definition: types.h:100
int end() const
Last element.
Definition: range.h:95
This is an index class for creating submatrices.
Definition: range.h:44
const AT & e(int i) const
Element operator.
Definition: var_general_matrix.h:214
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39
int start() const
First element.
Definition: range.h:89
Definition: matrix.h:44

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home