def ones(shape, dtype=float, order='C'): """ generic implementation of numpy.ones """ if numpy.isscalar(shape): shape = (shape, ) if isinstance(dtype, type): return numpy.ones(shape, dtype=dtype, order=order) elif isinstance(dtype, numpy.ndarray): return numpy.ones(shape, dtype=dtype.dtype, order=order) elif isinstance(dtype, UTPM): D, P = dtype.data.shape[:2] tmp = numpy.zeros((D, P) + shape, dtype=dtype.data.dtype) tmp[0, ...] = 1. return UTPM(tmp) elif isinstance(dtype, Function): return dtype.pushforward(ones, [shape, dtype, order]) else: return numpy.ones(shape, dtype=type(dtype), order=order)
def probconv(p, x): """1-component BD(NBD)""" # def BDNBD(p, x): # return ((p[1] * (((1 - p[3]) / (1 - (p[3] * x))) ** p[2])) + (1. - p[1])) ** p[0] """1-component BD(NBD) X NBD""" # def BDNBD(p, x): # return (((p[1] * (((1. - p[3]) / (1. - (p[3] * x))) ** p[2])) + (1. - p[1])) ** p[0]) * (((1. - p[5]) / (1. - (p[5] * x))) ** p[4]) """2-component BD(NBD) """ # def BDNBD(p, x): # return (p[8] * (((p[1] * (((1 - p[3]) / (1 - (p[3] * x))) ** p[2])) + (1. - p[1])) ** p[0])) + ((1. - p[8]) * ((p[5] * (((1. - p[7]) / (1. - (p[7] * x))) ** p[6]) + (1. - p[5])) ** p[4])) """2-component BD(NBD) X NBD""" def BDNBD(p, x): return (p[12] * ((((p[1] * (((1. - p[3]) / (1. - (p[3] * x)))**p[2])) + (1. - p[1]))**p[0]) * (((1. - p[5]) / (1. - (p[5] * x)))**p[4]))) + ((1. - p[12]) * (( ((p[7] * (((1. - p[9]) / (1. - (p[9] * x)))**p[8])) + (1. - p[7]))**p[6]) * (((1. - p[11]) / (1. - (p[11] * x)))**p[10]))) D = len(x) P = 1 xderv = UTPM(np.zeros((D, P))) xderv.data[0, 0] = 0 #the value to evaluate Cj at corresponding to z=0 xderv.data[1, 0] = 1 derv = BDNBD(p, xderv) prob = derv.data[:, 0] return prob
def test_utpm_logdet_trace_expm(self): D, P, N = 3, 5, 4 x = 0.1 * UTPM(numpy.random.randn(D, P, N, N)) x = UTPM.dot(x.T, x) observed_logdet = UTPM.logdet(expm(x)) desired_logdet = UTPM.trace(x) assert_allclose(observed_logdet.data, desired_logdet.data)
def J_fcn(x_new, x, t_new, t, p): """ computes the Jacobian of F_fcn all inputs are double arrays """ y = UTPM(numpy.zeros((D, N, N))) y.data[0, :] = x_new y.data[1, :, :] = numpy.eye(N) F = F_fcn(y, x, t_new, t, p) return F.data[1, :, :].T
def __call__(self, z0=0): z = np.atleast_1d(z0).ravel() x = UTPM(np.zeros((self.n, 1, z.size), dtype=z.dtype)) x.data[0, 0, :] = z x.data[1, 0, :] = 1 y = self.fun(x) coefs = np.squeeze(y.data) return coefs
def _forward(self, x, *args, **kwds): x0 = np.asarray(x) shape = x0.shape P = 1 x = UTPM(np.zeros((self.n + 1, P) + shape)) x.data[0, 0] = x0 x.data[1, 0] = 1 z = self.fun(x, *args, **kwds) y = UTPM.as_utpm(z) return y.data[self.n, 0] * special.factorial(self.n)
def _forward(self, x, *args, **kwds): d, n = 2 + 1, x.size p = n y = UTPM(np.zeros((d, p, n))) y.data[0, :] = x.ravel() y.data[1, :] = np.eye(n) z0 = self.fun(y, *args, **kwds) z = UTPM.as_utpm(z0) H = z.data[2, ...] * 2 return H
def _forward(self, x, *args, **kwds): # return np.diag(super(Hessdiag, self)._forward(x, *args, **kwds)) D, Nm = 2 + 1, x.size P = Nm y = UTPM(np.zeros((D, P, Nm))) y.data[0, :] = x.ravel() y.data[1, :] = np.eye(Nm) z0 = self.f(y, *args, **kwds) z = UTPM.as_utpm(z0) H = z.data[2, ...] * 2 return H
def test_svd(self): D, P, M, N = 3, 1, 5, 2 A = UTPM(numpy.random.random((D, P, M, N))) U, s, V = svd(A) S = zeros((M, N), dtype=A) S[:N, :N] = diag(s) assert_array_almost_equal((dot(dot(U, S), V.T) - A).data, 0.) assert_array_almost_equal((dot(U.T, U) - numpy.eye(M)).data, 0.) assert_array_almost_equal((dot(U, U.T) - numpy.eye(M)).data, 0.) assert_array_almost_equal((dot(V.T, V) - numpy.eye(N)).data, 0.) assert_array_almost_equal((dot(V, V.T) - numpy.eye(N)).data, 0.)
def test_hyp0f1(self): """ check that algopy.special.hyp0f1 can be called with UTPM and Function instances as arguments """ b, x = 2., 3. y1 = hyp0f1(b, x) b, x = 2., UTPM(3. * numpy.ones((1, 1))) y2 = hyp0f1(b, x) assert_almost_equal(y1, y2.data[0, 0]) b, x = 2., Function(3.) y3 = hyp0f1(b, x) assert_almost_equal(y1, y3.x)
def test_expit(self): """ check that algopy.special.expit can be called with UTPM and Function instances as arguments """ x = 3. y1 = expit(x) x = UTPM(3. * numpy.ones((1, 1))) y2 = expit(x) assert_almost_equal(y1, y2.data[0, 0]) x = Function(3.) y3 = expit(x) assert_almost_equal(y1, y3.x)
def test_logit(self): """ check that algopy.special.logit can be called with UTPM and Function instances as arguments """ p = 0.5 x = p y1 = logit(x) x = UTPM(p * numpy.ones((1, 1))) y2 = logit(x) assert_almost_equal(y1, y2.data[0, 0]) x = Function(p) y3 = logit(x) assert_almost_equal(y1, y3.x)
def test_dpm_hyp1f1(self): """ check that algopy.special.dpm_hyp1f1 can be called with UTPM and Function instances as arguments """ a, b, x = 1., 2., 3. y1 = dpm_hyp1f1(a, b, x) a, b, x = 1., 2., UTPM(3. * numpy.ones((1, 1))) y2 = dpm_hyp1f1(a, b, x) assert_almost_equal(y1, y2.data[0, 0]) a, b, x = 1., 2., Function(3.) y3 = dpm_hyp1f1(a, b, x) assert_almost_equal(y1, y3.x)
def test_psi(self): """ check that algopy.special.polygamma can be called with UTPM and Function instances as arguments """ x = 1.2 y1 = psi(x) x = UTPM(1.2 * numpy.ones((1, 1))) y2 = psi(x) assert_almost_equal(y1, y2.data[0, 0]) x = Function(1.2) y3 = psi(x) assert_almost_equal(y1, y3.x)
def test_polygamma(self): """ check that algopy.special.polygamma can be called with UTPM and Function instances as arguments """ n, x = 2, 3. y1 = polygamma(n, x) n, x = 2, UTPM(3. * numpy.ones((1, 1))) y2 = polygamma(n, x) assert_almost_equal(y1, y2.data[0, 0]) n, x = 2, Function(3.) y3 = polygamma(n, x) assert_almost_equal(y1, y3.x)
def test_hyp2f0(self): """ check that algopy.special.hyp2f0 can be called with UTPM and Function instances as arguments """ # use small x to ameliorate convergence issues a1, a2, x = 1., 2., 0.03 y1 = hyp2f0(a1, a2, x) a1, a2, x = 1., 2., UTPM(0.03 * numpy.ones((1, 1))) y2 = hyp2f0(a1, a2, x) assert_almost_equal(y1, y2.data[0, 0]) a1, a2, x = 1., 2., Function(0.03) y3 = hyp2f0(a1, a2, x) assert_almost_equal(y1, y3.x)
def eigh1(A): """ generic implementation of eigh1 """ if isinstance(A, UTPM): return UTPM.eigh1(A) elif isinstance(A, Function): return Function.eigh1(A) elif isinstance(A, numpy.ndarray): A = UTPM(A.reshape((1,1) + A.shape)) retval = UTPM.eigh1(A) return retval[0].data[0,0], retval[1].data[0,0],retval[2] else: raise NotImplementedError('don\'t know what to do with this instance')
def test_dpm_hyp2f0(self): """ check that algopy.special.dpm_hyp2f0 can be called with UTPM and Function instances as arguments """ # these give hyp2f0 a chance of outputting real numbers a1, a2 = 1.5, 1.0 # use small x to ameliorate convergence issues x = 0.03 y1 = dpm_hyp2f0(a1, a2, x) x = UTPM(0.03 * numpy.ones((1, 1))) y2 = dpm_hyp2f0(a1, a2, x) assert_almost_equal(y1, y2.data[0, 0]) x = Function(0.03) y3 = dpm_hyp2f0(a1, a2, x) assert_almost_equal(y1, y3.x)
def test_interpolation(self): def f(x): return x[0] + x[1] + 3. * x[0] * x[1] + 7. * x[1] * x[1] + 17. * x[ 0] * x[0] * x[0] N = 2 D = 5 deg_list = [0, 1, 2, 3, 4] coeff_list = [] for n, deg in enumerate(deg_list): Gamma, rays = generate_Gamma_and_rays(N, deg) x = UTPM(numpy.zeros((D, ) + rays.shape)) #print x #print type(x) x.data[1, :, :] = rays y = f(x) coeff_list.append(numpy.dot(Gamma, y.data[deg])) assert_array_almost_equal([0], coeff_list[0]) assert_array_almost_equal([1, 1], coeff_list[1]) assert_array_almost_equal([0, 3, 7], coeff_list[2]) assert_array_almost_equal([17, 0, 0, 0], coeff_list[3])
import numpy from algopy import CGraph, Function, UTPM, dot, qr, eigh, inv, zeros def f(x, y): return dot(x.T, y) + dot((x * y - x).T, (x - y)) # create an UTPM instance D, N, M = 2, 3, 2 P = 2 * N x = UTPM(numpy.zeros((D, P, 2 * N, 1))) x.data[0, :] = numpy.random.rand(2 * N, 1) x.data[1, :, :, 0] = numpy.eye(P) y = x[N:] x = x[:N] # wrap the UTPM instance in a Function instance to trace all operations # that have x as an argument # create a CGraph instance that to store the computational trace cg = CGraph().trace_on() x = Function(x) y = Function(y) z = f(x, y) cg.trace_off() # define dependent and independent variables in the computational procedure cg.independentFunctionList = [x, y] cg.dependentFunctionList = [z]
import numpy from numpy import sin, cos from algopy import UTPM, zeros D, P = 4, 1 x = UTPM(numpy.zeros((D, P, 2))) x.data[0, :, 0] = 1 p = UTPM(numpy.zeros((D, P))) p.data[0, :] = 3 p.data[1, :] = 1 def f(x): retval = x.zeros_like() retval[0] = x[1] retval[1] = -p * x[0] return retval # compute AD solution ts = numpy.linspace(0, 2 * numpy.pi, 2000) x_list = [x.data.copy()] for nts in range(ts.size - 1): h = ts[nts + 1] - ts[nts] x = x + h * f(x) x_list.append(x.data.copy()) # analytical solution def x_analytical(t, p): return numpy.cos(numpy.sqrt(p) * t)
import numpy from algopy import CGraph, Function, UTPM, dot, qr, eigh, inv, zeros def f(x, N): return dot(x[:N], x[N:]) * x[N:] - x[:N] * (x[:N] - x[N:]) # create a CGraph instance that to store the computational trace cg = CGraph() # create an UTPM instance D, N, M = 2, 3, 2 P = N A = UTPM(numpy.zeros((D, P, M, N))) x = UTPM(numpy.zeros((D, P, N, 1))) x.data[0, :] = numpy.random.rand(N, 1) A.data[0, :] = numpy.random.rand(M, N) x.data[1, :, :, 0] = numpy.eye(P) x = Function(x) A = Function(A) # wrap the UTPM instance in a Function instance to trace all operations # that have x as an argument # x = Function(x) y = dot(A, x)
M, N = J1.shape K, N = J2.shape Q, R = qr_full(J2.T) Q2 = Q[:, K:].T J1_tilde = dot(J1, Q2.T) Q, R = qr(J1_tilde) V = solve(R.T, Q2) return dot(V.T, V) # dimensions of the involved matrices D, P, M, N, K, Nx = 2, 1, 5, 3, 1, 1 # trace the function evaluation of METHOD 1: nullspace method cg1 = CGraph() J1 = Function(UTPM(numpy.random.rand(*(D, P, M, N)))) J2 = Function(UTPM(numpy.random.rand(*(D, P, K, N)))) C = eval_covariance_matrix_qr(J1, J2) y = C[0, 0] cg1.trace_off() cg1.independentFunctionList = [J1, J2] cg1.dependentFunctionList = [y] print('covariance matrix: C =\n', C) # trace the function evaluation of METHOD 2: naive method (potentially numerically unstable) cg2 = CGraph() J1 = Function(J1.x) J2 = Function(J2.x) C2 = eval_covariance_matrix_naive(J1, J2) y = C2[0, 0] cg2.trace_off()
import numpy from algopy import UTPM # symmetric eigenvalue decomposition, forward UTPM D, P, M, N = 3, 1, 4, 4 Q, R = UTPM.qr(UTPM(numpy.random.rand(D, P, M, N))) l = UTPM(numpy.random.rand(*(D, P, N))) l.data[0, 0, :4] = [1, 1, 2, 3] l.data[1, 0, :4] = [0, 0, 3, 4] l.data[2, 0, :4] = [1, 2, 5, 6] L = UTPM.diag(l) B = UTPM.dot(Q, UTPM.dot(L, Q.T)) print('B = \n', B) l2, Q2 = UTPM.eigh(B) print('l2 - l =\n', l2 - l)
import numpy from algopy import CGraph, Function, UTPM, dot, qr, eigh, inv, zeros def f(y): retval = zeros((3, 1), dtype=y) retval[0, 0] = numpy.log(dot(y.T, y)) retval[1, 0] = numpy.exp(dot(y.T, y)) retval[2, 0] = numpy.exp(dot(y.T, y)) - numpy.log(dot(y.T, y)) return retval D, Nm = 2, 40 P = Nm y = UTPM(numpy.zeros((2, P, Nm))) y.data[0, :] = numpy.random.rand(Nm) y.data[1, :] = numpy.eye(Nm) # print f(y) J = f(y).data[1, :, :, 0] print('Jacobian J(y) = \n', J) C_epsilon = 0.3 * numpy.eye(Nm) print(J.shape) C = dot(J.T, dot(C_epsilon, J)) print('Covariance matrix of z: C = \n', C)
import numpy from algopy import CGraph, Function, UTPM, dot, qr, eigh, inv, solve # first order derivatives, one directional derivative # D - 1 is the degree of the Taylor polynomial # P directional derivatives at once # M number of rows of J1 # N number of cols of J1 # K number of rows of J2 (must be smaller than N) D, P, M, N, K, Nx = 2, 1, 100, 3, 1, 1 # METHOD 1: nullspace method cg1 = CGraph() J1 = Function(UTPM(numpy.random.rand(*(D, P, M, N)))) J2 = Function(UTPM(numpy.random.rand(*(D, P, K, N)))) Q, R = Function.qr_full(J2.T) Q2 = Q[:, K:].T J1_tilde = dot(J1, Q2.T) Q, R = qr(J1_tilde) V = solve(R.T, Q2) C = dot(V.T, V) cg1.trace_off() cg1.independentFunctionList = [J1, J2] cg1.dependentFunctionList = [C] print('covariance matrix: C =\n', C)
import numpy from numpy import sin, cos from algopy import UTPM def f(x): return sin(cos(x) + sin(x)) D = 100 P = 1 x = UTPM(numpy.zeros((D, P))) x.data[0, 0] = 0.3 x.data[1, 0] = 1 y = f(x) print('coefficients of y =', y.data[:, 0]) import matplotlib.pyplot as pyplot import os zs = numpy.linspace(-1, 2, 100) ts = zs - 0.3 fzs = f(zs) for d in [2, 4, 10, 50, 100]: yzs = numpy.polyval(y.data[:d, 0][::-1], ts) pyplot.plot(zs, yzs, label='%d\'th order approx.' % (d - 1)) pyplot.plot([0.3], f([0.3]), 'ro') pyplot.plot(zs, fzs, 'k.') pyplot.grid()
Low rank matrices A are built by low rank updates, then it is tested how big the residuals res_1 = QR - A and res_2 = Q.T Q - I and res_3 = P_L ( R) """ from numpy.testing import * import numpy import matplotlib.pyplot as pyplot import prettyplotting from algopy import UTPM, qr, dot, triu D, P, N = 4, 1, 3 x1 = UTPM(numpy.random.rand(D, P, N, 1)) x2 = UTPM(numpy.random.rand(D, P, N, 1)) x3 = UTPM(numpy.random.rand(D, P, N, 1)) def alpha(beta): return numpy.array([1., 2.**(-beta / 2.), 2.**(-beta)]) # create matrix A by lowrank updates, alpha triggeres what the rank is res_1_list = [] res_2_list = [] res_3_list = [] betas = list(range(0, 200, 10))
""" This example shows that most computations can be performed by numpy functions on arrays of UTPM objects. Just bear in mind that is much faster use UTPM instances of matrices than numpy.ndarrays with UTPM elements. """ import numpy, os from algopy import CGraph, Function, UTPM, dot, qr, eigh, inv N, D, P = 2, 2, 1 cg = CGraph() x = numpy.array([Function(UTPM(numpy.random.rand(*(D, P)))) for n in range(N)]) A = numpy.outer(x, x) A = numpy.exp(A) y = numpy.dot(A, x) cg.independentFunctionList = list(x) cg.dependentFunctionList = list(y) cg.plot(os.path.join(os.path.dirname(__file__), 'numpy_dot_graph.svg'))
# set up meta data d = -1.64601435 mol = [(6, (0.0, 0.0, 0.20165898)), (8, (0.0, 0.0, d))] ne = 14 system = System_mol(mol, basis, ne, shifted=False, mol_name='agua') manager = Tasks(system, name='h2_sto_3g', verbose=True) # calculate SCF energy #print 'EnergyH2', diffiqult.Energy.rhfenergy(*manager._energy_args()) # calculate derivatives args = list(manager._energy_args()) D = 20 P = 1 x = UTPM(np.zeros((D, P, 2, 1))) x.data[0, 0] = 0 x.data[1, 0] = 1 args[4] = [1 + x, 1 - x] args[-1] = x args = tuple(args) y = diffiqult.Energy.rhfenergy(*(args)) #print 'derivatives', y.data # calculate target energy heargs = list(manager._energy_args()) heargs[4] = [7, 7] #print 'EnergyTarget', diffiqult.Energy.rhfenergy(*heargs)