00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00035 #include <acado/code_generation/export_argument_internal.hpp>
00036 #include <acado/code_generation/export_argument.hpp>
00037
00038
00039 BEGIN_NAMESPACE_ACADO
00040
00041 using namespace std;
00042
00043
00044 static const double undefinedEntry = 1073741824.03125;
00045
00046
00047
00048
00049
00050
00051 ExportArgumentInternal::ExportArgumentInternal( ) : ExportDataInternal()
00052 {
00053 data->init(0, 0);
00054
00055 callItByValue = false;
00056 }
00057
00058 ExportArgumentInternal::ExportArgumentInternal( const std::string& _name,
00059 const DMatrixPtr& _data,
00060 ExportType _type,
00061 ExportStruct _dataStruct,
00062 bool _callItByValue,
00063 const ExportIndex& _addressIdx,
00064 const std::string& _prefix
00065 )
00066 : ExportDataInternal(_name, _type, _dataStruct, _prefix)
00067 {
00068 data = _data ;
00069 callItByValue = _callItByValue;
00070 addressIdx = _addressIdx;
00071 }
00072
00073 ExportArgumentInternal::~ExportArgumentInternal( )
00074 {}
00075
00076 ExportArgumentInternal* ExportArgumentInternal::clone() const
00077 {
00078 return new ExportArgumentInternal( *this );
00079 }
00080
00081 void ExportArgumentInternal::deepCopyMembers( std::map<CasADi::SharedObjectNode*, CasADi::SharedObject>& already_copied
00082 )
00083 {
00084 data.reset(new DMatrix( *data ));
00085 }
00086
00087 ExportArgument ExportArgumentInternal::getAddress( const ExportIndex& rowIdx,
00088 const ExportIndex& colIdx
00089 ) const
00090 {
00091 if ( rowIdx.isGiven( ) )
00092 {
00093 ASSERT( rowIdx.getGivenValue() < (int)getNumRows() );
00094 }
00095 if( colIdx.isGiven() )
00096 {
00097 ASSERT( colIdx.getGivenValue() < (int)getNumCols() );
00098 }
00099
00100 ExportIndex ind = getTotalIdx(rowIdx, colIdx);
00101
00102 ExportArgument tmp(name, data, type, dataStruct, false, ind, prefix);
00103
00104 return tmp;
00105 }
00106
00107
00108 const std::string ExportArgumentInternal::getAddressString( bool withDataStruct
00109 ) const
00110 {
00111 stringstream ss;
00112
00113 std::string nameStr;
00114
00115 if (withDataStruct == true)
00116 nameStr = getFullName();
00117 else
00118 nameStr = getName();
00119
00120 if ( addressIdx.isGiven() == true )
00121 {
00122 if ( addressIdx.getGivenValue() == 0 )
00123 ss << nameStr;
00124 else
00125 ss << "&(" << nameStr << "[ " << addressIdx.getGivenValue() << " ])";
00126 }
00127 else
00128 {
00129 ss << "&(" << nameStr << "[ " << addressIdx.get() << " ])";
00130 }
00131
00132 return ss.str();
00133 }
00134
00135
00136 uint ExportArgumentInternal::getNumRows( ) const
00137 {
00138 return data->getNumRows( );
00139 }
00140
00141
00142 uint ExportArgumentInternal::getNumCols( ) const
00143 {
00144 return data->getNumCols( );
00145 }
00146
00147
00148 uint ExportArgumentInternal::getDim( ) const
00149 {
00150 return data->getDim( );
00151 }
00152
00153
00154 bool ExportArgumentInternal::isGiven( ) const
00155 {
00156 if ( getDim() == 0 )
00157 return true;
00158
00159 if (getType() == STATIC_CONST_INT || getType() == STATIC_CONST_REAL)
00160 return false;
00161
00162 for (uint i = 0; i < getNumRows(); ++i)
00163 for (uint j = 0; j < getNumCols(); ++j)
00164 if (acadoIsEqual(data->operator()(i, j), undefinedEntry) == true)
00165 return false;
00166
00167 return true;
00168 }
00169
00170
00171
00172 bool ExportArgumentInternal::isCalledByValue( ) const
00173 {
00174 return callItByValue;
00175 }
00176
00177
00178 returnValue ExportArgumentInternal::callByValue( )
00179 {
00180 callItByValue = true;
00181 return SUCCESSFUL_RETURN;
00182 }
00183
00184
00185 returnValue ExportArgumentInternal::exportDataDeclaration( std::ostream& stream,
00186 const std::string& _realString,
00187 const std::string& _intString,
00188 int _precision
00189 ) const
00190 {
00191
00192 if ( getDim( ) == 0 )
00193 return SUCCESSFUL_RETURN;
00194
00195
00196 if (isGiven() == true && getDataStruct() != ACADO_LOCAL)
00197 return SUCCESSFUL_RETURN;
00198
00199 if ( ( isCalledByValue() == true ) && ( getDim() == 1 ) )
00200 {
00201 stream << getTypeString(_realString, _intString) << " " << name;
00202 }
00203 else
00204 {
00205 if (data->getNumCols() > 1 && data->getNumRows() > 1)
00206 {
00207 stream << "/** " << "Matrix of size: " << data->getNumRows() << " x " << data->getNumCols() << " (row major format)";
00208 }
00209 else
00210 {
00211 if (data->getNumCols() == 1)
00212 stream << "/** " << "Column vector of size: " << data->getNumRows();
00213 else
00214 stream << "/** " << "Row vector of size: " << data->getNumCols();
00215 }
00216
00217 if (description.empty() == false)
00218 {
00219 stream << "\n * \n * " << description << endl;
00220 }
00221
00222 stream << " */\n";
00223
00224 stream << getTypeString(_realString, _intString) << " " << name << "[ " << getDim() << " ]";
00225 }
00226
00227 if ( isGiven() == false )
00228 {
00229 if (getType() == STATIC_CONST_INT)
00230 {
00231 stream << " = " << endl;
00232 IMatrix( data->cast<int>() ).print(stream, "", "{ ", " };\n", 5, 0, ", ", ", \n");
00233 }
00234 else if (getType() == STATIC_CONST_REAL)
00235 {
00236 stream << " = " << endl;
00237 data->print(stream, "", "{ ", " };\n", 1, 16, ", ", ", \n");
00238 }
00239 else
00240 stream << ";\n\n";
00241 }
00242 else
00243 {
00244 stream << " = " << endl;
00245
00246 switch ( getType() )
00247 {
00248 case INT:
00249
00250 IMatrix( data->cast<int>() ).print(stream, "", "{ ", " };\n", 5, 0, ", ", ", \n");
00251 break;
00252
00253 case REAL:
00254
00255 data->print(stream, "", "{ ", " };\n", 1, 16, ", ", ", \n");
00256 break;
00257
00258 default:
00259 return ACADOERROR( RET_NOT_YET_IMPLEMENTED );
00260 }
00261 }
00262
00263 return SUCCESSFUL_RETURN;
00264 }
00265
00266
00267
00268
00269
00270
00271
00272
00273 uint ExportArgumentInternal::getColDim( ) const
00274 {
00275 return data->getNumCols( );
00276 }
00277
00278 ExportIndex ExportArgumentInternal::getTotalIdx( const ExportIndex& rowIdx,
00279 const ExportIndex& colIdx
00280 ) const
00281 {
00282 return rowIdx * getNumCols() + colIdx;
00283 }
00284
00285
00286 CLOSE_NAMESPACE_ACADO
00287
00288