def test_symbol_nonpositive(): x = Symbol('x', nonpositive=True) assert x.is_positive is False assert x.is_nonpositive is True assert x.is_negative is None assert x.is_nonnegative is None assert x.is_zero is None assert x.is_nonzero is None
def test_symbol_zero(): x = Symbol('x', zero=True) assert x.is_positive is False assert x.is_nonpositive assert x.is_negative is False assert x.is_nonnegative assert x.is_zero is True assert x.is_nonzero is False assert x.is_finite is True
def test_hash(): D = Symbol('D') Lorentz = TensorIndexType('Lorentz', dim=D, dummy_fmt='L') a, b, c, d, e = tensor_indices('a,b,c,d,e', Lorentz) g = Lorentz.metric p, q = tensorhead('p q', [Lorentz], [[1]]) p_type = p.args[1] t1 = p(a) * q(b) t2 = p(a) * p(b) assert hash(t1) != hash(t2) t3 = p(a) * p(b) + g(a, b) t4 = p(a) * p(b) - g(a, b) assert hash(t3) != hash(t4) assert a.func(*a.args) == a assert Lorentz.func(*Lorentz.args) == Lorentz assert g.func(*g.args) == g assert p.func(*p.args) == p assert p_type.func(*p_type.args) == p_type assert p(a).func(*(p(a)).args) == p(a) assert t1.func(*t1.args) == t1 assert t2.func(*t2.args) == t2 assert t3.func(*t3.args) == t3 assert t4.func(*t4.args) == t4 assert hash(a.func(*a.args)) == hash(a) assert hash(Lorentz.func(*Lorentz.args)) == hash(Lorentz) assert hash(g.func(*g.args)) == hash(g) assert hash(p.func(*p.args)) == hash(p) assert hash(p_type.func(*p_type.args)) == hash(p_type) assert hash(p(a).func(*(p(a)).args)) == hash(p(a)) assert hash(t1.func(*t1.args)) == hash(t1) assert hash(t2.func(*t2.args)) == hash(t2) assert hash(t3.func(*t3.args)) == hash(t3) assert hash(t4.func(*t4.args)) == hash(t4) def check_all(obj): return all(isinstance(_, Basic) for _ in obj.args) assert check_all(a) assert check_all(Lorentz) assert check_all(g) assert check_all(p) assert check_all(p_type) assert check_all(p(a)) assert check_all(t1) assert check_all(t2) assert check_all(t3) assert check_all(t4) tsymmetry = tensorsymmetry([2], [1], [1, 1, 1]) assert tsymmetry.func(*tsymmetry.args) == tsymmetry assert hash(tsymmetry.func(*tsymmetry.args)) == hash(tsymmetry) assert check_all(tsymmetry)
def test_sympyissue_4149(): assert (3 + I).is_complex assert (3 + I).is_imaginary is False assert (3 * I + S.Pi * I).is_imaginary y = Symbol('y', real=True) assert (3 * I + S.Pi * I + y * I).is_imaginary is True p = Symbol('p', positive=True, finite=True) assert (3 * I + S.Pi * I + p * I).is_imaginary n = Symbol('n', negative=True, finite=True) assert (-3 * I - S.Pi * I + n * I).is_imaginary i = Symbol('i', imaginary=True) assert ([(i**a).is_imaginary for a in range(4)] == [False, True, False, True]) # tests from the PR sympy/sympy#7887: e = -sqrt(3) * I / 2 + Float(0.866025403784439) * I assert e.is_extended_real is False assert e.is_imaginary
def test_trigintegrate_odd(): assert trigintegrate(Rational(1), x) == x assert trigintegrate(x, x) is None assert trigintegrate(x**2, x) is None assert trigintegrate(sin(x), x) == -cos(x) assert trigintegrate(cos(x), x) == sin(x) assert trigintegrate(sin(3 * x), x) == -cos(3 * x) / 3 assert trigintegrate(cos(3 * x), x) == sin(3 * x) / 3 y = Symbol('y') assert trigintegrate(sin(y*x), x) == \ Piecewise((0, Eq(y, 0)), (-cos(y*x)/y, True)) assert trigintegrate(cos(y*x), x) == \ Piecewise((x, Eq(y, 0)), (sin(y*x)/y, True)) assert trigintegrate(sin(y*x)**2, x) == \ Piecewise((0, Eq(y, 0)), ((x*y/2 - sin(x*y)*cos(x*y)/2)/y, True)) assert trigintegrate(sin(y*x)*cos(y*x), x) == \ Piecewise((0, Eq(y, 0)), (sin(x*y)**2/(2*y), True)) assert trigintegrate(cos(y*x)**2, x) == \ Piecewise((x, Eq(y, 0)), ((x*y/2 + sin(x*y)*cos(x*y)/2)/y, True)) y = Symbol('y', positive=True) # TODO: remove conds='none' below. For this to work we would have to rule # out (e.g. by trying solve) the condition y = 0, incompatible with # y.is_positive being True. assert trigintegrate(sin(y * x), x, conds='none') == -cos(y * x) / y assert trigintegrate(cos(y * x), x, conds='none') == sin(y * x) / y assert trigintegrate(sin(x) * cos(x), x) == sin(x)**2 / 2 assert trigintegrate(sin(x) * cos(x)**2, x) == -cos(x)**3 / 3 assert trigintegrate(sin(x)**2 * cos(x), x) == sin(x)**3 / 3 # check if it selects right function to substitute, # so the result is kept simple assert trigintegrate(sin(x)**7 * cos(x), x) == sin(x)**8 / 8 assert trigintegrate(sin(x) * cos(x)**7, x) == -cos(x)**8 / 8 assert trigintegrate(sin(x)**7 * cos(x)**3, x) == \ -sin(x)**10/10 + sin(x)**8/8 assert trigintegrate(sin(x)**3 * cos(x)**7, x) == \ cos(x)**10/10 - cos(x)**8/8
def test_symbol_real(): # issue sympy/sympy#3848 a = Symbol('a', extended_real=False) assert a.is_extended_real is False assert a.is_integer is False assert a.is_negative is False assert a.is_positive is False assert a.is_nonnegative is False assert a.is_nonpositive is False assert a.is_zero is False
def test_zero(): assert 0**x != 0 assert 0**(2*x) == 0**x assert 0**(1.0*x) == 0**x assert 0**(2.0*x) == 0**x assert (0**(2 - x)).as_base_exp() == (0, 2 - x) assert 0**(x - 2) != oo**(2 - x) assert 0**(2*x*y) == 0**(x*y) assert 0**(-2*x*y) == zoo**(x*y) assert 0**I == nan i = Symbol('i', imaginary=True, nonzero=True) assert 0**i == nan
def test_real_root(): assert real_root(-8, 3) == -2 assert real_root(-16, 4) == root(-16, 4) r = root(-7, 4) assert real_root(r) == r r1 = root(-1, 3) r2 = r1**2 r3 = root(-1, 4) assert real_root(r1 + r2 + r3) == -1 + r2 + r3 assert real_root(root(-2, 3)) == -root(2, 3) assert real_root(-8., 3) == -2 x = Symbol('x') n = Symbol('n') g = real_root(x, n) assert g.subs({x: -8, n: 3}) == -2 assert g.subs({x: 8, n: 3}) == 2 # give principle root if there is no real root -- if this is not desired # then maybe a Root class is needed to raise an error instead assert g.subs({x: I, n: 3}) == cbrt(I) assert g.subs({x: -8, n: 2}) == sqrt(-8) assert g.subs({x: I, n: 2}) == sqrt(I)
def test_specfun(): n = Symbol('n') for f in [besselj, bessely, besseli, besselk]: assert octave_code(f(n, x)) == f.__name__ + '(n, x)' assert octave_code(hankel1(n, x)) == 'besselh(n, 1, x)' assert octave_code(hankel2(n, x)) == 'besselh(n, 2, x)' assert octave_code(airyai(x)) == 'airy(0, x)' assert octave_code(airyaiprime(x)) == 'airy(1, x)' assert octave_code(airybi(x)) == 'airy(2, x)' assert octave_code(airybiprime(x)) == 'airy(3, x)' assert octave_code(jn(n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*besselj(n + 1/2, x)/2' assert octave_code(yn(n, x)) == 'sqrt(2)*sqrt(pi)*sqrt(1./x).*bessely(n + 1/2, x)/2'
def test_MatrixSymbol(): n = Symbol('n', integer=True) A = MatrixSymbol('A', n, n) B = MatrixSymbol('B', n, n) assert mcode(A * B) == "A*B" assert mcode(B * A) == "B*A" assert mcode(2 * A * B) == "2*A*B" assert mcode(B * 2 * A) == "2*B*A" assert mcode(A * (B + 3 * Identity(n))) == "A*(3*eye(n) + B)" assert mcode(A**(x**2)) == "A^(x.^2)" assert mcode(A**3) == "A^3" assert mcode(A**(S.Half)) == "A^(1/2)"
def test_Add_is_pos_neg(): # these cover lines not covered by the rest of tests in core n = Symbol('n', negative=True, infinite=True) nn = Symbol('n', nonnegative=True, infinite=True) np = Symbol('n', nonpositive=True, infinite=True) p = Symbol('p', positive=True, infinite=True) r = Dummy(extended_real=True, finite=False) x = Symbol('x') xf = Symbol('xb', finite=True, real=True) assert (n + p).is_positive is None assert (n + x).is_positive is None assert (p + x).is_positive is None assert (n + p).is_negative is None assert (n + x).is_negative is None assert (p + x).is_negative is None assert (n + xf).is_positive is False assert (p + xf).is_positive is True assert (n + xf).is_negative is True assert (p + xf).is_negative is False assert (x - S.Infinity).is_negative is None # issue sympy/sympy#7798 # issue sympy/sympy#8046, 16.2 assert (p + nn).is_positive assert (n + np).is_negative assert (p + r).is_positive is None
def test_sympyissue_3449(): # test if powers are simplified correctly # see also issue sympy/sympy#3995 x = Symbol('x') assert ((x**Rational(1, 3))**Rational(2)) == x**Rational(2, 3) assert ( (x**Rational(3))**Rational(2, 5)) == (x**Rational(3))**Rational(2, 5) a = Symbol('a', extended_real=True) b = Symbol('b', extended_real=True) assert (a**2)**b == (abs(a)**b)**2 assert sqrt(1/a) != 1/sqrt(a) # e.g. for a = -1 assert (a**3)**Rational(1, 3) != a assert (x**a)**b != x**(a*b) # e.g. x = -1, a=2, b=1/2 assert (x**.5)**b == x**(.5*b) assert (x**.5)**.5 == x**.25 assert (x**2.5)**.5 != x**1.25 # e.g. for x = 5*I k = Symbol('k', integer=True) m = Symbol('m', integer=True) assert (x**k)**m == x**(k*m) assert Number(5)**Rational(2, 3) == Number(25)**Rational(1, 3) assert (x**.5)**2 == x**1.0 assert (x**2)**k == (x**k)**2 == x**(2*k) a = Symbol('a', positive=True) assert (a**3)**Rational(2, 5) == a**Rational(6, 5) assert (a**2)**b == (a**b)**2 assert (a**Rational(2, 3))**x == (a**(2*x/3)) != (a**x)**Rational(2, 3)
def test_sympyissue_6068(): x = Symbol('x') assert sqrt(sin(x)).series(x, 0, 8) == \ sqrt(x) - x**Rational(5, 2)/12 + x**Rational(9, 2)/1440 - \ x**Rational(13, 2)/24192 + O(x**8) assert sqrt(sin(x)).series(x, 0, 10) == \ sqrt(x) - x**Rational(5, 2)/12 + x**Rational(9, 2)/1440 - \ x**Rational(13, 2)/24192 - 67*x**Rational(17, 2)/29030400 + O(x**10) assert sqrt(sin(x**3)).series(x, 0, 19) == \ x**Rational(3, 2) - x**Rational(15, 2)/12 + x**Rational(27, 2)/1440 + O(x**19) assert sqrt(sin(x**3)).series(x, 0, 20) == \ x**Rational(3, 2) - x**Rational(15, 2)/12 + x**Rational(27, 2)/1440 - \ x**Rational(39, 2)/24192 + O(x**20)
def test_canonicalize3(): D = Symbol('D') Spinor = TensorIndexType('Spinor', dim=D, metric=True, dummy_fmt='S') a0, a1, a2, a3, a4 = tensor_indices('a0:5', Spinor) chi, psi = tensorhead('chi,psi', [Spinor], [[1]], 1) t = chi(a1) * psi(a0) t1 = t.canon_bp() assert t1 == t t = psi(a1) * chi(a0) t1 = t.canon_bp() assert t1 == -chi(a0) * psi(a1)
def fateman_poly_F_2(n): """Fateman's GCD benchmark: linearly dense quartic inputs """ Y = [Symbol('y_' + str(i)) for i in range(0, n + 1)] y_0 = Y[0] u = Add(*[y for y in Y[1:]]) H = Poly((y_0 + u + 1)**2, *Y) F = Poly((y_0 - u - 2)**2, *Y) G = Poly((y_0 + u + 2)**2, *Y) return H * F, H * G, H
def __new__(cls, label, shape=None, **kw_args): if isinstance(label, str): label = Symbol(label) elif isinstance(label, (Dummy, Symbol)): pass else: raise TypeError("Base label should be a string or Symbol.") obj = Expr.__new__(cls, label, **kw_args) if is_sequence(shape): obj._shape = Tuple(*shape) else: obj._shape = sympify(shape) return obj
def fateman_poly_F_3(n): """Fateman's GCD benchmark: sparse inputs (deg f ~ vars f) """ Y = [Symbol('y_' + str(i)) for i in range(0, n + 1)] y_0 = Y[0] u = Add(*[y**(n + 1) for y in Y[1:]]) H = Poly((y_0**(n + 1) + u + 1)**2, *Y) F = Poly((y_0**(n + 1) - u - 2)**2, *Y) G = Poly((y_0**(n + 1) + u + 2)**2, *Y) return H * F, H * G, H
def fateman_poly_F_1(n): """Fateman's GCD benchmark: trivial GCD """ Y = [Symbol('y_' + str(i)) for i in range(0, n + 1)] y_0, y_1 = Y[0], Y[1] u = y_0 + Add(*[y for y in Y[1:]]) v = y_0**2 + Add(*[y**2 for y in Y[1:]]) F = ((u + 1) * (u + 2)).as_poly(*Y) G = ((v + 1) * (-3 * y_1 * y_0**2 + y_1**2 - 1)).as_poly(*Y) H = Poly(1, *Y) return F, G, H
def test_special_is_rational(): i = Symbol('i', integer=True) i2 = Symbol('i2', integer=True) ni = Symbol('ni', integer=True, nonzero=True) r = Symbol('r', rational=True) rn = Symbol('r', rational=True, nonzero=True) nr = Symbol('nr', irrational=True) x = Symbol('x') assert sqrt(3).is_rational is False assert (3 + sqrt(3)).is_rational is False assert (3 * sqrt(3)).is_rational is False z = Symbol('z', zero=True) assert exp(z).is_rational assert exp(0, evaluate=False).is_rational assert exp(3).is_rational is False assert exp(ni).is_rational is False assert exp(rn).is_rational is False assert exp(x).is_rational is None assert exp(log(3), evaluate=False).is_rational is True assert log(exp(3), evaluate=False).is_rational is True assert log(3).is_rational is False assert log(ni + 1).is_rational is False assert log(rn + 1).is_rational is False assert log(x).is_rational is None assert (sqrt(3) + sqrt(5)).is_rational is None assert (sqrt(3) + S.Pi).is_rational is False assert (x**i).is_rational is None assert (i**i).is_rational is True assert (i**i2).is_rational is None assert (r**i).is_rational is None assert (r**r).is_rational is None assert (r**x).is_rational is None assert (nr**i).is_rational is None # issue sympy/sympy#8598 assert (nr**Symbol('z', zero=True)).is_rational assert sin(1).is_rational is False assert sin(ni).is_rational is False assert sin(rn).is_rational is False assert sin(x).is_rational is None assert asin(rn).is_rational is False assert sin(asin(3), evaluate=False).is_rational is True
def test_TensorIndexType(): D = Symbol('D') G = Metric('g', False) Lorentz = TensorIndexType('Lorentz', metric=G, dim=D, dummy_fmt='L') m0, m1, m2, m3, m4 = tensor_indices('m0:5', Lorentz) sym2 = tensorsymmetry([1] * 2) sym2n = tensorsymmetry(*get_symmetric_group_sgs(2)) assert sym2 == sym2n g = Lorentz.metric assert str(g) == 'g(Lorentz,Lorentz)' assert Lorentz.eps_dim == Lorentz.dim TSpace = TensorIndexType('TSpace') i0, i1 = tensor_indices('i0 i1', TSpace) g = TSpace.metric A = tensorhead('A', [TSpace] * 2, [[1] * 2]) assert str(A(i0, -i0).canon_bp()) == 'A(TSpace_0, -TSpace_0)'
def test_schwarzschild(): m = Manifold('Schwarzschild', 4) p = Patch('origin', m) cs = CoordSystem('spherical', p, ['t', 'r', 'theta', 'phi']) t, r, theta, phi = cs.coord_functions() dt, dr, dtheta, dphi = cs.base_oneforms() f, g = symbols('f g', cls=Function) metric = (exp(2*f(r))*TP(dt, dt) - exp(2*g(r))*TP(dr, dr) - r**2*TP(dtheta, dtheta) - r**2*sin(theta)**2*TP(dphi, dphi)) ricci = metric_to_Ricci_components(metric) assert all(ricci[i, j] == 0 for i in range(4) for j in range(4) if i != j) R = Symbol('R') eq1 = simplify((ricci[0, 0]/exp(2*f(r) - 2*g(r)) + ricci[1, 1])*r/2).subs(r, R).doit() assert eq1 == f(R).diff(R) + g(R).diff(R) eq2 = simplify(ricci[1, 1].replace(g, lambda x: -f(x)).replace(r, R).doit()) assert eq2 == -2*f(R).diff(R)**2 - f(R).diff(R, 2) - 2*f(R).diff(R)/R
def test_Mul_is_algebraic(): a = Symbol('a', algebraic=True) b = Symbol('a', algebraic=True) na = Symbol('na', algebraic=False) an = Symbol('an', algebraic=True, nonzero=True) nb = Symbol('nb', algebraic=False) x = Symbol('x') assert (a * b).is_algebraic assert (na * nb).is_algebraic is None assert (a * na).is_algebraic is None assert (an * na).is_algebraic is False assert (a * x).is_algebraic is None assert (na * x).is_algebraic is None
def test_contract_metric1(): D = Symbol('D') Lorentz = TensorIndexType('Lorentz', dim=D, dummy_fmt='L') a, b, c, d, e = tensor_indices('a,b,c,d,e', Lorentz) g = Lorentz.metric p = tensorhead('p', [Lorentz], [[1]]) t = g(a, b) * p(-b) t1 = t.contract_metric(g) assert t1 == p(a) A, B = tensorhead('A,B', [Lorentz] * 2, [[1] * 2]) # case with g with all free indices t1 = A(a, b) * B(-b, c) * g(d, e) t2 = t1.contract_metric(g) assert t1 == t2 # case of g(d, -d) t1 = A(a, b) * B(-b, c) * g(-d, d) t2 = t1.contract_metric(g) assert t2 == D * A(a, d) * B(-d, c) # g with one free index t1 = A(a, b) * B(-b, -c) * g(c, d) t2 = t1.contract_metric(g) assert t2 == A(a, c) * B(-c, d) # g with both indices contracted with another tensor t1 = A(a, b) * B(-b, -c) * g(c, -a) t2 = t1.contract_metric(g) assert _is_equal(t2, A(a, b) * B(-b, -a)) t1 = A(a, b) * B(-b, -c) * g(c, d) * g(-a, -d) t2 = t1.contract_metric(g) assert _is_equal(t2, A(a, b) * B(-b, -a)) t1 = A(a, b) * g(-a, -b) t2 = t1.contract_metric(g) assert _is_equal(t2, A(a, -a)) assert not t2.free Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') a, b = tensor_indices('a,b', Lorentz) g = Lorentz.metric pytest.raises(ValueError, lambda: g(a, -a).contract_metric(g)) # no dim
def test_Max(): n = Symbol('n', negative=True) n_ = Symbol('n_', negative=True) p = Symbol('p', positive=True) r = Symbol('r', extended_real=True) assert Max(5, 4) == 5 # lists pytest.raises(ValueError, lambda: Max()) assert Max(x, y) == Max(y, x) assert Max(x, y, z) == Max(z, y, x) assert Max(x, Max(y, z)) == Max(z, y, x) assert Max(x, Min(y, oo)) == Max(x, y) assert Max(n, -oo, n_, p, 2) == Max(p, 2) assert Max(n, -oo, n_, p) == p assert Max(2, x, p, n, -oo, -oo, n_, p, 2) == Max(2, x, p) assert Max(0, x, 1, y) == Max(1, x, y) assert Max(r, r + 1, r - 1) == 1 + r assert Max(1000, 100, -100, x, p, n) == Max(p, x, 1000) assert Max(cos(x), sin(x)) == Max(sin(x), cos(x)) assert Max(cos(x), sin(x)).subs({x: 1}) == sin(1) assert Max(cos(x), sin(x)).subs({x: Rational(1, 2)}) == cos(Rational(1, 2)) pytest.raises(ValueError, lambda: Max(cos(x), sin(x)).subs({x: I})) pytest.raises(ValueError, lambda: Max(I)) pytest.raises(ValueError, lambda: Max(I, x)) pytest.raises(ValueError, lambda: Max(zoo, 1)) # interesting: # Max(n, -oo, n_, p, 2) == Max(p, 2) # True # Max(n, -oo, n_, p, 1000) == Max(p, 1000) # False assert Max(1, x).diff(x) == Heaviside(x - 1) assert Max(x, 1).diff(x) == Heaviside(x - 1) assert Max(x**2, 1 + x, 1).diff(x) == \ 2*x*Heaviside(x**2 - Max(1, x + 1)) \ + Heaviside(x - Max(1, x**2) + 1) pytest.raises(ArgumentIndexError, lambda: Max(1, x).fdiff(3)) a, b = Symbol('a', extended_real=True), Symbol('b', extended_real=True) # a and b are both real, Max(a, b) should be real assert Max(a, b).is_extended_real # issue sympy/sympy#7233 e = Max(0, x) assert e.evalf == e.n assert e.evalf(strict=False).args == (0, x)
def test_diofant_parser(): x = Symbol('x') inputs = { '2*x': 2 * x, '3.00': Float(3), '22/7': Rational(22, 7), '2+3j': 2 + 3*I, 'exp(x)': exp(x), '-(2)': -Integer(2), '[-1, -2, 3]': [Integer(-1), Integer(-2), Integer(3)], 'Symbol("x").free_symbols': x.free_symbols, "Float(Integer(3).evalf(3))": 3.00, 'factorint(12, visual=True)': Mul( Pow(2, 2, evaluate=False), Pow(3, 1, evaluate=False), evaluate=False), 'Limit(sin(x), x, 0, dir="-")': Limit(sin(x), x, 0, dir='-'), } for text, result in inputs.items(): assert parse_expr(text) == result
def test_sympyissue_11799(): n = 2 M = Manifold('M', n) P = Patch('P', M) coord = CoordSystem('coord', P, ['x%s' % i for i in range(n)]) x = coord.coord_functions() dx = coord.base_oneforms() f = Function('f') g = [[f(x[0], x[1])**2, 0], [0, f(x[0], x[1])**2]] metric = sum(g[i][j]*TP(dx[i], dx[j]) for i in range(n) for j in range(n)) R = metric_to_Riemann_components(metric) d = Symbol('d') assert (R[0, 1, 0, 1] == -Subs(Derivative(f(d, x[1]), d, d), (d, x[0]))/f(x[0], x[1]) - Subs(Derivative(f(x[0], d), d, d), (d, x[1]))/f(x[0], x[1]) + Subs(Derivative(f(d, x[1]), d), (d, x[0]))**2/f(x[0], x[1])**2 + Subs(Derivative(f(x[0], d), d), (d, x[1]))**2/f(x[0], x[1])**2)
def test_canonicalize2(): D = Symbol('D') Eucl = TensorIndexType('Eucl', metric=0, dim=D, dummy_fmt='E') i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14 = \ tensor_indices('i0:15', Eucl) A = tensorhead('A', [Eucl] * 3, [[3]]) # two examples from Cvitanovic, Group Theory page 59 # of identities for antisymmetric tensors of rank 3 # contracted according to the Kuratowski graph eq.(6.59) t = A(i0, i1, i2) * A(-i1, i3, i4) * A(-i3, i7, i5) * A(-i2, -i5, i6) * A( -i4, -i6, i8) t1 = t.canon_bp() assert t1 == 0 # eq.(6.60) # t = A(i0,i1,i2)*A(-i1,i3,i4)*A(-i2,i5,i6)*A(-i3,i7,i8)*A(-i6,-i7,i9)* # A(-i8,i10,i13)*A(-i5,-i10,i11)*A(-i4,-i11,i12)*A(-i3,-i12,i14) t = A(i0, i1, i2)*A(-i1, i3, i4)*A(-i2, i5, i6)*A(-i3, i7, i8)*A(-i6, -i7, i9) *\ A(-i8, i10, i13)*A(-i5, -i10, i11)*A(-i4, -i11, i12)*A(-i9, -i12, i14) t1 = t.canon_bp() assert t1 == 0
def __new__(cls, e, z, z0, dir="+"): e = sympify(e) z = sympify(z) z0 = sympify(z0) if z0 is S.Infinity: dir = "-" elif z0 is S.NegativeInfinity: dir = "+" if isinstance(dir, str): dir = Symbol(dir) elif not isinstance(dir, Symbol): raise TypeError("direction must be of type str or Symbol, not %s" % type(dir)) if str(dir) not in ('+', '-', 'real'): raise ValueError( "direction must be either '+' or '-' or 'real', not %s" % dir) obj = Expr.__new__(cls) obj._args = (e, z, z0, dir) return obj
def test_correct_arguments(): pytest.raises(ValueError, lambda: R2.e_x(R2.e_x)) pytest.raises(ValueError, lambda: R2.e_x(R2.dx)) pytest.raises(ValueError, lambda: Commutator(R2.e_x, R2.x)) pytest.raises(ValueError, lambda: Commutator(R2.dx, R2.e_x)) pytest.raises(ValueError, lambda: Differential(Differential(R2.e_x))) pytest.raises(ValueError, lambda: R2.dx(R2.x)) pytest.raises(ValueError, lambda: TensorProduct(R2.e_x, R2.dx)) pytest.raises(ValueError, lambda: LieDerivative(R2.dx, R2.dx)) pytest.raises(ValueError, lambda: LieDerivative(R2.x, R2.dx)) pytest.raises(ValueError, lambda: CovarDerivativeOp(R2.dx, [])) pytest.raises(ValueError, lambda: CovarDerivativeOp(R2.x, [])) a = Symbol('a') pytest.raises(ValueError, lambda: intcurve_series(R2.dx, a, R2_r.point([1, 2]))) pytest.raises(ValueError, lambda: intcurve_series(R2.x, a, R2_r.point([1, 2]))) pytest.raises(ValueError, lambda: intcurve_diffequ(R2.dx, a, R2_r.point([1, 2]))) pytest.raises(ValueError, lambda: intcurve_diffequ(R2.x, a, R2_r.point([1, 2]))) pytest.raises(ValueError, lambda: contravariant_order(R2.e_x + R2.dx)) pytest.raises(ValueError, lambda: contravariant_order(R2.dx**2)) pytest.raises(ValueError, lambda: covariant_order(R2.e_x + R2.dx)) pytest.raises(ValueError, lambda: contravariant_order(R2.e_x * R2.e_y)) pytest.raises(ValueError, lambda: covariant_order(R2.dx * R2.dy)) assert covariant_order(Integer(0), True) == -1 assert contravariant_order(Integer(0), True) == -1
def test_contract_delta1(): # see Group Theory by Cvitanovic page 9 n = Symbol('n') Color = TensorIndexType('Color', metric=None, dim=n, dummy_fmt='C') a, b, c, d, e, f = tensor_indices('a,b,c,d,e,f', Color) delta = Color.delta def idn(a, b, d, c): assert a.is_up and d.is_up assert not (b.is_up or c.is_up) return delta(a, c) * delta(d, b) def T(a, b, d, c): assert a.is_up and d.is_up assert not (b.is_up or c.is_up) return delta(a, b) * delta(d, c) def P1(a, b, c, d): return idn(a, b, c, d) - 1 / n * T(a, b, c, d) def P2(a, b, c, d): return 1 / n * T(a, b, c, d) t = P1(a, -b, e, -f) * P1(f, -e, d, -c) t1 = t.contract_delta(delta) assert t1 == P1(a, -b, d, -c) t = P2(a, -b, e, -f) * P2(f, -e, d, -c) t1 = t.contract_delta(delta) assert t1 == P2(a, -b, d, -c) t = P1(a, -b, e, -f) * P2(f, -e, d, -c) t1 = t.contract_delta(delta) assert t1 == 0 t = P1(a, -b, b, -a) t1 = t.contract_delta(delta) assert t1.equals(n**2 - 1)