List of all members | Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes
casadi::CodeGenerator Class Reference

Helper class for C code generation. More...

#include <code_generator.hpp>

Detailed Description

Author
Joel Andersson
Date
2016

Extra doc: https://github.com/casadi/casadi/wiki/L_ru

Definition at line 43 of file code_generator.hpp.

Classes

struct  FunctionMeta
 

Public Types

enum  Auxiliary {
  AUX_COPY , AUX_CVX , AUX_CONVEXIFY , AUX_SWAP ,
  AUX_SCAL , AUX_AXPY , AUX_DOT , AUX_BILIN ,
  AUX_RANK1 , AUX_NORM_1 , AUX_NORM_2 , AUX_CLIP_MAX ,
  AUX_CLIP_MIN , AUX_VECTOR_FMAX , AUX_VECTOR_FMIN , AUX_NORM_INF ,
  AUX_MASKED_NORM_INF , AUX_IAMAX , AUX_CLEAR , AUX_FILL ,
  AUX_MV , AUX_MV_DENSE , AUX_MTIMES , AUX_TRILSOLVE ,
  AUX_TRIUSOLVE , AUX_PROJECT , AUX_TRI_PROJECT , AUX_DENSIFY ,
  AUX_SPARSIFY , AUX_TRANS , AUX_TO_MEX , AUX_FROM_MEX ,
  AUX_INTERPN , AUX_INTERPN_GRAD , AUX_FLIP , AUX_INTERPN_WEIGHTS ,
  AUX_LOW , AUX_INTERPN_INTERPOLATE , AUX_DE_BOOR , AUX_ND_BOOR_EVAL ,
  AUX_FINITE_DIFF , AUX_QR , AUX_QP , AUX_QRQP ,
  AUX_NLP , AUX_SQPMETHOD , AUX_FEASIBLESQPMETHOD , AUX_LDL ,
  AUX_NEWTON , AUX_TO_DOUBLE , AUX_TO_INT , AUX_CAST ,
  AUX_SQ , AUX_SIGN , AUX_IF_ELSE , AUX_PRINTF ,
  AUX_FMIN , AUX_FMAX , AUX_FABS , AUX_MIN ,
  AUX_MAX , AUX_VFMIN , AUX_VFMAX , AUX_MAX_VIOL ,
  AUX_SUM_VIOL , AUX_SUM , AUX_REGULARIZE , AUX_INF ,
  AUX_NAN , AUX_REAL_MIN , AUX_ISINF , AUX_BOUNDS_CONSISTENCY ,
  AUX_LSQR , AUX_FILE_SLURP , AUX_CACHE , AUX_LOG1P ,
  AUX_EXPM1 , AUX_HYPOT , AUX_MMIN , AUX_MMAX ,
  AUX_LOGSUMEXP , AUX_SPARSITY , AUX_BFGS , AUX_ORACLE_CALLBACK ,
  AUX_OCP_BLOCK , AUX_ORACLE , AUX_SCALED_COPY , AUX_BLAZING_DE_BOOR ,
  AUX_BLAZING_1D_BOOR_EVAL , AUX_BLAZING_2D_BOOR_EVAL , AUX_BLAZING_3D_BOOR_EVAL , AUX_PRINTME
}
 Auxiliary functions. More...
 

Public Member Functions

 CodeGenerator (const std::string &name, const Dict &opts=Dict())
 Constructor. More...
 
void add (const Function &f, bool with_jac_sparsity=false)
 Add a function (name generated) More...
 
void dump (std::ostream &s)
 Generate the code to a stream. More...
 
std::string dump ()
 Generate a file, return code as string. More...
 
std::string generate (const std::string &prefix="")
 Generate file(s) More...
 
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 <...> More...
 
std::string add_dependency (const Function &f)
 Add a function dependency. More...
 
void add_external (const std::string &new_external)
 Add an external function declaration. More...
 
std::string shorthand (const std::string &name) const
 Get a shorthand. More...
 
std::string shorthand (const std::string &name, bool allow_adding=true)
 Add/get a shorthand. More...
 
std::string sparsity (const Sparsity &sp, bool canonical=true)
 
casadi_int add_sparsity (const Sparsity &sp, bool canonical=true)
 
casadi_int get_sparsity (const Sparsity &sp) const
 Get the index of an existing sparsity pattern. More...
 
casadi_int get_constant (const std::vector< double > &v, bool allow_adding=false)
 Get or add a constant. More...
 
casadi_int get_constant (const std::vector< casadi_int > &v, bool allow_adding=false)
 Get or add an integer constant. More...
 
casadi_int get_constant (const std::vector< char > &v, bool allow_adding=false)
 Get or add a char constant. More...
 
casadi_int get_constant (const std::vector< std::string > &v, bool allow_adding=false)
 Get or add a vector<string> constant. More...
 
std::string constant (const std::vector< casadi_int > &v)
 Represent an array constant; adding it when new. More...
 
std::string constant (const std::vector< int > &v)
 Represent an array constant; adding it when new. More...
 
void constant_copy (const std::string &var_name, const std::vector< casadi_int > &v, const std::string &type="casadi_int")
 Represent an array constant; adding it when new. More...
 
std::string constant (const std::vector< double > &v)
 Represent an array constant; adding it when new. More...
 
std::string constant (const std::vector< char > &v)
 Represent an array constant; adding it when new. More...
 
std::string constant (const std::vector< std::string > &v)
 Represent an array constant; adding it when new. More...
 
void define_rom_double (const void *id, casadi_int size)
 Allocate file scope double read-only memory. More...
 
std::string rom_double (const void *id) const
 Access file scope double read-only memory. More...
 
void define_rom_integer (const void *id, casadi_int size)
 Allocate file scope integer read-only memory. More...
 
std::string rom_integer (const void *id) const
 Access file scope integer read-only memory. More...
 
void define_pool_double (const std::string &name, const std::vector< double > &def)
 Allocate file scope double writeable memory. More...
 
std::string pool_double (const std::string &name) const
 Access file scope double writeable memory. More...
 
void setup_callback (const std::string &s, const Function &f)
 Setup a callback. More...
 
std::string operator() (const Function &f, const std::string &arg, const std::string &res, const std::string &iw, const std::string &w, const std::string &failure_ret="1")
 Generate a call to a function (generic signature) More...
 
CodeGeneratoroperator<< (const std::string &s)
 Print a string to buffer. More...
 
void print_formatted (const std::string &s)
 Print without newline characters. More...
 
template<typename T >
CodeGeneratoroperator<< (T s)
 Print an arbitrary type to buffer. More...
 
void flush (std::ostream &s)
 Flush the buffer to a stream of choice. More...
 
void local (const std::string &name, const std::string &type, const std::string &ref="")
 Declare a local variable. More...
 
void scope_enter ()
 Enter a local scope. More...
 
void scope_exit ()
 Exit a local scope. More...
 
std::string sx_work (casadi_int i)
 Declare a work vector element. More...
 
void init_local (const std::string &name, const std::string &def)
 Specify the default value for a local variable. More...
 
void indent ()
 Increase indentation. More...
 
void unindent ()
 Decrease indentation. More...
 
bool avoid_stack () const
 Avoid stack? More...
 
std::string constant (double v)
 Print a constant in a lossless but compact manner. More...
 
std::string constant (casadi_int v)
 
std::string constant (const std::string &v)
 
std::string constant (char v)
 
std::string format_padded (casadi_int i) const
 
std::string zeros (casadi_int sz)
 
std::string ones (casadi_int sz)
 
template<typename T >
std::string initializer (const std::vector< T > &v)
 Print an initializer. More...
 
std::string sanitize_source (const std::string &src, const std::vector< std::string > &inst, bool add_shorthand=true)
 Sanitize source files for codegen. More...
 
std::string dot (casadi_int n, const std::string &x, const std::string &y)
 Codegen inner product. More...
 
std::string mv (const std::string &x, const Sparsity &sp_x, const std::string &y, const std::string &z, bool tr)
 Codegen sparse matrix-vector multiplication. More...
 
std::string mv (const std::string &x, casadi_int nrow_x, casadi_int ncol_x, const std::string &y, const std::string &z, bool tr)
 Codegen dense matrix-vector multiplication. More...
 
std::string axpy (casadi_int n, const std::string &a, const std::string &x, const std::string &y)
 Codegen axpy: y += a*x. More...
 
std::string clip_min (const std::string &x, casadi_int n, const std::string &min, const std::string &mask)
 Codegen clip_min: Clips the smaller entries in a vector than min to the min. More...
 
std::string clip_max (const std::string &x, casadi_int n, const std::string &min, const std::string &mask)
 Codegen clip_max: Clips the larger entries in a vector than max to the max. More...
 
std::string vector_fmax (casadi_int n, const std::string &x, const std::string &y, const std::string &z)
 Codegen vector_fmax: Takes vectorwise max of a vector and writes the result to second vector. More...
 
std::string vector_fmin (casadi_int n, const std::string &x, const std::string &y, const std::string &z)
 Codegen vector_fmin: Takes vectorwise min of a vector and writes the result to second vector. More...
 
std::string masked_norm_inf (casadi_int n, const std::string &x, const std::string &mask)
 codegen masked_norm_inf: The mask tells what entry is used in the inf-norm. More...
 
std::string scal (casadi_int n, const std::string &alpha, const std::string &x)
 What does scal do?? More...
 
std::string mtimes (const std::string &x, const Sparsity &sp_x, const std::string &y, const Sparsity &sp_y, const std::string &z, const Sparsity &sp_z, const std::string &w, bool tr)
 Codegen sparse matrix-matrix multiplication. More...
 
std::string trilsolve (const Sparsity &sp_x, const std::string &x, const std::string &y, bool tr, bool unity, casadi_int nrhs)
 Codegen lower triangular solve. More...
 
std::string triusolve (const Sparsity &sp_x, const std::string &x, const std::string &y, bool tr, bool unity, casadi_int nrhs)
 Codegen upper triangular solve. More...
 
std::string bilin (const std::string &A, const Sparsity &sp_A, const std::string &x, const std::string &y)
 Codegen bilinear form. More...
 
std::string rank1 (const std::string &A, const Sparsity &sp_A, const std::string &alpha, const std::string &x, const std::string &y)
 Rank-1 update. More...
 
std::string logsumexp (const std::string &A, casadi_int n)
 
std::string interpn (const std::string &res, casadi_int ndim, const std::string &grid, const std::string &offset, const std::string &values, const std::string &x, const std::string &lookup_mode, casadi_int m, const std::string &iw, const std::string &w)
 Multilinear interpolation. More...
 
std::string interpn_grad (const std::string &grad, casadi_int ndim, const std::string &grid, const std::string &offset, const std::string &values, const std::string &x, const std::string &lookup_mode, casadi_int m, const std::string &iw, const std::string &w)
 Multilinear interpolation - calculate gradient. More...
 
std::string trans (const std::string &x, const Sparsity &sp_x, const std::string &y, const Sparsity &sp_y, const std::string &iw)
 Transpose. More...
 
std::string qr (const std::string &sp, const std::string &A, const std::string &w, const std::string &sp_v, const std::string &v, const std::string &sp_r, const std::string &r, const std::string &beta, const std::string &prinv, const std::string &pc)
 QR factorization. More...
 
std::string qr_solve (const std::string &x, casadi_int nrhs, bool tr, const std::string &sp_v, const std::string &v, const std::string &sp_r, const std::string &r, const std::string &beta, const std::string &prinv, const std::string &pc, const std::string &w)
 QR solve. More...
 
std::string lsqr_solve (const std::string &A, const std::string &x, casadi_int nrhs, bool tr, const std::string &sp, const std::string &w)
 
std::string ldl (const std::string &sp_a, const std::string &a, const std::string &sp_lt, const std::string &lt, const std::string &d, const std::string &p, const std::string &w)
 LDL factorization. More...
 
std::string ldl_solve (const std::string &x, casadi_int nrhs, const std::string &sp_lt, const std::string &lt, const std::string &d, const std::string &p, const std::string &w)
 LDL solve. More...
 
std::string fmax (const std::string &x, const std::string &y)
 fmax More...
 
std::string fmin (const std::string &x, const std::string &y)
 fmin More...
 
std::string mmax (const std::string &x, casadi_int n, bool is_dense)
 mmax More...
 
std::string mmin (const std::string &x, casadi_int n, bool is_dense)
 mmin More...
 
std::string vfmax (const std::string &x, casadi_int n, const std::string &y)
 vfmax More...
 
std::string vfmin (const std::string &x, casadi_int n, const std::string &y)
 vfmin More...
 
std::string vfmax (const std::string &x, const std::string &n, const std::string &y)
 vfmax More...
 
std::string vfmin (const std::string &x, const std::string &n, const std::string &y)
 vfmin More...
 
std::string max (const std::string &x, const std::string &y)
 max More...
 
std::string min (const std::string &x, const std::string &y)
 min More...
 
std::string norm_inf (casadi_int n, const std::string &x)
 norm_inf More...
 
std::string norm_1 (casadi_int n, const std::string &x)
 norm_1 More...
 
std::string norm_2 (casadi_int n, const std::string &x)
 norm_2 More...
 
std::string max_viol (casadi_int n, const std::string &x, const std::string &lb, const std::string &ub)
 max_viol More...
 
std::string sum_viol (casadi_int n, const std::string &x, const std::string &lb, const std::string &ub)
 sum_viol More...
 
std::string bound_consistency (casadi_int n, const std::string &x, const std::string &lam, const std::string &lbx, const std::string &ubx)
 bound_consistency More...
 
std::string lb_eig (const Sparsity &sp_h, const std::string &h)
 lb_eig More...
 
std::string regularize (const Sparsity &sp_h, const std::string &h, const std::string &reg)
 regularize More...
 
std::string convexify_eval (const ConvexifyData &d, const std::string &Hin, const std::string &Hout, const std::string &iw, const std::string &w)
 convexify More...
 
std::string low (const std::string &x, const std::string &grid, casadi_int ng, casadi_int lookup_mode)
 low More...
 
std::string declare (std::string s)
 Declare a function. More...
 
void comment (const std::string &s)
 Write a comment line (ignored if not verbose) More...
 
void add_auxiliary (Auxiliary f, const std::vector< std::string > &inst={"casadi_real"})
 Add a built-in auxiliary function. More...
 
void add_io_sparsities (const std::string &name, const std::vector< Sparsity > &sp_in, const std::vector< Sparsity > &sp_out)
 Add io sparsity patterns of a function. More...
 
std::string work (casadi_int n, casadi_int sz, bool is_ref) const
 
std::string workel (casadi_int n) const
 
void reserve_work (casadi_int n)
 Reserve a maximum size of work elements, used for padding of index. More...
 
void print_vector (std::ostream &s, const std::string &name, const std::vector< casadi_int > &v)
 Print casadi_int vector to a c file. More...
 
void print_vector (std::ostream &s, const std::string &name, const std::vector< char > &v)
 Print char vector to a c file. More...
 
void print_vector (std::ostream &s, const std::string &name, const std::vector< double > &v)
 Print real vector to a c file. More...
 
void print_vector (std::ostream &s, const std::string &name, const std::vector< std::string > &v)
 Print string vector to a c file. More...
 
std::string copy (const std::string &arg, std::size_t n, const std::string &res)
 Create a copy operation. More...
 
void copy_check (const std::string &arg, std::size_t n, const std::string &res, bool check_lhs=true, bool check_rhs=true)
 
void copy_default (const std::string &arg, std::size_t n, const std::string &res, const std::string &def, bool check_rhs=true)
 
bool elide_copy (casadi_int sz)
 
std::string fill (const std::string &res, std::size_t n, const std::string &v)
 Create a fill operation. More...
 
std::string clear (const std::string &res, std::size_t n)
 Create a fill operation. More...
 
std::string arg (casadi_int i) const
 Refer to argument. More...
 
std::string res (casadi_int i) const
 Refer to resuly. More...
 
std::string mem (const Function &f)
 Access thread-local memory. More...
 
std::string project (const std::string &arg, const Sparsity &sp_arg, const std::string &res, const Sparsity &sp_res, const std::string &w)
 Sparse assignment. More...
 
std::string tri_project (const std::string &arg, const Sparsity &sp_arg, const std::string &res, bool lower)
 Project triangular part. More...
 
std::string densify (const std::string &arg, const Sparsity &sp_arg, const std::string &res, bool tr=false)
 Densify. More...
 
std::string sparsify (const std::string &arg, const std::string &res, const Sparsity &sp_res, bool tr=false)
 Sparsify. More...
 
std::string to_mex (const Sparsity &sp, const std::string &arg)
 Create matrix in MATLAB's MEX format. More...
 
std::string from_mex (std::string &arg, const std::string &res, std::size_t res_off, const Sparsity &sp_res, const std::string &w)
 Get matrix from MATLAB's MEX format. More...
 
std::string printf (const std::string &str, const std::vector< std::string > &arg=std::vector< std::string >())
 Printf. More...
 
std::string printf (const std::string &str, const std::string &arg1)
 
std::string printf (const std::string &str, const std::string &arg1, const std::string &arg2)
 
std::string printf (const std::string &str, const std::string &arg1, const std::string &arg2, const std::string &arg3)
 
std::string print_op (casadi_int op, const std::string &a0)
 Print an operation to a c file. More...
 
std::string print_op (casadi_int op, const std::string &a0, const std::string &a1)
 
std::string file_slurp (const std::string &fname, casadi_int n, const std::string &a)
 Slurp a file. More...
 
std::string cache_check (const std::string &key, const std::string &cache, const std::string &loc, casadi_int stride, casadi_int sz, casadi_int key_sz, const std::string &val)
 cache check More...
 
void sz_work (size_t &sz_arg, size_t &sz_res, size_t &sz_iw, size_t &sz_w) const
 Get number of temporary variables needed for all functions. More...
 
std::string wrapper (const Function &base, const std::string &name)
 

Static Public Member Functions

static std::string array (const std::string &type, const std::string &name, casadi_int len, const std::string &def=std::string())
 
static std::string fmu_helpers (const std::string &modelname)
 FMU helper functions. More...
 
static std::string casadi_version ()
 Current CasADi version as string. More...
 
static void file_open (std::ofstream &f, const std::string &name, bool cpp)
 Print file header. More...
 
static void file_close (std::ofstream &f, bool cpp)
 Print file header. More...
 
static size_t hash (const std::vector< double > &v)
 
static size_t hash (const std::vector< casadi_int > &v)
 
static size_t hash (const std::vector< char > &v)
 
static size_t hash (const std::vector< std::string > &v)
 
template<typename T >
static bool equal (const std::vector< T > &v1, const std::vector< T > &v2)
 

Public Attributes

std::string name
 
std::string suffix
 
std::string casadi_real_type
 
std::string casadi_int_type
 
bool with_mem
 
bool with_header
 
bool mex
 
bool with_sfunction
 
std::vector< std::string > added_sfunctions
 
bool unroll_args
 
bool verbose
 
bool verbose_runtime
 
bool cpp
 
bool main
 
bool include_math
 
bool avoid_stack_
 
std::string infinity
 
std::string nan
 
std::string real_min
 
bool codegen_scalars
 Codegen scalar. More...
 
bool with_export
 
bool with_import
 
casadi_int max_declarations_per_line
 
casadi_int max_initializer_elements_per_line
 
bool force_canonical
 
std::string dll_export
 
std::string dll_import
 
std::string prefix
 
std::stringstream includes
 
std::stringstream auxiliaries
 
std::stringstream body
 
std::stringstream header
 
std::stringstream buffer
 
bool newline_
 
casadi_int indent_
 
casadi_int current_indent_
 
casadi_int sz_zeros_
 
casadi_int sz_ones_
 
casadi_int padding_length_
 
std::vector< std::string > exposed_fname
 
std::set< std::string > sparsity_meta
 
std::set< std::string > added_includes_
 
std::set< std::string > added_externals_
 
std::set< std::string > added_shorthands_
 
std::multimap< Auxiliary, std::vector< std::string > > added_auxiliaries_
 
std::multimap< size_t, size_t > added_double_constants_
 
std::multimap< size_t, size_t > added_integer_constants_
 
std::multimap< size_t, size_t > added_char_constants_
 
std::multimap< size_t, size_t > added_string_constants_
 
std::map< std::string, std::pair< std::string, std::string > > local_variables_
 
std::map< std::string, std::string > local_default_
 
std::map< const void *, casadi_int > file_scope_double_
 
std::map< const void *, casadi_int > file_scope_integer_
 
std::vector< std::vector< double > > pool_double_defaults_
 
std::map< std::string, casadi_int > pool_double_
 
std::vector< FunctionMetaadded_functions_
 
std::map< std::string, std::map< FunctionInternal *, casadi_int > > added_wrappers_
 
std::vector< std::vector< double > > double_constants_
 
std::vector< std::vector< casadi_int > > integer_constants_
 
std::vector< std::vector< char > > char_constants_
 
std::vector< std::vector< std::string > > string_constants_
 
bool needs_mem_
 

Member Enumeration Documentation

◆ Auxiliary

Extra doc: https://github.com/casadi/casadi/wiki/L_to

