27 #include "casadi_misc.hpp"
38 int Concat::eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const {
39 return eval_gen<double>(arg, res, iw, w);
43 return eval_gen<SXElem>(arg, res, iw, w);
49 for (casadi_int i=0; i<
n_dep(); ++i) {
50 casadi_int n =
dep(i).
nnz();
51 std::copy(arg[i], arg[i]+n, r);
59 for (casadi_int i=0; i<
n_dep(); ++i) {
60 casadi_int n_i =
dep(i).
nnz();
61 const bvec_t *arg_i_ptr = arg[i];
62 std::copy(arg_i_ptr, arg_i_ptr+n_i, res_ptr);
70 for (casadi_int i=0; i<
n_dep(); ++i) {
71 casadi_int n_i =
dep(i).
nnz();
72 bvec_t *arg_i_ptr = arg[i];
73 for (casadi_int k=0; k<n_i; ++k) {
74 *arg_i_ptr++ |= *res_ptr;
82 const std::vector<casadi_int>& arg,
83 const std::vector<casadi_int>& res,
84 const std::vector<bool>& arg_is_ref,
85 std::vector<bool>& res_is_ref)
const {
86 g.
local(
"rr",
"casadi_real",
"*");
87 g <<
"rr=" << g.
work(res[0],
nnz(),
false) <<
";\n";
88 for (casadi_int i=0; i<arg.size(); ++i) {
89 casadi_int nz =
dep(i).
nnz();
91 g <<
"*rr++ = " << g.
workel(arg[i]) <<
";\n";
93 g.
local(
"i",
"casadi_int");
94 g.
local(
"cs",
"const casadi_real",
"*");
95 g <<
"for (i=0, " <<
"cs=" << g.
work(arg[i], nz, arg_is_ref[i]) <<
"; "
96 <<
"i<" << nz <<
"; ++i) *rr++ = *cs++;\n";
103 casadi_int nz_test = -1;
104 for (
auto&& i : nz) {
115 casadi_int begin=0, end=0;
117 for (i=0; i<
n_dep(); ++i) {
120 if (nz_test < end)
break;
124 for (
auto&& j : nz) {
125 if (j>=0 && (j < begin || j >= end)) {
136 std::vector<casadi_int> nz_new(nz);
137 for (
auto&& j : nz_new)
if (j>=0) j -= begin;
144 casadi_assert_dev(x.size()>1);
145 std::vector<Sparsity> sp(x.size());
146 for (casadi_int i=0; i<x.size(); ++i) sp[i] = x[i].
sparsity();
151 std::stringstream ss;
152 ss <<
"diagcat(" << arg.at(0);
153 for (casadi_int i=1; i<
n_dep(); ++i) ss <<
", " << arg.at(i);
159 res[0] = diagcat(arg);
163 std::vector<std::vector<MX> >& fsens)
const {
164 casadi_int nfwd = fsens.size();
165 for (casadi_int d = 0; d<nfwd; ++d) fsens[d][0] = diagcat(fseed[d]);
168 std::pair<std::vector<casadi_int>, std::vector<casadi_int> >
Diagcat::off()
const {
169 std::vector<casadi_int> offset1(
n_dep()+1, 0);
170 std::vector<casadi_int> offset2(
n_dep()+1, 0);
171 for (casadi_int i=0; i<
n_dep(); ++i) {
174 offset2[i+1] = offset2[i] + ncol;
175 offset1[i+1] = offset1[i] + nrow;
177 return std::make_pair(offset1, offset2);
181 std::vector<std::vector<MX> >& asens)
const {
186 casadi_int nadj = aseed.size();
187 for (casadi_int d=0; d<nadj; ++d) {
188 std::vector<MX> s = diagsplit(aseed[d][0],
off.first,
off.second);
189 for (casadi_int i=0; i<
n_dep(); ++i) {
196 casadi_assert_dev(x.size()>1);
197 std::vector<Sparsity> sp(x.size());
198 for (casadi_int i=0; i<x.size(); ++i)
204 std::stringstream ss;
205 ss <<
"horzcat(" << arg.at(0);
206 for (casadi_int i=1; i<
n_dep(); ++i) ss <<
", " << arg.at(i);
212 res[0] = horzcat(arg);
216 std::vector<std::vector<MX> >& fsens)
const {
217 casadi_int nfwd = fsens.size();
218 for (casadi_int d = 0; d<nfwd; ++d) {
219 fsens[d][0] = horzcat(fseed[d]);
224 std::vector<casadi_int> col_offset(
n_dep()+1, 0);
225 for (casadi_int i=0; i<
n_dep(); ++i) {
227 col_offset[i+1] = col_offset[i] + ncol;
233 std::vector<std::vector<MX> >& asens)
const {
235 std::vector<casadi_int> col_offset =
off();
238 casadi_int nadj = aseed.size();
239 for (casadi_int d=0; d<nadj; ++d) {
240 std::vector<MX> s = horzsplit(aseed[d][0], col_offset);
241 for (casadi_int i=0; i<
n_dep(); ++i) {
248 casadi_assert_dev(x.size()>1);
249 std::vector<Sparsity> sp(x.size());
250 for (casadi_int i=0; i<x.size(); ++i) sp[i] = x[i].
sparsity();
255 std::stringstream ss;
256 ss <<
"vertcat(" << arg.at(0);
257 for (casadi_int i=1; i<
n_dep(); ++i) ss <<
", " << arg.at(i);
263 res[0] = vertcat(arg);
267 std::vector<std::vector<MX> >& fsens)
const {
268 casadi_int nfwd = fsens.size();
269 for (casadi_int d = 0; d<nfwd; ++d) {
270 fsens[d][0] = vertcat(fseed[d]);
275 std::vector<casadi_int> row_offset(
n_dep()+1, 0);
276 for (casadi_int i=0; i<
n_dep(); ++i) {
278 row_offset[i+1] = row_offset[i] + nrow;
284 std::vector<std::vector<MX> >& asens)
const {
286 std::vector<casadi_int> row_offset =
off();
289 casadi_int nadj = aseed.size();
290 for (casadi_int d=0; d<nadj; ++d) {
291 std::vector<MX> s = vertsplit(aseed[d][0], row_offset);
292 for (casadi_int i=0; i<
n_dep(); ++i) {
299 for (casadi_int i=0; i<
n_dep(); ++i) {
306 casadi_int nprim = 0;
307 for (casadi_int i=0; i<
n_dep(); ++i) {
315 std::vector<T> s = horzsplit(x,
off());
316 for (casadi_int i=0; i<s.size(); ++i) {
322 split_primitives_gen<MX>(x, it);
326 split_primitives_gen<SX>(x, it);
330 split_primitives_gen<DM>(x, it);
335 std::vector<T> s(
n_dep());
336 for (casadi_int i=0; i<s.size(); ++i) {
343 return join_primitives_gen<MX>(it);
347 return join_primitives_gen<SX>(it);
351 return join_primitives_gen<DM>(it);
356 std::vector<T> s = vertsplit(x,
off());
357 for (casadi_int i=0; i<s.size(); ++i) {
364 std::vector<T> s(
n_dep());
365 for (casadi_int i=0; i<s.size(); ++i) {
372 split_primitives_gen<MX>(x, it);
376 split_primitives_gen<SX>(x, it);
380 split_primitives_gen<DM>(x, it);
384 return join_primitives_gen<MX>(it);
388 return join_primitives_gen<SX>(it);
392 return join_primitives_gen<DM>(it);
397 std::pair<std::vector<casadi_int>, std::vector<casadi_int> >
off = this->
off();
398 std::vector<T> s = diagsplit(x,
off.first,
off.second);
399 for (casadi_int i=0; i<s.size(); ++i) {
406 std::vector<T> s(
n_dep());
407 for (casadi_int i=0; i<s.size(); ++i) {
414 split_primitives_gen<MX>(x, it);
418 split_primitives_gen<SX>(x, it);
422 split_primitives_gen<DM>(x, it);
426 return join_primitives_gen<MX>(it);
430 return join_primitives_gen<SX>(it);
434 return join_primitives_gen<DM>(it);
439 for (casadi_int i=0; i<
n_dep(); ++i) {
446 for (casadi_int i=0; i<
n_dep(); ++i) {
452 for (casadi_int i=0; i<
n_dep(); ++i) {
Helper class for C code generation.
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
void local(const std::string &name, const std::string &type, const std::string &ref="")
Declare a local variable.
std::string workel(casadi_int n) const
Concatenation: Join multiple expressions stacking the nonzeros.
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
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.
~Concat() override=0
Destructor.
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
bool has_duplicates() const override
Detect duplicate symbolic expressions.
MX get_nzref(const Sparsity &sp, const std::vector< casadi_int > &nz) const override
Get the nonzeros of matrix.
void reset_input() const override
Reset the marker for an input expression.
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
casadi_int n_primitives() const override
Get the number of symbolic primitives.
void primitives(std::vector< MX >::iterator &it) const override
Get symbolic primitives.
bool is_valid_input() const override
Check if valid function input.
int eval_gen(const T *const *arg, T *const *res, casadi_int *iw, T *w) const
Evaluate the function (template)
Concat(const std::vector< MX > &x)
Constructor.
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.
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
void split_primitives_gen(const T &x, typename std::vector< T >::iterator &it) const
Split up an expression along primitives (template)
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
MX join_primitives(std::vector< MX >::const_iterator &it) const override
Join an expression along symbolic primitives.
T join_primitives_gen(typename std::vector< T >::const_iterator &it) const
Join an expression along symbolic primitives (template)
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Diagcat(const std::vector< MX > &x)
Constructor.
std::pair< std::vector< casadi_int >, std::vector< casadi_int > > off() const
Get offsets for split.
void split_primitives(const MX &x, std::vector< MX >::iterator &it) const override
Split up an expression along symbolic primitives.
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.
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
std::vector< casadi_int > off() const
Get offsets for split.
MX join_primitives(std::vector< MX >::const_iterator &it) const override
Join an expression along symbolic primitives.
void split_primitives(const MX &x, std::vector< MX >::iterator &it) const override
Split up an expression along symbolic primitives.
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
T join_primitives_gen(typename std::vector< T >::const_iterator &it) const
Join an expression along symbolic primitives (template)
void split_primitives_gen(const T &x, typename std::vector< T >::iterator &it) const
Split up an expression along primitives (template)
Horzcat(const std::vector< MX > &x)
Constructor.
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
virtual void reset_input() const
Reset the marker for an input expression.
virtual casadi_int n_primitives() const
Get the number of symbolic primitives.
virtual bool has_duplicates() const
Detect duplicate symbolic expressions.
virtual bool is_valid_input() const
Check if valid function input.
virtual MX join_primitives(std::vector< MX >::const_iterator &it) const
Join an expression along symbolic primitives.
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
casadi_int n_dep() const
Number of dependencies.
virtual void primitives(std::vector< MX >::iterator &it) const
Get symbolic primitives.
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
virtual MX get_nzref(const Sparsity &sp, const std::vector< casadi_int > &nz) const
Get the nonzeros of matrix.
void set_dep(const MX &dep)
Set unary dependency.
virtual void split_primitives(const MX &x, std::vector< MX >::iterator &it) const
Split up an expression along symbolic primitives.
const Sparsity & sparsity() const
Get the sparsity pattern.
Sparse matrix class. SX and DM are specializations.
The basic scalar symbolic class of CasADi.
casadi_int size1() const
Get the number of rows.
casadi_int size2() const
Get the number of columns.
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res) const override
Evaluate symbolically (MX)
Vertcat(const std::vector< MX > &x)
Constructor.
std::vector< casadi_int > off() const
Get offsets for split.
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
void split_primitives_gen(const T &x, typename std::vector< T >::iterator &it) const
Split up an expression along primitives (template)
void split_primitives(const MX &x, std::vector< MX >::iterator &it) const override
Split up an expression along symbolic primitives.
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
T join_primitives_gen(typename std::vector< T >::const_iterator &it) const
Join an expression along symbolic primitives (template)
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
MX join_primitives(std::vector< MX >::const_iterator &it) const override
Join an expression along symbolic primitives.
unsigned long long bvec_t