All Classes Namespaces Functions Variables 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  typedef AT AtomicType;
42 
44 
45  protected:
46 
47  int M;
48 
49  AT *ele;
50 
51  template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A);
52  //inline void deepCopy(const Matrix<General,Var,Fixed<N>,AT> &A);
53 
55 
56  public:
57 
58  Matrix() : M(0), ele(0) { }
59 
60 // template<class Ini=All<AT> >
61 // Matrix(int m, Ini ini=All<AT>()) : M(m), ele(new AT[M*N]) {
62 // init(ini);
63 // }
64 // template<class Ini=All<AT> >
65 // Matrix(int m, int n, Ini ini=All<AT>()) : M(m), ele(new AT[M*N]) {
66 // init(ini);
67 // }
68 
69  Matrix(int m, Noinit) : M(m), ele(new AT[M*N]) { }
70  Matrix(int m, Init ini=INIT, const AT &a=0) : M(m), ele(new AT[M*N]) { init(a); }
71  Matrix(int m, Eye ini, const AT &a=1) : M(m), ele(new AT[M*N]) { init(ini,a); }
72  Matrix(int m, int n, Noinit) : M(m), ele(new AT[M*N]) { }
73  Matrix(int m, int n, Init ini=INIT, const AT &a=0) : M(m), ele(new AT[M*N]) { init(a); }
74  Matrix(int m, int n, Eye ini, const AT &a=1) : M(m), ele(new AT[M*N]) { init(ini,a); }
75 
83  Matrix(const Matrix<General,Var,Fixed<N>,AT> &A) : M(A.M), ele(new AT[M*N]) {
84  deepCopy(A);
85  }
86 
87  template<class Row, class Col>
88  Matrix(const Matrix<General,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*N]) {
89 
90  deepCopy(A);
91  }
92 
93  template<class Type, class Row, class Col>
94  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*N]) {
95 
96 #ifndef FMATVEC_NO_SIZE_CHECK
97  assert(A.cols() == N);
98 #endif
99 
100  deepCopy(A);
101  }
102 
115  Matrix(const char *str);
116 
120  delete[] ele;
121  }
122 
123  Matrix<General,Var,Fixed<N>,AT>& resize() {
124  delete[] ele;
125  M = 0;
126  ele = 0;
127  return *this;
128  }
129 
130  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Noinit) {
131  delete[] ele;
132  M = m;
133  ele = new AT[M*N];
134  return *this;
135  }
136 
137  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Init ini=INIT, const AT &a=0) { return resize(m,Noinit()).init(a); }
138 
139  Matrix<General,Var,Fixed<N>,AT>& resize(int m, Eye ini, const AT &a=1) { return resize(m,Noinit()).init(ini,a); }
140 
143  void resize(int m, int n) {
144  if(n!=N)
145  throw std::runtime_error("A var-fixed matrix can only be resized in the first dimension.");
146  resize(m);
147  }
148 
155  inline Matrix<General,Var,Fixed<N>,AT>& operator=(const Matrix<General,Var,Fixed<N>,AT> &A);
156 
157  template <class Type, class Row, class Col>
158  inline Matrix<General,Var,Fixed<N>,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
159 
160  template <class Type, class Row, class Col>
161  inline Matrix<General,Var,Fixed<N>,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
162 
174  AT& operator()(int i, int j) {
175 #ifndef FMATVEC_NO_BOUNDS_CHECK
176  assert(i>=0);
177  assert(j>=0);
178  assert(i<M);
179  assert(j<N);
180 #endif
181 
182  return e(i,j);
183  };
184 
189  const AT& operator()(int i, int j) const {
190 #ifndef FMATVEC_NO_BOUNDS_CHECK
191  assert(i>=0);
192  assert(j>=0);
193  assert(i<M);
194  assert(j<N);
195 #endif
196 
197  return e(i,j);
198  };
199 
200  AT& e(int i, int j) {
201  return ele[i*N+j];
202  };
203 
208  const AT& e(int i, int j) const {
209  return ele[i*N+j];
210  };
211 
212  AT& e(int i) {
213  return ele[i];
214  };
215 
220  const AT& e(int i) const {
221  return ele[i];
222  };
223 
229  AT* operator()() {return ele;};
230 
235  const AT* operator()() const {return ele;};
236 
241  int rows() const {return M;};
242 
247  int cols() const {return N;};
248 
253  int ldim() const {return M;};
254 
256  bool transposed() const {
257  return false;
258  };
259 
266  const CBLAS_TRANSPOSE blasTrans() const {
267  return CblasNoTrans;
268  };
269 
277  const CBLAS_ORDER blasOrder() const {
278  return CblasColMajor;
279  };
280 
281  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
282 
283  inline const RowVector<Fixed<N>,AT> row(int j) const;
284  inline const Vector<Var,AT> col(int j) const;
285 
293  inline Matrix<General,Var,Fixed<N>,AT>& init(const AT &a=0);
294  inline Matrix<General,Var,Fixed<N>,AT>& init(Init, const AT &a=0) { return init(a); }
295  inline Matrix<General,Var,Fixed<N>,AT>& init(Eye, const AT &a=1);
296  inline Matrix<General,Var,Fixed<N>,AT>& init(Noinit, const AT &a=0) { return *this; }
297 
302  inline operator std::vector<std::vector<AT> >();
303 
309  inline Matrix(std::vector<std::vector<AT> > m);
310 
311  inline const Matrix<General,Fixed<N>,Var,AT> T() const;
312 
313  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
314 
315  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
316 
317  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);
318 
319  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
320 
321  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
322 
323  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);
324 
325  };
326 
327  template <int N, class AT>
328  Matrix<General,Var,Fixed<N>,AT>::Matrix(const char *strs) : M(0), ele(0) {
329  // if 'strs' is a single scalar, surround it first with '[' and ']'.
330  // This is more Matlab-like, because e.g. '5' and '[5]' is just the same.
331  // (This functionallitiy is needed e.g. by MBXMLUtils (OpenMBV,MBSim))
332  std::istringstream iss(strs);
333  char c;
334  iss>>c;
335  if(c=='[') iss.str(strs);
336  else iss.str(std::string("[")+strs+"]");
337 
338  int n=0;
339  int buf=0;
340  iss >> c;
341  iss >> c;
342  if(c!=']') {
343  iss.putback(c);
344  AT x;
345  do {
346  iss >> x;
347  iss >> c;
348  if(c==';') {
349  if(buf)
350  assert(buf == n);
351 
352  buf=n;
353  n=0;
354  M++;
355  }
356  else if(c==',')
357  n++;
358  c='0';
359  } while(iss);
360 
361  n++; M++;
362  ele = new AT[M*N];
363  iss.clear();
364  iss.seekg(0);
365  iss >> c;
366  for(int i=0; i<M; i++)
367  for(int j=0; j<N; j++) {
368  iss >> e(i,j);
369  iss >> c;
370  }
371  }
372  assert(n==N);
373  }
374 
375  template <int N, class AT> template< class Type, class Row, class Col>
377 
378 #ifndef FMATVEC_NO_SIZE_CHECK
379  assert(N == A.cols());
380 #endif
381  if(!ele) {
382  delete[] ele;
383  M = A.rows();
384  ele = new AT[M*N];
385  } else {
386 #ifndef FMATVEC_NO_SIZE_CHECK
387  assert(M == A.rows());
388 #endif
389  }
390 
391  deepCopy(A);
392 
393  return *this;
394  }
395 
396  template <int N, class AT>
398 
399  if(!ele) {
400  delete[] ele;
401  M = A.rows();
402  ele = new AT[M*N];
403  } else {
404 #ifndef FMATVEC_NO_SIZE_CHECK
405  assert(M == A.rows());
406 #endif
407  }
408 
409  deepCopy(A);
410 
411  return *this;
412  }
413 
414  template <int N, class AT> template< class Type, class Row, class Col>
415  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::operator<<(const Matrix<Type,Row,Col,AT> &A) {
416 
417 #ifndef FMATVEC_NO_SIZE_CHECK
418  assert(N == A.cols());
419 #endif
420  if(M!=A.rows()) {
421  delete[] ele;
422  M = A.rows();
423  ele = new AT[M*N];
424  }
425 
426  deepCopy(A);
427 
428  return *this;
429  }
430 
431  template <int N, class AT>
432  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(const AT &val) {
433  for(int i=0; i<M*N; i++)
434  e(i) = val;
435  return *this;
436  }
437 
438  template <int N, class AT>
439  inline Matrix<General,Var,Fixed<N>,AT>& Matrix<General,Var,Fixed<N>,AT>::init(Eye eye, const AT &val) {
440  for(int i=0; i<M; i++)
441  for(int j=0; j<N; j++)
442  e(i,j) = (i==j) ? val : 0;
443  return *this;
444  }
445 
446  template <int N, class AT>
447  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 {
448 #ifndef FMATVEC_NO_BOUNDS_CHECK
449  assert(I.end()<M);
450  assert(J.end()<N);
451 #endif
452  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
453 
454  for(int i=0; i<A.rows(); i++)
455  for(int j=0; j<A.cols(); j++)
456  A.e(i,j) = e(I.start()+i,J.start()+j);
457 
458  return A;
459  }
460 
461  template <int N, class AT>
462  inline const RowVector<Fixed<N>,AT> Matrix<General,Var,Fixed<N>,AT>::row(int i) const {
463 
464 #ifndef FMATVEC_NO_BOUNDS_CHECK
465  assert(i>=0);
466  assert(i<M);
467 #endif
468 
469  RowVector<Fixed<N>,AT> x(NONINIT);
470 
471  for(int j=0; j<N; j++)
472  x.e(j) = e(i,j);
473 
474  return x;
475 
476  }
477 
478  template <int N, class AT>
479  inline const Vector<Var,AT> Matrix<General,Var,Fixed<N>,AT>::col(int j) const {
480 
481 #ifndef FMATVEC_NO_BOUNDS_CHECK
482  assert(j>=0);
483  assert(j<N);
484 #endif
485 
486  Vector<Var,AT> x(M,NONINIT);
487 
488  for(int i=0; i<M; i++)
489  x.e(i) = e(i,j);
490 
491  return x;
492 
493  }
494 
495  template <int N, class AT>
496  inline const Matrix<General,Fixed<N>,Var,AT> Matrix<General,Var,Fixed<N>,AT>::T() const {
497  Matrix<General,Fixed<N>,Var,AT> A(rows(),NONINIT);
498  for(int i=0; i<N; i++)
499  for(int j=0; j<M; j++)
500  A.e(i,j) = e(j,i);
501  return A;
502  }
503 
504  template <int N, class AT> template <class Row>
505  inline void Matrix<General,Var,Fixed<N>,AT>::set(int j, const Vector<Row,AT> &x) {
506 #ifndef FMATVEC_NO_BOUNDS_CHECK
507  assert(j<cols());
508  assert(rows()==x.size());
509 #endif
510  for(int i=0; i<rows(); i++)
511  e(i,j) = x.e(i);
512  }
513 
514  template <int N, class AT> template <class Col>
515  inline void Matrix<General,Var,Fixed<N>,AT>::set(int i, const RowVector<Col,AT> &x) {
516 #ifndef FMATVEC_NO_BOUNDS_CHECK
517  assert(i<rows());
518  assert(cols()==x.size());
519 #endif
520  for(int j=0; j<cols(); j++)
521  e(i,j) = x.e(j);
522  }
523 
524  template <int N, class AT> template<class Type, class Row, class Col>
525  inline void Matrix<General,Var,Fixed<N>,AT>::set(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
526 
527 #ifndef FMATVEC_NO_BOUNDS_CHECK
528  assert(I.end()<rows());
529  assert(J.end()<cols());
530  assert(I.size()==A.rows());
531  assert(J.size()==A.cols());
532 #endif
533 
534  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
535  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
536  e(i,j) = A.e(ii,jj);
537  }
538 
539  template <int N, class AT> template <class Row>
540  inline void Matrix<General,Var,Fixed<N>,AT>::add(int j, const Vector<Row,AT> &x) {
541 #ifndef FMATVEC_NO_BOUNDS_CHECK
542  assert(j<cols());
543  assert(rows()==x.size());
544 #endif
545  for(int i=0; i<rows(); i++)
546  e(i,j) += x.e(i);
547  }
548 
549  template <int N, class AT> template <class Col>
550  inline void Matrix<General,Var,Fixed<N>,AT>::add(int i, const RowVector<Col,AT> &x) {
551 #ifndef FMATVEC_NO_BOUNDS_CHECK
552  assert(i<rows());
553  assert(cols()==x.size());
554 #endif
555  for(int j=0; j<cols(); j++)
556  e(i,j) += x.e(j);
557  }
558 
559  template <int N, class AT> template<class Type, class Row, class Col>
560  inline void Matrix<General,Var,Fixed<N>,AT>::add(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
561 
562 #ifndef FMATVEC_NO_BOUNDS_CHECK
563  assert(I.end()<rows());
564  assert(J.end()<cols());
565  assert(I.size()==A.rows());
566  assert(J.size()==A.cols());
567 #endif
568 
569  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
570  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
571  e(i,j) += A.e(ii,jj);
572  }
573 
574  template <int N, class AT>
575  inline Matrix<General,Var,Fixed<N>,AT>::operator std::vector<std::vector<AT> >() {
576  std::vector<std::vector<AT> > ret(rows());
577  for(int r=0; r<rows(); r++) {
578  ret[r].resize(cols());
579  for(int c=0; c<cols(); c++)
580  ret[r][c]=e(r,c);
581  }
582  return ret;
583  }
584 
585  template <int N, class AT>
586  inline Matrix<General,Var,Fixed<N>,AT>::Matrix(std::vector<std::vector<AT> > m) {
587 #ifndef FMATVEC_NO_SIZE_CHECK
588  assert(m.size() == M);
589  assert(m[0].size() == N);
590 #endif
591  for(int r=0; r<rows(); r++) {
592  assert(m[r].size()==cols());
593  for(int c=0; c<cols(); c++)
594  e(r,c)=m[r][c];
595  }
596  }
597 
599 
600  template <int N, class AT> template <class Type, class Row, class Col>
601  inline void Matrix<General,Var,Fixed<N>,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) {
602  for(int i=0; i<M; i++)
603  for(int j=0; j<N; j++)
604  e(i,j) = A.e(i,j);
605  }
606 
607 // template<int N, class AT>
608 // inline void Matrix<General,Var,Fixed<N>,AT>::deepCopy(const Matrix<General,Var,Fixed<N>,AT> &A) {
609 // for(int i=0; i<M*N; i++)
610 // e(i) = A.e(i);
611 // }
612 
614 
615 }
616 
617 #endif
const AT & operator()(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:189
Definition: fmatvec.h:38
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
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:83
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
Definition: types.h:65
~Matrix()
Destructor.
Definition: var_fixed_general_matrix.h:119
int rows() const
Number of rows.
Definition: var_fixed_general_matrix.h:241
Definition: fmatvec.h:50
void resize(int m, int n)
Definition: var_fixed_general_matrix.h:143
bool transposed() const
A var-fixed matrix is stored in fortran-storage order -&gt; return always false.
Definition: var_fixed_general_matrix.h:256
int rows() const
Number of rows.
const AT & e(int i) const
Element operator.
Definition: var_fixed_general_matrix.h:220
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_fixed_general_matrix.h:266
const AT & e(int i, int j) const
Element operator.
Definition: var_fixed_general_matrix.h:208
int ldim() const
Leading dimension.
Definition: var_fixed_general_matrix.h:253
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_fixed_general_matrix.h:277
AT * operator()()
Pointer operator.
Definition: var_fixed_general_matrix.h:229
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:235
int cols() const
Number of columns.
Definition: var_fixed_general_matrix.h:247
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:174
Definition: matrix.h:40

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML