casadi_kron.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 "kron"
21 template<typename T1>
22 void casadi_kron(const T1* a, const casadi_int* sp_a, const T1* b, const casadi_int* sp_b, T1* r) {
23  casadi_int a_ncol, b_ncol, k;
24  const casadi_int *a_colind, *b_colind;
25  T1 a_v, b_v;
26 
27  k = 0;
28 
29  a_ncol = sp_a[1];
30  a_colind = sp_a+2;
31  b_ncol = sp_b[1];
32  b_colind = sp_b+2;
33 
34  // Loop over the columns
35  for (casadi_int a_cc=0; a_cc<a_ncol; ++a_cc) {
36  // Loop over the columns
37  for (casadi_int b_cc=0; b_cc<b_ncol; ++b_cc) {
38  // Loop over existing nonzeros
39  for (casadi_int a_el=a_colind[a_cc]; a_el<a_colind[a_cc+1]; ++a_el) {
40  a_v = a[a_el];
41  // Loop over existing nonzeros
42  for (casadi_int b_el=b_colind[b_cc]; b_el<b_colind[b_cc+1]; ++b_el) {
43  b_v = b[b_el];
44  r[k++] = a_v*b_v;
45  }
46  }
47  }
48  }
49 
50 }