26 #include "getnonzeros_param.hpp"
27 #include "casadi_misc.hpp"
28 #include "serializing_stream.hpp"
39 casadi_assert(inner.
is_vector() && inner.
is_dense(),
"inner must be dense vector");
45 casadi_assert(outer.
is_vector() && outer.
is_dense(),
"outer must be dense vector");
51 casadi_assert(outer.
is_vector() && outer.
is_dense(),
"outer must be dense vector");
52 casadi_assert(inner.
is_vector() && inner.
is_dense(),
"inner must be dense vector");
63 const MX& nz,
const MX& nz_extra) {
69 eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const {
70 const double* idata = arg[0];
71 const double* nz = arg[1];
72 double* odata = res[0];
75 casadi_int max_ind =
dep(0).
nnz();
77 for (casadi_int i=0; i<
nnz; ++i) {
79 casadi_int index =
static_cast<casadi_int
>(*nz++);
81 *odata++ = index>=0 && index<max_ind ? idata[index] :
nan;
91 casadi_int* iw,
double* w)
const {
92 const double* idata = arg[0];
93 const double* nz = arg[1];
94 double* odata = res[0];
98 casadi_int max_ind =
dep(0).
nnz();
100 casadi_int* inner = iw; iw +=
nnz;
101 for (casadi_int i=0; i<
nnz; ++i) {
103 inner[i] =
static_cast<casadi_int
>(*nz++);
108 for (casadi_int* inner_it=inner; inner_it!=inner+
nnz; ++inner_it) {
109 casadi_int index = i+*inner_it;
111 *odata++ = index>=0 && index<max_ind ? idata[index] :
nan;
118 eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const {
119 const double* idata = arg[0];
120 const double* nz = arg[1];
121 double* odata = res[0];
124 casadi_int max_ind =
dep(0).
nnz();
126 for (casadi_int i=0; i<
nnz; ++i) {
128 casadi_int
ind =
static_cast<casadi_int
>(*nz++);
130 casadi_int index =
ind+j;
132 *odata++ = index>=0 && index<max_ind ? idata[index] :
nan;
143 casadi_int* iw,
double* w)
const {
144 const double* idata = arg[0];
145 const double* nz = arg[1];
146 const double* nz2 = arg[2];
147 double* odata = res[0];
152 casadi_int nnz2 =
dep(2).
nnz();
153 casadi_int max_ind =
dep(0).
nnz();
155 casadi_int* inner = iw; iw +=
nnz;
156 for (casadi_int i=0; i<
nnz; ++i) {
158 inner[i] =
static_cast<casadi_int
>(*nz++);
160 for (casadi_int i=0; i<nnz2; ++i) {
162 casadi_int
ind =
static_cast<casadi_int
>(*nz2++);
164 for (casadi_int* inner_it=inner; inner_it!=inner+
nnz; ++inner_it) {
165 casadi_int index =
ind+*inner_it;
167 *odata++ = index>=0 && index<max_ind ? idata[index] :
nan;
178 std::fill(r, r+
nnz(), a);
186 std::fill(res[0], res[0]+
nnz(),
bvec_t(0));
188 for (casadi_int i=0;i<
dep(0).
nnz();++i) {
195 std::stringstream ss;
196 ss << arg.at(0) <<
"[" << arg.at(1) <<
"]";
201 std::stringstream ss;
202 ss << arg.at(0) <<
"[(" << arg.at(1) <<
";" <<
outer_ <<
")]";
207 std::stringstream ss;
208 ss << arg.at(0) <<
"[(" <<
inner_ <<
";" << arg.at(1) <<
")]";
213 std::stringstream ss;
214 ss << arg.at(0) <<
"[(" << arg.at(1) <<
";" << arg.at(2) <<
")]";
219 res[0] = project(arg[0],
dep(0).
sparsity())->get_nz_ref(arg[1]);
231 res[0] = project(arg[0],
dep(0).
sparsity())->get_nz_ref(arg[1], arg[2]);
235 std::vector<std::vector<MX> >& fsens)
const {
236 const MX& nz =
dep(1);
238 for (casadi_int d=0; d<fsens.size(); ++d) {
246 std::vector<std::vector<MX> >& asens)
const {
247 const MX& nz =
dep(1);
249 for (casadi_int d=0; d<asens.size(); ++d) {
257 std::vector<std::vector<MX> >& fsens)
const {
258 const MX& inner =
dep(1);
260 for (casadi_int d=0; d<fsens.size(); ++d) {
268 std::vector<std::vector<MX> >& asens)
const {
269 const MX& inner =
dep(1);
271 for (casadi_int d=0; d<asens.size(); ++d) {
280 std::vector<std::vector<MX> >& fsens)
const {
281 const MX& outer =
dep(1);
283 for (casadi_int d=0; d<fsens.size(); ++d) {
291 std::vector<std::vector<MX> >& asens)
const {
292 const MX& outer =
dep(1);
294 for (casadi_int d=0; d<asens.size(); ++d) {
303 std::vector<std::vector<MX> >& fsens)
const {
304 const MX& inner =
dep(1);
305 const MX& outer =
dep(2);
307 for (casadi_int d=0; d<fsens.size(); ++d) {
315 std::vector<std::vector<MX> >& asens)
const {
316 const MX& inner =
dep(1);
317 const MX& outer =
dep(2);
319 for (casadi_int d=0; d<asens.size(); ++d) {
327 const std::vector<casadi_int>& arg,
328 const std::vector<casadi_int>& res,
329 const std::vector<bool>& arg_is_ref,
330 std::vector<bool>& res_is_ref)
const {
331 g.
local(
"i",
"casadi_int");
332 g.
local(
"rr",
"casadi_real",
"*");
333 g.
local(
"cr",
"const casadi_real",
"*");
334 g <<
"for (rr=" << g.
work(res[0],
nnz(),
false) <<
", cr="
335 << g.
work(arg[1],
dep(1).
nnz(), arg_is_ref[1])
336 <<
"; rr!=" << g.
work(res[0],
nnz(),
false) <<
"+" <<
nnz()
337 <<
"; ++rr) { i=(int) *cr++; "
338 <<
"*rr = i>=0 && i<" <<
dep(0).
nnz() <<
" ? "
343 const std::vector<casadi_int>& arg,
344 const std::vector<casadi_int>& res,
345 const std::vector<bool>& arg_is_ref,
346 std::vector<bool>& res_is_ref)
const {
347 g.
local(
"cii",
"const casadi_int",
"*");
348 g.
local(
"i",
"casadi_int");
349 g <<
"for (i=0;i<" <<
dep(1).
nnz() <<
";++i) iw[i] = (int) "
350 << g.
work(arg[1],
dep(1).
nnz(), arg_is_ref[1]) <<
"[i];\n";
352 g.
local(
"rr",
"casadi_real",
"*");
353 g.
local(
"k",
"casadi_int");
354 g <<
"for (rr=" << g.
work(res[0],
nnz(),
false) <<
", "
356 g <<
"for (cii=iw; cii!=iw" <<
"+" <<
dep(1).
nnz() <<
"; ++cii) { i=k+*cii; "
357 <<
"*rr++ = i>=0 && i<" <<
dep(0).
nnz() <<
" ? "
362 const std::vector<casadi_int>& arg,
363 const std::vector<casadi_int>& res,
364 const std::vector<bool>& arg_is_ref,
365 std::vector<bool>& res_is_ref)
const {
366 g.
local(
"i",
"casadi_int");
367 g.
local(
"j",
"casadi_int");
368 g.
local(
"rr",
"casadi_real",
"*");
369 g.
local(
"k",
"casadi_int");
370 g.
local(
"cr",
"const casadi_real",
"*");
371 g <<
"for (cr=" << g.
work(arg[1],
dep(1).
nnz(), arg_is_ref[1])
372 <<
", rr=" << g.
work(res[0],
nnz(),
false)
373 <<
"; cr!=" << g.
work(arg[1],
dep(1).
nnz(), arg_is_ref[1]) <<
"+" <<
dep(1).
nnz()
375 g <<
"for (j=(int) *cr, "
378 <<
"*rr++ = i>=0 && i<" <<
dep(0).
nnz() <<
" ? "
384 const std::vector<casadi_int>& arg,
385 const std::vector<casadi_int>& res,
386 const std::vector<bool>& arg_is_ref,
387 std::vector<bool>& res_is_ref)
const {
388 g.
local(
"cii",
"const casadi_int",
"*");
389 g.
local(
"i",
"casadi_int");
390 g <<
"for (i=0;i<" <<
dep(1).
nnz() <<
";++i) iw[i] = (int) "
391 << g.
work(arg[1],
dep(1).
nnz(), arg_is_ref[1]) <<
"[i];\n";
393 g.
local(
"j",
"casadi_int");
394 g.
local(
"cr",
"const casadi_real",
"*");
395 g.
local(
"rr",
"casadi_real",
"*");
396 g <<
"for (cr=" << g.
work(arg[2],
dep(2).
nnz(), arg_is_ref[2])
397 <<
", rr=" << g.
work(res[0],
nnz(),
false)
398 <<
"; cr!=" << g.
work(arg[2],
dep(2).
nnz(), arg_is_ref[2]) <<
"+" <<
dep(2).
nnz()
400 g <<
"for (j=(int) *cr, cii=iw; cii!=iw" <<
"+" <<
dep(1).
nnz()
403 <<
"*rr++ = i>=0 && i<" <<
dep(0).
nnz() <<
" ? "
413 s.
pack(
"GetNonzerosParam::type",
'a');
422 s.
pack(
"GetNonzerosParamSlice::outer",
outer_);
427 s.
pack(
"GetNonzerosParam::type",
'b');
436 s.
pack(
"GetNonzerosSliceParam::inner",
inner_);
441 s.
pack(
"GetNonzerosParam::type",
'c');
450 s.
pack(
"GetNonzerosParam::type",
'd');
458 s.
unpack(
"GetNonzerosParam::type", t);
464 default: casadi_assert_dev(
false);
Helper class for C code generation.
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
std::string constant(const std::vector< casadi_int > &v)
Represent an array constant; adding it when new.
void local(const std::string &name, const std::string &type, const std::string &ref="")
Declare a local variable.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
casadi_int numel() const
Get the number of elements.
bool is_dense() const
Check if the matrix expression is dense.
bool is_vector() const
Check if the matrix is a row or column vector.
casadi_int nnz() const
Get the number of (structural) non-zero elements.
static MX zeros(casadi_int nrow=1, casadi_int ncol=1)
Create a dense matrix or a matrix with specified sparsity with all entries zero.
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
GetNonzerosParamParam(const Sparsity &sp, const MX &x, const MX &inner, const MX &outer)
Constructor.
void serialize_type(SerializingStream &s) const override
Serialize type information.
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) 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 eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
size_t sz_iw() const override
Get required length of iw field.
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
void serialize_type(SerializingStream &s) const override
Serialize type information.
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
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.
GetNonzerosParamSlice(const Sparsity &sp, const MX &x, const MX &inner, const Slice &outer)
Constructor.
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
size_t sz_iw() const override
Get required length of iw field.
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.
void serialize_type(SerializingStream &s) const override
Serialize 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.
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
GetNonzerosParamVector(const MX &x, const MX &nz)
Constructor.
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Get nonzeros of a matrix, parametrically.
static MX create(const MX &x, const MX &nz)
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
static MXNode * deserialize(DeserializingStream &s)
Deserialize without type information.
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
GetNonzerosParam(const Sparsity &sp, const MX &y, const MX &nz)
Constructor.
void serialize_type(SerializingStream &s) const override
Serialize type information.
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
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(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
GetNonzerosSliceParam(const Sparsity &sp, const MX &x, const Slice &inner, const MX &outer)
Constructor.
Node class for MX objects.
virtual void serialize_type(SerializingStream &s) const
Serialize type information.
virtual casadi_int ind() const
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 MX get_nz_ref(const MX &nz) const
Get the nonzeros of matrix, parametrically.
virtual void serialize_body(SerializingStream &s) const
Serialize an object without type information.
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
virtual MX get_nzadd(const MX &y, const std::vector< casadi_int > &nz) const
Add the nonzeros of a matrix to another matrix.
void set_dep(const MX &dep)
Set unary dependency.
static MX create(MXNode *node)
Create from node.
const Sparsity & sparsity() const
Get the sparsity pattern.
Helper class for Serialization.
void pack(const Sparsity &e)
Serializes an object to the output stream.
Class representing a Slice.
size_t size() const
Get number of elements.
casadi_int stop
stop value: use std::numeric_limits<casadi_int>::max() to indicate unboundedness
casadi_int start
start value: negative values will get added to length
static Sparsity dense(casadi_int nrow, casadi_int ncol=1)
Create a dense rectangular sparsity pattern *.
unsigned long long bvec_t
const double nan
Not a number.
bvec_t bvec_or(const bvec_t *arg, casadi_int n)
Bit-wise or operation on bvec_t array.