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 <stdlib.h>
28 
29 namespace fmatvec {
30 
39  template <int M, class AT> class Matrix<General,Fixed<M>,Var,AT> {
40 
41  public:
42 
43  typedef AT AtomicType;
44 
46 
47  protected:
48 
49  int N;
50 
51  AT *ele;
52 
53  template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A);
54 
56 
57  public:
58 
63  Matrix() : N(0), ele(0) { }
64 
65 // template<class Ini=All<AT> >
66 // Matrix(int n, Ini ini=All<AT>()) : N(n), ele(new AT[M*N]) {
67 // init(ini);
68 // }
69 // template<class Ini=All<AT> >
70 // Matrix(int m, int n, Ini ini=All<AT>()) : N(n), ele(new AT[M*N]) {
71 // init(ini);
72 // }
73 
74  Matrix(int n, Noinit) : N(n), ele(new AT[M*N]) { }
75  Matrix(int n, Init ini=INIT, const AT &a=0) : N(n), ele(new AT[M*N]) { init(a); }
76  Matrix(int n, Eye ini, const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
77  Matrix(int m, int n, Noinit) : N(n), ele(new AT[M*N]) { }
78  Matrix(int m, int n, Init ini=INIT, const AT &a=0) : N(n), ele(new AT[M*N]) { init(a); }
79  Matrix(int m, int n, Eye ini, const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
80 
88  Matrix(const Matrix<General,Fixed<M>,Var,AT> &A) : N(A.N), ele(new AT[M*N]) {
89  deepCopy(A);
90  }
91 
92  template<class Row, class Col>
93  Matrix(const Matrix<General,Row,Col,AT> &A) : N(A.cols()), ele(new AT[M*N]) {
94 
95  deepCopy(A);
96  }
97 
98  template<class Type, class Row, class Col>
99  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : N(A.cols()), ele(new AT[M*N]) {
100 
101 #ifndef FMATVEC_NO_SIZE_CHECK
102  assert(A.rows() == M);
103 #endif
104 
105  deepCopy(A);
106  }
107 
120  Matrix(const char *str);
121 
125  delete[] ele;
126  }
127 
128  Matrix<General,Fixed<M>,Var,AT>& resize() {
129  delete[] ele;
130  N = 0;
131  ele = 0;
132  return *this;
133  }
134 
135  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Noinit) {
136  delete[] ele;
137  N = n;
138  ele = new AT[M*N];
139  return *this;
140  }
141 
142  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Init ini=INIT, const AT &a=0) { return resize(n,Noinit()).init(a); }
143 
144  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Eye ini, const AT &a=1) { return resize(n,Noinit()).init(ini,a); }
145 
148  void resize(int m, int n) {
149  if(m!=M)
150  throw std::runtime_error("A fixed-var matrix can only be resized in the second dimension.");
151  resize(n);
152  }
153 
160  inline Matrix<General,Fixed<M>,Var,AT>& operator=(const Matrix<General,Fixed<M>,Var,AT> &A);
161 
162  template <class Type, class Row, class Col>
163  inline Matrix<General,Fixed<M>,Var,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
164 
165  template<class Type, class Row, class Col>
166  inline Matrix<General,Fixed<M>,Var,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
167 
179  AT& operator()(int i, int j) {
180 #ifndef FMATVEC_NO_BOUNDS_CHECK
181  assert(i>=0);
182  assert(j>=0);
183  assert(i<M);
184  assert(j<N);
185 #endif
186 
187  return e(i,j);
188  };
189 
194  const AT& operator()(int i, int j) const {
195 #ifndef FMATVEC_NO_BOUNDS_CHECK
196  assert(i>=0);
197  assert(j>=0);
198  assert(i<M);
199  assert(j<N);
200 #endif
201 
202  return e(i,j);
203  };
204 
205  AT& e(int i, int j) {
206  return ele[i*N+j];
207  };
208 
213  const AT& e(int i, int j) const {
214  return ele[i*N+j];
215  };
216 
217  AT& e(int i) {
218  return ele[i];
219  };
220 
225  const AT& e(int i) const {
226  return ele[i];
227  };
228 
234  AT* operator()() {return ele;};
235 
240  const AT* operator()() const {return ele;};
241 
246  int rows() const {return M;};
247 
252  int cols() const {return N;};
253 
258  int ldim() const {return M;};
259 
261  bool transposed() const {
262  return false;
263  };
264 
271  const CBLAS_TRANSPOSE blasTrans() const {
272  return CblasNoTrans;
273  };
274 
282  const CBLAS_ORDER blasOrder() const {
283  return CblasColMajor;
284  };
285 
312  //inline Matrix<GeneralVar, AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J);
313 
318  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
319 
320  template <int M1, int M2>
321  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Var,Var> &J) const;
322 
323  inline const RowVector<Var,AT> row(int j) const;
324  inline const Vector<Fixed<M>,AT> col(int j) const;
325 
333  inline Matrix<General,Fixed<M>,Var,AT>& init(const AT &a=0);
334  inline Matrix<General,Fixed<M>,Var,AT>& init(Init, const AT &a=0) { return init(a); }
335  inline Matrix<General,Fixed<M>,Var,AT>& init(Eye, const AT &a=1);
336  inline Matrix<General,Fixed<M>,Var,AT>& init(Noinit, const AT &a=0) { return *this; }
337 
342  inline operator std::vector<std::vector<AT> >();
343 
349  inline Matrix(std::vector<std::vector<AT> > m);
350 
351  inline const Matrix<General,Var,Fixed<M>,AT> T() const;
352 
353  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
354 
355  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
356 
357  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);
358 
359  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
360 
361  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
362 
363  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);
364 
365  };
366 
367  template <int M, class AT>
368  Matrix<General,Fixed<M>,Var,AT>::Matrix(const char *strs) : N(0), ele(0) {
369  // if 'strs' is a single scalar, surround it first with '[' and ']'.
370  // This is more Matlab-like, because e.g. '5' and '[5]' is just the same.
371  // (This functionallitiy is needed e.g. by MBXMLUtils (OpenMBV,MBSim))
372  std::istringstream iss(strs);
373  char c;
374  iss>>c;
375  if(c=='[') iss.str(strs);
376  else iss.str(std::string("[")+strs+"]");
377 
378  int m=0;
379  int buf=0;
380  iss >> c;
381  iss >> c;
382  if(c!=']') {
383  iss.putback(c);
384  AT x;
385  do {
386  iss >> x;
387  iss >> c;
388  if(c==';') {
389  if(buf)
390  assert(buf == N);
391 
392  buf=N;
393  N=0;
394  m++;
395  }
396  else if(c==',')
397  N++;
398  c='0';
399  } while(iss);
400 
401  N++; m++;
402  ele = new AT[M*N];
403  iss.clear();
404  iss.seekg(0);
405  iss >> c;
406  for(int i=0; i<M; i++)
407  for(int j=0; j<N; j++) {
408  iss >> e(i,j);
409  iss >> c;
410  }
411  }
412  assert(m==M);
413  }
414 
415  template <int M, class AT> template< class Type, class Row, class Col>
416  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::operator=(const Matrix<Type,Row,Col,AT> &A) {
417 
418 #ifndef FMATVEC_NO_SIZE_CHECK
419  assert(M == A.rows());
420 #endif
421  if(!ele) {
422  delete[] ele;
423  N = A.cols();
424  ele = new AT[M*N];
425  } else {
426 #ifndef FMATVEC_NO_SIZE_CHECK
427  assert(N == A.cols());
428 #endif
429  }
430 
431  deepCopy(A);
432 
433  return *this;
434  }
435 
436  template <int M, class AT>
438 
439  if(!ele) {
440  delete[] ele;
441  N = A.cols();
442  ele = new AT[M*N];
443  } else {
444 #ifndef FMATVEC_NO_SIZE_CHECK
445  assert(N == A.cols());
446 #endif
447  }
448 
449  deepCopy(A);
450 
451  return *this;
452  }
453 
454  template <int M, class AT> template< class Type, class Row, class Col>
455  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::operator<<(const Matrix<Type,Row,Col,AT> &A) {
456 
457 #ifndef FMATVEC_NO_SIZE_CHECK
458  assert(M == A.rows());
459 #endif
460  if(N!=A.cols()) {
461  delete[] ele;
462  N = A.cols();
463  ele = new AT[M*N];
464  }
465 
466  deepCopy(A);
467 
468  return *this;
469  }
470 
471  template <int M, class AT>
472  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::init(const AT &val) {
473  for(int i=0; i<M*N; i++)
474  e(i) = val;
475  return *this;
476  }
477 
478  template <int M, class AT>
479  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::init(Eye eye, const AT &val) {
480  for(int i=0; i<M; i++)
481  for(int j=0; j<N; j++)
482  e(i,j) = (i==j) ? val : 0;
483  return *this;
484  }
485 
486  template <int M, class AT> template <int M1, int M2>
487  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 {
488 #ifndef FMATVEC_NO_BOUNDS_CHECK
489  assert(M2<M);
490  assert(J.end()<N);
491 #endif
492  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(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(M1+i,J.start()+j);
497 
498  return A;
499  }
500 
501  template <int M, class AT>
502  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 {
503 #ifndef FMATVEC_NO_BOUNDS_CHECK
504  assert(I.end()<M);
505  assert(J.end()<N);
506 #endif
507  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
508 
509  for(int i=0; i<A.rows(); i++)
510  for(int j=0; j<A.cols(); j++)
511  A.e(i,j) = e(I.start()+i,J.start()+j);
512 
513  return A;
514  }
515 
516  template <int M, class AT>
517  inline const RowVector<Var,AT> Matrix<General,Fixed<M>,Var,AT>::row(int i) const {
518 
519 #ifndef FMATVEC_NO_BOUNDS_CHECK
520  assert(i>=0);
521  assert(i<M);
522 #endif
523 
524  RowVector<Var,AT> x(N,NONINIT);
525 
526  for(int j=0; j<N; j++)
527  x.e(j) = e(i,j);
528 
529  return x;
530 
531  }
532 
533  template <int M, class AT>
534  inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::col(int j) const {
535 
536 #ifndef FMATVEC_NO_BOUNDS_CHECK
537  assert(j>=0);
538  assert(j<N);
539 #endif
540 
541  Vector<Fixed<M>,AT> x(NONINIT);
542 
543  for(int i=0; i<M; i++)
544  x.e(i) = e(i,j);
545 
546  return x;
547 
548  }
549 
550  template <int M, class AT>
551  inline const Matrix<General,Var,Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::T() const {
552  Matrix<General,Var,Fixed<M>,AT> A(cols(),NONINIT);
553  for(int i=0; i<N; i++)
554  for(int j=0; j<M; j++)
555  A.e(i,j) = e(j,i);
556  return A;
557  }
558 
559  template <int M, class AT> template <class Row>
560  inline void Matrix<General,Fixed<M>,Var,AT>::set(int j, const Vector<Row,AT> &x) {
561 #ifndef FMATVEC_NO_BOUNDS_CHECK
562  assert(j<cols());
563  assert(rows()==x.size());
564 #endif
565  for(int i=0; i<rows(); i++)
566  e(i,j) = x.e(i);
567  }
568 
569  template <int M, class AT> template <class Col>
570  inline void Matrix<General,Fixed<M>,Var,AT>::set(int i, const RowVector<Col,AT> &x) {
571 #ifndef FMATVEC_NO_BOUNDS_CHECK
572  assert(i<rows());
573  assert(cols()==x.size());
574 #endif
575  for(int j=0; j<cols(); j++)
576  e(i,j) = x.e(j);
577  }
578 
579  template <int M, class AT> template<class Type, class Row, class Col>
580  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) {
581 
582 #ifndef FMATVEC_NO_BOUNDS_CHECK
583  assert(I.end()<rows());
584  assert(J.end()<cols());
585  assert(I.size()==A.rows());
586  assert(J.size()==A.cols());
587 #endif
588 
589  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
590  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
591  e(i,j) = A.e(ii,jj);
592  }
593 
594  template <int M, class AT> template <class Row>
595  inline void Matrix<General,Fixed<M>,Var,AT>::add(int j, const Vector<Row,AT> &x) {
596 #ifndef FMATVEC_NO_BOUNDS_CHECK
597  assert(j<cols());
598  assert(rows()==x.size());
599 #endif
600  for(int i=0; i<rows(); i++)
601  e(i,j) += x.e(i);
602  }
603 
604  template <int M, class AT> template <class Col>
605  inline void Matrix<General,Fixed<M>,Var,AT>::add(int i, const RowVector<Col,AT> &x) {
606 #ifndef FMATVEC_NO_BOUNDS_CHECK
607  assert(i<rows());
608  assert(cols()==x.size());
609 #endif
610  for(int j=0; j<cols(); j++)
611  e(i,j) += x.e(j);
612  }
613 
614  template <int M, class AT> template<class Type, class Row, class Col>
615  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) {
616 
617 #ifndef FMATVEC_NO_BOUNDS_CHECK
618  assert(I.end()<rows());
619  assert(J.end()<cols());
620  assert(I.size()==A.rows());
621  assert(J.size()==A.cols());
622 #endif
623 
624  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
625  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
626  e(i,j) += A.e(ii,jj);
627  }
628 
629  template <int M, class AT>
630  inline Matrix<General,Fixed<M>,Var,AT>::operator std::vector<std::vector<AT> >() {
631  std::vector<std::vector<AT> > ret(rows());
632  for(int r=0; r<rows(); r++) {
633  ret[r].resize(cols());
634  for(int c=0; c<cols(); c++)
635  ret[r][c]=e(r,c);
636  }
637  return ret;
638  }
639 
640  template <int M, class AT>
641  inline Matrix<General,Fixed<M>,Var,AT>::Matrix(std::vector<std::vector<AT> > m) {
642 #ifndef FMATVEC_NO_SIZE_CHECK
643  assert(m.size() == M);
644  assert(m[0].size() == N);
645 #endif
646  for(int r=0; r<rows(); r++) {
647  assert(m[r].size()==cols());
648  for(int c=0; c<cols(); c++)
649  e(r,c)=m[r][c];
650  }
651  }
652 
654 
655  template <int M, class AT> template <class Type, class Row, class Col>
656  inline void Matrix<General,Fixed<M>,Var,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) {
657  for(int i=0; i<M; i++)
658  for(int j=0; j<N; j++)
659  e(i,j) = A.e(i,j);
660  }
661 
663 
664 }
665 
666 #endif
Definition: fmatvec.h:38
void resize(int m, int n)
Definition: fixed_var_general_matrix.h:148
AT & operator()(int i, int j)
Element operator.
Definition: fixed_var_general_matrix.h:179
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Matrix()
Standard constructor.
Definition: fixed_var_general_matrix.h:63
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:86
Definition: matrix.h:39
int rows() const
Number of rows.
Definition: fixed_var_general_matrix.h:246
Definition: types.h:65
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:194
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_var_general_matrix.h:282
const AT & e(int i) const
Element operator.
Definition: fixed_var_general_matrix.h:225
AT * operator()()
Pointer operator.
Definition: fixed_var_general_matrix.h:234
const AT & e(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:213
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:252
~Matrix()
Destructor.
Definition: fixed_var_general_matrix.h:124
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:261
int ldim() const
Leading dimension.
Definition: fixed_var_general_matrix.h:258
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_var_general_matrix.h:271
Definition: matrix.h:38
const AT * operator()() const
Pointer operator.
Definition: fixed_var_general_matrix.h:240
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:88
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