casadi_mtimes.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 "mtimes"
21 template<typename T1>
22 void casadi_mtimes(const T1* x, const casadi_int* sp_x, const T1* y, const casadi_int* sp_y, T1* z, const casadi_int* sp_z, T1* w, casadi_int tr) { // NOLINT(whitespace/line_length)
23  casadi_int ncol_x, ncol_y, ncol_z, cc;
24  const casadi_int *colind_x, *row_x, *colind_y, *row_y, *colind_z, *row_z;
25 
26  // Get sparsities
27  ncol_x = sp_x[1];
28  colind_x = sp_x+2; row_x = sp_x + 2 + ncol_x+1;
29  ncol_y = sp_y[1];
30  colind_y = sp_y+2; row_y = sp_y + 2 + ncol_y+1;
31  ncol_z = sp_z[1];
32  colind_z = sp_z+2; row_z = sp_z + 2 + ncol_z+1;
33 
34  if (tr) {
35  // Loop over the columns of y and z
36  for (cc=0; cc<ncol_z; ++cc) {
37  casadi_int kk;
38  // Get the dense column of y
39  for (kk=colind_y[cc]; kk<colind_y[cc+1]; ++kk) {
40  w[row_y[kk]] = y[kk];
41  }
42  // Loop over the nonzeros of z
43  for (kk=colind_z[cc]; kk<colind_z[cc+1]; ++kk) {
44  casadi_int kk1;
45  casadi_int rr = row_z[kk];
46  // Loop over corresponding columns of x
47  for (kk1=colind_x[rr]; kk1<colind_x[rr+1]; ++kk1) {
48  z[kk] += x[kk1] * w[row_x[kk1]];
49  }
50  }
51  }
52  } else {
53  // Loop over the columns of y and z
54  for (cc=0; cc<ncol_y; ++cc) {
55  casadi_int kk;
56  // Get the dense column of z
57  for (kk=colind_z[cc]; kk<colind_z[cc+1]; ++kk) {
58  w[row_z[kk]] = z[kk];
59  }
60  // Loop over the nonzeros of y
61  for (kk=colind_y[cc]; kk<colind_y[cc+1]; ++kk) {
62  casadi_int kk1;
63  casadi_int rr = row_y[kk];
64  // Loop over corresponding columns of x
65  for (kk1=colind_x[rr]; kk1<colind_x[rr+1]; ++kk1) {
66  w[row_x[kk1]] += x[kk1]*y[kk];
67  }
68  }
69  // Get the sparse column of z
70  for (kk=colind_z[cc]; kk<colind_z[cc+1]; ++kk) {
71  z[kk] = w[row_z[kk]];
72  }
73  }
74  }
75 }