33 #ifndef ACADO_TOOLKIT_T_MATRIX_HPP 34 #define ACADO_TOOLKIT_T_MATRIX_HPP 56 template <
typename U>
friend class Tmatrix;
57 template <
typename U>
friend std::ostream&
operator<<
59 template <
typename U>
friend Tmatrix<U> operator+
61 template <
typename U,
typename V>
friend Tmatrix<U> operator+
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+
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-
73 template <
typename U,
typename V>
friend Tmatrix<U>
operator-
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*
81 template <
typename U,
typename V>
friend Tmatrix<U>
operator/
82 (
const Tmatrix<U>&,
const V& );
95 (
const unsigned int nr,
const unsigned int nc=1,
const bool alloc=
true ):
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 );
106 (
const unsigned int nr,
const unsigned int nc,
const U&
v,
107 const bool alloc=
true ):
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 );
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 );
129 (
const Tmatrix<U>&M ):
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 );
149 (
const unsigned int nr,
const unsigned int nc=1,
const bool alloc=
true )
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 );
165 (
const unsigned int ic );
168 (
const unsigned int ir );
171 (
const unsigned int ir,
const unsigned int ic );
192 (
const unsigned int ir,
const unsigned int ic );
194 (
const unsigned int ir,
const unsigned int ic )
const;
196 (
const unsigned int ie );
198 (
const unsigned int ie )
const;
200 (
const Tmatrix<T>&
M );
201 Tmatrix<T>&
operator=
203 template <
typename U> Tmatrix<T>&
operator+=
204 (
const Tmatrix<U>&
M );
205 template <
typename U> Tmatrix<T>&
operator+=
207 template <
typename U> Tmatrix<T>&
operator-=
208 (
const Tmatrix<U>&
M );
209 template <
typename U> Tmatrix<T>&
operator-=
211 template <
typename U> Tmatrix<T>&
operator*=
212 (
const Tmatrix<U>&
M );
213 template <
typename U> Tmatrix<T>&
operator*=
215 template <
typename U> Tmatrix<T>&
operator/=
237 (
const unsigned int ir,
const unsigned int ic );
239 (
const unsigned int ir,
const unsigned int ic )
const;
241 (
const unsigned int ie );
243 (
const unsigned int ie )
const;
246 (
const unsigned int ie );
256 template <
typename T>
inline T&
258 (
const unsigned int ir,
const unsigned int ic )
263 template <
typename T>
inline const T&
265 (
const unsigned int ir,
const unsigned int ic )
const 270 template <
typename T>
inline T&
272 (
const unsigned int ie )
277 template <
typename T>
inline const T&
279 (
const unsigned int ie )
const 284 template <
typename T>
inline T&
286 (
const unsigned int ir,
const unsigned int ic )
291 template <
typename T>
inline const T&
293 (
const unsigned int ir,
const unsigned int ic )
const 298 template <
typename T>
inline T&
300 (
const unsigned int ie )
302 ASSERT( ie<_nr*_nc && ie>=0 );
306 template <
typename T>
inline const T&
308 (
const unsigned int ie )
const 310 ASSERT( ie<_nr*_nc && ie>=0 );
314 template <
typename T>
inline T*&
316 (
const unsigned int ir,
const unsigned int ic )
321 template <
typename T>
inline T*&
323 (
const unsigned int ie )
325 ASSERT( ie<_nr*_nc && ie>=0 );
329 template <
typename T>
inline void 332 typename std::vector<T*>::iterator it =
_data.begin();
333 for( ; it!=
_data.end(); it++ )
delete *it;
338 (
const unsigned int ic )
340 ASSERT( ic<_nc && ic>=0 );
342 for(
unsigned int ir=0; ir<
_nr; ir++ ){
344 #ifdef DEBUG__MATRIX_COL 345 std::cout << ir <<
" , 0 : " <<
pval(ir,ic) <<
" " 346 <<
_pcol->pval(ir,0) << std::endl;
349 #ifdef DEBUG__MATRIX_COL 350 std::cout << std::endl;
357 (
const unsigned int ir )
359 ASSERT( ir<_nr && ir>=0 );
361 for(
unsigned int ic=0; ic<
_nc; ic++ ){
363 #ifdef DEBUG__MATRIX_ROW 364 std::cout <<
"0 , " << ic <<
" : " <<
pval(ir,ic) <<
" " 365 <<
_prow->pval(0,ic) << std::endl;
368 #ifdef DEBUG__MATRIX_ROW 369 std::cout << std::endl;
380 for(
unsigned int ie=0; ie!=
_nr*
_nc; ie++ ){
381 _val(ie) =
M._val(ie);
387 Tmatrix<T>::operator=
390 for(
unsigned int ie=0; ie!=
_nr*
_nc; ie++ ){
396 template <
typename T>
template <
typename U>
inline Tmatrix<T>&
397 Tmatrix<T>::operator+=
398 (
const Tmatrix<U>&
M )
401 for(
unsigned int ie=0; ie!=
_nr*
_nc; ie++ )
402 _val(ie) +=
M._val(ie);
406 template <
typename T>
template <
typename U>
inline Tmatrix<T>&
407 Tmatrix<T>::operator+=
410 for(
unsigned int ie=0; ie!=
_nr*
_nc; ie++ )
415 template <
typename T>
template <
typename U>
inline Tmatrix<T>&
416 Tmatrix<T>::operator-=
417 (
const Tmatrix<U>&
M )
420 for(
unsigned int ie=0; ie!=
_nr*
_nc; ie++ )
421 _val(ie) -=
M._val(ie);
425 template <
typename T>
template <
typename U>
inline Tmatrix<T>&
426 Tmatrix<T>::operator-=
429 for(
unsigned int ie=0; ie!=
_nr*
_nc; ie++ )
434 template <
typename T>
template <
typename U>
inline Tmatrix<T>&
435 Tmatrix<T>::operator*=
436 (
const Tmatrix<U>&
M )
439 for(
unsigned int ir=0; ir<
_nr; 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);
451 template <
typename T>
template <
typename U>
inline Tmatrix<T>&
452 Tmatrix<T>::operator*=
455 for(
unsigned int ie=0; ie!=
_nr*
_nc; ie++ )
460 template <
typename T>
template <
typename U>
inline Tmatrix<T>&
461 Tmatrix<T>::operator/=
464 for(
unsigned int ie=0; ie!=
_nr*
_nc; ie++ )
476 template <
typename T,
typename U>
inline Tmatrix<T> 485 template <
typename T,
typename U>
inline Tmatrix<T> 494 template <
typename T,
typename U>
inline Tmatrix<T> 508 template <
typename T,
typename U>
inline Tmatrix<T> 517 template <
typename T,
typename U>
inline Tmatrix<T> 526 template <
typename T,
typename U>
inline Tmatrix<T> 528 (
const T&m,
const Tmatrix<U>&
N )
535 template <
typename T,
typename U>
inline Tmatrix<T> 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);
552 template <
typename T,
typename U>
inline Tmatrix<T> 561 template <
typename T,
typename U>
inline Tmatrix<T> 568 template <
typename T,
typename U>
inline Tmatrix<T> 577 template <
typename T>
inline std::ostream&
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;
587 template <
typename T>
inline T*
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);
597 template <
typename T>
inline void 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];
607 template <
typename T>
inline unsigned int 611 if( n == 0 )
return 1;
625 #endif // ACADO_TOOLKIT_T_MATRIX_HPP
Implements a templated dense matrix class.
Tmatrix()
Default constructor.
USING_NAMESPACE_ACADO typedef TaylorVariable< Interval > T
Tmatrix< T > * _prow
Pointer to Tmatrix<T> container storing row.
unsigned int getNumCols() const
#define CLOSE_NAMESPACE_ACADO
T & _val(const unsigned int ir, const unsigned int ic)
Returns a reference to actual value at position ie.
bool _sub
Flag indicating whether the current object is a submatrix.
Tmatrix< unsigned int > sort()
Other operators.
static unsigned int _digits(unsigned int n)
Returns the number of digits of an unsigned int value.
Tmatrix< T > * _pblock
Pointer to Tmatrix<T> container storing blocks.
Tmatrix< T > * _pcol
Pointer to Tmatrix<T> container storing column.
unsigned int getDim() const
T *& _pval(const unsigned int ie)
Returns a pointer to actual value at position ie.
unsigned int getNumRows() const
void _reset()
Resets the entries in vector _data.
T *& pval(const unsigned int ir, const unsigned int ic)
Sets/retrieves pointer to entry (ir,ic)
std::vector< T * > _data
Vector of pointers to elements (column-wise storage)
unsigned int col() const
Retrieves number of columns.
#define BEGIN_NAMESPACE_ACADO
unsigned int _nr
Number of rows.
unsigned int _nc
Number of columns.
T * array() const
Converts a matrix into array form (columnwise storage)
unsigned int row() const
Retrieves number of rows.
void resize(const unsigned int nr, const unsigned int nc=1, const bool alloc=true)
Resets the dimension of a Tmatrix objects.