export_argument_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 
37 
38 
40 
41 using namespace std;
42 
43 
44 static const double undefinedEntry = 1073741824.03125; // = 2^30 + 2^-5
45 
46 
47 //
48 // PUBLIC MEMBER FUNCTIONS:
49 //
50 
52 {
53  data->init(0, 0);
54 
55  callItByValue = false;
56 }
57 
59  const DMatrixPtr& _data,
60  ExportType _type,
61  ExportStruct _dataStruct,
62  bool _callItByValue,
63  const ExportIndex& _addressIdx,
64  const std::string& _prefix
65  )
66  : ExportDataInternal(_name, _type, _dataStruct, _prefix)
67 {
68  data = _data ;
69  callItByValue = _callItByValue;
70  addressIdx = _addressIdx;
71 }
72 
74 {}
75 
77 {
78  return new ExportArgumentInternal( *this );
79 }
80 
81 void ExportArgumentInternal::deepCopyMembers( std::map<CasADi::SharedObjectNode*, CasADi::SharedObject>& already_copied
82  )
83 {
84  data.reset(new DMatrix( *data ));
85 }
86 
88  const ExportIndex& colIdx
89  ) const
90 {
91  if ( rowIdx.isGiven( ) )
92  {
93  ASSERT( rowIdx.getGivenValue() < (int)getNumRows() );
94  }
95  if( colIdx.isGiven() )
96  {
97  ASSERT( colIdx.getGivenValue() < (int)getNumCols() );
98  }
99 
100  ExportIndex ind = getTotalIdx(rowIdx, colIdx);
101 
102  ExportArgument tmp(name, data, type, dataStruct, false, ind, prefix);
103 
104  return tmp;
105 }
106 
107 
108 const std::string ExportArgumentInternal::getAddressString( bool withDataStruct
109  ) const
110 {
111  stringstream ss;
112 
113  std::string nameStr;
114 
115  if (withDataStruct == true)
116  nameStr = getFullName();
117  else
118  nameStr = getName();
119 
120  if ( addressIdx.isGiven() == true )
121  {
122  if ( addressIdx.getGivenValue() == 0 )
123  ss << nameStr;
124  else
125  ss << "&(" << nameStr << "[ " << addressIdx.getGivenValue() << " ])";
126  }
127  else
128  {
129  ss << "&(" << nameStr << "[ " << addressIdx.get() << " ])";
130  }
131 
132  return ss.str();
133 }
134 
135 
137 {
138  return data->getNumRows( );
139 }
140 
141 
143 {
144  return data->getNumCols( );
145 }
146 
147 
149 {
150  return data->getDim( );
151 }
152 
153 
155 {
156  if ( getDim() == 0 )
157  return true;
158 
160  return false;
161 
162  for (uint i = 0; i < getNumRows(); ++i)
163  for (uint j = 0; j < getNumCols(); ++j)
164  if (acadoIsEqual(data->operator()(i, j), undefinedEntry) == true)
165  return false;
166 
167  return true;
168 }
169 
170 
171 
173 {
174  return callItByValue;
175 }
176 
177 
179 {
180  callItByValue = true;
181  return SUCCESSFUL_RETURN;
182 }
183 
184 
186  const std::string& _realString,
187  const std::string& _intString,
188  int _precision
189  ) const
190 {
191  // Variable not in use, thus no declaration necessary
192  if ( getDim( ) == 0 )
193  return SUCCESSFUL_RETURN;
194 
195  // Variable will be hard-coded
196  if (isGiven() == true && getDataStruct() != ACADO_LOCAL)
197  return SUCCESSFUL_RETURN;
198 
199  if ( ( isCalledByValue() == true ) && ( getDim() == 1 ) )
200  {
201  stream << getTypeString(_realString, _intString) << " " << name;
202  }
203  else
204  {
205  if (data->getNumCols() > 1 && data->getNumRows() > 1)
206  {
207  stream << "/** " << "Matrix of size: " << data->getNumRows() << " x " << data->getNumCols() << " (row major format)";
208  }
209  else
210  {
211  if (data->getNumCols() == 1)
212  stream << "/** " << "Column vector of size: " << data->getNumRows();
213  else
214  stream << "/** " << "Row vector of size: " << data->getNumCols();
215  }
216 
217  if (description.empty() == false)
218  {
219  stream << "\n * \n * " << description << endl;
220  }
221 
222  stream << " */\n";
223 
224  stream << getTypeString(_realString, _intString) << " " << name << "[ " << getDim() << " ]";
225  }
226 
227  if ( isGiven() == false )
228  {
229  if (getType() == STATIC_CONST_INT)
230  {
231  stream << " = " << endl;
232  IMatrix( data->cast<int>() ).print(stream, "", "{ ", " };\n", 5, 0, ", ", ", \n");
233  }
234  else if (getType() == STATIC_CONST_REAL)
235  {
236  stream << " = " << endl;
237  data->print(stream, "", "{ ", " };\n", 1, 16, ", ", ", \n");
238  }
239  else
240  stream << ";\n\n";
241  }
242  else
243  {
244  stream << " = " << endl;
245 
246  switch ( getType() )
247  {
248  case INT:
249 // case STATIC_CONST_INT:
250  IMatrix( data->cast<int>() ).print(stream, "", "{ ", " };\n", 5, 0, ", ", ", \n");
251  break;
252 
253  case REAL:
254 // case STATIC_CONST_REAL:
255  data->print(stream, "", "{ ", " };\n", 1, 16, ", ", ", \n");
256  break;
257 
258  default:
260  }
261  }
262 
263  return SUCCESSFUL_RETURN;
264 }
265 
266 
267 
268 //
269 // PROTECTED MEMBER FUNCTIONS:
270 //
271 
272 
274 {
275  return data->getNumCols( );
276 }
277 
279  const ExportIndex& colIdx
280  ) const
281 {
282  return rowIdx * getNumCols() + colIdx;
283 }
284 
285 
287 
288 // end of file.
virtual returnValue exportDataDeclaration(std::ostream &stream, const std::string &_realString="real_t", const std::string &_intString="int", int _precision=16) const
ExportArgument getAddress(const ExportIndex &rowIdx, const ExportIndex &colIdx=emptyConstExportIndex) const
virtual ExportIndex getTotalIdx(const ExportIndex &rowIdx, const ExportIndex &colIdx) const
BooleanType acadoIsEqual(double x, double y, double TOL)
Definition: acado_utils.cpp:88
virtual ExportArgumentInternal * clone() const
Make a deep copy of the instance.
std::string getFullName() const
virtual void deepCopyMembers(std::map< CasADi::SharedObjectNode *, CasADi::SharedObject > &already_copied)
Allows to pass back messages to the calling function.
std::shared_ptr< GenericMatrix< double > > DMatrixPtr
Definition: matrix.hpp:463
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
std::string getTypeString(const std::string &_realString="real_t", const std::string &_intString="int") const
#define CLOSE_NAMESPACE_ACADO
GenericMatrix< double > DMatrix
Definition: matrix.hpp:457
Defines a scalar-valued index variable to be used for exporting code.
Defines a matrix-valued variable that can be passed as argument to exported functions.
static const double undefinedEntry
ExportStruct
virtual uint getNumRows() const
const std::string getAddressString(bool withDataStruct=true) const
Defines a matrix-valued variable that can be passed as argument to exported functions.
ExportType getType() const
ExportType
GenericMatrix< int > IMatrix
Definition: matrix.hpp:459
std::string getName() const
const std::string get() const
#define ASSERT(x)
int getGivenValue() const
bool isGiven() const
ExportStruct getDataStruct() const
#define BEGIN_NAMESPACE_ACADO
virtual uint getNumCols() const
#define ACADOERROR(retval)


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