def test_ufunc_support(): f = Function("f") g = Function("g") x = IndexedBase("x") y = IndexedBase("y") i, j = Idx("i"), Idx("j") a = symbols("a") assert get_indices(f(x[i])) == (set([i]), {}) assert get_indices(f(x[i], y[j])) == (set([i, j]), {}) assert get_indices(f(y[i]) * g(x[i])) == (set(), {}) assert get_indices(f(a, x[i])) == (set([i]), {}) assert get_indices(f(a, y[i], x[j]) * g(x[i])) == (set([j]), {}) assert get_indices(g(f(x[i]))) == (set([i]), {}) assert get_contraction_structure(f(x[i])) == {None: set([f(x[i])])} assert get_contraction_structure(f(y[i]) * g(x[i])) == { (i, ): set([f(y[i]) * g(x[i])]) } assert get_contraction_structure(f(y[i]) * g(f(x[i]))) == { (i, ): set([f(y[i]) * g(f(x[i]))]) } assert get_contraction_structure(f(x[j], y[i]) * g(x[i])) == { (i, ): set([f(x[j], y[i]) * g(x[i])]) }
def test_ufunc_support(): f = Function('f') g = Function('g') x = IndexedBase('x') y = IndexedBase('y') i, j, k = Idx('i'), Idx('j'), Idx('k') a = symbols('a') assert get_indices(f(x[i])) == (set([i]), {}) assert get_indices(f(x[i], y[j])) == (set([i, j]), {}) assert get_indices(f(y[i]) * g(x[i])) == (set(), {}) assert get_indices(f(a, x[i])) == (set([i]), {}) assert get_indices(f(a, y[i], x[j]) * g(x[i])) == (set([j]), {}) assert get_indices(g(f(x[i]))) == (set([i]), {}) assert get_contraction_structure(f(x[i])) == {None: set([f(x[i])])} assert get_contraction_structure(f(y[i]) * g(x[i])) == { (i, ): set([f(y[i]) * g(x[i])]) } assert get_contraction_structure(f(y[i]) * g(f(x[i]))) == { (i, ): set([f(y[i]) * g(f(x[i]))]) } assert get_contraction_structure(f(x[j], y[i]) * g(x[i])) == { (i, ): set([f(x[j], y[i]) * g(x[i])]) }
def test_contraction_structure_Mul_and_Pow(): x = IndexedBase('x') y = IndexedBase('y') i, j, k = Idx('i'), Idx('j'), Idx('k') i_ji = x[i]**(y[j] * x[i]) assert get_contraction_structure(i_ji) == {None: set([i_ji])} ij_i = (x[i] * y[j])**(y[i]) assert get_contraction_structure(ij_i) == {None: set([ij_i])} j_ij_i = x[j] * (x[i] * y[j])**(y[i]) assert get_contraction_structure(j_ij_i) == {(j, ): set([j_ij_i])} j_i_ji = x[j] * x[i]**(y[j] * x[i]) assert get_contraction_structure(j_i_ji) == {(j, ): set([j_i_ji])} ij_exp_kki = x[i] * y[j] * exp(y[i] * y[k, k]) result = get_contraction_structure(ij_exp_kki) expected = { (i, ): set([ij_exp_kki]), ij_exp_kki: [{ None: set([exp(y[i] * y[k, k])]), exp(y[i] * y[k, k]): [{ None: set([y[i] * y[k, k]]), y[i] * y[k, k]: [{ (k, ): set([y[k, k]]) }] }] }] } assert result == expected
def test_issue_12533(): d = IndexedBase('d') assert IndexedBase(range(5)) == Range(0, 5, 1) assert d[0].subs(Symbol("d"), range(5)) == 0 assert d[0].subs(d, range(5)) == 0 assert d[1].subs(d, range(5)) == 1 assert Indexed(Range(5), 2) == 2
def test_contraction_structure_Pow_in_Pow(): x = IndexedBase("x") y = IndexedBase("y") z = IndexedBase("z") i, j, k = Idx("i"), Idx("j"), Idx("k") ii_jj_kk = x[i, i]**y[j, j]**z[k, k] expected = { None: set([ii_jj_kk]), ii_jj_kk: [ { (i, ): set([x[i, i]]) }, { None: set([y[j, j]**z[k, k]]), y[j, j]**z[k, k]: [{ (j, ): set([y[j, j]]) }, { (k, ): set([z[k, k]]) }], }, ], } assert get_contraction_structure(ii_jj_kk) == expected
def test_Indexed_subs(): i, j, k = symbols('i j k', integer=True) a, b = symbols('a b') A = IndexedBase(a) B = IndexedBase(b) assert A[i, j] == B[i, j].subs(b, a) assert A[i, j] == A[i, k].subs(k, j)
def test_contraction_structure_Add_in_Pow(): x = IndexedBase('x') y = IndexedBase('y') i, j, k = Idx('i'), Idx('j'), Idx('k') s_ii_jj_s = (1 + x[i, i])**(1 + y[j, j]) expected = { None: set([s_ii_jj_s]), s_ii_jj_s: [{ None: set([S.One]), (i, ): set([x[i, i]]) }, { None: set([S.One]), (j, ): set([y[j, j]]) }] } result = get_contraction_structure(s_ii_jj_s) assert result == expected s_ii_jk_s = (1 + x[i, i])**(1 + y[j, k]) expected_2 = { None: set([(x[i, i] + 1)**(y[j, k] + 1)]), s_ii_jk_s: [{ None: set([S.One]), (i, ): set([x[i, i]]) }] } result_2 = get_contraction_structure(s_ii_jk_s) assert result_2 == expected_2
def test_IndexedBase_subs(): i = symbols('i', integer=True) a, b = symbols('a b') A = IndexedBase(a) B = IndexedBase(b) assert A[i] == B[i].subs(b, a) C = {1: 2} assert C[1] == A[1].subs(A, C)
def test_get_contraction_structure_basic(): x = IndexedBase('x') y = IndexedBase('y') i, j = Idx('i'), Idx('j') assert get_contraction_structure(x[i]*y[j]) == {None: set([x[i]*y[j]])} assert get_contraction_structure(x[i] + y[j]) == {None: set([x[i], y[j]])} assert get_contraction_structure(x[i]*y[i]) == {(i,): set([x[i]*y[i]])} assert get_contraction_structure(1 + x[i]*y[i]) == {None: set([S.One]), (i,): set([x[i]*y[i]])} assert get_contraction_structure(x[i]**y[i]) == {None: set([x[i]**y[i]])}
def test_IndexedBase_assumptions_inheritance(): I = Symbol('I', integer=True) I_inherit = IndexedBase(I) I_explicit = IndexedBase('I', integer=True) assert I_inherit.is_integer assert I_explicit.is_integer assert I_inherit.label.is_integer assert I_explicit.label.is_integer assert I_inherit == I_explicit
def test_cse_Indexed(): len_y = 5 y = IndexedBase('y', shape=(len_y, )) x = IndexedBase('x', shape=(len_y, )) i = Idx('i', len_y - 1) expr1 = (y[i + 1] - y[i]) / (x[i + 1] - x[i]) expr2 = 1 / (x[i + 1] - x[i]) replacements, reduced_exprs = cse([expr1, expr2]) assert len(replacements) > 0
def test_get_indices_add(): x = IndexedBase('x') y = IndexedBase('y') A = IndexedBase('A') i, j, k = Idx('i'), Idx('j'), Idx('k') assert get_indices(x[i] + 2*y[i]) == (set([i,]), {}) assert get_indices(y[i] + 2*A[i, j]*x[j]) == (set([i,]), {}) assert get_indices(y[i] + 2*(x[i] + A[i, j]*x[j])) == (set([i,]), {}) assert get_indices(y[i] + x[i]*(A[j, j] + 1)) == (set([i,]), {}) assert get_indices(y[i] + x[i]*x[j]*(y[j] + A[j, k]*x[k])) == (set([i,]), {})
def test_get_contraction_structure_complex(): x = IndexedBase('x') y = IndexedBase('y') A = IndexedBase('A') i, j, k = Idx('i'), Idx('j'), Idx('k') expr1 = y[i] + A[i, j]*x[j] d1 = {None: set([y[i]]), (j,): set([A[i, j]*x[j]])} assert get_contraction_structure(expr1) == d1 expr2 = expr1*A[k, i] + x[k] d2 = {None: set([x[k]]), (i,): set([expr1*A[k, i]]), expr1*A[k, i]: [d1]} assert get_contraction_structure(expr2) == d2
def test_contraction_structure_simple_Pow(): x = IndexedBase('x') y = IndexedBase('y') i, j, k = Idx('i'), Idx('j'), Idx('k') ii_jj = x[i, i]**y[j, j] assert get_contraction_structure(ii_jj) == { None: set([ii_jj]), ii_jj: [ {(i,): set([x[i, i]])}, {(j,): set([y[j, j]])} ] }
def test_get_indices_Pow(): x = IndexedBase('x') y = IndexedBase('y') A = IndexedBase('A') i, j, k = Idx('i'), Idx('j'), Idx('k') assert get_indices(Pow(x[i], y[j])) == (set([i,j]), {}) assert get_indices(Pow(x[i, k], y[j, k])) == (set([i, j, k]), {}) assert get_indices(Pow(A[i, k], y[k] + A[k, j]*x[j])) == (set([i, k]), {}) assert get_indices(Pow(2, x[i])) == get_indices(exp(x[i])) # test of a design decision, this may change: assert get_indices(Pow(x[i], 2)) == (set([i,]), {})
def test_get_contraction_structure_basic(): x = IndexedBase("x") y = IndexedBase("y") i, j = Idx("i"), Idx("j") assert get_contraction_structure(x[i] * y[j]) == {None: set([x[i] * y[j]])} assert get_contraction_structure(x[i] + y[j]) == {None: set([x[i], y[j]])} assert get_contraction_structure(x[i] * y[i]) == { (i, ): set([x[i] * y[i]]) } assert get_contraction_structure(1 + x[i] * y[i]) == { None: set([S.One]), (i, ): set([x[i] * y[i]]), } assert get_contraction_structure(x[i]**y[i]) == {None: set([x[i]**y[i]])}
def test_get_indices_Pow(): x = IndexedBase("x") y = IndexedBase("y") A = IndexedBase("A") i, j, k = Idx("i"), Idx("j"), Idx("k") assert get_indices(Pow(x[i], y[j])) == (set([i, j]), {}) assert get_indices(Pow(x[i, k], y[j, k])) == (set([i, j, k]), {}) assert get_indices(Pow(A[i, k], y[k] + A[k, j] * x[j])) == (set([i, k]), {}) assert get_indices(Pow(2, x[i])) == get_indices(exp(x[i])) # test of a design decision, this may change: assert get_indices(Pow(x[i], 2)) == (set([ i, ]), {})
def test_get_contraction_structure_complex(): x = IndexedBase("x") y = IndexedBase("y") A = IndexedBase("A") i, j, k = Idx("i"), Idx("j"), Idx("k") expr1 = y[i] + A[i, j] * x[j] d1 = {None: set([y[i]]), (j, ): set([A[i, j] * x[j]])} assert get_contraction_structure(expr1) == d1 expr2 = expr1 * A[k, i] + x[k] d2 = { None: set([x[k]]), (i, ): set([expr1 * A[k, i]]), expr1 * A[k, i]: [d1] } assert get_contraction_structure(expr2) == d2
def test_IndexedBase_shape(): i, j, m, n = symbols('i j m n', integer=True) a = IndexedBase('a', shape=(m, m)) b = IndexedBase('a', shape=(m, n)) assert b.shape == Tuple(m, n) assert a[i, j] != b[i, j] assert a[i, j] == b[i, j].subs(n, m) assert b.func(*b.args) == b assert b[i, j].func(*b[i, j].args) == b[i, j] raises(IndexException, lambda: b[i]) raises(IndexException, lambda: b[i, i, j]) F = IndexedBase("F", shape=m) assert F.shape == Tuple(m) assert F[i].subs(i, j) == F[j] raises(IndexException, lambda: F[i, j])
def test_MultivariateEwens(): n, theta, i = symbols('n theta i', positive=True) # tests for integer dimensions theta_f = symbols('t_f', negative=True) a = symbols('a_1:4', positive=True, integer=True) ed = MultivariateEwens('E', 3, theta) assert density(ed)(a[0], a[1], a[2]) == Piecewise( (6 * 2**(-a[1]) * 3**(-a[2]) * theta**a[0] * theta**a[1] * theta**a[2] / (theta * (theta + 1) * (theta + 2) * factorial(a[0]) * factorial(a[1]) * factorial(a[2])), Eq(a[0] + 2 * a[1] + 3 * a[2], 3)), (0, True)) assert marginal_distribution(ed, ed[1])(a[1]) == Piecewise( (6 * 2**(-a[1]) * theta**a[1] / ((theta + 1) * (theta + 2) * factorial(a[1])), Eq(2 * a[1] + 1, 3)), (0, True)) raises(ValueError, lambda: MultivariateEwens('e1', 5, theta_f)) assert ed.pspace.distribution.set == ProductSet(Range(0, 4, 1), Range(0, 2, 1), Range(0, 2, 1)) # tests for symbolic dimensions eds = MultivariateEwens('E', n, theta) a = IndexedBase('a') j, k = symbols('j, k') den = Piecewise((factorial(n) * Product(theta**a[j] * (j + 1)**(-a[j]) / factorial(a[j]), (j, 0, n - 1)) / RisingFactorial(theta, n), Eq(n, Sum((k + 1) * a[k], (k, 0, n - 1)))), (0, True)) assert density(eds)(a).dummy_eq(den)
def __init__(self, polynomials, variables): """ A class that takes two lists, a list of polynomials and list of variables. Returns the Dixon matrix of the multivariate system. Parameters ---------- polynomials : list of polynomials A list of m n-degree polynomials variables: list A list of all n variables """ self.polynomials = polynomials self.variables = variables self.n = len(self.variables) self.m = len(self.polynomials) a = IndexedBase("alpha") # A list of n alpha variables (the replacing variables) self.dummy_variables = [a[i] for i in range(self.n)] # A list of the d_max of each variable. self._max_degrees = [max(degree_list(poly)[i] for poly in self.polynomials) for i in range(self.n)]
def test_IndexedBase_assumptions(): i = Symbol('i', integer=True) a = Symbol('a') A = IndexedBase(a, positive=True) for c in (A, A[i]): assert c.is_real assert c.is_complex assert not c.is_imaginary assert c.is_nonnegative assert c.is_nonzero assert c.is_commutative assert log(exp(c)) == c assert A != IndexedBase(a) assert A == IndexedBase(a, positive=True, real=True) assert A[i] != Indexed(a, i)
def test_Indexed_coeff(): N = Symbol('N', integer=True) len_y = N i = Idx('i', len_y - 1) y = IndexedBase('y', shape=(len_y, )) a = (1 / y[i + 1] * y[i]).coeff(y[i]) b = (y[i] / y[i + 1]).coeff(y[i]) assert a == b
def test_Indexed_constructor(): i, j = symbols('i j', integer=True) A = Indexed('A', i, j) assert A == Indexed(Symbol('A'), i, j) assert A == Indexed(IndexedBase('A'), i, j) raises(TypeError, lambda: Indexed(A, i, j)) raises(IndexException, lambda: Indexed("A")) assert A.free_symbols == {A, A.base.label, i, j}
def test_dixon_resultant_init(): """Test init method of DixonResultant.""" a = IndexedBase("alpha") assert dixon.polynomials == [p, q] assert dixon.variables == [x, y] assert dixon.n == 2 assert dixon.m == 2 assert dixon.dummy_variables == [a[0], a[1]]
def test_CircularOrthogonalEnsemble(): CO = COE('U', 3) j, k = (Dummy('j', integer=True, positive=True), Dummy('k', integer=True, positive=True)) t = IndexedBase('t') assert joint_eigen_distribution(CO).dummy_eq( Lambda((t[1], t[2], t[3]), Product(Abs(exp(I * t[j]) - exp(I * t[k])), (j, k + 1, 3), (k, 1, 2)) / (48 * pi**2)))
def test_indexed_is_constant(): A = IndexedBase("A") i, j, k = symbols("i,j,k") assert not A[i].is_constant() assert A[i].is_constant(j) assert not A[1 + 2 * i, k].is_constant() assert not A[1 + 2 * i, k].is_constant(i) assert A[1 + 2 * i, k].is_constant(j) assert not A[1 + 2 * i, k].is_constant(k)
def test_Indexed(): # Issue #10934 if not numpy: skip("numpy not installed") a = IndexedBase('a') i, j = symbols('i j') b = numpy.array([[1, 2], [3, 4]]) assert lambdify(a, Sum(a[x, y], (x, 0, 1), (y, 0, 1)))(b) == 10
def test_CircularSymplecticEnsemble(): CS = CSE('U', 3) j, k = (Dummy('j', integer=True, positive=True), Dummy('k', integer=True, positive=True)) t = IndexedBase('t') assert joint_eigen_distribution(CS).dummy_eq( Lambda((t[1], t[2], t[3]), Product( Abs(exp(I * t[j]) - exp(I * t[k]))**4, (j, k + 1, 3), (k, 1, 2)) / (720 * pi**3)))
def test_IndexedBase_sugar(): i, j = symbols('i j', integer=True) a = symbols('a') A1 = Indexed(a, i, j) A2 = IndexedBase(a) assert A1 == A2[i, j] assert A1 == A2[(i, j)] assert A1 == A2[[i, j]] assert A1 == A2[Tuple(i, j)] assert all(a.is_Integer for a in A2[1, 0].args[1:])