Enumerator
AUX_COPY 
AUX_CVX 
AUX_CONVEXIFY 
AUX_SWAP 
AUX_SCAL 
AUX_AXPY 
AUX_DOT 
AUX_BILIN 
AUX_RANK1 
AUX_NORM_1 
AUX_NORM_2 
AUX_CLIP_MAX 
AUX_CLIP_MIN 
AUX_VECTOR_FMAX 
AUX_VECTOR_FMIN 
AUX_NORM_INF 
AUX_MASKED_NORM_INF 
AUX_IAMAX 
AUX_CLEAR 
AUX_FILL 
AUX_MV 
AUX_MV_DENSE 
AUX_MTIMES 
AUX_TRILSOLVE 
AUX_TRIUSOLVE 
AUX_PROJECT 
AUX_TRI_PROJECT 
AUX_DENSIFY 
AUX_SPARSIFY 
AUX_TRANS 
AUX_TO_MEX 
AUX_FROM_MEX 
AUX_INTERPN 
AUX_INTERPN_GRAD 
AUX_FLIP 
AUX_INTERPN_WEIGHTS 
AUX_LOW 
AUX_INTERPN_INTERPOLATE 
AUX_DE_BOOR 
AUX_ND_BOOR_EVAL 
AUX_FINITE_DIFF 
AUX_QR 
AUX_QP 
AUX_QRQP 
AUX_NLP 
AUX_SQPMETHOD 
AUX_FEASIBLESQPMETHOD 
AUX_LDL 
AUX_NEWTON 
AUX_TO_DOUBLE 
AUX_TO_INT 
AUX_CAST 
AUX_SQ 
AUX_SIGN 
AUX_IF_ELSE 
AUX_PRINTF 
AUX_FMIN 
AUX_FMAX 
AUX_FABS 
AUX_MIN 
AUX_MAX 
AUX_VFMIN 
AUX_VFMAX 
AUX_MAX_VIOL 
AUX_SUM_VIOL 
AUX_SUM 
AUX_REGULARIZE 
AUX_INF 
AUX_NAN 
AUX_REAL_MIN 
AUX_ISINF 
AUX_BOUNDS_CONSISTENCY 
AUX_LSQR 
AUX_FILE_SLURP 
AUX_CACHE 
AUX_LOG1P 
AUX_EXPM1 
AUX_HYPOT 
AUX_MMIN 
AUX_MMAX 
AUX_LOGSUMEXP 
AUX_SPARSITY 
AUX_BFGS 
AUX_ORACLE_CALLBACK 
AUX_OCP_BLOCK 
AUX_ORACLE 
AUX_SCALED_COPY 
AUX_BLAZING_DE_BOOR 
AUX_BLAZING_1D_BOOR_EVAL 
AUX_BLAZING_2D_BOOR_EVAL 
AUX_BLAZING_3D_BOOR_EVAL 
AUX_PRINTME 

Definition at line 598 of file code_generator.hpp.

598  {
599  AUX_COPY,
600  AUX_CVX,
602  AUX_SWAP,
603  AUX_SCAL,
604  AUX_AXPY,
605  AUX_DOT,
606  AUX_BILIN,
607  AUX_RANK1,
608  AUX_NORM_1,
609  AUX_NORM_2,
610  AUX_CLIP_MAX,
611  AUX_CLIP_MIN,
614  AUX_NORM_INF,
616  AUX_IAMAX,
617  AUX_CLEAR,
618  AUX_FILL,
619  AUX_MV,
620  AUX_MV_DENSE,
621  AUX_MTIMES,
624  AUX_PROJECT,
626  AUX_DENSIFY,
627  AUX_SPARSIFY,
628  AUX_TRANS,
629  AUX_TO_MEX,
630  AUX_FROM_MEX,
631  AUX_INTERPN,
633  AUX_FLIP,
635  AUX_LOW,
637  AUX_DE_BOOR,
640  AUX_QR,
641  AUX_QP,
642  AUX_QRQP,
643  AUX_NLP,
646  AUX_LDL,
647  AUX_NEWTON,
649  AUX_TO_INT,
650  AUX_CAST,
651  AUX_SQ,
652  AUX_SIGN,
653  AUX_IF_ELSE,
654  AUX_PRINTF,
655  AUX_FMIN,
656  AUX_FMAX,
657  AUX_FABS,
658  AUX_MIN,
659  AUX_MAX,
660  AUX_VFMIN,
661  AUX_VFMAX,
662  AUX_MAX_VIOL,
663  AUX_SUM_VIOL,
664  AUX_SUM,
666  AUX_INF,
667  AUX_NAN,
668  AUX_REAL_MIN,
669  AUX_ISINF,
671  AUX_LSQR,
673  AUX_CACHE,
674  AUX_LOG1P,
675  AUX_EXPM1,
676  AUX_HYPOT,
677  AUX_MMIN,
678  AUX_MMAX,
680  AUX_SPARSITY,
681  AUX_BFGS,
684  AUX_ORACLE,
691  };

Constructor & Destructor Documentation

◆ CodeGenerator()

casadi::CodeGenerator::CodeGenerator ( const std::string &  name,
const Dict opts = Dict() 
)

Definition at line 37 of file code_generator.cpp.

37  {
38  // Default options
39  this->verbose = true;
40  this->verbose_runtime = false;
41  this->mex = false;
42  this->with_sfunction = false;
43  this->unroll_args = false;
44  this->cpp = false;
45  this->main = false;
46  this->casadi_real_type = "double";
47  this->casadi_int_type = CASADI_INT_TYPE_STR;
48  this->codegen_scalars = false;
49  this->with_header = false;
50  this->with_mem = false;
51  this->with_export = true;
52  this->with_import = false;
53  this->include_math = true;
54  this->infinity = "INFINITY";
55  this->nan = "NAN";
56  this->real_min = "";
57  bool prefix_set = false;
58  this->prefix = "";
59  this->max_declarations_per_line = 12;
61  this->force_canonical = false;
62 
63  avoid_stack_ = false;
64  indent_ = 2;
65  sz_zeros_ = 0;
66  sz_ones_ = 0;
67 
68  // Read options
69  for (auto&& e : opts) {
70  if (e.first=="verbose") {
71  this->verbose = e.second;
72  } else if (e.first=="verbose_runtime") {
73  this->verbose_runtime = e.second;
74  } else if (e.first=="mex") {
75  this->mex = e.second;
76  } else if (e.first=="with_sfunction") {
77  this->with_sfunction = e.second;
78  } else if (e.first=="unroll_args") {
79  this->unroll_args = e.second;
80  } else if (e.first=="cpp") {
81  this->cpp = e.second;
82  } else if (e.first=="main") {
83  this->main = e.second;
84  } else if (e.first=="casadi_real") {
85  this->casadi_real_type = e.second.to_string();
86  } else if (e.first=="casadi_int") {
87  this->casadi_int_type = e.second.to_string();
88  } else if (e.first=="codegen_scalars") {
89  this->codegen_scalars = e.second;
90  } else if (e.first=="with_header") {
91  this->with_header = e.second;
92  } else if (e.first=="with_mem") {
93  this->with_mem = e.second;
94  } else if (e.first=="with_export") {
95  this->with_export = e.second;
96  } else if (e.first=="with_import") {
97  this->with_import = e.second;
98  } else if (e.first=="include_math") {
99  this->include_math = e.second;
100  } else if (e.first=="infinity") {
101  this->infinity = e.second.to_string();
102  } else if (e.first=="nan") {
103  this->nan = e.second.to_string();
104  } else if (e.first=="real_min") {
105  this->real_min = e.second.to_string();
106  } else if (e.first=="indent") {
107  indent_ = e.second;
108  casadi_assert_dev(indent_>=0);
109  } else if (e.first=="avoid_stack") {
110  avoid_stack_ = e.second;
111  } else if (e.first=="prefix") {
112  this->prefix = e.second.to_string();
113  prefix_set = true;
114  } else if (e.first=="max_declarations_per_line") {
115  this->max_declarations_per_line = e.second;
116  casadi_assert(this->max_declarations_per_line>=0,
117  "Option max_declarations_per_line must be >=0");
118  } else if (e.first=="max_initializer_elements_per_line") {
119  this->max_initializer_elements_per_line = e.second;
120  casadi_assert(this->max_initializer_elements_per_line>=0,
121  "Option max_initializer_elements_per_line must be >=0");
122  } else if (e.first=="force_canonical") {
123  this->force_canonical = e.second;
124  } else {
125  casadi_error("Unrecognized option: " + str(e.first));
126  }
127  }
128 
129  // If real_min is not specified, make an educated guess
130  if (this->real_min.empty()) {
131  std::stringstream ss;
132  ss << std::scientific << std::setprecision(std::numeric_limits<double>::digits10 + 1);
133  if (casadi_real_type=="float") {
134  ss << std::numeric_limits<float>::min();
135  this->real_min = ss.str();
136  } else if (casadi_real_type=="double") {
137  ss << std::numeric_limits<double>::min();
138  this->real_min = ss.str();
139  } else {
140  this->real_min = "<NOT SPECIFIED>";
141  }
142  }
143 
144  // Start at new line with no indentation
145  newline_ = true;
146  current_indent_ = 0;
147 
148  // Start off without the need for thread-local memory
149  needs_mem_ = false;
150 
151  // Divide name into base and suffix (if any)
152  std::string::size_type dotpos = name.rfind('.');
153  if (dotpos==std::string::npos) {
154  this->name = name;
155  this->suffix = this->cpp ? ".cpp" : ".c";
156  } else {
157  this->name = name.substr(0, dotpos);
158  this->suffix = name.substr(dotpos);
159  }
160 
161  // Symbol prefix
162  if (this->with_export) dll_export = "CASADI_SYMBOL_EXPORT ";
163  if (this->with_import) dll_import = "CASADI_SYMBOL_IMPORT ";
164 
165  // Make sure that the base name is sane
166  casadi_assert_dev(Function::check_name(this->name));
167 
168  // Includes needed
169  if (this->include_math) add_include("math.h");
170  if (this->main) add_include("stdio.h");
172 
173  // Mex and main need string.h
174  if (this->mex || this->main) {
175  add_include("string.h");
176  }
177 
178  // Mex
179  if (this->mex) {
180  add_include("mex.h", false, "MATLAB_MEX_FILE");
181  }
182 
183  // s-Function
184  if (this->with_sfunction) {
185  this->casadi_real_type = "real_T";
186  this->casadi_int_type = "int_T";
187  this->with_header = true;
188  add_include("simstruc.h");
189  }
190 
191  // Memory struct entry point
192  if (this->with_mem) {
193  this->header << "#include <casadi/mem.h>\n";
194  }
195 
196  // Use name as default prefix
197  if (!prefix_set) {
198  this->prefix = this->name;
199  }
200 
201  }
bool codegen_scalars
Codegen scalar.
casadi_int max_initializer_elements_per_line
std::stringstream header
casadi_int max_declarations_per_line
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 <....
void add_auxiliary(Auxiliary f, const std::vector< std::string > &inst={"casadi_real"})
Add a built-in auxiliary function.
static bool check_name(const std::string &name)
Check if a string is a valid function name.
Definition: function.cpp:1316
std::string str(const T &v)
String representation, any type.

References add_auxiliary(), add_include(), AUX_PRINTF, avoid_stack_, casadi_int_type, casadi_real_type, casadi::Function::check_name(), codegen_scalars, cpp, current_indent_, dll_export, dll_import, force_canonical, header, include_math, indent_, infinity, main, max_declarations_per_line, max_initializer_elements_per_line, mex, name, nan, needs_mem_, newline_, prefix, real_min, casadi::str(), suffix, sz_ones_, sz_zeros_, unroll_args, verbose, verbose_runtime, with_export, with_header, with_import, with_mem, and with_sfunction.

Member Function Documentation

◆ add()

void casadi::CodeGenerator::add ( const Function f,
bool  with_jac_sparsity = false 
)

Definition at line 331 of file code_generator.cpp.

331  {
332  // Add if not already added
333  std::string codegen_name = add_dependency(f);
334 
335  // Define function
336  *this << declare(f->signature(f.name())) << "{\n"
337  << "return " << codegen_name << "(arg, res, iw, w, mem);\n"
338  << "}\n\n";
339 
340  if (this->unroll_args) {
341  // Define function
342  *this << declare(f->signature_unrolled(f.name())) << "{\n";
343  for (casadi_int i=0; i<f.n_in(); ++i) {
344  *this << "arg[" << i << "] = " << f.name_in(i) << ";\n";
345  }
346  for (casadi_int i=0; i<f.n_out(); ++i) {
347  *this << "res[" << i << "] = " << f.name_out(i) << ";\n";
348  }
349  *this << "return " << codegen_name << "(arg, res, iw, w, mem);\n";
350  *this << "}\n\n";
351  // Flush buffers
352  flush(this->body);
353  }
354 
355  // Generate meta information
356  f->codegen_meta(*this);
357 
358  // Generate Jacobian sparsity information
359  if (with_jac_sparsity) {
360  // Generate/get Jacobian sparsity
361  std::vector<Sparsity> jac = f.jac_sparsity();
362  // Code generate the sparsity pattern
363  add_io_sparsities("jac_" + f.name(), f->sparsity_in_, jac);
364 
365  // Flush buffers
366  flush(this->body);
367  }
368 
369  // Generate function specific code for Simulink sfunction
370  if (this->with_sfunction) this->added_sfunctions.push_back( this->codegen_sfunction(f) );
371 
372  // Add to list of exposed symbols
373  this->exposed_fname.push_back(f.name());
374  }
std::string add_dependency(const Function &f)
Add a function dependency.
std::vector< std::string > added_sfunctions
void add_io_sparsities(const std::string &name, const std::vector< Sparsity > &sp_in, const std::vector< Sparsity > &sp_out)
Add io sparsity patterns of a function.
void flush(std::ostream &s)
Flush the buffer to a stream of choice.
std::string declare(std::string s)
Declare a function.
std::stringstream body
std::vector< std::string > exposed_fname

References add_dependency(), add_io_sparsities(), added_sfunctions, body, casadi::FunctionInternal::codegen_meta(), declare(), exposed_fname, flush(), casadi::Function::jac_sparsity(), casadi::Function::n_in(), casadi::Function::n_out(), casadi::Function::name(), casadi::Function::name_in(), casadi::Function::name_out(), casadi::FunctionInternal::signature(), casadi::FunctionInternal::signature_unrolled(), casadi::FunctionInternal::sparsity_in_, unroll_args, and with_sfunction.

Referenced by casadi::DaeBuilderInternal::export_fmu(), casadi::FunctionInternal::finalize(), casadi::Function::generate(), casadi::OracleFunction::generate_dependencies(), and casadi::Scpgen::init().

◆ add_auxiliary()

