export_cholesky_decomposition.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 
33 
34 #include <string>
35 #include <sstream>
36 
37 using namespace std;
38 
40 
42  const std::string& _commonHeaderName
43  ) : ExportAlgorithm(_userInteraction, _commonHeaderName)
44 {
45  init("choleskyDecomposition", 0, false);
46 }
47 
49  unsigned _dim,
50  bool _unrolling
51  )
52 {
53  unrolling = _unrolling;
54 
55  A.setup("A", _dim, _dim, REAL, ACADO_LOCAL);
56  fcn.init(_name, A);
57 
58  return SUCCESSFUL_RETURN;
59 }
60 
62 {
63  ASSERT( A.getDim() );
64 
65  stringstream s;
66 
67  ExportVariable ret("ret", 1, 1, INT, ACADO_LOCAL, true);
68  fcn.setReturnValue( ret );
69 
70  string name( A.getName() );
71  unsigned n = A.getNumRows();
72 
73  s << "register unsigned i, j, k;" << endl
74  << "real_t inv;" << endl
75  << "for (i = 0; i < " << n << "; ++i)" << endl
76  << "{" << endl
77  << name << "[i * " << n << " + i] = "
78  << name << "[i * " << n << " + i] < 1e-8 ? 1e-8 : "
79  << "sqrt(" << name << "[i * " << n << " + i]);" << endl
80 
81  << "inv = 1 / " << name << "[i * " << n << " + i];" << endl
82 
83  << "for (j = i + 1; j < " << n << "; ++j)" << endl
84  << name << "[j * " << n << " + i] = " << name << "[j * " << n << " + i] * inv;" << endl
85 
86  << "for (j = i + 1; j < " << n << "; ++j)" << endl
87  << "for (k = j; k < " << n << "; ++k)" << endl
88  << name << "[k * " << n << " + j] = " << name << "[k * " << n << " + j] - "
89  << name << "[k * " << n << " + i] * " << name << "[j * " << n << " + i];" << endl
90  << "}" << endl
91 
92  // Clear the upper triangular part
93  << "for (i = 0; i < " << n << "; ++i)" << endl
94  << "for (j = i + 1; j < " << n << "; ++j)" << endl
95  << name << "[i * " << n << " + j] = 0.0;" << endl
96  << ret.getName() << " = 0;" << endl;
97 
98  fcn.addStatement( s.str() );
99 
100  return SUCCESSFUL_RETURN;
101 }
102 
104  ExportStruct dataStruct
105  ) const
106 {
107  return SUCCESSFUL_RETURN;
108 }
109 
111  ) const
112 {
113  if (A.getDim() != 0)
114  return declarations.addFunction( fcn );
115 
116  return SUCCESSFUL_RETURN;
117 }
118 
120  )
121 {
122  if (A.getDim() != 0)
123  return code.addFunction( fcn );
124 
125  return SUCCESSFUL_RETURN;
126 }
127 
129 {
130  return fcn.getName();
131 }
132 
ExportVariable & setup(const std::string &_name, uint _nRows=1, uint _nCols=1, ExportType _type=REAL, ExportStruct _dataStruct=ACADO_LOCAL, bool _callItByValue=false, const std::string &_prefix=std::string())
returnValue init(const std::string &_name="defaultFunctionName", const ExportArgument &_argument1=emptyConstExportArgument, const ExportArgument &_argument2=emptyConstExportArgument, const ExportArgument &_argument3=emptyConstExportArgument, const ExportArgument &_argument4=emptyConstExportArgument, const ExportArgument &_argument5=emptyConstExportArgument, const ExportArgument &_argument6=emptyConstExportArgument, const ExportArgument &_argument7=emptyConstExportArgument, const ExportArgument &_argument8=emptyConstExportArgument, const ExportArgument &_argument9=emptyConstExportArgument)
Allows to pass back messages to the calling function.
ExportCholeskyDecomposition(UserInteraction *_userInteraction=0, const std::string &_commonHeaderName="")
#define CLOSE_NAMESPACE_ACADO
Allows to export automatically generated algorithms for fast model predictive control.
ExportStruct
Encapsulates all user interaction for setting options, logging data and plotting results.
virtual returnValue getFunctionDeclarations(ExportStatementBlock &declarations) const
virtual uint getDim() const
returnValue addStatement(const ExportStatement &_statement)
#define ASSERT(x)
ExportFunction & setReturnValue(const ExportVariable &_functionReturnValue, bool _returnAsPointer=false)
std::string getName() const
virtual returnValue getDataDeclarations(ExportStatementBlock &declarations, ExportStruct dataStruct=ACADO_ANY) const
#define BEGIN_NAMESPACE_ACADO
virtual returnValue getCode(ExportStatementBlock &code)
returnValue addFunction(const ExportFunction &_function)
returnValue init(const std::string &_name, unsigned _dim, bool _unrolling=false)
Allows to export code for a block of statements.
Defines a matrix-valued variable to be used for exporting code.
std::string getName() const
Definition: export_data.cpp:86
virtual uint getNumRows() const


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