repmat.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 "repmat.hpp"
27 #include "casadi_misc.hpp"
28 #include "serializing_stream.hpp"
29 
30 namespace casadi {
31 
32  HorzRepmat::HorzRepmat(const MX& x, casadi_int n) : n_(n) {
33  set_dep(x);
34  set_sparsity(repmat(x.sparsity(), 1, n));
35  }
36 
37  std::string HorzRepmat::disp(const std::vector<std::string>& arg) const {
38  std::stringstream ss;
39  ss << "repmat(" << arg.at(0) << ", " << n_ << ")";
40  return ss.str();
41  }
42 
43  template<typename T>
44  int HorzRepmat::eval_gen(const T** arg, T** res, casadi_int* iw, T* w) const {
45  casadi_int nnz = dep(0).nnz();
46  for (casadi_int i=0; i<n_; ++i) {
47  std::copy(arg[0], arg[0]+nnz, res[0]+i*nnz);
48  }
49  return 0;
50  }
51 
52  int HorzRepmat::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 HorzRepmat::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 HorzRepmat::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
61  const std::vector<bool>& unique) const {
62  res[0] = arg[0]->get_repmat(1, n_);
63  }
64 
65  MX HorzRepmat::get_nzref(const Sparsity& sp, const std::vector<casadi_int>& nz,
66  bool unique) const {
67  // Each block holds the nonzeros of dep(0) in order: refer to those directly
68  casadi_int nnz = dep(0).nnz();
69  std::vector<casadi_int> nz_new(nz);
70  for (auto&& j : nz_new) if (j>=0) j %= nnz;
71  return dep(0)->get_nzref(sp, nz_new);
72  }
73 
74  static bvec_t Orring(bvec_t x, bvec_t y) { return x | y; }
75 
76  int HorzRepmat::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
77  return eval_gen<bvec_t>(arg, res, iw, w);
78  }
79 
80  int HorzRepmat::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
81  casadi_int nnz = dep(0).nnz();
82  casadi_int NNZ = sparsity().nnz();
83  for (casadi_int i=0;i<n_;++i) {
84  std::transform(res[0]+i*nnz, res[0]+(i+1)*nnz, arg[0], arg[0], &Orring);
85  }
86  std::fill(res[0], res[0]+NNZ, 0);
87  return 0;
88  }
89 
90  void HorzRepmat::ad_forward(const std::vector<std::vector<MX> >& fseed,
91  std::vector<std::vector<MX> >& fsens) const {
92  for (casadi_int d=0; d<fsens.size(); ++d) {
93  fsens[d][0] = fseed[d][0]->get_repmat(1, n_);
94  }
95  }
96 
97  void HorzRepmat::ad_reverse(const std::vector<std::vector<MX> >& aseed,
98  std::vector<std::vector<MX> >& asens) const {
99  for (casadi_int d=0; d<asens.size(); ++d) {
100  asens[d][0] += aseed[d][0]->get_repsum(1, n_);
101  }
102  }
103 
105  const std::vector<casadi_int>& arg,
106  const std::vector<casadi_int>& res,
107  const std::vector<bool>& arg_is_ref,
108  std::vector<bool>& res_is_ref) const {
109  casadi_int nnz = dep(0).nnz();
110  g.local("i", "casadi_int");
111  g << "for (i=0;i<" << n_ << ";++i) {\n"
112  << g.copy(g.work(arg[0], dep(0).nnz(), arg_is_ref[0]), nnz,
113  g.work(res[0], sparsity().nnz(), false) + "+ i*" + str(nnz)) << "\n"
114  << "}\n";
115  }
116 
117  HorzRepsum::HorzRepsum(const MX& x, casadi_int n) : n_(n) {
118  casadi_assert_dev(x.size2() % n == 0);
119  std::vector<Sparsity> sp = horzsplit_n(x.sparsity(), n);
120  Sparsity block = sp[0];
121  for (casadi_int i=1;i<sp.size();++i) {
122  block = block+sp[i];
123  }
124  Sparsity goal = repmat(block, 1, n);
125  set_dep(project(x, goal));
126  set_sparsity(block);
127  }
128 
129  std::string HorzRepsum::disp(const std::vector<std::string>& arg) const {
130  std::stringstream ss;
131  ss << "repsum(" << arg.at(0) << ", " << n_ << ")";
132  return ss.str();
133  }
134 
135  template<typename T, typename R>
136  int HorzRepsum::eval_gen(const T** arg, T** res, casadi_int* iw, T* w,
137  R reduction) const {
138  casadi_int nnz = sparsity().nnz();
139  std::fill_n(res[0], nnz, 0);
140  for (casadi_int i=0;i<n_;++i) {
141  std::transform(arg[0]+i*nnz, arg[0]+(i+1)*nnz, res[0], res[0], reduction);
142  }
143  return 0;
144  }
145 
146  int HorzRepsum::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
147  return eval_gen<double>(arg, res, iw, w, std::plus<double>());
148  }
149 
150  int HorzRepsum::eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
151  return eval_gen<SXElem>(arg, res, iw, w, std::plus<SXElem>());
152  }
153 
154  void HorzRepsum::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
155  const std::vector<bool>& unique) const {
156  res[0] = arg[0]->get_repsum(1, n_);
157  }
158 
159  int HorzRepsum::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
160  casadi_int nnz = sparsity().nnz();
161  std::fill(res[0], res[0]+nnz, 0);
162  return eval_gen<bvec_t>(arg, res, iw, w, &Orring);
163  }
164 
165  int HorzRepsum::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
166  casadi_int nnz = sparsity().nnz();
167  for (casadi_int i=0;i<n_;++i) {
168  std::transform(res[0], res[0]+nnz, arg[0]+i*nnz, arg[0]+i*nnz, &Orring);
169  }
170  std::fill(res[0], res[0]+nnz, 0);
171  return 0;
172  }
173 
174  void HorzRepsum::ad_forward(const std::vector<std::vector<MX> >& fseed,
175  std::vector<std::vector<MX> >& fsens) const {
176  for (casadi_int d=0; d<fsens.size(); ++d) {
177  fsens[d][0] = fseed[d][0]->get_repsum(1, n_);
178  }
179  }
180 
181  void HorzRepsum::ad_reverse(const std::vector<std::vector<MX> >& aseed,
182  std::vector<std::vector<MX> >& asens) const {
183  for (casadi_int d=0; d<asens.size(); ++d) {
184  asens[d][0] += aseed[d][0]->get_repmat(1, n_);
185  }
186  }
187 
189  const std::vector<casadi_int>& arg,
190  const std::vector<casadi_int>& res,
191  const std::vector<bool>& arg_is_ref,
192  std::vector<bool>& res_is_ref) const {
194  casadi_int nnz = sparsity().nnz();
195  g.local("i", "casadi_int");
196  g.local("j", "casadi_int");
197  g << g.clear(g.work(res[0], nnz, false), nnz) << "\n"
198  << " for (i=0;i<" << n_ << ";++i) {\n"
199  << " for (j=0;j<" << nnz << ";++j) {\n"
200  << " " << g.work(res[0], nnz, false)<< "[j] += "
201  << g.work(arg[0], dep(0).nnz(), arg_is_ref[0]) << "[j+i*" << nnz << "];\n"
202  << " }\n"
203  << " }\n";
204  }
205 
208  s.pack("HorzRepmat::n", n_);
209  }
210 
213  s.pack("HorzRepsum::n", n_);
214  }
215 
217  s.unpack("HorzRepmat::n", n_);
218  }
219 
221  s.unpack("HorzRepsum::n", n_);
222  }
223 
224 } // namespace casadi
Helper class for C code generation.
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
std::string copy(const std::string &arg, std::size_t n, const std::string &res)
Create a copy operation.
void local(const std::string &name, const std::string &type, const std::string &ref="")
Declare a local variable.
std::string clear(const std::string &res, std::size_t n)
Create a fill operation.
void add_auxiliary(Auxiliary f, const std::vector< std::string > &inst={"casadi_real"})
Add a built-in auxiliary function.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
casadi_int nnz() const
Get the number of structural nonzeros.
casadi_int size2() const
Get the second dimension (i.e. number of columns)
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: repmat.cpp:104
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: repmat.cpp:56
HorzRepmat(const MX &x, casadi_int n)
Constructor.
Definition: repmat.cpp:32
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Definition: repmat.cpp:90
MX get_nzref(const Sparsity &sp, const std::vector< casadi_int > &nz, bool unique=false) const override
Get the nonzeros of matrix.
Definition: repmat.cpp:65
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
Definition: repmat.cpp:206
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
Definition: repmat.cpp:97
int eval_gen(const T **arg, T **res, casadi_int *iw, T *w) const
Evaluate the function (template)
Definition: repmat.cpp:44
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res, const std::vector< bool > &unique={}) const override
Evaluate symbolically (MX)
Definition: repmat.cpp:60
casadi_int n_
Definition: repmat.hpp:128
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: repmat.cpp:52
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
Definition: repmat.cpp:76
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: repmat.cpp:37
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
Definition: repmat.cpp:80
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: repmat.cpp:150
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
Definition: repmat.cpp:165
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res, const std::vector< bool > &unique={}) const override
Evaluate symbolically (MX)
Definition: repmat.cpp:154
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: repmat.cpp:188
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
Definition: repmat.cpp:159
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: repmat.cpp:146
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: repmat.cpp:129
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
Definition: repmat.cpp:211
int eval_gen(const T **arg, T **res, casadi_int *iw, T *w, R reduction) const
Evaluate the function (template)
Definition: repmat.cpp:136
HorzRepsum(const MX &x, casadi_int n)
Constructor.
Definition: repmat.cpp:117
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
Definition: repmat.cpp:181
casadi_int n_
Definition: repmat.hpp:219
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Definition: repmat.cpp:174
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:661
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
virtual void serialize_body(SerializingStream &s) const
Serialize an object without type information.
Definition: mx_node.cpp:531
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
Definition: mx_node.cpp:225
void set_dep(const MX &dep)
Set unary dependency.
Definition: mx_node.cpp:229
MX - Matrix expression.
Definition: mx.hpp:145
const Sparsity & sparsity() const
Get the sparsity pattern.
Definition: mx.cpp:615
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.
General sparsity class.
Definition: sparsity.hpp:200
casadi_int nnz() const
Number of structural nonzeros.
Definition: sparsity.cpp:150
The casadi namespace.
Definition: archiver.cpp:28
static bvec_t Orring(bvec_t x, bvec_t y)
Definition: repmat.cpp:74
unsigned long long bvec_t
std::string str(const T &v)
String representation, any type.