void casadi::CodeGenerator::add_auxiliary ( Auxiliary  f,
const std::vector< std::string > &  inst = {"casadi_real"} 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tp

Definition at line 1355 of file code_generator.cpp.

1355  {
1356  // Look for existing instantiations
1357  auto f_match = added_auxiliaries_.equal_range(f);
1358  // Look for duplicates
1359  for (auto it=f_match.first; it!=f_match.second; ++it) {
1360  if (it->second==inst) return;
1361  }
1362  added_auxiliaries_.insert(std::make_pair(f, inst));
1363 
1364  // Add the appropriate function
1365  switch (f) {
1366  case AUX_COPY:
1367  this->auxiliaries << sanitize_source(casadi_copy_str, inst);
1368  break;
1369  case AUX_SCALED_COPY:
1370  this->auxiliaries << sanitize_source(casadi_scaled_copy_str, inst);
1371  break;
1372  case AUX_SWAP:
1373  this->auxiliaries << sanitize_source(casadi_swap_str, inst);
1374  break;
1375  case AUX_SCAL:
1376  this->auxiliaries << sanitize_source(casadi_scal_str, inst);
1377  break;
1378  case AUX_AXPY:
1379  this->auxiliaries << sanitize_source(casadi_axpy_str, inst);
1380  break;
1381  case AUX_DOT:
1382  this->auxiliaries << sanitize_source(casadi_dot_str, inst);
1383  break;
1384  case AUX_BILIN:
1385  this->auxiliaries << sanitize_source(casadi_bilin_str, inst);
1386  break;
1387  case AUX_RANK1:
1388  this->auxiliaries << sanitize_source(casadi_rank1_str, inst);
1389  break;
1390  case AUX_IAMAX:
1391  this->auxiliaries << sanitize_source(casadi_iamax_str, inst);
1392  break;
1393  case AUX_INTERPN:
1396  add_auxiliary(AUX_FLIP, {});
1398  add_auxiliary(AUX_CLEAR, {"casadi_int"});
1399  this->auxiliaries << sanitize_source(casadi_interpn_str, inst);
1400  break;
1401  case AUX_INTERPN_GRAD:
1403  this->auxiliaries << sanitize_source(casadi_interpn_grad_str, inst);
1404  break;
1405  case AUX_DE_BOOR:
1406  this->auxiliaries << sanitize_source(casadi_de_boor_str, inst);
1407  break;
1408  case AUX_ND_BOOR_EVAL:
1411  add_auxiliary(AUX_FILL, {"casadi_int"});
1413  add_auxiliary(AUX_CLEAR, {"casadi_int"});
1415  this->auxiliaries << sanitize_source(casadi_nd_boor_eval_str, inst);
1416  break;
1417  case AUX_FLIP:
1418  this->auxiliaries << sanitize_source(casadi_flip_str, inst);
1419  break;
1420  case AUX_LOW:
1421  this->auxiliaries << sanitize_source(casadi_low_str, inst);
1422  break;
1423  case AUX_INTERPN_WEIGHTS:
1425  this->auxiliaries << sanitize_source(casadi_interpn_weights_str, inst);
1426  break;
1428  this->auxiliaries << sanitize_source(casadi_interpn_interpolate_str, inst);
1429  break;
1430  case AUX_NORM_1:
1431  this->auxiliaries << sanitize_source(casadi_norm_1_str, inst);
1432  break;
1433  case AUX_NORM_2:
1435  this->auxiliaries << sanitize_source(casadi_norm_2_str, inst);
1436  break;
1437  case AUX_NORM_INF:
1439  this->auxiliaries << sanitize_source(casadi_norm_inf_str, inst);
1440  break;
1441  case AUX_VECTOR_FMAX:
1443  this->auxiliaries << sanitize_source(casadi_vector_fmax_str, inst);
1444  break;
1445  case AUX_VECTOR_FMIN:
1447  this->auxiliaries << sanitize_source(casadi_vector_fmin_str, inst);
1448  break;
1449  case AUX_MASKED_NORM_INF:
1451  this->auxiliaries << sanitize_source(casadi_masked_norm_inf_str, inst);
1452  break;
1453  case AUX_CLIP_MIN:
1454  // add_auxiliary(AUX_CLIP_MIN);
1455  this->auxiliaries << sanitize_source(casadi_clip_min_str, inst);
1456  break;
1457  case AUX_CLIP_MAX:
1458  // add_auxiliary(AUX_CLIP_MAX);
1459  this->auxiliaries << sanitize_source(casadi_clip_max_str, inst);
1460  break;
1461  case AUX_CLEAR:
1462  this->auxiliaries << sanitize_source(casadi_clear_str, inst);
1463  break;
1464  case AUX_FILL:
1465  this->auxiliaries << sanitize_source(casadi_fill_str, inst);
1466  break;
1467  case AUX_MV:
1468  this->auxiliaries << sanitize_source(casadi_mv_str, inst);
1469  break;
1470  case AUX_MV_DENSE:
1471  this->auxiliaries << sanitize_source(casadi_mv_dense_str, inst);
1472  break;
1473  case AUX_MTIMES:
1474  this->auxiliaries << sanitize_source(casadi_mtimes_str, inst);
1475  break;
1476  case AUX_TRILSOLVE:
1477  this->auxiliaries << sanitize_source(casadi_trilsolve_str, inst);
1478  break;
1479  case AUX_TRIUSOLVE:
1480  this->auxiliaries << sanitize_source(casadi_triusolve_str, inst);
1481  break;
1482  case AUX_PROJECT:
1483  this->auxiliaries << sanitize_source(casadi_project_str, inst);
1484  break;
1485  case AUX_TRI_PROJECT:
1486  this->auxiliaries << sanitize_source(casadi_tri_project_str, inst);
1487  break;
1488  case AUX_DENSIFY:
1491  {
1492  std::vector<std::string> inst2 = inst;
1493  if (inst.size()==1) inst2.push_back(inst[0]);
1494  this->auxiliaries << sanitize_source(casadi_densify_str, inst2);
1495  }
1496  break;
1497  case AUX_SPARSIFY:
1499  {
1500  std::vector<std::string> inst2 = inst;
1501  if (inst.size()==1) inst2.push_back(inst[0]);
1502  this->auxiliaries << sanitize_source(casadi_sparsify_str, inst2);
1503  }
1504  break;
1505  case AUX_TRANS:
1506  this->auxiliaries << sanitize_source(casadi_trans_str, inst);
1507  break;
1508  case AUX_TO_MEX:
1510  this->auxiliaries << "#ifdef MATLAB_MEX_FILE\n"
1511  << sanitize_source(casadi_to_mex_str, inst)
1512  << "#endif\n\n";
1513  break;
1514  case AUX_FROM_MEX:
1516  this->auxiliaries << "#ifdef MATLAB_MEX_FILE\n"
1517  << sanitize_source(casadi_from_mex_str, inst)
1518  << "#endif\n\n";
1519  break;
1520  case AUX_FINITE_DIFF:
1522  this->auxiliaries << sanitize_source(casadi_finite_diff_str, inst);
1523  break;
1524  case AUX_QR:
1529  this->auxiliaries << sanitize_source(casadi_qr_str, inst);
1530  break;
1531  case AUX_LSQR:
1538  this->auxiliaries << sanitize_source(casadi_lsqr_str, inst);
1539  break;
1540  case AUX_QP:
1541  this->auxiliaries << sanitize_source(casadi_qp_str, inst);
1542  break;
1543  case AUX_QRQP:
1557  add_include("stdarg.h");
1558  add_include("stdio.h");
1559  add_include("math.h");
1560 
1561  this->auxiliaries << sanitize_source(casadi_qrqp_str, inst);
1562  break;
1563  case AUX_NLP:
1565  this->auxiliaries << sanitize_source(casadi_nlp_str, inst);
1566  break;
1567  case AUX_SQPMETHOD:
1572  this->auxiliaries << sanitize_source(casadi_sqpmethod_str, inst);
1573  break;
1574  case AUX_FEASIBLESQPMETHOD:
1577  this->auxiliaries << sanitize_source(casadi_feasiblesqpmethod_str, inst);
1578  break;
1579  case AUX_LDL:
1580  this->auxiliaries << sanitize_source(casadi_ldl_str, inst);
1581  break;
1582  case AUX_NEWTON:
1587  this->auxiliaries << sanitize_source(casadi_newton_str, inst);
1588  break;
1589  case AUX_MAX_VIOL:
1591  this->auxiliaries << sanitize_source(casadi_max_viol_str, inst);
1592  break;
1593  case AUX_SUM_VIOL:
1594  this->auxiliaries << sanitize_source(casadi_sum_viol_str, inst);
1595  break;
1596  case AUX_SUM:
1597  this->auxiliaries << sanitize_source(casadi_sum_str, inst);
1598  break;
1599  case AUX_VFMIN:
1601  this->auxiliaries << sanitize_source(casadi_vfmin_str, inst);
1602  break;
1603  case AUX_VFMAX:
1605  this->auxiliaries << sanitize_source(casadi_vfmax_str, inst);
1606  break;
1607  case AUX_REGULARIZE:
1610  this->auxiliaries << sanitize_source(casadi_regularize_str, inst);
1611  break;
1616  this->auxiliaries << sanitize_source(casadi_bound_consistency_str, inst);
1617  break;
1618  case AUX_FILE_SLURP:
1619  add_include("stdio.h");
1620  this->auxiliaries << sanitize_source(casadi_file_slurp_str, inst);
1621  break;
1622  case AUX_CACHE:
1623  this->auxiliaries << sanitize_source(casadi_cache_str, inst);
1624  break;
1625  case AUX_CVX:
1632  this->auxiliaries << sanitize_source(casadi_cvx_str, inst);
1633  break;
1634  case AUX_CONVEXIFY:
1639  this->auxiliaries << sanitize_source(casadi_convexify_str, inst);
1640  break;
1641  case AUX_LOGSUMEXP:
1644  this->auxiliaries << sanitize_source(casadi_logsumexp_str, inst);
1645  break;
1646  case AUX_SPARSITY:
1647  this->auxiliaries << sanitize_source(casadi_sparsity_str, inst);
1648  break;
1649  case AUX_BFGS:
1658  this->auxiliaries << sanitize_source(casadi_bfgs_str, inst);
1659  break;
1660  case AUX_ORACLE:
1661  this->auxiliaries << sanitize_source(casadi_oracle_str, inst);
1662  break;
1663  case AUX_ORACLE_CALLBACK:
1664  this->auxiliaries << sanitize_source(casadi_oracle_callback_str, inst);
1665  break;
1666  case AUX_OCP_BLOCK:
1667  this->auxiliaries << sanitize_source(casadi_ocp_block_str, inst);
1668  break;
1669  case AUX_TO_DOUBLE:
1670  this->auxiliaries << "#define casadi_to_double(x) "
1671  << "(" << (this->cpp ? "static_cast<double>(x)" : "(double) x") << ")\n\n";
1672  break;
1673  case AUX_TO_INT:
1674  this->auxiliaries << "#define casadi_to_int(x) "
1675  << "(" << (this->cpp ? "static_cast<casadi_int>(x)" : "(casadi_int) x")
1676  << ")\n\n";
1677  break;
1678  case AUX_CAST:
1679  this->auxiliaries << "#define CASADI_CAST(x,y) "
1680  << "(" << (this->cpp ? "static_cast<x>(y)" : "(x) y") << ")\n\n";
1681  break;
1682  case AUX_SQ:
1683  shorthand("sq");
1684  this->auxiliaries << "casadi_real casadi_sq(casadi_real x) { return x*x;}\n\n";
1685  break;
1686  case AUX_SIGN:
1687  shorthand("sign");
1688  this->auxiliaries << "casadi_real casadi_sign(casadi_real x) "
1689  << "{ return x<0 ? -1 : x>0 ? 1 : x;}\n\n";
1690  break;
1691  case AUX_IF_ELSE:
1692  shorthand("if_else");
1693  this->auxiliaries << "casadi_real casadi_if_else"
1694  << "(casadi_real c, casadi_real x, casadi_real y) "
1695  << "{ return c!=0 ? x : y;}\n\n";
1696  break;
1697  case AUX_PRINTF:
1698  this->auxiliaries << "#ifndef CASADI_PRINTF\n";
1699  if (this->mex) {
1700  this->auxiliaries << "#ifdef MATLAB_MEX_FILE\n"
1701  << " #define CASADI_PRINTF mexPrintf\n"
1702  << "#else\n"
1703  << " #define CASADI_PRINTF printf\n"
1704  << "#endif\n";
1705  } else {
1706  add_include("stdio.h");
1707  this->auxiliaries << "#define CASADI_PRINTF printf\n";
1708  this->auxiliaries << "#ifndef CASADI_SNPRINTF\n";
1709  this->auxiliaries << "#define CASADI_SNPRINTF snprintf\n";
1710  this->auxiliaries << "#endif\n\n";
1711  }
1712  this->auxiliaries << "#endif\n\n";
1713  break;
1714  case AUX_FMIN:
1715  shorthand("fmin");
1716  this->auxiliaries << "casadi_real casadi_fmin(casadi_real x, casadi_real y) {\n"
1717  << "/* Pre-c99 compatibility */\n"
1718  << "#if __STDC_VERSION__ < 199901L\n"
1719  << " return x<y ? x : y;\n"
1720  << "#else\n"
1721  << " return fmin(x, y);\n"
1722  << "#endif\n"
1723  << "}\n\n";
1724  break;
1725  case AUX_FMAX:
1726  shorthand("fmax");
1727  this->auxiliaries << "casadi_real casadi_fmax(casadi_real x, casadi_real y) {\n"
1728  << "/* Pre-c99 compatibility */\n"
1729  << "#if __STDC_VERSION__ < 199901L\n"
1730  << " return x>y ? x : y;\n"
1731  << "#else\n"
1732  << " return fmax(x, y);\n"
1733  << "#endif\n"
1734  << "}\n\n";
1735  break;
1736  case AUX_FABS:
1737  shorthand("fabs");
1738  this->auxiliaries << "casadi_real casadi_fabs(casadi_real x) {\n"
1739  << "/* Pre-c99 compatibility */\n"
1740  << "#if __STDC_VERSION__ < 199901L\n"
1741  << " return x>0 ? x : -x;\n"
1742  << "#else\n"
1743  << " return fabs(x);\n"
1744  << "#endif\n"
1745  << "}\n\n";
1746  break;
1747  case AUX_ISINF:
1748  shorthand("isinf");
1749  this->auxiliaries << "casadi_real casadi_isinf(casadi_real x) {\n"
1750  << "/* Pre-c99 compatibility */\n"
1751  << "#if __STDC_VERSION__ < 199901L\n"
1752  << " return x== INFINITY || x==-INFINITY;\n"
1753  << "#else\n"
1754  << " return isinf(x);\n"
1755  << "#endif\n"
1756  << "}\n\n";
1757  break;
1758  case AUX_MIN:
1759  this->auxiliaries << "casadi_int casadi_min(casadi_int x, casadi_int y) {\n"
1760  << " return x>y ? y : x;\n"
1761  << "}\n\n";
1762  break;
1763  case AUX_MAX:
1764  this->auxiliaries << "casadi_int casadi_max(casadi_int x, casadi_int y) {\n"
1765  << " return x>y ? x : y;\n"
1766  << "}\n\n";
1767  break;
1768  case AUX_MMIN:
1771  this->auxiliaries << sanitize_source(casadi_mmin_str, inst);
1772  break;
1773  case AUX_MMAX:
1776  this->auxiliaries << sanitize_source(casadi_mmax_str, inst);
1777  break;
1778  case AUX_INF:
1779  this->auxiliaries << "#ifndef casadi_inf\n"
1780  << " #define casadi_inf " << this->infinity << "\n"
1781  << "#endif\n\n";
1782  break;
1783  case AUX_NAN:
1784  this->auxiliaries << "#ifndef casadi_nan\n"
1785  << " #define casadi_nan " << this->nan << "\n"
1786  << "#endif\n\n";
1787  break;
1788  case AUX_REAL_MIN:
1789  this->auxiliaries << "#ifndef casadi_real_min\n"
1790  << " #define casadi_real_min " << this->real_min << "\n"
1791  << "#endif\n\n";
1792  break;
1793  case AUX_LOG1P:
1794  shorthand("log1p");
1795  this->auxiliaries << "casadi_real casadi_log1p(casadi_real x) {\n"
1796  << "/* Pre-c99 compatibility */\n"
1797  << "#if __STDC_VERSION__ < 199901L\n"
1798  << " return log(1+x);\n"
1799  << "#else\n"
1800  << " return log1p(x);\n"
1801  << "#endif\n"
1802  << "}\n\n";
1803  break;
1804  case AUX_EXPM1:
1805  shorthand("expm1");
1806  this->auxiliaries << "casadi_real casadi_expm1(casadi_real x) {\n"
1807  << "/* Pre-c99 compatibility */\n"
1808  << "#if __STDC_VERSION__ < 199901L\n"
1809  << " return exp(x)-1;\n"
1810  << "#else\n"
1811  << " return expm1(x);\n"
1812  << "#endif\n"
1813  << "}\n\n";
1814  break;
1815  case AUX_HYPOT:
1816  shorthand("hypot");
1817  this->auxiliaries << "casadi_real casadi_hypot(casadi_real x, casadi_real y) {\n"
1818  << "/* Pre-c99 compatibility */\n"
1819  << "#if __STDC_VERSION__ < 199901L\n"
1820  << " return sqrt(x*x+y*y);\n"
1821  << "#else\n"
1822  << " return hypot(x, y);\n"
1823  << "#endif\n"
1824  << "}\n\n";
1825  break;
1826  case AUX_BLAZING_DE_BOOR:
1827  this->auxiliaries << sanitize_source(casadi_blazing_de_boor_str, inst);
1828  break;
1832  this->auxiliaries << sanitize_source(casadi_blazing_1d_boor_eval_str, inst);
1833  break;
1837  this->auxiliaries << sanitize_source(casadi_blazing_2d_boor_eval_str, inst);
1838  break;
1842  this->auxiliaries << sanitize_source(casadi_blazing_3d_boor_eval_str, inst);
1843  break;
1844  case AUX_PRINTME:
1846  this->auxiliaries << sanitize_source(casadi_printme_str, inst);
1847  break;
1848  }
1849  }
std::multimap< Auxiliary, std::vector< std::string > > added_auxiliaries_
std::string sanitize_source(const std::string &src, const std::vector< std::string > &inst, bool add_shorthand=true)
Sanitize source files for codegen.
std::string shorthand(const std::string &name) const
Get a shorthand.
std::stringstream auxiliaries

References add_include(), added_auxiliaries_, AUX_AXPY, AUX_BFGS, AUX_BILIN, AUX_BLAZING_1D_BOOR_EVAL, AUX_BLAZING_2D_BOOR_EVAL, AUX_BLAZING_3D_BOOR_EVAL, AUX_BLAZING_DE_BOOR, AUX_BOUNDS_CONSISTENCY, AUX_CACHE, AUX_CAST, AUX_CLEAR, AUX_CLIP_MAX, AUX_CLIP_MIN, AUX_CONVEXIFY, AUX_COPY, AUX_CVX, AUX_DE_BOOR, AUX_DENSIFY, AUX_DOT, AUX_EXPM1, AUX_FABS, AUX_FEASIBLESQPMETHOD, AUX_FILE_SLURP, AUX_FILL, AUX_FINITE_DIFF, AUX_FLIP, AUX_FMAX, AUX_FMIN, AUX_FROM_MEX, AUX_HYPOT, AUX_IAMAX, AUX_IF_ELSE, AUX_INF, AUX_INTERPN, AUX_INTERPN_GRAD, AUX_INTERPN_INTERPOLATE, AUX_INTERPN_WEIGHTS, AUX_ISINF, AUX_LDL, AUX_LOG1P, AUX_LOGSUMEXP, AUX_LOW, AUX_LSQR, AUX_MASKED_NORM_INF, AUX_MAX, AUX_MAX_VIOL, AUX_MIN, AUX_MMAX, AUX_MMIN, AUX_MTIMES, AUX_MV, AUX_MV_DENSE, AUX_NAN, AUX_ND_BOOR_EVAL, AUX_NEWTON, AUX_NLP, AUX_NORM_1, AUX_NORM_2, AUX_NORM_INF, AUX_OCP_BLOCK, AUX_ORACLE, AUX_ORACLE_CALLBACK, AUX_PRINTF, AUX_PRINTME, AUX_PROJECT, AUX_QP, AUX_QR, AUX_QRQP, AUX_RANK1, AUX_REAL_MIN, AUX_REGULARIZE, AUX_SCAL, AUX_SCALED_COPY, AUX_SIGN, AUX_SPARSIFY, AUX_SPARSITY, AUX_SQ, AUX_SQPMETHOD, AUX_SUM, AUX_SUM_VIOL, AUX_SWAP, AUX_TO_DOUBLE, AUX_TO_INT, AUX_TO_MEX, AUX_TRANS, AUX_TRI_PROJECT, AUX_TRILSOLVE, AUX_TRIUSOLVE, AUX_VECTOR_FMAX, AUX_VECTOR_FMIN, AUX_VFMAX, AUX_VFMIN, auxiliaries, cpp, infinity, mex, nan, real_min, sanitize_source(), and shorthand().

Referenced by axpy(), bilin(), bound_consistency(), cache_check(), clear(), clip_max(), clip_min(), casadi::BlazingSplineFunction::codegen_body(), casadi::MapSum::codegen_body(), casadi::Switch::codegen_body(), casadi::ClarabelInterface::codegen_body(), casadi::DaqpInterface::codegen_body(), casadi::HighsInterface::codegen_body(), casadi::HpipmInterface::codegen_body(), casadi::OsqpInterface::codegen_body(), casadi::FastNewton::codegen_body(), casadi::Feasiblesqpmethod::codegen_body(), casadi::Qrqp::codegen_body(), casadi::Sqpmethod::codegen_body(), casadi::FiniteDiff::codegen_declarations(), casadi::Nlpsol::codegen_declarations(), casadi::FatropInterface::codegen_declarations(), casadi::IpoptInterface::codegen_declarations(), casadi::MadnlpInterface::codegen_declarations(), casadi::Sqpmethod::codegen_declarations(), CodeGenerator(), constant(), convexify_eval(), copy(), densify(), dot(), dump(), file_slurp(), fill(), fmax(), fmin(), from_mex(), casadi::BSplineCommon::generate(), casadi::HorzRepsum::generate(), interpn(), interpn_grad(), lb_eig(), ldl(), ldl_solve(), logsumexp(), low(), lsqr_solve(), masked_norm_inf(), max(), max_viol(), min(), mmax(), mmin(), mtimes(), mv(), norm_1(), norm_2(), norm_inf(), print_op(), printf(), project(), casadi::Conic::qp_codegen_body(), qr(), qr_solve(), rank1(), regularize(), scal(), sparsify(), sum_viol(), to_mex(), trans(), tri_project(), trilsolve(), triusolve(), vector_fmax(), vector_fmin(), vfmax(), and vfmin().

◆ add_dependency()

std::string casadi::CodeGenerator::add_dependency ( const Function f)

Definition at line 250 of file code_generator.cpp.

250  {
251  // Quick return if it already exists
252  for (auto&& e : added_functions_) if (e.f==f) return e.codegen_name;
253 
254  // Give it a name
255  std::string fname = shorthand("f" + str(added_functions_.size()));
256 
257  // Add to list of functions
258  added_functions_.push_back({f, fname});
259 
260  // Generate declarations
261  f->codegen_declarations(*this);
262 
263  // Print to file
264  f->codegen(*this, fname);
265 
266  // Codegen reference count functions, if needed
267  if (f->has_refcount_) {
268  // Increase reference counter
269  *this << "void " << fname << "_incref(void) {\n";
270  f->codegen_incref(*this);
271  *this << "}\n\n";
272 
273  // Decrease reference counter
274  *this << "void " << fname << "_decref(void) {\n";
275  f->codegen_decref(*this);
276  *this << "}\n\n";
277  }
278 
279  bool fun_needs_mem = !f->codegen_mem_type().empty();
280  needs_mem_ |= fun_needs_mem;
281 
282  if (fun_needs_mem) {
283  // Alloc memory
284  *this << "int " << fname << "_alloc_mem(void) {\n";
285  flush(this->body);
286  scope_enter();
287  f->codegen_alloc_mem(*this);
288  scope_exit();
289  *this << "}\n\n";
290 
291  // Initialize memory
292  *this << "int " << fname << "_init_mem(int mem) {\n";
293  flush(this->body);
294  scope_enter();
295  f->codegen_init_mem(*this);
296  scope_exit();
297  *this << "}\n\n";
298 
299  // Clear memory
300  *this << "void " << fname << "_free_mem(int mem) {\n";
301  flush(this->body);
302  scope_enter();
303  f->codegen_free_mem(*this);
304  scope_exit();
305  *this << "}\n\n";
306 
307  // Checkout
308  *this << "int " << fname << "_checkout(void) {\n";
309  flush(this->body);
310  scope_enter();
311  f->codegen_checkout(*this);
312  scope_exit();
313  *this << "}\n\n";
314 
315  // Clear memory
316  *this << "void " << fname << "_release(int mem) {\n";
317  flush(this->body);
318  scope_enter();
319  f->codegen_release(*this);
320  scope_exit();
321  *this << "}\n\n";
322 
323  }
324 
325  // Flush to body
326  flush(this->body);
327 
328  return fname;
329  }
void scope_enter()
Enter a local scope.
void scope_exit()
Exit a local scope.
std::vector< FunctionMeta > added_functions_

References added_functions_, body, casadi::FunctionInternal::codegen(), casadi::FunctionInternal::codegen_alloc_mem(), casadi::FunctionInternal::codegen_checkout(), casadi::FunctionInternal::codegen_declarations(), casadi::FunctionInternal::codegen_decref(), casadi::FunctionInternal::codegen_free_mem(), casadi::FunctionInternal::codegen_incref(), casadi::FunctionInternal::codegen_init_mem(), casadi::FunctionInternal::codegen_mem_type(), casadi::FunctionInternal::codegen_release(), flush(), casadi::FunctionInternal::has_refcount_, needs_mem_, scope_enter(), scope_exit(), shorthand(), and casadi::str().

Referenced by add(), casadi::Call::add_dependency(), casadi::FiniteDiff::codegen_declarations(), casadi::Map::codegen_declarations(), casadi::MapSum::codegen_declarations(), casadi::Nlpsol::codegen_declarations(), casadi::Switch::codegen_declarations(), casadi::SXFunction::codegen_declarations(), casadi::FatropInterface::codegen_declarations(), casadi::IpoptInterface::codegen_declarations(), casadi::MadnlpInterface::codegen_declarations(), casadi::FastNewton::codegen_declarations(), casadi::Feasiblesqpmethod::codegen_declarations(), casadi::Sqpmethod::codegen_declarations(), operator()(), and setup_callback().

◆ add_external()

void casadi::CodeGenerator::add_external ( const std::string &  new_external)

Definition at line 1161 of file code_generator.cpp.

1161  {
1162  added_externals_.insert(new_external);
1163  }
std::set< std::string > added_externals_

References added_externals_.

Referenced by casadi::External::codegen_declarations().

◆ add_include()

void casadi::CodeGenerator::add_include ( const std::string &  new_include,
bool  relative_path = false,
const std::string &  use_ifdef = std::string() 
)

Definition at line 1100 of file code_generator.cpp.

1101  {
1102  // Register the new element
1103  bool added = added_includes_.insert(new_include).second;
1104 
1105  // Quick return if it already exists
1106  if (!added) return;
1107 
1108  // Ifdef opening
1109  if (!use_ifdef.empty()) this->includes << "#ifdef " << use_ifdef << std::endl;
1110 
1111  // Print to the header section
1112  if (relative_path) {
1113  this->includes << "#include \"" << new_include << "\"\n";
1114  } else {
1115  this->includes << "#include <" << new_include << ">\n";
1116  }
1117 
1118  // Ifdef closing
1119  if (!use_ifdef.empty()) this->includes << "#endif\n";
1120  }
std::stringstream includes
std::set< std::string > added_includes_

References added_includes_, and includes.

Referenced by add_auxiliary(), casadi::ConstantPool::add_dependency(), casadi::BlazingSplineFunction::codegen_body(), casadi::ClarabelInterface::codegen_body(), casadi::DaqpInterface::codegen_body(), casadi::HighsInterface::codegen_body(), casadi::HpipmInterface::codegen_body(), casadi::OsqpInterface::codegen_body(), casadi::FatropInterface::codegen_declarations(), casadi::IpoptInterface::codegen_declarations(), casadi::MadnlpInterface::codegen_declarations(), and CodeGenerator().

◆ add_io_sparsities()

void casadi::CodeGenerator::add_io_sparsities ( const std::string &  name,
const std::vector< Sparsity > &  sp_in,
const std::vector< Sparsity > &  sp_out 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tq

Definition at line 2416 of file code_generator.cpp.

2419  {
2420  // Insert element, quick return if it already exists
2421  if (!sparsity_meta.insert(name).second) return;
2422 
2423  // Input sparsities
2424  *this << declare("const casadi_int* " + name + "_sparsity_in(casadi_int i)") << " {\n"
2425  << "switch (i) {\n";
2426  for (casadi_int i=0; i<sp_in.size(); ++i) {
2427  *this << "case " << i << ": return " << sparsity(sp_in[i], force_canonical) << ";\n";
2428  }
2429  *this << "default: return 0;\n}\n"
2430  << "}\n\n";
2431 
2432  // Output sparsities
2433  *this << declare("const casadi_int* " + name + "_sparsity_out(casadi_int i)") << " {\n"
2434  << "switch (i) {\n";
2435  for (casadi_int i=0; i<sp_out.size(); ++i) {
2436  *this << "case " << i << ": return " << sparsity(sp_out[i], force_canonical) << ";\n";
2437  }
2438  *this << "default: return 0;\n}\n"
2439  << "}\n\n";
2440  }
std::set< std::string > sparsity_meta
std::string sparsity(const Sparsity &sp, bool canonical=true)

References declare(), force_canonical, name, sparsity(), and sparsity_meta.

Referenced by add(), and casadi::FunctionInternal::codegen_sparsities().

◆ add_sparsity()

casadi_int casadi::CodeGenerator::add_sparsity ( const Sparsity sp,
bool  canonical = true 
)

Definition at line 1178 of file code_generator.cpp.

1178  {
1179  return get_constant(sp.compress(canonical), true);
1180  }
casadi_int get_constant(const std::vector< double > &v, bool allow_adding=false)
Get or add a constant.

References casadi::Sparsity::compress(), and get_constant().

Referenced by sparsity().

◆ arg()

std::string casadi::CodeGenerator::arg ( casadi_int  i) const

◆ array()

std::string casadi::CodeGenerator::array ( const std::string &  type,
const std::string &  name,
casadi_int  len,
const std::string &  def = std::string() 
)
static

Declare an array

Definition at line 1026 of file code_generator.cpp.

1027  {
1028  std::stringstream s;
1029  s << type << " ";
1030  if (len==0) {
1031  s << "*" << name << " = 0";
1032  } else {
1033  s << name << "[" << len << "]";
1034  if (!def.empty()) s << " = " << def;
1035  }
1036  s << ";\n";
1037  return s.str();
1038  }

References name.

Referenced by casadi::FunctionInternal::codegen_meta(), and print_vector().

◆ avoid_stack()

bool casadi::CodeGenerator::avoid_stack ( ) const
inline

◆ axpy()

std::string casadi::CodeGenerator::axpy ( casadi_int  n,
const std::string &  a,
const std::string &  x,
const std::string &  y 
)

◆ bilin()

std::string casadi::CodeGenerator::bilin ( const std::string &  A,
const Sparsity sp_A,
const std::string &  x,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_su

Definition at line 1999 of file code_generator.cpp.

2000  {
2002  std::stringstream s;
2003  s << "casadi_bilin(" << A << ", " << sparsity(sp_A) << ", " << x << ", " << y << ")";
2004  return s.str();
2005  }

References add_auxiliary(), AUX_BILIN, and sparsity().

Referenced by casadi::Feasiblesqpmethod::codegen_eval_m_k(), and casadi::Bilin::generate().

◆ bound_consistency()

std::string casadi::CodeGenerator::bound_consistency ( casadi_int  n,
const std::string &  x,
const std::string &  lam,
const std::string &  lbx,
const std::string &  ubx 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_th

Definition at line 2606 of file code_generator.cpp.

2608  {
2610  return "casadi_bound_consistency(" + str(n) + ", " + x + ", " + lam +
2611  ", " + lbx + ", " + ubx + ")";
2612  }

References add_auxiliary(), AUX_BOUNDS_CONSISTENCY, and casadi::str().

Referenced by casadi::Feasiblesqpmethod::codegen_body(), and casadi::Nlpsol::codegen_body_exit().

◆ cache_check()

std::string casadi::CodeGenerator::cache_check ( const std::string &  key,
const std::string &  cache,
const std::string &  loc,
casadi_int  stride,
casadi_int  sz,
casadi_int  key_sz,
const std::string &  val 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u8

Definition at line 2620 of file code_generator.cpp.

2622  {
2624  return "cache_check(" + key + ", " + cache + ", " + loc + ", " +
2625  str(stride) + ", " + str(sz) + ", " + str(key_sz) + ", " + val + ")";
2626  }

References add_auxiliary(), AUX_CACHE, and casadi::str().

Referenced by casadi::LinsolQr::generate().

◆ casadi_version()

std::string casadi::CodeGenerator::casadi_version ( )
static

Definition at line 382 of file code_generator.cpp.

382  {
383  std::stringstream ss;
384  ss << CASADI_MAJOR_VERSION << "." << CASADI_MINOR_VERSION << "." << CASADI_PATCH_VERSION;
385  if (!CASADI_IS_RELEASE) ss << "+";
386  return ss.str();
387  }

Referenced by file_open().

◆ clear()

std::string casadi::CodeGenerator::clear ( const std::string &  res,
std::size_t  n 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tv

Definition at line 1959 of file code_generator.cpp.

1959  {
1960  std::stringstream s;
1961  // Perform operation
1963  s << "casadi_clear(" << res << ", " << n << ");";
1964  return s.str();
1965  }
std::string res(casadi_int i) const
Refer to resuly.

References add_auxiliary(), AUX_CLEAR, and res().

Referenced by casadi::Feasiblesqpmethod::codegen_body(), casadi::Sqpmethod::codegen_body(), casadi::OsqpInterface::codegen_init_mem(), casadi::Sqpmethod::codegen_solve_elastic_mode(), fill(), casadi::BSplineCommon::generate(), casadi::Constant< Value >::generate(), and casadi::HorzRepsum::generate().

◆ clip_max()

std::string casadi::CodeGenerator::clip_max ( const std::string &  x,
casadi_int  n,
const std::string &  min,
const std::string &  mask 
)

Definition at line 2155 of file code_generator.cpp.

2156  {
2158  return "casadi_clip_max(" + x + ", " + str(n) + ", " + min + ", " + mask + ");";
2159  }
std::string min(const std::string &x, const std::string &y)
min

References add_auxiliary(), AUX_CLIP_MAX, min(), and casadi::str().

Referenced by casadi::Feasiblesqpmethod::codegen_body(), and casadi::Feasiblesqpmethod::codegen_feasibility_iterations().

◆ clip_min()

std::string casadi::CodeGenerator::clip_min ( const std::string &  x,
casadi_int  n,
const std::string &  min,
const std::string &  mask 
)

Definition at line 2149 of file code_generator.cpp.

2150  {
2152  return "casadi_clip_min(" + x + ", " + str(n) + ", " + min + ", " + mask + ");";
2153  }

References add_auxiliary(), AUX_CLIP_MIN, min(), and casadi::str().

Referenced by casadi::Feasiblesqpmethod::codegen_body(), and casadi::Feasiblesqpmethod::codegen_feasibility_iterations().

◆ comment()

void casadi::CodeGenerator::comment ( const std::string &  s)

◆ constant() [1/9]

std::string casadi::CodeGenerator::constant ( casadi_int  v)

Definition at line 1894 of file code_generator.cpp.

1894  {
1895  return str(v);
1896  }

References casadi::str().

◆ constant() [2/9]

std::string casadi::CodeGenerator::constant ( char  v)

Definition at line 1898 of file code_generator.cpp.

1898  {
1899  return constant(static_cast<casadi_int>(v));
1900  }
std::string constant(const std::vector< casadi_int > &v)
Represent an array constant; adding it when new.

References constant().

◆ constant() [3/9]

std::string casadi::CodeGenerator::constant ( const std::string &  v)

Definition at line 1887 of file code_generator.cpp.

1887  {
1888  std::string ret = v;
1889  ret = replace(ret, "\\", "\\\\");
1890  ret = replace(ret, "\"", "\\\"");
1891  return "\"" + ret + "\"";
1892  }
CASADI_EXPORT std::string replace(const std::string &s, const std::string &p, const std::string &r)
Replace all occurences of p with r in s.

References casadi::replace().

◆ constant() [4/9]

std::string casadi::CodeGenerator::constant ( const std::vector< casadi_int > &  v)

◆ constant() [5/9]

std::string casadi::CodeGenerator::constant ( const std::vector< char > &  v)

Extra doc: https://github.com/casadi/casadi/wiki/L_27m

Definition at line 1317 of file code_generator.cpp.

1317  {
1318  return shorthand("b" + str(get_constant(v, true)));
1319  }

References get_constant(), shorthand(), and casadi::str().

◆ constant() [6/9]

std::string casadi::CodeGenerator::constant ( const std::vector< double > &  v)

Extra doc: https://github.com/casadi/casadi/wiki/L_s1

Definition at line 1351 of file code_generator.cpp.

1351  {
1352  return shorthand("c" + str(get_constant(v, true)));
1353  }

References get_constant(), shorthand(), and casadi::str().

◆ constant() [7/9]

std::string casadi::CodeGenerator::constant ( const std::vector< int > &  v)
inline

Extra doc: https://github.com/casadi/casadi/wiki/L_255

Definition at line 132 of file code_generator.hpp.

132  {
133  return constant(vector_static_cast<casadi_int>(v));
134  }

◆ constant() [8/9]

std::string casadi::CodeGenerator::constant ( const std::vector< std::string > &  v)

Extra doc: https://github.com/casadi/casadi/wiki/L_280

Definition at line 1321 of file code_generator.cpp.

1321  {
1322  return shorthand("a" + str(get_constant(v, true)));
1323  }

References get_constant(), shorthand(), and casadi::str().

◆ constant() [9/9]

std::string casadi::CodeGenerator::constant ( double  v)

Extra doc: https://github.com/casadi/casadi/wiki/L_sj

Definition at line 1901 of file code_generator.cpp.

1901  {
1902  std::stringstream s;
1903  if (isnan(v)) {
1905  s << "casadi_nan";
1906  } else if (isinf(v)) {
1908  if (v<0) s << "-";
1909  s << "casadi_inf";
1910  } else {
1911  casadi_int v_int = static_cast<casadi_int>(v);
1912  if (static_cast<double>(v_int)==v) {
1913  // Print integer
1914  s << v_int << ".";
1915  } else {
1916  // Print real
1917  std::ios_base::fmtflags fmtfl = s.flags(); // get current format flags
1918  s << std::scientific << std::setprecision(std::numeric_limits<double>::digits10 + 1) << v;
1919  s.flags(fmtfl); // reset current format flags
1920  }
1921  }
1922  return s.str();
1923  }

References add_auxiliary(), AUX_INF, and AUX_NAN.

◆ constant_copy()

void casadi::CodeGenerator::constant_copy ( const std::string &  var_name,
const std::vector< casadi_int > &  v,
const std::string &  type = "casadi_int" 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_s0

Definition at line 1335 of file code_generator.cpp.

1336  {
1337  std::string ref = constant(v);
1338  if (!v.empty()) {
1339  local(name+"[" + str(v.size()) + "]", type);
1340  } else {
1341  local(name, type, "*");
1342  }
1343  if (!v.empty()) {
1344  local("i", type);
1345  (*this) << "for (i=0;i<" << v.size() << ";++i) " + name + "[i] = " + ref + "[i];\n";
1346  } else {
1347  init_local(name, "0");
1348  }
1349  }
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.

References constant(), init_local(), local(), name, and casadi::str().

Referenced by casadi::OsqpInterface::codegen_init_mem().

◆ convexify_eval()

std::string casadi::CodeGenerator::convexify_eval ( const ConvexifyData d,
const std::string &  Hin,
const std::string &  Hout,
const std::string &  iw,
const std::string &  w 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tk

Definition at line 2593 of file code_generator.cpp.

2595  {
2597  return Convexify::generate(*this, d, Hin, Hout, iw, w);
2598  }
void generate(CodeGenerator &g, const std::vector< casadi_int > &arg, const std::vector< casadi_int > &res, const std::vector< bool > &arg_is_ref, std::vector< bool > &res_is_ref) const override
Generate code for the operation.
Definition: convexify.cpp:156

References add_auxiliary(), AUX_CONVEXIFY, and casadi::Convexify::generate().

Referenced by casadi::Sqpmethod::codegen_body(), and casadi::Convexify::generate().

◆ copy()

std::string casadi::CodeGenerator::copy ( const std::string &  arg,
std::size_t  n,
const std::string &  res 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tt

Definition at line 1925 of file code_generator.cpp.

1926  {
1927  std::stringstream s;
1928  // Perform operation
1930  s << "casadi_copy(" << arg << ", " << n << ", " << res << ");";
1931  return s.str();
1932  }
std::string arg(casadi_int i) const
Refer to argument.

References add_auxiliary(), arg(), AUX_COPY, and res().

Referenced by casadi::FiniteDiff::codegen_body(), casadi::JitFunction::codegen_body(), casadi::FastNewton::codegen_body(), casadi::Feasiblesqpmethod::codegen_body(), casadi::Sqpmethod::codegen_body(), casadi::Nlpsol::codegen_body_exit(), casadi::Feasiblesqpmethod::codegen_feasibility_iterations(), casadi::Sqpmethod::codegen_solve_elastic_mode(), casadi::Feasiblesqpmethod::codegen_step_update(), copy_check(), copy_default(), casadi::LinsolQr::generate(), casadi::ConstantDM::generate(), casadi::ConstantFile::generate(), casadi::ConstantPool::generate(), casadi::Einstein::generate(), casadi::Input::generate(), casadi::Output::generate(), casadi::Multiplication::generate(), casadi::DenseMultiplication::generate(), casadi::Rank1::generate(), casadi::HorzRepmat::generate(), casadi::SetNonzerosVector< Add >::generate(), casadi::SetNonzerosSlice< Add >::generate(), casadi::SetNonzerosSlice2< Add >::generate(), casadi::SetNonzerosParam< Add >::generate(), casadi::LinsolCall< Tr >::generate(), casadi::TriuSolve< Tr >::generate(), casadi::TrilSolve< Tr >::generate(), casadi::TriuSolveUnity< Tr >::generate(), casadi::TrilSolveUnity< Tr >::generate(), casadi::Split::generate(), casadi::MXNode::generate_copy(), and project().

◆ copy_check()

void casadi::CodeGenerator::copy_check ( const std::string &  arg,
std::size_t  n,
const std::string &  res,
bool  check_lhs = true,
bool  check_rhs = true 
)

Definition at line 1939 of file code_generator.cpp.

1940  {
1941  std::vector<std::string> checks;
1942  if (check_lhs) checks.push_back(arg);
1943  if (check_rhs) checks.push_back(res);
1944  if (!checks.empty()) *this << "if (" << join(checks, " && ") << ") ";
1945  *this << copy(arg, n, res) << "\n";
1946  }
std::string copy(const std::string &arg, std::size_t n, const std::string &res)
Create a copy operation.
std::string join(const std::vector< std::string > &l, const std::string &delim)

References arg(), copy(), casadi::join(), and res().

Referenced by casadi::OsqpInterface::codegen_body(), casadi::Feasiblesqpmethod::codegen_body(), casadi::Qrqp::codegen_body(), and casadi::Nlpsol::codegen_body_exit().

◆ copy_default()

void casadi::CodeGenerator::copy_default ( const std::string &  arg,
std::size_t  n,
const std::string &  res,
const std::string &  def,
bool  check_rhs = true 
)

Definition at line 1948 of file code_generator.cpp.

1949  {
1950  *this << "if (" << arg << ") {\n";
1951  if (check_rhs) *this << "if (" << res << ") ";
1952  *this << copy(arg, n, res) << "\n";
1953  *this << "} else {\n";
1954  if (check_rhs) *this << "if (" << res << ") ";
1955  *this << fill(res, n, def) << "\n";
1956  *this << "}\n";
1957  }
std::string fill(const std::string &res, std::size_t n, const std::string &v)
Create a fill operation.

References arg(), copy(), fill(), and res().

Referenced by casadi::OsqpInterface::codegen_body(), casadi::Feasiblesqpmethod::codegen_body(), casadi::Qrqp::codegen_body(), and casadi::Nlpsol::codegen_body_enter().

◆ declare()

std::string casadi::CodeGenerator::declare ( std::string  s)

Extra doc: https://github.com/casadi/casadi/wiki/L_tm

Definition at line 2050 of file code_generator.cpp.

2050  {
2051  // Add c linkage
2052  std::string cpp_prefix = this->cpp ? "extern \"C\" " : "";
2053 
2054  // To header file
2055  if (this->with_header) {
2056  this->header << cpp_prefix << this->dll_import << s << ";\n";
2057  }
2058 
2059  // Return name with declarations
2060  return cpp_prefix + this->dll_export + s;
2061  }

References cpp, dll_import, header, and with_header.

Referenced by add(), add_io_sparsities(), and casadi::FunctionInternal::codegen_meta().

◆ define_pool_double()

void casadi::CodeGenerator::define_pool_double ( const std::string &  name,
const std::vector< double > &  def 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_2aw

Definition at line 824 of file code_generator.cpp.

824  {
825  auto it = pool_double_.find(name);
826  if (it==pool_double_.end()) {
827  casadi_int index = pool_double_defaults_.size();
828  pool_double_defaults_.push_back(def);
829  shorthand("pd" + str(index));
830  pool_double_[name] = index;
831  } else {
832  casadi_assert_dev(def==pool_double_defaults_[it->second]);
833  }
834  }
std::map< std::string, casadi_int > pool_double_
std::vector< std::vector< double > > pool_double_defaults_

References name, pool_double_, pool_double_defaults_, shorthand(), and casadi::str().

Referenced by casadi::ConstantPool::add_dependency().

◆ define_rom_double()

void casadi::CodeGenerator::define_rom_double ( const void *  id,
casadi_int  size 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_s2

Definition at line 796 of file code_generator.cpp.

796  {
797  auto it = file_scope_double_.find(id);
798  casadi_assert(it==file_scope_double_.end(), "Already defined.");
799  shorthand("rd" + str(file_scope_double_.size()));
800  file_scope_double_[id] = size;
801  }
std::map< const void *, casadi_int > file_scope_double_

References file_scope_double_, shorthand(), and casadi::str().

Referenced by casadi::ConstantFile::add_dependency().

◆ define_rom_integer()

void casadi::CodeGenerator::define_rom_integer ( const void *  id,
casadi_int  size 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_s4

Definition at line 810 of file code_generator.cpp.

810  {
811  auto it = file_scope_double_.find(id);
812  casadi_assert(it==file_scope_double_.end(), "Already defined.");
813  shorthand("ri" + str(file_scope_double_.size()));
814  file_scope_double_[id] = size;
815  }

References file_scope_double_, shorthand(), and casadi::str().

◆ densify()

std::string casadi::CodeGenerator::densify ( const std::string &  arg,
const Sparsity sp_arg,
const std::string &  res,
bool  tr = false 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u1

Definition at line 2090 of file code_generator.cpp.

2091  {
2092  // Create call
2094  std::stringstream s;
2095  s << "casadi_densify(" << arg << ", " << sparsity(sp_arg) << ", " << res << ", "
2096  << (tr ? 1 : 0) << ");";
2097  return s.str();
2098  }

References add_auxiliary(), arg(), AUX_DENSIFY, res(), and sparsity().

Referenced by casadi::Densify::generate().

◆ dot()

std::string casadi::CodeGenerator::dot ( casadi_int  n,
const std::string &  x,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sm

Definition at line 1991 of file code_generator.cpp.

1992  {
1994  std::stringstream s;
1995  s << "casadi_dot(" << n << ", " << x << ", " << y << ")";
1996  return s.str();
1997  }

References add_auxiliary(), and AUX_DOT.

Referenced by casadi::Sqpmethod::codegen_body(), casadi::Feasiblesqpmethod::codegen_eval_m_k(), and casadi::Dot::generate().

◆ dump() [1/2]

std::string casadi::CodeGenerator::dump ( )

Definition at line 376 of file code_generator.cpp.

376  {
377  std::stringstream s;
378  dump(s);
379  return s.str();
380  }
std::string dump()
Generate a file, return code as string.

Referenced by generate().

◆ dump() [2/2]

void casadi::CodeGenerator::dump ( std::ostream &  s)

Definition at line 842 of file code_generator.cpp.

842  {
843  // Consistency check
844  casadi_assert_dev(current_indent_ == 0);
845 
846  // Prefix internal symbols to avoid symbol collisions
847  s << "/* How to prefix internal symbols */\n"
848  << "#ifdef CASADI_CODEGEN_PREFIX\n"
849  << " #define CASADI_NAMESPACE_CONCAT(NS, ID) _CASADI_NAMESPACE_CONCAT(NS, ID)\n"
850  << " #define _CASADI_NAMESPACE_CONCAT(NS, ID) NS ## ID\n"
851  << " #define CASADI_PREFIX(ID) CASADI_NAMESPACE_CONCAT(CODEGEN_PREFIX, ID)\n"
852  << "#else\n"
853  << " #define CASADI_PREFIX(ID) " << this->prefix << "_ ## ID\n"
854  << "#endif\n\n";
855 
856  s << this->includes.str();
857  s << std::endl;
858 
859  // Numeric types after includes: may depend on them. e.g. mex type
860  // Real type (usually double)
861  generate_casadi_real(s);
862 
863  // Integer type (usually long long)
864  generate_casadi_int(s);
865 
866  if (needs_mem_) {
867  s << "#ifndef CASADI_MAX_NUM_THREADS\n";
868  s << "#define CASADI_MAX_NUM_THREADS 1\n";
869  s << "#endif\n\n";
870  }
871 
872  // casadi/mem after numeric types to define derived types
873  // Memory struct entry point
874  if (this->with_mem) {
875  s << "#include <casadi/mem.h>\n" << std::endl;
876  }
877 
878  // Macros
879  if (!added_shorthands_.empty()) {
880  s << "/* Add prefix to internal symbols */\n";
881  for (auto&& i : added_shorthands_) {
882  s << "#define " << "casadi_" << i << " CASADI_PREFIX(" << i << ")\n";
883  }
884  s << std::endl;
885  }
886 
887  if (this->with_export) generate_export_symbol(s);
888 
889  // Check if inf/nan is needed
890  for (const auto& d : double_constants_) {
891  for (double e : d) {
892  if (isinf(e)) add_auxiliary(AUX_INF);
893  if (isnan(e)) add_auxiliary(AUX_NAN);
894  }
895  }
896 
897  // Codegen auxiliary functions
898  s << this->auxiliaries.str();
899 
900  // Print integer constants
901  if (!integer_constants_.empty()) {
902  for (casadi_int i=0; i<integer_constants_.size(); ++i) {
903  print_vector(s, "casadi_s" + str(i), integer_constants_[i]);
904  }
905  s << std::endl;
906  }
907 
908  // Print double constants
909  if (!double_constants_.empty()) {
910  for (casadi_int i=0; i<double_constants_.size(); ++i) {
911  print_vector(s, "casadi_c" + str(i), double_constants_[i]);
912  }
913  s << std::endl;
914  }
915 
916  // Print char constants
917  if (!char_constants_.empty()) {
918  for (casadi_int i=0; i<char_constants_.size(); ++i) {
919  print_vector(s, "casadi_b" + str(i), char_constants_[i]);
920  }
921  s << std::endl;
922  }
923 
924  // Print string constants
925  if (!string_constants_.empty()) {
926  for (casadi_int i=0; i<string_constants_.size(); ++i) {
927  print_vector(s, "casadi_a" + str(i), string_constants_[i]);
928  }
929  s << std::endl;
930  }
931 
932  if (sz_zeros_) {
933  std::vector<double> sz_zeros(sz_zeros_, 0);
934  print_vector(s, "casadi_zeros", std::vector<double>(sz_zeros));
935  s << std::endl;
936  }
937 
938  if (sz_ones_) {
939  std::vector<double> sz_ones(sz_ones_, 0);
940  print_vector(s, "casadi_ones", std::vector<double>(sz_ones));
941  s << std::endl;
942  }
943 
944  // Print file scope double work
945  if (!file_scope_double_.empty()) {
946  casadi_int i=0;
947  for (const auto& it : file_scope_double_) {
948  s << "static casadi_real casadi_rd" + str(i++) + "[" + str(it.second) + "];\n";
949  }
950  s << std::endl;
951  }
952 
953  // Print file scope integer work
954  if (!file_scope_integer_.empty()) {
955  casadi_int i=0;
956  for (const auto& it : file_scope_integer_) {
957  s << "static casadi_real casadi_ri" + str(i++) + "[" + str(it.second) + "];\n";
958  }
959  s << std::endl;
960  }
961 
962  // Print file scope double pool
963  if (!pool_double_.empty()) {
964  casadi_int i=0;
965  for (const auto& v : pool_double_defaults_) {
966  s << "casadi_real casadi_pd" + str(i) +
967  "[" + str(v.size()) + "] = " + initializer(v) + ";\n";
968  i++;
969  }
970  s << std::endl;
971  }
972 
973  // External function declarations
974  if (!added_externals_.empty()) {
975  s << "/* External functions */\n";
976  for (auto&& i : added_externals_) {
977  s << i << std::endl;
978  }
979  s << std::endl << std::endl;
980  }
981 
982  // Codegen body
983  s << this->body.str();
984 
985  // End with new line
986  s << std::endl;
987  }
std::vector< std::vector< char > > char_constants_
void print_vector(std::ostream &s, const std::string &name, const std::vector< casadi_int > &v)
Print casadi_int vector to a c file.
std::vector< std::vector< double > > double_constants_
std::vector< std::vector< casadi_int > > integer_constants_
std::vector< std::vector< std::string > > string_constants_
std::map< const void *, casadi_int > file_scope_integer_
std::string initializer(const std::vector< T > &v)
Print an initializer.
std::set< std::string > added_shorthands_

References add_auxiliary(), added_externals_, added_shorthands_, AUX_INF, AUX_NAN, auxiliaries, body, char_constants_, current_indent_, double_constants_, file_scope_double_, file_scope_integer_, includes, initializer(), integer_constants_, needs_mem_, pool_double_, pool_double_defaults_, prefix, print_vector(), casadi::str(), string_constants_, sz_ones_, sz_zeros_, with_export, and with_mem.

◆ elide_copy()

bool casadi::CodeGenerator::elide_copy ( casadi_int  sz)

◆ equal()

template<typename T >
static bool casadi::CodeGenerator::equal ( const std::vector< T > &  v1,
const std::vector< T > &  v2 
)
inlinestatic

Definition at line 1039 of file code_generator.hpp.

1039  {
1040  if (v1.size()!=v2.size()) return false;
1041  for (casadi_int j=0; j<v1.size(); ++j) {
1042  if (v1[j]!=v2[j]) return false;
1043  }
1044  return true;
1045  }

Referenced by get_constant().

◆ file_close()

void casadi::CodeGenerator::file_close ( std::ofstream &  f,
bool  cpp 
)
static

Definition at line 410 of file code_generator.cpp.

410  {
411  // C linkage
412  if (!cpp) {
413  f << "#ifdef __cplusplus\n"
414  << "} /* extern \"C\" */\n"
415  << "#endif\n";
416  }
417 
418  // Close file(s)
419  f.close();
420  }

References cpp.

Referenced by generate(), and casadi::DaeBuilderInternal::generate_wrapper().

◆ file_open()

void casadi::CodeGenerator::file_open ( std::ofstream &  f,
const std::string &  name,
bool  cpp 
)
static

Definition at line 389 of file code_generator.cpp.

389  {
390  // Open a file for writing
392 
393  // Print header
394  f << "/* This file was automatically generated by CasADi " << casadi_version() << ".\n"
395  << " * It consists of: \n"
396  << " * 1) content generated by CasADi runtime: not copyrighted\n"
397  << " * 2) template code copied from CasADi source: permissively licensed (MIT-0)\n"
398  << " * 3) user code: owned by the user\n"
399  << " *\n"
400  << " */\n";
401 
402  // C linkage
403  if (!cpp) {
404  f << "#ifdef __cplusplus\n"
405  << "extern \"C\" {\n"
406  << "#endif\n\n";
407  }
408  }
static std::string casadi_version()
Current CasADi version as string.
static void open(std::ofstream &, const std::string &path, std::ios_base::openmode mode=std::ios_base::out)
Definition: filesystem.cpp:115

References casadi_version(), cpp, name, and casadi::Filesystem::open().

Referenced by generate(), and casadi::DaeBuilderInternal::generate_wrapper().

◆ file_slurp()

std::string casadi::CodeGenerator::file_slurp ( const std::string &  fname,
casadi_int  n,
const std::string &  a 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u7

Definition at line 2614 of file code_generator.cpp.

2615  {
2617  return "casadi_file_slurp(\"" + fname + "\", " + str(n) + ", " + a + ")";
2618  }

References add_auxiliary(), AUX_FILE_SLURP, and casadi::str().

Referenced by casadi::ConstantFile::codegen_incref().

◆ fill()

std::string casadi::CodeGenerator::fill ( const std::string &  res,
std::size_t  n,
const std::string &  v 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tu

Definition at line 1981 of file code_generator.cpp.

1982  {
1983  if (v=="0") return clear(res, n);
1984  std::stringstream s;
1985  // Perform operation
1987  s << "casadi_fill(" << res << ", " << n << ", " << v << ");";
1988  return s.str();
1989  }
std::string clear(const std::string &res, std::size_t n)
Create a fill operation.

References add_auxiliary(), AUX_FILL, clear(), and res().

Referenced by casadi::Qrqp::codegen_body(), casadi::Sqpmethod::codegen_body(), casadi::Sqpmethod::codegen_solve_elastic_mode(), copy_default(), and casadi::Constant< Value >::generate().

◆ flush()

void casadi::CodeGenerator::flush ( std::ostream &  s)

◆ fmax()

std::string casadi::CodeGenerator::fmax ( const std::string &  x,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t4

Definition at line 2491 of file code_generator.cpp.

2492  {
2494  return "casadi_fmax(" + x + ", " + y + ");";
2495  }

References add_auxiliary(), and AUX_FMAX.

Referenced by casadi::Sqpmethod::codegen_body(), and casadi::Sqpmethod::codegen_calc_gamma_1().

◆ fmin()

std::string casadi::CodeGenerator::fmin ( const std::string &  x,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t5

Definition at line 2497 of file code_generator.cpp.

2498  {
2500  return "casadi_fmin(" + x + ", " + y + ");";
2501  }

References add_auxiliary(), and AUX_FMIN.

Referenced by casadi::Feasiblesqpmethod::codegen_tr_update().

◆ fmu_helpers()

std::string casadi::CodeGenerator::fmu_helpers ( const std::string &  modelname)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_257

Definition at line 1871 of file code_generator.cpp.

1871  {
1872  // Process C++ source
1873  std::stringstream ret;
1874  std::string line;
1875  std::istringstream stream(casadi_fmu_str);
1876  while (std::getline(stream, line)) {
1877  // Replacements
1878  if (line.find("MODELNAME") != std::string::npos) {
1879  line = replace(line, "MODELNAME", modelname);
1880  }
1881  // Append to return
1882  ret << line << "\n";
1883  }
1884  return ret.str();
1885  }

References casadi::replace().

Referenced by casadi::DaeBuilderInternal::generate_wrapper().

◆ format_padded()

std::string casadi::CodeGenerator::format_padded ( casadi_int  i) const

Definition at line 1019 of file code_generator.cpp.

1019  {
1020  std::stringstream ss;
1021  ss.str("");
1022  ss << std::setw(padding_length_) << std::setfill('0') << i;
1023  return ss.str();
1024  }

References padding_length_.

Referenced by casadi::MXFunction::codegen_body(), sx_work(), work(), and workel().

◆ from_mex()

std::string casadi::CodeGenerator::from_mex ( std::string &  arg,
const std::string &  res,
std::size_t  res_off,
const Sparsity sp_res,
const std::string &  w 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u4

Definition at line 1858 of file code_generator.cpp.

1860  {
1861  // Handle offset with recursion
1862  if (res_off!=0) return from_mex(arg, res+"+"+str(res_off), 0, sp_res, w);
1863 
1865  std::stringstream s;
1866  s << "casadi_from_mex(" << arg
1867  << ", " << res << ", " << sparsity(sp_res) << ", " << w << ");";
1868  return s.str();
1869  }
std::string from_mex(std::string &arg, const std::string &res, std::size_t res_off, const Sparsity &sp_res, const std::string &w)
Get matrix from MATLAB's MEX format.

References add_auxiliary(), arg(), AUX_FROM_MEX, res(), sparsity(), and casadi::str().

Referenced by casadi::FunctionInternal::codegen_meta().

◆ generate()

std::string casadi::CodeGenerator::generate ( const std::string &  prefix = "")

The "prefix" argument will be prepended to the generated files and may be a directory or a file prefix. returns the filename

Extra doc: https://github.com/casadi/casadi/wiki/L_rv

Definition at line 468 of file code_generator.cpp.

468  {
469  // Throw an error if the prefix contains the filename, since since syntax
470  // has changed
471  casadi_assert(prefix.find(this->name + this->suffix)==std::string::npos,
472  "The signature of CodeGenerator::generate has changed. "
473  "Instead of providing the filename, only provide the prefix.");
474 
475  // Create c file
476  std::ofstream s;
477  std::string fullname = prefix + this->name + this->suffix;
478  file_open(s, fullname, this->cpp);
479 
480  // Dump code to file
481  dump(s);
482 
483  if (!pool_double_defaults_.empty()) {
484  s << "CASADI_SYMBOL_EXPORT casadi_real* CASADI_PREFIX(get_pool_double)(const char* name) {\n";
485  for (const auto& e : pool_double_) {
486  casadi_int i = e.second;
487  s << " if (strcmp(name, \"" + e.first + "\")==0) "
488  << "return casadi_pd" + str(i) + ";\n";
489  }
490  s << " return 0;\n";
491  s << "}\n";
492  }
493 
494  // Mex entry point
495  if (this->mex) generate_mex(s);
496 
497  // Main entry point
498  if (this->main) generate_main(s);
499 
500  // Finalize file
501  file_close(s, this->cpp);
502 
503  // Generate s-function
504  if (this->with_sfunction) {
505  for (unsigned ii=0; ii<this->added_sfunctions.size(); ii++) {
506  std::string sfunction_code = this->added_sfunctions.at(ii);
507  std::string sfunction_name = this->exposed_fname.at(ii);
508  generate_sfunction(sfunction_name, sfunction_code);
509  }
510  }
511 
512  // Generate header
513  if (this->with_header) {
514  // Create a header file
515  file_open(s, prefix + this->name + ".h", this->cpp);
516 
517  // Define the casadi_real type (typically double)
518  generate_casadi_real(s);
519 
520  // Define the casadi_int type
521  generate_casadi_int(s);
522 
523  // Generate export symbol macros
524  if (this->with_import) generate_import_symbol(s);
525 
526  // Add declarations
527  s << this->header.str();
528 
529  // Finalize file
530  file_close(s, this->cpp);
531  }
532  return fullname;
533  }
static void file_close(std::ofstream &f, bool cpp)
Print file header.
static void file_open(std::ofstream &f, const std::string &name, bool cpp)
Print file header.

References added_sfunctions, cpp, dump(), exposed_fname, file_close(), file_open(), header, main, mex, name, pool_double_, pool_double_defaults_, prefix, casadi::str(), suffix, with_header, with_import, and with_sfunction.

Referenced by casadi::DaeBuilderInternal::export_fmu(), casadi::FunctionInternal::finalize(), casadi::Function::generate(), casadi::OracleFunction::generate_dependencies(), and casadi::Scpgen::init().

◆ get_constant() [1/4]

casadi_int casadi::CodeGenerator::get_constant ( const std::vector< casadi_int > &  v,
bool  allow_adding = false 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_ry

Definition at line 1244 of file code_generator.cpp.

1244  {
1245  // Hash the vector
1246  size_t h = hash(v);
1247 
1248  // Try to locate it in already added constants
1249  std::pair<std::multimap<size_t, size_t>::iterator, std::multimap<size_t, size_t>::iterator> eq =
1250  added_integer_constants_.equal_range(h);
1251  for (std::multimap<size_t, size_t>::iterator i=eq.first; i!=eq.second; ++i) {
1252  if (equal(v, integer_constants_[i->second])) return i->second;
1253  }
1254 
1255  if (allow_adding) {
1256  // Add to constants
1257  casadi_int ind = integer_constants_.size();
1258  integer_constants_.push_back(v);
1259  added_integer_constants_.insert(std::pair<size_t, size_t>(h, ind));
1260  return ind;
1261  } else {
1262  casadi_error("Constant not found");
1263  return -1;
1264  }
1265  }
static size_t hash(const std::vector< double > &v)
std::multimap< size_t, size_t > added_integer_constants_
static bool equal(const std::vector< T > &v1, const std::vector< T > &v2)

References added_integer_constants_, equal(), hash(), and integer_constants_.

◆ get_constant() [2/4]

casadi_int casadi::CodeGenerator::get_constant ( const std::vector< char > &  v,
bool  allow_adding = false 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_27l

Definition at line 1267 of file code_generator.cpp.

1267  {
1268  // Hash the vector
1269  size_t h = hash(v);
1270 
1271  // Try to locate it in already added constants
1272  std::pair<std::multimap<size_t, size_t>::iterator, std::multimap<size_t, size_t>::iterator> eq =
1273  added_char_constants_.equal_range(h);
1274  for (std::multimap<size_t, size_t>::iterator i=eq.first; i!=eq.second; ++i) {
1275  if (equal(v, char_constants_[i->second])) return i->second;
1276  }
1277 
1278  if (allow_adding) {
1279  // Add to constants
1280  casadi_int ind = char_constants_.size();
1281  char_constants_.push_back(v);
1282  added_char_constants_.insert(std::pair<size_t, size_t>(h, ind));
1283  return ind;
1284  } else {
1285  casadi_error("Constant not found");
1286  return -1;
1287  }
1288  }
std::multimap< size_t, size_t > added_char_constants_

References added_char_constants_, char_constants_, equal(), and hash().

◆ get_constant() [3/4]

casadi_int casadi::CodeGenerator::get_constant ( const std::vector< double > &  v,
bool  allow_adding = false 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_rx

Definition at line 1222 of file code_generator.cpp.

1222  {
1223  // Hash the vector
1224  size_t h = hash(v);
1225 
1226  // Try to locate it in already added constants
1227  auto eq = added_double_constants_.equal_range(h);
1228  for (auto i=eq.first; i!=eq.second; ++i) {
1229  if (equal(v, double_constants_[i->second])) return i->second;
1230  }
1231 
1232  if (allow_adding) {
1233  // Add to constants
1234  casadi_int ind = double_constants_.size();
1235  double_constants_.push_back(v);
1236  added_double_constants_.insert(std::make_pair(h, ind));
1237  return ind;
1238  } else {
1239  casadi_error("Constant not found");
1240  return -1;
1241  }
1242  }
std::multimap< size_t, size_t > added_double_constants_

References added_double_constants_, double_constants_, equal(), and hash().

Referenced by add_sparsity(), constant(), and get_sparsity().

◆ get_constant() [4/4]

casadi_int casadi::CodeGenerator::get_constant ( const std::vector< std::string > &  v,
bool  allow_adding = false 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_27z

Definition at line 1290 of file code_generator.cpp.

1290  {
1291  // Hash the vector
1292  size_t h = hash(v);
1293 
1294  // Try to locate it in already added constants
1295  std::pair<std::multimap<size_t, size_t>::iterator, std::multimap<size_t, size_t>::iterator> eq =
1296  added_string_constants_.equal_range(h);
1297  for (std::multimap<size_t, size_t>::iterator i=eq.first; i!=eq.second; ++i) {
1298  if (equal(v, string_constants_[i->second])) return i->second;
1299  }
1300 
1301  if (allow_adding) {
1302  // Add to constants
1303  casadi_int ind = string_constants_.size();
1304  string_constants_.push_back(v);
1305  added_string_constants_.insert(std::pair<size_t, size_t>(h, ind));
1306  return ind;
1307  } else {
1308  casadi_error("Constant not found");
1309  return -1;
1310  }
1311  }
std::multimap< size_t, size_t > added_string_constants_

References added_string_constants_, equal(), hash(), and string_constants_.

◆ get_sparsity()

casadi_int casadi::CodeGenerator::get_sparsity ( const Sparsity sp) const

Extra doc: https://github.com/casadi/casadi/wiki/L_rw

Definition at line 1186 of file code_generator.cpp.

1186  {
1187  return const_cast<CodeGenerator&>(*this).get_constant(sp, false);
1188  }
CodeGenerator(const std::string &name, const Dict &opts=Dict())
Constructor.

References get_constant().

◆ hash() [1/4]

size_t casadi::CodeGenerator::hash ( const std::vector< casadi_int > &  v)
static

Definition at line 1204 of file code_generator.cpp.

1204  {
1205  size_t seed=0;
1206  hash_combine(seed, v);
1207  return seed;
1208  }
void hash_combine(std::size_t &seed, T v)
Generate a hash value incrementally (function taken from boost)
Definition: sparsity.hpp:1207

References casadi::hash_combine().

◆ hash() [2/4]

size_t casadi::CodeGenerator::hash ( const std::vector< char > &  v)
static

Definition at line 1210 of file code_generator.cpp.

1210  {
1211  size_t seed=0;
1212  hash_combine(seed, v);
1213  return seed;
1214  }

References casadi::hash_combine().

◆ hash() [3/4]

size_t casadi::CodeGenerator::hash ( const std::vector< double > &  v)
static

Definition at line 1190 of file code_generator.cpp.

1190  {
1191  // Calculate a hash value for the vector
1192  std::size_t seed=0;
1193  if (!v.empty()) {
1194  casadi_assert_dev(sizeof(double) % sizeof(size_t)==0);
1195  const casadi_int int_len = v.size()*(sizeof(double)/sizeof(size_t));
1196  const size_t* int_v = reinterpret_cast<const size_t*>(&v.front());
1197  for (size_t i=0; i<int_len; ++i) {
1198  hash_combine(seed, int_v[i]);
1199  }
1200  }
1201  return seed;
1202  }

References casadi::hash_combine().

Referenced by get_constant().

◆ hash() [4/4]

size_t casadi::CodeGenerator::hash ( const std::vector< std::string > &  v)
static

Definition at line 1216 of file code_generator.cpp.

1216  {
1217  size_t seed=0;
1218  hash_combine(seed, v);
1219  return seed;
1220  }

References casadi::hash_combine().

◆ indent()

void casadi::CodeGenerator::indent ( )
inline

Extra doc: https://github.com/casadi/casadi/wiki/L_sg

Definition at line 254 of file code_generator.hpp.

254 {current_indent_++;}

Referenced by print_formatted().

◆ init_local()

void casadi::CodeGenerator::init_local ( const std::string &  name,
const std::string &  def 
)

◆ initializer()

template<typename T >
std::string casadi::CodeGenerator::initializer ( const std::vector< T > &  v)
inline

Extra doc: https://github.com/casadi/casadi/wiki/L_sk

Definition at line 283 of file code_generator.hpp.

283  {
284  std::stringstream s;
285  if (v.size() > max_initializer_elements_per_line) {
286  s << "\n ";
287  }
288 
289  s << "{";
290  for (casadi_int i = 0; i < v.size(); ++i) {
291  if (i != 0) {
294  s << ",\n ";
295  } else {
296  s << ", ";
297  }
298  }
299  s << constant(v[i]);
300  }
301  s << "}";
302  return s.str();
303  }

Referenced by dump(), and print_vector().

◆ interpn()

std::string casadi::CodeGenerator::interpn ( const std::string &  res,
casadi_int  ndim,
const std::string &  grid,
const std::string &  offset,
const std::string &  values,
const std::string &  x,
const std::string &  lookup_mode,
casadi_int  m,
const std::string &  iw,
const std::string &  w 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sw

Definition at line 2017 of file code_generator.cpp.

2022  {
2024  std::stringstream s;
2025  s << "casadi_interpn(" << res << ", " << ndim << ", " << grid << ", " << offset << ", "
2026  << values << ", " << x << ", " << lookup_mode << ", " << m << ", " << iw << ", " << w << ");";
2027  return s.str();
2028  }

References add_auxiliary(), AUX_INTERPN, and res().

Referenced by casadi::LinearInterpolant::codegen_body().

◆ interpn_grad()

std::string casadi::CodeGenerator::interpn_grad ( const std::string &  grad,
casadi_int  ndim,
const std::string &  grid,
const std::string &  offset,
const std::string &  values,
const std::string &  x,
const std::string &  lookup_mode,
casadi_int  m,
const std::string &  iw,
const std::string &  w 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sx

Definition at line 2030 of file code_generator.cpp.

2034  {
2036  std::stringstream s;
2037  s << "casadi_interpn_grad(" << grad << ", " << ndim << ", " << grid << ", " << offset << ", "
2038  << values << ", " << x << ", " << lookup_mode << "," << m << ", " << iw << ", " << w << ");";
2039  return s.str();
2040  }

References add_auxiliary(), and AUX_INTERPN_GRAD.

Referenced by casadi::LinearInterpolantJac::codegen_body().

◆ lb_eig()

std::string casadi::CodeGenerator::lb_eig ( const Sparsity sp_h,
const std::string &  h 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_ti

Definition at line 2581 of file code_generator.cpp.

2582  {
2584  return "casadi_lb_eig(" + sparsity(sp_h) + ", " + h + ")";
2585  }

References add_auxiliary(), AUX_REGULARIZE, and sparsity().

◆ ldl()

std::string casadi::CodeGenerator::ldl ( const std::string &  sp_a,
const std::string &  a,
const std::string &  sp_lt,
const std::string &  lt,
const std::string &  d,
const std::string &  p,
const std::string &  w 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t2

Definition at line 2473 of file code_generator.cpp.

2476  {
2478  return "casadi_ldl(" + sp_a + ", " + a + ", " + sp_lt + ", " + lt + ", "
2479  + d + ", " + p + ", " + w + ");";
2480  }

References add_auxiliary(), and AUX_LDL.

Referenced by casadi::LinsolLdl::generate().

◆ ldl_solve()

std::string casadi::CodeGenerator::ldl_solve ( const std::string &  x,
casadi_int  nrhs,
const std::string &  sp_lt,
const std::string &  lt,
const std::string &  d,
const std::string &  p,
const std::string &  w 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t3

Definition at line 2482 of file code_generator.cpp.

2485  {
2487  return "casadi_ldl_solve(" + x + ", " + str(nrhs) + ", " + sp_lt + ", "
2488  + lt + ", " + d + ", " + p + ", " + w + ");";
2489  }

References add_auxiliary(), AUX_LDL, and casadi::str().

Referenced by casadi::LinsolLdl::generate().

◆ local()

void casadi::CodeGenerator::local ( const std::string &  name,
const std::string &  type,
const std::string &  ref = "" 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sb

Definition at line 2281 of file code_generator.cpp.

2282  {
2283  // Check if the variable already exists
2284  auto it = local_variables_.find(name);
2285  if (it==local_variables_.end()) {
2286  // Add it
2287  local_variables_[name] = std::make_pair(type, ref);
2288  } else {
2289  // Consistency check
2290  casadi_assert(it->second.first==type, "Type mismatch for " + name);
2291  casadi_assert(it->second.second==ref, "Type mismatch for " + name);
2292  }
2293  }
std::map< std::string, std::pair< std::string, std::string > > local_variables_

References local_variables_, and name.

Referenced by casadi::FiniteDiff::codegen_body(), casadi::JitFunction::codegen_body(), casadi::Map::codegen_body(), casadi::MapSum::codegen_body(), casadi::MXFunction::codegen_body(), casadi::Switch::codegen_body(), casadi::ClarabelInterface::codegen_body(), casadi::DaqpInterface::codegen_body(), casadi::FatropInterface::codegen_body(), casadi::HighsInterface::codegen_body(), casadi::HpipmInterface::codegen_body(), casadi::IpoptInterface::codegen_body(), casadi::MadnlpInterface::codegen_body(), casadi::OsqpInterface::codegen_body(), casadi::FastNewton::codegen_body(), casadi::Feasiblesqpmethod::codegen_body(), casadi::Qrqp::codegen_body(), casadi::Sqpmethod::codegen_body(), casadi::OracleFunction::codegen_body_enter(), casadi::Nlpsol::codegen_body_enter(), casadi::Nlpsol::codegen_body_exit(), casadi::Feasiblesqpmethod::codegen_feasibility_iterations(), casadi::OsqpInterface::codegen_init_mem(), casadi::codegen_local(), casadi::codegen_unpack_block(), constant_copy(), casadi::Convexify::generate(), casadi::BinaryMX< ScX, ScY >::generate(), casadi::Call::generate(), casadi::Find::generate(), casadi::Low::generate(), casadi::Concat::generate(), casadi::Einstein::generate(), casadi::GetNonzerosVector::generate(), casadi::GetNonzerosSlice::generate(), casadi::GetNonzerosSlice2::generate(), casadi::GetNonzerosParamVector::generate(), casadi::GetNonzerosSliceParam::generate(), casadi::GetNonzerosParamSlice::generate(), casadi::GetNonzerosParamParam::generate(), casadi::Monitor::generate(), casadi::DenseMultiplication::generate(), casadi::HorzRepmat::generate(), casadi::HorzRepsum::generate(), casadi::SetNonzerosVector< Add >::generate(), casadi::SetNonzerosSlice< Add >::generate(), casadi::SetNonzerosSlice2< Add >::generate(), casadi::SetNonzerosParamVector< Add >::generate(), casadi::SetNonzerosParamSlice< Add >::generate(), casadi::SetNonzerosSliceParam< Add >::generate(), casadi::SetNonzerosParamParam< Add >::generate(), casadi::LinsolCall< Tr >::generate(), casadi::DenseTranspose::generate(), casadi::UnaryMX::generate(), operator()(), casadi::Conic::qp_codegen_body(), and sx_work().

◆ logsumexp()

std::string casadi::CodeGenerator::logsumexp ( const std::string &  A,
casadi_int  n 
)

\brie LogSumExp

Definition at line 2222 of file code_generator.cpp.

2222  {
2224  std::stringstream s;
2225  s << "casadi_logsumexp(" << A << ", " << n << ");";
2226  return s.str();
2227  }

References add_auxiliary(), and AUX_LOGSUMEXP.

Referenced by casadi::LogSumExp::generate().

◆ low()

std::string casadi::CodeGenerator::low ( const std::string &  x,
const std::string &  grid,
casadi_int  ng,
casadi_int  lookup_mode 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tl

Definition at line 2600 of file code_generator.cpp.

2601  {
2603  return "casadi_low(" + x + ", " + grid + ", " + str(ng) + ", " + str(lookup_mode) + ");";
2604  }

References add_auxiliary(), AUX_LOW, and casadi::str().

Referenced by casadi::Low::generate().

◆ lsqr_solve()

std::string casadi::CodeGenerator::lsqr_solve ( const std::string &  A,
const std::string &  x,
casadi_int  nrhs,
bool  tr,
const std::string &  sp,
const std::string &  w 
)

\brief LSQR solve

Extra doc: https://github.com/casadi/casadi/wiki/L_t1

Definition at line 2465 of file code_generator.cpp.

2467  {
2469  return "casadi_lsqr_solve(" + A + ", " + x + ", " + str(nrhs) + ", "
2470  + (tr ? "1" : "0") + ", " + sp + ", " + w + ");";
2471  }

References add_auxiliary(), AUX_LSQR, and casadi::str().

Referenced by casadi::Lsqr::generate().

◆ masked_norm_inf()

std::string casadi::CodeGenerator::masked_norm_inf ( casadi_int  n,
const std::string &  x,
const std::string &  mask 
)

Definition at line 2173 of file code_generator.cpp.

2174  {
2176  return "casadi_masked_norm_inf(" + str(n) + ", " + x + ", " + mask + ")";
2177  }

References add_auxiliary(), AUX_MASKED_NORM_INF, and casadi::str().

Referenced by casadi::Feasiblesqpmethod::codegen_body(), casadi::Feasiblesqpmethod::codegen_feasibility_iterations(), and casadi::Feasiblesqpmethod::codegen_tr_update().

◆ max()

std::string casadi::CodeGenerator::max ( const std::string &  x,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tc

Definition at line 2527 of file code_generator.cpp.

2528  {
2530  return "casadi_max(" + x + ", " + y + ")";
2531  }

References add_auxiliary(), and AUX_MAX.

◆ max_viol()

std::string casadi::CodeGenerator::max_viol ( casadi_int  n,
const std::string &  x,
const std::string &  lb,
const std::string &  ub 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tf

Definition at line 2551 of file code_generator.cpp.

2552  {
2554  return "casadi_max_viol(" + str(n) + ", " + x+ ", " + lb + ", " + ub + ")";
2555  }

References add_auxiliary(), AUX_MAX_VIOL, and casadi::str().

Referenced by casadi::Feasiblesqpmethod::codegen_body(), casadi::Sqpmethod::codegen_body(), and casadi::Feasiblesqpmethod::codegen_feasibility_iterations().

◆ mem()

std::string casadi::CodeGenerator::mem ( const Function f)

Extra doc: https://github.com/casadi/casadi/wiki/L_ty

Definition at line 1975 of file code_generator.cpp.

1975  {
1976  std::string name = f->codegen_name(*this, false);
1977  std::string mem_array = shorthand(name + "_mem");
1978  return mem_array+"[mem]";
1979  }

References casadi::FunctionInternal::codegen_name(), name, and shorthand().

Referenced by operator()().

◆ min()

std::string casadi::CodeGenerator::min ( const std::string &  x,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_td

Definition at line 2533 of file code_generator.cpp.

2534  {
2536  return "casadi_min(" + x + ", " + y + ")";
2537  }

References add_auxiliary(), and AUX_MIN.

Referenced by clip_max(), and clip_min().

◆ mmax()

std::string casadi::CodeGenerator::mmax ( const std::string &  x,
casadi_int  n,
bool  is_dense 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t6

Definition at line 2539 of file code_generator.cpp.

2540  {
2542  return "casadi_mmax(" + x + ", " + str(n) + ", " + str(casadi_int(is_dense)) + ")";
2543  }

References add_auxiliary(), AUX_MMAX, and casadi::str().

Referenced by casadi::MMax::generate().

◆ mmin()

std::string casadi::CodeGenerator::mmin ( const std::string &  x,
casadi_int  n,
bool  is_dense 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t7

Definition at line 2545 of file code_generator.cpp.

2546  {
2548  return "casadi_mmin(" + x + ", " + str(n) + ", " + str(casadi_int(is_dense)) + ")";
2549  }

References add_auxiliary(), AUX_MMIN, and casadi::str().

Referenced by casadi::MMin::generate().

◆ mtimes()

std::string casadi::CodeGenerator::mtimes ( const std::string &  x,
const Sparsity sp_x,
const std::string &  y,
const Sparsity sp_y,
const std::string &  z,
const Sparsity sp_z,
const std::string &  w,
bool  tr 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sr

Definition at line 2198 of file code_generator.cpp.

2201  {
2203  return "casadi_mtimes(" + x + ", " + sparsity(sp_x) + ", " + y + ", " + sparsity(sp_y) + ", "
2204  + z + ", " + sparsity(sp_z) + ", " + w + ", " + (tr ? "1" : "0") + ");";
2205  }

References add_auxiliary(), AUX_MTIMES, and sparsity().

Referenced by casadi::Multiplication::generate().

◆ mv() [1/2]

std::string casadi::CodeGenerator::mv ( const std::string &  x,
casadi_int  nrow_x,
casadi_int  ncol_x,
const std::string &  y,
const std::string &  z,
bool  tr 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_so

Definition at line 2191 of file code_generator.cpp.

2192  {
2194  return "casadi_mv_dense(" + x + ", " + str(nrow_x) + ", " + str(ncol_x) + ", "
2195  + y + ", " + z + ", " + (tr ? "1" : "0") + ");";
2196  }

References add_auxiliary(), AUX_MV_DENSE, and casadi::str().

◆ mv() [2/2]

std::string casadi::CodeGenerator::mv ( const std::string &  x,
const Sparsity sp_x,
const std::string &  y,
const std::string &  z,
bool  tr 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sn

Definition at line 2184 of file code_generator.cpp.

2185  {
2187  return "casadi_mv(" + x + ", " + sparsity(sp_x) + ", " + y + ", "
2188  + z + ", " + (tr ? "1" : "0") + ");";
2189  }

References add_auxiliary(), AUX_MV, and sparsity().

Referenced by casadi::Feasiblesqpmethod::codegen_body(), casadi::Sqpmethod::codegen_body(), casadi::Feasiblesqpmethod::codegen_feasibility_iterations(), and casadi::Sqpmethod::codegen_solve_elastic_mode().

◆ norm_1()

std::string casadi::CodeGenerator::norm_1 ( casadi_int  n,
const std::string &  x 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_2br

Definition at line 2569 of file code_generator.cpp.

2570  {
2572  return "casadi_norm_1(" + str(n) + ", " + x + ")";
2573  }

References add_auxiliary(), AUX_NORM_1, and casadi::str().

Referenced by casadi::Norm1::generate().

◆ norm_2()

std::string casadi::CodeGenerator::norm_2 ( casadi_int  n,
const std::string &  x 
)
     Extra doc: https://github.com/casadi/casadi/wiki/L_256 

Definition at line 2575 of file code_generator.cpp.

2576  {
2578  return "casadi_norm_2(" + str(n) + ", " + x + ")";
2579  }

References add_auxiliary(), AUX_NORM_2, and casadi::str().

Referenced by casadi::Feasiblesqpmethod::codegen_feasibility_iterations(), and casadi::NormF::generate().

◆ norm_inf()

std::string casadi::CodeGenerator::norm_inf ( casadi_int  n,
const std::string &  x 
)

◆ ones()

std::string casadi::CodeGenerator::ones ( casadi_int  sz)

Definition at line 1330 of file code_generator.cpp.

1330  {
1331  sz_ones_ = std::max(sz_ones_, sz);
1332  return shorthand("ones");
1333  }

References shorthand(), and sz_ones_.

◆ operator()()

std::string casadi::CodeGenerator::operator() ( const Function f,
const std::string &  arg,
const std::string &  res,
const std::string &  iw,
const std::string &  w,
const std::string &  failure_ret = "1" 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_s6

Definition at line 1139 of file code_generator.cpp.

1142  {
1143  std::string name = add_dependency(f);
1144  bool needs_mem = !f->codegen_mem_type().empty();
1145  if (needs_mem) {
1146  std::string mem = "mid";
1147  local("flag", "int");
1148  local(mem, "int");
1149  *this << mem << " = " << name << "_checkout();\n";
1150  *this << "if (" << mem << "<0) return " << failure_ret << ";\n";
1151  *this << "flag = " + name + "(" + arg + ", " + res + ", "
1152  + iw + ", " + w + ", " << mem << ");\n";
1153  *this << name << "_release(" << mem << ");\n";
1154  return "flag";
1155  } else {
1156  return name + "(" + arg + ", " + res + ", "
1157  + iw + ", " + w + ", 0)";
1158  }
1159  }
std::string mem(const Function &f)
Access thread-local memory.

References add_dependency(), arg(), casadi::FunctionInternal::codegen_mem_type(), local(), mem(), name, and res().

◆ operator<<() [1/2]

CodeGenerator & casadi::CodeGenerator::operator<< ( const std::string &  s)

Extra doc: https://github.com/casadi/casadi/wiki/L_s7

Definition at line 2255 of file code_generator.cpp.

2255  {
2256  // Loop over newline characters
2257  size_t off=0;
2258  while (true) {
2259  size_t pos = s.find('\n', off);
2260  if (pos==std::string::npos) {
2261  // No more newline characters
2262  print_formatted(s.substr(off));
2263  break;
2264  } else {
2265  // Ends with newline
2266  print_formatted(s.substr(off, pos-off));
2267  this->buffer << '\n';
2268  newline_ = true;
2269  off = pos+1;
2270  }
2271  }
2272 
2273  return *this;
2274  }
void print_formatted(const std::string &s)
Print without newline characters.

References buffer, newline_, and print_formatted().

◆ operator<<() [2/2]

template<typename T >
CodeGenerator& casadi::CodeGenerator::operator<< ( s)
inline

Extra doc: https://github.com/casadi/casadi/wiki/L_s9

Definition at line 215 of file code_generator.hpp.

215  {
216  std::stringstream ss;
217  ss << s;
218  return (*this) << ss.str();
219  }

◆ pool_double()

std::string casadi::CodeGenerator::pool_double ( const std::string &  name) const

Extra doc: https://github.com/casadi/casadi/wiki/L_2ax

Definition at line 836 of file code_generator.cpp.

836  {
837  auto it = pool_double_.find(name);
838  casadi_assert(it!=pool_double_.end(), "Not defined.");
839  return "casadi_pd" + str(it->second);
840  }

References name, pool_double_, and casadi::str().

Referenced by casadi::ConstantPool::generate().

◆ print_formatted()

void casadi::CodeGenerator::print_formatted ( const std::string &  s)

Extra doc: https://github.com/casadi/casadi/wiki/L_s8

Definition at line 2229 of file code_generator.cpp.

2229  {
2230  // Quick return if empty
2231  if (s.empty()) return;
2232 
2233  // If new line, add indentation
2234  if (newline_) {
2235  casadi_int shift = s.front()=='}' ? -1 : 0;
2236  casadi_assert_dev(current_indent_+shift>=0);
2237  this->buffer << std::string(indent_*(current_indent_+shift), ' ');
2238  newline_ = false;
2239  }
2240 
2241  // Print to body
2242  this->buffer << s;
2243 
2244  // Brackets change indentation for next row
2245  // NOTE(@jaeandersson): Should ignore strings, comments
2246  for (char c : s) {
2247  if (c=='{') {
2248  indent();
2249  } else if (c=='}') {
2250  unindent();
2251  }
2252  }
2253  }
void indent()
Increase indentation.
void unindent()
Decrease indentation.

References buffer, current_indent_, indent(), indent_, newline_, and unindent().

Referenced by operator<<().

◆ print_op() [1/2]

std::string casadi::CodeGenerator::print_op ( casadi_int  op,
const std::string &  a0 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u6

Definition at line 1060 of file code_generator.cpp.

1060  {
1061  switch (op) {
1062  case OP_FABS:
1064  return "casadi_fabs("+a0+")";
1065  case OP_SQ:
1067  return "casadi_sq("+a0+")";
1068  case OP_SIGN:
1070  return "casadi_sign("+a0+")";
1071  case OP_LOG1P:
1073  return "casadi_log1p("+a0+")";
1074  case OP_EXPM1:
1076  return "casadi_expm1("+a0+")";
1077  default:
1078  return casadi_math<double>::print(op, a0);
1079  }
1080  }
@ OP_SIGN
Definition: calculus.hpp:71
@ OP_LOG1P
Definition: calculus.hpp:202
@ OP_EXPM1
Definition: calculus.hpp:204
@ OP_FABS
Definition: calculus.hpp:71
@ OP_SQ
Definition: calculus.hpp:67
static std::string print(unsigned char op, const std::string &x, const std::string &y)
Print.
Definition: calculus.hpp:1641

References add_auxiliary(), AUX_EXPM1, AUX_FABS, AUX_LOG1P, AUX_SIGN, AUX_SQ, casadi::OP_EXPM1, casadi::OP_FABS, casadi::OP_LOG1P, casadi::OP_SIGN, casadi::OP_SQ, and casadi::casadi_math< T >::print().

Referenced by casadi::SXFunction::codegen_body(), casadi::BinaryMX< ScX, ScY >::generate(), and casadi::UnaryMX::generate().

◆ print_op() [2/2]

std::string casadi::CodeGenerator::print_op ( casadi_int  op,
const std::string &  a0,
const std::string &  a1 
)

Definition at line 1081 of file code_generator.cpp.

1081  {
1082  switch (op) {
1083  case OP_FMIN:
1085  return "casadi_fmin("+a0+","+a1+")";
1086  case OP_FMAX:
1088  return "casadi_fmax("+a0+","+a1+")";
1089  case OP_HYPOT:
1091  return "casadi_hypot("+a0+","+a1+")";
1092  case OP_PRINTME:
1094  return "casadi_printme("+a0+","+a1+")";
1095  default:
1096  return casadi_math<double>::print(op, a0, a1);
1097  }
1098  }
@ OP_FMAX
Definition: calculus.hpp:72
@ OP_PRINTME
Definition: calculus.hpp:190
@ OP_HYPOT
Definition: calculus.hpp:206
@ OP_FMIN
Definition: calculus.hpp:72

References add_auxiliary(), AUX_FMAX, AUX_FMIN, AUX_HYPOT, AUX_PRINTME, casadi::OP_FMAX, casadi::OP_FMIN, casadi::OP_HYPOT, casadi::OP_PRINTME, and casadi::casadi_math< T >::print().

◆ print_vector() [1/4]

void casadi::CodeGenerator::print_vector ( std::ostream &  s,
const std::string &  name,
const std::vector< casadi_int > &  v 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tr

Definition at line 1040 of file code_generator.cpp.

1041  {
1042  s << array("static const casadi_int", name, v.size(), initializer(v));
1043  }
static std::string array(const std::string &type, const std::string &name, casadi_int len, const std::string &def=std::string())

References array(), initializer(), and name.

Referenced by dump().

◆ print_vector() [2/4]

void casadi::CodeGenerator::print_vector ( std::ostream &  s,
const std::string &  name,
const std::vector< char > &  v 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_27n

Definition at line 1045 of file code_generator.cpp.

1046  {
1047  s << array("static const char", name, v.size(), initializer(v));
1048  }

References array(), initializer(), and name.

◆ print_vector() [3/4]

void casadi::CodeGenerator::print_vector ( std::ostream &  s,
const std::string &  name,
const std::vector< double > &  v 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_ts

Definition at line 1050 of file code_generator.cpp.

1051  {
1052  s << array("static const casadi_real", name, v.size(), initializer(v));
1053  }

References array(), initializer(), and name.

◆ print_vector() [4/4]

void casadi::CodeGenerator::print_vector ( std::ostream &  s,
const std::string &  name,
const std::vector< std::string > &  v 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_281

Definition at line 1055 of file code_generator.cpp.

1056  {
1057  s << array("static const char*", name, v.size(), initializer(v));
1058  }

References array(), initializer(), and name.

◆ printf() [1/4]

std::string casadi::CodeGenerator::printf ( const std::string &  str,
const std::string &  arg1 
)

Definition at line 2120 of file code_generator.cpp.

2120  {
2121  std::vector<std::string> arg;
2122  arg.push_back(arg1);
2123  return printf(str, arg);
2124  }
std::string printf(const std::string &str, const std::vector< std::string > &arg=std::vector< std::string >())
Printf.

References arg(), printf(), and casadi::str().

◆ printf() [2/4]

std::string casadi::CodeGenerator::printf ( const std::string &  str,
const std::string &  arg1,
const std::string &  arg2 
)

Definition at line 2126 of file code_generator.cpp.

2127  {
2128  std::vector<std::string> arg;
2129  arg.push_back(arg1);
2130  arg.push_back(arg2);
2131  return printf(str, arg);
2132  }

References arg(), printf(), and casadi::str().

◆ printf() [3/4]

std::string casadi::CodeGenerator::printf ( const std::string &  str,
const std::string &  arg1,
const std::string &  arg2,
const std::string &  arg3 
)

Definition at line 2134 of file code_generator.cpp.

2135  {
2136  std::vector<std::string> arg;
2137  arg.push_back(arg1);
2138  arg.push_back(arg2);
2139  arg.push_back(arg3);
2140  return printf(str, arg);
2141  }

References arg(), printf(), and casadi::str().

◆ printf() [4/4]

std::string casadi::CodeGenerator::printf ( const std::string &  str,
const std::vector< std::string > &  arg = std::vector<std::string>() 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u5

Definition at line 2111 of file code_generator.cpp.

2111  {
2113  std::stringstream s;
2114  s << "CASADI_PRINTF(\"" << str << "\"";
2115  for (casadi_int i=0; i<arg.size(); ++i) s << ", " << arg[i];
2116  s << ");";
2117  return s.str();
2118  }

References add_auxiliary(), arg(), AUX_PRINTF, and casadi::str().

Referenced by casadi::Feasiblesqpmethod::codegen_body(), casadi::Qrqp::codegen_body(), casadi::FunctionInternal::codegen_meta(), casadi::Monitor::generate(), and printf().

◆ project()

std::string casadi::CodeGenerator::project ( const std::string &  arg,
const Sparsity sp_arg,
const std::string &  res,
const Sparsity sp_res,
const std::string &  w 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tz

Definition at line 2064 of file code_generator.cpp.

2066  {
2067  // If sparsity match, simple copy
2068  if (sp_arg==sp_res) return copy(arg, sp_arg.nnz(), res);
2069 
2070  // Create call
2072  std::stringstream s;
2073  s << "casadi_project(" << arg << ", " << sparsity(sp_arg) << ", " << res << ", "
2074  << sparsity(sp_res) << ", " << w << ");";
2075  return s.str();
2076  }

References add_auxiliary(), arg(), AUX_PROJECT, copy(), casadi::Sparsity::nnz(), res(), and sparsity().

Referenced by casadi::Switch::codegen_body(), and casadi::Project::generate().

◆ qr()

std::string casadi::CodeGenerator::qr ( const std::string &  sp,
const std::string &  A,
const std::string &  w,
const std::string &  sp_v,
const std::string &  v,
const std::string &  sp_r,
const std::string &  r,
const std::string &  beta,
const std::string &  prinv,
const std::string &  pc 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sz

Definition at line 2442 of file code_generator.cpp.

2446  {
2448  return "casadi_qr(" + sp + ", " + A + ", " + w + ", "
2449  + sp_v + ", " + v + ", " + sp_r + ", " + r + ", "
2450  + beta + ", " + prinv + ", " + pc + ");";
2451  }

References add_auxiliary(), and AUX_QR.

Referenced by casadi::LinsolQr::generate().

◆ qr_solve()

std::string casadi::CodeGenerator::qr_solve ( const std::string &  x,
casadi_int  nrhs,
bool  tr,
const std::string &  sp_v,
const std::string &  v,
const std::string &  sp_r,
const std::string &  r,
const std::string &  beta,
const std::string &  prinv,
const std::string &  pc,
const std::string &  w 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t0

Definition at line 2453 of file code_generator.cpp.

2458  {
2460  return "casadi_qr_solve(" + x + ", " + str(nrhs) + ", " + (tr ? "1" : "0") + ", "
2461  + sp_v + ", " + v + ", " + sp_r + ", " + r + ", "
2462  + beta + ", " + prinv + ", " + pc + ", " + w + ");";
2463  }

References add_auxiliary(), AUX_QR, and casadi::str().

Referenced by casadi::LinsolQr::generate().

◆ rank1()

std::string casadi::CodeGenerator::rank1 ( const std::string &  A,
const Sparsity sp_A,
const std::string &  alpha,
const std::string &  x,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sv

Definition at line 2007 of file code_generator.cpp.

2009  {
2011  std::stringstream s;
2012  s << "casadi_rank1(" << A << ", " << sparsity(sp_A) << ", "
2013  << alpha << ", " << x << ", " << y << ");";
2014  return s.str();
2015  }

References add_auxiliary(), AUX_RANK1, and sparsity().

Referenced by casadi::Rank1::generate().

◆ regularize()

std::string casadi::CodeGenerator::regularize ( const Sparsity sp_h,
const std::string &  h,
const std::string &  reg 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tj

Definition at line 2587 of file code_generator.cpp.

2588  {
2590  return "casadi_regularize(" + sparsity(sp_h) + ", " + h + ", " + reg + ");";
2591  }

References add_auxiliary(), AUX_REGULARIZE, and sparsity().

◆ res()

std::string casadi::CodeGenerator::res ( casadi_int  i) const

◆ reserve_work()

void casadi::CodeGenerator::reserve_work ( casadi_int  n)

Extra doc: https://github.com/casadi/casadi/wiki/L_2ay

Definition at line 1010 of file code_generator.cpp.

1010  {
1011  if (n == 0) {
1012  padding_length_ = 1;
1013  } else {
1014  // Zero based counting. The 10th entry is to be rendered as '9'
1015  padding_length_ = str(n-1).length();
1016  }
1017  }

References padding_length_, and casadi::str().

Referenced by casadi::MXFunction::codegen_body(), and casadi::SXFunction::codegen_body().

◆ rom_double()

std::string casadi::CodeGenerator::rom_double ( const void *  id) const

Extra doc: https://github.com/casadi/casadi/wiki/L_s3

Definition at line 803 of file code_generator.cpp.

803  {
804  auto it = file_scope_double_.find(id);
805  casadi_assert(it!=file_scope_double_.end(), "Not defined.");
806  casadi_int size = std::distance(file_scope_double_.begin(), it);
807  return "casadi_rd" + str(size);
808  }

References file_scope_double_, and casadi::str().

Referenced by casadi::ConstantFile::codegen_incref(), and casadi::ConstantFile::generate().

◆ rom_integer()

std::string casadi::CodeGenerator::rom_integer ( const void *  id) const

Extra doc: https://github.com/casadi/casadi/wiki/L_s5

Definition at line 817 of file code_generator.cpp.

817  {
818  auto it = file_scope_double_.find(id);
819  casadi_assert(it!=file_scope_double_.end(), "Not defined.");
820  casadi_int size = std::distance(file_scope_double_.begin(), it);
821  return "casadi_ri" + str(size);
822  }

References file_scope_double_, and casadi::str().

◆ sanitize_source()

std::string casadi::CodeGenerator::sanitize_source ( const std::string &  src,
const std::vector< std::string > &  inst,
bool  add_shorthand = true 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sl

Definition at line 2316 of file code_generator.cpp.

2318  {
2319  // Create suffix if templates type are not all "casadi_real"
2320  std::string suffix;
2321  for (const std::string& s : inst) {
2322  if (s!="casadi_real") {
2323  for (const std::string& s : inst) suffix += "_" + s;
2324  break;
2325  }
2326  }
2327 
2328  // Construct map of name replacements
2329  std::vector<std::pair<std::string, std::string> > rep;
2330  for (casadi_int i=0; i<inst.size(); ++i) {
2331  rep.push_back(std::make_pair("T" + str(i+1), inst[i]));
2332  }
2333 
2334  // Return object
2335  std::stringstream ret;
2336  // Process C++ source
2337  std::string line;
2338  std::istringstream stream(src);
2339  while (std::getline(stream, line)) {
2340  size_t n1, n2;
2341 
2342  // C++ template declarations are ignored
2343  if (line.find("template")==0) continue;
2344 
2345  // Macro definitions are ignored
2346  if (line.find("#define")==0) continue;
2347  if (line.find("#undef")==0) continue;
2348 
2349  // Inline declaration
2350  if (line == "inline") continue;
2351 
2352  // If line starts with "// SYMBOL", add shorthand
2353  if (line.find("// SYMBOL") != std::string::npos) {
2354  n1 = line.find("\"");
2355  n2 = line.find("\"", n1+1);
2356  std::string sym = line.substr(n1+1, n2-n1-1);
2357  if (add_shorthand) shorthand(sym + suffix);
2358  if (!suffix.empty()) {
2359  rep.push_back(std::make_pair(sym, sym + suffix));
2360  }
2361  continue;
2362  }
2363 
2364  // If line starts with "// C-REPLACE", add to list of replacements
2365  if (line.find("// C-REPLACE") != std::string::npos) {
2366  // Get C++ string
2367  n1 = line.find("\"");
2368  n2 = line.find("\"", n1+1);
2369  std::string key = line.substr(n1+1, n2-n1-1);
2370  // Get C string
2371  n1 = line.find("\"", n2+1);
2372  n2 = line.find("\"", n1+1);
2373  std::string sub = line.substr(n1+1, n2-n1-1);
2374  // Add to replacements
2375  rep.push_back(std::make_pair(key, sub));
2376  continue;
2377  }
2378 
2379  // If line starts with "// C-VERBOSE", skip the next line
2380  if (!verbose_runtime && line.find("// C-VERBOSE") != std::string::npos) {
2381  // Ignore next line
2382  std::getline(stream, line);
2383  continue;
2384  }
2385 
2386  // Ignore other C++ style comment
2387  if ((n1 = line.find("//")) != std::string::npos) line.erase(n1);
2388 
2389  // Remove trailing spaces
2390  if ((n1 = line.find_last_not_of(' ')) != std::string::npos) {
2391  line.erase(n1 + 1);
2392  } else {
2393  continue;
2394  }
2395 
2396  // Perform string replacements
2397  for (auto&& it = rep.rbegin(); it!=rep.rend(); ++it) {
2398  line = replace(line, it->first, it->second);
2399  }
2400 
2401  // Append to return
2402  ret << line << "\n";
2403  }
2404 
2405  // Trailing newline
2406  ret << "\n";
2407  return ret.str();
2408  }

References casadi::replace(), shorthand(), casadi::str(), suffix, and verbose_runtime.

Referenced by add_auxiliary(), casadi::ClarabelInterface::codegen_body(), casadi::DaqpInterface::codegen_body(), casadi::FatropInterface::codegen_body(), casadi::HighsInterface::codegen_body(), casadi::HpipmInterface::codegen_body(), casadi::IpoptInterface::codegen_body(), and casadi::MadnlpInterface::codegen_body().

◆ scal()

std::string casadi::CodeGenerator::scal ( casadi_int  n,
const std::string &  alpha,
const std::string &  x 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sq

Definition at line 2179 of file code_generator.cpp.

2179  {
2181  return "casadi_scal(" + str(n) + ", " + alpha + ", " + x + ");";
2182  }

References add_auxiliary(), AUX_SCAL, and casadi::str().

Referenced by casadi::Sqpmethod::codegen_body(), and casadi::Nlpsol::codegen_body_exit().

◆ scope_enter()

void casadi::CodeGenerator::scope_enter ( )

◆ scope_exit()

void casadi::CodeGenerator::scope_exit ( )

Extra doc: https://github.com/casadi/casadi/wiki/L_sd

Definition at line 208 of file code_generator.cpp.

208  {
209  // Order local variables
210  std::map<std::string, std::set<std::pair<std::string, std::string>>> local_variables_by_type;
211  for (auto&& e : local_variables_) {
212  local_variables_by_type[e.second.first].insert(std::make_pair(e.first, e.second.second));
213  }
214 
215  // Codegen local variables
216  for (auto&& e : local_variables_by_type) {
217  casadi_int cnt = 0;
218  for (auto it=e.second.begin(); it!=e.second.end(); ++it) {
219  bool split_declaration = it==e.second.begin() ||
221  if (split_declaration) {
222  if (it!=e.second.begin()) body << ";\n";
223  body << " " << e.first << " ";
224  } else {
225  body << ", ";
226  }
227  body << it->second << it->first;
228  // Insert definition, if any
229  auto k=local_default_.find(it->first);
230  if (k!=local_default_.end()) body << "=" << k->second;
231  cnt++;
232  }
233  body << ";\n";
234  }
235  }

References body, local_default_, local_variables_, and max_declarations_per_line.

Referenced by add_dependency(), casadi::FunctionInternal::codegen(), casadi::FatropInterface::codegen_declarations(), and casadi::IpoptInterface::codegen_declarations().

◆ setup_callback()

void casadi::CodeGenerator::setup_callback ( const std::string &  s,
const Function f 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_27s

Definition at line 1122 of file code_generator.cpp.

1122  {
1123  std::string name = add_dependency(f);
1124  bool needs_mem = !f->codegen_mem_type().empty();
1125  if (needs_mem) {
1126  *this << s << ".checkout = " << name << "_checkout;\n";
1127  } else {
1128  *this << s << ".checkout = 0;\n";
1129  }
1130 
1131  *this << s << ".eval = " << name << ";\n";
1132  if (needs_mem) {
1133  *this << s << ".release = " << name << "_release;\n";
1134  } else {
1135  *this << s << ".release = 0;\n";
1136  }
1137  }

References add_dependency(), casadi::FunctionInternal::codegen_mem_type(), and name.

Referenced by casadi::FatropInterface::set_fatrop_prob(), and casadi::MadnlpInterface::set_madnlp_prob().

◆ shorthand() [1/2]

std::string casadi::CodeGenerator::shorthand ( const std::string &  name) const

◆ shorthand() [2/2]

std::string casadi::CodeGenerator::shorthand ( const std::string &  name,
bool  allow_adding = true 
)

Definition at line 1170 of file code_generator.cpp.

1170  {
1171  bool added = added_shorthands_.insert(name).second;
1172  if (!allow_adding) {
1173  casadi_assert(added, "Duplicate macro: " + name);
1174  }
1175  return "casadi_" + name;
1176  }

References added_shorthands_, and name.

◆ sparsify()

std::string casadi::CodeGenerator::sparsify ( const std::string &  arg,
const std::string &  res,
const Sparsity sp_res,
bool  tr = false 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u2

Definition at line 2101 of file code_generator.cpp.

2102  {
2103  // Create call
2105  std::stringstream s;
2106  s << "casadi_sparsify(" << arg << ", " << res << ", "
2107  << sparsity(sp_res) << ", " << (tr ? 1 : 0) << ");";
2108  return s.str();
2109  }

References add_auxiliary(), arg(), AUX_SPARSIFY, res(), and sparsity().

Referenced by casadi::Sparsify::generate().

◆ sparsity()

std::string casadi::CodeGenerator::sparsity ( const Sparsity sp,
bool  canonical = true 
)

◆ sum_viol()

std::string casadi::CodeGenerator::sum_viol ( casadi_int  n,
const std::string &  x,
const std::string &  lb,
const std::string &  ub 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tg

Definition at line 2557 of file code_generator.cpp.

2558  {
2560  return "casadi_sum_viol(" + str(n) + ", " + x+ ", " + lb + ", " + ub + ")";
2561  }

References add_auxiliary(), AUX_SUM_VIOL, and casadi::str().

Referenced by casadi::Sqpmethod::codegen_body().

◆ sx_work()

std::string casadi::CodeGenerator::sx_work ( casadi_int  i)

Extra doc: https://github.com/casadi/casadi/wiki/L_se

Definition at line 2295 of file code_generator.cpp.

2295  {
2296  if (avoid_stack_) {
2297  return "w[" + str(i) + "]";
2298  } else {
2299  std::string name = "a"+format_padded(i);
2300 
2301  // Make sure work vector element has been declared
2302  local(name, "casadi_real");
2303 
2304  return name;
2305  }
2306  }
std::string format_padded(casadi_int i) const

References avoid_stack_, format_padded(), local(), name, and casadi::str().

Referenced by casadi::SXFunction::codegen_body().

◆ sz_work()

void casadi::CodeGenerator::sz_work ( size_t &  sz_arg,
size_t &  sz_res,
size_t &  sz_iw,
size_t &  sz_w 
) const

Extra doc: https://github.com/casadi/casadi/wiki/L_258

Definition at line 2628 of file code_generator.cpp.

2628  {
2629  sz_arg = sz_res = sz_iw = sz_w = 0;
2630  for (auto&& f : added_functions_) {
2631  sz_arg = std::max(sz_arg, f.f.sz_arg());
2632  sz_res = std::max(sz_res, f.f.sz_res());
2633  sz_iw = std::max(sz_iw, f.f.sz_iw());
2634  sz_w = std::max(sz_w, f.f.sz_w());
2635  }
2636  }

References added_functions_.

Referenced by casadi::DaeBuilderInternal::generate_wrapper().

◆ to_mex()

std::string casadi::CodeGenerator::to_mex ( const Sparsity sp,
const std::string &  arg 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u3

Definition at line 1851 of file code_generator.cpp.

1851  {
1853  std::stringstream s;
1854  s << "casadi_to_mex(" << sparsity(sp) << ", " << arg << ");";
1855  return s.str();
1856  }

References add_auxiliary(), arg(), AUX_TO_MEX, and sparsity().

Referenced by casadi::FunctionInternal::codegen_meta().

◆ trans()

std::string casadi::CodeGenerator::trans ( const std::string &  x,
const Sparsity sp_x,
const std::string &  y,
const Sparsity sp_y,
const std::string &  iw 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_sy

Definition at line 2042 of file code_generator.cpp.

2044  {
2046  return "casadi_trans(" + x + "," + sparsity(sp_x) + ", "
2047  + y + ", " + sparsity(sp_y) + ", " + iw + ")";
2048  }

References add_auxiliary(), AUX_TRANS, and sparsity().

Referenced by casadi::Transpose::generate().

◆ tri_project()

std::string casadi::CodeGenerator::tri_project ( const std::string &  arg,
const Sparsity sp_arg,
const std::string &  res,
bool  lower 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_u0

Definition at line 2079 of file code_generator.cpp.

2080  {
2081  // Create call
2083  std::stringstream s;
2084  s << "casadi_tri_project(" << arg << ", " << sparsity(sp_arg) << ", ";
2085  s << res << ", " << (lower ? 1: 0) << ");";
2086  return s.str();
2087  }

References add_auxiliary(), arg(), AUX_TRI_PROJECT, res(), and sparsity().

Referenced by casadi::OsqpInterface::codegen_body().

◆ trilsolve()

std::string casadi::CodeGenerator::trilsolve ( const Sparsity sp_x,
const std::string &  x,
const std::string &  y,
bool  tr,
bool  unity,
casadi_int  nrhs 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_ss

Definition at line 2207 of file code_generator.cpp.

2208  {
2210  return "casadi_trilsolve(" + sparsity(sp_x) + ", " + x + ", " + y + ", " + str(tr) + ", "
2211  + str(unity) + ", " + str(nrhs) + ");";
2212  }

References add_auxiliary(), AUX_TRILSOLVE, sparsity(), and casadi::str().

Referenced by casadi::TrilSolve< Tr >::generate(), and casadi::TrilSolveUnity< Tr >::generate().

◆ triusolve()

std::string casadi::CodeGenerator::triusolve ( const Sparsity sp_x,
const std::string &  x,
const std::string &  y,
bool  tr,
bool  unity,
casadi_int  nrhs 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_st

Definition at line 2214 of file code_generator.cpp.

2215  {
2217  return "casadi_triusolve(" + sparsity(sp_x) + ", " + x + ", " + y + ", " + str(tr) + ", "
2218  + str(unity) + ", " + str(nrhs) + ");";
2219  }

References add_auxiliary(), AUX_TRIUSOLVE, sparsity(), and casadi::str().

Referenced by casadi::TriuSolve< Tr >::generate(), and casadi::TriuSolveUnity< Tr >::generate().

◆ unindent()

void casadi::CodeGenerator::unindent ( )
inline

Extra doc: https://github.com/casadi/casadi/wiki/L_sh

Definition at line 259 of file code_generator.hpp.

259 {current_indent_--;}

Referenced by print_formatted().

◆ vector_fmax()

std::string casadi::CodeGenerator::vector_fmax ( casadi_int  n,
const std::string &  x,
const std::string &  y,
const std::string &  z 
)

Definition at line 2161 of file code_generator.cpp.

2162  {
2164  return "casadi_vector_fmax(" + str(n) + ", " + x + ", " + y + ", " + z + ");";
2165  }

References add_auxiliary(), AUX_VECTOR_FMAX, and casadi::str().

Referenced by casadi::Feasiblesqpmethod::codegen_feasibility_iterations().

◆ vector_fmin()

std::string casadi::CodeGenerator::vector_fmin ( casadi_int  n,
const std::string &  x,
const std::string &  y,
const std::string &  z 
)

Definition at line 2167 of file code_generator.cpp.

2168  {
2170  return "casadi_vector_fmin(" + str(n) + ", " + x + ", " + y + ", " + z + ");";
2171  }

References add_auxiliary(), AUX_VECTOR_FMIN, and casadi::str().

Referenced by casadi::Feasiblesqpmethod::codegen_feasibility_iterations().

◆ vfmax() [1/2]

std::string casadi::CodeGenerator::vfmax ( const std::string &  x,
casadi_int  n,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t8

Definition at line 2503 of file code_generator.cpp.

2504  {
2506  return "casadi_vfmax(" + x + ", " + str(n) + ", " + y + ");";
2507  }

References add_auxiliary(), AUX_VFMAX, and casadi::str().

◆ vfmax() [2/2]

std::string casadi::CodeGenerator::vfmax ( const std::string &  x,
const std::string &  n,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_ta

Definition at line 2515 of file code_generator.cpp.

2516  {
2518  return "casadi_vfmax(" + x + ", " + n + ", " + y + ");";
2519  }

References add_auxiliary(), and AUX_VFMAX.

◆ vfmin() [1/2]

std::string casadi::CodeGenerator::vfmin ( const std::string &  x,
casadi_int  n,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_t9

Definition at line 2509 of file code_generator.cpp.

2510  {
2512  return "casadi_vfmin(" + x + ", " + str(n) + ", " + y + ");";
2513  }

References add_auxiliary(), AUX_VFMIN, and casadi::str().

◆ vfmin() [2/2]

std::string casadi::CodeGenerator::vfmin ( const std::string &  x,
const std::string &  n,
const std::string &  y 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_tb

Definition at line 2521 of file code_generator.cpp.

2522  {
2524  return "casadi_vfmin(" + x + ", " + n + ", " + y + ");";
2525  }

References add_auxiliary(), and AUX_VFMIN.

◆ work()

std::string casadi::CodeGenerator::work ( casadi_int  n,
casadi_int  sz,
bool  is_ref 
) const

Get work vector name from index

Definition at line 989 of file code_generator.cpp.

989  {
990  if (is_ref) {
991  return "wr" + format_padded(n);
992  }
993  if (n<0 || sz==0) {
994  return "0";
995  } else if (sz==1 && !this->codegen_scalars) {
996  return "(&w" + format_padded(n) + ")";
997  } else {
998  return "w" + format_padded(n);
999  }
1000  }

References codegen_scalars, and format_padded().

Referenced by casadi::BSplineParametric::generate(), casadi::Bilin::generate(), casadi::BinaryMX< ScX, ScY >::generate(), casadi::BSplineCommon::generate(), casadi::Call::generate(), casadi::Find::generate(), casadi::Low::generate(), casadi::Concat::generate(), casadi::ConstantDM::generate(), casadi::ConstantFile::generate(), casadi::ConstantPool::generate(), casadi::Constant< Value >::generate(), casadi::Convexify::generate(), casadi::Dot::generate(), casadi::Einstein::generate(), casadi::GetNonzerosVector::generate(), casadi::GetNonzerosSlice::generate(), casadi::GetNonzerosSlice2::generate(), casadi::GetNonzerosParamVector::generate(), casadi::GetNonzerosSliceParam::generate(), casadi::GetNonzerosParamSlice::generate(), casadi::GetNonzerosParamParam::generate(), casadi::Input::generate(), casadi::Output::generate(), casadi::LogSumExp::generate(), casadi::MMin::generate(), casadi::MMax::generate(), casadi::Monitor::generate(), casadi::Multiplication::generate(), casadi::DenseMultiplication::generate(), casadi::NormF::generate(), casadi::Norm1::generate(), casadi::NormInf::generate(), casadi::Project::generate(), casadi::Densify::generate(), casadi::Sparsify::generate(), casadi::Rank1::generate(), casadi::HorzRepmat::generate(), casadi::HorzRepsum::generate(), casadi::SetNonzerosVector< Add >::generate(), casadi::SetNonzerosSlice< Add >::generate(), casadi::SetNonzerosSlice2< Add >::generate(), casadi::SetNonzerosParam< Add >::generate(), casadi::SetNonzerosParamVector< Add >::generate(), casadi::SetNonzerosParamSlice< Add >::generate(), casadi::SetNonzerosSliceParam< Add >::generate(), casadi::SetNonzerosParamParam< Add >::generate(), casadi::LinsolCall< Tr >::generate(), casadi::TriuSolve< Tr >::generate(), casadi::TrilSolve< Tr >::generate(), casadi::TriuSolveUnity< Tr >::generate(), casadi::TrilSolveUnity< Tr >::generate(), casadi::Split::generate(), casadi::Transpose::generate(), casadi::DenseTranspose::generate(), casadi::UnaryMX::generate(), and casadi::MXNode::generate_copy().

◆ workel()

std::string casadi::CodeGenerator::workel ( casadi_int  n) const

◆ wrapper()

std::string casadi::CodeGenerator::wrapper ( const Function base,
const std::string &  name 
)

Definition at line 237 of file code_generator.cpp.

237  {
238  FunctionInternal* f = base.get();
239  std::map<FunctionInternal*, casadi_int>& funs = added_wrappers_[name];
240  auto it = funs.find(f);
241  if (it==funs.end()) {
242  casadi_int n = funs.size();
243  funs[f] = n;
244  return name + str(n);
245  } else {
246  return name + str(it->second);
247  }
248  }
std::map< std::string, std::map< FunctionInternal *, casadi_int > > added_wrappers_

References added_wrappers_, casadi::Function::get(), name, and casadi::str().

Referenced by casadi::Nlpsol::codegen_body_enter(), casadi::Nlpsol::codegen_declarations(), casadi::IpoptInterface::codegen_declarations(), and casadi::IpoptInterface::set_ipopt_prob().

◆ zeros()

std::string casadi::CodeGenerator::zeros ( casadi_int  sz)

Definition at line 1325 of file code_generator.cpp.

1325  {
1326  sz_zeros_ = std::max(sz_zeros_, sz);
1327  return shorthand("zeros");
1328  }

References shorthand(), and sz_zeros_.

Referenced by casadi::Input::generate().

Member Data Documentation

◆ added_auxiliaries_

std::multimap<Auxiliary, std::vector<std::string> > casadi::CodeGenerator::added_auxiliaries_

Definition at line 996 of file code_generator.hpp.

Referenced by add_auxiliary().

◆ added_char_constants_

std::multimap<size_t, size_t> casadi::CodeGenerator::added_char_constants_

Definition at line 999 of file code_generator.hpp.

Referenced by get_constant().

◆ added_double_constants_

std::multimap<size_t, size_t> casadi::CodeGenerator::added_double_constants_

Definition at line 997 of file code_generator.hpp.

Referenced by get_constant().

◆ added_externals_

std::set<std::string> casadi::CodeGenerator::added_externals_

Definition at line 994 of file code_generator.hpp.

Referenced by add_external(), and dump().

◆ added_functions_

std::vector<FunctionMeta> casadi::CodeGenerator::added_functions_

◆ added_includes_

std::set<std::string> casadi::CodeGenerator::added_includes_

Definition at line 993 of file code_generator.hpp.

Referenced by add_include().

◆ added_integer_constants_

std::multimap<size_t, size_t> casadi::CodeGenerator::added_integer_constants_

Definition at line 998 of file code_generator.hpp.

Referenced by get_constant().

◆ added_sfunctions

std::vector<std::string> casadi::CodeGenerator::added_sfunctions

Definition at line 915 of file code_generator.hpp.

Referenced by add(), and generate().

◆ added_shorthands_

std::set<std::string> casadi::CodeGenerator::added_shorthands_

Definition at line 995 of file code_generator.hpp.

Referenced by dump(), and shorthand().

◆ added_string_constants_

std::multimap<size_t, size_t> casadi::CodeGenerator::added_string_constants_

Definition at line 1000 of file code_generator.hpp.

Referenced by get_constant().

◆ added_wrappers_

std::map<std::string, std::map<FunctionInternal*, casadi_int> > casadi::CodeGenerator::added_wrappers_

Definition at line 1018 of file code_generator.hpp.

Referenced by wrapper().

◆ auxiliaries

std::stringstream casadi::CodeGenerator::auxiliaries

◆ avoid_stack_

bool casadi::CodeGenerator::avoid_stack_

Definition at line 936 of file code_generator.hpp.

Referenced by CodeGenerator(), and sx_work().

◆ body

std::stringstream casadi::CodeGenerator::body

◆ buffer

std::stringstream casadi::CodeGenerator::buffer

Definition at line 971 of file code_generator.hpp.

Referenced by flush(), operator<<(), and print_formatted().

◆ casadi_int_type

std::string casadi::CodeGenerator::casadi_int_type

Definition at line 902 of file code_generator.hpp.

Referenced by CodeGenerator().

◆ casadi_real_type

std::string casadi::CodeGenerator::casadi_real_type

Definition at line 899 of file code_generator.hpp.

Referenced by CodeGenerator().

◆ char_constants_

std::vector<std::vector<char> > casadi::CodeGenerator::char_constants_

Definition at line 1023 of file code_generator.hpp.

Referenced by dump(), and get_constant().

◆ codegen_scalars

bool casadi::CodeGenerator::codegen_scalars

Use the work vector for storing work vector elements of length 1 (typically scalar) instead of using local variables

Extra doc: https://github.com/casadi/casadi/wiki/L_u9

Definition at line 946 of file code_generator.hpp.

Referenced by casadi::MXFunction::codegen_body(), CodeGenerator(), casadi::BinaryMX< ScX, ScY >::generate(), work(), and workel().

◆ cpp

bool casadi::CodeGenerator::cpp

◆ current_indent_

casadi_int casadi::CodeGenerator::current_indent_

Definition at line 978 of file code_generator.hpp.

Referenced by CodeGenerator(), dump(), and print_formatted().

◆ dll_export

std::string casadi::CodeGenerator::dll_export

Definition at line 961 of file code_generator.hpp.

Referenced by CodeGenerator().

◆ dll_import

std::string casadi::CodeGenerator::dll_import

Definition at line 961 of file code_generator.hpp.

Referenced by CodeGenerator(), and declare().

◆ double_constants_

std::vector<std::vector<double> > casadi::CodeGenerator::double_constants_

Definition at line 1021 of file code_generator.hpp.

Referenced by dump(), and get_constant().

◆ exposed_fname

std::vector<std::string> casadi::CodeGenerator::exposed_fname

Definition at line 987 of file code_generator.hpp.

Referenced by add(), and generate().

◆ file_scope_double_

std::map<const void *, casadi_int> casadi::CodeGenerator::file_scope_double_

◆ file_scope_integer_

std::map<const void *, casadi_int> casadi::CodeGenerator::file_scope_integer_

Definition at line 1004 of file code_generator.hpp.

Referenced by dump().

◆ force_canonical

bool casadi::CodeGenerator::force_canonical

Definition at line 958 of file code_generator.hpp.

Referenced by add_io_sparsities(), and CodeGenerator().

◆ header

std::stringstream casadi::CodeGenerator::header

◆ include_math

bool casadi::CodeGenerator::include_math

Definition at line 933 of file code_generator.hpp.

Referenced by CodeGenerator().

◆ includes

std::stringstream casadi::CodeGenerator::includes

Definition at line 967 of file code_generator.hpp.

Referenced by add_include(), and dump().

◆ indent_

casadi_int casadi::CodeGenerator::indent_

Definition at line 977 of file code_generator.hpp.

Referenced by CodeGenerator(), and print_formatted().

◆ infinity

std::string casadi::CodeGenerator::infinity

Definition at line 938 of file code_generator.hpp.

Referenced by add_auxiliary(), and CodeGenerator().

◆ integer_constants_

std::vector<std::vector<casadi_int> > casadi::CodeGenerator::integer_constants_

Definition at line 1022 of file code_generator.hpp.

Referenced by dump(), and get_constant().

◆ local_default_

std::map<std::string, std::string> casadi::CodeGenerator::local_default_

Definition at line 1002 of file code_generator.hpp.

Referenced by init_local(), scope_enter(), and scope_exit().

◆ local_variables_

std::map<std::string, std::pair<std::string, std::string> > casadi::CodeGenerator::local_variables_

Definition at line 1001 of file code_generator.hpp.

Referenced by local(), scope_enter(), and scope_exit().

◆ main

bool casadi::CodeGenerator::main

◆ max_declarations_per_line

casadi_int casadi::CodeGenerator::max_declarations_per_line

Definition at line 952 of file code_generator.hpp.

Referenced by CodeGenerator(), and scope_exit().

◆ max_initializer_elements_per_line

casadi_int casadi::CodeGenerator::max_initializer_elements_per_line

Definition at line 955 of file code_generator.hpp.

Referenced by CodeGenerator().

◆ mex

bool casadi::CodeGenerator::mex

◆ name

std::string casadi::CodeGenerator::name

◆ nan

std::string casadi::CodeGenerator::nan

Definition at line 938 of file code_generator.hpp.

Referenced by add_auxiliary(), and CodeGenerator().

◆ needs_mem_

bool casadi::CodeGenerator::needs_mem_

Definition at line 1027 of file code_generator.hpp.

Referenced by add_dependency(), CodeGenerator(), and dump().

◆ newline_

bool casadi::CodeGenerator::newline_

Definition at line 974 of file code_generator.hpp.

Referenced by CodeGenerator(), operator<<(), and print_formatted().

◆ padding_length_

casadi_int casadi::CodeGenerator::padding_length_

Definition at line 984 of file code_generator.hpp.

Referenced by format_padded(), and reserve_work().

◆ pool_double_

std::map<std::string, casadi_int> casadi::CodeGenerator::pool_double_

Definition at line 1006 of file code_generator.hpp.

Referenced by define_pool_double(), dump(), generate(), and pool_double().

◆ pool_double_defaults_

std::vector< std::vector<double> > casadi::CodeGenerator::pool_double_defaults_

Definition at line 1005 of file code_generator.hpp.

Referenced by define_pool_double(), dump(), and generate().

◆ prefix

std::string casadi::CodeGenerator::prefix

Definition at line 964 of file code_generator.hpp.

Referenced by CodeGenerator(), dump(), and generate().

◆ real_min

std::string casadi::CodeGenerator::real_min

Definition at line 938 of file code_generator.hpp.

Referenced by add_auxiliary(), and CodeGenerator().

◆ sparsity_meta

std::set<std::string> casadi::CodeGenerator::sparsity_meta

Definition at line 990 of file code_generator.hpp.

Referenced by add_io_sparsities().

◆ string_constants_

std::vector<std::vector<std::string> > casadi::CodeGenerator::string_constants_

Definition at line 1024 of file code_generator.hpp.

Referenced by dump(), and get_constant().

◆ suffix

std::string casadi::CodeGenerator::suffix

Definition at line 896 of file code_generator.hpp.

Referenced by CodeGenerator(), generate(), and sanitize_source().

◆ sz_ones_

casadi_int casadi::CodeGenerator::sz_ones_

Definition at line 982 of file code_generator.hpp.

Referenced by CodeGenerator(), dump(), and ones().

◆ sz_zeros_

casadi_int casadi::CodeGenerator::sz_zeros_

Definition at line 981 of file code_generator.hpp.

Referenced by CodeGenerator(), dump(), and zeros().

◆ unroll_args

bool casadi::CodeGenerator::unroll_args

Definition at line 918 of file code_generator.hpp.

Referenced by add(), and CodeGenerator().

◆ verbose

bool casadi::CodeGenerator::verbose

Definition at line 921 of file code_generator.hpp.

Referenced by casadi::MXFunction::codegen_body(), CodeGenerator(), and comment().

◆ verbose_runtime

bool casadi::CodeGenerator::verbose_runtime

Definition at line 924 of file code_generator.hpp.

Referenced by CodeGenerator(), and sanitize_source().

◆ with_export

bool casadi::CodeGenerator::with_export

Definition at line 949 of file code_generator.hpp.

Referenced by CodeGenerator(), and dump().

◆ with_header

bool casadi::CodeGenerator::with_header

◆ with_import

bool casadi::CodeGenerator::with_import

Definition at line 949 of file code_generator.hpp.

Referenced by CodeGenerator(), and generate().

◆ with_mem

bool casadi::CodeGenerator::with_mem

Definition at line 905 of file code_generator.hpp.

Referenced by casadi::FunctionInternal::codegen_meta(), CodeGenerator(), and dump().

◆ with_sfunction

bool casadi::CodeGenerator::with_sfunction

Definition at line 914 of file code_generator.hpp.

Referenced by add(), CodeGenerator(), and generate().


The documentation for this class was generated from the following files: