Ejemplo n.º 1
0
import numpy
from numpy import sin, cos
from taylorpoly import UTPS
x = numpy.array([UTPS([1, 0, 0], P=2), UTPS([0, 0, 0], P=2)])
p = UTPS([3, 1, 0], P=2)


def f(x):
    return numpy.array([x[1], -p * x[0]])


ts = numpy.linspace(0, 2 * numpy.pi, 100)
x_list = [[xi.data.copy() for xi in x]]
for nts in range(ts.size - 1):
    h = ts[nts + 1] - ts[nts]
    x = x + h * f(x)
    x_list.append([xi.data.copy() for xi in x])

xs = numpy.array(x_list)
import matplotlib.pyplot as pyplot
pyplot.plot(ts, xs[:, 0, 0], '.k-', label=r'$x(t)$')
pyplot.plot(ts, xs[:, 0, 1], '.r-', label=r'$x_p(t)$')
pyplot.xlabel('time $t$')
pyplot.legend(loc='best')
pyplot.grid()
pyplot.show()
at x = (3,7)
"""

import numpy
from numpy import sin, cos, array, zeros
from taylorpoly import UTPS


def f_fcn(x):
    return sin(x[0] + cos(x[1]) * x[0])


S = array([[1, 0, 1], [0, 1, 1]], dtype=float)
P = S.shape[1]
print('seed matrix with P = %d directions S = \n' % P, S)
x1 = UTPS(zeros(1 + 2 * P), P=P)
x2 = UTPS(zeros(1 + 2 * P), P=P)
x1.data[0] = 3
x1.data[1::2] = S[0, :]
x2.data[0] = 7
x2.data[1::2] = S[1, :]
y = f_fcn([x1, x2])
print('x1=', x1)
print('x2=', x2)
print('y=', y)
H = zeros((2, 2), dtype=float)
H[0, 0] = 2 * y.coeff[0, 2]
H[1, 0] = H[0, 1] = (y.coeff[2, 2] - y.coeff[0, 2] - y.coeff[1, 2])
H[1, 1] = 2 * y.coeff[1, 2]

import numpy; from numpy import sin,cos; from taylorpoly import UTPS
x1 = UTPS([3,1,0],P=2); x2 = UTPS([7,0,1],P=2)
# forward mode 
vm1 = x1
v0  = x2
v1 = cos(v0)
v2 = v1 * vm1
v3 = vm1 + v2
v4 = sin(v3)
y  = v4
# reverse mode
v4bar = UTPS([0,0,0],P=2);  v3bar = UTPS([0,0,0],P=2)
v2bar = UTPS([0,0,0],P=2);  v1bar = UTPS([0,0,0],P=2)
v0bar = UTPS([0,0,0],P=2); vm1bar = UTPS([0,0,0],P=2)
v4bar.data[0] = 1.
v3bar += v4bar*cos(v3) 
vm1bar += v3bar; v2bar += v3bar
v1bar += v2bar * vm1;  vm1bar += v2bar * v1
v0bar -= v1bar * sin(v0) 
g1 = y.data[1:]; g2 = numpy.array([vm1bar.data[0], v0bar.data[0]])
print 'UTPS gradient g(x_0)=', g1
print 'reverse gradient g(x_0)=', g2
print 'Hessian H(x_0)=\n',numpy.vstack([vm1bar.data[1:], v0bar.data[1:]])
"""
compute the Hessian H of the function
def f(x):
    return sin(x[0] + cos(x[1])*x[0])
at x = (3,7)
"""

import numpy; from numpy import sin,cos, array, zeros
from taylorpoly import UTPS
def f_fcn(x):
    return sin(x[0] + cos(x[1])*x[0])

S = array([[1,0,1],[0,1,1]], dtype=float)
P = S.shape[1]
print 'seed matrix with P = %d directions S = \n'%P, S
x1 = UTPS(zeros(1+2*P), P = P)
x2 = UTPS(zeros(1+2*P), P = P)
x1.data[0] = 3; x1.data[1::2] = S[0,:]
x2.data[0] = 7; x2.data[1::2] = S[1,:]
y = f_fcn([x1,x2])
print 'x1=',x1;  print 'x2=',x2; print 'y=',y
H = zeros((2,2),dtype=float)
H[0,0] = 2*y.coeff[0,2]
H[1,0] = H[0,1] = (y.coeff[2,2] - y.coeff[0,2] - y.coeff[1,2])
H[1,1] =  2*y.coeff[1,2]

def H_fcn(x):
    H11 = -(1+cos(x[1]))**2*sin(x[0]+cos(x[1])*x[0])
    H21 = -sin(x[1]) * cos(x[0] + cos(x[1])*x[0]) \
          +sin(x[1]) *x[0]*(1+ cos(x[1]))*sin(x[0]+cos(x[1])*x[0])
    H22 = -cos(x[1])*x[0]*cos(x[0]+cos(x[1])*x[0])\
Ejemplo n.º 5
0
import numpy
from numpy import sin, cos
from taylorpoly import UTPS


def f(x):
    return sin(x[0] + cos(x[1]) * x[0]) + x[1] * x[0]


x = [UTPS([3, 1, 0], P=2), UTPS([7, 0, 1], P=2)]
y = f(x)

print('normal function evaluation y_0 = f(x_0) = ', y.data[0])
print('gradient evaluation g(x_0) = ', y.data[1:])
Ejemplo n.º 6
0
import numpy
from numpy import sin, cos
from taylorpoly import UTPS
x1 = UTPS([3, 1, 0], P=2)
x2 = UTPS([7, 0, 1], P=2)
# forward mode
vm1 = x1
v0 = x2
v1 = cos(v0)
v2 = v1 * vm1
v3 = vm1 + v2
v4 = sin(v3)
y = v4
# reverse mode
v4bar = UTPS([0, 0, 0], P=2)
v3bar = UTPS([0, 0, 0], P=2)
v2bar = UTPS([0, 0, 0], P=2)
v1bar = UTPS([0, 0, 0], P=2)
v0bar = UTPS([0, 0, 0], P=2)
vm1bar = UTPS([0, 0, 0], P=2)
v4bar.data[0] = 1.
v3bar += v4bar * cos(v3)
vm1bar += v3bar
v2bar += v3bar
v1bar += v2bar * vm1
vm1bar += v2bar * v1
v0bar -= v1bar * sin(v0)
g1 = y.data[1:]
g2 = numpy.array([vm1bar.data[0], v0bar.data[0]])
print('UTPS gradient g(x_0)=', g1)
print('reverse gradient g(x_0)=', g2)