casadi_nd_boor_eval.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 "nd_boor_eval"
21 template<typename T1>
22 void casadi_nd_boor_eval(T1* ret, casadi_int n_dims, const T1* all_knots, const casadi_int* offset, const casadi_int* all_degree, const casadi_int* strides, const T1* c, casadi_int m, const T1* all_x, const casadi_int* lookup_mode, casadi_int* iw, T1* w) { // NOLINT(whitespace/line_length)
23  casadi_int n_iter, k, i, pivot;
24  casadi_int *boor_offset, *starts, *index, *coeff_offset;
25  T1 *cumprod, *all_boor;
26 
27  boor_offset = iw; iw+=n_dims+1;
28  starts = iw; iw+=n_dims;
29  index = iw; iw+=n_dims;
30  coeff_offset = iw;
31 
32  cumprod = w; w+= n_dims+1;
33  all_boor = w;
34 
35  boor_offset[0] = 0;
36  cumprod[n_dims] = 1;
37  coeff_offset[n_dims] = 0;
38 
39  n_iter = 1;
40  for (k=0;k<n_dims;++k) {
41  T1 *boor;
42  const T1* knots;
43  T1 x;
44  casadi_int degree, n_knots, n_b, L, start;
45  boor = all_boor+boor_offset[k];
46 
47  degree = all_degree[k];
48  knots = all_knots + offset[k];
49  n_knots = offset[k+1]-offset[k];
50  n_b = n_knots-degree-1;
51 
52  x = all_x[k];
53  L = casadi_low(x, knots+degree, n_knots-2*degree, lookup_mode[k]);
54 
55  start = L;
56  if (start>n_b-degree-1) start = n_b-degree-1;
57 
58  starts[k] = start;
59 
60  casadi_clear(boor, 2*degree+1);
61  if (x>=knots[0] && x<=knots[n_knots-1]) {
62  if (x==knots[1]) {
63  casadi_fill(boor, degree+1, 1.0);
64  } else if (x==knots[n_knots-1]) {
65  boor[degree] = 1;
66  } else if (knots[L+degree]==x) {
67  boor[degree-1] = 1;
68  } else {
69  boor[degree] = 1;
70  }
71  }
72  casadi_de_boor(x, knots+start, 2*degree+2, degree, boor);
73  boor+= degree+1;
74  n_iter*= degree+1;
75  boor_offset[k+1] = boor_offset[k] + degree+1;
76  }
77 
78  casadi_clear_casadi_int(index, n_dims);
79 
80  // Prepare cumulative product
81  for (pivot=n_dims-1;pivot>=0;--pivot) {
82  cumprod[pivot] = (*(all_boor+boor_offset[pivot]))*cumprod[pivot+1];
83  coeff_offset[pivot] = starts[pivot]*strides[pivot]+coeff_offset[pivot+1];
84  }
85 
86  for (k=0;k<n_iter;++k) {
87  casadi_int pivot = 0;
88  // accumulate result
89  for (i=0;i<m;++i) ret[i] += c[coeff_offset[0]+i]*cumprod[0];
90 
91  // Increment index
92  index[0]++;
93 
94  // Handle index overflow
95  {
96  // increment next index (forward)
97  while (index[pivot]==boor_offset[pivot+1]-boor_offset[pivot]) {
98  index[pivot] = 0;
99  if (pivot==n_dims-1) break;
100  index[++pivot]++;
101  }
102 
103  // update cumulative structures (reverse)
104  while (pivot>0) {
105  // Compute product
106  cumprod[pivot] = (*(all_boor+boor_offset[pivot]+index[pivot]))*cumprod[pivot+1];
107  // Compute offset
108  coeff_offset[pivot] = (starts[pivot]+index[pivot])*strides[pivot]+coeff_offset[pivot+1];
109  pivot--;
110  }
111  }
112 
113  // Compute product
114  cumprod[0] = (*(all_boor+index[0]))*cumprod[1];
115 
116  // Compute offset
117  coeff_offset[0] = (starts[0]+index[0])*m+coeff_offset[1];
118 
119  }
120 }