project.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 "project.hpp"
27 #include "casadi_misc.hpp"
28 #include <sstream>
29 #include <vector>
30 
31 namespace casadi {
32 
33  Project::Project(const MX& x, const Sparsity& sp) {
34  set_dep(x);
36  }
37 
38  std::string Project::disp(const std::vector<std::string>& arg) const {
39  if (sparsity().is_dense()) {
40  return "dense(" + arg.at(0) + ")";
41  } else {
42  return "project(" + arg.at(0) + ")";
43  }
44  }
45 
46  template<typename T>
47  int Project::eval_gen(const T** arg, T** res, casadi_int* iw, T* w) const {
48  casadi_project(arg[0], dep().sparsity(), res[0], sparsity(), w);
49  return 0;
50  }
51 
52  int Project::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
53  return eval_gen<double>(arg, res, iw, w);
54  }
55 
56  int Project::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  void Project::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
61  const std::vector<bool>& unique) const {
62  bool unique_arg0 = !unique.empty() && unique[0];
63  res[0] = arg[0]->get_project(sparsity(), unique_arg0);
64  }
65 
66  MX Project::get_project(const Sparsity& sp, bool unique) const {
67  if (unique && sp.is_subset(dep(0).sparsity())) {
68  return dep(0)->get_project(sp);
69  } else {
70  return MXNode::get_project(sp, unique);
71  }
72  }
73 
74  MX Project::get_nzref(const Sparsity& sp, const std::vector<casadi_int>& nz,
75  bool unique) const {
76  if (unique) {
77  // Decay projection into GetNonzeros
78  // Defer simplification logic to GetNonzeros::get_nzref
79 
80  std::vector<unsigned char> mapping;
82 
83  // Nonzero indices of the original matrix
84  std::vector<casadi_int> dnz;
85  casadi_int i=0;
86  for (auto e : mapping) {
87  if (e & 2) { // entry present in projection
88  if (e & 1) {
89  dnz.push_back(i); // present in original
90  } else {
91  dnz.push_back(-1); // not present in original
92  }
93  }
94  if (e & 1) i++; // Only count a nonzero when present
95  }
96 
97  return dep(0)->get_nzref(sparsity(), dnz)->get_nzref(sp, nz);
98  } else {
99  return MXNode::get_nzref(sp, nz);
100  }
101  }
102 
103  void Project::ad_forward(const std::vector<std::vector<MX> >& fseed,
104  std::vector<std::vector<MX> >& fsens) const {
105  casadi_int nfwd = fsens.size();
106  for (casadi_int d=0; d<nfwd; ++d) {
107  fsens[d][0] = project(fseed[d][0], sparsity() * dep().sparsity(), true);
108  }
109  }
110 
111  void Project::ad_reverse(const std::vector<std::vector<MX> >& aseed,
112  std::vector<std::vector<MX> >& asens) const {
113  casadi_int nadj = aseed.size();
114  for (casadi_int d=0; d<nadj; ++d) {
115  asens[d][0] += project(aseed[d][0], sparsity() * dep().sparsity(), true);
116  }
117  }
118 
119  int Project::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
120  sparsity().set(res[0], arg[0], dep().sparsity());
121  return 0;
122  }
123 
124  int Project::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
125  dep().sparsity().bor(arg[0], res[0], sparsity());
126  std::fill(res[0], res[0]+nnz(), 0);
127  return 0;
128  }
129 
131  const std::vector<casadi_int>& arg,
132  const std::vector<casadi_int>& res,
133  const std::vector<bool>& arg_is_ref,
134  std::vector<bool>& res_is_ref) const {
135  g << g.project(g.work(arg.front(), dep().nnz(), arg_is_ref.front()), dep(0).sparsity(),
136  g.work(res.front(), nnz(), false), sparsity(), "w") << "\n";
137  }
138 
141  s.pack("Project::type", 'n');
142  }
143 
145  MXNode::serialize_type(s); // NOLINT
146  s.pack("Project::type", 'd');
147  }
148 
150  MXNode::serialize_type(s); // NOLINT
151  s.pack("Project::type", 's');
152  }
153 
155  char t;
156  s.unpack("Project::type", t);
157  switch (t) {
158  case 'n':
159  return new Project(s);
160  case 'd':
161  return new Densify(s);
162  case 's':
163  return new Sparsify(s);
164  default:
165  casadi_assert_dev(false);
166  }
167  }
168 
170  const std::vector<casadi_int>& arg,
171  const std::vector<casadi_int>& res,
172  const std::vector<bool>& arg_is_ref,
173  std::vector<bool>& res_is_ref) const {
174  g << g.densify(g.work(arg.front(), dep().nnz(), arg_is_ref.front()), dep(0).sparsity(),
175  g.work(res.front(), nnz(), false)) << "\n";
176  }
177 
179  const std::vector<casadi_int>& arg,
180  const std::vector<casadi_int>& res,
181  const std::vector<bool>& arg_is_ref,
182  std::vector<bool>& res_is_ref) const {
183  g << g.sparsify(g.work(arg.front(), dep().nnz(), arg_is_ref.front()),
184  g.work(res.front(), nnz(), false), sparsity()) << "\n";
185  }
186 
187  template<typename T>
188  int Densify::eval_gen(const T** arg, T** res, casadi_int* iw, T* w) const {
189  casadi_densify(arg[0], dep().sparsity(), res[0], false);
190  return 0;
191  }
192 
193  template<typename T>
194  int Sparsify::eval_gen(const T** arg, T** res, casadi_int* iw, T* w) const {
195  casadi_sparsify(arg[0], res[0], sparsity(), false);
196  return 0;
197  }
198 
199  int Densify::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
200  return eval_gen<double>(arg, res, iw, w);
201  }
202 
203  int Densify::eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
204  return eval_gen<SXElem>(arg, res, iw, w);
205  }
206 
207  int Sparsify::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
208  return eval_gen<double>(arg, res, iw, w);
209  }
210 
211  int Sparsify::eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
212  return eval_gen<SXElem>(arg, res, iw, w);
213  }
214 
215 } // namespace casadi
Helper class for C code generation.
std::string project(const std::string &arg, const Sparsity &sp_arg, const std::string &res, const Sparsity &sp_res, const std::string &w)
Sparse assignment.
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
std::string sparsify(const std::string &arg, const std::string &res, const Sparsity &sp_res, bool tr=false)
Sparsify.
std::string densify(const std::string &arg, const Sparsity &sp_arg, const std::string &res, bool tr=false)
Densify.
Densify.
Definition: project.hpp:167
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: project.cpp:199
void serialize_type(SerializingStream &s) const override
Serialize type information.
Definition: project.cpp:144
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: project.cpp:169
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: project.cpp:203
int eval_gen(const T **arg, T **res, casadi_int *iw, T *w) const
Evaluate the function (template)
Definition: project.cpp:188
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
Node class for MX objects.
Definition: mx_node.hpp:51
virtual MX get_nzref(const Sparsity &sp, const std::vector< casadi_int > &nz, bool unique=false) const
Get the nonzeros of matrix.
Definition: mx_node.cpp:660
virtual void serialize_type(SerializingStream &s) const
Serialize type information.
Definition: mx_node.cpp:535
virtual Matrix< casadi_int > mapping() const
Get an IM representation of a GetNonzeros or SetNonzeros node.
Definition: mx_node.cpp:996
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
virtual MX get_project(const Sparsity &sp, bool unique=false) const
Create set sparse.
Definition: mx_node.cpp:759
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
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: project.cpp:56
MX get_project(const Sparsity &sp, bool unique=false) const override
Create set sparse.
Definition: project.cpp:66
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
Definition: project.cpp:124
MX get_nzref(const Sparsity &sp, const std::vector< casadi_int > &nz, bool unique=false) const override
Get the nonzeros of matrix.
Definition: project.cpp:74
Project(const MX &x, const Sparsity &sp)
Constructor.
Definition: project.cpp:33
int eval_gen(const T **arg, T **res, casadi_int *iw, T *w) const
Evaluate the function (template)
Definition: project.cpp:47
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Definition: project.cpp:103
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: project.cpp:38
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
Definition: project.cpp:119
void serialize_type(SerializingStream &s) const override
Serialize type information.
Definition: project.cpp:139
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: project.cpp:52
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: project.cpp:130
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
Definition: project.cpp:111
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res, const std::vector< bool > &unique={}) const override
Evaluate symbolically (MX)
Definition: project.cpp:60
static MXNode * deserialize(DeserializingStream &s)
Deserialize without type information.
Definition: project.cpp:154
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.
void serialize_type(SerializingStream &s) const override
Serialize type information.
Definition: project.cpp:149
int eval_gen(const T **arg, T **res, casadi_int *iw, T *w) const
Evaluate the function (template)
Definition: project.cpp:194
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: project.cpp:207
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: project.cpp:211
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: project.cpp:178
General sparsity class.
Definition: sparsity.hpp:106
bool is_subset(const Sparsity &rhs) const
Is subset?
Definition: sparsity.cpp:426
void bor(T *data, const T *val_data, const Sparsity &val_sp) const
Bitwise or of the nonzero entries of one sparsity pattern and the nonzero.
Sparsity intersect(const Sparsity &y, std::vector< unsigned char > &mapping) const
Intersection of two sparsity patterns.
Definition: sparsity.cpp:417
void set(T *data, const T *val_data, const Sparsity &val_sp) const
Assign the nonzero entries of one sparsity pattern to the nonzero.
The casadi namespace.
Definition: archiver.cpp:28
unsigned long long bvec_t
void casadi_sparsify(const T1 *x, T2 *y, const casadi_int *sp_y, casadi_int tr)
Convert dense to sparse.
void casadi_project(const T1 *x, const casadi_int *sp_x, T1 *y, const casadi_int *sp_y, T1 *w)
Sparse copy: y <- x, w work vector (length >= number of rows)
void casadi_densify(const T1 *x, const casadi_int *sp_x, T2 *y, casadi_int tr)
Convert sparse to dense.