export_variable_internal.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 
27 
36 
38 
39 using namespace std;
40 
41 static const double undefinedEntry = 1073741824.03125; // = 2^30 + 2^-5
42 
43 
44 //
45 // PUBLIC MEMBER FUNCTIONS:
46 //
47 
49 {
50  doAccessTransposed = false;
51 
52  rowOffset = 0;
53  colOffset = 0;
54 
55  rowDim = 0;
56  colDim = 0;
57  nRows = 0;
58  nCols = 0;
59 }
60 
62  const DMatrixPtr& _data,
63  ExportType _type,
64  ExportStruct _dataStruct,
65  bool _callItByValue,
66  const std::string& _prefix
67  )
68  : ExportArgumentInternal(_name, _data, _type, _dataStruct, _callItByValue, emptyConstExportIndex, _prefix)
69 {
70  doAccessTransposed = false;
71 
72  rowOffset = 0;
73  colOffset = 0;
74 
75  rowDim = _data->getNumRows();
76  colDim = _data->getNumCols();
77  nRows = 0;
78  nCols = 0;
79 }
80 
81 
83 {}
84 
85 
87 {
88  return new ExportVariableInternal( *this );
89 }
90 
92  const ExportIndex& colIdx
93  ) const
94 {
95  return hasValue(rowIdx, colIdx, 0.0);
96 }
97 
99  const ExportIndex& colIdx
100  ) const
101 {
102  return hasValue(rowIdx, colIdx, 1.0);
103 }
104 
105 
107  const ExportIndex& colIdx
108  ) const
109 {
110  if (hasValue(rowIdx, colIdx, undefinedEntry) == true)
111  return false;
112 
113  return true;
114 }
115 
117 {
119 }
120 
121 
122 const std::string ExportVariableInternal::get( const ExportIndex& rowIdx,
123  const ExportIndex& colIdx
124  ) const
125 {
126  stringstream s;
127 
128  IoFormatter iof( s );
129 
130  iof.set( getType() == INT || getType() == STATIC_CONST_INT ? 5 : 16,
131  iof.width,
132  getType() == INT || getType() == STATIC_CONST_INT ? ios::fixed : ios::scientific
133  );
134 
135  ExportIndex totalIdx = getTotalIdx(rowIdx + rowOffset, colIdx + colOffset);
136 
137  if ( ( totalIdx.isGiven() == true ) && ( rowIdx.isGiven() == true ) && ( colIdx.isGiven() == true ) )
138  {
139  if (isGiven(rowIdx, colIdx) == false)
140  {
141  if ( ( isCalledByValue() == true ) && ( totalIdx.getGivenValue() == 0 ) )
142  s << getFullName();
143  else
144  s << getFullName() << "[" << totalIdx.getGivenValue() << "]";
145  }
146  else
147  {
148  s << "(real_t)" << data->operator()(totalIdx.getGivenValue());
149  }
150  }
151  else
152  s << getFullName() << "[" << totalIdx.get( ) << "]";
153 
154  return s.str();
155 }
156 
157 
159 {
160  if ( nRows > 0 )
161  return nRows;
162 
163  return data->getNumRows( );
164 }
165 
166 
168 {
169  if ( nCols > 0 )
170  return nCols;
171 
172  return data->getNumCols( );
173 }
174 
175 
177 {
178  return (getNumRows() * getNumCols());
179 }
180 
181 
183 {
184  DMatrix m = data->transpose();
185 
188  transposed->doAccessTransposed = true;
189 
190  return transposed;
191 }
192 
193 
195  ) const
196 {
197  ASSERT( doAccessTransposed == false );
198 
200 
201  if (idx.isGiven() == true
202  && (idx.getGivenValue() < 0 || idx.getGivenValue() > ((int) getNumRows( ) - 1)) )
203  {
204  LOG( LVL_ERROR )
205  << "getRow: invalid row arguments, row index "
206  << idx.getGivenValue() << " of variable " << getFullName()
207  << " does not lie in the admissible range " << "0 - " << getNumRows( ) - 1 << endl;
208  }
209 
210  tmp->setSubmatrixOffsets(idx, 0, getNumRows(), getNumCols( ), 1, getNumCols( ));
211 
212  return tmp;
213 }
214 
215 
217  ) const
218 {
219  ASSERT( doAccessTransposed == false );
220 
222 
223  if (idx.isGiven() == true
224  && (idx.getGivenValue() < 0 || idx.getGivenValue() > (int) getNumCols( ) - 1) )
225  {
226  LOG( LVL_ERROR )
227  << "getCol: invalid column arguments, column index "
228  << idx.getGivenValue() << " of variable " << getFullName()
229  << " does not lie in the admissible range " << "0 - " << getNumCols( ) - 1 << endl;
230  }
231 
232  tmp->setSubmatrixOffsets(0, idx, getNumRows(), getNumCols( ), getNumRows(), 1);
233 
234  return tmp;
235 }
236 
237 
239  const ExportIndex& idx2
240  ) const
241 {
242  if (doAccessTransposed == true) ASSERT(data->getNumCols() == 1 || data->getNumRows() == 1);
243 
245 
246  ExportIndex size = idx2 - idx1;
247 
248  if (size.isGiven() == true && size.getGivenValue() == 0)
249  return tmp;
250 
251  if (idx1.isGiven() == true && idx2.isGiven() == true
252  && ( idx1.getGivenValue() < 0 ||
253  idx1.getGivenValue() > idx2.getGivenValue() ||
254  idx2.getGivenValue() > (int) getNumRows( ) ))
255  {
256  LOG( LVL_ERROR ) << getFullName() << ": getRows: invalid row arguments" << endl;
257  }
258  else if (size.isGiven() == false)
259  {
260  LOG( LVL_ERROR ) << getFullName() << ": getRows: Cannot determine size" << endl;
261  }
262  else
263  tmp->setSubmatrixOffsets(idx1, 0, getNumRows(), getNumCols( ), size.getGivenValue(), getNumCols( ));
264 
265  return tmp;
266 }
267 
268 
270  const ExportIndex& idx2
271  ) const
272 {
273  if (doAccessTransposed == true) ASSERT(data->getNumCols() == 1 || data->getNumRows() == 1);
274 
276 
277  ExportIndex size = idx2 - idx1;
278 
279  if (size.isGiven() == true && size.getGivenValue() == 0)
280  return tmp;
281 
282  if (idx1.isGiven() == true && idx2.isGiven() == true
283  && ( idx1.getGivenValue() < 0 ||
284  idx1.getGivenValue() > idx2.getGivenValue() ||
285  idx2.getGivenValue() > (int) getNumCols( ) ))
286  {
287  LOG( LVL_ERROR ) << getFullName() << ": getCols: invalid column arguments" << endl;
288  }
289  else if (size.isGiven() == false)
290  {
291  LOG( LVL_ERROR ) << getFullName() << ": getCols: Cannot determine size" << endl;
292  }
293  else
294  tmp->setSubmatrixOffsets(0, idx1, getNumRows(), getNumCols( ), getNumRows( ), size.getGivenValue() );
295 
296  return tmp;
297 }
298 
299 
301  const ExportIndex& _rowIdx2,
302  const ExportIndex& _colIdx1,
303  const ExportIndex& _colIdx2
304  ) const
305 {
306  ASSERT(doAccessTransposed == false);
307 
308  ExportVariable tmp;
309 
310  ExportIndex sizeRow = _rowIdx2 - _rowIdx1;
311  ExportIndex sizeCol = _colIdx2 - _colIdx1;
312 
313  if (sizeRow.isGiven() == true && sizeRow.getGivenValue() == 0)
314  return tmp;
315 
316  if (sizeCol.isGiven() == true && sizeCol.getGivenValue() == 0)
317  return tmp;
318 
319  if (_rowIdx1.isGiven() == true && _rowIdx2.isGiven() == true
320  && ( _rowIdx1.getGivenValue() < 0 ||
321  _rowIdx1.getGivenValue() > _rowIdx2.getGivenValue() ||
322  _rowIdx2.getGivenValue() > (int) getNumRows( ) ))
323  {
324  LOG( LVL_ERROR ) << getFullName() << ": getSubMatrix: invalid row arguments" << endl;
325  return tmp;
326  }
327  else if (sizeRow.isGiven() == false)
328  {
329  LOG( LVL_ERROR ) << getFullName() << ": getSubMatrix: cannot determine row size" << endl;
330  return tmp;
331  }
332 
333  if (_colIdx1.isGiven() == true && _colIdx2.isGiven() == true
334  && ( _colIdx1.getGivenValue() < 0 ||
335  _colIdx1.getGivenValue() > _colIdx2.getGivenValue() ||
336  _colIdx2.getGivenValue() > (int) getNumCols( ) ))
337  {
338  LOG( LVL_ERROR ) << getFullName() << ": getSubMatrix: invalid column arguments" << endl;
339  return tmp;
340  }
341  else if (sizeCol.isGiven() == false)
342  {
343  LOG( LVL_ERROR ) << getFullName() << ": getSubMatrix: cannot determine column size" << endl;
344  return tmp;
345  }
346 
348  tmp->setSubmatrixOffsets(_rowIdx1, _colIdx1, getNumRows(), getNumCols( ), sizeRow.getGivenValue(), sizeCol.getGivenValue());
349 
350  return tmp;
351 }
352 
353 
355 {
356  ASSERT( ( nRows == 0 ) && ( nCols == 0 ) );
357 
358  DMatrix foo( *data.get() );
359  foo.makeVector().transposeInPlace();
360 
362 
363  return tmp;
364 }
365 
366 
368 {
369  ASSERT( ( nRows == 0 ) && ( nCols == 0 ) );
370 
371  DMatrix foo( *data.get() );
372  foo.makeVector();
373 
375 
376  return tmp;
377 }
378 
379 
381 {
382  if (getNumRows( ) == 1 || getNumCols( ) == 1)
383  return true;
384 
385  return false;
386 }
387 
388 
390 {
391  if ( isGiven() == true )
392  return *data.get();
393 
394  return emptyConstMatrix;
395 }
396 
397 
399 {
400  return data->print( );
401 }
402 
403 
404 //
405 // PROTECTED MEMBER FUNCTIONS:
406 //
407 
408 
410 {
411  if (doAccessTransposed == true)
412  return rowDim;
413 
414  return colDim;
415 }
416 
417 
419  const ExportIndex& _colIdx
420  ) const
421 {
422  ExportIndex tmp;
423 
424  if ( doAccessTransposed == false )
425  tmp = _rowIdx * getColDim() + _colIdx;
426  else
427  tmp = _colIdx * getColDim() + _rowIdx;
428 
429  return tmp;
430 }
431 
432 
434  const ExportIndex& _colOffset,
435  unsigned _rowDim,
436  unsigned _colDim,
437  unsigned _nRows,
438  unsigned _nCols
439  )
440 {
441  if ( ( _rowOffset.isGiven() == true ) && ( _rowOffset.getGivenValue() < 0 ) )
443 
444  if ( ( _colOffset.isGiven() == true ) && ( _colOffset.getGivenValue() < 0 ) )
446 
447  if ( ( _colOffset.isGiven() == true ) && ( _colOffset.getGivenValue() > (int)_colDim ) )
449 
450  rowOffset = _rowOffset;
451  colOffset = _colOffset;
452  rowDim = _rowDim;
453  colDim = _colDim;
454 
455  nRows = _nRows;
456  nCols = _nCols;
457 
458  return SUCCESSFUL_RETURN;
459 }
460 
461 
463  const ExportIndex& colIdx,
464  double _value
465  ) const
466 {
467  if ((getType() == STATIC_CONST_INT || getType() == STATIC_CONST_REAL) &&
468  acadoIsEqual(_value, undefinedEntry) == true)
469  return true;
470 
471  ExportIndex ind = getTotalIdx(rowIdx + rowOffset, colIdx + colOffset);
472 
473  if (ind.isGiven() == true)
474  return acadoIsEqual(data->operator()(ind.getGivenValue()), _value);
475 
476  return false;
477 }
478 
480 {
481  if (nRows == 0 && nCols == 0)
482  return false;
483 
484  return true;
485 }
486 
488 {
489  if (isSubMatrix() == true)
490  {
491  LOG( LVL_DEBUG ) << "Digonal check works for non-sub-matrices only ATM" << endl;
492  return false;
493  }
494 
495  DMatrix foo( *data );
497 
498  return (foo == bar);
499 }
500 
501 
503 
504 // end of file.
void assignNode(SharedObjectNode *node)
Assign the node to a node class pointer (or null)
#define LOG(level)
Just define a handy macro for getting the logger.
Lowest level, the debug level.
ExportVariable getRow(const ExportIndex &idx) const
bool isOne(const ExportIndex &rowIdx, const ExportIndex &colIdx) const
ExportVariable makeRowVector() const
BooleanType acadoIsEqual(double x, double y, double TOL)
Definition: acado_utils.cpp:88
static const DMatrix emptyConstMatrix
Definition: matrix.hpp:466
returnValue setSubmatrixOffsets(const ExportIndex &_rowOffset, const ExportIndex &_colOffset, unsigned _rowDim=0, unsigned _colDim=0, unsigned _nRows=0, unsigned _nCols=0)
std::string getFullName() const
Allows to pass back messages to the calling function.
Represents a diagonal matrix with its storage.
std::shared_ptr< GenericMatrix< double > > DMatrixPtr
Definition: matrix.hpp:463
GenericMatrix & makeVector()
Definition: matrix.cpp:124
const std::string get(const ExportIndex &rowIdx, const ExportIndex &colIdx) const
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
ExportVariable getRows(const ExportIndex &idx1, const ExportIndex &idx2) const
Returned value is a error.
Defines a matrix-valued variable to be used for exporting code.
#define CLOSE_NAMESPACE_ACADO
virtual ExportIndex getTotalIdx(const ExportIndex &rowIdx, const ExportIndex &colIdx) const
Defines a scalar-valued index variable to be used for exporting code.
const ExportIndex emptyConstExportIndex(int(0))
virtual uint getNumCols() const
ExportStruct
bool isZero(const ExportIndex &rowIdx, const ExportIndex &colIdx) const
Defines a matrix-valued variable that can be passed as argument to exported functions.
virtual uint getNumRows() const
ExportVariable getTranspose() const
ExportType getType() const
static const double undefinedEntry
void set(streamsize _precision, streamsize _width, ios_base::fmtflags _flags)
ExportType
const DMatrix & getGivenMatrix() const
const std::string get() const
#define ASSERT(x)
bool hasValue(const ExportIndex &_rowIdx, const ExportIndex &_colIdx, double _value) const
int getGivenValue() const
bool isGiven() const
ExportVariable getSubMatrix(const ExportIndex &_rowIdx1, const ExportIndex &_rowIdx2, const ExportIndex &_colIdx1, const ExportIndex &_colIdx2) const
#define BEGIN_NAMESPACE_ACADO
virtual ExportVariableInternal * clone() const
ExportVariable makeColVector() const
ExportVariable getCols(const ExportIndex &idx1, const ExportIndex &idx2) const
ExportVariable getCol(const ExportIndex &idx) const
#define ACADOERROR(retval)
Defines a matrix-valued variable to be used for exporting code.


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