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