multiplication.cpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_MULTIPLICATION_CPP
27 #define CASADI_MULTIPLICATION_CPP
28 
29 #include "multiplication.hpp"
30 #include "casadi_misc.hpp"
31 #include "function_internal.hpp"
32 #include "serializing_stream.hpp"
33 
34 namespace casadi {
35 
36  Multiplication::Multiplication(const MX& z, const MX& x, const MX& y) {
37  casadi_assert(x.size2() == y.size1() && x.size1() == z.size1()
38  && y.size2() == z.size2(),
39  "Multiplication::Multiplication: dimension mismatch. Attempting to multiply "
40  + x.dim() + " with " + y.dim()
41  + " and add the result to " + z.dim());
42 
43  set_dep(z, x, y);
45  }
46 
47  std::string Multiplication::disp(const std::vector<std::string>& arg) const {
48  return "mac(" + arg.at(1) + "," + arg.at(2) + "," + arg.at(0) + ")";
49  }
50 
51  int Multiplication::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
52  return eval_gen<double>(arg, res, iw, w);
53  }
54 
56  eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
57  return eval_gen<SXElem>(arg, res, iw, w);
58  }
59 
60  template<typename T>
61  int Multiplication::eval_gen(const T** arg, T** res, casadi_int* iw, T* w) const {
62  if (arg[0]!=res[0]) std::copy(arg[0], arg[0]+dep(0).nnz(), res[0]);
63  casadi_mtimes(arg[1], dep(1).sparsity(),
64  arg[2], dep(2).sparsity(),
65  res[0], sparsity(), w, false);
66  return 0;
67  }
68 
69  void Multiplication::ad_forward(const std::vector<std::vector<MX> >& fseed,
70  std::vector<std::vector<MX> >& fsens) const {
71  for (casadi_int d=0; d<fsens.size(); ++d) {
72  fsens[d][0] = fseed[d][0]
73  + mac(dep(1), fseed[d][2], MX::zeros(dep(0).sparsity()))
74  + mac(fseed[d][1], dep(2), MX::zeros(dep(0).sparsity()));
75  }
76  }
77 
78  void Multiplication::ad_reverse(const std::vector<std::vector<MX> >& aseed,
79  std::vector<std::vector<MX> >& asens) const {
80  for (casadi_int d=0; d<aseed.size(); ++d) {
81  asens[d][1] += mac(aseed[d][0], dep(2).T(), MX::zeros(dep(1).sparsity()));
82  asens[d][2] += mac(dep(1).T(), aseed[d][0], MX::zeros(dep(2).sparsity()));
83  asens[d][0] += aseed[d][0];
84  }
85  }
86 
87  void Multiplication::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res) const {
88  res[0] = mac(arg[1], arg[2], arg[0]);
89  }
90 
91  void Multiplication::eval_linear(const std::vector<std::array<MX, 3> >& arg,
92  std::vector<std::array<MX, 3> >& res) const {
93  const std::array<MX, 3>& x = arg[1];
94  const std::array<MX, 3>& y = arg[2];
95  const std::array<MX, 3>& z = arg[0];
96  std::array<MX, 3>& f = res[0];
97  f[0] = mac(x[0], y[0], z[0]);
98  f[1] = mac(x[0], y[1], z[1]);
99  f[1] = mac(x[1], y[0], f[1]);
100  f[2] = mac(x[1]+x[2], y[1]+y[2], z[2]);
101  f[2] = mac(x[2], y[0], f[2]);
102  f[2] = mac(x[0], y[2], f[2]);
103  }
104 
106  sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
107  copy_fwd(arg[0], res[0], nnz());
108  Sparsity::mul_sparsityF(arg[1], dep(1).sparsity(),
109  arg[2], dep(2).sparsity(),
110  res[0], sparsity(), w);
111  return 0;
112  }
113 
115  sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
116  Sparsity::mul_sparsityR(arg[1], dep(1).sparsity(),
117  arg[2], dep(2).sparsity(),
118  res[0], sparsity(), w);
119  copy_rev(arg[0], res[0], nnz());
120  return 0;
121  }
122 
124  const std::vector<casadi_int>& arg,
125  const std::vector<casadi_int>& res,
126  const std::vector<bool>& arg_is_ref,
127  std::vector<bool>& res_is_ref) const {
128  // Copy first argument if not inplace
129  if (arg[0]!=res[0] || arg_is_ref[0]) {
130  g << g.copy(g.work(arg[0], nnz(), arg_is_ref[0]),
131  nnz(),
132  g.work(res[0], nnz(), false)) << '\n';
133  }
134 
135  // Perform sparse matrix multiplication
136  g << g.mtimes(g.work(arg[1], dep(1).nnz(), arg_is_ref[1]), dep(1).sparsity(),
137  g.work(arg[2], dep(2).nnz(), arg_is_ref[2]), dep(2).sparsity(),
138  g.work(res[0], nnz(), false), sparsity(), "w", false) << '\n';
139  }
140 
143  const std::vector<casadi_int>& arg,
144  const std::vector<casadi_int>& res,
145  const std::vector<bool>& arg_is_ref,
146  std::vector<bool>& res_is_ref) const {
147  // Copy first argument if not inplace
148  if (arg[0]!=res[0] || arg_is_ref[0]) {
149  g << g.copy(g.work(arg[0], nnz(), arg_is_ref[0]), nnz(),
150  g.work(res[0], nnz(), false)) << '\n';
151  }
152 
153  casadi_int nrow_x = dep(1).size1(), nrow_y = dep(2).size1(), ncol_y = dep(2).size2();
154  g.local("rr", "casadi_real", "*");
155  g.local("cs", "const casadi_real", "*");
156  g.local("ct", "const casadi_real", "*");
157  g.local("i", "casadi_int");
158  g.local("j", "casadi_int");
159  g.local("k", "casadi_int");
160  g << "for (i=0, rr=" << g.work(res[0], nnz(), false) <<"; i<" << ncol_y << "; ++i)"
161  << " for (j=0; j<" << nrow_x << "; ++j, ++rr)"
162  << " for (k=0, cs=" << g.work(arg[1], dep(1).nnz(), arg_is_ref[1]) << "+j, ct="
163  << g.work(arg[2], dep(2).nnz(), arg_is_ref[2]) << "+i*" << nrow_y << "; "
164  << "k<" << nrow_y << "; ++k)"
165  << " *rr += cs[k*" << nrow_x << "]**ct++;\n";
166  }
167 
170  s.pack("Multiplication::dense", false);
171  }
172 
174  MXNode::serialize_type(s); // NOLINT
175  s.pack("Multiplication::dense", true);
176  }
177 
179  bool dense;
180  s.unpack("Multiplication::dense", dense);
181  if (dense) {
182  return new DenseMultiplication(s);
183  } else {
184  return new Multiplication(s);
185  }
186 
187  }
188 
189 } // namespace casadi
190 
191 #endif // CASADI_MULTIPLICATION_CPP
Helper class for C code generation.
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
std::string copy(const std::string &arg, std::size_t n, const std::string &res)
Create a copy operation.
void local(const std::string &name, const std::string &type, const std::string &ref="")
Declare a local variable.
std::string mtimes(const std::string &x, const Sparsity &sp_x, const std::string &y, const Sparsity &sp_y, const std::string &z, const Sparsity &sp_z, const std::string &w, bool tr)
Codegen sparse matrix-matrix multiplication.
An MX atomic for matrix-matrix product,.
void serialize_type(SerializingStream &s) const override
Serialize specific part of node.
void generate(CodeGenerator &g, const std::vector< casadi_int > &arg, const std::vector< casadi_int > &res, const std::vector< bool > &arg_is_ref, std::vector< bool > &res_is_ref) const override
Generate code for the operation.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
casadi_int size2() const
Get the second dimension (i.e. number of columns)
casadi_int size1() const
Get the first dimension (i.e. number of rows)
std::string dim(bool with_nz=false) const
Get string representation of dimensions.
static MX zeros(casadi_int nrow=1, casadi_int ncol=1)
Create a dense matrix or a matrix with specified sparsity with all entries zero.
Node class for MX objects.
Definition: mx_node.hpp:51
virtual void serialize_type(SerializingStream &s) const
Serialize type information.
Definition: mx_node.cpp:528
static void copy_fwd(const bvec_t *arg, bvec_t *res, casadi_int len)
Propagate sparsities forward through a copy operation.
Definition: mx_node.cpp:1241
static void copy_rev(bvec_t *arg, bvec_t *res, casadi_int len)
Propagate sparsities backwards through a copy operation.
Definition: mx_node.cpp:1247
const Sparsity & sparsity() const
Get the sparsity.
Definition: mx_node.hpp:372
casadi_int nnz(casadi_int i=0) const
Definition: mx_node.hpp:389
const MX & dep(casadi_int ind=0) const
dependencies - functions that have to be evaluated before this one
Definition: mx_node.hpp:354
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
Definition: mx_node.cpp:222
void set_dep(const MX &dep)
Set unary dependency.
Definition: mx_node.cpp:226
MX - Matrix expression.
Definition: mx.hpp:92
const Sparsity & sparsity() const
Get the sparsity pattern.
Definition: mx.cpp:592
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
void serialize_type(SerializingStream &s) const override
Serialize specific part of node.
void generate(CodeGenerator &g, const std::vector< casadi_int > &arg, const std::vector< casadi_int > &res, const std::vector< bool > &arg_is_ref, std::vector< bool > &res_is_ref) const override
Generate code for the operation.
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
static MXNode * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
int eval_gen(const T **arg, T **res, casadi_int *iw, T *w) const
Evaluate the function (template)
Multiplication(const MX &z, const MX &x, const MX &y)
Constructor.
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
void eval_linear(const std::vector< std::array< MX, 3 > > &arg, std::vector< std::array< MX, 3 > > &res) const override
Evaluate the MX node on a const/linear/nonlinear partition.
The basic scalar symbolic class of CasADi.
Definition: sx_elem.hpp:75
Helper class for Serialization.
void pack(const Sparsity &e)
Serializes an object to the output stream.
static void mul_sparsityR(bvec_t *x, const Sparsity &x_sp, bvec_t *y, const Sparsity &y_sp, bvec_t *z, const Sparsity &z_sp, bvec_t *w)
Propagate sparsity using 0-1 logic through a matrix product,.
Definition: sparsity.cpp:1791
static void mul_sparsityF(const bvec_t *x, const Sparsity &x_sp, const bvec_t *y, const Sparsity &y_sp, bvec_t *z, const Sparsity &z_sp, bvec_t *w)
Propagate sparsity using 0-1 logic through a matrix product,.
Definition: sparsity.cpp:1747
The casadi namespace.
Definition: archiver.cpp:28
unsigned long long bvec_t
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)
Sparse matrix-matrix multiplication: z <- z + x*y.