All Classes Namespaces Functions Variables Typedefs Enumerations Pages
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<General,Var,Fixed<N>,AT> {
40 
41  public:
42  typedef AT value_type;
43 
45 
46  protected:
47 
48  int M{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  //inline void deepCopy(const Matrix<General,Var,Fixed<N>,AT> &A);
54 
56 
57  public:
58 
59  Matrix() : ele(nullptr) { }
60 
61 // template<class Ini=All<AT> >
62 // Matrix(int m, Ini ini=All<AT>()) : M(m), ele(new AT[M*N]) {
63 // init(ini);
64 // }
65 // template<class Ini=All<AT> >
66 // Matrix(int m, int n, Ini ini=All<AT>()) : M(m), ele(new AT[M*N]) {
67 // init(ini);
68 // }
69 
70  Matrix(int m, Noinit) : M(m), ele(new AT[M*N]) { }
71  Matrix(int m, Init ini=INIT, const AT &a=0) : M(m), ele(new AT[M*N]) { init(a); }
72  Matrix(int m, Eye ini, const AT &a=1) : M(m), ele(new AT[M*N]) { init(ini,a); }
73  Matrix(int m, int n, Noinit) : M(m), ele(new AT[M*N]) { }
74  Matrix(int m, int n, Init ini=INIT, const AT &a=0) : M(m), ele(new AT[M*N]) { init(a); }
75  Matrix(int m, int n, Eye ini, const AT &a=1) : M(m), ele(new AT[M*N]) { init(ini,a); }
76 
84  Matrix(const Matrix<General,Var,Fixed<N>,AT> &A) : M(A.M), ele(new AT[M*N]) {
85  deepCopy(A);
86  }
87 
88  template<class Row, class Col>
89  Matrix(const Matrix<General,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*N]) {
90 
91  deepCopy(A);
92  }
93 
94  template<class Type, class Row, class Col>
95  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*N]) {
96 
97 #ifndef FMATVEC_NO_SIZE_CHECK
98  assert(A.cols() == N);
99 #endif
100 
101  deepCopy(A);
102  }
103 
116  Matrix(const std::string &strs);
117  Matrix(const char *strs);
118 
122  delete[] ele;
123  }
124 
125  Matrix<General,Var,Fixed<N>,AT>& resize() {
126  delete[] ele;
127  M = 0;
128  ele = 0;
129  return *this;
130  }
131 
132  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Noinit) {
133  delete[] ele;
134  M = m;
135  ele = new AT[M*N];
136  return *this;
137  }
138 
139  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Init ini=INIT, const AT &a=0) { return resize(m,Noinit()).init(a); }
140 
141  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Eye ini, const AT &a=1) { return resize(m,Noinit()).init(ini,a); }
142 
145  void resize(int m, int n) {
146  if(n!=N)
147  throw std::runtime_error("A var-fixed matrix can only be resized in the first dimension.");
148  resize(m);
149  }
150 
157  inline Matrix<General,Var,Fixed<N>,AT>& operator=(const Matrix<General,Var,Fixed<N>,AT> &A);
158 
159  template <class Type, class Row, class Col>
160  inline Matrix<General,Var,Fixed<N>,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
161 
162  template <class Type, class Row, class Col>
163  inline Matrix<General,Var,Fixed<N>,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
164 
176  AT& operator()(int i, int j) {
177 #ifndef FMATVEC_NO_BOUNDS_CHECK
178  assert(i>=0);
179  assert(j>=0);
180  assert(i<M);
181  assert(j<N);
182 #endif
183 
184  return e(i,j);
185  };
186 
191  const AT& operator()(int i, int j) const {
192 #ifndef FMATVEC_NO_BOUNDS_CHECK
193  assert(i>=0);
194  assert(j>=0);
195  assert(i<M);
196  assert(j<N);
197 #endif
198 
199  return e(i,j);
200  };
201 
202  AT& e(int i, int j) {
203  return ele[i*N+j];
204  };
205 
210  const AT& e(int i, int j) const {
211  return ele[i*N+j];
212  };
213 
214  AT& e(int i) {
215  return ele[i];
216  };
217 
222  const AT& e(int i) const {
223  return ele[i];
224  };
225 
231  AT* operator()() {return ele;};
232 
237  const AT* operator()() const {return ele;};
238 
243  int rows() const {return M;};
244 
249  int cols() const {return N;};
250 
255  int ldim() const {return M;};
256 
258  bool transposed() const {
259  return false;
260  };
261 
268  const CBLAS_TRANSPOSE blasTrans() const {
269  return CblasNoTrans;
270  };
271 
279  const CBLAS_ORDER blasOrder() const {
280  return CblasColMajor;
281  };
282 
283  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
284 
285  inline const RowVector<Fixed<N>,AT> row(int i) const;
286  inline const Vector<Var,AT> col(int j) const;
287 
295  inline Matrix<General,Var,Fixed<N>,AT>& init(const AT &val=0);
296  inline Matrix<General,Var,Fixed<N>,AT>& init(Init, const AT &a=0) { return init(a); }
297  inline Matrix<General,Var,Fixed<N>,AT>& init(Eye, const AT &val=1);
298  inline Matrix<General,Var,Fixed<N>,AT>& init(Noinit, const AT &a=0) { return *this; }
299 
304  inline operator std::vector<std::vector<AT> >() const;
305 
311  inline Matrix(std::vector<std::vector<AT> > m);
312 
313  inline const Matrix<General,Fixed<N>,Var,AT> T() const;
314 
315  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
316 
317  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
318 
319  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);
320 
321  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
322 
323  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
324 
325  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);
326 
327  };
328 
329  template <int N, class AT>
330  Matrix<General,Var,Fixed<N>,AT>::Matrix(const std::string &strs) : ele(0) {
331  std::istringstream iss(strs);
332  iss>>*this;
333 
334  // check end of stream
335  iss>>std::ws;
336  if(!iss.eof())
337  throw std::runtime_error("Input not fully read.");
338  }
339  template <int N, class AT> Matrix<General,Var,Fixed<N>,AT>::Matrix(const char * strs) :
340  Matrix<General,Var,Fixed<N>,AT>::Matrix(std::string(strs)) {}
341 
342  template <int N, class AT> template< class Type, class Row, class Col>
343  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::operator=(const Matrix<Type,Row,Col,AT> &A) {
344 
345 #ifndef FMATVEC_NO_SIZE_CHECK
346  assert(N == A.cols());
347 #endif
348  if(!ele) {
349  delete[] ele;
350  M = A.rows();
351  ele = new AT[M*N];
352  } else {
353 #ifndef FMATVEC_NO_SIZE_CHECK
354  assert(M == A.rows());
355 #endif
356  }
357 
358  deepCopy(A);
359 
360  return *this;
361  }
362 
363  template <int N, class AT>
365 
366  if(!ele) {
367  delete[] ele;
368  M = A.rows();
369  ele = new AT[M*N];
370  } else {
371 #ifndef FMATVEC_NO_SIZE_CHECK
372  assert(M == A.rows());
373 #endif
374  }
375 
376  deepCopy(A);
377 
378  return *this;
379  }
380 
381  template <int N, class AT> template< class Type, class Row, class Col>
382  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::operator<<(const Matrix<Type,Row,Col,AT> &A) {
383 
384 #ifndef FMATVEC_NO_SIZE_CHECK
385  assert(N == A.cols());
386 #endif
387  if(M!=A.rows()) {
388  delete[] ele;
389  M = A.rows();
390  ele = new AT[M*N];
391  }
392 
393  deepCopy(A);
394 
395  return *this;
396  }
397 
398  template <int N, class AT>
399  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(const AT &val) {
400  for(int i=0; i<M*N; i++)
401  e(i) = val;
402  return *this;
403  }
404 
405  template <int N, class AT>
406  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(Eye eye, const AT &val) {
407  for(int i=0; i<M; i++)
408  for(int j=0; j<N; j++)
409  e(i,j) = (i==j) ? val : 0;
410  return *this;
411  }
412 
413  template <int N, class AT>
414  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 {
415 #ifndef FMATVEC_NO_BOUNDS_CHECK
416  assert(I.end()<M);
417  assert(J.end()<N);
418 #endif
419  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
420 
421  for(int i=0; i<A.rows(); i++)
422  for(int j=0; j<A.cols(); j++)
423  A.e(i,j) = e(I.start()+i,J.start()+j);
424 
425  return A;
426  }
427 
428  template <int N, class AT>
429  inline const RowVector<Fixed<N>,AT> Matrix<General,Var,Fixed<N>,AT>::row(int i) const {
430 
431 #ifndef FMATVEC_NO_BOUNDS_CHECK
432  assert(i>=0);
433  assert(i<M);
434 #endif
435 
436  RowVector<Fixed<N>,AT> x(NONINIT);
437 
438  for(int j=0; j<N; j++)
439  x.e(j) = e(i,j);
440 
441  return x;
442 
443  }
444 
445  template <int N, class AT>
446  inline const Vector<Var,AT> Matrix<General,Var,Fixed<N>,AT>::col(int j) const {
447 
448 #ifndef FMATVEC_NO_BOUNDS_CHECK
449  assert(j>=0);
450  assert(j<N);
451 #endif
452 
453  Vector<Var,AT> x(M,NONINIT);
454 
455  for(int i=0; i<M; i++)
456  x.e(i) = e(i,j);
457 
458  return x;
459 
460  }
461 
462  template <int N, class AT>
463  inline const Matrix<General,Fixed<N>,Var,AT> Matrix<General,Var,Fixed<N>,AT>::T() const {
464  Matrix<General,Fixed<N>,Var,AT> A(rows(),NONINIT);
465  for(int i=0; i<N; i++)
466  for(int j=0; j<M; j++)
467  A.e(i,j) = e(j,i);
468  return A;
469  }
470 
471  template <int N, class AT> template <class Row>
472  inline void Matrix<General,Var,Fixed<N>,AT>::set(int j, const Vector<Row,AT> &x) {
473 #ifndef FMATVEC_NO_BOUNDS_CHECK
474  assert(j<cols());
475  assert(rows()==x.size());
476 #endif
477  for(int i=0; i<rows(); i++)
478  e(i,j) = x.e(i);
479  }
480 
481  template <int N, class AT> template <class Col>
482  inline void Matrix<General,Var,Fixed<N>,AT>::set(int i, const RowVector<Col,AT> &x) {
483 #ifndef FMATVEC_NO_BOUNDS_CHECK
484  assert(i<rows());
485  assert(cols()==x.size());
486 #endif
487  for(int j=0; j<cols(); j++)
488  e(i,j) = x.e(j);
489  }
490 
491  template <int N, class AT> template<class Type, class Row, class Col>
492  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) {
493 
494 #ifndef FMATVEC_NO_BOUNDS_CHECK
495  assert(I.end()<rows());
496  assert(J.end()<cols());
497  assert(I.size()==A.rows());
498  assert(J.size()==A.cols());
499 #endif
500 
501  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
502  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
503  e(i,j) = A.e(ii,jj);
504  }
505 
506  template <int N, class AT> template <class Row>
507  inline void Matrix<General,Var,Fixed<N>,AT>::add(int j, const Vector<Row,AT> &x) {
508 #ifndef FMATVEC_NO_BOUNDS_CHECK
509  assert(j<cols());
510  assert(rows()==x.size());
511 #endif
512  for(int i=0; i<rows(); i++)
513  e(i,j) += x.e(i);
514  }
515 
516  template <int N, class AT> template <class Col>
517  inline void Matrix<General,Var,Fixed<N>,AT>::add(int i, const RowVector<Col,AT> &x) {
518 #ifndef FMATVEC_NO_BOUNDS_CHECK
519  assert(i<rows());
520  assert(cols()==x.size());
521 #endif
522  for(int j=0; j<cols(); j++)
523  e(i,j) += x.e(j);
524  }
525 
526  template <int N, class AT> template<class Type, class Row, class Col>
527  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) {
528 
529 #ifndef FMATVEC_NO_BOUNDS_CHECK
530  assert(I.end()<rows());
531  assert(J.end()<cols());
532  assert(I.size()==A.rows());
533  assert(J.size()==A.cols());
534 #endif
535 
536  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
537  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
538  e(i,j) += A.e(ii,jj);
539  }
540 
541  template <int N, class AT>
542  inline Matrix<General,Var,Fixed<N>,AT>::operator std::vector<std::vector<AT> >() const {
543  std::vector<std::vector<AT> > ret(rows());
544  for(int r=0; r<rows(); r++) {
545  ret[r].resize(cols());
546  for(int c=0; c<cols(); c++)
547  ret[r][c]=e(r,c);
548  }
549  return ret;
550  }
551 
552  template <int N, class AT>
553  inline Matrix<General,Var,Fixed<N>,AT>::Matrix(std::vector<std::vector<AT> > m) : M(m.size()), ele(new AT[M*N]) {
554  if(m.size()<1)
555  throw std::runtime_error("The input must have at least 1 row.");
556  if(m[0].size() != N)
557  throw std::runtime_error("The input has "+std::to_string(m[0].size())+" columns but "+std::to_string(N)+" columns are required.");
558  for(int r=0; r<rows(); r++) {
559  if(static_cast<int>(m[r].size())!=cols())
560  throw std::runtime_error("The rows of the input have different length.");
561  for(int c=0; c<cols(); c++)
562  e(r,c)=m[r][c];
563  }
564  }
565 
567 
568  template <int N, class AT> template <class Type, class Row, class Col>
569  inline void Matrix<General,Var,Fixed<N>,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) {
570  for(int i=0; i<M; i++)
571  for(int j=0; j<N; j++)
572  e(i,j) = A.e(i,j);
573  }
574 
575 // template<int N, class AT>
576 // inline void Matrix<General,Var,Fixed<N>,AT>::deepCopy(const Matrix<General,Var,Fixed<N>,AT> &A) {
577 // for(int i=0; i<M*N; i++)
578 // e(i) = A.e(i);
579 // }
580 
582 
583 }
584 
585 #endif
const AT & operator()(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:191
Definition: fmatvec.h:38
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
This is a vector class of general shape in dense storage format.
Definition: var_vector.h:39
Matrix(const Matrix< General, Var, Fixed< N >, AT > &A)
Copy Constructor.
Definition: var_fixed_general_matrix.h:84
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
Definition: types.h:65
~Matrix()
Destructor.
Definition: var_fixed_general_matrix.h:121
int rows() const
Number of rows.
Definition: var_fixed_general_matrix.h:243
Definition: fmatvec.h:50
void resize(int m, int n)
Definition: var_fixed_general_matrix.h:145
bool transposed() const
A var-fixed matrix is stored in fortran-storage order -&gt; return always false.
Definition: var_fixed_general_matrix.h:258
int rows() const
Number of rows.
const AT & e(int i) const
Element operator.
Definition: var_fixed_general_matrix.h:222
int cols() const
Number of columns.
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_fixed_general_matrix.h:268
const AT & e(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:210
int ldim() const
Leading dimension.
Definition: var_fixed_general_matrix.h:255
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_fixed_general_matrix.h:279
AT * operator()()
Pointer operator.
Definition: var_fixed_general_matrix.h:231
Definition: matrix.h:42
Basic shape class for matrices.
Definition: types.h:100
const AT * operator()() const
Pointer operator.
Definition: var_fixed_general_matrix.h:237
int cols() const
Number of columns.
Definition: var_fixed_general_matrix.h:249
This is an index class for creating submatrices.
Definition: range.h:44
AT & operator()(int i, int j)
Element operator.
Definition: var_fixed_general_matrix.h:176
Definition: matrix.h:44

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home