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