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 
34 #ifndef ACADO_TOOLKIT_MATRIX_HPP
35 #define ACADO_TOOLKIT_MATRIX_HPP
36 
37 #include <memory>
38 
40 
42 
44 template<typename T>
45 class GenericMatrix : public Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor | Eigen::AutoAlign>
46 {
47 public:
48 
56 
57 
59  template<typename OtherDerived>
60  inline GenericMatrix(const Eigen::MatrixBase<OtherDerived>& other) : Base( other ) {}
61 
63  template<typename OtherDerived>
64  inline GenericMatrix(const Eigen::ReturnByValue<OtherDerived>& other) : Base( other ) {}
65 
67  template<typename OtherDerived>
68  inline GenericMatrix(const Eigen::EigenBase<OtherDerived>& other) : Base( other ) {}
69 
78  GenericMatrix() : Base() {}
79 
81  GenericMatrix( const T& _value
82  )
83  : Base(1, 1)
84  { Base::data()[ 0 ] = _value; }
85 
87  GenericMatrix( unsigned _nRows,
88  unsigned _nCols
89  )
90  : Base(_nRows, _nCols)
91  { Base::setZero(); }
92 
94  GenericMatrix( unsigned _nRows,
95  unsigned _nCols,
96  const T* const _values
97  )
98  : Base(_nRows, _nCols)
99  { std::copy(_values, _values + _nRows * _nCols, Base::data()); }
100 
102  GenericMatrix( unsigned _nRows,
103  unsigned _nCols,
104  std::vector< T >& _values)
105  : Base(_nRows, _nCols)
106  { std::copy(_values.begin(), _values.end(), Base::data()); }
107 
109  GenericMatrix( unsigned _nRows,
110  unsigned _nCols,
111  std::vector< std::vector< T > >& _values
112  );
113 
117  virtual ~GenericMatrix()
118  {}
119 
121  bool operator==(const GenericMatrix& arg) const
122  {
123  if (Base::rows() != arg.rows() || Base::cols() != arg.cols())
124  return false;
125  return Base::isApprox(arg, EQUALITY_EPS);
126  }
127 
129  bool operator!=(const GenericMatrix& arg) const
130  {
131  return (operator==( arg ) == false);
132  }
133 
135  void init( unsigned _nRows = 0,
136  unsigned _nCols = 0
137  )
138  { Base::_set(GenericMatrix<T>(_nRows, _nCols)); }
139 
141  void setAll( const T& _value)
142  { Base::setConstant( _value ); }
143 
145  GenericMatrix& appendRows( const GenericMatrix& _arg
146  );
147 
149  GenericMatrix& appendCols( const GenericMatrix& _arg
150  );
151 
162  GenericVector< T > sumCol() const;
163 
175  GenericVector< T > sumRow() const;
176 
178  GenericMatrix& makeVector( );
179 
181  unsigned getDim( ) const
182  { return (Base::rows() * Base::cols()); }
183 
185  unsigned getNumRows( ) const
186  { return Base::rows(); }
187 
189  unsigned getNumCols( ) const
190  { return Base::cols(); }
191 
193  bool isEmpty( ) const
194  { return Base::rows() == 0 || Base::cols() == 0; }
195 
197  GenericVector< T > getRow( unsigned _idx
198  ) const
199  {
200  ASSERT( _idx < Base::rows() );
201  return Base::row( _idx ).transpose();
202  }
203 
205  GenericVector< T > getCol( unsigned _idx
206  ) const
207  {
208  ASSERT( _idx < Base::cols() );
209  return Base::col( _idx );
210  }
211 
213  GenericMatrix& setRow( unsigned _idx,
214  const GenericVector< T >& _values
215  )
216  {
217  ASSERT(Base::cols() == _values.rows() && _idx < Base::rows());
218  Base::row( _idx ) = _values.transpose();
219 
220  return *this;
221  }
222 
224  GenericMatrix& setCol( unsigned _idx,
225  const GenericVector< T >& _arg
226  )
227  {
228  ASSERT(Base::rows() == _arg.rows() && _idx < Base::cols());
229  Base::col( _idx ) = _arg;
230 
231  return *this;
232  }
233 
235  GenericMatrix getRows( unsigned _start,
236  unsigned _end
237  ) const
238  {
239  if (_start >= Base::rows() || _end >= Base::rows() || _start > _end)
240  return GenericMatrix();
241 
242  return Base::block(_start, 0, _end - _start + 1, Base::cols());
243  }
244 
246  GenericMatrix getCols( unsigned _start,
247  unsigned _end
248  ) const
249  {
250  if (_start >= Base::cols() || _end >= Base::cols() || _start > _end)
251  return GenericMatrix();
252 
253  return Base::block(0, _start, Base::rows(), _end - _start + 1);
254  }
255 
257  GenericVector< T > getDiag( ) const;
258 
260  bool isSquare() const;
261 
263  bool isSymmetric( ) const;
264 
267 
271  bool isPositiveSemiDefinite( ) const;
272 
276  bool isPositiveDefinite( ) const;
277 
280  GenericMatrix absolute() const;
281 
285  GenericMatrix positive() const;
286 
290  GenericMatrix negative() const;
291 
293  T getMax( ) const
294  { return Base::maxCoeff(); }
295 
297  T getMin( ) const
298  { return Base::minCoeff(); }
299 
301  T getMean( ) const
302  { return Base::mean(); }
303 
306  { return Base::cwiseAbs(); }
307 
309  T getNorm( ) const;
310 
312  T getTrace( ) const;
313 
317  T getConditionNumber( ) const;
318 
335  virtual returnValue print( std::ostream& _stream = std::cout,
336  const std::string& _name = DEFAULT_LABEL,
337  const std::string& _startString = DEFAULT_START_STRING,
338  const std::string& _endString = DEFAULT_END_STRING,
339  uint _width = DEFAULT_WIDTH,
340  uint _precision = DEFAULT_PRECISION,
341  const std::string& _colSeparator = DEFAULT_COL_SEPARATOR,
342  const std::string& _rowSeparator = DEFAULT_ROW_SEPARATOR
343  ) const;
344 
356  virtual returnValue print( std::ostream& stream,
357  const std::string& name,
358  PrintScheme printScheme
359  ) const;
360 
377  virtual returnValue print( const std::string& _filename,
378  const std::string& _name = DEFAULT_LABEL,
379  const std::string& _startString = DEFAULT_START_STRING,
380  const std::string& _endString = DEFAULT_END_STRING,
381  uint _width = DEFAULT_WIDTH,
382  uint _precision = DEFAULT_PRECISION,
383  const std::string& _colSeparator = DEFAULT_COL_SEPARATOR,
384  const std::string& _rowSeparator = DEFAULT_ROW_SEPARATOR
385  ) const;
386 
398  virtual returnValue print( const std::string& _filename,
399  const std::string& _name,
400  PrintScheme _printScheme
401  ) const;
402 
404  virtual returnValue read( std::istream& _stream
405  );
406 
408  virtual returnValue read( const std::string& _filename
409  );
410 };
411 
413 template<typename T>
414 std::ostream& operator<<( std::ostream& _stream,
415  const GenericMatrix< T >& _arg
416  )
417 {
418  if (_arg.print( _stream ) != SUCCESSFUL_RETURN)
419  ACADOERRORTEXT(RET_INVALID_ARGUMENTS, "Cannot write to output stream.");
420 
421  return _stream;
422 }
423 
425 template<typename T>
426 std::istream& operator>>( std::istream& _stream,
427  GenericMatrix< T >& _arg
428 )
429 {
430  if (_arg.read( _stream ) != SUCCESSFUL_RETURN )
431  ACADOERRORTEXT(RET_INVALID_ARGUMENTS, "Cannot read from input stream.");
432 
433  return _stream;
434 }
435 
437 template<typename T>
438 GenericMatrix< T > ones( unsigned _nRows,
439  unsigned _nCols = 1
440  )
441 { return GenericMatrix< T >(_nRows, _nCols).setOnes(); }
442 
444 template<typename T>
445 GenericMatrix< T > zeros( unsigned _nRows,
446  unsigned _nCols = 1
447  )
448 { return GenericMatrix< T >(_nRows, _nCols).setZero(); }
449 
451 template<typename T>
452 GenericMatrix< T > eye( unsigned _dim
453  )
454 { return GenericMatrix< T >(_dim, _dim).setIdentity(); }
455 
463 typedef std::shared_ptr< GenericMatrix< double > > DMatrixPtr;
464 
467 
469 
471 namespace Eigen
472 {
473 namespace internal
474 {
475 template<typename T>
476 struct traits< ACADO::GenericMatrix< T > >
477  : traits<Matrix<T, Dynamic, Dynamic, Eigen::RowMajor | Eigen::AutoAlign> >
478 {};
479 
480 } // namespace internal
481 } // namespace Eigen
484 #endif // ACADO_TOOLKIT_MATRIX_HPP
const char DEFAULT_END_STRING[4]
GenericVector< T > getCol(unsigned _idx) const
Definition: matrix.hpp:205
GenericMatrix(const T &_value)
Definition: matrix.hpp:81
T getMax() const
Definition: matrix.hpp:293
bool isEmpty() const
Definition: matrix.hpp:193
USING_NAMESPACE_ACADO typedef TaylorVariable< Interval > T
GenericVector< T > sumCol() const
Definition: matrix.cpp:104
void init(unsigned _nRows=0, unsigned _nCols=0)
Definition: matrix.hpp:135
bool isPositiveSemiDefinite() const
Definition: matrix.cpp:173
T getTrace() const
Definition: matrix.cpp:231
GenericMatrix & appendCols(const GenericMatrix &_arg)
Definition: matrix.cpp:83
static const DMatrix emptyConstMatrix
Definition: matrix.hpp:466
bool isSquare() const
Definition: matrix.cpp:135
const uint DEFAULT_PRECISION
GenericMatrix< bool > BMatrix
Definition: matrix.hpp:461
GenericMatrix & setCol(unsigned _idx, const GenericVector< T > &_arg)
Definition: matrix.hpp:224
T getNorm() const
Definition: matrix.cpp:227
GenericMatrix(const Eigen::MatrixBase< OtherDerived > &other)
Definition: matrix.hpp:60
Allows to pass back messages to the calling function.
std::shared_ptr< GenericMatrix< double > > DMatrixPtr
Definition: matrix.hpp:463
GenericMatrix & makeVector()
Definition: matrix.cpp:124
const char DEFAULT_COL_SEPARATOR[2]
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: matrix.hpp:471
GenericMatrix(const Eigen::ReturnByValue< OtherDerived > &other)
Definition: matrix.hpp:64
Block< Derived > block(Index startRow, Index startCol, Index blockRows, Index blockCols)
Definition: BlockMethods.h:56
GenericMatrix< T > getAbsolute() const
Definition: matrix.hpp:305
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
void setAll(const T &_value)
Definition: matrix.hpp:141
GenericMatrix(unsigned _nRows, unsigned _nCols, const T *const _values)
Definition: matrix.hpp:94
GenericMatrix(unsigned _nRows, unsigned _nCols)
Definition: matrix.hpp:87
EIGEN_STRONG_INLINE const CwiseUnaryOp< internal::scalar_abs_op< Scalar >, const Derived > cwiseAbs() const
GenericMatrix< T > eye(unsigned _dim)
Definition: matrix.hpp:452
GenericMatrix & appendRows(const GenericMatrix &_arg)
Definition: matrix.cpp:62
GenericMatrix(unsigned _nRows, unsigned _nCols, std::vector< T > &_values)
Definition: matrix.hpp:102
std::ostream & operator<<(std::ostream &_stream, const GenericMatrix< T > &_arg)
Definition: matrix.hpp:414
#define CLOSE_NAMESPACE_ACADO
const char DEFAULT_START_STRING[3]
GenericMatrix< double > DMatrix
Definition: matrix.hpp:457
EIGEN_STRONG_INLINE Index rows() const
GenericMatrix getCols(unsigned _start, unsigned _end) const
Definition: matrix.hpp:246
static DMatrix emptyMatrix
Definition: matrix.hpp:465
virtual returnValue print(std::ostream &_stream=std::cout, const std::string &_name=DEFAULT_LABEL, const std::string &_startString=DEFAULT_START_STRING, const std::string &_endString=DEFAULT_END_STRING, uint _width=DEFAULT_WIDTH, uint _precision=DEFAULT_PRECISION, const std::string &_colSeparator=DEFAULT_COL_SEPARATOR, const std::string &_rowSeparator=DEFAULT_ROW_SEPARATOR) const
Definition: matrix.cpp:254
bool isSymmetric() const
Definition: matrix.cpp:139
bool isApprox(const Scalar &x, const Scalar &y, typename NumTraits< Scalar >::Real precision=NumTraits< Scalar >::dummy_precision())
unsigned getDim() const
Definition: matrix.hpp:181
T getConditionNumber() const
Definition: matrix.cpp:235
GenericMatrix & setRow(unsigned _idx, const GenericVector< T > &_values)
Definition: matrix.hpp:213
GenericMatrix positive() const
Definition: matrix.cpp:197
bool isPositiveDefinite() const
Definition: matrix.cpp:184
PrintScheme
std::istream & operator>>(std::istream &_stream, GenericMatrix< T > &_arg)
Definition: matrix.hpp:426
GenericMatrix< T > ones(unsigned _nRows, unsigned _nCols=1)
Definition: matrix.hpp:438
Derived & setZero(Index size)
Derived & setConstant(Index size, const Scalar &value)
GenericMatrix< int > IMatrix
Definition: matrix.hpp:459
T getMean() const
Definition: matrix.hpp:301
Derived & setOnes(Index size)
EIGEN_STRONG_INLINE const Scalar * data() const
virtual returnValue read(std::istream &_stream)
Definition: matrix.cpp:419
unsigned getNumRows() const
Definition: matrix.hpp:185
#define ASSERT(x)
EIGEN_STRONG_INLINE Derived & _set(const DenseBase< OtherDerived > &other)
Copies the value of the expression other into *this with automatic resizing.
T getMin() const
Definition: matrix.hpp:297
RowXpr row(Index i)
Definition: BlockMethods.h:725
GenericMatrix absolute() const
Definition: matrix.cpp:193
unsigned getNumCols() const
Definition: matrix.hpp:189
const uint DEFAULT_WIDTH
const double EQUALITY_EPS
GenericVector< T > sumRow() const
Definition: matrix.cpp:114
bool operator!=(const GenericMatrix &arg) const
Definition: matrix.hpp:129
const char DEFAULT_ROW_SEPARATOR[6]
#define BEGIN_NAMESPACE_ACADO
bool operator==(const GenericMatrix &arg) const
Definition: matrix.hpp:121
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor|Eigen::AutoAlign > Base
Definition: matrix.hpp:55
ColXpr col(Index i)
Definition: BlockMethods.h:708
GenericMatrix(const Eigen::EigenBase< OtherDerived > &other)
Definition: matrix.hpp:68
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
EIGEN_STRONG_INLINE Index cols() const
virtual ~GenericMatrix()
Definition: matrix.hpp:117
GenericVector< T > getDiag() const
Definition: matrix.cpp:131
returnValue symmetrize()
Definition: matrix.cpp:156
GenericMatrix< T > zeros(unsigned _nRows, unsigned _nCols=1)
Definition: matrix.hpp:445
const char DEFAULT_LABEL[1]
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
GenericMatrix negative() const
Definition: matrix.cpp:212
GenericMatrix getRows(unsigned _start, unsigned _end) const
Definition: matrix.hpp:235
GenericVector< T > getRow(unsigned _idx) const
Definition: matrix.hpp:197
#define ACADOERRORTEXT(retval, text)


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