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) const {
42  res[0] = bilin(arg[0], arg[1], arg[2]);
43  }
44 
45  void Bilin::ad_forward(const std::vector<std::vector<MX> >& fseed,
46  std::vector<std::vector<MX> >& fsens) const {
47  for (casadi_int d=0; d<fsens.size(); ++d) {
48  fsens[d][0]
49  = bilin(fseed[d][0], dep(1), dep(2))
50  + bilin(dep(0), fseed[d][1], dep(2))
51  + bilin(dep(0), dep(1), fseed[d][2]);
52  }
53  }
54 
55  void Bilin::ad_reverse(const std::vector<std::vector<MX> >& aseed,
56  std::vector<std::vector<MX> >& asens) const {
57  for (casadi_int d=0; d<aseed.size(); ++d) {
58  asens[d][0] = rank1(project(asens[d][0], dep(0).sparsity()),
59  aseed[d][0], dep(1), dep(2));
60  asens[d][1] += aseed[d][0] * mtimes(dep(0), dep(2));
61  asens[d][2] += aseed[d][0] * mtimes(dep(0).T(), dep(1));
62  }
63  }
64 
65  int Bilin::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
66  return eval_gen<double>(arg, res, iw, w);
67  }
68 
69  int Bilin::eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
70  return eval_gen<SXElem>(arg, res, iw, w);
71  }
72 
73  template<typename T>
74  int Bilin::eval_gen(const T** arg, T** res, casadi_int* iw, T* w) const {
75  *res[0] = casadi_bilin(arg[0], dep(0).sparsity(), arg[1], arg[2]);
76  return 0;
77  }
78 
79  int Bilin::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
80  /* Return value */
81  bvec_t r=0;
82 
83  /* Loop over the columns of A */
84  SparsityStruct sp_A = dep(0).sparsity();
85  casadi_int cc, rr, el;
86  for (cc=0; cc<sp_A.ncol; ++cc) {
87  /* Loop over the nonzeros of A */
88  for (el=sp_A.colind[cc]; el<sp_A.colind[cc+1]; ++el) {
89  /* Get the row */
90  rr=sp_A.row[el];
91 
92  /* Add contribution */
93  r |= arg[1][rr] | arg[0][el] | arg[2][cc];
94  }
95  }
96  *res[0] = r;
97  return 0;
98  }
99 
100  int Bilin::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
101  /* Seed */
102  bvec_t s_r=res[0][0]; res[0][0] = 0;
103 
104  /* Loop over the columns of A */
105  SparsityStruct sp_A = dep(0).sparsity();
106  casadi_int cc, rr, el;
107  for (cc=0; cc<sp_A.ncol; ++cc) {
108  /* Loop over the nonzeros of A */
109  for (el=sp_A.colind[cc]; el<sp_A.colind[cc+1]; ++el) {
110  /* Get the row */
111  rr=sp_A.row[el];
112 
113  /* Add contribution */
114  arg[0][el] |= s_r;
115  arg[1][rr] |= s_r;
116  arg[2][cc] |= s_r;
117  }
118  }
119  return 0;
120  }
121 
123  const std::vector<casadi_int>& arg,
124  const std::vector<casadi_int>& res,
125  const std::vector<bool>& arg_is_ref,
126  std::vector<bool>& res_is_ref) const {
127  g << g.workel(res[0]) << " = "
128  << g.bilin(g.work(arg[0], dep(0).nnz(), arg_is_ref[0]), dep(0).sparsity(),
129  g.work(arg[1], dep(1).nnz(), arg_is_ref[1]),
130  g.work(arg[2], dep(2).nnz(), arg_is_ref[2])) << ";\n";
131  }
132 
133 } // 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:79
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:122
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
Definition: bilin.cpp:100
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:55
int eval_gen(const T **arg, T **res, casadi_int *iw, T *w) const
Evaluate the function (template)
Definition: bilin.cpp:74
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:65
Bilin(const MX &A, const MX &x, const MX &y)
Constructor.
Definition: bilin.cpp:30
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
Definition: bilin.cpp:41
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: bilin.cpp:69
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:45
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: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
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