Пример #1
0
def fun_property_xam():
    #
    import numpy
    import cppad_py
    #
    # initialize return variable
    ok = True
    # ---------------------------------------------------------------------
    n_ind = 1  # number of independent variables
    n_dep = 2  # number of dependent variables
    n_var = 1  # phantom variable at address 0
    n_op = 1  # special operator at beginning
    #
    # dimension some vectors
    # x  = numpy.empty(n_ind, dtype=float)
    x = numpy.empty(n_ind, dtype=float)
    ay = numpy.empty(n_dep, dtype=cppad_py.a_double)
    #
    # independent variables
    x[0] = 1.0
    ax = cppad_py.independent(x)
    n_var = n_var + n_ind  # one for each indpendent
    n_op = n_op + n_ind
    #
    # first dependent variable
    ay[0] = ax[0] + ax[0]
    n_var = n_var + 1  # one variable and operator
    n_op = n_op + 1
    #
    # second dependent variable
    ax0 = ax[0]
    ay[1] = ax0.sin()
    n_var = n_var + 2  # two varialbes, one operator
    n_op = n_op + 1
    #
    # define f(x) = y
    f = cppad_py.d_fun(ax, ay)
    n_op = n_op + 1  # speical operator at end
    #
    # check af properties
    ok = ok and f.size_domain() == n_ind
    ok = ok and f.size_range() == n_dep
    ok = ok and f.size_var() == n_var
    ok = ok and f.size_op() == n_op
    ok = ok and f.size_order() == 0
    #
    # compute zero order Taylor coefficients
    y = f.forward(0, x)
    ok = ok and f.size_order() == 1
    # ---------------------------------------------------------------------
    af = cppad_py.a_fun(f)
    #
    # check af properties
    ok = ok and af.size_domain() == n_ind
    ok = ok and af.size_range() == n_dep
    ok = ok and af.size_var() == n_var
    ok = ok and af.size_op() == n_op
    ok = ok and af.size_order() == 0
    #
    return (ok)
Пример #2
0
def fun_jacobian_xam() :
	#
	import numpy
	import cppad_py
	#
	# initialize return variable
	ok = True
	# ---------------------------------------------------------------------
	# number of dependent and independent variables
	n_dep = 1
	n_ind = 3
	#
	# create the independent variables ax
	x = numpy.empty(n_ind, dtype=float)
	for i in range( n_ind  ) :
		x[i] = i + 2.0
	#
	ax = cppad_py.independent(x)
	#
	# create dependent variables ay with ay0 = ax_0 * ax_1 * ax_2
	ax_0  = ax[0]
	ax_1  = ax[1]
	ax_2  = ax[2]
	ay    = numpy.empty(n_dep, dtype=cppad_py.a_double)
	ay[0] = ax_0 * ax_1 * ax_2
	#
	# define af corresponding to f(x) = x_0 * x_1 * x_2
	f  = cppad_py.d_fun(ax, ay)
	#
	# compute the Jacobian f'(x) = ( x_1*x_2, x_0*x_2, x_0*x_1 )
	fp = f.jacobian(x)
	#
	# check Jacobian
	x_0 = x[0]
	x_1 = x[1]
	x_2 = x[2]
	ok = ok and fp[0, 0] == x_1 * x_2
	ok = ok and fp[0, 1] == x_0 * x_2
	ok = ok and fp[0, 2] == x_0 * x_1
	# ---------------------------------------------------------------------
	af = cppad_py.a_fun(f)
	#
	ax   = numpy.empty(n_ind, dtype=cppad_py.a_double)
	for i in range( n_ind ) :
		ax[i] = x[i]
	#
	# compute the Jacobian f'(x) = ( x_1*x_2, x_0*x_2, x_0*x_1 )
	afp = af.jacobian(ax)
	#
	# check Jacobian
	ok = ok and afp[0, 0] == cppad_py.a_double(x_1 * x_2)
	ok = ok and afp[0, 1] == cppad_py.a_double(x_0 * x_2)
	ok = ok and afp[0, 2] == cppad_py.a_double(x_0 * x_1)
	#
	return( ok )
Пример #3
0
def a_fun_xam():
    #
    import numpy
    import cppad_py
    #
    # initialize return variable
    ok = True
    # ---------------------------------------------------------------------
    # number of dependent and independent variables
    m = 1
    n = 3
    #
    # Record the function
    #	f(x) = 0.5 * ( x[0]^2 + ... + x[n-1]^2 )
    x = numpy.empty(n, dtype=float)
    for i in range(n):
        x[i] = i
    ax = cppad_py.independent(x)
    ay = numpy.empty(m, dtype=cppad_py.a_double)
    asum = cppad_py.a_double(0.0)
    for i in range(n):
        asum = asum + ax[i] * ax[i]
    ay[0] = cppad_py.a_double(0.5) * asum
    f = cppad_py.d_fun(ax, ay)
    af = cppad_py.a_fun(f)
    #
    # Record the function
    #	g(x) = f'(x) * v
    au = numpy.empty(m, dtype=cppad_py.a_double)
    av = numpy.empty(n, dtype=cppad_py.a_double)
    for i in range(n):
        av[i] = cppad_py.a_double(i)
    ax = cppad_py.independent(x)
    af.forward(0, ax)
    au = af.forward(1, av)
    g = cppad_py.d_fun(ax, au)
    #
    # compute g(x)
    u = g.forward(0, x)
    #
    # compute g'(x)
    uq = numpy.empty((m, 1), dtype=float)
    uq[0, 0] = 1.0
    xq = g.reverse(1, uq)
    #
    # g'(x) = f''(x) * v = v
    for i in range(n):
        ok = ok and cppad_py.a_double(xq[i, 0]) == av[i]
    return ok
