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