casadi_regularize.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 
21 // C-REPLACE "fmin" "casadi_fmin"
22 
23 // SYMBOL "lb_eig"
24 // Use Gershgorin to finds upper and lower bounds on the eigenvalues
25 template<typename T1>
26 T1 casadi_lb_eig(const casadi_int* sp_h, const T1* h) {
27  // Local variables
28  casadi_int ncol, c, k;
29  T1 center, radius;
30  const casadi_int *colind, *row;
31  // Return value
32  T1 lb_eig = 0;
33  // Get sparsity
34  ncol = sp_h[1];
35  colind = sp_h+2; row = sp_h+ncol+3;
36  for (c=0; c<ncol; ++c) {
37  // Calculate Gershgorin discs
38  center = 0;
39  radius = 0;
40  for (k=colind[c]; k<colind[c+1]; ++k) {
41  if (row[k]==c) {
42  center = h[k];
43  } else {
44  radius += fabs(h[k]);
45  }
46  }
47  // Update the eigenvalue estimates
48  if (c==0) {
49  lb_eig = center - radius;
50  } else {
51  lb_eig = fmin(lb_eig, center - radius);
52  }
53  }
54  return lb_eig;
55 }
56 
57 // SYMBOL "regularize"
58 // Add a multiple of the identity matrix to the diagonal
59 template<typename T1>
60 void casadi_regularize(const casadi_int* sp_h, T1* h, T1 reg) {
61  // Local variables
62  casadi_int ncol, c, k;
63  const casadi_int *colind, *row;
64  // Get sparsity
65  ncol = sp_h[1];
66  colind = sp_h+2; row = sp_h+ncol+3;
67  // Shift diagonal entries
68  for (c=0; c<ncol; ++c) {
69  for (k=colind[c]; k<colind[c+1]; ++k) {
70  if (row[k]==c) h[k] += reg;
71  }
72  }
73 }