t_matrix.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
33 #ifndef ACADO_TOOLKIT_T_MATRIX_HPP
34 #define ACADO_TOOLKIT_T_MATRIX_HPP
35 
36 
38 
52 template <typename T>
53 class Tmatrix
55 {
56  template <typename U> friend class Tmatrix;
57  template <typename U> friend std::ostream& operator<<
58  ( std::ostream&, const Tmatrix<U>& );
59  template <typename U> friend Tmatrix<U> operator+
60  ( const Tmatrix<U>& );
61  template <typename U, typename V> friend Tmatrix<U> operator+
62  ( const Tmatrix<U>&, const V& );
63  template <typename U, typename V> friend Tmatrix<U> operator+
64  ( const V&, const Tmatrix<U>& );
65  template <typename U, typename V> friend Tmatrix<U> operator+
66  ( const Tmatrix<U>&, const Tmatrix<V>& );
67  template <typename U> friend Tmatrix<U> operator-
68  ( const Tmatrix<U>& );
69  template <typename U, typename V> friend Tmatrix<U> operator-
70  ( const Tmatrix<U>&, const V& );
71  template <typename U, typename V> friend Tmatrix<U> operator-
72  ( const U&, const Tmatrix<V>& );
73  template <typename U, typename V> friend Tmatrix<U> operator-
74  ( const Tmatrix<U>&, const Tmatrix<V>& );
75  template <typename U, typename V> friend Tmatrix<U> operator*
76  ( const Tmatrix<U>&, const V& );
77  template <typename U, typename V> friend Tmatrix<U> operator*
78  ( const V&, const Tmatrix<U>& );
79  template <typename U, typename V> friend Tmatrix<U> operator*
80  ( const Tmatrix<U>&, const Tmatrix<V>& );
81  template <typename U, typename V> friend Tmatrix<U> operator/
82  ( const Tmatrix<U>&, const V& );
83 
84 public:
85 
89  Tmatrix():
91  _nr(0), _nc(0), _sub(false), _pcol(0), _prow(0), _pblock(0)
92  {}
94  Tmatrix
95  ( const unsigned int nr, const unsigned int nc=1, const bool alloc=true ):
96  _nr(nr), _nc(nc), _sub(!alloc), _pcol(0), _prow(0), _pblock(0)
97  {
98  unsigned int ne = nr*nc;
99  for( unsigned int ie=0; ie<ne; ie++ ){
100  T*pvall = ( alloc? new T: 0 );
101  _data.push_back( pvall );
102  }
103  }
105  template <typename U> Tmatrix
106  ( const unsigned int nr, const unsigned int nc, const U&v,
107  const bool alloc=true ):
108  _nr(nr), _nc(nc), _sub(!alloc), _pcol(0), _prow(0), _pblock(0)
109  {
110  unsigned int ne = nr*nc;
111  for( unsigned int ie=0; ie<ne; ie++ ){
112  T*pvall = ( alloc? new T(v): 0 );
113  _data.push_back( pvall );
114  }
115  }
117  Tmatrix
118  ( const Tmatrix<T>&M ):
119  _nr(M._nr), _nc(M._nc), _sub(false), _pcol(0), _prow(0), _pblock(0)
120  {
121  typename std::vector<T*>::const_iterator it = M._data.begin();
122  for( ; it!=M._data.end(); it++ ){
123  T*pvall = new T(**it);
124  _data.push_back( pvall );
125  }
126  }
128  template <typename U> Tmatrix
129  ( const Tmatrix<U>&M ):
130  _nr(M._nr), _nc(M._nc), _sub(false), _pcol(0), _prow(0), _pblock(0)
131  {
132  typename std::vector<U*>::const_iterator it = M._data.begin();
133  for( ; it!=M._data.end(); it++ ){
134  T*pvall = new T(**it);
135  _data.push_back( pvall );
136  }
137  }
140  {
141  delete _pcol;
142  delete _prow;
143  delete _pblock;
144  if( !_sub ) _reset();
145  }
146 
148  void resize
149  ( const unsigned int nr, const unsigned int nc=1, const bool alloc=true )
150  {
151  delete _pcol;
152  delete _prow;
153  delete _pblock;
154  if( !_sub ) _reset();
155  _nr = nr; _nc = nc; _sub = !alloc;
156  _pcol = _prow = _pblock = 0;
157  unsigned int ne = nr*nc;
158  for( unsigned int ie=0; ie<ne; ie++ ){
159  T*pvall = ( alloc? new T: 0 );
160  _data.push_back( pvall );
161  }
162  }
164  Tmatrix<T>& col
165  ( const unsigned int ic );
167  Tmatrix<T>& row
168  ( const unsigned int ir );
170  T*& pval
171  ( const unsigned int ir, const unsigned int ic );
173  unsigned int col() const
174  { return _nc; };
176  unsigned int row() const
177  { return _nr; };
179  T* array() const;
181  void array
182  ( const T*pM );
183 
184  unsigned int getDim() const{ return _nc*_nr; }
185  unsigned int getNumRows() const{ return _nr; }
186  unsigned int getNumCols() const{ return _nc; }
187 
188 
191  T& operator()
192  ( const unsigned int ir, const unsigned int ic );
193  const T& operator()
194  ( const unsigned int ir, const unsigned int ic ) const;
195  T& operator()
196  ( const unsigned int ie );
197  const T& operator()
198  ( const unsigned int ie ) const;
199  Tmatrix<T>& operator=
200  ( const Tmatrix<T>&M );
201  Tmatrix<T>& operator=
202  ( const T&m );
203  template <typename U> Tmatrix<T>& operator+=
204  ( const Tmatrix<U>&M );
205  template <typename U> Tmatrix<T>& operator+=
206  ( const U&m );
207  template <typename U> Tmatrix<T>& operator-=
208  ( const Tmatrix<U>&M );
209  template <typename U> Tmatrix<T>& operator-=
210  ( const U&m );
211  template <typename U> Tmatrix<T>& operator*=
212  ( const Tmatrix<U>&M );
213  template <typename U> Tmatrix<T>& operator*=
214  ( const U&m );
215  template <typename U> Tmatrix<T>& operator/=
216  ( const U&m );
217 
218 private:
219 
221  unsigned int _nr;
223  unsigned int _nc;
225  std::vector<T*> _data;
227  bool _sub;
229  Tmatrix<T>* _pcol;
231  Tmatrix<T>* _prow;
233  Tmatrix<T>* _pblock;
234 
236  T& _val
237  ( const unsigned int ir, const unsigned int ic );
238  const T& _val
239  ( const unsigned int ir, const unsigned int ic ) const;
240  T& _val
241  ( const unsigned int ie );
242  const T& _val
243  ( const unsigned int ie ) const;
245  T*& _pval
246  ( const unsigned int ie );
248  void _reset();
250  static unsigned int _digits
251  ( unsigned int n );
252 };
253 
255 
256 template <typename T> inline T&
258 ( const unsigned int ir, const unsigned int ic )
259 {
260  return _val(ic*_nr+ir);
261 }
262 
263 template <typename T> inline const T&
265 ( const unsigned int ir, const unsigned int ic ) const
266 {
267  return _val(ic*_nr+ir);
268 }
269 
270 template <typename T> inline T&
272 ( const unsigned int ie )
273 {
274  return _val(ie);
275 }
276 
277 template <typename T> inline const T&
279 ( const unsigned int ie ) const
280 {
281  return _val(ie);
282 }
283 
284 template <typename T> inline T&
286 ( const unsigned int ir, const unsigned int ic )
287 {
288  return _val(ic*_nr+ir);
289 }
290 
291 template <typename T> inline const T&
293 ( const unsigned int ir, const unsigned int ic ) const
294 {
295  return _val(ic*_nr+ir);
296 }
297 
298 template <typename T> inline T&
300 ( const unsigned int ie )
301 {
302  ASSERT( ie<_nr*_nc && ie>=0 );
303  return *(_data[ie]);
304 }
305 
306 template <typename T> inline const T&
308 ( const unsigned int ie ) const
309 {
310  ASSERT( ie<_nr*_nc && ie>=0 );
311  return *(_data[ie]);
312 }
313 
314 template <typename T> inline T*&
316 ( const unsigned int ir, const unsigned int ic )
317 {
318  return _pval(ic*_nr+ir);
319 }
320 
321 template <typename T> inline T*&
323 ( const unsigned int ie )
324 {
325  ASSERT( ie<_nr*_nc && ie>=0 );
326  return _data[ie];
327 }
328 
329 template <typename T> inline void
331 {
332  typename std::vector<T*>::iterator it = _data.begin();
333  for( ; it!=_data.end(); it++ ) delete *it;
334 }
335 
336 template <typename T> inline Tmatrix<T>&
338 ( const unsigned int ic )
339 {
340  ASSERT( ic<_nc && ic>=0 );
341  if( !_pcol ) _pcol = new Tmatrix<T>( _nr, 1, false );
342  for( unsigned int ir=0; ir<_nr; ir++ ){
343  _pcol->pval(ir,0) = pval(ir,ic);
344 #ifdef DEBUG__MATRIX_COL
345  std::cout << ir << " , 0 : " << pval(ir,ic) << " "
346  << _pcol->pval(ir,0) << std::endl;
347 #endif
348  }
349 #ifdef DEBUG__MATRIX_COL
350  std::cout << std::endl;
351 #endif
352  return *_pcol;
353 }
354 
355 template <typename T> inline Tmatrix<T>&
357 ( const unsigned int ir )
358 {
359  ASSERT( ir<_nr && ir>=0 );
360  if( !_prow ) _prow = new Tmatrix<T>( 1, _nc, false );
361  for( unsigned int ic=0; ic<_nc; ic++ ){
362  _prow->pval(0,ic) = pval(ir,ic);
363 #ifdef DEBUG__MATRIX_ROW
364  std::cout << "0 , " << ic << " : " << pval(ir,ic) << " "
365  << _prow->pval(0,ic) << std::endl;
366 #endif
367  }
368 #ifdef DEBUG__MATRIX_ROW
369  std::cout << std::endl;
370 #endif
371  return *_prow;
372 }
373 
374 template <typename T> inline Tmatrix<T>&
376 ( const Tmatrix<T>&M )
377 {
378  if( M._nr!=_nr || M._nc!=_nc )
379  resize( M._nr, M._nc );
380  for( unsigned int ie=0; ie!=_nr*_nc; ie++ ){
381  _val(ie) = M._val(ie);
382  }
383  return *this;
384 }
385 
386 template <typename T> inline Tmatrix<T>&
387 Tmatrix<T>::operator=
388 ( const T&m )
389 {
390  for( unsigned int ie=0; ie!=_nr*_nc; ie++ ){
391  _val(ie) = m;
392  }
393  return *this;
394 }
395 
396 template <typename T> template <typename U> inline Tmatrix<T>&
397 Tmatrix<T>::operator+=
398 ( const Tmatrix<U>&M )
399 {
400  ASSERT( M._nr==_nr && M._nc==_nc );
401  for( unsigned int ie=0; ie!=_nr*_nc; ie++ )
402  _val(ie) += M._val(ie);
403  return *this;
404 }
405 
406 template <typename T> template <typename U> inline Tmatrix<T>&
407 Tmatrix<T>::operator+=
408 ( const U&m )
409 {
410  for( unsigned int ie=0; ie!=_nr*_nc; ie++ )
411  _val(ie) += m;
412  return *this;
413 }
414 
415 template <typename T> template <typename U> inline Tmatrix<T>&
416 Tmatrix<T>::operator-=
417 ( const Tmatrix<U>&M )
418 {
419  ASSERT( M._nr==_nr && M._nc==_nc );
420  for( unsigned int ie=0; ie!=_nr*_nc; ie++ )
421  _val(ie) -= M._val(ie);
422  return *this;
423 }
424 
425 template <typename T> template <typename U> inline Tmatrix<T>&
426 Tmatrix<T>::operator-=
427 ( const U&m )
428 {
429  for( unsigned int ie=0; ie!=_nr*_nc; ie++ )
430  _val(ie) -= m;
431  return *this;
432 }
433 
434 template <typename T> template <typename U> inline Tmatrix<T>&
435 Tmatrix<T>::operator*=
436 ( const Tmatrix<U>&M )
437 {
438  ASSERT( M._nr==M._nc && M._nr==_nc );
439  for( unsigned int ir=0; ir<_nr; ir++ ){
440  Tmatrix<T> irow = row(ir);
441  for( unsigned int ic=0; ic<_nc; ic++ ){
442  _val(ir,ic) = irow(0)*M(0,ic);
443  for( unsigned int k=1; k<_nc; k++ ){
444  _val(ir,ic) += irow(k)*M(k,ic);
445  }
446  }
447  }
448  return *this;
449 }
450 
451 template <typename T> template <typename U> inline Tmatrix<T>&
452 Tmatrix<T>::operator*=
453 ( const U&m )
454 {
455  for( unsigned int ie=0; ie!=_nr*_nc; ie++ )
456  _val(ie) *= m;
457  return *this;
458 }
459 
460 template <typename T> template <typename U> inline Tmatrix<T>&
461 Tmatrix<T>::operator/=
462 ( const U&m )
463 {
464  for( unsigned int ie=0; ie!=_nr*_nc; ie++ )
465  _val(ie) /= m;
466  return *this;
467 }
468 
469 template <typename T> inline Tmatrix<T>
470 operator+
471 ( const Tmatrix<T>&M )
472 {
473  return M;
474 }
475 
476 template <typename T, typename U> inline Tmatrix<T>
477 operator+
478 ( const Tmatrix<T>&M, const Tmatrix<U>&N )
479 {
480  Tmatrix<T> P( M );
481  P += N;
482  return P;
483 }
484 
485 template <typename T, typename U> inline Tmatrix<T>
486 operator+
487 ( const Tmatrix<T>&M, const U&n )
488 {
489  Tmatrix<T> P( M );
490  P += n;
491  return P;
492 }
493 
494 template <typename T, typename U> inline Tmatrix<T>
495 operator+
496 ( const U&m, const Tmatrix<T>&N )
497 {
498  return N+m;
499 }
500 
501 template <typename T> inline Tmatrix<T>
502 operator-
503 ( const Tmatrix<T>&M )
504 {
505  return -M;
506 }
507 
508 template <typename T, typename U> inline Tmatrix<T>
509 operator-
510 ( const Tmatrix<T>&M, const Tmatrix<U>&N )
511 {
512  Tmatrix<T> P( M );
513  P -= N;
514  return P;
515 }
516 
517 template <typename T, typename U> inline Tmatrix<T>
518 operator-
519 ( const Tmatrix<T>&M, const U&n )
520 {
521  Tmatrix<T> P( M );
522  P -= n;
523  return P;
524 }
525 
526 template <typename T, typename U> inline Tmatrix<T>
527 operator-
528 ( const T&m, const Tmatrix<U>&N )
529 {
530  Tmatrix<T> P( -N );
531  P += m;
532  return P;
533 }
534 
535 template <typename T, typename U> inline Tmatrix<T>
536 operator*
537 ( const Tmatrix<T>&M, const Tmatrix<U>&N )
538 {
539  ASSERT( M._nc==N._nr );
540  Tmatrix<T> P( M._nr, N._nc );
541  for( unsigned int ir=0; ir<M._nr; ir++ ){
542  for( unsigned int ic=0; ic<N._nc; ic++ ){
543  P(ir,ic) = M(ir,0)*N(0,ic);
544  for( unsigned int k=1; k<M._nc; k++ ){
545  P(ir,ic) += M(ir,k)*N(k,ic);
546  }
547  }
548  }
549  return P;
550 }
551 
552 template <typename T, typename U> inline Tmatrix<T>
553 operator*
554 ( const Tmatrix<T>&M, const U&n )
555 {
556  Tmatrix<T> P( M );
557  P *= n;
558  return P;
559 }
560 
561 template <typename T, typename U> inline Tmatrix<T>
562 operator*
563 ( const U&m, const Tmatrix<T>&N )
564 {
565  return N*m;
566 }
567 
568 template <typename T, typename U> inline Tmatrix<T>
569 operator/
570 ( const Tmatrix<T>&M, const U&n )
571 {
572  Tmatrix<T> P( M );
573  P /= n;
574  return P;
575 }
576 
577 template <typename T> inline std::ostream&
578 operator<<
579 ( std::ostream& os, const Tmatrix<T>&M )
580 {
581  for( unsigned int ir=0; ir<M._nr; ir++ )
582  for( unsigned int ic=0; ic<M._nc; ic++ )
583  os << ir << " , " << ic << " : " << M(ir,ic) << std::endl;
584  return os;
585 }
586 
587 template <typename T> inline T*
589 {
590  T*pM = new T[_nr*_nc];
591  for( unsigned int ic=0, ie=0; ic<_nc; ic++ )
592  for( unsigned int ir=0; ir<_nr; ir++, ie++ )
593  pM[ie] = _val (ir,ic);
594  return pM;
595 }
596 
597 template <typename T> inline void
599 ( const T*pM )
600 {
601  for( unsigned int ic=0, ie=0; ic<_nc; ic++ )
602  for( unsigned int ir=0; ir<_nr; ir++, ie++ )
603  _val(ir,ic) = pM[ie];
604 }
605 
606 
607 template <typename T> inline unsigned int
609 ( unsigned int n )
610 {
611  if( n == 0 ) return 1;
612  int l = 0;
613  while( n > 0 ){
614  ++l;
615  n /= 10;
616  }
617  return l;
618 }
619 
620 
621 
623 
624 
625 #endif // ACADO_TOOLKIT_T_MATRIX_HPP
626 
627 /*
628  * end of file
629  */
#define N
Implements a templated dense matrix class.
Definition: t_matrix.hpp:53
~Tmatrix()
Destructor.
Definition: t_matrix.hpp:139
Tmatrix()
Default constructor.
Definition: t_matrix.hpp:90
USING_NAMESPACE_ACADO typedef TaylorVariable< Interval > T
Tmatrix< T > * _prow
Pointer to Tmatrix<T> container storing row.
Definition: t_matrix.hpp:231
unsigned int getNumCols() const
Definition: t_matrix.hpp:186
#define CLOSE_NAMESPACE_ACADO
T & _val(const unsigned int ir, const unsigned int ic)
Returns a reference to actual value at position ie.
Definition: t_matrix.hpp:286
bool _sub
Flag indicating whether the current object is a submatrix.
Definition: t_matrix.hpp:227
Tmatrix< unsigned int > sort()
Other operators.
static unsigned int _digits(unsigned int n)
Returns the number of digits of an unsigned int value.
Definition: t_matrix.hpp:609
Tmatrix< T > * _pblock
Pointer to Tmatrix<T> container storing blocks.
Definition: t_matrix.hpp:233
Tmatrix< T > * _pcol
Pointer to Tmatrix<T> container storing column.
Definition: t_matrix.hpp:229
unsigned int getDim() const
Definition: t_matrix.hpp:184
#define v
T *& _pval(const unsigned int ie)
Returns a pointer to actual value at position ie.
Definition: t_matrix.hpp:323
#define ASSERT(x)
unsigned int getNumRows() const
Definition: t_matrix.hpp:185
void _reset()
Resets the entries in vector _data.
Definition: t_matrix.hpp:330
T *& pval(const unsigned int ir, const unsigned int ic)
Sets/retrieves pointer to entry (ir,ic)
Definition: t_matrix.hpp:316
std::vector< T * > _data
Vector of pointers to elements (column-wise storage)
Definition: t_matrix.hpp:225
unsigned int col() const
Retrieves number of columns.
Definition: t_matrix.hpp:173
#define BEGIN_NAMESPACE_ACADO
unsigned int _nr
Number of rows.
Definition: t_matrix.hpp:221
unsigned int _nc
Number of columns.
Definition: t_matrix.hpp:223
T * array() const
Converts a matrix into array form (columnwise storage)
Definition: t_matrix.hpp:588
unsigned int row() const
Retrieves number of rows.
Definition: t_matrix.hpp:176
void resize(const unsigned int nr, const unsigned int nc=1, const bool alloc=true)
Resets the dimension of a Tmatrix objects.
Definition: t_matrix.hpp:149


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:35:12