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_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 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 test_5 (): q, r, s = map(lambda x: Tensor(x, rank=1), 'qrs') s_t = Transpose(s) delta = Tensor('delta', rank=0) eqn1 = r - s - q * delta eqn2 = s_t * r print assump_solve([eqn1, eqn2], [r, q])
def test_forward_solve(): q, r, s = map(lambda x: Tensor(x, rank=1), 'qrs') delta = Tensor('delta', rank=0) eqn1 = s + q eqn2 = delta * r - q sol_dict = forward_solve([eqn1, eqn2], [delta, r]) print "sol_dict should be: {q: delta*r, s: -q }" print " actual:", sol_dict
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_3 (): q, r, s = map(lambda x: Tensor(x, rank=1), 'qrs') s_t = Transpose(s) delta = Tensor('delta', rank=0) eqn1 = r - s - q * delta eqn2 = s_t * r print forward_solve([eqn1, eqn2], [r, s, q]) print forward_solve([eqn1, eqn2], [delta, s, q]) print forward_solve([eqn2, eqn1], [delta, s, q])
def test_0 (): A = Tensor('A', rank=2, has_inv=True) x = Tensor('x', rank=1) y = Tensor('y', rank=1) alpha = Tensor('alpha', rank=0) beta = Tensor('beta', rank=0) eqn = alpha * A * x + beta * y print eqn, "rank:", expr_rank(eqn) print "solution for y", solve_vec_eqn(eqn, y) print "solution for x", solve_vec_eqn(eqn, x)
def test_backward_sub(): q, r, s = map(lambda x: Tensor(x, rank=1), 'qrs') s_t = Transpose(s) delta = Tensor('delta', rank=0) eqn1 = r - s - q * delta eqn2 = s_t * r sol_dict, failed_var = backward_sub([eqn1, eqn2], [q, s], [r, delta]) print sol_dict, failed_var sol_dict, failed_var = backward_sub([eqn1, eqn2], [q, s], [delta, r]) print sol_dict, failed_var
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 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 testOne(): A = Tensor('A', 2) a = Tensor('a', 1) alpha = Tensor('alpha', 0) ONE = Tensor('1', 2) one = Tensor('1', 0) assert (A * ONE == A) assert (ONE * A == A) assert (A * one == A) assert (one * A == A) assert (a * one == a) assert (one * a == a) assert (alpha * one == alpha) assert (one * alpha == alpha)
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 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_3 (): 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, pi_2 - T(r_2) * A * r_2 + T(mu_12) * pi_1 * mu_12, T(R_0) * r_2, T(r_1) * r_2, T(P_0) * A * p_2, T(p_1) * A * p_2, ] knowns = [p_1, r_1, q_1, pi_1, A, R_0, P_0] return eqns, knowns
def test_sol_without_recomputes (): a, b, c, d = map(lambda x: Tensor(x, rank=0), 'abcd') sol_dict = {a: b + c*d, b: c*d} ord_unk = [a, b] assert(sol_without_recomputes((sol_dict, ord_unk)) == (sol_dict, ord_unk)) sol_dict = {a: b + c*d, b: c*d} ord_unk = [b, a] assert(sol_without_recomputes((sol_dict, ord_unk)) is None) sol_dict = {a: set([b + c*d]), b: set([c*d])} ord_unk = [b, a] assert(sol_without_recomputes((sol_dict, ord_unk)) is None) sol_dict = {a: set([b + c*d, b*d]), b: set([c*d])} ord_unk = [b, a] assert(sol_without_recomputes((sol_dict, ord_unk)) == \ ({a: set([b*d]), b: set([c*d])}, ord_unk)) sol_dict = {a: set([b + c*d, b*d]), b: set([c*d])} ord_unk = [a, b] assert(sol_without_recomputes((sol_dict, ord_unk)) == (sol_dict, ord_unk))
def test_2 (): from sympy import expand, S A = Tensor('A', rank=2) A_t = Transpose(A) B = Tensor('B', rank=2) x = Tensor('x', rank=1) y = Tensor('y', rank=1) x_t = Transpose(x) y_t = Transpose(y) alpha = Tensor('alpha', rank=0) beta = Tensor('beta', rank=0) teqn = Transpose((A + B) * x) print "teqn.expand: ", teqn.expand(), "should be x'*A' + x'*B'" print expand(S(2) * beta * (alpha * A + beta * B) * x), "should be 2*beta**2*B*x + 2*alpha*beta*A*x" print expr_shape(expand(S(2) * A * x)), "should be (n, 1)"
def iterative_arg(name, rank, *args, **kws): ten = Tensor(name, rank) arg_src = kws.get("arg_src", PObj.ARG_SRC["Input"]) if type(arg_src) is str: arg_src = PObj.ARG_SRC[arg_src] part_suffix = kws.get("part_suffix", None) part_fun = kws.get("part_fun", None) repart_fun = kws.get("repart_fun", None) fuse_fun = kws.get("fuse_fun", None) if part_suffix is None: part_suffix = PART_SUFFIX_DEFAULT if part_fun is not None: part_suffix = get_part_suffix(part_fun.__name__) elif repart_fun is not None: part_suffix = get_part_suffix(repart_fun.__name__) elif fuse_fun is not None: part_suffix = get_part_suffix(fuse_fun.__name__) if part_fun is None: try: part_fun = getattr(iterative_prules, "Part_" + part_suffix)() except AttributeError: raise ValueError("Unable to find partition function for argument.") if repart_fun is None: try: repart_fun = getattr(iterative_prules, "Repart_" + part_suffix)() except AttributeError: raise ValueError( "Unable to find repartition function for argument.") if fuse_fun is None: try: fuse_fun = getattr(iterative_prules, "Fuse_" + part_suffix)() except AttributeError: raise ValueError("Unable to find fuse function for argument.") return PObj(ten, part_fun=part_fun, repart_fun=repart_fun, fuse_fun=fuse_fun, arg_src=arg_src)
from ignition.flame.tensors import (numpy_print, latex_print, T, Tensor) 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, x_1 = \ map(lambda x: Tensor(x, rank=1), ['r_1', 'r_2', 'q_1', 'q_2', 'p_1', 'p_2', 's_1', 's_2', 'x_1']) A, R_0, P_0 = map(lambda x: Tensor(x, rank=2), ['A', 'R_0', 'P_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_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 testZeroOne(): ZERO = Tensor('0', 2) One = Tensor('1', 1) k = Tensor('k', 1) assert (k - ZERO * One == k)
def test_numerator (): a, b, c = map(lambda x: Tensor(x, rank=0), 'abc') expr = (a + b) / c sol = solve_vec_eqn(expr, a) assert(sol == -b)
def test_cyclic_solve (): a, b = map(lambda x: Tensor(x, rank=0), 'ab') assert(backward_sub([a + b], [], [a, b]) == (None, b))