acado_csparse.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/bindings/acado_csparse/acado_csparse.hpp>
00033 
00034 #ifndef __MATLAB__
00035 
00036 #if defined( ACADO_CMAKE_BUILD ) && defined( __cplusplus )
00037 extern "C"
00038 {
00039 #endif // ACADO_CMAKE_BUILD
00040 #include "../../csparse/cs.h"
00041 
00042 #if defined( ACADO_CMAKE_BUILD ) && defined( __cplusplus )
00043 }
00044 #endif // ACADO_CMAKE_BUILD
00045 
00046 BEGIN_NAMESPACE_ACADO
00047 
00048 //
00049 // PUBLIC MEMBER FUNCTIONS:
00050 //
00051 
00052 ACADOcsparse::ACADOcsparse()
00053 {
00054         dim = 0;
00055         nDense = 0;
00056         index1 = 0;
00057         index2 = 0;
00058         x = 0;
00059         S = 0;
00060         N = 0;
00061         TOL = 1e-14;
00062         printLevel = LOW;
00063 }
00064 
00065 ACADOcsparse::ACADOcsparse(const ACADOcsparse &arg)
00066 {
00067         int run1;
00068 
00069         dim = arg.dim;
00070         nDense = arg.nDense;
00071         index1 = 0;
00072         index2 = 0;
00073 
00074         if (arg.x == 0)
00075                 x = 0;
00076         else
00077         {
00078                 x = new double[dim];
00079                 for (run1 = 0; run1 < dim; run1++)
00080                         x[run1] = arg.x[run1];
00081         }
00082 
00083         S = 0;
00084         N = 0;
00085 
00086         TOL = arg.TOL;
00087         printLevel = arg.printLevel;
00088 }
00089 
00090 ACADOcsparse::~ACADOcsparse()
00091 {
00092         if (index1 != 0)
00093                 delete[] index1;
00094         if (index2 != 0)
00095                 delete[] index2;
00096         if (x != 0)
00097                 delete[] x;
00098 
00099         if (S != 0)
00100                 cs_free(S);
00101         if (N != 0)
00102         {
00103 
00104                 if (N->L != 0)
00105                         cs_spfree(N->L);
00106                 if (N->U != 0)
00107                         cs_spfree(N->U);
00108                 if (N->pinv != 0)
00109                         free(N->pinv);
00110                 if (N->B != 0)
00111                         free(N->B);
00112                 free(N);
00113         }
00114 }
00115 
00116 ACADOcsparse* ACADOcsparse::clone() const
00117 {
00118 
00119         return new ACADOcsparse(*this);
00120 }
00121 
00122 returnValue ACADOcsparse::solve(double *b)
00123 {
00124         // CONSISTENCY CHECKS:
00125         // -------------------
00126         if (dim <= 0)
00127                 return ACADOERROR(RET_MEMBER_NOT_INITIALISED);
00128         if (nDense <= 0)
00129                 return ACADOERROR(RET_MEMBER_NOT_INITIALISED);
00130         if (S == 0)
00131                 return ACADOERROR(RET_MEMBER_NOT_INITIALISED);
00132 
00133         // CASE: LU
00134 
00135         cs_ipvec(N->pinv, b, x, dim); /* x = b(p) */
00136         cs_lsolve(N->L, x); /* x = L\x  */
00137         cs_usolve(N->U, x); /* x = U\x  */
00138         cs_ipvec(S->q, x, b, dim); /* b(q) = x */
00139 
00140         return SUCCESSFUL_RETURN;
00141 }
00142 
00143 returnValue ACADOcsparse::solveTranspose(double *b)
00144 {
00145         // CONSISTENCY CHECKS:
00146         // -------------------
00147         if (dim <= 0)
00148                 return ACADOERROR(RET_MEMBER_NOT_INITIALISED);
00149         if (nDense <= 0)
00150                 return ACADOERROR(RET_MEMBER_NOT_INITIALISED);
00151         if (S == 0)
00152                 return ACADOERROR(RET_MEMBER_NOT_INITIALISED);
00153 
00154         // CASE: LU
00155 
00156         cs_ipvec(N->pinv, b, x, dim); /* x = b(p) */
00157         cs_utsolve(N->U, x); /* x = U'\x */
00158         cs_ltsolve(N->L, x); /* x = L'\x */
00159         cs_ipvec(S->q, x, b, dim); /* b(q) = x */
00160 
00161         return SUCCESSFUL_RETURN;
00162 }
00163 
00164 returnValue ACADOcsparse::setDimension(const int &n)
00165 {
00166         dim = n;
00167 
00168         if (x != 0)
00169         {
00170                 delete[] x;
00171                 x = 0;
00172         }
00173         x = new double[dim];
00174 
00175         return SUCCESSFUL_RETURN;
00176 }
00177 
00178 returnValue ACADOcsparse::setNumberOfEntries(const int &nDense_)
00179 {
00180         nDense = nDense_;
00181         return SUCCESSFUL_RETURN;
00182 }
00183 
00184 returnValue ACADOcsparse::setIndices(const int *rowIdx_, const int *colIdx_)
00185 {
00186         if (index1 != 0)
00187                 delete[] index1;
00188         if (index2 != 0)
00189                 delete[] index2;
00190 
00191         int run1;
00192 
00193         index1 = new int[nDense];
00194         index2 = new int[nDense];
00195 
00196         for (run1 = 0; run1 < nDense; run1++)
00197         {
00198                 index1[run1] = rowIdx_[run1];
00199                 index2[run1] = colIdx_[run1];
00200         }
00201         return SUCCESSFUL_RETURN;
00202 }
00203 
00204 returnValue ACADOcsparse::setMatrix(double *A_)
00205 {
00206         int run1;
00207         int order = 0;
00208 
00209         if (dim <= 0)
00210                 return ACADOERROR(RET_MEMBER_NOT_INITIALISED);
00211         if (nDense <= 0)
00212                 return ACADOERROR(RET_MEMBER_NOT_INITIALISED);
00213 
00214         cs *C, *D;
00215         C = cs_spalloc(0, 0, 1, 1, 1);
00216 
00217         for (run1 = 0; run1 < nDense; run1++)
00218                 cs_entry(C, index1[run1], index2[run1], A_[run1]);
00219 
00220         D = cs_compress(C);
00221         S = cs_sqr(order, D, 0);
00222         N = cs_lu(D, S, TOL);
00223 
00224         cs_spfree(C);
00225         cs_spfree(D);
00226 
00227         return SUCCESSFUL_RETURN;
00228 }
00229 
00230 returnValue ACADOcsparse::getX(double *x_)
00231 {
00232         int run1;
00233         for (run1 = 0; run1 < dim; run1++)
00234                 x_[run1] = x[run1];
00235 
00236         return SUCCESSFUL_RETURN;
00237 }
00238 
00239 returnValue ACADOcsparse::setTolerance(double TOL_)
00240 {
00241         TOL = TOL_;
00242         return SUCCESSFUL_RETURN;
00243 }
00244 
00245 returnValue ACADOcsparse::setPrintLevel(PrintLevel printLevel_)
00246 {
00247         printLevel = printLevel_;
00248         return SUCCESSFUL_RETURN;
00249 }
00250 
00251 CLOSE_NAMESPACE_ACADO
00252 
00253 #else // __MATLAB__
00254 
00255 BEGIN_NAMESPACE_ACADO
00256 
00257 //
00258 // PUBLIC MEMBER FUNCTIONS:
00259 //
00260 
00261 ACADOcsparse::ACADOcsparse( )
00262 {
00263 
00264 }
00265 
00266 ACADOcsparse::ACADOcsparse( const ACADOcsparse &arg )
00267 {}
00268 
00269 ACADOcsparse::~ACADOcsparse( )
00270 {}
00271 
00272 ACADOcsparse* ACADOcsparse::clone() const
00273 {
00274         return new ACADOcsparse(*this);
00275 }
00276 
00277 returnValue ACADOcsparse::solve( double *b )
00278 {
00279         return ACADOERROR(RET_NOT_IMPLEMENTED_YET);
00280 }
00281 
00282 returnValue ACADOcsparse::setDimension( const int &n )
00283 {
00284         return ACADOERROR(RET_NOT_IMPLEMENTED_YET);
00285 }
00286 
00287 returnValue ACADOcsparse::setNumberOfEntries( const int &nDense_ )
00288 {
00289         return ACADOERROR(RET_NOT_IMPLEMENTED_YET);
00290 }
00291 
00292 returnValue ACADOcsparse::setIndices( const int *rowIdx_, const int *colIdx_ )
00293 {
00294         return ACADOERROR(RET_NOT_IMPLEMENTED_YET);
00295 }
00296 
00297 returnValue ACADOcsparse::setMatrix( double *A_ )
00298 {
00299         return ACADOERROR(RET_NOT_IMPLEMENTED_YET);
00300 }
00301 
00302 returnValue ACADOcsparse::solveTranspose( double *b )
00303 {
00304         return ACADOERROR(RET_NOT_IMPLEMENTED_YET);
00305 }
00306 
00307 returnValue ACADOcsparse::getX( double *x_ )
00308 {
00309         return ACADOERROR(RET_NOT_IMPLEMENTED_YET);
00310 }
00311 
00312 returnValue ACADOcsparse::setTolerance( double TOL_ )
00313 {
00314         return ACADOERROR(RET_NOT_IMPLEMENTED_YET);
00315 }
00316 
00317 returnValue ACADOcsparse::setPrintLevel( PrintLevel printLevel_ )
00318 {
00319         return ACADOERROR(RET_NOT_IMPLEMENTED_YET);
00320 }
00321 
00322 CLOSE_NAMESPACE_ACADO
00323 
00324 #endif // __MATLAB__
00325 
00326 
00327 /*
00328  *   end of file
00329  */


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Thu Aug 27 2015 11:57:48