matrix.cpp
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 
35 #include <iomanip>
36 
37 using namespace std;
38 
40 
41 template<typename T>
43  unsigned _nCols,
44  std::vector< std::vector< T > >& _values
45  )
46  : Base(_nRows, _nCols)
47 {
48  ASSERT( _values.size() > 0 );
49 
50  unsigned nRows = _values.size();
51  unsigned nCols = _values[ 0 ].size();
52 
53  for (unsigned r = 0; r < nRows; ++r)
54  {
55  ASSERT( _values[ r ].size() == nCols );
56 
57  std::copy(_values[ r ].begin(), _values[ r ].end(), Base::data() + r * nCols);
58  }
59 }
60 
61 template<typename T>
63 )
64 {
65  if (getDim() == 0)
66  {
67  Base::_set( _arg );
68  return *this;
69  }
70 
71  ASSERT(Base::cols() == _arg.cols());
72 
73  unsigned oldRows = Base::rows();
74  unsigned argRows = _arg.rows();
75 
76  Base::conservativeResize(oldRows + argRows, Base::cols());
77  Base::block(oldRows, 0, argRows, Base::cols()) = _arg;
78 
79  return *this;
80 }
81 
82 template<typename T>
84 )
85 {
86  if (getDim() == 0)
87  {
88  Base::_set( _arg );
89  return *this;
90  }
91 
92  ASSERT(Base::rows() == _arg.rows());
93 
94  unsigned oldCols = Base::cols();
95  unsigned argCols = _arg.cols();
96 
97  Base::conservativeResize(Base::rows(), oldCols + argCols);
98  Base::block(0, oldCols, Base::rows(), argCols) = _arg;
99 
100  return *this;
101 }
102 
103 template<typename T>
105 {
107  for (unsigned r = 0; r < Base::rows(); ++r)
108  foo( r ) = Base::row( r ).sum();
109 
110  return foo;
111 }
112 
113 template<typename T>
115 {
117  for (unsigned c = 0; c < Base::cols(); ++c)
118  foo( c ) = Base::col( c ).sum();
119 
120  return foo;
121 }
122 
123 template<typename T>
125 {
127  return *this;
128 }
129 
130 template<typename T>
132 { return Base::diagonal(); }
133 
134 template<typename T>
136 { return Base::rows() == Base::cols(); }
137 
138 template<typename T>
140 { return Base::operator==(Base::transpose()); }
141 
142 template<>
144 {
145  if ( isSquare( ) == BT_FALSE )
146  return BT_FALSE;
147 
148  for (unsigned r = 0; r < getNumRows(); ++r)
149  for (unsigned c = r + 1; c < getNumRows(); ++c)
150  if (acadoIsEqual(Base::operator()(r, c), Base::operator()(c, r)) == false)
151  return false;
152  return true;
153 }
154 
155 template<typename T>
157 {
158  if ( isSquare( ) == false )
160 
161  for (unsigned i = 0; i < getNumRows(); ++i)
162  for (unsigned j = i + 1; j < getNumRows(); ++j)
163  {
164  T m = (Base::operator()(i, j) + Base::operator()(j, i)) / T( 2 );
165  Base::operator()(i, j) = m;
166  Base::operator()(j, i) = m;
167  }
168 
169  return SUCCESSFUL_RETURN;
170 }
171 
172 template<typename T>
174 {
175  Eigen::LDLT< Base > foo( Base::size() );
176  foo.compute( *this );
177  if (foo.info() == Eigen::Success && foo.isPositive() == true)
178  return true;
179 
180  return false;
181 }
182 
183 template<typename T>
185 {
186  if (this->llt().info() == Eigen::Success)
187  return true;
188 
189  return false;
190 }
191 
192 template<typename T>
194 { return Base::cwiseAbs(); }
195 
196 template<typename T>
198 {
199  GenericMatrix foo = *this;
200  unsigned dim = foo.rows() * foo.cols();
201  for (unsigned el = 0; el < dim; ++el)
202  {
203  T bar = foo.data()[ el ];
204  if (bar < T( 0 ))
205  foo.data()[ el ] = T( 0 );
206  }
207 
208  return foo;
209 }
210 
211 template<typename T>
213 {
214  GenericMatrix foo = *this;
215  unsigned dim = foo.rows() * foo.cols();
216  for (unsigned el = 0; el < dim; ++el)
217  {
218  T bar = foo.data()[ el ];
219  if (bar > T( 0 ))
220  foo.data()[ el ] = T( 0 );
221  }
222 
223  return foo;
224 }
225 
226 template<typename T>
228 { return Base::norm(); }
229 
230 template<typename T>
232 { return Base::trace(); }
233 
234 template<typename T>
236 {
237  ASSERT(1 == 0);
238 
239  return T( 0 );
240 }
241 
242 template<>
244 {
245  ASSERT(isSquare() == true);
246 
249 
250  return (V( 0 ) / V(V.size() - 1));
251 }
252 
253 template<typename T>
254 returnValue GenericMatrix< T >::print( std::ostream& _stream,
255  const std::string& _name,
256  const std::string& _startString,
257  const std::string& _endString,
258  uint _width,
259  uint _precision,
260  const std::string& _colSeparator,
261  const std::string& _rowSeparator
262  ) const
263 {
264  if (_name.size())
265  _stream << _name << " = ";
266 
267  _stream << _startString;
268 
269  for (unsigned r = 0; r < Base::rows(); ++r)
270  {
271  for (unsigned c = 0; c < Base::cols(); ++c)
272  {
273  _stream << Base::operator()(r, c);
274 
275  if (c < (Base::cols() - 1))
276  _stream << _colSeparator;
277  }
278 
279  if (r < (Base::rows() - 1))
280  _stream << _rowSeparator;
281  }
282 
283  _stream << _endString;
284 
285  return SUCCESSFUL_RETURN;
286 }
287 
288 template<>
289 returnValue GenericMatrix< double >::print( std::ostream& _stream,
290  const std::string& _name,
291  const std::string& _startString,
292  const std::string& _endString,
293  uint _width,
294  uint _precision,
295  const std::string& _colSeparator,
296  const std::string& _rowSeparator
297  ) const
298 {
299  IoFormatter iof( _stream );
300 
301  if (_name.size())
302  _stream << _name << " = ";
303 
304  _stream << _startString;
305 
306  iof.set(_precision,
307  _width,
308  _precision > 0 ? ios::scientific | ios::right : ios::fixed);
309 
310  for (unsigned r = 0; r < Base::rows(); ++r)
311  {
312  for (unsigned c = 0; c < Base::cols(); ++c)
313  {
314  _stream << Base::operator()(r, c);
315 
316  if (c < (Base::cols() - 1))
317  _stream << _colSeparator;
318  }
319 
320  if (r < (Base::rows() - 1))
321  _stream << _rowSeparator;
322  }
323 
324  _stream << _endString;
325 
326  iof.reset();
327 
328  return SUCCESSFUL_RETURN;
329 }
330 
331 template<typename T>
332 returnValue GenericMatrix< T >::print( std::ostream& _stream,
333  const std::string& _name,
334  PrintScheme _printScheme
335  ) const
336 {
337  MatFile< T >* matFile;
338 
339  switch ( _printScheme )
340  {
341  case PS_MATLAB_BINARY:
342  matFile = new MatFile<T>();
343 
344  matFile->write(_stream, *this, _name.c_str());
345 
346  delete matFile;
347 
348  return SUCCESSFUL_RETURN;
349 
350  default:
351  char* startString = 0;
352  char* endString = 0;
353  uint width = 0;
354  uint precision = 0;
355  char* colSeparator = 0;
356  char* rowSeparator = 0;
357 
358  returnValue ret = getGlobalStringDefinitions(_printScheme, &startString,
359  &endString, width, precision, &colSeparator, &rowSeparator);
360  if (ret != SUCCESSFUL_RETURN)
361  return ret;
362 
363  returnValue status = print(_stream, _name, startString, endString, width,
364  precision, colSeparator, rowSeparator);
365 
366  if ( startString != 0 ) delete[] startString;
367  if ( endString != 0 ) delete[] endString;
368  if ( colSeparator != 0 ) delete[] colSeparator;
369  if ( rowSeparator != 0 ) delete[] rowSeparator;
370 
371  return status;
372  }
373 }
374 
375 template<typename T>
376 returnValue GenericMatrix<T>::print( const std::string& _filename,
377  const std::string& _name,
378  const std::string& _startString,
379  const std::string& _endString,
380  uint _width,
381  uint _precision,
382  const std::string& _colSeparator,
383  const std::string& _rowSeparator
384  ) const
385 {
386  ofstream stream( _filename.c_str() );
387 
388  if ( stream.is_open() )
389  return print(stream, _name, _startString, _endString, _width, _precision,
390  _colSeparator, _rowSeparator);
391  else
393 
394  stream.close();
395 
396  return SUCCESSFUL_RETURN;
397 }
398 
399 template<typename T>
400 returnValue GenericMatrix< T >::print( const std::string& _filename,
401  const std::string& _name,
402  PrintScheme _printScheme
403  ) const
404 {
405  ofstream stream( _filename.c_str() );
406  returnValue status;
407 
408  if ( stream.is_open() )
409  status = print(stream, _name, _printScheme);
410  else
412 
413  stream.close();
414 
415  return status;
416 }
417 
418 template<typename T>
419 returnValue GenericMatrix< T >::read( std::istream& _stream )
420 {
421  vector< vector< T > > tmp;
422  _stream >> tmp;
423 
424  if (tmp.size() == 0)
425  {
427  return SUCCESSFUL_RETURN;
428  }
429 
430  // Sanity check
431  unsigned nc = tmp[ 0 ].size();
432  unsigned nr = tmp.size();
433  for (unsigned r = 0; r < nr; ++r)
434  if (tmp[ r ].size() != nc)
436 
437  Base::_set(GenericMatrix< T >(nr, nc, tmp));
438 
439  return SUCCESSFUL_RETURN;
440 }
441 
442 template<typename T>
443 returnValue GenericMatrix< T >::read(const std::string& _filename)
444 {
445  ifstream stream( _filename.c_str() );
446  returnValue status;
447 
448  if (stream.is_open())
449  status = read( stream );
450  else
452 
453  stream.close();
454 
455  return status;
456 }
457 
458 //
459 // Explicit instantiation of templates.
460 //
461 template class GenericMatrix<double>;
462 template class GenericMatrix<int>;
463 template class GenericMatrix<bool>;
464 
returnValue getGlobalStringDefinitions(PrintScheme _printScheme, char **_startString, char **_endString, uint &_width, uint &_precision, char **_colSeparator, char **_rowSeparator)
Robust Cholesky decomposition of a matrix with pivoting.
Definition: LDLT.h:45
USING_NAMESPACE_ACADO typedef TaylorVariable< Interval > T
GenericVector< T > sumCol() const
Definition: matrix.cpp:104
bool isPositiveSemiDefinite() const
Definition: matrix.cpp:173
EIGEN_STRONG_INLINE void conservativeResize(Index nbRows, Index nbCols)
T getTrace() const
Definition: matrix.cpp:231
BooleanType acadoIsEqual(double x, double y, double TOL)
Definition: acado_utils.cpp:88
GenericMatrix & appendCols(const GenericMatrix &_arg)
Definition: matrix.cpp:83
bool isSquare() const
Definition: matrix.cpp:135
T getNorm() const
Definition: matrix.cpp:227
Allows to pass back messages to the calling function.
std::string operator==(const ExportIndex &_arg1, const ExportIndex &_arg2)
GenericMatrix & makeVector()
Definition: matrix.cpp:124
Block< Derived > block(Index startRow, Index startCol, Index blockRows, Index blockCols)
Definition: BlockMethods.h:56
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
const SingularValuesType & singularValues() const
Definition: JacobiSVD.h:630
EIGEN_STRONG_INLINE const CwiseUnaryOp< internal::scalar_abs_op< Scalar >, const Derived > cwiseAbs() const
GenericMatrix & appendRows(const GenericMatrix &_arg)
Definition: matrix.cpp:62
Simple class for writing binary data file that are compatible with Matlab.
#define CLOSE_NAMESPACE_ACADO
EIGEN_STRONG_INLINE Index rows() const
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
unsigned getDim() const
Definition: matrix.hpp:181
T getConditionNumber() const
Definition: matrix.cpp:235
GenericMatrix positive() const
Definition: matrix.cpp:197
bool isPositiveDefinite() const
Definition: matrix.cpp:184
PrintScheme
void set(streamsize _precision, streamsize _width, ios_base::fmtflags _flags)
EIGEN_STRONG_INLINE const Scalar * data() const
virtual returnValue read(std::istream &_stream)
Definition: matrix.cpp:419
unsigned getNumRows() const
Definition: matrix.hpp:185
bool isPositive() const
Definition: LDLT.h:139
#define ASSERT(x)
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: LDLT.h:221
EIGEN_STRONG_INLINE Derived & _set(const DenseBase< OtherDerived > &other)
Copies the value of the expression other into *this with automatic resizing.
RowXpr row(Index i)
Definition: BlockMethods.h:725
GenericMatrix absolute() const
Definition: matrix.cpp:193
GenericVector< T > sumRow() const
Definition: matrix.cpp:114
Two-sided Jacobi SVD decomposition of a rectangular matrix.
#define BEGIN_NAMESPACE_ACADO
void write(std::ostream &stream, const GenericMatrix< T > &mat, const char *name)
#define BT_FALSE
Definition: acado_types.hpp:49
ColXpr col(Index i)
Definition: BlockMethods.h:708
EIGEN_STRONG_INLINE Index cols() const
GenericVector< T > getDiag() const
Definition: matrix.cpp:131
returnValue symmetrize()
Definition: matrix.cpp:156
LDLT & compute(const MatrixType &matrix)
Definition: LDLT.h:437
GenericMatrix negative() const
Definition: matrix.cpp:212
#define ACADOERROR(retval)


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