All Classes Namespaces Functions 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 <stdlib.h>
28 
29 namespace fmatvec {
30 
39  template <int M, class AT> class Matrix<General,Fixed<M>,Var,AT> {
40 
41  public:
42 
44 
45  protected:
46 
47  int 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() : N(0), ele(0) { }
62 
63 // template<class Ini=All<AT> >
64 // Matrix(int n, Ini ini=All<AT>()) : N(n), ele(new AT[M*N]) {
65 // init(ini);
66 // }
67 // template<class Ini=All<AT> >
68 // Matrix(int m, int n, Ini ini=All<AT>()) : N(n), ele(new AT[M*N]) {
69 // init(ini);
70 // }
71 
72  Matrix(int n, Noinit) : N(n), ele(new AT[M*N]) { }
73  Matrix(int n, Init ini=INIT, const AT &a=0) : N(n), ele(new AT[M*N]) { init(a); }
74  Matrix(int n, Eye ini, const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
75  Matrix(int m, int n, Noinit) : N(n), ele(new AT[M*N]) { }
76  Matrix(int m, int n, Init ini=INIT, const AT &a=0) : N(n), ele(new AT[M*N]) { init(a); }
77  Matrix(int m, int n, Eye ini, const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
78 
86  Matrix(const Matrix<General,Fixed<M>,Var,AT> &A) : N(A.N), ele(new AT[M*N]) {
87  deepCopy(A);
88  }
89 
90  template<class Row, class Col>
91  Matrix(const Matrix<General,Row,Col,AT> &A) : N(A.cols()), ele(new AT[M*N]) {
92 
93  deepCopy(A);
94  }
95 
96  template<class Type, class Row, class Col>
97  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : N(A.cols()), ele(new AT[M*N]) {
98 
99 #ifndef FMATVEC_NO_SIZE_CHECK
100  assert(A.rows() == M);
101 #endif
102 
103  deepCopy(A);
104  }
105 
118  Matrix(const char *str);
119 
123  delete[] ele;
124  }
125 
126  Matrix<General,Fixed<M>,Var,AT>& resize() {
127  delete[] ele;
128  N = 0;
129  ele = 0;
130  return *this;
131  }
132 
133  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Noinit) {
134  delete[] ele;
135  N = n;
136  ele = new AT[M*N];
137  return *this;
138  }
139 
140  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Init ini=INIT, const AT &a=0) { return resize(n,Noinit()).init(a); }
141 
142  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Eye ini, const AT &a=1) { return resize(n,Noinit()).init(ini,a); }
143 
150  inline Matrix<General,Fixed<M>,Var,AT>& operator=(const Matrix<General,Fixed<M>,Var,AT> &A);
151 
152  template <class Type, class Row, class Col>
153  inline Matrix<General,Fixed<M>,Var,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
154 
155  template<class Type, class Row, class Col>
156  inline Matrix<General,Fixed<M>,Var,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
157 
169  AT& operator()(int i, int j) {
170 #ifndef FMATVEC_NO_BOUNDS_CHECK
171  assert(i>=0);
172  assert(j>=0);
173  assert(i<M);
174  assert(j<N);
175 #endif
176 
177  return e(i,j);
178  };
179 
184  const AT& operator()(int i, int j) const {
185 #ifndef FMATVEC_NO_BOUNDS_CHECK
186  assert(i>=0);
187  assert(j>=0);
188  assert(i<M);
189  assert(j<N);
190 #endif
191 
192  return e(i,j);
193  };
194 
195  AT& e(int i, int j) {
196  return ele[i*N+j];
197  };
198 
203  const AT& e(int i, int j) const {
204  return ele[i*N+j];
205  };
206 
207  AT& e(int i) {
208  return ele[i];
209  };
210 
215  const AT& e(int i) const {
216  return ele[i];
217  };
218 
224  AT* operator()() {return ele;};
225 
230  const AT* operator()() const {return ele;};
231 
236  int rows() const {return M;};
237 
242  int cols() const {return N;};
243 
248  int ldim() const {return M;};
249 
256  const CBLAS_TRANSPOSE blasTrans() const {
257  return CblasNoTrans;
258  };
259 
267  const CBLAS_ORDER blasOrder() const {
268  return CblasColMajor;
269  };
270 
297  //inline Matrix<GeneralVar, AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J);
298 
303  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
304 
305  template <int M1, int M2>
306  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Var,Var> &J) const;
307 
308  inline const RowVector<Var,AT> row(int j) const;
309  inline const Vector<Fixed<M>,AT> col(int j) const;
310 
318  inline Matrix<General,Fixed<M>,Var,AT>& init(const AT &a=0);
319  inline Matrix<General,Fixed<M>,Var,AT>& init(Init, const AT &a=0) { return init(a); }
320  inline Matrix<General,Fixed<M>,Var,AT>& init(Eye, const AT &a=1);
321  inline Matrix<General,Fixed<M>,Var,AT>& init(Noinit, const AT &a=0) { return *this; }
322 
327  inline operator std::vector<std::vector<AT> >();
328 
334  inline Matrix(std::vector<std::vector<AT> > m);
335 
336  inline const Matrix<General,Var,Fixed<M>,AT> T() const;
337 
338  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
339 
340  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
341 
342  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);
343 
344  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
345 
346  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
347 
348  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);
349 
350  };
351 
352  template <int M, class AT>
353  Matrix<General,Fixed<M>,Var,AT>::Matrix(const char *strs) : N(0), ele(0) {
354  // if 'strs' is a single scalar, surround it first with '[' and ']'.
355  // This is more Matlab-like, because e.g. '5' and '[5]' is just the same.
356  // (This functionallitiy is needed e.g. by MBXMLUtils (OpenMBV,MBSim))
357  std::istringstream iss(strs);
358  char c;
359  iss>>c;
360  if(c=='[') iss.str(strs);
361  else iss.str(std::string("[")+strs+"]");
362 
363  int m=0;
364  int buf=0;
365  iss >> c;
366  iss >> c;
367  if(c!=']') {
368  iss.putback(c);
369  AT x;
370  do {
371  iss >> x;
372  iss >> c;
373  if(c==';') {
374  if(buf)
375  assert(buf == N);
376 
377  buf=N;
378  N=0;
379  m++;
380  }
381  else if(c==',')
382  N++;
383  c='0';
384  } while(iss);
385 
386  N++; m++;
387  ele = new AT[M*N];
388  iss.clear();
389  iss.seekg(0);
390  iss >> c;
391  for(int i=0; i<M; i++)
392  for(int j=0; j<N; j++) {
393  iss >> e(i,j);
394  iss >> c;
395  }
396  }
397  assert(m==M);
398  }
399 
400  template <int M, class AT> template< class Type, class Row, class Col>
401  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::operator=(const Matrix<Type,Row,Col,AT> &A) {
402 
403 #ifndef FMATVEC_NO_SIZE_CHECK
404  assert(M == A.rows());
405 #endif
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>
423 
424  if(!ele) {
425  delete[] ele;
426  N = A.cols();
427  ele = new AT[M*N];
428  } else {
429 #ifndef FMATVEC_NO_SIZE_CHECK
430  assert(N == A.cols());
431 #endif
432  }
433 
434  deepCopy(A);
435 
436  return *this;
437  }
438 
439  template <int M, class AT> template< class Type, class Row, class Col>
440  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::operator<<(const Matrix<Type,Row,Col,AT> &A) {
441 
442 #ifndef FMATVEC_NO_SIZE_CHECK
443  assert(M == A.rows());
444 #endif
445  if(N!=A.cols()) {
446  delete[] ele;
447  N = A.cols();
448  ele = new AT[M*N];
449  }
450 
451  deepCopy(A);
452 
453  return *this;
454  }
455 
456  template <int M, class AT>
457  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::init(const AT &val) {
458  for(int i=0; i<M*N; i++)
459  e(i) = val;
460  return *this;
461  }
462 
463  template <int M, class AT>
464  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::init(Eye eye, const AT &val) {
465  for(int i=0; i<M; i++)
466  for(int j=0; j<N; j++)
467  e(i,j) = (i==j) ? val : 0;
468  return *this;
469  }
470 
471  template <int M, class AT> template <int M1, int M2>
472  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 {
473 #ifndef FMATVEC_NO_BOUNDS_CHECK
474  assert(M2<M);
475  assert(J.end()<N);
476 #endif
477  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
478 
479  for(int i=0; i<A.rows(); i++)
480  for(int j=0; j<A.cols(); j++)
481  A.e(i,j) = e(M1+i,J.start()+j);
482 
483  return A;
484  }
485 
486  template <int M, class AT>
487  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 {
488 #ifndef FMATVEC_NO_BOUNDS_CHECK
489  assert(I.end()<M);
490  assert(J.end()<N);
491 #endif
492  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
493 
494  for(int i=0; i<A.rows(); i++)
495  for(int j=0; j<A.cols(); j++)
496  A.e(i,j) = e(I.start()+i,J.start()+j);
497 
498  return A;
499  }
500 
501  template <int M, class AT>
502  inline const RowVector<Var,AT> Matrix<General,Fixed<M>,Var,AT>::row(int i) const {
503 
504 #ifndef FMATVEC_NO_BOUNDS_CHECK
505  assert(i>=0);
506  assert(i<M);
507 #endif
508 
509  RowVector<Var,AT> x(N,NONINIT);
510 
511  for(int j=0; j<N; j++)
512  x.e(j) = e(i,j);
513 
514  return x;
515 
516  }
517 
518  template <int M, class AT>
519  inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::col(int j) const {
520 
521 #ifndef FMATVEC_NO_BOUNDS_CHECK
522  assert(j>=0);
523  assert(j<N);
524 #endif
525 
526  Vector<Fixed<M>,AT> x(NONINIT);
527 
528  for(int i=0; i<M; i++)
529  x.e(i) = e(i,j);
530 
531  return x;
532 
533  }
534 
535  template <int M, class AT>
536  inline const Matrix<General,Var,Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::T() const {
537  Matrix<General,Var,Fixed<M>,AT> A(cols(),NONINIT);
538  for(int i=0; i<N; i++)
539  for(int j=0; j<M; j++)
540  A.e(i,j) = e(j,i);
541  return A;
542  }
543 
544  template <int M, class AT> template <class Row>
545  inline void Matrix<General,Fixed<M>,Var,AT>::set(int j, const Vector<Row,AT> &x) {
546 #ifndef FMATVEC_NO_BOUNDS_CHECK
547  assert(j<cols());
548  assert(rows()==x.size());
549 #endif
550  for(int i=0; i<rows(); i++)
551  e(i,j) = x.e(i);
552  }
553 
554  template <int M, class AT> template <class Col>
555  inline void Matrix<General,Fixed<M>,Var,AT>::set(int i, const RowVector<Col,AT> &x) {
556 #ifndef FMATVEC_NO_BOUNDS_CHECK
557  assert(i<rows());
558  assert(cols()==x.size());
559 #endif
560  for(int j=0; j<cols(); j++)
561  e(i,j) = x.e(j);
562  }
563 
564  template <int M, class AT> template<class Type, class Row, class Col>
565  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) {
566 
567 #ifndef FMATVEC_NO_BOUNDS_CHECK
568  assert(I.end()<rows());
569  assert(J.end()<cols());
570  assert(I.size()==A.rows());
571  assert(J.size()==A.cols());
572 #endif
573 
574  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
575  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
576  e(i,j) = A.e(ii,jj);
577  }
578 
579  template <int M, class AT> template <class Row>
580  inline void Matrix<General,Fixed<M>,Var,AT>::add(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 <int M, class AT> template <class Col>
590  inline void Matrix<General,Fixed<M>,Var,AT>::add(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 <int M, class AT> template<class Type, class Row, class Col>
600  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) {
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 <int M, class AT>
615  inline Matrix<General,Fixed<M>,Var,AT>::operator std::vector<std::vector<AT> >() {
616  std::vector<std::vector<AT> > ret(rows());
617  for(int r=0; r<rows(); r++) {
618  ret[r].resize(cols());
619  for(int c=0; c<cols(); c++)
620  ret[r][c]=e(r,c);
621  }
622  return ret;
623  }
624 
625  template <int M, class AT>
626  inline Matrix<General,Fixed<M>,Var,AT>::Matrix(std::vector<std::vector<AT> > m) {
627 #ifndef FMATVEC_NO_SIZE_CHECK
628  assert(m.size() == M);
629  assert(m[0].size() == N);
630 #endif
631  for(int r=0; r<rows(); r++) {
632  assert(m[r].size()==cols());
633  for(int c=0; c<cols(); c++)
634  e(r,c)=m[r][c];
635  }
636  }
637 
639 
640  template <int M, class AT> template <class Type, class Row, class Col>
641  inline void Matrix<General,Fixed<M>,Var,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) {
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: types.h:68
AT & operator()(int i, int j)
Element operator.
Definition: fixed_var_general_matrix.h:169
This is the basic matrix class for arbitrary matrices.
Definition: matrix.h:56
Matrix()
Standard constructor.
Definition: fixed_var_general_matrix.h:61
This is a matrix class for general matrices.
Definition: var_general_matrix.h:38
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:85
Definition: matrix.h:39
int rows() const
Number of rows.
Definition: fixed_var_general_matrix.h:236
Definition: types.h:65
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:184
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_var_general_matrix.h:267
const AT & e(int i) const
Element operator.
Definition: fixed_var_general_matrix.h:215
AT * operator()()
Pointer operator.
Definition: fixed_var_general_matrix.h:224
const AT & e(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:203
int cols() const
Number of columns.
Definition: matrix.h:215
This is an index class for creating submatrices.
Definition: range.h:35
int cols() const
Number of columns.
Definition: fixed_var_general_matrix.h:242
~Matrix()
Destructor.
Definition: fixed_var_general_matrix.h:122
int ldim() const
Leading dimension.
Definition: fixed_var_general_matrix.h:248
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_var_general_matrix.h:256
Definition: matrix.h:38
const AT * operator()() const
Pointer operator.
Definition: fixed_var_general_matrix.h:230
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:86
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39
Definition: matrix.h:40

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML