All Classes Namespaces Functions 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 <stdlib.h>
27 
28 namespace fmatvec {
29 
38  template <class AT> class Matrix<General,Var,Var,AT> {
39 
40  public:
41 
43 
44  protected:
45 
46  int M, N;
47 
48  AT *ele;
49 
50  template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A);
51 
53 
54  public:
55 
60  Matrix() : M(0), N(0), ele(0) { }
61 
62 // Works with -std=gnu++0x only
63 // template<class Ini=All<AT> >
64 // Matrix(int m, int n, Ini ini=All<AT>()) : M(m), N(n), ele(new AT[M*N]) {
65 // init(ini);
66 // }
67 
68  Matrix(int m, int n, Noinit) : M(m), N(n), ele(new AT[M*N]) { }
69  Matrix(int m, int n, Init ini=INIT, const AT &a=0) : M(m), N(n), ele(new AT[M*N]) { init(a); }
70  Matrix(int m, int n, Eye ini, const AT &a=1) : M(m), N(n), ele(new AT[M*N]) { init(ini,a); }
71 
79  Matrix(const Matrix<General,Var,Var,AT> &A) : M(A.M), N(A.N), ele(new AT[M*N]) {
80  deepCopy(A);
81  }
82 
83  template<class Row, class Col>
84  Matrix(const Matrix<General,Row,Col,AT> &A) : M(A.rows()), N(A.cols()), ele(new AT[M*N]) {
85 
86  deepCopy(A);
87  }
88 
89  template<class Type, class Row, class Col>
90  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), N(A.cols()), ele(new AT[M*N]) {
91 
92  deepCopy(A);
93  }
94 
107  Matrix(const char *str);
108 
112  delete[] ele;
113  }
114 
115  Matrix<General,Var,Var,AT>& resize() {
116  delete[] ele;
117  M = N = 0;
118  ele = 0;
119  return *this;
120  }
121 
122  Matrix<General,Var,Var,AT>& resize(int m, int n, Noinit) {
123  delete[] ele;
124  M = m;
125  N = n;
126  ele = new AT[M*N];
127  return *this;
128  }
129 
130  Matrix<General,Var,Var,AT>& resize(int m, int n, Init ini=INIT, const AT &a=0) { return resize(m,n,Noinit()).init(a); }
131 
132  Matrix<General,Var,Var,AT>& resize(int m, int n, Eye ini, const AT &a=1) { return resize(m,n,Noinit()).init(ini,a); }
133 
140  inline Matrix<General,Var,Var,AT>& operator=(const Matrix<General,Var,Var,AT> &A);
141 
142  template <class Type, class Row, class Col>
143  inline Matrix<General,Var,Var,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
144 
151  template<class Type, class Row, class Col>
152  inline Matrix<General,Var,Var,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
153 
165  AT& operator()(int i, int j) {
166 #ifndef FMATVEC_NO_BOUNDS_CHECK
167  assert(i>=0);
168  assert(j>=0);
169  assert(i<M);
170  assert(j<N);
171 #endif
172 
173  return e(i,j);
174  };
175 
180  const AT& operator()(int i, int j) const {
181 #ifndef FMATVEC_NO_BOUNDS_CHECK
182  assert(i>=0);
183  assert(j>=0);
184  assert(i<M);
185  assert(j<N);
186 #endif
187 
188  return e(i,j);
189  };
190 
191  AT& e(int i, int j) {
192  return ele[i*N+j];
193  };
194 
199  const AT& e(int i, int j) const {
200  return ele[i*N+j];
201  };
202 
203  AT& e(int i) {
204  return ele[i];
205  };
206 
211  const AT& e(int i) const {
212  return ele[i];
213  };
214 
220  AT* operator()() {return ele;};
221 
226  const AT* operator()() const {return ele;};
227 
232  int rows() const {return M;};
233 
238  int cols() const {return N;};
239 
244  int ldim() const {return M;};
245 
252  const CBLAS_TRANSPOSE blasTrans() const {
253  return CblasNoTrans;
254  };
255 
263  const CBLAS_ORDER blasOrder() const {
264  return CblasColMajor;
265  };
266 
293  //inline Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J);
294 
299  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
300 
301  template <int M1, int M2, int N1, int N2>
302  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;
303 
304  template <int M1, int M2>
305  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Var,Var > &J) const;
306 
307  template <int N1, int N2>
308  inline const Matrix<General,Var,Fixed<N2-N1+1>,AT> operator()(const Range<Var,Var> &I, const Range<Fixed<N1>,Fixed<N2> > &J) const;
309 
310  inline const RowVector<Var,AT> row(int i) const;
311  inline const Vector<Var,AT> col(int j) const;
312 
320  inline Matrix<General,Var,Var,AT>& init(const AT &a=0);
321  inline Matrix<General,Var,Var,AT>& init(Init all, const AT &a=0) { return init(a); }
322  inline Matrix<General,Var,Var,AT>& init(Eye eye, const AT &a=1);
323  inline Matrix<General,Var,Var,AT>& init(Noinit, const AT &a=0) { return *this; }
324 
329  inline operator std::vector<std::vector<AT> >();
330 
336  inline Matrix(std::vector<std::vector<AT> > m);
337 
338  inline const Matrix<General,Var,Var,AT> T() const;
339 
340  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
341 
342  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
343 
344  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);
345 
346  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
347 
348  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
349 
350  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);
351 
352  };
353 
354  template <class AT>
355  Matrix<General,Var,Var,AT>::Matrix(const char *strs) : M(0), N(0), ele(0) {
356  // if 'strs' is a single scalar, surround it first with '[' and ']'.
357  // This is more Matlab-like, because e.g. '5' and '[5]' is just the same.
358  // (This functionallitiy is needed e.g. by MBXMLUtils (OpenMBV,MBSim))
359  std::istringstream iss(strs);
360  char c;
361  iss>>c;
362  if(c=='[') iss.str(strs);
363  else iss.str(std::string("[")+strs+"]");
364 
365  int buf=0;
366  iss >> c;
367  iss >> c;
368  if(c!=']') {
369  iss.putback(c);
370  AT x;
371  do {
372  iss >> x;
373  iss >> c;
374  if(c==';') {
375  if(buf)
376  assert(buf == N);
377 
378  buf=N;
379  N=0;
380  M++;
381  }
382  else if(c==',')
383  N++;
384  c='0';
385  } while(iss);
386 
387  N++; M++;
388  ele = new AT[M*N];
389  iss.clear();
390  iss.seekg(0);
391  iss >> c;
392  for(int i=0; i<M; i++)
393  for(int j=0; j<N; j++) {
394  iss >> e(i,j);
395  iss >> c;
396  }
397  }
398  }
399 
400  template <class AT> template< class Type, class Row, class Col>
402 
403  if(!ele) {
404  delete[] ele;
405  M = A.rows();
406  N = A.cols();
407  ele = new AT[M*N];
408  } else {
409 #ifndef FMATVEC_NO_SIZE_CHECK
410  assert(M == A.rows());
411  assert(N == A.cols());
412 #endif
413  }
414 
415  deepCopy(A);
416 
417  return *this;
418  }
419 
420  template <class AT>
422 
423  if(!ele) {
424  delete[] ele;
425  M = A.rows();
426  N = A.cols();
427  ele = new AT[M*N];
428  } else {
429 #ifndef FMATVEC_NO_SIZE_CHECK
430  assert(M == A.rows());
431  assert(N == A.cols());
432 #endif
433  }
434 
435  deepCopy(A);
436 
437  return *this;
438  }
439 
440  template <class AT>
442  for(int i=0; i<M*N; i++)
443  e(i) = val;
444  return *this;
445  }
446 
447  template <class AT>
449  for(int i=0; i<M; i++)
450  for(int j=0; j<N; j++)
451  e(i,j) = (i==j) ? val : 0;
452  return *this;
453  }
454 
455  template <class AT> template< class Type, class Row, class Col>
457 
458  if(M!=A.rows() || N!=A.cols()) {
459  delete[] ele;
460  M = A.rows();
461  N = A.cols();
462  ele = new AT[M*N];
463  }
464 
465  deepCopy(A);
466 
467  return *this;
468  }
469 
470  template <class AT>
472 #ifndef FMATVEC_NO_BOUNDS_CHECK
473  assert(I.end()<M);
474  assert(J.end()<N);
475 #endif
476  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
477 
478  for(int i=0; i<A.rows(); i++)
479  for(int j=0; j<A.cols(); j++)
480  A.e(i,j) = e(I.start()+i,J.start()+j);
481 
482  return A;
483  }
484 
485  template <class AT> template <int M1, int M2, int N1, int N2>
486  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 {
487 #ifndef FMATVEC_NO_BOUNDS_CHECK
488  assert(M2<M);
489  assert(N2<N);
490 #endif
491  Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> A(NONINIT);
492 
493  for(int i=0; i<A.rows(); i++)
494  for(int j=0; j<A.cols(); j++)
495  A.e(i,j) = e(I.start()+i,J.start()+j);
496 
497  return A;
498  }
499 
500  template <class AT> template <int M1, int M2>
501  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 {
502 #ifndef FMATVEC_NO_BOUNDS_CHECK
503  assert(M2<M);
504  assert(J.end()<N);
505 #endif
506  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
507 
508  for(int i=0; i<A.rows(); i++)
509  for(int j=0; j<A.cols(); j++)
510  A.e(i,j) = e(I.start()+i,J.start()+j);
511 
512  return A;
513  }
514 
515  template <class AT> template <int N1, int N2>
516  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 {
517 #ifndef FMATVEC_NO_BOUNDS_CHECK
518  assert(I.end()<M);
519  assert(N2<N);
520 #endif
521  Matrix<General,Var,Fixed<N2-N1+1>,AT> A(I.end()-I.start()+1,NONINIT);
522 
523  for(int i=0; i<A.rows(); i++)
524  for(int j=0; j<A.cols(); j++)
525  A.e(i,j) = e(I.start()+i,J.start()+j);
526 
527  return A;
528  }
529 
530  template <class AT>
531  inline const RowVector<Var,AT> Matrix<General,Var,Var,AT>::row(int i) const {
532 
533 #ifndef FMATVEC_NO_BOUNDS_CHECK
534  assert(i>=0);
535  assert(i<M);
536 #endif
537 
538  RowVector<Var,AT> x(N,NONINIT);
539 
540  for(int j=0; j<N; j++)
541  x.e(j) = e(i,j);
542 
543  return x;
544 
545  }
546 
547  template <class AT>
548  inline const Vector<Var,AT> Matrix<General,Var,Var,AT>::col(int j) const {
549 
550 #ifndef FMATVEC_NO_BOUNDS_CHECK
551  assert(j>=0);
552  assert(j<N);
553 #endif
554 
555  Vector<Var,AT> x(M,NONINIT);
556 
557  for(int i=0; i<M; i++)
558  x.e(i) = e(i,j);
559 
560  return x;
561 
562  }
563 
564  template <class AT>
565  inline const Matrix<General,Var,Var,AT> Matrix<General,Var,Var,AT>::T() const {
566  Matrix<General,Var,Var,AT> A(N,M,NONINIT);
567  for(int i=0; i<N; i++)
568  for(int j=0; j<M; j++)
569  A.e(i,j) = e(j,i);
570  return A;
571  }
572 
573  template <class AT> template <class Row>
574  inline void Matrix<General,Var,Var,AT>::set(int j, const Vector<Row,AT> &x) {
575 #ifndef FMATVEC_NO_BOUNDS_CHECK
576  assert(j<cols());
577  assert(rows()==x.size());
578 #endif
579  for(int i=0; i<rows(); i++)
580  e(i,j) = x.e(i);
581  }
582 
583  template <class AT> template <class Col>
584  inline void Matrix<General,Var,Var,AT>::set(int i, const RowVector<Col,AT> &x) {
585 #ifndef FMATVEC_NO_BOUNDS_CHECK
586  assert(i<rows());
587  assert(cols()==x.size());
588 #endif
589  for(int j=0; j<cols(); j++)
590  e(i,j) = x.e(j);
591  }
592 
593  template <class AT> template<class Type, class Row, class Col>
594  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) {
595 
596 #ifndef FMATVEC_NO_BOUNDS_CHECK
597  assert(I.end()<rows());
598  assert(J.end()<cols());
599  assert(I.size()==A.rows());
600  assert(J.size()==A.cols());
601 #endif
602 
603  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
604  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
605  e(i,j) = A.e(ii,jj);
606  }
607 
608  template <class AT> template <class Row>
609  inline void Matrix<General,Var,Var,AT>::add(int j, const Vector<Row,AT> &x) {
610 #ifndef FMATVEC_NO_BOUNDS_CHECK
611  assert(j<cols());
612  assert(rows()==x.size());
613 #endif
614  for(int i=0; i<rows(); i++)
615  e(i,j) += x.e(i);
616  }
617 
618  template <class AT> template <class Col>
619  inline void Matrix<General,Var,Var,AT>::add(int i, const RowVector<Col,AT> &x) {
620 #ifndef FMATVEC_NO_BOUNDS_CHECK
621  assert(i<rows());
622  assert(cols()==x.size());
623 #endif
624  for(int j=0; j<cols(); j++)
625  e(i,j) += x.e(j);
626  }
627 
628  template <class AT> template<class Type, class Row, class Col>
629  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) {
630 
631 #ifndef FMATVEC_NO_BOUNDS_CHECK
632  assert(I.end()<rows());
633  assert(J.end()<cols());
634  assert(I.size()==A.rows());
635  assert(J.size()==A.cols());
636 #endif
637 
638  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
639  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
640  e(i,j) += A.e(ii,jj);
641  }
642 
643  template <class AT>
644  inline Matrix<General,Var,Var,AT>::operator std::vector<std::vector<AT> >() {
645  std::vector<std::vector<AT> > ret(rows());
646  for(int r=0; r<rows(); r++) {
647  ret[r].resize(cols());
648  for(int c=0; c<cols(); c++)
649  ret[r][c]=e(r,c);
650  }
651  return ret;
652  }
653 
654  template <class AT>
655  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]) {
656  for(int r=0; r<rows(); r++) {
657  assert(m[r].size()==cols());
658  for(int c=0; c<cols(); c++)
659  e(r,c)=m[r][c];
660  }
661  }
662 
664 
665  template <class AT> template <class Type, class Row, class Col>
667  for(int i=0; i<M; i++)
668  for(int j=0; j<N; j++)
669  e(i,j) = A.e(i,j);
670  }
671 
673 
674 }
675 
676 #endif
Definition: types.h:68
This is the basic matrix class for arbitrary matrices.
Definition: matrix.h:56
~Matrix()
Destructor.
Definition: var_general_matrix.h:111
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_general_matrix.h:263
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:38
AT & operator()(int i, int j)
Element operator.
Definition: var_general_matrix.h:165
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:85
Definition: matrix.h:39
Definition: types.h:65
const AT & operator()(int i, int j) const
Element operator.
Definition: var_general_matrix.h:180
int rows() const
Number of rows.
Definition: var_general_matrix.h:232
const AT * operator()() const
Pointer operator.
Definition: var_general_matrix.h:226
int rows() const
Number of rows.
int cols() const
Number of columns.
Matrix< General, Var, Var, AT > & init(const AT &a=0)
Initialization.
Definition: var_general_matrix.h:441
Matrix()
Standard constructor.
Definition: var_general_matrix.h:60
int cols() const
Number of columns.
Definition: var_general_matrix.h:238
Matrix(const Matrix< General, Var, Var, AT > &A)
Copy Constructor.
Definition: var_general_matrix.h:79
const AT & e(int i, int j) const
Element operator.
Definition: var_general_matrix.h:199
This is an index class for creating submatrices.
Definition: range.h:35
int ldim() const
Leading dimension.
Definition: var_general_matrix.h:244
AT * operator()()
Pointer operator.
Definition: var_general_matrix.h:220
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_general_matrix.h:252
Definition: matrix.h:38
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:211
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:40

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML