All Classes Namespaces Functions Variables Typedefs Enumerations Pages
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 
44  typedef AT value_type;
45 
47 
48  protected:
49 
50  int N{0};
51 
52  AT *ele;
53 
54  template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A);
55 
57 
58  public:
59 
64  Matrix() : ele(nullptr) { }
65 
66 // template<class Ini=All<AT> >
67 // Matrix(int n, Ini ini=All<AT>()) : N(n), ele(new AT[M*N]) {
68 // init(ini);
69 // }
70 // template<class Ini=All<AT> >
71 // Matrix(int m, int n, Ini ini=All<AT>()) : N(n), ele(new AT[M*N]) {
72 // init(ini);
73 // }
74 
75  Matrix(int n, Noinit) : N(n), ele(new AT[M*N]) { }
76  Matrix(int n, Init ini=INIT, const AT &a=0) : N(n), ele(new AT[M*N]) { init(a); }
77  Matrix(int n, Eye ini, const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
78  Matrix(int m, int n, Noinit) : N(n), ele(new AT[M*N]) { }
79  Matrix(int m, int n, Init ini=INIT, const AT &a=0) : N(n), ele(new AT[M*N]) { init(a); }
80  Matrix(int m, int n, Eye ini, const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
81 
89  Matrix(const Matrix<General,Fixed<M>,Var,AT> &A) : N(A.N), ele(new AT[M*N]) {
90  deepCopy(A);
91  }
92 
93  template<class Row, class Col>
94  Matrix(const Matrix<General,Row,Col,AT> &A) : N(A.cols()), ele(new AT[M*N]) {
95 
96  deepCopy(A);
97  }
98 
99  template<class Type, class Row, class Col>
100  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : N(A.cols()), ele(new AT[M*N]) {
101 
102 #ifndef FMATVEC_NO_SIZE_CHECK
103  assert(A.rows() == M);
104 #endif
105 
106  deepCopy(A);
107  }
108 
121  Matrix(const std::string &strs);
122  Matrix(const char *strs);
123 
127  delete[] ele;
128  }
129 
130  Matrix<General,Fixed<M>,Var,AT>& resize() {
131  delete[] ele;
132  N = 0;
133  ele = nullptr;
134  return *this;
135  }
136 
137  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Noinit) {
138  delete[] ele;
139  N = n;
140  ele = new AT[M*N];
141  return *this;
142  }
143 
144  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Init ini=INIT, const AT &a=0) { return resize(n,Noinit()).init(a); }
145 
146  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Eye ini, const AT &a=1) { return resize(n,Noinit()).init(ini,a); }
147 
150  void resize(int m, int n) {
151  if(m!=M)
152  throw std::runtime_error("A fixed-var matrix can only be resized in the second dimension.");
153  resize(n);
154  }
155 
162  inline Matrix<General,Fixed<M>,Var,AT>& operator=(const Matrix<General,Fixed<M>,Var,AT> &A);
163 
164  template <class Type, class Row, class Col>
165  inline Matrix<General,Fixed<M>,Var,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
166 
167  template<class Type, class Row, class Col>
168  inline Matrix<General,Fixed<M>,Var,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
169 
181  AT& operator()(int i, int j) {
182 #ifndef FMATVEC_NO_BOUNDS_CHECK
183  assert(i>=0);
184  assert(j>=0);
185  assert(i<M);
186  assert(j<N);
187 #endif
188 
189  return e(i,j);
190  };
191 
196  const AT& operator()(int i, int j) const {
197 #ifndef FMATVEC_NO_BOUNDS_CHECK
198  assert(i>=0);
199  assert(j>=0);
200  assert(i<M);
201  assert(j<N);
202 #endif
203 
204  return e(i,j);
205  };
206 
207  AT& e(int i, int j) {
208  return ele[i*N+j];
209  };
210 
215  const AT& e(int i, int j) const {
216  return ele[i*N+j];
217  };
218 
219  AT& e(int i) {
220  return ele[i];
221  };
222 
227  const AT& e(int i) const {
228  return ele[i];
229  };
230 
236  AT* operator()() {return ele;};
237 
242  const AT* operator()() const {return ele;};
243 
248  int rows() const {return M;};
249 
254  int cols() const {return N;};
255 
260  int ldim() const {return M;};
261 
263  bool transposed() const {
264  return false;
265  };
266 
273  const CBLAS_TRANSPOSE blasTrans() const {
274  return CblasNoTrans;
275  };
276 
284  const CBLAS_ORDER blasOrder() const {
285  return CblasColMajor;
286  };
287 
314  //inline Matrix<GeneralVar, AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J);
315 
320  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
321 
322  template <int M1, int M2>
323  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Var,Var> &J) const;
324 
325  inline const RowVector<Var,AT> row(int i) const;
326  inline const Vector<Fixed<M>,AT> col(int j) const;
327 
335  inline Matrix<General,Fixed<M>,Var,AT>& init(const AT &val=0);
336  inline Matrix<General,Fixed<M>,Var,AT>& init(Init, const AT &a=0) { return init(a); }
337  inline Matrix<General,Fixed<M>,Var,AT>& init(Eye, const AT &val=1);
338  inline Matrix<General,Fixed<M>,Var,AT>& init(Noinit, const AT &a=0) { return *this; }
339 
344  inline operator std::vector<std::vector<AT> >() const;
345 
351  inline Matrix(std::vector<std::vector<AT> > m);
352 
353  inline const Matrix<General,Var,Fixed<M>,AT> T() const;
354 
355  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
356 
357  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
358 
359  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);
360 
361  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
362 
363  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
364 
365  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);
366 
367  };
368 
369  template <int M, class AT>
370  Matrix<General,Fixed<M>,Var,AT>::Matrix(const std::string &strs) : ele(nullptr) {
371  std::istringstream iss(strs);
372  iss>>*this;
373 
374  // check end of stream
375  iss>>std::ws;
376  if(!iss.eof())
377  throw std::runtime_error("Input not fully read.");
378  }
379  template <int M, class AT> Matrix<General,Fixed<M>,Var,AT>::Matrix(const char *strs) :
380  Matrix<General,Fixed<M>,Var,AT>::Matrix(std::string(strs)) {}
381 
382  template <int M, class AT> template< class Type, class Row, class Col>
383  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::operator=(const Matrix<Type,Row,Col,AT> &A) {
384 
385 #ifndef FMATVEC_NO_SIZE_CHECK
386  assert(M == A.rows());
387 #endif
388  if(!ele) {
389  delete[] ele;
390  N = A.cols();
391  ele = new AT[M*N];
392  } else {
393 #ifndef FMATVEC_NO_SIZE_CHECK
394  assert(N == A.cols());
395 #endif
396  }
397 
398  deepCopy(A);
399 
400  return *this;
401  }
402 
403  template <int M, class AT>
405 
406  if(!ele) {
407  delete[] ele;
408  N = A.cols();
409  ele = new AT[M*N];
410  } else {
411 #ifndef FMATVEC_NO_SIZE_CHECK
412  assert(N == A.cols());
413 #endif
414  }
415 
416  deepCopy(A);
417 
418  return *this;
419  }
420 
421  template <int M, class AT> template< class Type, class Row, class Col>
422  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::operator<<(const Matrix<Type,Row,Col,AT> &A) {
423 
424 #ifndef FMATVEC_NO_SIZE_CHECK
425  assert(M == A.rows());
426 #endif
427  if(N!=A.cols()) {
428  delete[] ele;
429  N = A.cols();
430  ele = new AT[M*N];
431  }
432 
433  deepCopy(A);
434 
435  return *this;
436  }
437 
438  template <int M, class AT>
439  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::init(const AT &val) {
440  for(int i=0; i<M*N; i++)
441  e(i) = val;
442  return *this;
443  }
444 
445  template <int M, class AT>
446  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::init(Eye eye, const AT &val) {
447  for(int i=0; i<M; i++)
448  for(int j=0; j<N; j++)
449  e(i,j) = (i==j) ? val : 0;
450  return *this;
451  }
452 
453  template <int M, class AT> template <int M1, int M2>
454  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 {
455 #ifndef FMATVEC_NO_BOUNDS_CHECK
456  assert(M2<M);
457  assert(J.end()<N);
458 #endif
459  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
460 
461  for(int i=0; i<A.rows(); i++)
462  for(int j=0; j<A.cols(); j++)
463  A.e(i,j) = e(M1+i,J.start()+j);
464 
465  return A;
466  }
467 
468  template <int M, class AT>
469  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 {
470 #ifndef FMATVEC_NO_BOUNDS_CHECK
471  assert(I.end()<M);
472  assert(J.end()<N);
473 #endif
474  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
475 
476  for(int i=0; i<A.rows(); i++)
477  for(int j=0; j<A.cols(); j++)
478  A.e(i,j) = e(I.start()+i,J.start()+j);
479 
480  return A;
481  }
482 
483  template <int M, class AT>
484  inline const RowVector<Var,AT> Matrix<General,Fixed<M>,Var,AT>::row(int i) const {
485 
486 #ifndef FMATVEC_NO_BOUNDS_CHECK
487  assert(i>=0);
488  assert(i<M);
489 #endif
490 
491  RowVector<Var,AT> x(N,NONINIT);
492 
493  for(int j=0; j<N; j++)
494  x.e(j) = e(i,j);
495 
496  return x;
497 
498  }
499 
500  template <int M, class AT>
501  inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::col(int j) const {
502 
503 #ifndef FMATVEC_NO_BOUNDS_CHECK
504  assert(j>=0);
505  assert(j<N);
506 #endif
507 
508  Vector<Fixed<M>,AT> x(NONINIT);
509 
510  for(int i=0; i<M; i++)
511  x.e(i) = e(i,j);
512 
513  return x;
514 
515  }
516 
517  template <int M, class AT>
518  inline const Matrix<General,Var,Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::T() const {
519  Matrix<General,Var,Fixed<M>,AT> A(cols(),NONINIT);
520  for(int i=0; i<N; i++)
521  for(int j=0; j<M; j++)
522  A.e(i,j) = e(j,i);
523  return A;
524  }
525 
526  template <int M, class AT> template <class Row>
527  inline void Matrix<General,Fixed<M>,Var,AT>::set(int j, const Vector<Row,AT> &x) {
528 #ifndef FMATVEC_NO_BOUNDS_CHECK
529  assert(j<cols());
530  assert(rows()==x.size());
531 #endif
532  for(int i=0; i<rows(); i++)
533  e(i,j) = x.e(i);
534  }
535 
536  template <int M, class AT> template <class Col>
537  inline void Matrix<General,Fixed<M>,Var,AT>::set(int i, const RowVector<Col,AT> &x) {
538 #ifndef FMATVEC_NO_BOUNDS_CHECK
539  assert(i<rows());
540  assert(cols()==x.size());
541 #endif
542  for(int j=0; j<cols(); j++)
543  e(i,j) = x.e(j);
544  }
545 
546  template <int M, class AT> template<class Type, class Row, class Col>
547  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) {
548 
549 #ifndef FMATVEC_NO_BOUNDS_CHECK
550  assert(I.end()<rows());
551  assert(J.end()<cols());
552  assert(I.size()==A.rows());
553  assert(J.size()==A.cols());
554 #endif
555 
556  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
557  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
558  e(i,j) = A.e(ii,jj);
559  }
560 
561  template <int M, class AT> template <class Row>
562  inline void Matrix<General,Fixed<M>,Var,AT>::add(int j, const Vector<Row,AT> &x) {
563 #ifndef FMATVEC_NO_BOUNDS_CHECK
564  assert(j<cols());
565  assert(rows()==x.size());
566 #endif
567  for(int i=0; i<rows(); i++)
568  e(i,j) += x.e(i);
569  }
570 
571  template <int M, class AT> template <class Col>
572  inline void Matrix<General,Fixed<M>,Var,AT>::add(int i, const RowVector<Col,AT> &x) {
573 #ifndef FMATVEC_NO_BOUNDS_CHECK
574  assert(i<rows());
575  assert(cols()==x.size());
576 #endif
577  for(int j=0; j<cols(); j++)
578  e(i,j) += x.e(j);
579  }
580 
581  template <int M, class AT> template<class Type, class Row, class Col>
582  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) {
583 
584 #ifndef FMATVEC_NO_BOUNDS_CHECK
585  assert(I.end()<rows());
586  assert(J.end()<cols());
587  assert(I.size()==A.rows());
588  assert(J.size()==A.cols());
589 #endif
590 
591  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
592  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
593  e(i,j) += A.e(ii,jj);
594  }
595 
596  template <int M, class AT>
597  inline Matrix<General,Fixed<M>,Var,AT>::operator std::vector<std::vector<AT> >() const{
598  std::vector<std::vector<AT> > ret(rows());
599  for(int r=0; r<rows(); r++) {
600  ret[r].resize(cols());
601  for(int c=0; c<cols(); c++)
602  ret[r][c]=e(r,c);
603  }
604  return ret;
605  }
606 
607  template <int M, class AT>
608  inline Matrix<General,Fixed<M>,Var,AT>::Matrix(std::vector<std::vector<AT> > m) :
609  Matrix<General,Fixed<M>,Var,AT>(!m.empty() ? m[0].size() : 0) {
610  if(m.size() != M)
611  throw std::runtime_error("The input has "+std::to_string(m.size())+" rows but "+std::to_string(M)+" rows are required.");
612  if(static_cast<int>(m[0].size()) != N)
613  throw std::runtime_error("The input has "+std::to_string(m[0].size())+" columns but "+std::to_string(N)+" columns are required.");
614  for(int r=0; r<rows(); r++) {
615  if(static_cast<int>(m[r].size())!=cols())
616  throw std::runtime_error("The rows of the input have different length.");
617  for(int c=0; c<cols(); c++)
618  e(r,c)=m[r][c];
619  }
620  }
621 
623 
624  template <int M, class AT> template <class Type, class Row, class Col>
625  inline void Matrix<General,Fixed<M>,Var,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) {
626  for(int i=0; i<M; i++)
627  for(int j=0; j<N; j++)
628  e(i,j) = A.e(i,j);
629  }
630 
632 
633 }
634 
635 #endif
Definition: fmatvec.h:38
void resize(int m, int n)
Definition: fixed_var_general_matrix.h:150
AT & operator()(int i, int j)
Element operator.
Definition: fixed_var_general_matrix.h:181
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Matrix()
Standard constructor.
Definition: fixed_var_general_matrix.h:64
This is a matrix class for general matrices.
Definition: var_general_matrix.h:39
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:90
Definition: matrix.h:43
int rows() const
Number of rows.
Definition: fixed_var_general_matrix.h:248
Definition: types.h:65
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:196
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_var_general_matrix.h:284
const AT & e(int i) const
Element operator.
Definition: fixed_var_general_matrix.h:227
AT * operator()()
Pointer operator.
Definition: fixed_var_general_matrix.h:236
int rows() const
Number of rows.
const AT & e(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:215
int cols() const
Number of columns.
Definition: fmatvec.h:47
This is an index class for creating submatrices.
Definition: range.h:35
int cols() const
Number of columns.
Definition: fixed_var_general_matrix.h:254
~Matrix()
Destructor.
Definition: fixed_var_general_matrix.h:126
bool transposed() const
The storage format for a fixed-var matrix is fortran-storage order -&gt; transposed is always false...
Definition: fixed_var_general_matrix.h:263
int ldim() const
Leading dimension.
Definition: fixed_var_general_matrix.h:260
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_var_general_matrix.h:273
Definition: matrix.h:42
const AT * operator()() const
Pointer operator.
Definition: fixed_var_general_matrix.h:242
Basic shape class for matrices.
Definition: types.h:100
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
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39
Definition: matrix.h:44

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home