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 const std::vector<bool>& unique)
const {
62 res[0] = arg[0]->get_repmat(1,
n_);
69 std::vector<casadi_int> nz_new(nz);
70 for (
auto&& j : nz_new)
if (j>=0) j %=
nnz;
77 return eval_gen<bvec_t>(arg, res, iw, w);
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);
86 std::fill(res[0], res[0]+NNZ, 0);
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_);
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_);
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 {
110 g.
local(
"i",
"casadi_int");
111 g <<
"for (i=0;i<" <<
n_ <<
";++i) {\n"
118 casadi_assert_dev(x.
size2() % n == 0);
119 std::vector<Sparsity> sp = horzsplit_n(x.
sparsity(), n);
121 for (casadi_int i=1;i<sp.size();++i) {
124 Sparsity goal = repmat(block, 1, n);
130 std::stringstream ss;
131 ss <<
"repsum(" << arg.at(0) <<
", " <<
n_ <<
")";
135 template<
typename T,
typename R>
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);
147 return eval_gen<double>(arg, res, iw, w, std::plus<double>());
151 return eval_gen<SXElem>(arg, res, iw, w, std::plus<SXElem>());
155 const std::vector<bool>& unique)
const {
156 res[0] = arg[0]->get_repsum(1,
n_);
161 std::fill(res[0], res[0]+
nnz, 0);
162 return eval_gen<bvec_t>(arg, res, iw, w, &
Orring);
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);
170 std::fill(res[0], res[0]+
nnz, 0);
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_);
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_);
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 {
195 g.
local(
"i",
"casadi_int");
196 g.
local(
"j",
"casadi_int");
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"
208 s.
pack(
"HorzRepmat::n",
n_);
213 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 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.
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.
MX get_nzref(const Sparsity &sp, const std::vector< casadi_int > &nz, bool unique=false) const override
Get the nonzeros of matrix.
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 std::vector< bool > &unique={}) 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 eval_mx(const std::vector< MX > &arg, std::vector< MX > &res, const std::vector< bool > &unique={}) const override
Evaluate symbolically (MX)
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.
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.
virtual MX get_nzref(const Sparsity &sp, const std::vector< casadi_int > &nz, bool unique=false) const
Get the nonzeros of matrix.
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
Number of structural nonzeros.
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.