bilin.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 "bilin.hpp"
27 
28 namespace casadi {
29 
30  Bilin::Bilin(const MX& A, const MX& x, const MX& y) {
31  casadi_assert(x.is_column(), "Dimension mismatch");
32  casadi_assert(y.is_column(), "Dimension mismatch");
33  set_dep(A, densify(x), densify(y));
35  }
36 
37  std::string Bilin::disp(const std::vector<std::string>& arg) const {
38  return "bilin(" + arg.at(0) + ", " + arg.at(1) + ", " + arg.at(2) + ")";
39  }
40 
41  void Bilin::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
42  const std::vector<bool>& unique) const {
43  res[0] = bilin(arg[0], arg[1], arg[2]);
44  }
45 
46  void Bilin::ad_forward(const std::vector<std::vector<MX> >& fseed,
47  std::vector<std::vector<MX> >& fsens) const {
48  for (casadi_int d=0; d<fsens.size(); ++d) {
49  fsens[d][0]
50  = bilin(fseed[d][0], dep(1), dep(2))
51  + bilin(dep(0), fseed[d][1], dep(2))
52  + bilin(dep(0), dep(1), fseed[d][2]);
53  }
54  }
55 
56  void Bilin::ad_reverse(const std::vector<std::vector<MX> >& aseed,
57  std::vector<std::vector<MX> >& asens) const {
58  for (casadi_int d=0; d<aseed.size(); ++d) {
59  asens[d][0] = rank1(project(asens[d][0], dep(0).sparsity()),
60  aseed[d][0], dep(1), dep(2));
61  asens[d][1] += aseed[d][0] * mtimes(dep(0), dep(2));
62  asens[d][2] += aseed[d][0] * mtimes(dep(0).T(), dep(1));
63  }
64  }
65 
66  int Bilin::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
67  return eval_gen<double>(arg, res, iw, w);
68  }
69 
70  int Bilin::eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
71  return eval_gen<SXElem>(arg, res, iw, w);
72  }
73 
74  template<typename T>
75  int Bilin::eval_gen(const T** arg, T** res, casadi_int* iw, T* w) const {
76  *res[0] = casadi_bilin(arg[0], dep(0).sparsity(), arg[1], arg[2]);
77  return 0;
78  }
79 
80  int Bilin::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
81  /* Return value */
82  bvec_t r=0;
83 
84  /* Loop over the columns of A */
85  SparsityStruct sp_A = dep(0).sparsity();
86  casadi_int cc, rr, el;
87  for (cc=0; cc<sp_A.ncol; ++cc) {
88  /* Loop over the nonzeros of A */
89  for (el=sp_A.colind[cc]; el<sp_A.colind[cc+1]; ++el) {
90  /* Get the row */
91  rr=sp_A.row[el];
92 
93  /* Add contribution */
94  r |= arg[1][rr] | arg[0][el] | arg[2][cc];
95  }
96  }
97  *res[0] = r;
98  return 0;
99  }
100 
101  int Bilin::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
102  /* Seed */
103  bvec_t s_r=res[0][0]; res[0][0] = 0;
104 
105  /* Loop over the columns of A */
106  SparsityStruct sp_A = dep(0).sparsity();
107  casadi_int cc, rr, el;
108  for (cc=0; cc<sp_A.ncol; ++cc) {
109  /* Loop over the nonzeros of A */
110  for (el=sp_A.colind[cc]; el<sp_A.colind[cc+1]; ++el) {
111  /* Get the row */
112  rr=sp_A.row[el];
113 
114  /* Add contribution */
115  arg[0][el] |= s_r;
116  arg[1][rr] |= s_r;
117  arg[2][cc] |= s_r;
118  }
119  }
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  g << g.workel(res[0]) << " = "
129  << g.bilin(g.work(arg[0], dep(0).nnz(), arg_is_ref[0]), dep(0).sparsity(),
130  g.work(arg[1], dep(1).nnz(), arg_is_ref[1]),
131  g.work(arg[2], dep(2).nnz(), arg_is_ref[2])) << ";\n";
132  }
133 
134 } // namespace casadi
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
Definition: bilin.cpp:80
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: bilin.cpp:123
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
Definition: bilin.cpp:101
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
Definition: bilin.cpp:56
int eval_gen(const T **arg, T **res, casadi_int *iw, T *w) const
Evaluate the function (template)
Definition: bilin.cpp:75
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: bilin.cpp:37
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: bilin.cpp:66
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res, const std::vector< bool > &unique={}) const override
Evaluate symbolically (MX)
Definition: bilin.cpp:41
Bilin(const MX &A, const MX &x, const MX &y)
Constructor.
Definition: bilin.cpp:30
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: bilin.cpp:70
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Definition: bilin.cpp:46
Helper class for C code generation.
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
std::string bilin(const std::string &A, const Sparsity &sp_A, const std::string &x, const std::string &y)
Codegen bilinear form.
std::string workel(casadi_int n) const
bool is_column() const
Check if the matrix is a column vector (i.e. size2()==1)
const Sparsity & sparsity() const
Get the sparsity.
Definition: mx_node.hpp:410
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_bilin(const T1 *A, const casadi_int *sp_A, const T1 *x, const T1 *y)
Compact representation of a sparsity pattern.
Definition: sparsity.hpp:57
const casadi_int * colind
Definition: sparsity.hpp:60
const casadi_int * row
Definition: sparsity.hpp:61