def test_dK_dtheta_5(): n = 4 theta = np.array( [ 2.59193443e-02, 0.00000000e+00, 6.83797216e-07, 3.08837678e-03, 0.00000000e+00, 2.56956907e-02, -1.48051536e+00, -1.51759911e+00, -1.34983215e+00, -1.22431771e+00]) size = len(theta) dK1 = np.zeros((size, n, n)) dK2 = np.zeros((size, n, n)) dK3 = np.zeros((size, n, n)) for u in range(size): _ratematrix.dK_dtheta_ij(theta, n, u, A=None, out=dK1[u, :, :]) for i in range(n): for j in range(n): _ratematrix.dK_dtheta_u(theta, n, i, j, out=dK2[:, i, j]) for i in range(n): for j in range(n): dKij = np.zeros(size) _ratematrix.dK_dtheta_u(theta, n, i, j, out=dKij) dK3[:, i, j] = dKij np.testing.assert_almost_equal(dK1, dK2) np.testing.assert_almost_equal(dK1, dK3) np.testing.assert_almost_equal(dK2, dK3)
def test_dK_dtheta_5(): n = 4 theta = np.array([ 2.59193443e-02, 0.00000000e+00, 6.83797216e-07, 3.08837678e-03, 0.00000000e+00, 2.56956907e-02, -1.48051536e+00, -1.51759911e+00, -1.34983215e+00, -1.22431771e+00 ]) size = len(theta) dK1 = np.zeros((size, n, n)) dK2 = np.zeros((size, n, n)) dK3 = np.zeros((size, n, n)) for u in range(size): _ratematrix.dK_dtheta_ij(theta, n, u, A=None, out=dK1[u, :, :]) for i in range(n): for j in range(n): _ratematrix.dK_dtheta_u(theta, n, i, j, out=dK2[:, i, j]) for i in range(n): for j in range(n): dKij = np.zeros(size) _ratematrix.dK_dtheta_u(theta, n, i, j, out=dKij) dK3[:, i, j] = dKij np.testing.assert_almost_equal(dK1, dK2) np.testing.assert_almost_equal(dK1, dK3) np.testing.assert_almost_equal(dK2, dK3)
def test_dK_dtheta_4(): # check that the dot product part of dK_dtheta_u works n = 4 theta = example_theta(n) A = random.randn(len(theta), len(theta)) for i in range(n): for j in range(n): grad = np.zeros(len(theta)) _ratematrix.dK_dtheta_u(theta, n, i, j, out=grad) gradprod1 = np.dot(grad, A) gradprod2 = np.zeros(len(theta)) grad2 = np.zeros(len(theta)) _ratematrix.dK_dtheta_u(theta, n, i, j, out=grad2, A=A, out2=gradprod2) np.testing.assert_almost_equal(grad, grad2) np.testing.assert_almost_equal(gradprod1, gradprod2) np.testing.assert_almost_equal(np.dot(grad2, A), gradprod2)
def test_dK_dtheta_3(): # test dK_dtheta_ij vs dK_dtheta_u. both return slices of the same 3D # tensor, so by repeated calls to both functions we can build the whole # tensor using both approaches and check that they're equal. for n in [3, 4]: theta = example_theta(n) dKuij1 = np.zeros((len(theta), n, n)) dKuij2 = np.zeros((len(theta), n, n)) for u in range(len(theta)): _ratematrix.dK_dtheta_ij(theta, n, u, None, dKuij1[u]) for i in range(n): for j in range(n): _ratematrix.dK_dtheta_u(theta, n, i, j, out=dKuij2[:, i, j]) np.testing.assert_array_almost_equal(dKuij1, dKuij2)