-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcppadcg_c_codegen.py
49 lines (36 loc) · 1.38 KB
/
cppadcg_c_codegen.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from pycppad import AD, ADCG, CG, Independent, Value, ADCGFun, CodeHandler, LanguageC, LangCDefaultVariableNameGenerator
import numpy as np
#/***************************************************************************
# the model
#*************************************************************************/
# independent variable vector
n = 2
x = np.array([ADCG(CG(0.)),]*n)
x[0] = ADCG(CG(2.))
x[1] = ADCG(CG(3.))
Independent(x)
# dependent variable vector
m=1
y = np.array([ADCG(CG(0)),]*m)
#the model
a = x[0] / ADCG(CG(1.)) + x[1] * x[1]
y[0] = a / ADCG(CG(2.))
fun = ADCGFun(x, y); # the model tape
# /***************************************************************************
# * Generate the C source code
# **************************************************************************/
# /**
# * start the special steps for source code generation for a Jacobian
# */
handler = CodeHandler(50)
indVars = np.array([CG(0.)]*n)
handler.makeVariables(indVars)
jac = fun.Jacobian(indVars)
langC = LanguageC("double", 3)
nameGen = LangCDefaultVariableNameGenerator("y","x","v","array","sarray")
code = handler.generateCode(langC, jac, nameGen, "source")
output = code.splitlines()
assert(output[0]==' y[1] = 0.5 * x[1] + 0.5 * x[1];')
assert(output[1]==' // dependent variables without operations')
assert(output[2]==' y[0] = 0.5;')
print(code)