26 #ifndef CASADI_CODE_GENERATOR_HPP
27 #define CASADI_CODE_GENERATOR_HPP
29 #include "function.hpp"
53 void dump(std::ostream& s);
66 std::string
generate(
const std::string& prefix=
"");
69 void add_include(
const std::string& new_include,
bool relative_path=
false,
70 const std::string& use_ifdef=std::string());
74 std::string add_dependency(
const Function& f);
77 void add_external(
const std::string& new_external);
80 std::string shorthand(
const std::string& name)
const;
83 std::string shorthand(
const std::string& name,
bool allow_adding=
true);
86 std::string sparsity(
const Sparsity& sp);
89 casadi_int add_sparsity(
const Sparsity& sp);
94 casadi_int get_sparsity(
const Sparsity& sp)
const;
99 casadi_int get_constant(
const std::vector<double>& v,
bool allow_adding=
false);
104 casadi_int get_constant(
const std::vector<casadi_int>& v,
bool allow_adding=
false);
109 casadi_int get_constant(
const std::vector<char>& v,
bool allow_adding=
false);
114 casadi_int get_constant(
const std::vector<std::string>& v,
bool allow_adding=
false);
119 std::string constant(
const std::vector<casadi_int>& v);
124 std::string constant(
const std::vector<int>& v) {
125 return constant(vector_static_cast<casadi_int>(v));
132 const std::string& var_name,
133 const std::vector<casadi_int>& v,
134 const std::string& type=
"casadi_int");
139 std::string constant(
const std::vector<double>& v);
144 std::string constant(
const std::vector<char>& v);
149 std::string constant(
const std::vector<std::string>& v);
154 void define_rom_double(
const void*
id, casadi_int size);
159 std::string rom_double(
const void*
id)
const;
164 void define_rom_integer(
const void*
id, casadi_int size);
169 void setup_callback(
const std::string& s,
const Function& f);
173 std::string rom_integer(
const void*
id)
const;
178 std::string operator()(
const Function& f,
const std::string& arg,
179 const std::string& res,
const std::string& iw,
180 const std::string& w,
const std::string& failure_ret=
"1");
190 void print_formatted(
const std::string& s);
197 std::stringstream ss;
199 return (*
this) << ss.str();
205 void flush(std::ostream &s);
210 void local(
const std::string& name,
const std::string& type,
const std::string& ref=
"");
225 std::string sx_work(casadi_int i);
230 void init_local(
const std::string& name,
const std::string& def);
235 void indent() {current_indent_++;}
240 void unindent() {current_indent_--;}
245 bool avoid_stack() {
return avoid_stack_;}
250 std::string constant(
double v);
251 std::string constant(casadi_int v);
252 std::string constant(
const std::string& v);
257 std::string initializer(
const std::vector<double>& v);
258 std::string initializer(
const std::vector<casadi_int>& v);
259 std::string initializer(
const std::vector<char>& v);
260 std::string initializer(
const std::vector<std::string>& v);
265 std::string sanitize_source(
const std::string& src,
266 const std::vector<std::string>& inst,
267 bool add_shorthand=
true);
272 std::string dot(casadi_int n,
const std::string& x,
const std::string& y);
277 std::string mv(
const std::string& x,
const Sparsity& sp_x,
278 const std::string& y,
const std::string& z,
bool tr);
283 std::string mv(
const std::string& x, casadi_int nrow_x, casadi_int ncol_x,
284 const std::string& y,
const std::string& z,
bool tr);
289 std::string axpy(casadi_int n,
const std::string& a,
290 const std::string& x,
const std::string& y);
297 std::string clip_min(
const std::string& x, casadi_int n,
298 const std::string& min,
const std::string& mask);
305 std::string clip_max(
const std::string& x, casadi_int n,
306 const std::string& min,
const std::string& mask);
312 std::string vector_fmax(casadi_int n,
const std::string& x,
313 const std::string& y,
const std::string& z);
319 std::string vector_fmin(casadi_int n,
const std::string& x,
320 const std::string& y,
const std::string& z);
326 std::string masked_norm_inf(casadi_int n,
const std::string& x,
327 const std::string& mask);
333 std::string scal(casadi_int n,
const std::string& alpha,
const std::string& x);
338 std::string mtimes(
const std::string& x,
const Sparsity& sp_x,
339 const std::string& y,
const Sparsity& sp_y,
340 const std::string& z,
const Sparsity& sp_z,
341 const std::string& w,
bool tr);
346 std::string trilsolve(
const Sparsity& sp_x,
const std::string& x,
const std::string& y,
347 bool tr,
bool unity, casadi_int nrhs);
352 std::string triusolve(
const Sparsity& sp_x,
const std::string& x,
const std::string& y,
353 bool tr,
bool unity, casadi_int nrhs);
358 std::string bilin(
const std::string& A,
const Sparsity& sp_A,
359 const std::string& x,
const std::string& y);
364 std::string rank1(
const std::string& A,
const Sparsity& sp_A,
const std::string& alpha,
365 const std::string& x,
const std::string& y);
368 std::string logsumexp(
const std::string& A, casadi_int n);
373 std::string interpn(
const std::string& res, casadi_int ndim,
const std::string& grid,
374 const std::string& offset,
375 const std::string& values,
const std::string& x,
376 const std::string& lookup_mode, casadi_int m,
377 const std::string& iw,
const std::string& w);
382 std::string interpn_grad(
const std::string& grad,
383 casadi_int ndim,
const std::string& grid,
384 const std::string& offset,
385 const std::string& values,
const std::string& x,
386 const std::string& lookup_mode, casadi_int m,
387 const std::string& iw,
const std::string& w);
392 std::string trans(
const std::string& x,
const Sparsity& sp_x,
393 const std::string& y,
const Sparsity& sp_y,
const std::string& iw);
398 std::string qr(
const std::string& sp,
const std::string& A,
399 const std::string& w,
const std::string& sp_v,
400 const std::string& v,
const std::string& sp_r,
401 const std::string& r,
const std::string& beta,
402 const std::string& prinv,
const std::string& pc);
407 std::string qr_solve(
const std::string& x, casadi_int nrhs,
bool tr,
408 const std::string& sp_v,
const std::string& v,
409 const std::string& sp_r,
const std::string& r,
410 const std::string& beta,
const std::string& prinv,
411 const std::string& pc,
const std::string& w);
416 std::string lsqr_solve(
const std::string& A,
const std::string&x,
417 casadi_int nrhs,
bool tr,
const std::string& sp,
const std::string& w);
422 std::string ldl(
const std::string& sp_a,
const std::string& a,
423 const std::string& sp_lt,
const std::string& lt,
424 const std::string& d,
const std::string& p,
425 const std::string& w);
430 std::string ldl_solve(
const std::string& x, casadi_int nrhs,
431 const std::string& sp_lt,
const std::string& lt,
432 const std::string& d,
const std::string& p,
433 const std::string& w);
438 std::string fmax(
const std::string& x,
const std::string& y);
443 std::string fmin(
const std::string& x,
const std::string& y);
448 std::string mmax(
const std::string& x, casadi_int n,
bool is_dense);
453 std::string mmin(
const std::string& x, casadi_int n,
bool is_dense);
458 std::string vfmax(
const std::string& x, casadi_int n,
const std::string& y);
463 std::string vfmin(
const std::string& x, casadi_int n,
const std::string& y);
468 std::string vfmax(
const std::string& x,
const std::string& n,
const std::string& y);
473 std::string vfmin(
const std::string& x,
const std::string& n,
const std::string& y);
478 std::string max(
const std::string& x,
const std::string& y);
483 std::string min(
const std::string& x,
const std::string& y);
488 std::string norm_inf(casadi_int n,
const std::string& x);
495 std::string norm_2(casadi_int n,
const std::string& x);
500 std::string max_viol(casadi_int n,
const std::string& x,
501 const std::string& lb,
const std::string& ub);
506 std::string sum_viol(casadi_int n,
const std::string& x,
507 const std::string& lb,
const std::string& ub);
512 std::string bound_consistency(casadi_int n,
const std::string& x,
513 const std::string& lam,
const std::string& lbx,
const std::string& ubx);
518 std::string lb_eig(
const Sparsity& sp_h,
const std::string& h);
523 std::string regularize(
const Sparsity& sp_h,
const std::string& h,
const std::string& reg);
528 std::string convexify_eval(
const ConvexifyData& d,
529 const std::string& Hin,
const std::string& Hout,
const std::string& iw,
const std::string& w);
534 std::string low(
const std::string& x,
const std::string& grid,
535 casadi_int ng, casadi_int lookup_mode);
540 std::string declare(std::string s);
545 void comment(
const std::string& s);
588 AUX_INTERPN_INTERPOLATE,
597 AUX_FEASIBLESQPMETHOD,
622 AUX_BOUNDS_CONSISTENCY,
643 void add_auxiliary(Auxiliary f,
const std::vector<std::string>& inst = {
"casadi_real"});
648 void add_io_sparsities(
const std::string& name,
649 const std::vector<Sparsity>& sp_in,
650 const std::vector<Sparsity>& sp_out);
653 std::string work(casadi_int n, casadi_int sz)
const;
656 std::string workel(casadi_int n)
const;
659 static std::string array(
const std::string& type,
const std::string& name, casadi_int len,
660 const std::string& def=std::string());
665 void print_vector(std::ostream &s,
const std::string& name,
666 const std::vector<casadi_int>& v);
671 void print_vector(std::ostream &s,
const std::string& name,
672 const std::vector<char>& v);
677 void print_vector(std::ostream &s,
const std::string& name,
678 const std::vector<double>& v);
683 void print_vector(std::ostream &s,
const std::string& name,
684 const std::vector<std::string>& v);
689 std::string copy(
const std::string& arg, std::size_t n,
const std::string& res);
690 void copy_check(
const std::string& arg, std::size_t n,
const std::string& res,
691 bool check_lhs=
true,
bool check_rhs=
true);
692 void copy_default(
const std::string& arg, std::size_t n,
const std::string& res,
693 const std::string& def,
bool check_rhs=
true);
698 std::string fill(
const std::string& res, std::size_t n,
const std::string& v);
703 std::string clear(
const std::string& res, std::size_t n);
708 std::string arg(casadi_int i)
const;
713 std::string res(casadi_int i)
const;
718 std::string mem(
const Function& f);
723 std::string project(
const std::string& arg,
const Sparsity& sp_arg,
724 const std::string& res,
const Sparsity& sp_res,
725 const std::string& w);
730 std::string tri_project(
const std::string& arg,
const Sparsity& sp_arg,
731 const std::string& res,
bool lower);
736 std::string densify(
const std::string& arg,
const Sparsity& sp_arg,
737 const std::string& res,
bool tr=
false);
742 std::string sparsify(
const std::string& arg,
const std::string& res,
743 const Sparsity& sp_res,
bool tr=
false);
748 std::string to_mex(
const Sparsity& sp,
const std::string& arg);
753 std::string from_mex(std::string& arg,
754 const std::string& res, std::size_t res_off,
const Sparsity& sp_res,
755 const std::string& w);
760 static std::string fmu_helpers(
const std::string& modelname);
765 std::string printf(
const std::string& str,
766 const std::vector<std::string>& arg=std::vector<std::string>());
767 std::string printf(
const std::string& str,
const std::string& arg1);
768 std::string printf(
const std::string& str,
const std::string& arg1,
const std::string& arg2);
769 std::string printf(
const std::string& str,
const std::string& arg1,
const std::string& arg2,
770 const std::string& arg3);
775 std::string print_op(casadi_int op,
const std::string& a0);
776 std::string print_op(casadi_int op,
const std::string& a0,
const std::string& a1);
781 std::string file_slurp(
const std::string& fname, casadi_int n,
const std::string& a);
786 std::string cache_check(
const std::string& key,
const std::string& cache,
787 const std::string& loc, casadi_int stride, casadi_int sz, casadi_int key_sz,
788 const std::string& val);
791 static std::string casadi_version();
794 static void file_open(std::ofstream& f,
const std::string& name,
bool cpp);
797 static void file_close(std::ofstream& f,
bool cpp);
802 void sz_work(
size_t& sz_arg,
size_t& sz_res,
size_t& sz_iw,
size_t& sz_w)
const;
807 void generate_casadi_real(std::ostream &s)
const;
810 void generate_casadi_int(std::ostream &s)
const;
813 void generate_mex(std::ostream &s)
const;
816 std::string codegen_sfunction(
const Function& f)
const;
819 void generate_sfunction(
const std::string& name,
const std::string& sfunction)
const;
822 void generate_main(std::ostream &s)
const;
825 void generate_export_symbol(std::ostream &s)
const;
828 void generate_import_symbol(std::ostream &s)
const;
835 std::string name, suffix;
838 std::string casadi_real_type;
841 std::string casadi_int_type;
854 std::vector<std::string> added_sfunctions;
863 bool verbose_runtime;
877 std::string infinity, nan, real_min;
885 bool codegen_scalars;
888 bool with_export, with_import;
891 std::string dll_export, dll_import;
897 std::stringstream includes;
898 std::stringstream auxiliaries;
899 std::stringstream body;
900 std::stringstream header;
901 std::stringstream buffer;
908 casadi_int current_indent_;
911 std::vector<std::string> exposed_fname;
914 std::set<std::string> sparsity_meta;
917 std::set<std::string> added_includes_;
918 std::set<std::string> added_externals_;
919 std::set<std::string> added_shorthands_;
920 std::multimap<Auxiliary, std::vector<std::string>> added_auxiliaries_;
921 std::multimap<size_t, size_t> added_double_constants_;
922 std::multimap<size_t, size_t> added_integer_constants_;
923 std::multimap<size_t, size_t> added_char_constants_;
924 std::multimap<size_t, size_t> added_string_constants_;
925 std::map<std::string, std::pair<std::string, std::string> > local_variables_;
926 std::map<std::string, std::string> local_default_;
927 std::map<const void *, casadi_int> file_scope_double_;
928 std::map<const void *, casadi_int> file_scope_integer_;
931 struct FunctionMeta {
935 std::string codegen_name;
937 std::vector<FunctionMeta> added_functions_;
940 std::map<std::string, std::map<FunctionInternal*, casadi_int> > added_wrappers_;
943 std::vector<std::vector<double> > double_constants_;
944 std::vector<std::vector<casadi_int> > integer_constants_;
945 std::vector<std::vector<char> > char_constants_;
946 std::vector<std::vector<std::string> > string_constants_;
952 static size_t hash(
const std::vector<double>& v);
953 static size_t hash(
const std::vector<casadi_int>& v);
954 static size_t hash(
const std::vector<char>& v);
955 static size_t hash(
const std::vector<std::string>& v);
957 std::string wrapper(
const Function& base,
const std::string& name);
961 static bool equal(
const std::vector<T>& v1,
const std::vector<T>& v2) {
962 if (v1.size()!=v2.size())
return false;
963 for (casadi_int j=0; j<v1.size(); ++j) {
964 if (v1[j]!=v2[j])
return false;
Helper class for C code generation.
std::string dump()
Generate a file, return code as string.
void add(const Function &f, bool with_jac_sparsity=false)
Add a function (name generated)
CodeGenerator(const std::string &name, const Dict &opts=Dict())
Constructor.
std::string generate(const std::string &prefix="")
Generate file(s)
void add_include(const std::string &new_include, bool relative_path=false, const std::string &use_ifdef=std::string())
Add an include file optionally using a relative path "..." instead of an absolute path <....
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.