26 #ifndef CASADI_MULTIPLICATION_CPP
27 #define CASADI_MULTIPLICATION_CPP
29 #include "multiplication.hpp"
30 #include "casadi_misc.hpp"
31 #include "function_internal.hpp"
32 #include "serializing_stream.hpp"
39 "Multiplication::Multiplication: dimension mismatch. Attempting to multiply "
40 + x.
dim() +
" with " + y.
dim()
41 +
" and add the result to " + z.
dim());
48 return "mac(" + arg.at(1) +
"," + arg.at(2) +
"," + arg.at(0) +
")";
52 return eval_gen<double>(arg, res, iw, w);
57 return eval_gen<SXElem>(arg, res, iw, w);
62 if (arg[0]!=res[0]) std::copy(arg[0], arg[0]+
dep(0).
nnz(), res[0]);
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]
79 std::vector<std::vector<MX> >& asens)
const {
80 for (casadi_int d=0; d<aseed.size(); ++d) {
83 asens[d][0] += aseed[d][0];
88 res[0] = mac(arg[1], arg[2], arg[0]);
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]);
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 {
129 if (arg[0]!=res[0] || arg_is_ref[0]) {
132 g.
work(res[0],
nnz(),
false)) <<
'\n';
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 {
148 if (arg[0]!=res[0] || arg_is_ref[0]) {
150 g.
work(res[0],
nnz(),
false)) <<
'\n';
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";
170 s.
pack(
"Multiplication::dense",
false);
175 s.
pack(
"Multiplication::dense",
true);
180 s.
unpack(
"Multiplication::dense", dense);
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.
virtual void serialize_type(SerializingStream &s) const
Serialize type information.
static void copy_fwd(const bvec_t *arg, bvec_t *res, casadi_int len)
Propagate sparsities forward through a copy operation.
static void copy_rev(bvec_t *arg, bvec_t *res, casadi_int len)
Propagate sparsities backwards through a copy operation.
const Sparsity & sparsity() const
Get the sparsity.
casadi_int nnz(casadi_int i=0) const
const MX & dep(casadi_int ind=0) const
dependencies - functions that have to be evaluated before this one
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
void set_dep(const MX &dep)
Set unary dependency.
const Sparsity & sparsity() const
Get the sparsity pattern.
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.
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,.
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,.
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.