27 #include "casadi_misc.hpp"
28 #include "serializing_stream.hpp"
39 ss <<
"repmat(" << arg.at(0) <<
", " <<
n_ <<
")";
46 for (casadi_int i=0; i<
n_; ++i) {
47 std::copy(arg[0], arg[0]+
nnz, res[0]+i*
nnz);
52 int HorzRepmat::eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const {
53 return eval_gen<double>(arg, res, iw, w);
57 return eval_gen<SXElem>(arg, res, iw, w);
61 res[0] = arg[0]->get_repmat(1,
n_);
67 return eval_gen<bvec_t>(arg, res, iw, w);
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);
76 std::fill(res[0], res[0]+NNZ, 0);
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_);
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_);
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 {
100 g.
local(
"i",
"casadi_int");
101 g <<
"for (i=0;i<" <<
n_ <<
";++i) {\n"
108 casadi_assert_dev(x.
size2() % n == 0);
109 std::vector<Sparsity> sp = horzsplit_n(x.
sparsity(), n);
111 for (casadi_int i=1;i<sp.size();++i) {
114 Sparsity goal = repmat(block, 1, n);
120 std::stringstream ss;
121 ss <<
"repsum(" << arg.at(0) <<
", " <<
n_ <<
")";
125 template<
typename T,
typename R>
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);
137 return eval_gen<double>(arg, res, iw, w, std::plus<double>());
141 return eval_gen<SXElem>(arg, res, iw, w, std::plus<SXElem>());
145 res[0] = arg[0]->get_repsum(1,
n_);
150 std::fill(res[0], res[0]+
nnz, 0);
151 return eval_gen<bvec_t>(arg, res, iw, w, &
Orring);
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);
159 std::fill(res[0], res[0]+
nnz, 0);
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_);
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_);
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 {
184 g.
local(
"i",
"casadi_int");
185 g.
local(
"j",
"casadi_int");
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"
197 s.
pack(
"HorzRepmat::n",
n_);
202 s.
pack(
"HorzRepsum::n",
n_);
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.
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
HorzRepmat(const MX &x, casadi_int n)
Constructor.
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
int eval_gen(const T **arg, T **res, casadi_int *iw, T *w) const
Evaluate the function (template)
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
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.
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
int eval_gen(const T **arg, T **res, casadi_int *iw, T *w, R reduction) const
Evaluate the function (template)
HorzRepsum(const MX &x, casadi_int n)
Constructor.
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Node class for MX objects.
const Sparsity & sparsity() const
Get the sparsity.
casadi_int nnz(casadi_int i=0) const
const MX & dep(casadi_int ind=0) const
dependencies - functions that have to be evaluated before this one
virtual void serialize_body(SerializingStream &s) const
Serialize an object without type information.
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
void set_dep(const MX &dep)
Set unary dependency.
const Sparsity & sparsity() const
Get the sparsity pattern.
The basic scalar symbolic class of CasADi.
Helper class for Serialization.
void pack(const Sparsity &e)
Serializes an object to the output stream.
casadi_int nnz() const
Get the number of (structural) non-zeros.
static bvec_t Orring(bvec_t x, bvec_t y)
unsigned long long bvec_t
std::string str(const T &v)
String representation, any type.