Пример #4
0
def fun_reverse_xam() :
	#
	import numpy
	import cppad_py
	#
	# initialize return variable
	ok = True
	# ---------------------------------------------------------------------
	# number of dependent and independent variables
	n_dep = 1
	n_ind = 3
	#
	# create the independent variables ax
	xp = numpy.empty(n_ind, dtype=float)
	for i in range( n_ind  ) :
		xp[i] = i
	#
	ax = cppad_py.independent(xp)
	#
	# create dependent variables ay with ay0 = ax_0 * ax_1 * ax_2
	ax_0  = ax[0]
	ax_1  = ax[1]
	ax_2  = ax[2]
	ay    = numpy.empty(n_dep, dtype=cppad_py.a_double)
	ay[0] = ax_0 * ax_1 * ax_2
	#
	# define af corresponding to f(x) = x_0 * x_1 * x_2
	f  = cppad_py.d_fun(ax, ay)
	# -----------------------------------------------------------------------
	# define          X(t) = (x_0 + t, x_1 + t, x_2 + t)
	# it follows that Y(t) = f(X(t)) = (x_0 + t) * (x_1 + t) * (x_2 + t)
	# and that       Y'(0) = x_1 * x_2 + x_0 * x_2 + x_0 * x_1
	# -----------------------------------------------------------------------
	# zero order forward mode
	p     = 0
	xp[0] = 2.0
	xp[1] = 3.0
	xp[2] = 4.0
	yp = f.forward(p, xp)
	ok = ok and yp[0] == 24.0
	# -----------------------------------------------------------------------
	# first order reverse (derivative of zero order forward)
	# define G( Y ) = y_0 = x_0 * x_1 * x_2
	m         = f.size_range()
	q         = 1
	yq1       = numpy.empty( (m, q), dtype=float)
	yq1[0, 0] = 1.0
	xq1       = f.reverse(q, yq1)
	# partial G w.r.t x_0
	ok = ok and xq1[0,0] == 3.0 * 4.0
	# partial G w.r.t x_1
	ok = ok and xq1[1,0] == 2.0 * 4.0
	# partial G w.r.t x_2
	ok = ok and xq1[2,0] == 2.0 * 3.0
	# -----------------------------------------------------------------------
	# first order forward mode
	p     = 1
	xp[0] = 1.0
	xp[1] = 1.0
	xp[2] = 1.0
	yp    = f.forward(p, xp)
	ok    = ok and yp[0] == 3.0*4.0 + 2.0*4.0 + 2.0*3.0
	# -----------------------------------------------------------------------
	# second order reverse (derivative of first order forward)
	# define G( y_0^0 , y_0^1 ) = y_0^1
	# = x_1^0 * x_2^0  +  x_0^0 * x_2^0  +  x_0^0  *  x_1^0
	q         = 2
	yq2       = numpy.empty( (m, q), dtype=float)
	yq2[0, 0] = 0.0 # partial of G w.r.t y_0^0
	yq2[0, 1] = 1.0 # partial of G w.r.t y_0^1
	xq2       = f.reverse(q, yq2)
	# partial G w.r.t x_0^0
	ok = ok and xq2[0, 0] == 3.0 + 4.0
	# partial G w.r.t x_1^0
	ok = ok and xq2[1, 0] == 2.0 + 4.0
	# partial G w.r.t x_2^0
	ok = ok and xq2[2, 0] == 2.0 + 3.0
	# -----------------------------------------------------------------------
	af = cppad_py.a_fun(f)
	#
	# zero order forward
	axp   = numpy.empty(n_ind, dtype=cppad_py.a_double)
	p     = 0
	axp[0] = 2.0
	axp[1] = 3.0
	axp[2] = 4.0
	ayp = af.forward(p, axp)
	ok = ok and ayp[0] == cppad_py.a_double(24.0)
	#
	# first order reverse
	q          = 1
	ayq1       = numpy.empty( (m, q), dtype=cppad_py.a_double)
	ayq1[0, 0] = 1.0
	axq1       = af.reverse(q, ayq1)
	# partial G w.r.t x_0
	ok = ok and axq1[0,0] == cppad_py.a_double(3.0 * 4.0)
	# partial G w.r.t x_1
	ok = ok and axq1[1,0] == cppad_py.a_double(2.0 * 4.0)
	# partial G w.r.t x_2
	ok = ok and axq1[2,0] == cppad_py.a_double(2.0 * 3.0)
	#
	return( ok )
