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) const {
61  res[0] = arg[0]->get_repmat(1, n_);
62  }
63 
64  static bvec_t Orring(bvec_t x, bvec_t y) { return x | y; }
65 
66  int HorzRepmat::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
67  return eval_gen<bvec_t>(arg, res, iw, w);
68  }
69 
70  int HorzRepmat::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
71  casadi_int nnz = dep(0).nnz();
72  casadi_int NNZ = sparsity().nnz();
73  for (casadi_int i=0;i<n_;++i) {
74  std::transform(res[0]+i*nnz, res[0]+(i+1)*nnz, arg[0], arg[0], &Orring);
75  }
76  std::fill(res[0], res[0]+NNZ, 0);
77  return 0;
78  }
79 
80  void HorzRepmat::ad_forward(const std::vector<std::vector<MX> >& fseed,
81  std::vector<std::vector<MX> >& fsens) const {
82  for (casadi_int d=0; d<fsens.size(); ++d) {
83  fsens[d][0] = fseed[d][0]->get_repmat(1, n_);
84  }
85  }
86 
87  void HorzRepmat::ad_reverse(const std::vector<std::vector<MX> >& aseed,
88  std::vector<std::vector<MX> >& asens) const {
89  for (casadi_int d=0; d<asens.size(); ++d) {
90  asens[d][0] += aseed[d][0]->get_repsum(1, n_);
91  }
92  }
93 
95  const std::vector<casadi_int>& arg,
96  const std::vector<casadi_int>& res,
97  const std::vector<bool>& arg_is_ref,
98  std::vector<bool>& res_is_ref) const {
99  casadi_int nnz = dep(0).nnz();
100  g.local("i", "casadi_int");
101  g << "for (i=0;i<" << n_ << ";++i) {\n"
102  << g.copy(g.work(arg[0], dep(0).nnz(), arg_is_ref[0]), nnz,
103  g.work(res[0], sparsity().nnz(), false) + "+ i*" + str(nnz)) << "\n"
104  << "}\n";
105  }
106 
107  HorzRepsum::HorzRepsum(const MX& x, casadi_int n) : n_(n) {
108  casadi_assert_dev(x.size2() % n == 0);
109  std::vector<Sparsity> sp = horzsplit_n(x.sparsity(), n);
110  Sparsity block = sp[0];
111  for (casadi_int i=1;i<sp.size();++i) {
112  block = block+sp[i];
113  }
114  Sparsity goal = repmat(block, 1, n);
115  set_dep(project(x, goal));
116  set_sparsity(block);
117  }
118 
119  std::string HorzRepsum::disp(const std::vector<std::string>& arg) const {
120  std::stringstream ss;
121  ss << "repsum(" << arg.at(0) << ", " << n_ << ")";
122  return ss.str();
123  }
124 
125  template<typename T, typename R>
126  int HorzRepsum::eval_gen(const T** arg, T** res, casadi_int* iw, T* w,
127  R reduction) const {
128  casadi_int nnz = sparsity().nnz();
129  std::fill_n(res[0], nnz, 0);
130  for (casadi_int i=0;i<n_;++i) {
131  std::transform(arg[0]+i*nnz, arg[0]+(i+1)*nnz, res[0], res[0], reduction);
132  }
133  return 0;
134  }
135 
136  int HorzRepsum::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
137  return eval_gen<double>(arg, res, iw, w, std::plus<double>());
138  }
139 
140  int HorzRepsum::eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
141  return eval_gen<SXElem>(arg, res, iw, w, std::plus<SXElem>());
142  }
143 
144  void HorzRepsum::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res) const {
145  res[0] = arg[0]->get_repsum(1, n_);
146  }
147 
148  int HorzRepsum::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
149  casadi_int nnz = sparsity().nnz();
150  std::fill(res[0], res[0]+nnz, 0);
151  return eval_gen<bvec_t>(arg, res, iw, w, &Orring);
152  }
153 
154  int HorzRepsum::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
155  casadi_int nnz = sparsity().nnz();
156  for (casadi_int i=0;i<n_;++i) {
157  std::transform(res[0], res[0]+nnz, arg[0]+i*nnz, arg[0]+i*nnz, &Orring);
158  }
159  std::fill(res[0], res[0]+nnz, 0);
160  return 0;
161  }
162 
163  void HorzRepsum::ad_forward(const std::vector<std::vector<MX> >& fseed,
164  std::vector<std::vector<MX> >& fsens) const {
165  for (casadi_int d=0; d<fsens.size(); ++d) {
166  fsens[d][0] = fseed[d][0]->get_repsum(1, n_);
167  }
168  }
169 
170  void HorzRepsum::ad_reverse(const std::vector<std::vector<MX> >& aseed,
171  std::vector<std::vector<MX> >& asens) const {
172  for (casadi_int d=0; d<asens.size(); ++d) {
173  asens[d][0] += aseed[d][0]->get_repmat(1, n_);
174  }
175  }
176 
178  const std::vector<casadi_int>& arg,
179  const std::vector<casadi_int>& res,
180  const std::vector<bool>& arg_is_ref,
181  std::vector<bool>& res_is_ref) const {
183  casadi_int nnz = sparsity().nnz();
184  g.local("i", "casadi_int");
185  g.local("j", "casadi_int");
186  g << g.clear(g.work(res[0], nnz, false), nnz) << "\n"
187  << " for (i=0;i<" << n_ << ";++i) {\n"
188  << " for (j=0;j<" << nnz << ";++j) {\n"
189  << " " << g.work(res[0], nnz, false)<< "[j] += "
190  << g.work(arg[0], dep(0).nnz(), arg_is_ref[0]) << "[j+i*" << nnz << "];\n"
191  << " }\n"
192  << " }\n";
193  }
194 
197  s.pack("HorzRepmat::n", n_);
198  }
199 
202  s.pack("HorzRepsum::n", n_);
203  }
204 
206  s.unpack("HorzRepmat::n", n_);
207  }
208 
210  s.unpack("HorzRepsum::n", n_);
211  }
212 
213 } // 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) non-zero elements.
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:94
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:80
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
Definition: repmat.cpp:195
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:87
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 override
Evaluate symbolically (MX)
Definition: repmat.cpp:60
casadi_int n_
Definition: repmat.hpp:116
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:66
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:70
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: repmat.cpp:140
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
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:177
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
Definition: repmat.cpp:148
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: repmat.cpp:136
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: repmat.cpp:119
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
Definition: repmat.cpp:200
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
Definition: repmat.cpp:144
int eval_gen(const T **arg, T **res, casadi_int *iw, T *w, R reduction) const
Evaluate the function (template)
Definition: repmat.cpp:126
HorzRepsum(const MX &x, casadi_int n)
Constructor.
Definition: repmat.cpp:107
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:170
casadi_int n_
Definition: repmat.hpp:206
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:163
Node class for MX objects.
Definition: mx_node.hpp:51
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
virtual void serialize_body(SerializingStream &s) const
Serialize an object without type information.
Definition: mx_node.cpp:523
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
Helper class for Serialization.
void pack(const Sparsity &e)
Serializes an object to the output stream.
General sparsity class.
Definition: sparsity.hpp:106
casadi_int nnz() const
Get the number of (structural) non-zeros.
Definition: sparsity.cpp:148
The casadi namespace.
Definition: archiver.cpp:28
static bvec_t Orring(bvec_t x, bvec_t y)
Definition: repmat.cpp:64
unsigned long long bvec_t
std::string str(const T &v)
String representation, any type.