export_cholesky_decomposition.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 
00032 #include <acado/code_generation/export_cholesky_decomposition.hpp>
00033 
00034 #include <string>
00035 #include <sstream>
00036 
00037 using namespace std;
00038 
00039 BEGIN_NAMESPACE_ACADO
00040 
00041 ExportCholeskyDecomposition::ExportCholeskyDecomposition(       UserInteraction* _userInteraction,
00042                                                                                                                         const std::string& _commonHeaderName
00043                                                                                                                         ) : ExportAlgorithm(_userInteraction, _commonHeaderName)
00044 {
00045         init("choleskyDecomposition", 0, false);
00046 }
00047 
00048 returnValue ExportCholeskyDecomposition::init(  const std::string& _name,
00049                                                                                                 unsigned _dim,
00050                                                                                                 bool _unrolling
00051                                                                                                 )
00052 {
00053         unrolling = _unrolling;
00054 
00055         A.setup("A", _dim, _dim, REAL, ACADO_LOCAL);
00056         fcn.init(_name, A);
00057 
00058         return SUCCESSFUL_RETURN;
00059 }
00060 
00061 returnValue ExportCholeskyDecomposition::setup()
00062 {
00063         ASSERT( A.getDim() );
00064 
00065         stringstream s;
00066 
00067         ExportVariable ret("ret", 1, 1, INT, ACADO_LOCAL, true);
00068         fcn.setReturnValue( ret );
00069 
00070         string name( A.getName() );
00071         unsigned n = A.getNumRows();
00072 
00073         s               << "register unsigned i, j, k;" << endl
00074                         << "real_t inv;" << endl
00075                         << "for (i = 0; i < " << n << "; ++i)" << endl
00076                         << "{" << endl
00077                         << name << "[i * " << n << " + i] = "
00078                                 << name << "[i * " << n << " + i] < 1e-8 ? 1e-8 : "
00079                                 << "sqrt(" << name <<  "[i * " << n << " + i]);" << endl
00080 
00081                         << "inv = 1 / " << name << "[i * " << n << " + i];" << endl
00082 
00083                         << "for (j = i + 1; j < " << n << "; ++j)"  << endl
00084                         << name << "[j * " << n << " + i] = " << name << "[j * " << n << " + i] * inv;"  << endl
00085 
00086                         << "for (j = i + 1; j < " << n << "; ++j)"  << endl
00087                         << "for (k = j; k < " << n << "; ++k)"  << endl
00088                         << name << "[k * " << n << " + j] = " << name << "[k * " << n << " + j] - "
00089                                         << name << "[k * " << n << " + i] * " << name << "[j * " << n << " + i];"  << endl
00090                         << "}"  << endl
00091 
00092                         // Clear the upper triangular part
00093                         << "for (i = 0; i < " << n << "; ++i)"  << endl
00094                         << "for (j = i + 1; j < " << n << "; ++j)"  << endl
00095                         << name << "[i * " << n << " + j] = 0.0;" << endl
00096                         << ret.getName() << " = 0;" << endl;
00097 
00098         fcn.addStatement( s.str() );
00099 
00100         return SUCCESSFUL_RETURN;
00101 }
00102 
00103 returnValue ExportCholeskyDecomposition::getDataDeclarations(   ExportStatementBlock& declarations,
00104                                                                                                                                 ExportStruct dataStruct
00105                                                                                                                                 ) const
00106 {
00107         return SUCCESSFUL_RETURN;
00108 }
00109 
00110 returnValue ExportCholeskyDecomposition::getFunctionDeclarations(       ExportStatementBlock& declarations
00111                                                                                                                                         ) const
00112 {
00113         if (A.getDim() != 0)
00114                 return declarations.addFunction( fcn );
00115 
00116         return SUCCESSFUL_RETURN;
00117 }
00118 
00119 returnValue ExportCholeskyDecomposition::getCode(       ExportStatementBlock& code
00120                                                                                                         )
00121 {
00122         if (A.getDim() != 0)
00123                 return code.addFunction( fcn );
00124 
00125         return SUCCESSFUL_RETURN;
00126 }
00127 
00128 const std::string ExportCholeskyDecomposition::getName()
00129 {
00130         return fcn.getName();
00131 }
00132 
00133 CLOSE_NAMESPACE_ACADO


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