All Classes Namespaces Functions 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 <stdlib.h>
27 
28 namespace fmatvec {
29 
38  template <int N, class AT> class Matrix<General,Var,Fixed<N>,AT> {
39 
40  public:
41 
43 
44  protected:
45 
46  int M;
47 
48  AT *ele;
49 
50  template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A);
51  //inline void deepCopy(const Matrix<General,Var,Fixed<N>,AT> &A);
52 
54 
55  public:
56 
57  Matrix() : M(0), ele(0) { }
58 
59 // template<class Ini=All<AT> >
60 // Matrix(int m, Ini ini=All<AT>()) : M(m), ele(new AT[M*N]) {
61 // init(ini);
62 // }
63 // template<class Ini=All<AT> >
64 // Matrix(int m, int n, Ini ini=All<AT>()) : M(m), ele(new AT[M*N]) {
65 // init(ini);
66 // }
67 
68  Matrix(int m, Noinit) : M(m), ele(new AT[M*N]) { }
69  Matrix(int m, Init ini=INIT, const AT &a=0) : M(m), ele(new AT[M*N]) { init(a); }
70  Matrix(int m, Eye ini, const AT &a=1) : M(m), ele(new AT[M*N]) { init(ini,a); }
71  Matrix(int m, int n, Noinit) : M(m), ele(new AT[M*N]) { }
72  Matrix(int m, int n, Init ini=INIT, const AT &a=0) : M(m), ele(new AT[M*N]) { init(a); }
73  Matrix(int m, int n, Eye ini, const AT &a=1) : M(m), ele(new AT[M*N]) { init(ini,a); }
74 
82  Matrix(const Matrix<General,Var,Fixed<N>,AT> &A) : M(A.M), ele(new AT[M*N]) {
83  deepCopy(A);
84  }
85 
86  template<class Row, class Col>
87  Matrix(const Matrix<General,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*N]) {
88 
89  deepCopy(A);
90  }
91 
92  template<class Type, class Row, class Col>
93  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*N]) {
94 
95 #ifndef FMATVEC_NO_SIZE_CHECK
96  assert(A.cols() == N);
97 #endif
98 
99  deepCopy(A);
100  }
101 
114  Matrix(const char *str);
115 
119  delete[] ele;
120  }
121 
122  Matrix<General,Var,Fixed<N>,AT>& resize() {
123  delete[] ele;
124  M = 0;
125  ele = 0;
126  return *this;
127  }
128 
129  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Noinit) {
130  delete[] ele;
131  M = m;
132  ele = new AT[M*N];
133  return *this;
134  }
135 
136  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Init ini=INIT, const AT &a=0) { return resize(m,Noinit()).init(a); }
137 
138  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Eye ini, const AT &a=1) { return resize(m,Noinit()).init(ini,a); }
139 
146  inline Matrix<General,Var,Fixed<N>,AT>& operator=(const Matrix<General,Var,Fixed<N>,AT> &A);
147 
148  template <class Type, class Row, class Col>
149  inline Matrix<General,Var,Fixed<N>,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
150 
151  template <class Type, class Row, class Col>
152  inline Matrix<General,Var,Fixed<N>,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 
267  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
268 
269  inline const RowVector<Fixed<N>,AT> row(int j) const;
270  inline const Vector<Var,AT> col(int j) const;
271 
279  inline Matrix<General,Var,Fixed<N>,AT>& init(const AT &a=0);
280  inline Matrix<General,Var,Fixed<N>,AT>& init(Init, const AT &a=0) { return init(a); }
281  inline Matrix<General,Var,Fixed<N>,AT>& init(Eye, const AT &a=1);
282  inline Matrix<General,Var,Fixed<N>,AT>& init(Noinit, const AT &a=0) { return *this; }
283 
288  inline operator std::vector<std::vector<AT> >();
289 
295  inline Matrix(std::vector<std::vector<AT> > m);
296 
297  inline const Matrix<General,Fixed<N>,Var,AT> T() const;
298 
299  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
300 
301  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
302 
303  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);
304 
305  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
306 
307  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
308 
309  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);
310 
311  };
312 
313  template <int N, class AT>
314  Matrix<General,Var,Fixed<N>,AT>::Matrix(const char *strs) : M(0), ele(0) {
315  // if 'strs' is a single scalar, surround it first with '[' and ']'.
316  // This is more Matlab-like, because e.g. '5' and '[5]' is just the same.
317  // (This functionallitiy is needed e.g. by MBXMLUtils (OpenMBV,MBSim))
318  std::istringstream iss(strs);
319  char c;
320  iss>>c;
321  if(c=='[') iss.str(strs);
322  else iss.str(std::string("[")+strs+"]");
323 
324  int n=0;
325  int buf=0;
326  iss >> c;
327  iss >> c;
328  if(c!=']') {
329  iss.putback(c);
330  AT x;
331  do {
332  iss >> x;
333  iss >> c;
334  if(c==';') {
335  if(buf)
336  assert(buf == n);
337 
338  buf=n;
339  n=0;
340  M++;
341  }
342  else if(c==',')
343  n++;
344  c='0';
345  } while(iss);
346 
347  n++; M++;
348  ele = new AT[M*N];
349  iss.clear();
350  iss.seekg(0);
351  iss >> c;
352  for(int i=0; i<M; i++)
353  for(int j=0; j<N; j++) {
354  iss >> e(i,j);
355  iss >> c;
356  }
357  }
358  assert(n==N);
359  }
360 
361  template <int N, class AT> template< class Type, class Row, class Col>
363 
364 #ifndef FMATVEC_NO_SIZE_CHECK
365  assert(N == A.cols());
366 #endif
367  if(!ele) {
368  delete[] ele;
369  M = A.rows();
370  ele = new AT[M*N];
371  } else {
372 #ifndef FMATVEC_NO_SIZE_CHECK
373  assert(M == A.rows());
374 #endif
375  }
376 
377  deepCopy(A);
378 
379  return *this;
380  }
381 
382  template <int N, class AT>
384 
385  if(!ele) {
386  delete[] ele;
387  M = A.rows();
388  ele = new AT[M*N];
389  } else {
390 #ifndef FMATVEC_NO_SIZE_CHECK
391  assert(M == A.rows());
392 #endif
393  }
394 
395  deepCopy(A);
396 
397  return *this;
398  }
399 
400  template <int N, class AT> template< class Type, class Row, class Col>
401  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::operator<<(const Matrix<Type,Row,Col,AT> &A) {
402 
403 #ifndef FMATVEC_NO_SIZE_CHECK
404  assert(N == A.cols());
405 #endif
406  if(M!=A.rows()) {
407  delete[] ele;
408  M = A.rows();
409  ele = new AT[M*N];
410  }
411 
412  deepCopy(A);
413 
414  return *this;
415  }
416 
417  template <int N, class AT>
418  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(const AT &val) {
419  for(int i=0; i<M*N; i++)
420  e(i) = val;
421  return *this;
422  }
423 
424  template <int N, class AT>
425  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(Eye eye, const AT &val) {
426  for(int i=0; i<M; i++)
427  for(int j=0; j<N; j++)
428  e(i,j) = (i==j) ? val : 0;
429  return *this;
430  }
431 
432  template <int N, class AT>
433  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 {
434 #ifndef FMATVEC_NO_BOUNDS_CHECK
435  assert(I.end()<M);
436  assert(J.end()<N);
437 #endif
438  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
439 
440  for(int i=0; i<A.rows(); i++)
441  for(int j=0; j<A.cols(); j++)
442  A.e(i,j) = e(I.start()+i,J.start()+j);
443 
444  return A;
445  }
446 
447  template <int N, class AT>
448  inline const RowVector<Fixed<N>,AT> Matrix<General,Var,Fixed<N>,AT>::row(int i) const {
449 
450 #ifndef FMATVEC_NO_BOUNDS_CHECK
451  assert(i>=0);
452  assert(i<M);
453 #endif
454 
455  RowVector<Fixed<N>,AT> x(NONINIT);
456 
457  for(int j=0; j<N; j++)
458  x.e(j) = e(i,j);
459 
460  return x;
461 
462  }
463 
464  template <int N, class AT>
465  inline const Vector<Var,AT> Matrix<General,Var,Fixed<N>,AT>::col(int j) const {
466 
467 #ifndef FMATVEC_NO_BOUNDS_CHECK
468  assert(j>=0);
469  assert(j<N);
470 #endif
471 
472  Vector<Var,AT> x(M,NONINIT);
473 
474  for(int i=0; i<M; i++)
475  x.e(i) = e(i,j);
476 
477  return x;
478 
479  }
480 
481  template <int N, class AT>
482  inline const Matrix<General,Fixed<N>,Var,AT> Matrix<General,Var,Fixed<N>,AT>::T() const {
483  Matrix<General,Fixed<N>,Var,AT> A(rows(),NONINIT);
484  for(int i=0; i<N; i++)
485  for(int j=0; j<M; j++)
486  A.e(i,j) = e(j,i);
487  return A;
488  }
489 
490  template <int N, class AT> template <class Row>
491  inline void Matrix<General,Var,Fixed<N>,AT>::set(int j, const Vector<Row,AT> &x) {
492 #ifndef FMATVEC_NO_BOUNDS_CHECK
493  assert(j<cols());
494  assert(rows()==x.size());
495 #endif
496  for(int i=0; i<rows(); i++)
497  e(i,j) = x.e(i);
498  }
499 
500  template <int N, class AT> template <class Col>
501  inline void Matrix<General,Var,Fixed<N>,AT>::set(int i, const RowVector<Col,AT> &x) {
502 #ifndef FMATVEC_NO_BOUNDS_CHECK
503  assert(i<rows());
504  assert(cols()==x.size());
505 #endif
506  for(int j=0; j<cols(); j++)
507  e(i,j) = x.e(j);
508  }
509 
510  template <int N, class AT> template<class Type, class Row, class Col>
511  inline void Matrix<General,Var,Fixed<N>,AT>::set(const Index &I, const Index &J, const Matrix<Type,Row,Col,AT> &A) {
512 
513 #ifndef FMATVEC_NO_BOUNDS_CHECK
514  assert(I.end()<rows());
515  assert(J.end()<cols());
516  assert(I.size()==A.rows());
517  assert(J.size()==A.cols());
518 #endif
519 
520  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
521  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
522  e(i,j) = A.e(ii,jj);
523  }
524 
525  template <int N, class AT> template <class Row>
526  inline void Matrix<General,Var,Fixed<N>,AT>::add(int j, const Vector<Row,AT> &x) {
527 #ifndef FMATVEC_NO_BOUNDS_CHECK
528  assert(j<cols());
529  assert(rows()==x.size());
530 #endif
531  for(int i=0; i<rows(); i++)
532  e(i,j) += x.e(i);
533  }
534 
535  template <int N, class AT> template <class Col>
536  inline void Matrix<General,Var,Fixed<N>,AT>::add(int i, const RowVector<Col,AT> &x) {
537 #ifndef FMATVEC_NO_BOUNDS_CHECK
538  assert(i<rows());
539  assert(cols()==x.size());
540 #endif
541  for(int j=0; j<cols(); j++)
542  e(i,j) += x.e(j);
543  }
544 
545  template <int N, class AT> template<class Type, class Row, class Col>
546  inline void Matrix<General,Var,Fixed<N>,AT>::add(const Index &I, const Index &J, const Matrix<Type,Row,Col,AT> &A) {
547 
548 #ifndef FMATVEC_NO_BOUNDS_CHECK
549  assert(I.end()<rows());
550  assert(J.end()<cols());
551  assert(I.size()==A.rows());
552  assert(J.size()==A.cols());
553 #endif
554 
555  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
556  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
557  e(i,j) += A.e(ii,jj);
558  }
559 
560  template <int N, class AT>
561  inline Matrix<General,Var,Fixed<N>,AT>::operator std::vector<std::vector<AT> >() {
562  std::vector<std::vector<AT> > ret(rows());
563  for(int r=0; r<rows(); r++) {
564  ret[r].resize(cols());
565  for(int c=0; c<cols(); c++)
566  ret[r][c]=e(r,c);
567  }
568  return ret;
569  }
570 
571  template <int N, class AT>
572  inline Matrix<General,Var,Fixed<N>,AT>::Matrix(std::vector<std::vector<AT> > m) {
573 #ifndef FMATVEC_NO_SIZE_CHECK
574  assert(m.size() == M);
575  assert(m[0].size() == N);
576 #endif
577  for(int r=0; r<rows(); r++) {
578  assert(m[r].size()==cols());
579  for(int c=0; c<cols(); c++)
580  e(r,c)=m[r][c];
581  }
582  }
583 
585 
586  template <int N, class AT> template <class Type, class Row, class Col>
587  inline void Matrix<General,Var,Fixed<N>,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) {
588  for(int i=0; i<M; i++)
589  for(int j=0; j<N; j++)
590  e(i,j) = A.e(i,j);
591  }
592 
593 // template<int N, class AT>
594 // inline void Matrix<General,Var,Fixed<N>,AT>::deepCopy(const Matrix<General,Var,Fixed<N>,AT> &A) {
595 // for(int i=0; i<M*N; i++)
596 // e(i) = A.e(i);
597 // }
598 
600 
601 }
602 
603 #endif
const AT & operator()(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:180
Definition: types.h:68
This is the basic matrix class for arbitrary matrices.
Definition: matrix.h:56
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:82
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
Definition: types.h:65
~Matrix()
Destructor.
Definition: var_fixed_general_matrix.h:118
int rows() const
Number of rows.
Definition: var_fixed_general_matrix.h:232
Definition: matrix.h:218
int rows() const
Number of rows.
const AT & e(int i) const
Element operator.
Definition: var_fixed_general_matrix.h:211
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_fixed_general_matrix.h:252
const AT & e(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:199
int ldim() const
Leading dimension.
Definition: var_fixed_general_matrix.h:244
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_fixed_general_matrix.h:263
AT * operator()()
Pointer operator.
Definition: var_fixed_general_matrix.h:220
Definition: matrix.h:38
Basic shape class for matrices.
Definition: types.h:100
const AT * operator()() const
Pointer operator.
Definition: var_fixed_general_matrix.h:226
int cols() const
Number of columns.
Definition: var_fixed_general_matrix.h:238
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:165
Definition: matrix.h:40

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML