casadi_densify.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 "densify"
21 template<typename T1, typename T2>
22 void casadi_densify(const T1* x, const casadi_int* sp_x, T2* y, casadi_int tr) {
23  casadi_int nrow_x, ncol_x, i, el;
24  const casadi_int *colind_x, *row_x;
25  // Quick return - output ignored
26  if (!y) return;
27  nrow_x = sp_x[0]; ncol_x = sp_x[1];
28  colind_x = sp_x+2; row_x = sp_x+ncol_x+3;
29  // Zero out return value
30  casadi_clear(y, nrow_x*ncol_x);
31  // Quick return - input is zero
32  if (!x) return;
33  // Copy nonzeros
34  if (tr) {
35  for (i=0; i<ncol_x; ++i) {
36  for (el=colind_x[i]; el!=colind_x[i+1]; ++el) {
37  y[i + row_x[el]*ncol_x] = CASADI_CAST(T2, *x++);
38  }
39  }
40  } else {
41  for (i=0; i<ncol_x; ++i) {
42  for (el=colind_x[i]; el!=colind_x[i+1]; ++el) {
43  y[row_x[el]] = CASADI_CAST(T2, *x++);
44  }
45  y += nrow_x;
46  }
47  }
48 }