casadi_newton.hpp
1 //
2 // MIT No Attribution
3 //
4 // Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl, KU Leuven.
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy of this
7 // software and associated documentation files (the "Software"), to deal in the Software
8 // without restriction, including without limitation the rights to use, copy, modify,
9 // merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13 // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
14 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
15 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
16 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
17 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 //
19 
20 // SYMBOL "newton_mem"
21 
22 template<typename T1>
24  casadi_int n;
25  T1 abstol;
27  T1* x;
28  T1* g;
29  T1* jac_g_x;
30 
31  const casadi_int* sp_a;
32  const casadi_int* sp_v;
33  const casadi_int* sp_r;
34  const casadi_int* prinv;
35  const casadi_int* pc;
36 
37  T1* lin_w;
38  T1* lin_v;
39  T1* lin_r;
40  T1* lin_beta;
41 };
42 
43 // C-REPLACE "casadi_newton_mem<T1>" "struct casadi_newton_mem"
44 // SYMBOL "newton"
45 template<typename T1>
46 int casadi_newton(const casadi_newton_mem<T1>* m) {
47  // Check tolerance on residual
48  if (m->abstol>0 && casadi_norm_inf(m->n, m->g) <= m->abstol) return 1;
49 
50  // Factorize J
51  casadi_qr(m->sp_a, m->jac_g_x, m->lin_w,
52  m->sp_v, m->lin_v, m->sp_r, m->lin_r, m->lin_beta,
53  m->prinv, m->pc);
54  // Solve J^(-1) g
55  casadi_qr_solve(m->g, 1, 0, m->sp_v, m->lin_v, m->sp_r, m->lin_r, m->lin_beta,
56  m->prinv, m->pc, m->lin_w);
57 
58  // Update Xk+1 = Xk - J^(-1) g
59  casadi_axpy(m->n, -1., m->g, m->x);
60 
61  // Check tolerance on step
62  if (m->abstol_step>0 && casadi_norm_inf(m->n, m->g) <= m->abstol_step) return 2;
63 
64  // We will need another newton step
65  return 0;
66 }
const casadi_int * sp_a
const casadi_int * pc
const casadi_int * sp_v
const casadi_int * sp_r
const casadi_int * prinv