acado_csparse.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 #ifndef __MATLAB__
35 
36 #if defined( ACADO_CMAKE_BUILD ) && defined( __cplusplus )
37 extern "C"
38 {
39 #endif // ACADO_CMAKE_BUILD
40 #include "../../csparse/cs.h"
41 
42 #if defined( ACADO_CMAKE_BUILD ) && defined( __cplusplus )
43 }
44 #endif // ACADO_CMAKE_BUILD
45 
47 
48 //
49 // PUBLIC MEMBER FUNCTIONS:
50 //
51 
53 {
54  dim = 0;
55  nDense = 0;
56  index1 = 0;
57  index2 = 0;
58  x = 0;
59  S = 0;
60  N = 0;
61  TOL = 1e-14;
62  printLevel = LOW;
63 }
64 
66 {
67  int run1;
68 
69  dim = arg.dim;
70  nDense = arg.nDense;
71  index1 = 0;
72  index2 = 0;
73 
74  if (arg.x == 0)
75  x = 0;
76  else
77  {
78  x = new double[dim];
79  for (run1 = 0; run1 < dim; run1++)
80  x[run1] = arg.x[run1];
81  }
82 
83  S = 0;
84  N = 0;
85 
86  TOL = arg.TOL;
87  printLevel = arg.printLevel;
88 }
89 
91 {
92  if (index1 != 0)
93  delete[] index1;
94  if (index2 != 0)
95  delete[] index2;
96  if (x != 0)
97  delete[] x;
98 
99  if (S != 0)
100  cs_free(S);
101  if (N != 0)
102  {
103 
104  if (N->L != 0)
105  cs_spfree(N->L);
106  if (N->U != 0)
107  cs_spfree(N->U);
108  if (N->pinv != 0)
109  free(N->pinv);
110  if (N->B != 0)
111  free(N->B);
112  free(N);
113  }
114 }
115 
117 {
118 
119  return new ACADOcsparse(*this);
120 }
121 
123 {
124  // CONSISTENCY CHECKS:
125  // -------------------
126  if (dim <= 0)
128  if (nDense <= 0)
130  if (S == 0)
132 
133  // CASE: LU
134 
135  cs_ipvec(N->pinv, b, x, dim); /* x = b(p) */
136  cs_lsolve(N->L, x); /* x = L\x */
137  cs_usolve(N->U, x); /* x = U\x */
138  cs_ipvec(S->q, x, b, dim); /* b(q) = x */
139 
140  return SUCCESSFUL_RETURN;
141 }
142 
144 {
145  // CONSISTENCY CHECKS:
146  // -------------------
147  if (dim <= 0)
149  if (nDense <= 0)
151  if (S == 0)
153 
154  // CASE: LU
155 
156  cs_ipvec(N->pinv, b, x, dim); /* x = b(p) */
157  cs_utsolve(N->U, x); /* x = U'\x */
158  cs_ltsolve(N->L, x); /* x = L'\x */
159  cs_ipvec(S->q, x, b, dim); /* b(q) = x */
160 
161  return SUCCESSFUL_RETURN;
162 }
163 
165 {
166  dim = n;
167 
168  if (x != 0)
169  {
170  delete[] x;
171  x = 0;
172  }
173  x = new double[dim];
174 
175  return SUCCESSFUL_RETURN;
176 }
177 
179 {
180  nDense = nDense_;
181  return SUCCESSFUL_RETURN;
182 }
183 
184 returnValue ACADOcsparse::setIndices(const int *rowIdx_, const int *colIdx_)
185 {
186  if (index1 != 0)
187  delete[] index1;
188  if (index2 != 0)
189  delete[] index2;
190 
191  int run1;
192 
193  index1 = new int[nDense];
194  index2 = new int[nDense];
195 
196  for (run1 = 0; run1 < nDense; run1++)
197  {
198  index1[run1] = rowIdx_[run1];
199  index2[run1] = colIdx_[run1];
200  }
201  return SUCCESSFUL_RETURN;
202 }
203 
205 {
206  int run1;
207  int order = 0;
208 
209  if (dim <= 0)
211  if (nDense <= 0)
213 
214  cs *C, *D;
215  C = cs_spalloc(0, 0, 1, 1, 1);
216 
217  for (run1 = 0; run1 < nDense; run1++)
218  cs_entry(C, index1[run1], index2[run1], A_[run1]);
219 
220  D = cs_compress(C);
221  S = cs_sqr(order, D, 0);
222  N = cs_lu(D, S, TOL);
223 
224  cs_spfree(C);
225  cs_spfree(D);
226 
227  return SUCCESSFUL_RETURN;
228 }
229 
231 {
232  int run1;
233  for (run1 = 0; run1 < dim; run1++)
234  x_[run1] = x[run1];
235 
236  return SUCCESSFUL_RETURN;
237 }
238 
240 {
241  TOL = TOL_;
242  return SUCCESSFUL_RETURN;
243 }
244 
246 {
247  printLevel = printLevel_;
248  return SUCCESSFUL_RETURN;
249 }
250 
252 
253 #else // __MATLAB__
254 
256 
257 //
258 // PUBLIC MEMBER FUNCTIONS:
259 //
260 
262 {
263 
264 }
265 
267 {}
268 
270 {}
271 
273 {
274  return new ACADOcsparse(*this);
275 }
276 
277 returnValue ACADOcsparse::solve( double *b )
278 {
280 }
281 
283 {
285 }
286 
287 returnValue ACADOcsparse::setNumberOfEntries( const int &nDense_ )
288 {
290 }
291 
292 returnValue ACADOcsparse::setIndices( const int *rowIdx_, const int *colIdx_ )
293 {
295 }
296 
298 {
300 }
301 
303 {
305 }
306 
307 returnValue ACADOcsparse::getX( double *x_ )
308 {
310 }
311 
313 {
315 }
316 
318 {
320 }
321 
323 
324 #endif // __MATLAB__
325 
326 
327 /*
328  * end of file
329  */
PrintLevel printLevel
css * cs_sqr(int order, const cs *A, int qr)
Definition: cs_sqr.c:60
int * pinv
Definition: cs.h:66
int cs_lsolve(const cs *L, double *x)
Definition: cs_lsolve.c:3
double * B
Definition: cs.h:67
cs * cs_spfree(cs *A)
Definition: cs_util.c:32
cs * cs_spalloc(int m, int n, int nzmax, int values, int triplet)
Definition: cs_util.c:3
int cs_ltsolve(const cs *L, double *x)
Definition: cs_ltsolve.c:3
Allows to pass back messages to the calling function.
virtual returnValue setMatrix(double *A_)
cs * U
Definition: cs.h:65
void * cs_free(void *p)
Definition: cs_malloc.c:22
#define CLOSE_NAMESPACE_ACADO
int cs_ipvec(const int *p, const double *b, double *x, int n)
Definition: cs_ipvec.c:3
virtual returnValue setPrintLevel(PrintLevel printLevel_)
int cs_usolve(const cs *U, double *x)
Definition: cs_usolve.c:3
virtual returnValue setIndices(const int *rowIdx_, const int *colIdx_)
int cs_utsolve(const cs *U, double *x)
Definition: cs_utsolve.c:3
virtual ~ACADOcsparse()
virtual ACADOcsparse * clone() const
int cs_entry(cs *T, int i, int j, double x)
Definition: cs_entry.c:3
Definition: cs.h:16
virtual returnValue setNumberOfEntries(const int &nDense_)
virtual returnValue getX(double *x_)
cs_symbolic * S
PrintLevel
int * q
Definition: cs.h:53
cs * cs_compress(const cs *T)
Definition: cs_compress.c:3
virtual returnValue setDimension(const int &n)
#define BEGIN_NAMESPACE_ACADO
virtual returnValue setTolerance(double TOL_)
virtual returnValue solve(double *b)
csn * cs_lu(const cs *A, const css *S, double tol)
Definition: cs_lu.c:3
cs * L
Definition: cs.h:64
(not yet documented)
cs_numeric * N
#define ACADOERROR(retval)
virtual returnValue solveTranspose(double *b)


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