dot.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 #include "dot.hpp"
27 namespace casadi {
28 
29  Dot::Dot(const MX& x, const MX& y) {
30  casadi_assert_dev(x.sparsity()==y.sparsity());
31  set_dep(x, y);
33  }
34 
35  std::string Dot::disp(const std::vector<std::string>& arg) const {
36  return "dot(" + arg.at(0) + ", " + arg.at(1) + ")";
37  }
38 
39  void Dot::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
40  const std::vector<bool>& unique) const {
41  res[0] = arg[0]->get_dot(arg[1]);
42  }
43 
44  void Dot::eval_linear(const std::vector<std::array<MX, 3> >& arg,
45  std::vector<std::array<MX, 3> >& res) const {
46  const std::array<MX, 3>& x = arg[0];
47  const std::array<MX, 3>& y = arg[1];
48  std::array<MX, 3>& f = res[0];
49  MX x12 = x[1]+x[2];
50  f[0] += dot(x[0], y[0]);
51  f[1] += dot(x[0], y[1]);
52  f[1] += dot(x[1], y[0]);
53  f[2] += dot(x[0], y[2]);
54  f[2] += dot(x[1]+x[2], y[1]+y[2]);
55  f[2] += dot(x[2], y[0]);
56  }
57 
58  void Dot::ad_forward(const std::vector<std::vector<MX> >& fseed,
59  std::vector<std::vector<MX> >& fsens) const {
60  for (casadi_int d=0; d<fsens.size(); ++d) {
61  fsens[d][0] = dep(0)->get_dot(fseed[d][1])
62  + fseed[d][0]->get_dot(dep(1));
63  }
64  }
65 
66  void Dot::ad_reverse(const std::vector<std::vector<MX> >& aseed,
67  std::vector<std::vector<MX> >& asens) const {
68  for (casadi_int d=0; d<aseed.size(); ++d) {
69  asens[d][0] += aseed[d][0] * dep(1);
70  asens[d][1] += aseed[d][0] * dep(0);
71  }
72  }
73 
74  int Dot::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
75  return eval_gen<double>(arg, res, iw, w);
76  }
77 
78  int Dot::eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
79  return eval_gen<SXElem>(arg, res, iw, w);
80  }
81 
82  template<typename T>
83  int Dot::eval_gen(const T** arg, T** res, casadi_int* iw, T* w) const {
84  *res[0] = casadi_dot(dep(0).nnz(), arg[0], arg[1]);
85  return 0;
86  }
87 
88  int Dot::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
89  const bvec_t *a0=arg[0], *a1=arg[1];
90  bvec_t* r = res[0];
91  const casadi_int n = dep(0).nnz();
92  *r = 0;
93  for (casadi_int i=0; i<n; ++i) {
94  *r |= *a0++ | *a1++;
95  }
96  return 0;
97  }
98 
99  int Dot::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
100  bvec_t *a0=arg[0], *a1=arg[1], *r=res[0];
101  const casadi_int n = dep(0).nnz();
102  for (casadi_int i=0; i<n; ++i) {
103  *a0++ |= *r;
104  *a1++ |= *r;
105  }
106  *r = 0;
107  return 0;
108  }
109 
111  const std::vector<casadi_int>& arg,
112  const std::vector<casadi_int>& res,
113  const std::vector<bool>& arg_is_ref,
114  std::vector<bool>& res_is_ref) const {
115  g << g.workel(res[0]) << " = "
116  << g.dot(dep().nnz(),
117  g.work(arg[0], dep(0).nnz(), arg_is_ref[0]),
118  g.work(arg[1], dep(1).nnz(), arg_is_ref[1]))
119  << ";\n";
120  }
121 
122 } // namespace casadi
Helper class for C code generation.
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
std::string workel(casadi_int n) const
std::string dot(casadi_int n, const std::string &x, const std::string &y)
Codegen inner product.
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
Definition: dot.cpp:66
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
Definition: dot.cpp:99
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
Definition: dot.cpp:88
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.
Definition: dot.cpp:44
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.
Definition: dot.cpp:110
int eval_gen(const T **arg, T **res, casadi_int *iw, T *w) const
Evaluate the function (template)
Definition: dot.cpp:83
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: dot.cpp:78
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Definition: dot.cpp:58
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res, const std::vector< bool > &unique={}) const override
Evaluate symbolically (MX)
Definition: dot.cpp:39
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: dot.cpp:74
Dot(const MX &x, const MX &y)
Constructor.
Definition: dot.cpp:29
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: dot.cpp:35
casadi_int nnz() const
Get the number of (structural) non-zero elements.
virtual MX get_dot(const MX &y) const
Inner product.
Definition: mx_node.cpp:1093
casadi_int nnz(casadi_int i=0) const
Definition: mx_node.hpp:427
const MX & dep(casadi_int ind=0) const
dependencies - functions that have to be evaluated before this one
Definition: mx_node.hpp:392
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
Definition: mx_node.cpp:224
void set_dep(const MX &dep)
Set unary dependency.
Definition: mx_node.cpp:228
MX - Matrix expression.
Definition: mx.hpp:92
const Sparsity & sparsity() const
Get the sparsity pattern.
Definition: mx.cpp:612
The basic scalar symbolic class of CasADi.
Definition: sx_elem.hpp:75
static Sparsity scalar(bool dense_scalar=true)
Create a scalar sparsity pattern *.
Definition: sparsity.hpp:153
The casadi namespace.
Definition: archiver.cpp:28
unsigned long long bvec_t
T1 casadi_dot(casadi_int n, const T1 *x, const T1 *y)
Inner product.
T dot(const std::vector< T > &a, const std::vector< T > &b)