Пример #5
0
def fun_forward_xam() :
	#
	import numpy
	import cppad_py
	#
	# initialize return variable
	ok = True
	# ---------------------------------------------------------------------
	# number of dependent and independent variables
	n_dep = 1
	n_ind = 2
	#
	# create the independent variables ax
	xp = numpy.empty(n_ind, dtype=float)
	for i in range( n_ind  ) :
		xp[i] = i + 1.0
	#
	ax = cppad_py.independent(xp)
	#
	# create dependent varialbes ay with ay0 = ax0 * ax1
	ax0    = ax[0]
	ax1    = ax[1]
	ay    = numpy.empty(n_dep, dtype=cppad_py.a_double)
	ay[0] = ax0 * ax1
	#
	# define af corresponding to f(x) = x0 * x1
	f  = cppad_py.d_fun(ax, ay)
	#
	# define X(t) = (3 + t, 2 + t)
	# it follows that Y(t) = f(X(t)) = (3 + t) * (2 + t)
	#
	# Y(0) = 6 and p ! = 1
	p     = 0
	xp[0] = 3.0
	xp[1] = 2.0
	yp = f.forward(p, xp)
	ok = ok and yp[0] == 6.0
	#
	# first order Taylor coefficients for X(t)
	p     = 1
	xp[0] = 1.0
	xp[1] = 1.0
	#
	# first order Taylor coefficient for Y(t)
	# Y'(0) = 3 + 2 = 5 and p ! = 1
	yp = f.forward(p, xp)
	ok = ok and yp[0] == 5.0
	#
	# second order Taylor coefficients for X(t)
	p     = 2
	xp[0] = 0.0
	xp[1] = 0.0
	#
	# second order Taylor coefficient for Y(t)
	# Y''(0) = 2.0 and p ! = 2
	yp = f.forward(p, xp)
	ok = ok and yp[0] == 1.0
	# ---------------------------------------------------------------------
	af = cppad_py.a_fun(f)
	ok = ok and af.size_order() == 0
	#
	# zero order forward
	p   = 0
	axp = numpy.empty(n_ind, dtype=cppad_py.a_double)
	axp[0] = 3.0
	axp[1] = 2.0
	ayp    = af.forward(p, axp)
	ok     = ok and ayp[0] == cppad_py.a_double(6.0)
	ok     = ok and af.size_order() == 1
	#
	return( ok )
Пример #6
0
def fun_hessian_xam():
    #
    import numpy
    import cppad_py
    #
    # initialize return variable
    ok = True
    # ---------------------------------------------------------------------
    # number of dependent and independent variables
    n_dep = 1
    n_ind = 3
    #
    # create the independent variables ax
    x = numpy.empty(n_ind, dtype=float)
    for i in range(n_ind):
        x[i] = i + 2.0
    #
    ax = cppad_py.independent(x)
    #
    # create dependent variables ay with ay0 = ax_0 * ax_1 * ax_2
    ax_0 = ax[0]
    ax_1 = ax[1]
    ax_2 = ax[2]
    ay = numpy.empty(n_dep, dtype=cppad_py.a_double)
    ay[0] = ax_0 * ax_1 * ax_2
    #
    # define af corresponding to f(x) = x_0 * x_1 * x_2
    f = cppad_py.d_fun(ax, ay)
    #
    # g(x) = w_0 * f_0 (x) = f(x)
    w = numpy.empty(n_dep, dtype=float)
    w[0] = 1.0
    #
    # compute Hessian
    fpp = f.hessian(x, w)
    #
    #          [ 0.0 , x_2 , x_1 ]
    # f''(x) = [ x_2 , 0.0 , x_0 ]
    #          [ x_1 , x_0 , 0.0 ]
    ok = ok and fpp[0, 0] == 0.0
    ok = ok and fpp[0, 1] == x[2]
    ok = ok and fpp[0, 2] == x[1]
    #
    ok = ok and fpp[1, 0] == x[2]
    ok = ok and fpp[1, 1] == 0.0
    ok = ok and fpp[1, 2] == x[0]
    #
    ok = ok and fpp[2, 0] == x[1]
    ok = ok and fpp[2, 1] == x[0]
    ok = ok and fpp[2, 2] == 0.0
    # ---------------------------------------------------------------------
    af = cppad_py.a_fun(f)
    #
    # compute and check Hessian
    aw = numpy.empty(n_dep, dtype=cppad_py.a_double)
    aw[0] = w[0]
    afpp = af.hessian(ax, aw)
    ok = ok and afpp.shape == fpp.shape
    (nr, nc) = fpp.shape
    for i in range(nr):
        for j in range(nc):
            ok = ok and afpp[i, j] == cppad_py.a_double(fpp[i, j])
    #
    return (ok)