export_index.cpp
Go to the documentation of this file.
00001 /*
00002  *    This file is part of ACADO Toolkit.
00003  *
00004  *    ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
00005  *    Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
00006  *    Milan Vukov, Rien Quirynen, KU Leuven.
00007  *    Developed within the Optimization in Engineering Center (OPTEC)
00008  *    under supervision of Moritz Diehl. All rights reserved.
00009  *
00010  *    ACADO Toolkit is free software; you can redistribute it and/or
00011  *    modify it under the terms of the GNU Lesser General Public
00012  *    License as published by the Free Software Foundation; either
00013  *    version 3 of the License, or (at your option) any later version.
00014  *
00015  *    ACADO Toolkit is distributed in the hope that it will be useful,
00016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  *    Lesser General Public License for more details.
00019  *
00020  *    You should have received a copy of the GNU Lesser General Public
00021  *    License along with ACADO Toolkit; if not, write to the Free Software
00022  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023  *
00024  */
00025 
00026 
00027 
00034 #include <acado/code_generation/export_index.hpp>
00035 #include <acado/code_generation/export_argument.hpp>
00036 #include <acado/code_generation/export_index_node.hpp>
00037 
00038 BEGIN_NAMESPACE_ACADO
00039 
00040 //
00041 // PUBLIC MEMBER FUNCTIONS:
00042 //
00043 
00044 ExportIndex::ExportIndex()
00045 {}
00046 
00047 
00048 ExportIndex::ExportIndex(       const int _value )
00049 {
00050         assignNode(new ExportIndexNode( _value ));
00051 }
00052 
00053 ExportIndex::ExportIndex(       const std::string& _name,
00054                                                         const std::string& _prefix )
00055 {
00056         assignNode(new ExportIndexNode(_name, _prefix));
00057 }
00058 
00059 
00060 ExportIndexNode* ExportIndex::operator->()
00061 {
00062         return (ExportIndexNode*)(ExportData::operator->());
00063 }
00064 
00065 
00066 const ExportIndexNode* ExportIndex::operator->() const
00067 {
00068         return (const ExportIndexNode*)(ExportData::operator->());
00069 }
00070 
00071 
00072 returnValue ExportIndex::exportDataDeclaration( std::ostream& stream,
00073                                                                                                 const std::string& _realString ,
00074                                                                                                 const std::string& _intString,
00075                                                                                                 int _precision
00076                                                                                                 ) const
00077 {
00078         return (*this)->exportDataDeclaration(stream, _realString, _intString, _precision);
00079 }
00080 
00081 
00082 ExportIndex operator+(  const ExportIndex& _arg1,
00083                                                 const ExportIndex& _arg2
00084                                                 )
00085 {
00086         ExportIndex tmp;
00087 
00088         if (_arg1.isGiven() == true && _arg2.isGiven() == true)
00089         {
00090                 tmp.assignNode(new ExportIndexNode(_arg1.getGivenValue() + _arg2.getGivenValue()));
00091 
00092                 return tmp;
00093         }
00094 
00095         if (_arg1.isVariable() == true && _arg2.isGiven() == true)
00096         {
00097                 tmp.assignNode(new ExportIndexNode(_arg1.getName(), _arg1.getPrefix(), _arg1->getFactor(), _arg1->getOffset() + _arg2.getGivenValue()));
00098 
00099                 return tmp;
00100         }
00101 
00102         if (_arg1.isGiven() == true && _arg2.isVariable() == true)
00103         {
00104                 tmp.assignNode(new ExportIndexNode(_arg2.getName(), _arg2.getPrefix(),  _arg2->getFactor(), _arg2->getOffset() + _arg1.getGivenValue()));
00105 
00106                 return tmp;
00107         }
00108 
00109         if(_arg1.isVariable() == true && _arg2.isVariable() == true && _arg1.getFullName() == _arg2.getFullName())
00110         {
00111                 if ((_arg1->getFactor() + _arg2->getFactor()) == 0)
00112                         tmp.assignNode(new ExportIndexNode(_arg1->getOffset() + _arg2->getOffset()));
00113                 else
00114                         tmp.assignNode(new ExportIndexNode(_arg1.getName(), _arg1.getPrefix(), _arg1->getFactor() + _arg2->getFactor(), _arg1->getOffset() + _arg2->getOffset()));
00115         }
00116         else
00117         {
00118                 tmp.assignNode(new ExportIndexNode(ESO_ADD, _arg1, _arg2));
00119         }
00120 
00121         return tmp;
00122 }
00123 
00124 
00125 ExportIndex operator-(  const ExportIndex& _arg1,
00126                                                 const ExportIndex& _arg2
00127                                                 )
00128 {
00129         ExportIndex tmp;
00130 
00131         if (_arg1.isGiven() == true && _arg2.isGiven() == true)
00132         {
00133                 tmp.assignNode(new ExportIndexNode(_arg1.getGivenValue() - _arg2.getGivenValue()));
00134 
00135                 return tmp;
00136         }
00137 
00138         if (_arg1.isVariable() == true && _arg2.isGiven() == true)
00139         {
00140                 tmp.assignNode(new ExportIndexNode(_arg1.getName(), _arg1.getPrefix(), _arg1->getFactor(), _arg1->getOffset() - _arg2.getGivenValue()));
00141 
00142                 return tmp;
00143         }
00144 
00145         if (_arg1.isGiven() == true && _arg2.isVariable() == true)
00146         {
00147                 tmp.assignNode(new ExportIndexNode(_arg2.getName(), _arg2.getPrefix(), -1 * _arg2->getFactor(), _arg1->getGivenValue() - _arg2->getOffset()));
00148 
00149                 return tmp;
00150         }
00151 
00152         if(_arg1.isVariable() == true && _arg2.isVariable() == true && _arg1.getFullName() == _arg2.getFullName())
00153         {
00154                 if ((_arg1->getFactor() - _arg2->getFactor()) == 0)
00155                         tmp.assignNode(new ExportIndexNode(_arg1->getOffset() - _arg2->getOffset()));
00156                 else
00157                         tmp.assignNode(new ExportIndexNode(_arg1.getName(), _arg1.getPrefix(), _arg1->getFactor() - _arg2->getFactor(), _arg1->getOffset() - _arg2->getOffset()));
00158         }
00159         else
00160                 tmp.assignNode(new ExportIndexNode(ESO_SUBTRACT, _arg1, _arg2));
00161 
00162         return tmp;
00163 }
00164 
00165 
00166 ExportIndex operator*(  const ExportIndex& _arg1,
00167                                                 const ExportIndex& _arg2
00168                                                 )
00169 {
00170         ExportIndex tmp;
00171 
00172         if (_arg1.isGiven() == true && _arg2.isGiven() == true)
00173         {
00174                 tmp.assignNode(new ExportIndexNode(_arg1.getGivenValue() * _arg2.getGivenValue()));
00175 
00176                 return tmp;
00177         }
00178 
00179         if (_arg1.isVariable() == true && _arg2.isGiven() == true)
00180         {
00181                 tmp.assignNode(new ExportIndexNode(_arg1.getName(), _arg1.getPrefix(), _arg1->getFactor() * _arg2.getGivenValue(), _arg1->getOffset() * _arg2.getGivenValue()));
00182 
00183                 return tmp;
00184         }
00185 
00186         if (_arg1.isGiven() == true && _arg2.isVariable() == true)
00187         {
00188                 tmp.assignNode(new ExportIndexNode(_arg2.getName(), _arg2.getPrefix(), _arg2->getFactor() * _arg1.getGivenValue(), _arg2->getOffset() * _arg1.getGivenValue()));
00189 
00190                 return tmp;
00191         }
00192 
00193         tmp.assignNode(new ExportIndexNode(ESO_MULTIPLY, _arg1, _arg2));
00194 
00195         return tmp;
00196 }
00197 
00198 
00199 ExportIndex operator/(  const ExportIndex& _arg1,
00200                                                 const ExportIndex& _arg2
00201                                                 )
00202 {
00203         ExportIndex tmp;
00204 
00205         if (_arg1.isGiven() == true && _arg2.isGiven() == true)
00206         {
00207                 tmp.assignNode(new ExportIndexNode(_arg1.getGivenValue() / _arg2.getGivenValue()));
00208 
00209                 return tmp;
00210         }
00211 
00212         tmp.assignNode(new ExportIndexNode(ESO_DIVIDE, _arg1, _arg2));
00213 
00214         return tmp;
00215 }
00216 
00217 ExportIndex operator%(  const ExportIndex& _arg1,
00218                                                 const ExportIndex& _arg2
00219                                                 )
00220 {
00221         ExportIndex tmp;
00222 
00223         if (_arg1.isGiven() == true && _arg2.isGiven() == true)
00224         {
00225                 tmp.assignNode(new ExportIndexNode(_arg1.getGivenValue() % _arg2.getGivenValue()));
00226 
00227                 return tmp;
00228         }
00229 
00230         tmp.assignNode(new ExportIndexNode(ESO_MODULO, _arg1, _arg2));
00231 
00232         return tmp;
00233 }
00234 
00235 std::string operator==( const ExportIndex& _arg1,
00236                                                 const ExportIndex& _arg2
00237                                                 )
00238 {
00239         std::stringstream ret;
00240         ret << _arg1.get() << " = " << _arg2.get() << ";\n";
00241 
00242         return ret.str();
00243 }
00244 
00245 
00246 const std::string ExportIndex::get( ) const
00247 {
00248         return (*this)->get();
00249 }
00250 
00251 
00252 int ExportIndex::getGivenValue( ) const
00253 {
00254         return (*this)->getGivenValue();
00255 }
00256 
00257 
00258 bool ExportIndex::isGiven( ) const
00259 {
00260         return (*this)->isGiven();
00261 }
00262 
00263 bool ExportIndex::isBinary( ) const
00264 {
00265         return (*this)->isBinary();
00266 }
00267 
00268 bool ExportIndex::isVariable() const
00269 {
00270         return (*this)->isVariable();
00271 }
00272 
00273 
00274 ExportIndex::operator ExportArgument()
00275 {
00276         std::string tmpName;
00277 
00278         // XXX In principle, this is an ugly hack. In case when an index is given,
00279         // We give it a name which is equal to its value. This is done in order
00280         // To be able to simplify function calls. Most probably much more sense
00281         // would make that ExportArgument is base class for this guy...
00282 
00283         if (isGiven() == true)
00284                 tmpName = std::toString(getGivenValue());
00285         else
00286                 tmpName = (*this)->getName();
00287 
00288         ExportArgument tmp(tmpName, 1 , 1, (*this)->getType(), ACADO_LOCAL, true, emptyConstExportIndex);
00289         tmp.setDoc( getDoc() );
00290 
00291         return tmp;
00292 }
00293 
00294 
00295 CLOSE_NAMESPACE_ACADO
00296 
00297 // end of file.


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Sat Jun 8 2019 19:37:01