25 #include "mx_function.hpp"
26 #include "casadi_misc.hpp"
27 #include "casadi_common.hpp"
28 #include "global_options.hpp"
29 #include "casadi_interrupt.hpp"
30 #include "io_instruction.hpp"
31 #include "serializing_stream.hpp"
37 #define CASADI_THROW_ERROR(FNAME, WHAT) \
38 throw CasadiException("Error in MXFunction::" FNAME " at " + CASADI_WHERE + ":\n"\
44 const std::vector<MX>& inputv,
45 const std::vector<MX>& outputv,
46 const std::vector<std::string>& name_in,
47 const std::vector<std::string>& name_out) :
59 "Default input values"}},
62 "Reuse variables in the work vector"}},
63 {
"print_instructions",
65 "Print each operation during evaluation. Influenced by print_canonical."}},
68 "Perform common subexpression elimination (complexity is N*log(N) in graph size)"}},
71 "Allow construction with free variables (Default: false)"}},
72 {
"allow_duplicate_io_names",
74 "Allow construction with duplicate io names (Default: false)"}}
104 return { io->
ind() };
118 bool cse_opt =
false;
119 bool allow_free =
false;
122 for (
auto&& op : opts) {
123 if (op.first==
"default_in") {
125 }
else if (op.first==
"live_variables") {
127 }
else if (op.first==
"print_instructions") {
129 }
else if (op.first==
"cse") {
131 }
else if (op.first==
"allow_free") {
132 allow_free = op.second;
141 "Option 'default_in' has incorrect length");
145 for (
const MX& e :
out_) {
146 casadi_assert(!e->has_output(),
147 "Function output contains MultiOutput nodes. "
148 "You must use get_output() to make a concrete instance.");
154 std::stack<MXNode*> s;
157 std::vector<MXNode*> nodes;
158 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
159 std::lock_guard<std::mutex> lock(MX::get_mutex_temp());
163 for (casadi_int ind=0; ind<
out_.size(); ++ind) {
165 std::vector<MX> prim =
out_[ind].primitives();
166 casadi_int nz_offset=0;
167 for (casadi_int p=0; p<prim.size(); ++p) {
169 s.push(prim[p].get());
172 nodes.push_back(
new Output(prim[p], ind, p, nz_offset));
174 nz_offset += prim[p].nnz();
179 for (casadi_int i=0; i<nodes.size(); ++i) {
184 std::vector<casadi_int> place_in_alg;
185 place_in_alg.reserve(nodes.size());
188 std::vector<std::pair<casadi_int, MXNode*> > symb_loc;
191 std::vector<casadi_int> refcount(nodes.size(), 0);
199 casadi_int op = n->op();
203 symb_loc.push_back(std::make_pair(
algorithm_.size(), n));
211 ae.
arg.resize(n->n_dep());
212 for (casadi_int i=0; i<n->n_dep(); ++i) {
213 ae.
arg[i] = n->dep(i)->temp;
215 ae.
res.resize(n->nout());
216 if (n->has_output()) {
217 std::fill(ae.
res.begin(), ae.
res.end(), -1);
218 }
else if (!ae.
res.empty()) {
223 for (casadi_int c=0; c<ae.
arg.size(); ++c) {
225 refcount[ae.
arg[c]]++;
235 casadi_int oind = n->which_output();
238 casadi_int pind = place_in_alg[n->dep(0)->temp];
241 casadi_int& otmp =
algorithm_[pind].res.at(oind);
249 place_in_alg.push_back(-1);
254 std::vector<casadi_int>& place = place_in_alg;
255 place.resize(nodes.size());
258 SPARSITY_MAP<casadi_int, std::stack<casadi_int> > unused_all;
261 casadi_int worksize = 0;
268 casadi_int first_to_free = 0;
269 casadi_int last_to_free = e.data->n_inplace();
270 for (casadi_int task=0; task<2; ++task) {
273 for (casadi_int c=last_to_free-1; c>=first_to_free; --c) {
278 casadi_int& ch_ind = e.arg[c];
283 casadi_int remaining = --refcount[ch_ind];
289 casadi_int nnz = nodes[ch_ind]->sparsity().nnz();
292 unused_all[nnz].push(place[ch_ind]);
296 ch_ind = place[ch_ind];
304 first_to_free = last_to_free;
305 last_to_free = e.arg.size();
308 for (casadi_int c=0; c<e.res.size(); ++c) {
314 casadi_int nnz = e.data->sparsity(c).nnz();
317 std::stack<casadi_int>& unused = unused_all[nnz];
320 if (!unused.empty()) {
321 e.res[c] = place[e.res[c]] = unused.top();
328 e.res[c] = place[e.res[c]] = worksize++;
336 casadi_message(
"Using live variables: work array is " +
str(worksize)
337 +
" instead of " +
str(nodes.size()));
339 casadi_message(
"Live variables disabled.");
346 size_t wind=0,
sz_w=0;
349 for (casadi_int c=0; c<e.res.size(); ++c) {
354 sz_w = std::max(
sz_w, e.data->sz_w());
357 wind += e.data->sparsity(c).nnz();
364 for (casadi_int i=0; i<
workloc_.size(); ++i) {
372 for (casadi_int i=0; i<nodes.size(); ++i) {
379 for (
auto it=symb_loc.begin(); it!=symb_loc.end(); ++it) {
380 it->second->temp = it->first+1;
384 for (casadi_int ind=0; ind<
in_.size(); ++ind) {
386 std::vector<MX> prim =
in_[ind].primitives();
387 casadi_int nz_offset=0;
388 for (casadi_int p=0; p<prim.size(); ++p) {
389 casadi_int i = prim[p].get_temp()-1;
395 algorithm_[i].data.own(
new Input(prim[p].sparsity(), ind, p, nz_offset));
398 nz_offset += prim[p]->nnz();
404 for (
auto it=symb_loc.begin(); it!=symb_loc.end(); ++it) {
405 casadi_int i = it->second->temp-1;
416 casadi_error(
name_ +
"::init: Initialization failed since variables [" +
417 join(
get_free(),
", ") +
"] are free. These symbols occur in the output expressions "
418 "but you forgot to declare these as inputs. "
419 "Set option 'allow_free' to allow free variables.");
424 if (a.data->has_refcount()) {
432 casadi_int* iw,
double* w,
void* mem)
const {
434 setup(mem, arg, res, iw, w);
436 const double** arg1 = arg+
n_in_;
437 double** res1 = res+
n_out_;
441 std::stringstream ss;
443 casadi_error(
"Cannot evaluate \"" + ss.str() +
"\" since variables "
456 double *w1 = w+
workloc_[e.res.front()];
457 casadi_int nnz=e.data.nnz();
458 casadi_int i=e.data->ind();
459 casadi_int nz_offset=e.data->offset();
460 if (arg[i]==
nullptr) {
461 std::fill(w1, w1+nnz, 0);
463 std::copy(arg[i]+nz_offset, arg[i]+nz_offset+nnz, w1);
467 double *w1 = w+
workloc_[e.arg.front()];
468 casadi_int nnz=e.data->dep().nnz();
469 casadi_int i=e.data->ind();
470 casadi_int nz_offset=e.data->offset();
471 if (res[i]) std::copy(w1, w1+nnz, res[i]+nz_offset);
474 for (casadi_int i=0; i<e.arg.size(); ++i)
475 arg1[i] = e.arg[i]>=0 ? w+
workloc_[e.arg[i]] :
nullptr;
476 for (casadi_int i=0; i<e.res.size(); ++i)
477 res1[i] = e.res[i]>=0 ? w+
workloc_[e.res[i]] :
nullptr;
481 if (e.data->eval(arg1, res1, iw, w))
return 1;
494 <<
" = @" << el.
arg.at(0);
496 if (el.
res.front()!=el.
arg.at(0)) {
497 s <<
"@" << el.
res.front() <<
" = @" << el.
arg.at(0) <<
"; ";
499 std::vector<std::string> arg(2);
500 arg[0] =
"@" +
str(el.
res.front());
501 arg[1] =
"@" +
str(el.
arg.at(1));
504 if (el.
res.size()==1) {
505 s <<
"@" << el.
res.front() <<
" = ";
508 for (casadi_int i=0; i<el.
res.size(); ++i) {
511 s <<
"@" << el.
res[i];
518 std::vector<std::string> arg;
520 arg.resize(el.
arg.size());
521 for (casadi_int i=0; i<el.
arg.size(); ++i) {
523 arg[i] =
"@" +
str(el.
arg[i]);
535 const double** arg)
const {
536 stream <<
name_ <<
":" << k <<
": " <<
print(el) <<
" inputs:" << std::endl;
537 for (
size_t i = 0; i < el.
arg.size(); ++i) {
551 const std::vector<casadi_int>& arg,
const std::vector<bool>& arg_is_ref)
const {
553 for (
size_t i = 0; i < el.
arg.size(); ++i) {
556 std::string a = g.
work(arg[i], el.
data->
dep(i).
nnz(), arg_is_ref[i]);
558 g << g.
printf(
"\\n") <<
"\n";
564 const std::vector<casadi_int>& res,
const std::vector<bool>& res_is_ref)
const {
566 for (
size_t i = 0; i < el.
res.size(); ++i) {
571 g << g.
printf(
"\\n") <<
"\n";
577 double** res)
const {
578 stream <<
name_ <<
":" << k <<
": " <<
print(el) <<
" outputs:" << std::endl;
579 for (
size_t i = 0; i < el.
res.size(); ++i) {
593 stream <<
"Algorithm:";
596 stream << std::endl <<
print(e);
613 casadi_int nnz=e.data.nnz();
614 casadi_int i=e.data->ind();
615 casadi_int nz_offset=e.data->offset();
616 const bvec_t* argi = arg[i];
619 std::copy(argi+nz_offset, argi+nz_offset+nnz, w1);
621 std::fill_n(w1, nnz, 0);
625 casadi_int nnz=e.data.dep().nnz();
626 casadi_int i=e.data->ind();
627 casadi_int nz_offset=e.data->offset();
630 if (resi!=
nullptr) std::copy(w1, w1+nnz, resi+nz_offset);
633 for (casadi_int i=0; i<e.arg.size(); ++i)
634 arg1[i] = e.arg[i]>=0 ? w+
workloc_[e.arg[i]] :
nullptr;
635 for (casadi_int i=0; i<e.res.size(); ++i)
636 res1[i] = e.res[i]>=0 ? w+
workloc_[e.res[i]] :
nullptr;
639 if (e.data->sp_forward(arg1, res1, iw, w))
return 1;
646 casadi_int* iw,
bvec_t* w,
void* mem)
const {
654 std::fill_n(w,
sz_w(), 0);
660 casadi_int nnz=it->data.nnz();
661 casadi_int i=it->data->ind();
662 casadi_int nz_offset=it->data->offset();
665 if (argi!=
nullptr)
for (casadi_int k=0; k<nnz; ++k) argi[nz_offset+k] |= w1[k];
666 std::fill_n(w1, nnz, 0);
669 casadi_int nnz=it->data.dep().nnz();
670 casadi_int i=it->data->ind();
671 casadi_int nz_offset=it->data->offset();
672 bvec_t* resi = res[i] ? res[i] + nz_offset :
nullptr;
675 for (casadi_int k=0; k<nnz; ++k) w1[k] |= resi[k];
676 std::fill_n(resi, nnz, 0);
681 for (casadi_int i=0; i<it->arg.size(); ++i)
682 arg1[i] = it->arg[i]>=0 ? w+
workloc_[it->arg[i]] :
nullptr;
683 for (casadi_int i=0; i<it->res.size(); ++i)
684 res1[i] = it->res[i]>=0 ? w+
workloc_[it->res[i]] :
nullptr;
687 if (it->data->sp_reverse(arg1, res1, iw, w))
return 1;
695 const casadi_int checking_depth = 2;
696 bool input_given =
true;
697 for (casadi_int i=0; i<arg.size() && input_given; ++i) {
712 bool always_inline,
bool never_inline)
const {
718 casadi_assert(arg.size()==
n_in_,
"Wrong number of input arguments");
719 res.resize(
out_.size());
722 if (!never_inline &&
isInput(arg)) {
723 std::copy(
out_.begin(),
out_.end(), res.begin());
733 std::vector<MX> swork(
workloc_.size()-1);
734 if (
verbose_) casadi_message(
"Allocated work vector");
737 std::vector<std::vector<MX> > arg_split(
in_.size());
738 for (casadi_int i=0; i<
in_.size(); ++i) arg_split[i] =
in_[i].split_primitives(arg[i]);
741 std::vector<std::vector<MX> > res_split(
out_.size());
742 for (casadi_int i=0; i<
out_.size(); ++i) res_split[i].resize(
out_[i].n_primitives());
744 std::vector<MX> arg1, res1;
747 casadi_int alg_counter = 0;
750 swork[it->res.front()] = project(arg_split.at(it->data->ind()).at(it->data->segment()),
751 it->data.sparsity(),
true);
754 res_split.at(it->data->ind()).at(it->data->segment()) = swork[it->arg.front()];
757 swork[it->res.front()] = it->data;
760 arg1.resize(it->arg.size());
761 for (casadi_int i=0; i<arg1.size(); ++i) {
762 casadi_int el = it->arg[i];
763 arg1[i] = el<0 ?
MX(it->data->dep(i).size()) : swork[el];
767 res1.resize(it->res.size());
768 it->data->eval_mx(arg1, res1);
771 for (casadi_int i=0; i<res1.size(); ++i) {
772 casadi_int el = it->res[i];
773 if (el>=0) swork[el] = res1[i];
779 for (casadi_int i=0; i<res.size(); ++i) res[i] =
out_[i].join_primitives(res_split[i]);
780 }
catch (std::exception& e) {
781 CASADI_THROW_ERROR(
"eval_mx", e.what());
786 std::vector<std::vector<MX> >& fsens)
const {
787 if (
verbose_) casadi_message(
name_ +
"::ad_forward(" +
str(fseed.size())+
")");
790 casadi_int nfwd = fseed.size();
792 for (casadi_int d=0; d<nfwd; ++d) {
801 for (
auto&& r : fseed) {
803 casadi_assert_dev(npar==1);
809 for (
auto&& r : fseed) {
812 std::vector<std::vector<MX> > fseed_purged, fsens_purged;
813 fseed_purged.reserve(nfwd);
814 std::vector<casadi_int> index_purged;
815 for (casadi_int d=0; d<nfwd; ++d) {
817 for (casadi_int i=0; i<fsens[d].size(); ++i) {
821 fseed_purged.push_back(fsens[d]);
822 index_purged.push_back(d);
830 for (casadi_int d=0; d<fseed_purged.size(); ++d) {
831 fsens[index_purged[d]] = fsens_purged[d];
845 std::vector<std::vector<MX> > dwork(
workloc_.size()-1);
846 fill(dwork.begin(), dwork.end(), std::vector<MX>(nfwd));
847 if (
verbose_) casadi_message(
"Allocated derivative work vector (forward mode)");
850 std::vector<std::vector<std::vector<MX>>> fseed_split(nfwd);
851 for (casadi_int d=0; d<nfwd; ++d) {
852 fseed_split[d].resize(fseed[d].size());
853 for (casadi_int i=0; i<fseed[d].size(); ++i) {
854 fseed_split[d][i] =
in_[i].split_primitives(fseed[d][i]);
859 std::vector<std::vector<std::vector<MX>>> fsens_split(nfwd);
860 for (casadi_int d=0; d<nfwd; ++d) {
861 fsens_split[d].resize(
out_.size());
862 for (casadi_int i=0; i<
out_.size(); ++i) {
863 fsens_split[d][i].resize(
out_[i].n_primitives());
868 std::vector<std::vector<MX> > oseed, osens;
871 std::vector<bool> skip(nfwd,
false);
877 for (casadi_int d=0; d<nfwd; ++d) {
878 dwork[e.res.front()][d] =
879 project(fseed_split[d].at(e.data->ind()).at(e.data->segment()),
880 e.data.sparsity(),
true);
884 for (casadi_int d=0; d<nfwd; ++d) {
885 fsens_split[d][e.data->ind()][e.data->segment()] = dwork[e.arg.front()][d];
889 for (casadi_int d=0; d<nfwd; ++d) {
890 dwork[e.res.front()][d] =
MX();
895 for (casadi_int d=0; d<nfwd; ++d) {
897 std::vector<MX> seed(e.arg.size());
899 for (casadi_int i=0; i<e.arg.size(); ++i) {
900 casadi_int el = e.arg[i];
901 if (el<0 || dwork[el][d].is_empty(
true)) {
902 seed[i] =
MX(e.data->dep(i).size());
904 seed[i] = dwork[el][d];
906 if (skip[d] && !seed[i].
is_zero()) skip[d] =
false;
908 if (!skip[d]) oseed.push_back(seed);
912 osens.resize(oseed.size());
913 if (!osens.empty()) {
914 fill(osens.begin(), osens.end(), std::vector<MX>(e.res.size()));
915 e.data.ad_forward(oseed, osens);
920 for (casadi_int d=0; d<nfwd; ++d) {
921 for (casadi_int i=0; i<e.res.size(); ++i) {
922 casadi_int el = e.res[i];
924 dwork[el][d] = skip[d] ?
MX(e.data->sparsity(i).size()) : osens[d1][i];
933 for (casadi_int d=0; d<nfwd; ++d) {
934 for (casadi_int i=0; i<
out_.size(); ++i) {
935 fsens[d][i] =
out_[i].join_primitives(fsens_split[d][i]);
938 }
catch (std::exception& e) {
939 CASADI_THROW_ERROR(
"ad_forward", e.what());
944 std::vector<std::vector<MX> >& asens)
const {
945 if (
verbose_) casadi_message(
name_ +
"::ad_reverse(" +
str(aseed.size())+
")");
949 casadi_int nadj = aseed.size();
951 for (casadi_int d=0; d<nadj; ++d) {
952 asens[d].resize(
n_in_);
960 for (
auto&& r : aseed) {
962 casadi_assert_dev(npar==1);
968 for (
auto&& r : aseed) {
972 std::vector<std::vector<MX> > aseed_purged, asens_purged;
973 aseed_purged.reserve(nadj);
974 std::vector<casadi_int> index_purged;
975 for (casadi_int d=0; d<nadj; ++d) {
977 for (casadi_int i=0; i<asens[d].size(); ++i) {
981 aseed_purged.push_back(asens[d]);
982 index_purged.push_back(d);
990 for (casadi_int d=0; d<aseed_purged.size(); ++d) {
991 asens[index_purged[d]] = asens_purged[d];
998 std::vector<std::vector<MX> > v;
1002 for (casadi_int i=0; i<v.size(); ++i) {
1003 for (casadi_int j=0; j<v[i].size(); ++j) {
1004 if (!v[i][j].is_empty()) {
1005 if (asens[i][j].is_empty()) {
1006 asens[i][j] = v[i][j];
1008 asens[i][j] += v[i][j];
1017 std::vector<std::vector<std::vector<MX>>> aseed_split(nadj);
1018 for (casadi_int d=0; d<nadj; ++d) {
1019 aseed_split[d].resize(
out_.size());
1020 for (casadi_int i=0; i<
out_.size(); ++i) {
1021 aseed_split[d][i] =
out_[i].split_primitives(aseed[d][i]);
1026 std::vector<std::vector<std::vector<MX>>> asens_split(nadj);
1027 for (casadi_int d=0; d<nadj; ++d) {
1028 asens_split[d].resize(
in_.size());
1029 for (casadi_int i=0; i<
in_.size(); ++i) {
1030 asens_split[d][i].resize(
in_[i].n_primitives());
1035 std::vector<std::vector<MX>> oseed, osens;
1036 oseed.reserve(nadj);
1037 osens.reserve(nadj);
1038 std::vector<bool> skip(nadj,
false);
1041 std::vector<std::vector<MX> > dwork(
workloc_.size()-1);
1042 fill(dwork.begin(), dwork.end(), std::vector<MX>(nadj));
1048 for (casadi_int d=0; d<nadj; ++d) {
1049 asens_split[d].at(it->data->ind()).at(it->data->segment()) = dwork[it->res.front()][d];
1050 dwork[it->res.front()][d] =
MX();
1054 for (casadi_int d=0; d<nadj; ++d) {
1055 MX a = project(aseed_split[d].at(it->data->ind()).at(it->data->segment()),
1056 it->data.dep().sparsity(),
true);
1057 if (dwork[it->arg.front()][d].is_empty(
true)) {
1058 dwork[it->arg.front()][d] = a;
1060 dwork[it->arg.front()][d] += a;
1065 for (casadi_int d=0; d<nadj; ++d) {
1066 dwork[it->res.front()][d] =
MX();
1071 for (casadi_int d=0; d<nadj; ++d) {
1076 std::vector<MX> seed(it->res.size());
1077 for (casadi_int i=0; i<it->res.size(); ++i) {
1079 casadi_int el = it->res[i];
1081 seed[i] = dwork[el][d];
1082 dwork[el][d] =
MX();
1088 if (seed[i].is_empty(
true)) seed[i] =
MX(it->data->sparsity(i).size());
1091 if (skip[d] && !seed[i].
is_zero()) skip[d] =
false;
1094 if (!skip[d]) oseed.push_back(seed);
1098 osens.resize(oseed.size());
1100 for (casadi_int d=0; d<nadj; ++d) {
1101 if (skip[d])
continue;
1102 osens[d1].resize(it->arg.size());
1103 for (casadi_int i=0; i<it->arg.size(); ++i) {
1105 casadi_int el = it->arg[i];
1107 osens[d1][i] = dwork[el][d];
1108 dwork[el][d] =
MX();
1110 osens[d1][i] =
MX();
1114 if (osens[d1][i].is_empty(
true)) osens[d1][i] =
MX(it->data->dep(i).size());
1120 if (!osens.empty()) {
1121 it->data.ad_reverse(oseed, osens);
1126 for (casadi_int d=0; d<nadj; ++d) {
1127 if (skip[d])
continue;
1128 for (casadi_int i=0; i<it->arg.size(); ++i) {
1129 casadi_int el = it->arg[i];
1131 if (dwork[el][d].is_empty(
true)) {
1132 dwork[el][d] = osens[d1][i];
1134 dwork[el][d] += osens[d1][i];
1144 for (casadi_int d=0; d<nadj; ++d) {
1145 for (casadi_int i=0; i<
in_.size(); ++i) {
1146 asens[d][i] =
in_[i].join_primitives(asens_split[d][i]);
1149 }
catch (std::exception& e) {
1150 CASADI_THROW_ERROR(
"ad_reverse", e.what());
1155 casadi_int* iw,
SXElem* w,
void* mem,
1156 bool always_inline,
bool never_inline)
const {
1166 std::vector<const SXElem*> argp(
sz_arg());
1167 std::vector<SXElem*> resp(
sz_res());
1175 casadi_int nnz=a.data.nnz();
1176 casadi_int i=a.data->ind();
1177 casadi_int nz_offset=a.data->offset();
1178 if (arg[i]==
nullptr) {
1179 std::fill(w1, w1+nnz, 0);
1181 std::copy(arg[i]+nz_offset, arg[i]+nz_offset+nnz, w1);
1186 casadi_int nnz=a.data.dep().nnz();
1187 casadi_int i=a.data->ind();
1188 casadi_int nz_offset=a.data->offset();
1189 if (res[i]) std::copy(w1, w1+nnz, res[i]+nz_offset);
1194 for (casadi_int i=0; i<a.arg.size(); ++i)
1195 argp[i] = a.arg[i]>=0 ? w+
workloc_[a.arg[i]] :
nullptr;
1196 for (casadi_int i=0; i<a.res.size(); ++i)
1197 resp[i] = a.res[i]>=0 ? w+
workloc_[a.res[i]] :
nullptr;
1200 if (a.data->eval_sx(
get_ptr(argp),
get_ptr(resp), iw, w))
return 1;
1210 casadi_error(
"Code generation of '" +
name_ +
"' is not possible since variables "
1216 a.data->add_dependency(g);
1221 std::set<void*> added;
1223 a.data->codegen_incref(g, added);
1228 std::set<void*> added;
1230 a.data->codegen_decref(g, added);
1245 std::vector<casadi_int> arg, res;
1248 std::vector<bool> work_is_ref(
workloc_.size()-1,
false);
1251 std::vector<bool> arg_is_ref, res_is_ref;
1254 std::vector<bool> needs_reference(
workloc_.size()-1,
false);
1255 std::vector<bool> needs_value(
workloc_.size()-1,
false);
1261 g <<
"/* #" << k <<
": " <<
print(e) <<
" */\n";
1265 arg.resize(e.arg.size());
1266 arg_is_ref.resize(e.arg.size());
1267 for (casadi_int i=0; i<e.arg.size(); ++i) {
1268 casadi_int j=e.arg.at(i);
1271 arg_is_ref.at(i) = work_is_ref.at(j);
1274 arg_is_ref.at(i) =
false;
1279 res.resize(e.res.size());
1280 for (casadi_int i=0; i<e.res.size(); ++i) {
1281 casadi_int j=e.res.at(i);
1289 res_is_ref.resize(e.res.size());
1291 std::fill(res_is_ref.begin(), res_is_ref.end(),
false);
1298 e.data->generate(g, arg, res, arg_is_ref, res_is_ref);
1300 for (casadi_int i=0; i<e.res.size(); ++i) {
1301 casadi_int j=e.res.at(i);
1303 work_is_ref.at(j) = res_is_ref.at(i);
1304 if (res_is_ref.at(i)) {
1305 needs_reference[j] =
true;
1307 needs_value[j] =
true;
1321 for (casadi_int i=0; i<
workloc_.size()-1; ++i) {
1334 if (needs_value[i]) {
1338 if (needs_reference[i]) {
1346 std::vector<MX> swork(
workloc_.size()-1);
1348 std::vector<MX> arg1, res1;
1351 std::vector<std::vector<MX> > in_split(
in_.size());
1352 for (casadi_int i=0; i<
in_.size(); ++i) in_split[i] =
in_[i].primitives();
1357 std::vector<std::vector<MX> > f_G(
out_.size());
1358 for (casadi_int i=0; i<
out_.size(); ++i) f_G[i].resize(
out_[i].n_primitives());
1361 std::vector<MX> x_init;
1364 std::stringstream ss;
1366 for (casadi_int algNo=0; algNo<2; ++algNo) {
1371 MX& arg = swork[e.arg.at(0)];
1372 MX& arg_init = swork[e.arg.at(1)];
1373 MX& res = swork[e.res.front()];
1376 ss.str(std::string());
1377 ss <<
"y" << y.
size();
1383 x_init.push_back(arg_init);
1390 swork[e.res.front()] = in_split.at(e.data->ind()).at(e.data->segment());
1393 swork[e.res.front()] = e.data;
1397 f_G.at(e.data->ind()).at(e.data->segment()) = swork[e.arg.front()];
1403 arg1.resize(e.arg.size());
1404 for (casadi_int i=0; i<arg1.size(); ++i) {
1405 casadi_int el = e.arg[i];
1406 arg1[i] = el<0 ?
MX(e.data->dep(i).size()) : swork[el];
1410 res1.resize(e.res.size());
1411 e.data->eval_mx(arg1, res1);
1414 for (casadi_int i=0; i<res1.size(); ++i) {
1415 casadi_int el = e.res[i];
1416 if (el>=0) swork[el] = res1[i];
1424 std::vector<MX> f_in =
in_;
1425 f_in.insert(f_in.end(), y.begin(), y.end());
1426 std::vector<MX> f_out;
1427 for (casadi_int i=0; i<
out_.size(); ++i) f_out.push_back(
out_[i].join_primitives(f_G[i]));
1428 f_out.insert(f_out.end(), g.begin(), g.end());
1429 vdef_fcn =
Function(
"lifting_variable_definition", f_in, f_out);
1434 vinit_fcn =
Function(
"lifting_variable_guess", f_in, f_out);
1446 return type==
"MXFunction"
1452 std::vector<MX> work(
workloc_.size()-1);
1453 std::vector<MX> oarg, ores;
1456 std::vector<std::vector<MX>> out_split(
out_.size());
1457 for (casadi_int i = 0; i < out_split.size(); ++i) out_split[i].resize(
out_[i].n_primitives());
1463 casadi_assert(it->data->segment()==0,
"Not implemented");
1464 work.at(it->res.front())
1465 =
out_.at(it->data->ind()).join_primitives(out_split.at(it->data->ind()));
1469 work.at(it->res.front()) = it->data;
1472 out_split.at(it->data->ind()).at(it->data->segment()) = work.at(it->arg.front());
1477 oarg.resize(it->arg.size());
1478 for (casadi_int i=0; i<oarg.size(); ++i) {
1479 casadi_int el = it->arg[i];
1480 oarg[i] = el<0 ?
MX(it->data->dep(i).size()) : work.at(el);
1484 ores.resize(it->res.size());
1485 it->data->eval_mx(oarg, ores);
1488 for (casadi_int i=0; i<ores.size(); ++i) {
1489 casadi_int el = it->res[i];
1490 if (el>=0) work.at(el) = ores[i];
1496 for (
size_t k = 0; k < out_split.size(); ++k) {
1497 MX a =
out_.at(k).join_primitives(out_split.at(k));
1498 if (k < vdef.size()) {
1501 ex.at(k - vdef.size()) = a;
1508 casadi_assert(!(always_inline && never_inline),
1510 casadi_assert(!(never_inline &&
has_free()),
1512 if (always_inline)
return true;
1513 if (never_inline)
return false;
1521 std::ostream &ss,
const Dict& options)
const {
1524 casadi_int indent_level = 0;
1527 for (
auto&& op : options) {
1528 if (op.first==
"indent_level") {
1529 indent_level = op.second;
1531 casadi_error(
"Unknown option '" + op.first +
"'.");
1537 for (casadi_int i=0;i<indent_level;++i) {
1541 Function f = shared_from_this<Function>();
1556 ss << indent <<
"w" << o[0] <<
" = varargin{" << i[0]+1 <<
"};" << std::endl;
1561 casadi_int segment =
info[
"segment"];
1563 {{
"name",
"sp_in"}, {
"indent_level", indent_level}, {
"as_matrix",
true}});
1564 ss << indent <<
"argout_" << o[0] <<
"{" << (1+segment) <<
"} = ";
1565 ss <<
"w" << i[0] <<
"(sp_in==1);" << std::endl;
1570 DM v =
static_cast<DM>(x);
1573 opts[
"indent_level"] = indent_level;
1575 ss << indent <<
"w" << o[0] <<
" = m;" << std::endl;
1579 ss << indent <<
"w" << o[0] <<
" = " <<
"w" << i[0] <<
".^2;" << std::endl;
1582 ss << indent <<
"w" << o[0] <<
" = ";
1583 ss <<
"w" << i[1] <<
"*w" << i[2] <<
"+w" << i[0] <<
";" << std::endl;
1588 ss << indent <<
"w" << o[0] <<
" = " <<
"w" << i[0] << prefix <<
"*w" << i[1] <<
";";
1593 ss << indent <<
"w" << o[0] <<
" = 2*w" << i[0] <<
";" << std::endl;
1596 ss << indent <<
"w" << o[0] <<
" = 1./w" << i[0] <<
";" << std::endl;
1599 ss << indent <<
"w" << o[0] <<
" = dot(w" << i[0] <<
",w" << i[1]<<
");" << std::endl;
1602 ss << indent <<
"w" << o[0] <<
" = w" << i[1] <<
".'*w" << i[0]<<
"*w" << i[2] <<
";";
1606 ss << indent <<
"w" << o[0] <<
" = w" << i[0] <<
"+";
1607 ss <<
"w" << i[1] <<
"*w" << i[2] <<
"*w" << i[3] <<
".';";
1611 ss << indent <<
"w" << o[0] <<
" = abs(w" << i[0] <<
");" << std::endl;
1614 ss << indent <<
"w" << o[0] <<
" = det(w" << i[0] <<
");" << std::endl;
1617 ss << indent <<
"w" << o[0] <<
" = inv(w" << i[0] <<
");";
1618 ss <<
"w" << o[0] <<
"(w" << o[0] <<
"==0) = 1e-200;" << std::endl;
1622 bool tr = x.
info()[
"tr"];
1624 ss << indent <<
"w" << o[0] <<
" = ((w" << i[1] <<
".')\\w" << i[0] <<
").';";
1627 ss << indent <<
"w" << o[0] <<
" = w" << i[1] <<
"\\w" << i[0] <<
";" << std::endl;
1629 ss <<
"w" << o[0] <<
"(w" << o[0] <<
"==0) = 1e-200;" << std::endl;
1635 ss << indent <<
"w" << o[0] <<
" = " <<
"w" << i[0] << prefix <<
"/w" << i[1] <<
";";
1641 ss << indent <<
"w" << o[0] <<
" = " <<
"w" << i[0] <<
".^w" << i[1] <<
";" << std::endl;
1644 ss << indent <<
"w" << o[0] <<
" = " <<
"w" << i[0] <<
".';" << std::endl;
1649 ss << indent <<
"w" << o[0] <<
" = [";
1650 for (casadi_int e : i) {
1651 ss <<
"w" << e << (op==
OP_HORZCAT ?
" " :
";");
1653 ss <<
"];" << std::endl;
1658 for (casadi_int k=0;k<i.size();++k) {
1660 {{
"name",
"sp_in" +
str(k)}, {
"indent_level", indent_level}, {
"as_matrix",
true}});
1662 ss << indent <<
"w" << o[0] <<
" = [";
1663 for (casadi_int k=0;k<i.size();++k) {
1664 ss <<
"w" << i[k] <<
"(sp_in" << k <<
"==1);";
1666 ss <<
"];" << std::endl;
1668 opts[
"name"] =
"sp";
1669 opts[
"indent_level"] = indent_level;
1670 opts[
"as_matrix"] =
false;
1672 ss << indent <<
"w" << o[0] <<
" = ";
1673 ss <<
"sparse(sp_i, sp_j, w" << o[0] <<
", sp_m, sp_n);" << std::endl;
1680 std::vector<casadi_int> offset =
info[
"offset"];
1682 std::vector<Sparsity> sp;
1683 for (casadi_int i=0;i<output.
n_out();i++)
1685 for (casadi_int k=0;k<o.size();++k) {
1686 if (o[k]==-1)
continue;
1688 {{
"name",
"sp_in"}, {
"indent_level", indent_level}, {
"as_matrix",
true}});
1689 ss << indent <<
"tmp = w" << i[0]<<
"(sp_in==1);" << std::endl;
1691 opts[
"name"] =
"sp";
1692 opts[
"indent_level"] = indent_level;
1693 opts[
"as_matrix"] =
false;
1694 sp[k].export_code(
"matlab", ss, opts);
1695 ss << indent <<
"w" << o[k] <<
" = sparse(sp_i, sp_j, ";
1696 ss <<
"tmp(" << offset[k]+1 <<
":" << offset[k+1] <<
"), sp_m, sp_n);" << std::endl;
1705 std::string nonzeros;
1707 nonzeros =
"1+" +
str(
info[
"nz"]);
1708 }
else if (
info.find(
"slice")!=
info.end()) {
1710 casadi_int start = s[
"start"];
1711 casadi_int step = s[
"step"];
1712 casadi_int stop = s[
"stop"];
1713 nonzeros =
str(start+1) +
":" +
str(step) +
":" +
str(stop);
1714 nonzeros =
"nonzeros(" + nonzeros +
")";
1718 casadi_int inner_start = inner[
"start"];
1719 casadi_int inner_step = inner[
"step"];
1720 casadi_int inner_stop = inner[
"stop"];
1721 casadi_int outer_start = outer[
"start"];
1722 casadi_int outer_step = outer[
"step"];
1723 casadi_int outer_stop = outer[
"stop"];
1724 std::string inner_slice =
"(" +
str(inner_start) +
":" +
1725 str(inner_step) +
":" +
str(inner_stop-1)+
")";
1726 std::string outer_slice =
"(" +
str(outer_start+1) +
":" +
1727 str(outer_step) +
":" +
str(outer_stop)+
")";
1728 casadi_int N =
range(outer_start, outer_stop, outer_step).size();
1729 casadi_int M =
range(inner_start, inner_stop, inner_step).size();
1730 nonzeros =
"repmat("+ inner_slice +
"', 1, " +
str(N) +
")+" +
1731 "repmat("+ outer_slice +
", " +
str(M) +
", 1)";
1732 nonzeros =
"nonzeros(" + nonzeros +
")";
1736 opts[
"name"] =
"sp";
1737 opts[
"indent_level"] = indent_level;
1738 opts[
"as_matrix"] =
false;
1743 {{
"name",
"sp_in"}, {
"indent_level", indent_level}, {
"as_matrix",
true}});
1746 ss << indent <<
"in_flat = w" << i[0] <<
"(sp_in==1);" << std::endl;
1749 ss << indent <<
"w" << o[0] <<
" = in_flat(" << nonzeros <<
");" << std::endl;
1752 {{
"name",
"sp_in0"}, {
"indent_level", indent_level}, {
"as_matrix",
true}});
1754 {{
"name",
"sp_in1"}, {
"indent_level", indent_level}, {
"as_matrix",
true}});
1755 ss << indent <<
"in_flat = w" << i[1] <<
"(sp_in1==1);" << std::endl;
1756 ss << indent <<
"w" << o[0] <<
" = w" << i[0] <<
"(sp_in0==1);" << std::endl;
1757 ss << indent <<
"w" << o[0] <<
"(" << nonzeros <<
") = ";
1758 if (
info[
"add"]) ss <<
"w" << o[0] <<
"(" << nonzeros <<
") + ";
1761 ss << indent <<
"w" << o[0] <<
" = ";
1762 ss <<
"sparse(sp_i, sp_j, w" << o[0] <<
", sp_m, sp_n);" << std::endl;
1768 opts[
"name"] =
"sp";
1769 opts[
"indent_level"] = indent_level;
1771 ss << indent <<
"w" << o[0] <<
" = ";
1772 ss <<
"sparse(sp_i, sp_j, w" << i[0] <<
"(sp==1), sp_m, sp_n);" << std::endl;
1776 ss << indent <<
"w" << o[0] <<
" = norm(w" << i[0] <<
", 1);" << std::endl;
1779 ss << indent <<
"w" << o[0] <<
" = norm(w" << i[0] <<
", 2);" << std::endl;
1782 ss << indent <<
"w" << o[0] <<
" = norm(w" << i[0] <<
", 'fro');" << std::endl;
1785 ss << indent <<
"w" << o[0] <<
" = norm(w" << i[0] <<
", inf);" << std::endl;
1788 ss << indent <<
"w" << o[0] <<
" = min(w" << i[0] <<
");" << std::endl;
1791 ss << indent <<
"w" << o[0] <<
" = max(w" << i[0] <<
");" << std::endl;
1794 ss << indent <<
"w" << o[0] <<
" = ~" <<
"w" << i[0] <<
";" << std::endl;
1797 ss << indent <<
"w" << o[0] <<
" = w" << i[0] <<
" | w" << i[1] <<
";" << std::endl;
1800 ss << indent <<
"w" << o[0] <<
" = w" << i[0] <<
" & w" << i[1] <<
";" << std::endl;
1803 ss << indent <<
"w" << o[0] <<
" = w" << i[0] <<
" ~= w" << i[1] <<
";" << std::endl;
1806 ss << indent <<
"w" << o[0] <<
" = ";
1807 ss <<
"if_else_zero_gen(w" << i[0] <<
", w" << i[1] <<
");" << std::endl;
1812 {{
"name",
"sp_in"}, {
"indent_level", indent_level}, {
"as_matrix",
true}});
1814 {{
"name",
"sp_out"}, {
"indent_level", indent_level}, {
"as_matrix",
false}});
1815 ss << indent <<
"w" << o[0] <<
" = sparse(sp_out_i, sp_out_j, ";
1816 ss <<
"w" << i[0] <<
"(sp_in==1), sp_out_m, sp_out_n);" << std::endl;
1822 "w"+std::to_string(i[0]),
"w"+std::to_string(i[1])) <<
";" << std::endl;
1825 "w"+std::to_string(i[0])) <<
";" << std::endl;
1827 ss <<
"unknown" + x.
class_name() << std::endl;
1839 Function d = e.data.which_function();
1840 if (d.
is_a(
"Conic",
true) || d.
is_a(
"Nlpsol")) {
1841 if (!dep.
is_null())
return stats;
1846 if (dep.
is_null())
return stats;
1847 return dep.
stats(1);
1858 s.
pack(
"MXFunction::alg::data", e.data);
1859 s.
pack(
"MXFunction::alg::arg", e.arg);
1860 s.
pack(
"MXFunction::alg::res", e.res);
1874 int version = s.
version(
"MXFunction", 1, 2);
1882 s.
unpack(
"MXFunction::alg::arg", e.
arg);
1883 s.
unpack(
"MXFunction::alg::res", e.
res);
1901 casadi_int max_depth)
const {
1909 if (option_name ==
"print_instructions") {
1918 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
1919 std::lock_guard<std::mutex> lock(MX::get_mutex_temp());
1922 std::stack<MXNode*> s;
1925 std::vector<MXNode*> nodes;
1928 for (casadi_int ind=0; ind<expr.size(); ++ind) {
1930 std::vector<MX> prim = expr[ind].primitives();
1931 for (casadi_int p=0; p<prim.size(); ++p) {
1933 s.push(prim[p].get());
1939 for (casadi_int i=0; i<nodes.size(); ++i) {
1943 std::vector<MX> ret(nodes.size());
1944 for (casadi_int i=0; i<nodes.size(); ++i) {
1945 ret[i].own(nodes[i]);
Helper class for C code generation.
bool codegen_scalars
Codegen scalar.
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
void reserve_work(casadi_int n)
Reserve a maximum size of work elements, used for padding of index.
std::string printf(const std::string &str, const std::vector< std::string > &arg=std::vector< std::string >())
Printf.
void local(const std::string &name, const std::string &type, const std::string &ref="")
Declare a local variable.
void init_local(const std::string &name, const std::string &def)
Specify the default value for a local variable.
std::string print_canonical(const Sparsity &sp, const std::string &arg)
Print canonical representaion of a matrix.
std::string format_padded(casadi_int i) const
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
bool has_refcount_
Reference counting in codegen?
void alloc_iw(size_t sz_iw, bool persistent=false)
Ensure required length of iw field.
Dict get_stats(void *mem) const override
Get all statistics.
virtual void call_forward(const std::vector< MX > &arg, const std::vector< MX > &res, const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens, bool always_inline, bool never_inline) const
Forward mode AD, virtual functions overloaded in derived classes.
std::vector< std::vector< M > > replace_fseed(const std::vector< std::vector< M >> &fseed, casadi_int npar) const
Replace 0-by-0 forward seeds.
void alloc_res(size_t sz_res, bool persistent=false)
Ensure required length of res field.
std::pair< casadi_int, casadi_int > size_in(casadi_int ind) const
Input/output dimensions.
std::string definition() const
Get function signature: name:(inputs)->(outputs)
virtual void call_reverse(const std::vector< MX > &arg, const std::vector< MX > &res, const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens, bool always_inline, bool never_inline) const
Reverse mode, virtual functions overloaded in derived classes.
void alloc_arg(size_t sz_arg, bool persistent=false)
Ensure required length of arg field.
static void print_canonical(std::ostream &stream, const Sparsity &sp, const double *nz)
Print canonical representation of a numeric matrix.
virtual double sp_weight() const
Weighting factor for chosing forward/reverse mode,.
size_t n_in_
Number of inputs and outputs.
size_t sz_res() const
Get required length of res field.
virtual void eval_mx(const MXVector &arg, MXVector &res, bool always_inline, bool never_inline) const
Evaluate with symbolic matrices.
virtual int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w, void *mem, bool always_inline, bool never_inline) const
Evaluate with symbolic scalars.
bool matching_arg(const std::vector< M > &arg, casadi_int &npar) const
Check if input arguments that needs to be replaced.
std::pair< casadi_int, casadi_int > size_out(casadi_int ind) const
Input/output dimensions.
virtual int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w, void *mem) const
Propagate sparsity forward.
static const Options options_
Options.
bool matching_res(const std::vector< M > &arg, casadi_int &npar) const
Check if output arguments that needs to be replaced.
void disp(std::ostream &stream, bool more) const override
Display object.
size_t sz_w() const
Get required length of w field.
virtual int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w, void *mem) const
Propagate sparsity backwards.
virtual Dict info() const
void alloc_w(size_t sz_w, bool persistent=false)
Ensure required length of w field.
size_t sz_arg() const
Get required length of arg field.
void setup(void *mem, const double **arg, double **res, casadi_int *iw, double *w) const
Set the (persistent and temporary) work vectors.
virtual std::vector< MX > symbolic_output(const std::vector< MX > &arg) const
Get a vector of symbolic variables corresponding to the outputs.
void change_option(const std::string &option_name, const GenericType &option_value) override
Change option after object creation for debugging.
static bool purgable(const std::vector< MatType > &seed)
Can a derivative direction be skipped.
Dict generate_options(const std::string &target) const override
Reconstruct options dict.
void add_embedded(std::map< FunctionInternal *, Function > &all_fun, const Function &dep, casadi_int max_depth) const
std::vector< std::vector< M > > replace_aseed(const std::vector< std::vector< M >> &aseed, casadi_int npar) const
Replace 0-by-0 reverse seeds.
casadi_int n_instructions() const
Number of instruction in the algorithm (SXFunction/MXFunction)
const Sparsity & sparsity_out(casadi_int ind) const
Get sparsity of a given output.
std::vector< casadi_int > instruction_input(casadi_int k) const
Locations in the work vector for the inputs of the instruction.
std::vector< casadi_int > instruction_output(casadi_int k) const
Location in the work vector for the output of the instruction.
MX instruction_MX(casadi_int k) const
Get the MX node corresponding to an instruction (MXFunction)
casadi_int n_out() const
Get the number of function outputs.
bool is_a(const std::string &type, bool recursive=true) const
Check if the function is of a particular type.
Dict stats(int mem=0) const
Get all statistics obtained at the end of the last evaluate call.
casadi_int instruction_id(casadi_int k) const
Identifier index of the instruction (SXFunction/MXFunction)
std::pair< casadi_int, casadi_int > size() const
Get the shape.
casadi_int nnz() const
Get the number of (structural) non-zero elements.
static MX sym(const std::string &name, casadi_int nrow=1, casadi_int ncol=1)
Create an nrow-by-ncol symbolic primitive.
bool is_scalar(bool scalar_and_dense=false) const
Check if the matrix expression is scalar.
bool is_null() const
Is a null pointer?
Generic data type, can hold different types such as bool, casadi_int, std::string etc.
An input or output instruction.
casadi_int ind() const override
static void check()
Raises an error if an interrupt was captured.
Internal node class for MXFunction.
void find(std::map< FunctionInternal *, Function > &all_fun, casadi_int max_depth) const override
void codegen_body(CodeGenerator &g) const override
Generate code for the body of the C function.
static const Options options_
Options.
std::vector< casadi_int > workloc_
Offsets for elements in the w_ vector.
bool live_variables_
Live variables?
MX instruction_MX(casadi_int k) const override
get MX expression associated with instruction
std::vector< casadi_int > instruction_output(casadi_int k) const override
Get the (integer) output argument of an atomic operation.
std::vector< double > default_in_
Default input values.
void change_option(const std::string &option_name, const GenericType &option_value) override
Change option after object creation for debugging.
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w, void *mem) const override
Propagate sparsity backwards.
const std::vector< MX > mx_in() const override
Get function input(s) and output(s)
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w, void *mem) const override
Propagate sparsity forward.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
void init(const Dict &opts) override
Initialize.
void print_arg(std::ostream &stream, casadi_int k, const AlgEl &el, const double **arg) const
void ad_reverse(const std::vector< std::vector< MX > > &adjSeed, std::vector< std::vector< MX > > &adjSens) const
Calculate reverse mode directional derivatives.
MXFunction(const std::string &name, const std::vector< MX > &input, const std::vector< MX > &output, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out)
Constructor.
void codegen_decref(CodeGenerator &g) const override
Codegen decref for dependencies.
std::vector< std::string > get_free() const override
Print free variables.
~MXFunction() override
Destructor.
std::string print(const AlgEl &el) const
bool has_free() const override
Does the function have free variables.
int eval(const double **arg, double **res, casadi_int *iw, double *w, void *mem) const override
Evaluate numerically, work vectors given.
void disp_more(std::ostream &stream) const override
Print description.
void codegen_declarations(CodeGenerator &g) const override
Generate code for the declarations of the C function.
bool print_instructions_
Print instructions during evaluation.
void substitute_inplace(std::vector< MX > &vdef, std::vector< MX > &ex) const
Substitute inplace, internal implementation.
std::vector< casadi_int > instruction_input(casadi_int k) const override
Get the (integer) input arguments of an atomic operation.
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w, void *mem, bool always_inline, bool never_inline) const override
Evaluate symbolically, SX type.
casadi_int n_instructions() const override
Get the number of atomic operations.
void codegen_incref(CodeGenerator &g) const override
Codegen incref for dependencies.
bool should_inline(bool with_sx, bool always_inline, bool never_inline) const override
std::vector< AlgEl > algorithm_
All the runtime elements in the order of evaluation.
void eval_mx(const MXVector &arg, MXVector &res, bool always_inline, bool never_inline) const override
Evaluate symbolically, MX type.
static std::vector< MX > order(const std::vector< MX > &expr)
bool is_a(const std::string &type, bool recursive) const override
Check if the function is of a particular type.
void print_res(std::ostream &stream, casadi_int k, const AlgEl &el, double **res) const
void ad_forward(const std::vector< std::vector< MX > > &fwdSeed, std::vector< std::vector< MX > > &fwdSens) const
Calculate forward mode directional derivatives.
std::vector< MX > free_vars_
Free variables.
Dict generate_options(const std::string &target="clone") const override
Reconstruct options dict.
void generate_lifted(Function &vdef_fcn, Function &vinit_fcn) const override
Extract the residual function G and the modified function Z out of an expression.
std::vector< MX > symbolic_output(const std::vector< MX > &arg) const override
Get a vector of symbolic variables corresponding to the outputs.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
Dict get_stats(void *mem) const override
Get all statistics.
void export_code_body(const std::string &lang, std::ostream &stream, const Dict &options) const override
Export function in a specific language.
Node class for MX objects.
virtual casadi_int ind() const
const Sparsity & sparsity() const
Get the sparsity.
const MX & dep(casadi_int ind=0) const
dependencies - functions that have to be evaluated before this one
virtual casadi_int segment() const
virtual std::string disp(const std::vector< std::string > &arg) const =0
Print expression.
static MX create(MXNode *node)
Create from node.
const Sparsity & sparsity() const
Get the sparsity pattern.
MX dep(casadi_int ch=0) const
Get the nth dependency as MX.
bool is_binary() const
Is binary operation.
bool is_unary() const
Is unary operation.
casadi_int op() const
Get operation type.
static void print_default(std::ostream &stream, const Sparsity &sp, const double *nonzeros, bool truncate=true)
Print default style.
void export_code(const std::string &lang, std::ostream &stream=casadi::uout(), const Dict &options=Dict()) const
Export matrix in specific language.
Base class for FunctionInternal and LinsolInternal.
bool verbose_
Verbose printout.
void clear_mem()
Clear all memory (called from destructor)
The basic scalar symbolic class of CasADi.
Helper class for Serialization.
void version(const std::string &name, int v)
void pack(const Sparsity &e)
Serializes an object to the output stream.
std::string class_name() const
Get class name.
casadi_int nnz() const
Get the number of (structural) non-zeros.
void export_code(const std::string &lang, std::ostream &stream=casadi::uout(), const Dict &options=Dict()) const
Export matrix in specific language.
Internal node class for the base class of SXFunction and MXFunction.
std::vector< MX > out_
Outputs of the function (needed for symbolic calculations)
void delayed_deserialize_members(DeserializingStream &s)
void init(const Dict &opts) override
Initialize.
virtual bool isInput(const std::vector< MX > &arg) const
Helper function: Check if a vector equals ex_in.
std::vector< MX > in_
Inputs of the function (needed for symbolic calculations)
void delayed_serialize_members(SerializingStream &s) const
Helper functions to avoid recursion limit.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
static void sort_depth_first(std::stack< MXNode * > &s, std::vector< MXNode * > &nodes)
Topological sorting of the nodes based on Depth-First Search (DFS)
bool is_equal(double x, double y, casadi_int depth=0)
std::vector< casadi_int > range(casadi_int start, casadi_int stop, casadi_int step, casadi_int len)
Range function.
std::string join(const std::vector< std::string > &l, const std::string &delim)
unsigned long long bvec_t
std::vector< MX > MXVector
std::string str(const T &v)
String representation, any type.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.
An element of the algorithm, namely an MX node.
MX data
Data associated with the operation.
std::vector< casadi_int > arg
Work vector indices of the arguments.
casadi_int op
Operator index.
std::vector< casadi_int > res
Work vector indices of the results.
Options metadata for a class.
static std::string print(unsigned char op, const std::string &x, const std::string &y)
Print.