\(\newcommand{\B}[1]{ {\bf #1} }\) \(\newcommand{\R}[1]{ {\rm #1} }\)
fun_to_json_xam.py#
View page sourcePython to_json: Example and Test#
def to_json_xam() :
#
import numpy
import cppad_py
import re
#
# initialize return variable
ok = True
# ---------------------------------------------------------------------
n = 1 # number of independent variables
m = 2 # number of dependent variables
#
# independent variables
x = numpy.array( [ 1.0 ] )
ax = cppad_py.independent(x)
#
# f(x) = [ x0 + x0, sin(x0) ]
ay = numpy.empty(m, dtype=cppad_py.a_double)
ay[0] = ax[0] + ax[0]
ay[1] = ax[0].sin()
f = cppad_py.d_fun(ax, ay)
#
# check f.to_json
json = f.to_json()
pattern = r'"op_code" *: *([^,]*),'
match = re.search(pattern, json)
op_code = int( match.group(1) )
ok &= op_code == 1
pattern = r'"name" *: *"([^"]*)" *,'
match = re.search(pattern, json)
name = match.group(1);
ok &= name == 'add' or name == 'sub'
#
return ok