def test_vec_solve(): mu_12 = Tensor('mu_12', rank=0) p_1, p_2 = map(lambda x: Tensor(x, rank=1), ['p_1', 'p_2']) A = Tensor('A', rank=2) print solve_vec_eqn( -mu_12 * (T(p_1) * A * p_2) - mu_12 * (T(p_2) * A * p_1) + (T(p_2) * A * p_1), mu_12)
def testZero(): A = Tensor('A', 2) a = Tensor('a', 1) alpha = Tensor('alpha', 0) Z = Tensor('0', 2) z = Tensor('0', 1) z_0 = Tensor('0', 0) assert(A + Z == A) assert(Z + A == A) assert(A * Z == Z) assert(Z * A == Z) assert(a + z == a) assert(z + a == a) assert(A * z == z) assert(T(a) * z == z_0) assert(T(z) * a == z_0) assert(z * T(a) == Z) assert(a * T(z) == Z) assert(alpha + z_0 == alpha) assert(alpha * z_0 == z_0) assert(alpha * z == z) assert(alpha * Z == Z) assert(z_0 * A == Z) assert(z_0 * a == z) assert(A + z_0 == A) assert(z_0 + A == A)
def get_chronos_eqns_1(): delta_1, omega_2, pi_1, pi_2, gamma_2, mu_12 = \ map(lambda x: Tensor(x, rank=0), ['delta_1', 'omega_2', 'pi_1', 'pi_2', 'gamma_2', 'mu_12']) r_1, r_2, q_1, q_2, p_1, p_2, s_1, s_2 = \ map(lambda x: Tensor(x, rank=1), ['r_1', 'r_2', 'q_1', 'q_2', 'p_1', 'p_2', 's_1', 's_2']) A, R_0, P_0 = map(lambda x: Tensor(x, rank=2), ['A', 'R_0', 'P_0']) eqns = [ r_2 - r_1 - delta_1 * q_1, q_2 - A * p_2, p_2 - r_2 + p_1 * mu_12, q_2 - A * r_2 + q_1 * mu_12, omega_2 - T(r_2) * r_2, pi_2 - T(p_2) * A * p_2, T(R_0) * r_2, T(r_1) * r_2, T(P_0) * A * p_2, T(p_1) * A * p_2, T(p_2) * A * p_2 - T(r_2) * A * r_2 + T(mu_12) * T(p_1) * A * p_1 * mu_12, ] knowns = [p_1, r_1, q_1, pi_1, A, R_0, P_0] return eqns, knowns
def test_latex_print(): assert (latex_print(-mu_12 * p_1 + r_2) == "-\mu_{12} p_1 + r_2") assert (latex_print(delta_1 * A * p_1 + r_1) == "\delta_1 A p_1 + r_1") assert (latex_print(delta_1 * p_1 + x_1) == "\delta_1 p_1 + x_1") assert(latex_print((T(r_1) * r_1) / (T(r_1) * A * p_1)) == \ "{r_1}^t r_1 1.0 / ({p_1}^t A r_1)") assert(latex_print((T(p_1) * A * r_2) / (T(p_1) * A * p_1)) == \ "{p_1}^t A r_2 1.0 / ({p_1}^t A p_1)")
def test_mul_inverse(): delta_11 = Tensor('delta_11', 0) r_1, p_1 = map(lambda x: Tensor(x, 1), ['r_1', 'p_1']) A = Tensor('A', 2) a = Tensor('a', 0) expr = (-(T(p_1)*A*p_1)*(T(p_1)*A*r_1)*a + (T(p_1)*A*r_1)) expr = expr.subs(a, (T(p_1)*A*p_1)**-1) assert(simplify(expr) == S(0))
def test_numpy_print(): assert (numpy_print(-mu_12 * p_1 + r_2) == "-dot(mu_12, p_1) + r_2") assert (numpy_print(delta_1 * A * p_1 + r_1) == "dot(delta_1, dot(A, p_1)) + r_1") assert (numpy_print(delta_1 * p_1 + x_1) == "dot(delta_1, p_1) + x_1") assert(numpy_print((T(r_1) * r_1) / (T(r_1) * A * p_1)) == \ "dot(dot((r_1).transpose(), r_1), 1.0 / (dot(dot((p_1).transpose(), A), r_1)))") assert(numpy_print((T(p_1) * A * r_2) / (T(p_1) * A * p_1)) == \ "dot(dot(dot((p_1).transpose(), A), r_2), 1.0 / (dot(dot((p_1).transpose(), A), p_1)))")
def test_chronos_cg(): skip("Test takes too long") delta_1, omega_2, pi_1, pi_2, mu_12 = map(lambda x: Tensor(x, rank=0), \ ['delta_1', 'omega_2', 'pi_1', 'pi_2', 'mu_12']) r_1, r_2, q_1, q_2, p_1, p_2, x_1, x_2 = map(lambda x: Tensor(x, rank=1), \ ['r_1', 'r_2', 'q_1', 'q_2', 'p_1', 'p_2', 'x_1', 'x_2']) A, R_0, P_0 = map(lambda x: Tensor(x, rank=2), ['A', 'R_0', 'P_0']) # Specify which variables are known knowns = [pi_1, p_1, r_1, q_1, x_1, A, R_0, P_0] # Now try the chronos variant and repeat. chronos_eqns = [ r_2 - r_1 - delta_1 * q_1, q_2 - A * p_2, p_2 - r_2 + p_1 * mu_12, q_2 - A * r_2 + q_1 * mu_12, x_2 - x_1 - delta_1 * p_1, omega_2 - T(r_2) * r_2, pi_2 - T(p_2) * A * p_2, T(R_0) * r_2, T(r_1) * r_2, T(P_0) * A * p_2, T(p_1) * A * p_2, T(p_2) * A * p_2 - T(r_2) * A * r_2 + T(mu_12) * pi_1 * mu_12, ] run_cg_algorithms(chronos_eqns, knowns)
def testUpdate(): k = Symbol('k') A = Tensor('A', 2) A_TL = A.update(l_ind='TL') A_TL_2 = A_TL.update(u_ind='2', rank=2) A_01 = A_TL.update(l_ind='01', shape=(k, k)) a_02 = T(A_TL.update(l_ind='02', shape=(1, k), rank=1)) assert(A_TL == Tensor('A_TL', 2)) assert(A_TL_2 == Tensor('A_TL^2', 2)) assert(A_01 == Tensor('A_01', 2, shape=(k, k))) assert(a_02 == T(Tensor('a_02', 1, shape=(1, k))))
def test_overdetermined_forward_solve(): pi_1, pi_2, mu_12 = \ map(lambda x: Tensor(x, rank=0), ['pi_1', 'pi_2', 'mu_12']) r_2, p_1, p_2 = \ map(lambda x: Tensor(x, rank=1), ['r_2', 'p_1', 'p_2']) A = Tensor('A', rank=2) eqns = [ pi_2 - T(r_2) * A * r_2 + T(mu_12) * pi_1 * mu_12, p_2 - r_2 + p_1 * mu_12, pi_2 - T(p_2) * A * p_2, ] return forward_solve(eqns, [r_2, p_1, mu_12, A, pi_1], True)
def testInverseMul(): ans = 5*I A = Tensor('A', rank=2, has_inv=True) A_I = Inverse(A) e = 5*A assert(A_I * e == ans) assert(e*A_I == ans) e = 5*A_I assert(A * e == ans) assert(e*A == ans) ans = 5*one p = Tensor('p', rank=1) pAp = T(p)*A*p pAp_I = Inverse(pAp) e = 5*pAp assert(e*pAp_I == ans) assert(pAp_I*e == ans) e = 5*pAp_I assert(e*pAp == ans) assert(pAp*e == ans) e = 5*pAp*A assert(A_I*e == 5*pAp*I) assert(e*pAp_I == 5*A)
def test_reg_cg(): delta_1, mu_12 = map(lambda x: Tensor(x, rank=0), ['delta_1', 'mu_12']) r_1, r_2, p_1, p_2, x_1, x_2 = map(lambda x: Tensor(x, rank=1), \ ['r_1', 'r_2', 'p_1', 'p_2', 'x_1', 'x_2']) A = Tensor('A', rank=2) # Specify which variables are known knowns = [p_1, r_1, x_1, A] # Specify the CG eqns (coming from a 4x4 PME) cg_eqns = [ delta_1 * A * p_1 - r_1 + r_2, p_2 - r_2 + p_1 * mu_12, x_2 - x_1 - delta_1 * p_1, T(r_1) * r_2, T(p_1) * A * p_2, ] run_cg_algorithms(cg_eqns, knowns)
def test_algebra(): delta_1, omega_2, pi_1, pi_2, gamma_2, mu_12 = \ map(lambda x: Tensor(x, rank=0), ['delta_1', 'omega_2', 'pi_1', 'pi_2', 'gamma_2', 'mu_12']) r_1, r_2, q_1, q_2, p_1, p_2, s_1, s_2 = \ map(lambda x: Tensor(x, rank=1), ['r_1', 'r_2', 'q_1', 'q_2', 'p_1', 'p_2', 's_1', 's_2']) A, R_0, P_0 = map(lambda x: Tensor(x, rank=2), ['A', 'R_0', 'P_0']) eqn = -2 * (T(p_1) * A * r_2) / (T(p_1) * A * p_1) * (T(p_1) * A * r_2) eqn -= -2 * (T(p_1) * A * r_2) * (T(p_1) * A * r_2) / (T(p_1) * A * p_1) assert (eqn == S(0)) assert (T(p_1) * A * r_2 == T(r_2) * A * p_1)
def test_algebra(): eqn = -2 * (T(p_1) * A * r_2) / (T(p_1) * A * p_1) * (T(p_1) * A * r_2) eqn -= -2 * (T(p_1) * A * r_2) * (T(p_1) * A * r_2) / (T(p_1) * A * p_1) assert(eqn == S(0)) assert(T(p_1) * A * r_2 == T(r_2) * A * p_1)