|
Prev | Next | a_fun_optimize_xam.py | Headings |
def a_fun_optimize_xam() :
#
# load the Cppad Swig library
import py_cppad
#
# initialize return variable
ok = True
# ---------------------------------------------------------------------
n_ind = 1 # number of independent variables
n_dep = 1 # number of dependent variables
n_var = 1 # phantom variable at address 0
n_op = 1 # special operator at beginning
#
# dimension some vectors
x = py_cppad.vec_double(n_ind)
ay = py_cppad.vec_a_double(n_dep)
#
# independent variables
x[0] = 1.0
ax = py_cppad.independent(x)
n_var = n_var + n_ind # one for each indpendent
n_op = n_op + n_ind
#
# accumulate summation
ax0 = ax[0]
csum = py_cppad.a_double(0.0)
csum = ax0 + ax0 + ax0 + ax0
n_var = n_var + 3 # one per + operator
n_op = n_op + 3
#
# define f(x) = y_0 = csum
ay[0] = csum
af = py_cppad.a_fun(ax, ay)
n_op = n_op + 1 # speical operator at end
#
# check number of variables and operators
ok = ok and af.size_var() == n_var
ok = ok and af.size_op() == n_op
#
# optimize
af.optimize()
#
# number of variables and operators has decreased by two
ok = ok and af.size_var() == n_var-2
ok = ok and af.size_op() == n_op-2
#
return( ok )
#