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) const {
61  res[0] = project(arg[0], sparsity());
62  }
63 
64  void Project::ad_forward(const std::vector<std::vector<MX> >& fseed,
65  std::vector<std::vector<MX> >& fsens) const {
66  casadi_int nfwd = fsens.size();
67  for (casadi_int d=0; d<nfwd; ++d) {
68  fsens[d][0] = project(fseed[d][0], sparsity() * dep().sparsity(), true);
69  }
70  }
71 
72  void Project::ad_reverse(const std::vector<std::vector<MX> >& aseed,
73  std::vector<std::vector<MX> >& asens) const {
74  casadi_int nadj = aseed.size();
75  for (casadi_int d=0; d<nadj; ++d) {
76  asens[d][0] += project(aseed[d][0], sparsity() * dep().sparsity(), true);
77  }
78  }
79 
80  int Project::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
81  sparsity().set(res[0], arg[0], dep().sparsity());
82  return 0;
83  }
84 
85  int Project::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
86  dep().sparsity().bor(arg[0], res[0], sparsity());
87  std::fill(res[0], res[0]+nnz(), 0);
88  return 0;
89  }
90 
92  const std::vector<casadi_int>& arg,
93  const std::vector<casadi_int>& res,
94  const std::vector<bool>& arg_is_ref,
95  std::vector<bool>& res_is_ref) const {
96  g << g.project(g.work(arg.front(), dep().nnz(), arg_is_ref.front()), dep(0).sparsity(),
97  g.work(res.front(), nnz(), false), sparsity(), "w") << "\n";
98  }
99 
102  s.pack("Project::type", 'n');
103  }
104 
106  MXNode::serialize_type(s); // NOLINT
107  s.pack("Project::type", 'd');
108  }
109 
111  MXNode::serialize_type(s); // NOLINT
112  s.pack("Project::type", 's');
113  }
114 
116  char t;
117  s.unpack("Project::type", t);
118  switch (t) {
119  case 'n':
120  return new Project(s);
121  case 'd':
122  return new Densify(s);
123  case 's':
124  return new Sparsify(s);
125  default:
126  casadi_assert_dev(false);
127  }
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.densify(g.work(arg.front(), dep().nnz(), arg_is_ref.front()), dep(0).sparsity(),
136  g.work(res.front(), nnz(), false)) << "\n";
137  }
138 
140  const std::vector<casadi_int>& arg,
141  const std::vector<casadi_int>& res,
142  const std::vector<bool>& arg_is_ref,
143  std::vector<bool>& res_is_ref) const {
144  g << g.sparsify(g.work(arg.front(), dep().nnz(), arg_is_ref.front()),
145  g.work(res.front(), nnz(), false), sparsity()) << "\n";
146  }
147 
148  template<typename T>
149  int Densify::eval_gen(const T** arg, T** res, casadi_int* iw, T* w) const {
150  casadi_densify(arg[0], dep().sparsity(), res[0], false);
151  return 0;
152  }
153 
154  template<typename T>
155  int Sparsify::eval_gen(const T** arg, T** res, casadi_int* iw, T* w) const {
156  casadi_sparsify(arg[0], res[0], sparsity(), false);
157  return 0;
158  }
159 
160  int Densify::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
161  return eval_gen<double>(arg, res, iw, w);
162  }
163 
164  int Densify::eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
165  return eval_gen<SXElem>(arg, res, iw, w);
166  }
167 
168  int Sparsify::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
169  return eval_gen<double>(arg, res, iw, w);
170  }
171 
172  int Sparsify::eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
173  return eval_gen<SXElem>(arg, res, iw, w);
174  }
175 
176 } // 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:146
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: project.cpp:160
void serialize_type(SerializingStream &s) const override
Serialize type information.
Definition: project.cpp:105
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
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: project.cpp:164
int eval_gen(const T **arg, T **res, casadi_int *iw, T *w) const
Evaluate the function (template)
Definition: project.cpp:149
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 void serialize_type(SerializingStream &s) const
Serialize type information.
Definition: mx_node.cpp:528
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
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: project.cpp:56
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
Definition: project.cpp:60
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
Definition: project.cpp:85
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:64
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:80
void serialize_type(SerializingStream &s) const override
Serialize type information.
Definition: project.cpp:100
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:91
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:72
static MXNode * deserialize(DeserializingStream &s)
Deserialize without type information.
Definition: project.cpp:115
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:110
int eval_gen(const T **arg, T **res, casadi_int *iw, T *w) const
Evaluate the function (template)
Definition: project.cpp:155
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: project.cpp:168
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: project.cpp:172
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:139
General sparsity class.
Definition: sparsity.hpp:106
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.
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.