Go to the documentation of this file.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
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
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