26 #ifndef CASADI_FUNCTION_INTERNAL_HPP
27 #define CASADI_FUNCTION_INTERNAL_HPP
29 #include "function.hpp"
32 #include "code_generator.hpp"
33 #include "importer.hpp"
34 #include "options.hpp"
35 #include "shared_object.hpp"
37 #ifdef CASADI_WITH_THREAD
38 #ifdef CASADI_WITH_THREAD_MINGW
39 #include <mingw.mutex.h>
46 #define INPUTSCHEME(name)
49 #define OUTPUTSCHEME(name)
55 std::vector<std::pair<std::string, T>>
zip(
const std::vector<std::string>&
id,
56 const std::vector<T>& mat) {
57 casadi_assert_dev(
id.size()==mat.size());
58 std::vector<std::pair<std::string, T>> r(
id.size());
59 for (casadi_uint i=0; i<r.size(); ++i) r[i] = std::make_pair(
id[i], mat[i]);
68 std::map<std::string, FStats>
fstats;
75 bool added = fstats.insert(std::make_pair(s,
FStats())).second;
76 casadi_assert(added,
"Duplicate stat: '" + s +
"'");
111 void construct(
const Dict& opts);
122 virtual Dict generate_options(
const std::string& target)
const;
127 void print_options(std::ostream &stream)
const;
132 void print_option(
const std::string &name, std::ostream &stream)
const;
137 bool has_option(
const std::string &option_name)
const;
142 virtual void change_option(
const std::string& option_name,
const GenericType& option_value);
152 virtual void init(
const Dict& opts);
160 virtual void finalize();
163 int checkout()
const;
166 void release(
int mem)
const;
169 void* memory(
int ind)
const;
172 bool has_memory(
int ind)
const;
189 virtual int init_mem(
void* mem)
const;
197 virtual Dict get_stats(
void* mem)
const;
207 void print(
const char* fmt, ...)
const;
212 void sprint(
char* buf,
size_t buf_sz,
const char* fmt, ...)
const;
217 void format_time(
char* buffer,
double time)
const;
222 void print_time(
const std::map<std::string, FStats>& fstats)
const;
269 #ifdef CASADI_WITH_THREAD
271 mutable std::mutex mtx_;
276 mutable std::vector<void*> mem_;
279 mutable std::stack<int> unused_;
315 Dict generate_options(
const std::string& target)
const override;
320 void change_option(
const std::string& option_name,
const GenericType& option_value)
override;
325 void reset_dump_count();
330 void init(
const Dict& opts)
override;
335 void finalize()
override;
348 Dict get_stats(
void* mem)
const override;
353 Function self()
const {
return shared_from_this<Function>();}
356 virtual Function factory(
const std::string& name,
357 const std::vector<std::string>& s_in,
358 const std::vector<std::string>& s_out,
360 const Dict& opts)
const;
363 virtual std::vector<std::string> get_function()
const;
366 virtual const Function& get_function(
const std::string &name)
const;
369 virtual bool has_function(
const std::string& fname)
const {
return false;}
372 void add_embedded(std::map<
FunctionInternal*, std::pair<Function, size_t> >& all_fun,
373 const Function& dep, casadi_int max_depth)
const;
377 casadi_int max_depth)
const;
387 virtual std::vector<bool> which_depends(
const std::string& s_in,
388 const std::vector<std::string>& s_out,
389 casadi_int order,
bool tr=
false)
const;
398 const std::vector<std::pair<std::string, casadi_int> >& tasks)
const;
412 int eval_gen(
const double** arg,
double** res, casadi_int* iw,
double* w,
void* mem,
413 bool always_inline,
bool never_inline)
const;
414 virtual int eval(
const double** arg,
double** res, casadi_int* iw,
double* w,
void* mem)
const;
421 casadi_int* iw,
SXElem* w,
void* mem,
bool always_inline,
bool never_inline)
const;
427 bool always_inline,
bool never_inline)
const;
433 virtual std::vector<DM> eval_dm(
const std::vector<DM>& arg)
const;
442 bool always_inline,
bool never_inline)
const {
443 return eval_sx(arg, res, iw, w, mem, always_inline, never_inline);
446 bool always_inline,
bool never_inline)
const {
447 return sp_forward(arg, res, iw, w, mem);
456 bool always_inline,
bool never_inline)
const;
460 casadi_int npar,
bool always_inline,
bool never_inline)
const;
467 void call(
const std::vector<M>& arg, std::vector<M>& res,
468 bool always_inline,
bool never_inline)
const;
476 static bool check_mat(
const Sparsity& arg,
const Sparsity& inp, casadi_int& npar);
489 void check_arg(
const std::vector<M>& arg, casadi_int& npar)
const;
502 void check_res(
const std::vector<M>& res, casadi_int& npar)
const;
513 template<
typename M>
bool
514 matching_arg(
const std::vector<M>& arg, casadi_int& npar)
const;
524 template<
typename M>
bool
525 matching_res(
const std::vector<M>& arg, casadi_int& npar)
const;
530 template<
typename M> std::vector<M>
531 replace_arg(
const std::vector<M>& arg, casadi_int npar)
const;
536 template<
typename M> std::vector<M>
537 project_arg(
const std::vector<M>& arg, casadi_int npar)
const;
542 template<
typename M> std::vector<M>
543 project_res(
const std::vector<M>& arg, casadi_int npar)
const;
548 template<
typename M> std::vector<M>
549 replace_res(
const std::vector<M>& res, casadi_int npar)
const;
554 template<
typename M> std::vector<std::vector<M>>
555 replace_fseed(
const std::vector<std::vector<M>>& fseed, casadi_int npar)
const;
560 template<
typename M> std::vector<std::vector<M>>
561 replace_aseed(
const std::vector<std::vector<M>>& aseed, casadi_int npar)
const;
568 std::map<std::string, M> convert_arg(
const std::vector<M>& arg)
const;
570 std::vector<M> convert_arg(
const std::map<std::string, M>& arg)
const;
572 std::map<std::string, M> convert_res(
const std::vector<M>& res)
const;
574 std::vector<M> convert_res(
const std::map<std::string, M>& res)
const;
581 std::vector<double> nz_in(
const std::vector<DM>& arg)
const;
582 std::vector<double> nz_out(
const std::vector<DM>& res)
const;
583 std::vector<DM> nz_in(
const std::vector<double>& arg)
const;
584 std::vector<DM> nz_out(
const std::vector<double>& res)
const;
591 virtual void call_forward(
const std::vector<MX>& arg,
const std::vector<MX>& res,
592 const std::vector<std::vector<MX> >& fseed,
593 std::vector<std::vector<MX> >& fsens,
594 bool always_inline,
bool never_inline)
const;
595 virtual void call_forward(
const std::vector<SX>& arg,
const std::vector<SX>& res,
596 const std::vector<std::vector<SX> >& fseed,
597 std::vector<std::vector<SX> >& fsens,
598 bool always_inline,
bool never_inline)
const;
605 virtual void call_reverse(
const std::vector<MX>& arg,
const std::vector<MX>& res,
606 const std::vector<std::vector<MX> >& aseed,
607 std::vector<std::vector<MX> >& asens,
608 bool always_inline,
bool never_inline)
const;
609 virtual void call_reverse(
const std::vector<SX>& arg,
const std::vector<SX>& res,
610 const std::vector<std::vector<SX> >& aseed,
611 std::vector<std::vector<SX> >& asens,
612 bool always_inline,
bool never_inline)
const;
618 std::vector<MX> mapsum_mx(
const std::vector<MX > &arg,
const std::string& parallelization);
631 virtual Function get_jacobian(
const std::string& name,
632 const std::vector<std::string>& inames,
633 const std::vector<std::string>& onames,
634 const Dict& opts)
const;
642 Sparsity& jac_sparsity(casadi_int oind, casadi_int iind,
bool compact,
bool symmetric)
const;
644 virtual Sparsity get_jac_sparsity(casadi_int oind, casadi_int iind,
bool symmetric)
const;
648 static std::string
forward_name(
const std::string& fcn, casadi_int nfwd) {
649 return "fwd" +
str(nfwd) +
"_" + fcn;
653 std::string diff_prefix(
const std::string& prefix)
const;
663 Function forward(casadi_int nfwd)
const;
665 virtual Function get_forward(casadi_int nfwd,
const std::string& name,
666 const std::vector<std::string>& inames,
667 const std::vector<std::string>& onames,
668 const Dict& opts)
const;
672 static std::string
reverse_name(
const std::string& fcn, casadi_int nadj) {
673 return "adj" +
str(nadj) +
"_" + fcn;
686 virtual Function get_reverse(casadi_int nadj,
const std::string& name,
687 const std::vector<std::string>& inames,
688 const std::vector<std::string>& onames,
689 const Dict& opts)
const;
695 template<
typename MatType>
696 static MatType ensure_stacked(
const MatType& v,
const Sparsity& sp, casadi_int n);
701 virtual Function slice(
const std::string& name,
const std::vector<casadi_int>& order_in,
702 const std::vector<casadi_int>& order_out,
const Dict& opts)
const;
707 virtual const Function& oracle()
const;
712 bool has_derivative()
const;
717 virtual double ad_weight()
const;
724 virtual double sp_weight()
const;
730 virtual const SX sx_in(casadi_int ind)
const;
731 virtual const SX sx_out(casadi_int ind)
const;
732 virtual const std::vector<SX> sx_in()
const;
733 virtual const std::vector<SX> sx_out()
const;
734 virtual const MX mx_in(casadi_int ind)
const;
735 virtual const MX mx_out(casadi_int ind)
const;
736 virtual const std::vector<MX> mx_in()
const;
737 virtual const std::vector<MX> mx_out()
const;
738 const DM dm_in(casadi_int ind)
const;
739 const DM dm_out(casadi_int ind)
const;
740 const std::vector<DM> dm_in()
const;
741 const std::vector<DM> dm_out()
const;
745 virtual std::vector<MX> free_mx()
const;
748 virtual std::vector<SX> free_sx()
const;
758 virtual void generate_lifted(
Function& vdef_fcn,
Function& vinit_fcn)
const;
763 virtual casadi_int n_instructions()
const;
768 virtual casadi_int instruction_id(casadi_int k)
const;
773 virtual std::vector<casadi_int> instruction_input(casadi_int k)
const;
778 virtual double instruction_constant(casadi_int k)
const;
783 virtual std::vector<casadi_int> instruction_output(casadi_int k)
const;
788 virtual casadi_int n_nodes()
const;
793 virtual MX instruction_MX(casadi_int k)
const;
798 virtual SX instructions_sx()
const;
804 Function wrap(
const std::string& name)
const;
812 Function wrap_as_needed(
const std::string& name,
const Dict& opts)
const;
824 bool incache(
const std::string& fname,
Function& f,
const std::string& suffix=
"")
const;
829 void tocache(
const Function& f,
const std::string& suffix=
"")
const;
835 void tocache_if_missing(
Function& f,
const std::string& suffix=
"")
const;
840 void codegen(
CodeGenerator& g,
const std::string& fname)
const;
855 virtual std::string codegen_name(
const CodeGenerator& g,
bool ns=
true)
const;
860 std::string codegen_mem(
CodeGenerator& g,
const std::string& index=
"mem")
const;
900 std::string signature(
const std::string& fname)
const;
905 std::string signature_unrolled(
const std::string& fname)
const;
937 virtual std::string generate_dependencies(
const std::string& fname,
const Dict& opts)
const;
952 static std::string get_jit_directory(
const Dict& jit_options);
957 virtual void export_code(
const std::string& lang,
958 std::ostream &stream,
const Dict& options)
const;
973 void disp(std::ostream& stream,
bool more)
const override;
983 std::string definition()
const;
988 void print_dimensions(std::ostream &stream)
const;
993 virtual std::vector<std::string> get_free()
const;
998 void get_partition(casadi_int iind, casadi_int oind,
Sparsity& D1,
Sparsity& D2,
999 bool compact,
bool symmetric,
1000 bool allow_forward,
bool allow_reverse)
const;
1006 casadi_int nnz_in()
const;
1007 casadi_int
nnz_in(casadi_int ind)
const {
return sparsity_in(ind).nnz(); }
1008 casadi_int nnz_out()
const;
1009 casadi_int
nnz_out(casadi_int ind)
const {
return sparsity_out(ind).nnz(); }
1016 casadi_int numel_in()
const;
1017 casadi_int
numel_in(casadi_int ind)
const {
return sparsity_in(ind).numel(); }
1018 casadi_int
numel_out(casadi_int ind)
const {
return sparsity_out(ind).numel(); }
1019 casadi_int numel_out()
const;
1026 casadi_int
size1_in(casadi_int ind)
const {
return sparsity_in(ind).size1(); }
1027 casadi_int
size2_in(casadi_int ind)
const {
return sparsity_in(ind).size2(); }
1028 casadi_int
size1_out(casadi_int ind)
const {
return sparsity_out(ind).size1(); }
1029 casadi_int
size2_out(casadi_int ind)
const {
return sparsity_out(ind).size2(); }
1030 std::pair<casadi_int, casadi_int>
size_in(casadi_int ind)
const {
1031 return sparsity_in(ind).size();
1033 std::pair<casadi_int, casadi_int>
size_out(casadi_int ind)
const {
1034 return sparsity_out(ind).size();
1050 bool all_scalar()
const;
1053 virtual bool jac_is_symm(casadi_int oind, casadi_int iind)
const;
1056 Sparsity to_compact(casadi_int oind, casadi_int iind,
const Sparsity& sp)
const;
1059 Sparsity from_compact(casadi_int oind, casadi_int iind,
const Sparsity& sp)
const;
1063 Sparsity get_jac_sparsity_gen(casadi_int oind, casadi_int iind)
const;
1066 Sparsity get_jac_sparsity_hierarchical(casadi_int oind, casadi_int iind)
const;
1071 Sparsity get_jac_sparsity_hierarchical_symm(casadi_int oind, casadi_int iind)
const;
1074 virtual std::vector<MX> symbolic_output(
const std::vector<MX>& arg)
const;
1080 virtual size_t get_n_in();
1081 virtual size_t get_n_out();
1089 virtual std::string get_name_in(casadi_int i);
1090 virtual std::string get_name_out(casadi_int i);
1115 return std::vector<double>(nnz_in(ind), 1.);
1119 return std::vector<double>(nnz_out(ind), 1.);
1139 virtual Sparsity get_sparsity_in(casadi_int i);
1144 virtual Sparsity get_sparsity_out(casadi_int i);
1160 for (casadi_int i=0; i<name_in_.size(); ++i) {
1161 if (name_in_[i]==name)
return i;
1163 casadi_error(
"FunctionInternal::index_in: could not find entry \""
1164 + name +
"\". Available names are: " +
str(name_in_) +
".");
1172 for (casadi_int i=0; i<name_out_.size(); ++i) {
1173 if (name_out_[i]==name)
return i;
1175 casadi_error(
"FunctionInternal::index_out: could not find entry \""
1176 + name +
"\". Available names are: " +
str(name_out_) +
".");
1183 virtual int sp_forward(
const bvec_t** arg,
bvec_t** res,
1184 casadi_int* iw,
bvec_t* w,
void* mem)
const;
1193 virtual int eval_activity(
const bvec_t** arg,
bvec_t** res,
1194 casadi_int* iw,
bvec_t* w,
void* mem)
const;
1199 virtual int sp_forward_block(
const bvec_t** arg,
bvec_t** res,
1200 casadi_int* iw,
bvec_t* w,
void* mem, casadi_int oind, casadi_int iind)
const;
1205 virtual int sp_reverse(
bvec_t** arg,
bvec_t** res, casadi_int* iw,
bvec_t* w,
void* mem)
const;
1210 void sz_work(
size_t& sz_arg,
size_t& sz_res,
size_t& sz_iw,
size_t& sz_w)
const;
1215 size_t sz_arg()
const {
return sz_arg_per_ + sz_arg_tmp_;}
1220 size_t sz_res()
const {
return sz_res_per_ + sz_res_tmp_;}
1225 size_t sz_iw()
const {
return sz_iw_per_ + sz_iw_tmp_;}
1230 size_t sz_w()
const {
return sz_w_per_ + sz_w_tmp_;}
1236 virtual size_t codegen_sz_arg(
const CodeGenerator& g)
const;
1237 virtual size_t codegen_sz_res(
const CodeGenerator& g)
const;
1245 void alloc_arg(
size_t sz_arg,
bool persistent=
false);
1250 void alloc_res(
size_t sz_res,
bool persistent=
false);
1255 void alloc_iw(
size_t sz_iw,
bool persistent=
false);
1260 void alloc_w(
size_t sz_w,
bool persistent=
false);
1265 void alloc(
const Function& f,
bool persistent=
false,
int num_threads=1);
1270 virtual void set_work(
void* mem,
const double**& arg,
double**& res,
1271 casadi_int*& iw,
double*& w)
const {}
1276 virtual void set_temp(
void* mem,
const double** arg,
double** res,
1277 casadi_int* iw,
double* w)
const {}
1282 void setup(
void* mem,
const double** arg,
double** res, casadi_int* iw,
double* w)
const;
1288 virtual bool fwdViaJac(casadi_int nfwd)
const;
1289 virtual bool adjViaJac(casadi_int nadj)
const;
1293 virtual Dict info()
const;
1298 Function map(casadi_int n,
const std::string& parallelization)
const;
1303 void generate_in(
const std::string& fname,
const double** arg)
const;
1304 void generate_out(
const std::string& fname,
double** res)
const;
1391 mutable std::vector<Sparsity> jac_sparsity_[2];
1393 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
1395 mutable std::mutex jac_sparsity_mtx_;
1413 bool enable_forward_, enable_reverse_, enable_jacobian_,
enable_fd_;
1414 bool enable_forward_op_, enable_reverse_op_, enable_jacobian_op_,
enable_fd_op_;
1463 #ifdef CASADI_WITH_THREAD
1464 mutable std::atomic<casadi_int> dump_count_;
1472 virtual bool is_a(
const std::string& type,
bool recursive)
const;
1477 virtual void merge(
const std::vector<MX>& arg,
1478 std::vector<MX>& subs_from, std::vector<MX>& subs_to)
const;
1483 template<
typename MatType>
1484 static bool purgable(
const std::vector<MatType>& seed);
1489 template<
typename MatType>
1490 std::vector<std::vector<MatType> >
1491 fwd_seed(casadi_int nfwd)
const;
1496 template<
typename MatType>
1497 std::vector<std::vector<MatType> >
1498 symbolicAdjSeed(casadi_int nadj,
const std::vector<MatType>& v)
const;
1516 void print_in(std::ostream &stream,
const double** arg,
bool truncate)
const;
1521 void print_out(std::ostream &stream,
double** res,
bool truncate)
const;
1526 static void print_canonical(std::ostream &stream,
const Sparsity& sp,
const double* nz);
1531 static void print_canonical(std::ostream &stream, casadi_int sz,
const double* nz);
1536 static void print_canonical(std::ostream &stream,
double a);
1542 void set_jac_sparsity(casadi_int oind, casadi_int iind,
const Sparsity& sp);
1547 casadi_int get_dump_id()
const;
1548 void dump_in(casadi_int
id,
const double** arg)
const;
1549 void dump_out(casadi_int
id,
double** res)
const;
1556 size_t sz_arg_per_, sz_res_per_, sz_iw_per_, sz_w_per_;
1561 size_t sz_arg_tmp_, sz_res_tmp_, sz_iw_tmp_, sz_w_tmp_;
1565 template<
typename MatType>
1567 for (
auto i=v.begin(); i!=v.end(); ++i) {
1568 if (!i->is_zero())
return false;
1573 template<
typename MatType>
1574 std::vector<std::vector<MatType> >
1577 std::vector<std::vector<MatType>> fseed(nfwd);
1578 for (casadi_int dir=0; dir<nfwd; ++dir) {
1579 fseed[dir].resize(
n_in_);
1580 for (casadi_int iind=0; iind<
n_in_; ++iind) {
1581 std::string n =
"f" +
str(dir) +
"_" +
name_in_[iind];
1583 fseed[dir][iind] = MatType::sym(n, sp);
1589 template<
typename MatType>
1590 std::vector<std::vector<MatType> >
1592 symbolicAdjSeed(casadi_int nadj,
const std::vector<MatType>& v)
const {
1593 std::vector<std::vector<MatType> > aseed(nadj, v);
1594 for (casadi_int dir=0; dir<nadj; ++dir) {
1597 for (
typename std::vector<MatType>::iterator i=aseed[dir].begin();
1598 i!=aseed[dir].end();
1601 std::stringstream ss;
1603 if (nadj>1) ss << dir <<
"_";
1614 template<
typename M>
1616 bool always_inline,
bool never_inline)
const {
1620 bool matrix_call =
false;
1621 std::pair<casadi_int, casadi_int> sz;
1622 for (
auto&& a : arg) {
1623 if (!a.is_scalar() && !a.is_empty()) {
1628 }
else if (a.size()!=sz) {
1630 matrix_call =
false;
1641 for (
auto&& a : res) a = z;
1643 std::vector<M> arg1 = arg, res1;
1644 for (casadi_int c=0; c<sz.second; ++c) {
1645 for (casadi_int r=0; r<sz.first; ++r) {
1647 for (casadi_int i=0; i<arg.size(); ++i) {
1648 if (arg[i].size()==sz) arg1[i] = arg[i](r, c);
1651 call(arg1, res1, always_inline, never_inline);
1653 casadi_assert_dev(res.size() == res1.size());
1654 for (casadi_int i=0; i<res.size(); ++i) res[i](r, c) = res1[i];
1663 casadi_int npar = 1;
1665 return call(
replace_arg(arg, npar), res, always_inline, never_inline);
1669 call_gen(arg, res, npar, always_inline, never_inline);
1672 template<
typename M>
1674 project_arg(
const std::vector<M>& arg, casadi_int npar)
const {
1675 casadi_assert_dev(arg.size()==
n_in_);
1678 std::vector<bool> mapped(
n_in_);
1679 for (casadi_int i=0; i<
n_in_; ++i) {
1680 mapped[i] = arg[i].size2()!=
size2_in(i);
1684 std::vector<bool> matching(
n_in_);
1685 bool any_mismatch =
false;
1686 for (casadi_int i=0; i<
n_in_; ++i) {
1688 matching[i] = arg[i].sparsity().is_stacked(
sparsity_in(i), npar);
1692 any_mismatch = any_mismatch || !matching[i];
1697 std::vector<M> arg2(arg);
1698 for (casadi_int i=0; i<
n_in_; ++i) {
1701 arg2[i] = project(arg2[i], repmat(
sparsity_in(i), 1, npar));
1712 template<
typename M>
1714 project_res(
const std::vector<M>& arg, casadi_int npar)
const {
1718 template<
typename D>
1721 casadi_int npar,
bool always_inline,
bool never_inline)
const {
1722 std::vector< Matrix<D> > arg2 =
project_arg(arg, npar);
1725 std::vector<bool> mapped(
n_in_);
1726 for (casadi_int i=0; i<
n_in_; ++i) {
1727 mapped[i] = arg[i].size2()!=
size2_in(i);
1732 for (casadi_int i=0; i<
n_out_; ++i) {
1733 if (!res[i].sparsity().is_stacked(
sparsity_out(i), npar)) {
1739 std::vector<casadi_int> iw_tmp(
sz_iw());
1740 std::vector<D> w_tmp(
sz_w());
1743 std::vector<const D*> argp(
sz_arg());
1744 for (casadi_int i=0; i<
n_in_; ++i) argp[i]=
get_ptr(arg2[i]);
1747 std::vector<D*> resp(
sz_res());
1748 for (casadi_int i=0; i<
n_out_; ++i) resp[i]=
get_ptr(res[i]);
1751 for (casadi_int p=0; p<npar; ++p) {
1755 always_inline, never_inline)) {
1759 if (p==npar-1)
break;
1760 for (casadi_int i=0; i<
n_in_; ++i)
if (mapped[i]) argp[i] +=
nnz_in(i);
1765 template<
typename M>
1767 casadi_assert(arg.size()==
n_in_,
"Incorrect number of inputs: Expected "
1769 for (casadi_int i=0; i<
n_in_; ++i) {
1772 std::string d_arg =
str(arg[i].size1()) +
"-by-" +
str(arg[i].size2());
1774 std::string e =
"Input " +
str(i) +
" (" +
name_in_[i] +
") has mismatching shape. "
1775 "Got " + d_arg +
". Allowed dimensions, in general, are:\n"
1776 " - The input dimension N-by-M (here " + d_in +
")\n"
1777 " - A scalar, i.e. 1-by-1\n"
1778 " - M-by-N if N=1 or M=1 (i.e. a transposed vector)\n"
1779 " - N-by-M1 if K*M1=M for some K (argument repeated horizontally)\n";
1781 e +=
" - N-by-P*M, indicating evaluation with multiple arguments (P must be a "
1782 "multiple of " +
str(npar) +
" for consistency with previous inputs)";
1789 template<
typename M>
1791 casadi_assert(res.size()==
n_out_,
"Incorrect number of outputs: Expected "
1793 for (casadi_int i=0; i<
n_out_; ++i) {
1795 "Output " +
str(i) +
" (" +
name_out_[i] +
") has mismatching shape. "
1800 template<
typename M>
1803 for (casadi_int i=0; i<
n_in_; ++i) {
1804 if (arg.at(i).size1()!=
size1_in(i))
return false;
1805 if (arg.at(i).size2()!=
size2_in(i) && arg.at(i).size2()!=npar*
size2_in(i))
return false;
1810 template<
typename M>
1813 for (casadi_int i=0; i<
n_out_; ++i) {
1814 if (res.at(i).size1()!=
size1_out(i))
return false;
1815 if (res.at(i).size2()!=
size2_out(i) && res.at(i).size2()!=npar*
size2_out(i))
return false;
1820 template<
typename M>
1822 if (arg.size()==inp.
size()) {
1825 }
else if (arg.is_scalar()) {
1828 }
else if (arg.is_vector() && inp.
size()==std::make_pair(arg.size2(), arg.size1())) {
1831 }
else if (arg.size1()==inp.
size1() && arg.size2()>0 && inp.
size2()>0
1832 && inp.
size2()%arg.size2()==0) {
1834 return repmat(arg, 1, inp.
size2()/arg.size2());
1835 }
else if (npar!=-1 && arg.size1()==inp.
size1() && arg.size2()>0 && inp.
size2()>0
1836 && (npar*inp.
size2())%arg.size2()==0) {
1838 return repmat(arg, 1, (npar*inp.
size2())/arg.size2());
1842 casadi_assert_dev(arg.is_empty());
1843 return M(inp.
size());
1847 template<
typename M>
1849 replace_arg(
const std::vector<M>& arg, casadi_int npar)
const {
1850 std::vector<M> r(arg.size());
1855 template<
typename M>
1857 replace_res(
const std::vector<M>& res, casadi_int npar)
const {
1858 std::vector<M> r(res.size());
1863 template<
typename M>
1865 replace_fseed(
const std::vector<std::vector<M> >& fseed, casadi_int npar)
const {
1866 std::vector<std::vector<M> > r(fseed.size());
1867 for (casadi_int d=0; d<r.size(); ++d) r[d] =
replace_arg(fseed[d], npar);
1871 template<
typename M>
1873 replace_aseed(
const std::vector<std::vector<M> >& aseed, casadi_int npar)
const {
1874 std::vector<std::vector<M> > r(aseed.size());
1875 for (casadi_int d=0; d<r.size(); ++d) r[d] =
replace_res(aseed[d], npar);
1879 template<
typename M>
1882 casadi_assert(arg.size()==
n_in_,
"Incorrect number of inputs: Expected "
1884 std::map<std::string, M> ret;
1885 for (casadi_int i=0;i<
n_in_;++i) {
1891 template<
typename M>
1893 convert_arg(
const std::map<std::string, M>& arg)
const {
1895 std::vector<M> arg_v(
n_in_);
1896 for (casadi_int i=0; i<arg_v.size(); ++i) {
1901 for (
auto&& e : arg) {
1902 arg_v.at(
index_in(e.first)) = e.second;
1908 template<
typename M>
1911 casadi_assert(res.size()==
n_out_,
"Incorrect number of outputs: Expected "
1913 std::map<std::string, M> ret;
1914 for (casadi_int i=0;i<
n_out_;++i) {
1920 template<
typename M>
1922 convert_res(
const std::map<std::string, M>& res)
const {
1924 std::vector<M> res_v(
n_out_);
1925 for (casadi_int i=0; i<res_v.size(); ++i) {
1926 res_v[i] = std::numeric_limits<double>::quiet_NaN();
1930 for (
auto&& e : res) {
1937 template<
typename MatType>
1940 if (v.size1() == sp.
size1() && v.size2() == n * sp.
size2()) {
1942 if (v.nnz() != 0 && !v.sparsity().is_stacked(sp, n)) {
1943 return project(v, repmat(sp, 1, n));
1947 casadi_assert_dev(v.is_empty());
Helper class for C code generation.
Helper class for Serialization.
Internal class for Function.
bool has_refcount_
Reference counting in codegen?
casadi_int size1_in(casadi_int ind) const
Input/output dimensions.
virtual std::string codegen_mem_type() const
Thread-local memory object type.
std::string jit_serialize_
Serialize behaviour.
std::vector< M > project_arg(const std::vector< M > &arg, casadi_int npar) const
Project sparsities.
virtual bool has_sprev() const
Is the class able to propagate seeds through the algorithm?
casadi_int nnz_out(casadi_int ind) const
Number of input/output nonzeros.
double jac_penalty_
Penalty factor for using a complete Jacobian to calculate directional derivatives.
void call_gen(const MXVector &arg, MXVector &res, casadi_int npar, bool always_inline, bool never_inline) const
Call a function, overloaded.
std::vector< Sparsity > sparsity_in_
Input and output sparsity.
virtual bool has_forward(casadi_int nfwd) const
Return function that calculates forward derivatives.
static std::string forward_name(const std::string &fcn, casadi_int nfwd)
Helper function: Get name of forward derivative function.
virtual void jit_dependencies(const std::string &fname)
Jit dependencies.
void check_arg(const std::vector< M > &arg, casadi_int &npar) const
Check if input arguments have correct length and dimensions.
std::vector< std::vector< M > > replace_fseed(const std::vector< std::vector< M >> &fseed, casadi_int npar) const
Replace 0-by-0 forward seeds.
std::vector< bool > is_diff_out_
std::vector< std::vector< MatType > > fwd_seed(casadi_int nfwd) const
Symbolic expressions for the forward seeds.
virtual bool codegen_mem_is_opaque() const
Is thread-local memory object managed by checkout/release.
std::string jit_name_
Name if jit source file.
int eval_gen(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w, void *mem, bool always_inline, bool never_inline) const
Evaluate a function, overloaded.
virtual void codegen_free_mem(CodeGenerator &g) const
Codegen for free_mem.
std::string compiler_plugin_
Just-in-time compiler.
casadi_release_t release_
Release redirected to a C function.
std::pair< casadi_int, casadi_int > size_in(casadi_int ind) const
Input/output dimensions.
virtual bool has_eval_dm() const
Evaluate with DM matrices.
virtual std::vector< double > get_nominal_out(casadi_int ind) const
const Options & get_options() const override
Options.
Function custom_jacobian_
const Sparsity & sparsity_in(casadi_int ind) const
Input/output sparsity.
void * user_data_
User-set field.
std::vector< M > replace_arg(const std::vector< M > &arg, casadi_int npar) const
Replace 0-by-0 inputs.
virtual bool has_jac_sparsity(casadi_int oind, casadi_int iind) const
Get Jacobian sparsity.
int eval_gen(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w, void *mem, bool always_inline, bool never_inline) const
Evaluate a function, overloaded.
virtual void set_work(void *mem, const double **&arg, double **&res, casadi_int *&iw, double *&w) const
Set the (persistent) work vectors.
std::string jit_base_name_
static std::map< std::string, ProtoFunction *(*)(DeserializingStream &)> deserialize_map
std::vector< std::vector< MatType > > symbolicAdjSeed(casadi_int nadj, const std::vector< MatType > &v) const
Symbolic expressions for the adjoint seeds.
double ad_weight_
Weighting factor for derivative calculation and sparsity pattern calculation.
virtual bool has_jacobian() const
Return Jacobian of all input elements with respect to all output elements.
std::vector< Function > registered_functions_
bool inputs_check_
Errors are thrown if numerical values of inputs look bad.
casadi_int nnz_in(casadi_int ind) const
Number of input/output nonzeros.
bool jit_
Use just-in-time compiler.
casadi_int index_out(const std::string &name) const
Get output scheme index by name.
virtual double get_min_in(casadi_int ind) const
Get smallest input value.
eval_t eval_
Numerical evaluation redirected to a C function.
bool has_refcount_in_deps_
Reference counting in dependent functions.
casadi_int numel_out(casadi_int ind) const
Number of input/output elements.
virtual std::string getAdaptorSolverName() const
Obtain solver name from Adaptor.
void * alloc_mem() const override
Create memory block.
virtual bool uses_output() const
Do the derivative functions need nondifferentiated outputs?
size_t n_in_
Number of inputs and outputs.
std::map< std::string, M > convert_res(const std::vector< M > &res) const
Convert from/to input/output lists/map.
size_t sz_res() const
Get required length of res field.
casadi_int size2_out(casadi_int ind) const
Input/output dimensions.
virtual void set_temp(void *mem, const double **arg, double **res, casadi_int *iw, double *w) const
Set the (temporary) work vectors.
bool matching_arg(const std::vector< M > &arg, casadi_int &npar) const
Check if input arguments that needs to be replaced.
std::vector< M > project_res(const std::vector< M > &arg, casadi_int npar) const
Project sparsities.
casadi_int size1_out(casadi_int ind) const
Input/output dimensions.
std::pair< casadi_int, casadi_int > size_out(casadi_int ind) const
Input/output dimensions.
virtual bool has_function(const std::string &fname) const
WeakCache< std::string, Function > cache_
Function cache.
casadi_checkout_t checkout_
Checkout redirected to a C function.
casadi_int nnz_in() const
Number of input/output nonzeros.
virtual void disp_more(std::ostream &stream) const
Print more.
static const Options options_
Options.
casadi_int numel_in(casadi_int ind) const
Number of input/output elements.
std::vector< M > replace_res(const std::vector< M > &res, casadi_int npar) const
Replace 0-by-0 outputs.
virtual bool get_diff_in(casadi_int i)
Which inputs are differentiable.
bool jit_temp_suffix_
Use a temporary name.
casadi_int max_num_dir_
Maximum number of sensitivity directions.
virtual std::vector< double > get_nominal_in(casadi_int ind) const
void call(const std::vector< M > &arg, std::vector< M > &res, bool always_inline, bool never_inline) const
Call a function, templated.
const Sparsity & sparsity_out(casadi_int ind) const
Input/output sparsity.
bool matching_res(const std::vector< M > &arg, casadi_int &npar) const
Check if output arguments that needs to be replaced.
casadi_int index_in(const std::string &name) const
Get input scheme index by name.
size_t sz_w() const
Get required length of w field.
bool jit_cleanup_
Cleanup jit source file.
virtual bool codegen_needs_mem() const
Is thread-local memory object needed?
static MatType ensure_stacked(const MatType &v, const Sparsity &sp, casadi_int n)
Ensure that a matrix's sparsity is a horizontal multiple of another, or empty.
std::string jit_directory_
Dict stats_
Dict of statistics (resulting from evaluate)
int eval_gen(const double **arg, double **res, casadi_int *iw, double *w, void *mem, bool always_inline, bool never_inline) const
Evaluate numerically.
bool all_scalar() const
Are all inputs and outputs scalar.
casadi_int nnz_out() const
Number of input/output nonzeros.
size_t sz_arg() const
Get required length of arg field.
virtual double get_abstol() const
Get absolute tolerance.
virtual bool has_codegen() const
Is codegen supported?
static bool check_mat(const Sparsity &arg, const Sparsity &inp, casadi_int &npar)
void check_res(const std::vector< M > &res, casadi_int &npar) const
Check if output arguments have correct length and dimensions.
virtual bool has_free() const
Does the function have free variables.
virtual bool has_reverse(casadi_int nadj) const
Return function that calculates adjoint derivatives.
std::map< std::string, M > convert_arg(const std::vector< M > &arg) const
Convert from/to input/output lists/map.
virtual double get_reltol() const
Get relative tolerance.
std::vector< bool > is_diff_in_
Are inputs and outputs differentiable?
size_t sz_iw() const
Get required length of iw field.
static bool purgable(const std::vector< MatType > &seed)
Can a derivative direction be skipped.
Dict cache_init_
Values to prepopulate the function cache with.
void free_mem(void *mem) const override
Free memory block.
std::vector< std::string > name_out_
virtual bool get_diff_out(casadi_int i)
Which outputs are differentiable.
Function derivative_of_
If the function is the derivative of another function.
virtual bool has_spfwd() const
Is the class able to propagate seeds through the algorithm?
static std::string reverse_name(const std::string &fcn, casadi_int nadj)
Helper function: Get name of adjoint derivative function.
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 size2_in(casadi_int ind) const
Input/output dimensions.
virtual double get_default_in(casadi_int ind) const
Get default input value.
std::vector< std::string > name_in_
Input and output scheme.
virtual double get_max_in(casadi_int ind) const
Get largest input value.
std::map< std::string, std::vector< std::string > > AuxOut
static Matrix< Scalar > zeros(casadi_int nrow=1, casadi_int ncol=1)
Create a dense matrix or a matrix with specified sparsity with all entries zero.
Generic data type, can hold different types such as bool, casadi_int, std::string etc.
Sparse matrix class. SX and DM are specializations.
Base class for FunctionInternal and LinsolInternal.
bool error_on_fail_
Throw an exception on failure?
virtual void serialize_type(SerializingStream &s) const
Serialize type information.
virtual void * alloc_mem() const
Create memory block.
virtual const Options & get_options() const
Options.
bool regularity_check_
Errors are thrown when NaN is produced.
virtual void free_mem(void *mem) const
Free memory block.
void * memory(int ind) const
Memory objects.
bool verbose_
Verbose printout.
virtual std::string serialize_base_function() const
String used to identify the immediate FunctionInternal subclass.
virtual void check_mem_count(casadi_int n) const
Check for validatity of memory object count.
static const Options options_
Options.
The basic scalar symbolic class of CasADi.
Helper class for Serialization.
casadi_int size1() const
Get the number of rows.
casadi_int size2() const
Get the number of columns.
std::pair< casadi_int, casadi_int > size() const
Get the shape.
const double eps
Machine epsilon.
unsigned long long bvec_t
int(* casadi_checkout_t)(void)
Function pointer types for the C API.
std::vector< std::pair< std::string, T > > zip(const std::vector< std::string > &id, const std::vector< T > &mat)
M replace_mat(const M &arg, const Sparsity &inp, casadi_int npar)
std::vector< casadi_int > find(const std::vector< T > &v)
find nonzeros
int(* eval_t)(const double **arg, double **res, casadi_int *iw, double *w, int)
Function pointer types for the C API.
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.
void(* casadi_release_t)(int)
Function pointer types for the C API.
void(* signal_t)(void)
Function pointer types for the C API.
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.
std::vector< T > reverse(const std::vector< T > &v)
Reverse a list.
Function memory with temporary work vectors.
Options metadata for a class.
Function memory with temporary work vectors.
std::map< std::string, FStats > fstats
void add_stat(const std::string &s)