SX/generateCode.py

This example looks at symbolic substitution is SX graphs

View output (PDF) | source (python)

/* This file was automatically generated by CasADi 3.6.6.
* It consists of:
* 1) content generated by CasADi runtime: not copyrighted
* 2) template code copied from CasADi source: permissively licensed (MIT-0)
* 3) user code: owned by the user
*
*/
#ifdef __cplusplus
extern "C" {
#endif
/* How to prefix internal symbols */
#ifdef CASADI_CODEGEN_PREFIX
#define CASADI_NAMESPACE_CONCAT(NS, ID) _CASADI_NAMESPACE_CONCAT(NS, ID)
#define _CASADI_NAMESPACE_CONCAT(NS, ID) NS ## ID
#define CASADI_PREFIX(ID) CASADI_NAMESPACE_CONCAT(CODEGEN_PREFIX, ID)
#else
#define CASADI_PREFIX(ID) f_generated_ ## ID
#endif
#include <math.h>
#ifndef casadi_real
#define casadi_real double
#endif
#ifndef casadi_int
#define casadi_int long long int
#endif
/* Add prefix to internal symbols */
#define casadi_f0 CASADI_PREFIX(f0)
#define casadi_s0 CASADI_PREFIX(s0)
/* Symbol visibility in DLLs */
#ifndef
#if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
#if defined(STATIC_LINKED)
#define
#else
#define __declspec(dllexport)
#endif
#elif defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY)
#define __attribute__ ((visibility ("default")))
#else
#define
#endif
#endif
static const casadi_int casadi_s0[5] = {1, 1, 0, 1, 0};
/* f:(i0,i1)->(o0) */
static int casadi_f0(const casadi_real** arg, casadi_real** res, casadi_int* iw, casadi_real* w, int mem) {
casadi_real a0, a1, a2;
a0=arg[0]? arg[0][0] : 0;
a1=arg[1]? arg[1][0] : 0;
a0=(a0*a1);
a2=2.;
a2=(a2*a1);
a0=(a0+a2);
a2=4.;
a2=(a2*a0);
a0=(a0+a2);
if (res[0]!=0) res[0][0]=a0;
return 0;
}
int f(const casadi_real** arg, casadi_real** res, casadi_int* iw, casadi_real* w, int mem){
return casadi_f0(arg, res, iw, w, mem);
}
int f_alloc_mem(void) {
return 0;
}
int f_init_mem(int mem) {
return 0;
}
void f_free_mem(int mem) {
}
int f_checkout(void) {
return 0;
}
void f_release(int mem) {
}
void f_incref(void) {
}
void f_decref(void) {
}
casadi_int f_n_in(void) { return 2;}
casadi_int f_n_out(void) { return 1;}
casadi_real f_default_in(casadi_int i) {
switch (i) {
default: return 0;
}
}
const char* f_name_in(casadi_int i) {
switch (i) {
case 0: return "i0";
case 1: return "i1";
default: return 0;
}
}
const char* f_name_out(casadi_int i) {
switch (i) {
case 0: return "o0";
default: return 0;
}
}
const casadi_int* f_sparsity_in(casadi_int i) {
switch (i) {
case 0: return casadi_s0;
case 1: return casadi_s0;
default: return 0;
}
}
const casadi_int* f_sparsity_out(casadi_int i) {
switch (i) {
case 0: return casadi_s0;
default: return 0;
}
}
int f_work(casadi_int *sz_arg, casadi_int* sz_res, casadi_int *sz_iw, casadi_int *sz_w) {
if (sz_arg) *sz_arg = 2;
if (sz_res) *sz_res = 1;
if (sz_iw) *sz_iw = 0;
if (sz_w) *sz_w = 0;
return 0;
}
int f_work_bytes(casadi_int *sz_arg, casadi_int* sz_res, casadi_int *sz_iw, casadi_int *sz_w) {
if (sz_arg) *sz_arg = 2*sizeof(const casadi_real*);
if (sz_res) *sz_res = 1*sizeof(casadi_real*);
if (sz_iw) *sz_iw = 0*sizeof(casadi_int);
if (sz_w) *sz_w = 0*sizeof(casadi_real);
return 0;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
See also
casadi::Function::generate(const std::string&);
casadi::Function::repr(std::ostream &stream);
casadi::Function::print(std::ostream &stream);
std::string generate(const std::string &fname, const Dict &opts=Dict()) const
Export / Generate C code for the function.
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 from casadi import *
27 
28 
29 
30 x = SX.sym("x")
31 y = SX.sym("y")
32 z = x*y+2*y
33 z += 4*z
34 
35 
36 
37 f = Function("f", [x,y],[z])
38 
39 
40 
41 print(f.__repr__())
42 
43 
44 
45 
46 print(f)
47 
48 
49 
50 f.generate("f_generated")
51 
52 
53 
54 print(open('f_generated.c').read())
55