Beispiel #1
0
def test_count_ops_visual():
    ADD, MUL, POW, SIN, COS, AND, D, G = symbols(
        'Add Mul Pow sin cos And Derivative Integral'.upper())
    DIV, SUB, NEG = symbols('DIV SUB NEG')
    NOT, OR, AND, XOR, IMPLIES, EQUIVALENT, _ITE, BASIC, TUPLE = symbols(
        'Not Or And Xor Implies Equivalent ITE Basic Tuple'.upper())

    def count(val):
        return count_ops(val, visual=True)

    assert count(7) is Integer(0)
    assert count(-1) == NEG
    assert count(-2) == NEG
    assert count(Rational(2, 3)) == DIV
    assert count(pi / 3) == DIV
    assert count(-pi / 3) == DIV + NEG
    assert count(I - 1) == SUB
    assert count(1 - I) == SUB
    assert count(1 - 2 * I) == SUB + MUL

    assert count(x) is Integer(0)
    assert count(-x) == NEG
    assert count(-2 * x / 3) == NEG + DIV + MUL
    assert count(1 / x) == DIV
    assert count(1 / (x * y)) == DIV + MUL
    assert count(-1 / x) == NEG + DIV
    assert count(-2 / x) == NEG + DIV
    assert count(x / y) == DIV
    assert count(-x / y) == NEG + DIV

    assert count(x**2) == POW
    assert count(-x**2) == POW + NEG
    assert count(-2 * x**2) == POW + MUL + NEG

    assert count(x + pi / 3) == ADD + DIV
    assert count(x + Rational(1, 3)) == ADD + DIV
    assert count(x + y) == ADD
    assert count(x - y) == SUB
    assert count(y - x) == SUB
    assert count(-1 / (x - y)) == DIV + NEG + SUB
    assert count(-1 / (y - x)) == DIV + NEG + SUB
    assert count(1 + x**y) == ADD + POW
    assert count(1 + x + y) == 2 * ADD
    assert count(1 + x + y + z) == 3 * ADD
    assert count(1 + x**y + 2 * x * y + y**2) == 3 * ADD + 2 * POW + 2 * MUL
    assert count(2 * z + y + x + 1) == 3 * ADD + MUL
    assert count(2 * z + y**17 + x + 1) == 3 * ADD + MUL + POW
    assert count(2 * z + y**17 + x + sin(x)) == 3 * ADD + POW + MUL + SIN
    assert count(2 * z + y**17 + x +
                 sin(x**2)) == 3 * ADD + MUL + 2 * POW + SIN
    assert count(2 * z + y**17 + x + sin(x**2) +
                 exp(cos(x))) == 4 * ADD + MUL + 3 * POW + COS + SIN

    assert count(Derivative(x, x)) == D
    assert count(Integral(x, x) + 2 * x / (1 + x)) == G + DIV + MUL + 2 * ADD
    assert count(Basic()) is Integer(0)

    assert count({x + 1: sin(x)}) == ADD + SIN
    assert count([x + 1, sin(x) + y, None]) == ADD + SIN + ADD
    assert count({x + 1: sin(x), y: cos(x) + 1}) == SIN + COS + 2 * ADD
    assert count({}) is Integer(0)
    assert count([x + 1, sin(x) * y, None]) == SIN + ADD + MUL
    assert count([]) is Integer(0)

    assert count(Basic()) == 0
    assert count(Basic(Basic(), Basic(x, x + y))) == ADD + 2 * BASIC
    assert count(Basic(x, x + y)) == ADD + BASIC
    assert count(Or(x, y)) == OR
    assert count(And(x, y)) == AND
    assert count(And(x**y, z)) == AND + POW
    assert count(Or(x, Or(y, And(z, a)))) == AND + OR
    assert count(Nor(x, y)) == NOT + OR
    assert count(Nand(x, y)) == NOT + AND
    assert count(Xor(x, y)) == XOR
    assert count(Implies(x, y)) == IMPLIES
    assert count(Equivalent(x, y)) == EQUIVALENT
    assert count(ITE(x, y, z)) == _ITE
    assert count([Or(x, y), And(x, y), Basic(x + y)]) == ADD + AND + BASIC + OR

    assert count(Basic(Tuple(x))) == BASIC + TUPLE
    # It checks that TUPLE is counted as an operation.

    assert count(Eq(x + y, 2)) == ADD
Beispiel #2
0
def test_Subs():
    assert Subs(x, (x, 0)) == Subs(y, (y, 0))
    assert Subs(x, (x, 0)).subs({x: 1}) == Subs(x, (x, 0))
    assert Subs(y, (x, 0)).subs({y: 1}) == Subs(1, (x, 0))
    assert Subs(f(x), (x, 0)).doit() == f(0)
    assert Subs(f(x**2), (x**2, 0)).doit() == f(0)
    assert Subs(f(x, y, z), (x, 0), (y, 1), (z, 1)) != \
        Subs(f(x, y, z), (x, 0), (y, 0), (z, 1))
    assert Subs(f(x, y), (x, 0), (y, 1), (z, 1)) == \
        Subs(f(x, y), (x, 0), (y, 1), (z, 2))
    assert Subs(f(x, y), (x, 0), (y, 1), (z, 1)) != \
        Subs(f(x, y) + z, (x, 0), (y, 1), (z, 0))
    assert Subs(f(x, y), (x, 0), (y, 1)).doit() == f(0, 1)
    assert Subs(Subs(f(x, y), (x, 0)), (y, 1)).doit() == f(0, 1)
    pytest.raises(ValueError, lambda: Subs(f(x, y), x))
    pytest.raises(ValueError, lambda: Subs(f(x, y), (x, 0), (x, 0), (y, 1)))

    assert len(Subs(f(x, y), (x, 0), (y, 1)).variables) == 2
    assert Subs(f(x, y), (x, 0), (y, 1)).point == Tuple(0, 1)

    assert Subs(f(x), (x, 0)) == Subs(f(y), (y, 0))
    assert Subs(f(x, y), (x, 0), (y, 1)) == Subs(f(x, y), (y, 1), (x, 0))
    assert Subs(f(x) * y, (x, 0), (y, 1)) == Subs(f(y) * x, (y, 0), (x, 1))
    assert Subs(f(x) * y, (x, 1), (y, 1)) == Subs(f(y) * x, (x, 1), (y, 1))

    assert Subs(f(x), (x, 0)).subs({x: 1}).doit() == f(0)
    assert Subs(f(x), (x, y)).subs({y: 0}) == Subs(f(x), (x, 0))
    assert Subs(y * f(x), (x, y)).subs({y: 2}) == Subs(2 * f(x), (x, 2))
    assert (2 * Subs(f(x), (x, 0))).subs({Subs(f(x), (x, 0)): y}) == 2 * y

    assert Subs(f(x), (x, 0)).free_symbols == set()
    assert Subs(f(x, y), (x, z)).free_symbols == {y, z}

    assert Subs(f(x).diff(x), (x, 0)).doit(), Subs(f(x).diff(x), (x, 0))
    assert Subs(1 + f(x).diff(x),
                (x, 0)).doit(), 1 + Subs(f(x).diff(x), (x, 0))
    assert Subs(y*f(x, y).diff(x), (x, 0), (y, 2)).doit() == \
        2*Subs(Derivative(f(x, 2), x), (x, 0))
    assert Subs(y**2 * f(x), (x, 0)).diff(y) == 2 * y * f(0)

    e = Subs(y**2 * f(x), (x, y))
    assert e.diff(y) == e.doit().diff(
        y) == y**2 * Derivative(f(y), y) + 2 * y * f(y)

    assert Subs(f(x), (x, 0)) + Subs(f(x), (x, 0)) == 2 * Subs(f(x), (x, 0))
    e1 = Subs(z * f(x), (x, 1))
    e2 = Subs(z * f(y), (y, 1))
    assert e1 + e2 == 2 * e1
    assert e1.__hash__() == e2.__hash__()
    assert Subs(z * f(x + 1), (x, 1)) not in (e1, e2)
    assert Derivative(f(x), x).subs({x: g(x)}) == Derivative(f(g(x)), g(x))
    assert Derivative(f(x), x).subs({x:
                                     x + y}) == Subs(Derivative(f(x), x),
                                                     (x, x + y))
    assert Subs(f(x)*cos(y) + z, (x, 0), (y, pi/3)).evalf(2, strict=False) == \
        Subs(f(x)*cos(y) + z, (x, 0), (y, pi/3)).evalf(2, strict=False) == \
        z + Rational('1/2').evalf(2)*f(0)

    assert f(x).diff(x).subs({x: 0}).subs({x: y}) == f(x).diff(x).subs({x: 0})
    assert (x * f(x).diff(x).subs({x: 0})).subs(
        {x: y}) == y * f(x).diff(x).subs({x: 0})
Beispiel #3
0
def test_issue_6075():
    assert Tuple(1, True).subs(1, 2) == Tuple(2, True)
Beispiel #4
0
def test_has():
    a, b, c = symbols('a, b, c', cls=Dummy)
    f = Hyper_Function([2, -a], [b])
    assert f.has(a)
    assert f.has(Tuple(b))
    assert not f.has(c)
Beispiel #5
0
def test_AlgebraicNumber():
    minpoly, root = x**2 - 2, sqrt(2)

    a = AlgebraicNumber(root, gen=x)

    assert a.rep == DMP([QQ(1), QQ(0)], QQ)
    assert a.root == root
    assert a.alias is None
    assert a.minpoly == minpoly
    assert a.is_number

    assert a.is_aliased is False

    assert a.coeffs() == [Integer(1), Integer(0)]
    assert a.native_coeffs() == [QQ(1), QQ(0)]

    a = AlgebraicNumber(root, gen=x, alias='y')

    assert a.rep == DMP([QQ(1), QQ(0)], QQ)
    assert a.root == root
    assert a.alias == Symbol('y')
    assert a.minpoly == minpoly
    assert a.is_number

    assert a.is_aliased is True

    a = AlgebraicNumber(root, gen=x, alias=Symbol('y'))

    assert a.rep == DMP([QQ(1), QQ(0)], QQ)
    assert a.root == root
    assert a.alias == Symbol('y')
    assert a.minpoly == minpoly
    assert a.is_number

    assert a.is_aliased is True

    assert AlgebraicNumber(sqrt(2), []).rep == DMP([], QQ)

    assert AlgebraicNumber(sqrt(2), [8]).rep == DMP([QQ(8)], QQ)
    assert AlgebraicNumber(sqrt(2), [Rational(8, 3)]).rep == DMP([QQ(8, 3)],
                                                                 QQ)

    assert AlgebraicNumber(sqrt(2), [7, 3]).rep == DMP([QQ(7), QQ(3)], QQ)
    assert AlgebraicNumber(sqrt(2),
                           [Rational(7, 9), Rational(3, 2)]).rep == DMP(
                               [QQ(7, 9), QQ(3, 2)], QQ)

    assert AlgebraicNumber(sqrt(2), [1, 2, 3]).rep == DMP([QQ(2), QQ(5)], QQ)

    a = AlgebraicNumber(AlgebraicNumber(root, gen=x), [1, 2])

    assert a.rep == DMP([QQ(1), QQ(2)], QQ)
    assert a.root == root
    assert a.alias is None
    assert a.minpoly == minpoly
    assert a.is_number

    assert a.is_aliased is False

    assert a.coeffs() == [Integer(1), Integer(2)]
    assert a.native_coeffs() == [QQ(1), QQ(2)]

    a = AlgebraicNumber((minpoly, root), [1, 2])

    assert a.rep == DMP([QQ(1), QQ(2)], QQ)
    assert a.root == root
    assert a.alias is None
    assert a.minpoly == minpoly
    assert a.is_number

    assert a.is_aliased is False

    a = AlgebraicNumber((Poly(minpoly), root), [1, 2])

    assert a.rep == DMP([QQ(1), QQ(2)], QQ)
    assert a.root == root
    assert a.alias is None
    assert a.minpoly == minpoly
    assert a.is_number

    assert a.is_aliased is False

    assert AlgebraicNumber(sqrt(3)).rep == DMP([QQ(1), QQ(0)], QQ)
    assert AlgebraicNumber(-sqrt(3)).rep == DMP([-QQ(1), QQ(0)], QQ)

    a = AlgebraicNumber(sqrt(2))
    b = AlgebraicNumber(sqrt(2))

    assert a == b

    c = AlgebraicNumber(sqrt(2), gen=x)
    d = AlgebraicNumber(sqrt(2), gen=x)

    assert a == b
    assert a == c

    a = AlgebraicNumber(sqrt(2), [1, 2])
    b = AlgebraicNumber(sqrt(2), [1, 3])

    assert a != b and a != sqrt(2) + 3

    assert (a == x) is False and (a != x) is True

    a = AlgebraicNumber(sqrt(2), [1, 0])
    b = AlgebraicNumber(sqrt(2), [1, 0], alias=y)

    assert a.as_poly(x) == Poly(x)
    assert b.as_poly() == Poly(y)

    assert a.as_expr() == sqrt(2)
    assert a.as_expr(x) == x
    assert b.as_expr() == sqrt(2)
    assert b.as_expr(x) == x

    a = AlgebraicNumber(sqrt(2), [2, 3])
    b = AlgebraicNumber(sqrt(2), [2, 3], alias=y)

    p = a.as_poly()

    assert p == Poly(2 * p.gen + 3)

    assert a.as_poly(x) == Poly(2 * x + 3)
    assert b.as_poly() == Poly(2 * y + 3)

    assert a.as_expr() == 2 * sqrt(2) + 3
    assert a.as_expr(x) == 2 * x + 3
    assert b.as_expr() == 2 * sqrt(2) + 3
    assert b.as_expr(x) == 2 * x + 3

    a = AlgebraicNumber(sqrt(2))
    b = to_number_field(sqrt(2))
    assert a.args == b.args == (sqrt(2), Tuple())
    b = AlgebraicNumber(sqrt(2), alias='alpha')
    assert b.args == (sqrt(2), Tuple(), Symbol('alpha'))

    a = AlgebraicNumber(sqrt(2), [1, 2, 3])
    assert a.args == (sqrt(2), Tuple(1, 2, 3))
Beispiel #6
0
def test_AlgebraicNumber():
    minpoly, root = x**2 - 2, sqrt(2)

    a = AlgebraicNumber(root, gen=x)

    assert a.rep == DMP([QQ(1), QQ(0)], QQ)
    assert a.root == root
    assert a.alias is None
    assert a.minpoly == minpoly
    assert a.is_number

    assert a.is_aliased is False

    assert a.coeffs() == [Integer(1), Integer(0)]
    assert a.native_coeffs() == [QQ(1), QQ(0)]

    a = AlgebraicNumber(root, gen=x, alias='y')

    assert a.rep == DMP([QQ(1), QQ(0)], QQ)
    assert a.root == root
    assert a.alias == Symbol('y')
    assert a.minpoly == minpoly
    assert a.is_number

    assert a.is_aliased is True

    a = AlgebraicNumber(root, gen=x, alias=Symbol('y'))

    assert a.rep == DMP([QQ(1), QQ(0)], QQ)
    assert a.root == root
    assert a.alias == Symbol('y')
    assert a.minpoly == minpoly
    assert a.is_number

    assert a.is_aliased is True

    assert AlgebraicNumber(sqrt(2), []).rep == DMP([], QQ)

    assert AlgebraicNumber(sqrt(2), [8]).rep == DMP([QQ(8)], QQ)
    assert AlgebraicNumber(sqrt(2), [Rational(8, 3)]).rep == DMP([QQ(8, 3)],
                                                                 QQ)

    assert AlgebraicNumber(sqrt(2), [7, 3]).rep == DMP([QQ(7), QQ(3)], QQ)
    assert AlgebraicNumber(sqrt(2),
                           [Rational(7, 9), Rational(3, 2)]).rep == DMP(
                               [QQ(7, 9), QQ(3, 2)], QQ)

    assert AlgebraicNumber(sqrt(2), [1, 2, 3]).rep == DMP([QQ(2), QQ(5)], QQ)

    a = AlgebraicNumber(AlgebraicNumber(root, gen=x), [1, 2])

    assert a.rep == DMP([QQ(1), QQ(2)], QQ)
    assert a.root == root
    assert a.alias is None
    assert a.minpoly == minpoly
    assert a.is_number

    assert a.is_aliased is False

    assert a.coeffs() == [Integer(1), Integer(2)]
    assert a.native_coeffs() == [QQ(1), QQ(2)]

    a = AlgebraicNumber((minpoly, root), [1, 2])

    assert a.rep == DMP([QQ(1), QQ(2)], QQ)
    assert a.root == root
    assert a.alias is None
    assert a.minpoly == minpoly
    assert a.is_number

    assert a.is_aliased is False

    a = AlgebraicNumber((Poly(minpoly), root), [1, 2])

    assert a.rep == DMP([QQ(1), QQ(2)], QQ)
    assert a.root == root
    assert a.alias is None
    assert a.minpoly == minpoly
    assert a.is_number

    assert a.is_aliased is False

    assert AlgebraicNumber(sqrt(3)).rep == DMP([QQ(1), QQ(0)], QQ)
    assert AlgebraicNumber(-sqrt(3)).rep == DMP([QQ(1), QQ(0)], QQ)

    a = AlgebraicNumber(sqrt(2))
    b = AlgebraicNumber(sqrt(2))

    assert a == b

    c = AlgebraicNumber(sqrt(2), gen=x)
    d = AlgebraicNumber(sqrt(2), gen=x)

    assert a == b
    assert a == c

    a = AlgebraicNumber(sqrt(2), [1, 2])
    b = AlgebraicNumber(sqrt(2), [1, 3])

    assert a != b and a != sqrt(2) + 3

    assert (a == x) is False and (a != x) is True

    a = AlgebraicNumber(sqrt(2), [1, 0])
    b = AlgebraicNumber(sqrt(2), [1, 0], alias=y)

    assert a.as_poly(x) == Poly(x)
    assert b.as_poly() == Poly(y)

    assert a.as_expr() == sqrt(2)
    assert a.as_expr(x) == x
    assert b.as_expr() == sqrt(2)
    assert b.as_expr(x) == x

    a = AlgebraicNumber(sqrt(2), [2, 3])
    b = AlgebraicNumber(sqrt(2), [2, 3], alias=y)

    p = a.as_poly()

    assert p == Poly(2 * p.gen + 3)

    assert a.as_poly(x) == Poly(2 * x + 3)
    assert b.as_poly() == Poly(2 * y + 3)

    assert a.as_expr() == 2 * sqrt(2) + 3
    assert a.as_expr(x) == 2 * x + 3
    assert b.as_expr() == 2 * sqrt(2) + 3
    assert b.as_expr(x) == 2 * x + 3

    a = AlgebraicNumber(sqrt(2))
    b = to_number_field(sqrt(2))
    assert a.args == b.args == (sqrt(2), Tuple(1, 0))
    b = AlgebraicNumber(sqrt(2), alias='alpha')
    assert b.args == (sqrt(2), Tuple(1, 0), Symbol('alpha'))

    a = AlgebraicNumber(sqrt(2), [1, 2, 3])
    assert a.args == (sqrt(2), Tuple(2, 5))

    pytest.raises(ValueError,
                  lambda: AlgebraicNumber(RootOf(x**3 + y * x + 1, x, 0)))

    a = AlgebraicNumber(RootOf(x**3 + 2 * x - 1, x, 1), alias='alpha')
    assert a.free_symbols == set()

    # integer powers:
    assert a**0 == 1
    assert a**2 == AlgebraicNumber(a, (1, 0, 0), alias='alpha')
    assert a**5 == AlgebraicNumber(a, (1, 0, 0, 0, 0, 0), alias='alpha')
    assert a**110 == AlgebraicNumber(a, ([1] + [0] * 110), alias='alpha')
    assert (a**pi).is_Pow

    b = AlgebraicNumber(sqrt(2), (1, 0), alias='theta')
    c = b + 1
    assert c**2 == 2 * b + 3
    assert c**5 == 29 * b + 41
    assert c**-2 == 3 - 2 * b
    assert c**-11 == 5741 * b - 8119

    # arithmetics
    assert a**3 == -2 * a + 1 == a * (-2) + 1 == 1 + (-2) * a == 1 - 2 * a
    assert a**5 == a**2 + 4 * a - 2
    assert a**4 == -2 * a**2 + a == a - 2 * a**2
    assert a**110 == (-2489094528619081871 * a**2 + 3737645722703173544 * a -
                      1182958048412500088)

    assert a + a == 2 * a
    assert 2 * a - a == a
    assert Integer(1) - a == (-a) + 1

    assert (a + pi).is_Add
    assert (pi + a).is_Add
    assert (a - pi).is_Add
    assert (pi - a).is_Add
    assert (a * pi).is_Mul
    assert (pi * a).is_Mul
Beispiel #7
0
def test_Tuple_equality():
    assert Tuple(1, 2) is not (1, 2)
    assert (Tuple(1, 2) == (1, 2)) is True
    assert (Tuple(1, 2) != (1, 2)) is False
    assert (Tuple(1, 2) == (1, 3)) is False
    assert (Tuple(1, 2) != (1, 3)) is True
    assert (Tuple(1, 2) == Tuple(1, 2)) is True
    assert (Tuple(1, 2) != Tuple(1, 2)) is False
    assert (Tuple(1, 2) == Tuple(1, 3)) is False
    assert (Tuple(1, 2) != Tuple(1, 3)) is True
Beispiel #8
0
def test_sympyissue_6075():
    assert Tuple(1, True).subs({1: 2}) == Tuple(2, True)
Beispiel #9
0
def test_Tuple_concatenation():
    assert Tuple(1, 2) + Tuple(3, 4) == Tuple(1, 2, 3, 4)
    assert (1, 2) + Tuple(3, 4) == Tuple(1, 2, 3, 4)
    assert Tuple(1, 2) + (3, 4) == Tuple(1, 2, 3, 4)
    pytest.raises(TypeError, lambda: Tuple(1, 2) + 3)
    pytest.raises(TypeError, lambda: 1 + Tuple(2, 3))

    # the Tuple case in __radd__ is only reached when a subclass is involved
    class Tuple2(Tuple):
        def __radd__(self, other):
            return Tuple.__radd__(self, other + other)
    assert Tuple(1, 2) + Tuple2(3, 4) == Tuple(1, 2, 1, 2, 3, 4)
    assert Tuple2(1, 2) + Tuple(3, 4) == Tuple(1, 2, 3, 4)
Beispiel #10
0
 def __radd__(self, other):
     return Tuple.__radd__(self, other + other)
Beispiel #11
0
def test_Tuple_contains():
    t1, t2 = Tuple(1), Tuple(2)
    assert t1 in Tuple(1, 2, 3, t1, Tuple(t2))
    assert t2 not in Tuple(1, 2, 3, t1, Tuple(t2))
Beispiel #12
0
def test_Tuple():
    t = (1, 2, 3, 4)
    st = Tuple(*t)
    assert set(sympify(t)) == set(st)
    assert len(t) == len(st)
    assert set(sympify(t[:2])) == set(st[:2])
    assert isinstance(st[:], Tuple)
    assert st == Tuple(1, 2, 3, 4)
    assert st.func(*st.args) == st
    p, q, r, s = symbols('p q r s')
    t2 = (p, q, r, s)
    st2 = Tuple(*t2)
    assert st2.atoms() == set(t2)
    assert st == st2.subs({p: 1, q: 2, r: 3, s: 4})
    # issue sympy/sympy#5505
    assert all(isinstance(arg, Basic) for arg in st.args)
    assert Tuple(p, 1).subs(p, 0) == Tuple(0, 1)
    assert Tuple(p, Tuple(p, 1)).subs(p, 0) == Tuple(0, Tuple(0, 1))

    assert Tuple(t2) == Tuple(Tuple(*t2))

    assert Tuple(1, 2).as_content_primitive() == (1, (1, 2))
Beispiel #13
0
def test_meijer():
    pytest.raises(TypeError, lambda: meijerg(1, z))
    pytest.raises(TypeError, lambda: meijerg(((1,), (2,)), (3,), (4,), z))
    pytest.raises(TypeError, lambda: meijerg((1, 2, 3), (4, 5), z))

    assert meijerg(((1, 2), (3,)), ((4,), (5,)), z) == \
        meijerg(Tuple(1, 2), Tuple(3), Tuple(4), Tuple(5), z)

    g = meijerg((1, 2), (3, 4, 5), (6, 7, 8, 9), (10, 11, 12, 13, 14), z)
    assert g.an == Tuple(1, 2)
    assert g.ap == Tuple(1, 2, 3, 4, 5)
    assert g.aother == Tuple(3, 4, 5)
    assert g.bm == Tuple(6, 7, 8, 9)
    assert g.bq == Tuple(6, 7, 8, 9, 10, 11, 12, 13, 14)
    assert g.bother == Tuple(10, 11, 12, 13, 14)
    assert g.argument == z
    assert g.nu == 75
    assert g.delta == -1
    assert g.is_commutative is True

    assert meijerg([1, 2], [3], [4], [5], z).delta == Rational(1, 2)

    # just a few checks to make sure that all arguments go where they should
    assert tn(meijerg(Tuple(), Tuple(), Tuple(0), Tuple(), -z), exp(z), z)
    assert tn(sqrt(pi)*meijerg(Tuple(), Tuple(),
                               Tuple(0), Tuple(Rational(1, 2)), z**2/4), cos(z), z)
    assert tn(meijerg(Tuple(1, 1), Tuple(), Tuple(1), Tuple(0), z),
              log(1 + z), z)

    # test exceptions
    pytest.raises(ValueError, lambda: meijerg(((3, 1), (2,)),
                                              ((oo,), (2, 0)), x))
    pytest.raises(ValueError, lambda: meijerg(((3, 1), (2,)),
                                              ((1,), (2, 0)), x))

    # differentiation
    g = meijerg((randcplx(),), (randcplx() + 2*I,), Tuple(),
                (randcplx(), randcplx()), z)
    assert td(g, z)

    g = meijerg(Tuple(), (randcplx(),), Tuple(),
                (randcplx(), randcplx()), z)
    assert td(g, z)

    g = meijerg(Tuple(), Tuple(), Tuple(randcplx()),
                Tuple(randcplx(), randcplx()), z)
    assert td(g, z)

    a1, a2, b1, b2, c1, c2, d1, d2 = symbols('a1:3, b1:3, c1:3, d1:3')
    assert meijerg((a1, a2), (b1, b2), (c1, c2), (d1, d2), z).diff(z) == \
        (meijerg((a1 - 1, a2), (b1, b2), (c1, c2), (d1, d2), z)
         + (a1 - 1)*meijerg((a1, a2), (b1, b2), (c1, c2), (d1, d2), z))/z

    assert meijerg([z, z], [], [], [], z).diff(z) == \
        Derivative(meijerg([z, z], [], [], [], z), z)

    # meijerg is unbranched wrt parameters
    assert meijerg([polar_lift(a1)], [polar_lift(a2)], [polar_lift(b1)],
                   [polar_lift(b2)], polar_lift(z)) == meijerg([a1], [a2],
                                                               [b1], [b2],
                                                               polar_lift(z))

    # integrand
    assert meijerg([a], [b], [c], [d], z).integrand(s) == \
        z**s*gamma(c - s)*gamma(-a + s + 1)/(gamma(b - s)*gamma(-d + s + 1))

    assert meijerg([[], []], [[Rational(1, 2)], [0]], 1).is_number
    assert not meijerg([[], []], [[x], [0]], 1).is_number
Beispiel #14
0
 def __radd__(self, other):
     return Tuple.__radd__(self, other + other)
Beispiel #15
0
def test_Tuple_comparision():
    assert (Tuple(1, 3) >= Tuple(-10, 30)) is S.true
    assert (Tuple(1, 3) <= Tuple(-10, 30)) is S.false
    assert (Tuple(1, 3) >= Tuple(1, 3)) is S.true
    assert (Tuple(1, 3) <= Tuple(1, 3)) is S.true
Beispiel #16
0
def test_subs_iter():
    assert x.subs(reversed([[x, y]])) == y
    it = iter([[x, y]])
    assert x.subs(it) == y
    assert x.subs(Tuple((x, y))) == y
Beispiel #17
0
def test_Tuple_tuple_count():
    assert Tuple(0, 1, 2, 3).tuple_count(4) == 0
    assert Tuple(0, 4, 1, 2, 3).tuple_count(4) == 1
    assert Tuple(0, 4, 1, 4, 2, 3).tuple_count(4) == 2
    assert Tuple(0, 4, 1, 4, 2, 4, 3).tuple_count(4) == 3
Beispiel #18
0
def plot_implicit(expr, x_var=None, y_var=None, **kwargs):
    """A plot function to plot implicit equations / inequalities.

    Parameters
    ==========

    ``expr`` : Expr
        The equation / inequality that is to be plotted.
    ``x_var`` : symbol or tuple, optional
        symbol to plot on x-axis or tuple giving symbol and range
        as ``(symbol, xmin, xmax)``
    ``y_var`` : symbol or tuple, optional
        symbol to plot on y-axis or tuple giving symbol and range
        as ``(symbol, ymin, ymax)``

    If neither ``x_var`` nor ``y_var`` are given then the free symbols in the
    expression will be assigned in the order they are sorted.

    The following keyword arguments can also be used:

    ``adaptive`` : Boolean, optional
        The default value is set to True. It has to be
        set to False if you want to use a mesh grid.

    ``depth`` : integer
        The depth of recursion for adaptive mesh grid.
        Default value is 0. Takes value in the range (0, 4).

    ``points`` : integer
        The number of points if adaptive mesh grid is not
        used. Default value is 200.

    ``title`` : str
        The title for the plot.

    ``xlabel`` : str
        The label for the x-axis

    ``ylabel`` : string
        The label for the y-axis

    Aesthetics options:

    ``line_color`` : float or str
        Specifies the color for the plot.  See ``Plot`` to see how to
        set color for the plots.

    plot_implicit, by default, uses interval arithmetic to plot functions. If
    the expression cannot be plotted using interval arithmetic, it defaults to
    a generating a contour using a mesh grid of fixed number of points. By
    setting adaptive to False, you can force plot_implicit to use the mesh
    grid. The mesh grid method can be effective when adaptive plotting using
    interval arithmetic, fails to plot with small line width.

    Examples
    ========

    Plot expressions:

    >>> from diofant import plot_implicit, cos, sin, symbols, Eq, And
    >>> x, y = symbols('x y')

    Without any ranges for the symbols in the expression

    >>> p1 = plot_implicit(Eq(x**2 + y**2, 5))

    With the range for the symbols

    >>> p2 = plot_implicit(Eq(x**2 + y**2, 3),
    ...         (x, -3, 3), (y, -3, 3))

    With depth of recursion as argument.

    >>> p3 = plot_implicit(Eq(x**2 + y**2, 5),
    ...         (x, -4, 4), (y, -4, 4), depth = 2)

    Using mesh grid and not using adaptive meshing.

    >>> p4 = plot_implicit(Eq(x**2 + y**2, 5),
    ...         (x, -5, 5), (y, -2, 2), adaptive=False)

    Using mesh grid with number of points as input.

    >>> p5 = plot_implicit(Eq(x**2 + y**2, 5),
    ...         (x, -5, 5), (y, -2, 2),
    ...         adaptive=False, points=400)

    Plotting regions.

    >>> p6 = plot_implicit(y > x**2)

    Plotting Using boolean conjunctions.

    >>> p7 = plot_implicit(And(y > x, y > -x))

    When plotting an expression with a single variable (y - 1, for example),
    specify the x or the y variable explicitly:

    >>> p8 = plot_implicit(y - 1, y_var=y)
    >>> p9 = plot_implicit(x - 1, x_var=x)

    """

    # Represents whether the expression contains an Equality,
    # GreaterThan or LessThan
    has_equality = False

    def arg_expand(bool_expr):
        """
        Recursively expands the arguments of an Boolean Function
        """
        for arg in bool_expr.args:
            if isinstance(arg, BooleanFunction):
                arg_expand(arg)
            elif isinstance(arg, Relational):
                arg_list.append(arg)

    arg_list = []
    if isinstance(expr, BooleanFunction):
        arg_expand(expr)

        # Check whether there is an equality in the expression provided.
        if any(
                isinstance(e, (Equality, GreaterThan, LessThan))
                for e in arg_list):
            has_equality = True

    elif not isinstance(expr, Relational):
        expr = Eq(expr, 0)
        has_equality = True
    elif isinstance(expr, (Equality, GreaterThan, LessThan)):
        has_equality = True

    xyvar = [i for i in (x_var, y_var) if i is not None]
    free_symbols = expr.free_symbols
    range_symbols = Tuple(*flatten(xyvar)).free_symbols
    undeclared = free_symbols - range_symbols
    if len(free_symbols & range_symbols) > 2:
        raise NotImplementedError("Implicit plotting is not implemented for "
                                  "more than 2 variables")

    # Create default ranges if the range is not provided.
    default_range = Tuple(-5, 5)

    def _range_tuple(s):
        if isinstance(s, (Dummy, Symbol)):
            return Tuple(s) + default_range
        if len(s) == 3:
            return Tuple(*s)
        raise ValueError('symbol or `(symbol, min, max)` expected but got %s' %
                         s)

    if len(xyvar) == 0:
        xyvar = list(_sort_gens(free_symbols))
    var_start_end_x = _range_tuple(xyvar[0])
    x = var_start_end_x[0]
    if len(xyvar) != 2:
        if x in undeclared or not undeclared:
            xyvar.append(Dummy('f(%s)' % x.name))
        else:
            xyvar.append(undeclared.pop())
    var_start_end_y = _range_tuple(xyvar[1])

    use_interval = kwargs.pop('adaptive', True)
    nb_of_points = kwargs.pop('points', 300)
    depth = kwargs.pop('depth', 0)
    line_color = kwargs.pop('line_color', "blue")
    # Check whether the depth is greater than 4 or less than 0.
    if depth > 4:
        depth = 4
    elif depth < 0:
        depth = 0

    series_argument = ImplicitSeries(expr, var_start_end_x, var_start_end_y,
                                     has_equality, use_interval, depth,
                                     nb_of_points, line_color)
    show = kwargs.pop('show', True)

    # set the x and y limits
    kwargs['xlim'] = tuple(float(x) for x in var_start_end_x[1:])
    kwargs['ylim'] = tuple(float(y) for y in var_start_end_y[1:])
    # set the x and y labels
    kwargs.setdefault('xlabel', var_start_end_x[0].name)
    kwargs.setdefault('ylabel', var_start_end_y[0].name)
    p = Plot(series_argument, **kwargs)
    if show:
        p.show()
    return p
Beispiel #19
0
def test_trace_new():
    a, b, c, d, Y = symbols('a b c d Y')
    A, B, C, D = symbols('A B C D', commutative=False)

    assert Tr(a + b) == a + b
    assert Tr(A + B) == Tr(A) + Tr(B)

    # check trace args not implicitly permuted
    assert Tr(C * D * A * B).args[0].args == (C, D, A, B)

    # check for mul and adds
    assert Tr((a * b) + (c * d)) == (a * b) + (c * d)
    # Tr(scalar*A) = scalar*Tr(A)
    assert Tr(a * A) == a * Tr(A)
    assert Tr(a * A * B * b) == a * b * Tr(A * B)

    # since A is symbol and not commutative
    assert isinstance(Tr(A), Tr)

    # POW
    assert Tr(pow(a, b)) == a**b
    assert isinstance(Tr(pow(A, a)), Tr)

    # Matrix
    M = Matrix([[1, 1], [2, 2]])
    assert Tr(M) == 3

    # test indices in different forms
    # no index
    t = Tr(A)
    assert t.args[1] == Tuple()

    # single index
    t = Tr(A, 0)
    assert t.args[1] == Tuple(0)

    # index in a list
    t = Tr(A, [0])
    assert t.args[1] == Tuple(0)

    t = Tr(A, [0, 1, 2])
    assert t.args[1] == Tuple(0, 1, 2)

    # index is tuple
    t = Tr(A, (0))
    assert t.args[1] == Tuple(0)

    t = Tr(A, (1, 2))
    assert t.args[1] == Tuple(1, 2)

    # trace indices test
    t = Tr((A + B), [2])
    assert t.args[0].args[1] == Tuple(2) and t.args[1].args[1] == Tuple(2)

    t = Tr(a * A, [2, 3])
    assert t.args[1].args[1] == Tuple(2, 3)

    # class with trace method defined
    # to simulate numpy objects
    class Foo:
        def trace(self):
            return 1

    assert Tr(Foo()) == 1

    # argument test
    # check for value error, when either/both arguments are not provided
    pytest.raises(ValueError, lambda: Tr())
    pytest.raises(ValueError, lambda: Tr(A, 1, 2))

    # non-Expr objects
    assert isinstance(Tr(None), Tr)
Beispiel #20
0
def test_sympyissue_3982():
    a = [3, 2.0]
    assert sympify(a) == [Integer(3), Float(2.0)]
    assert sympify(tuple(a)) == Tuple(Integer(3), Float(2.0))
    assert sympify(set(a)) == {Integer(3), Float(2.0)}
Beispiel #21
0
def test_deriv1():
    # These all requre derivatives evaluated at a point (issue sympy/sympy#4719) to work.
    # See issue sympy/sympy#4624
    assert f(
        2 * x).diff(x) == 2 * Subs(Derivative(f(x), x), Tuple(x), Tuple(2 * x))
    assert (f(x)**3).diff(x) == 3 * f(x)**2 * f(x).diff(x)
    assert (f(2 * x)**3).diff(x) == 6 * f(2 * x)**2 * Subs(
        Derivative(f(x), x), Tuple(x), Tuple(2 * x))

    assert f(2 + x).diff(x) == Subs(Derivative(f(x), x), Tuple(x),
                                    Tuple(x + 2))
    assert f(2 + 3 * x).diff(x) == 3 * Subs(Derivative(f(x), x), Tuple(x),
                                            Tuple(3 * x + 2))
    assert f(3 * sin(x)).diff(x) == 3 * cos(x) * Subs(Derivative(
        f(x), x), Tuple(x), Tuple(3 * sin(x)))

    # See issue sympy/sympy#8510
    assert f(x, x + z).diff(x) == Subs(Derivative(f(y, x + z), y), Tuple(y), Tuple(x)) \
            + Subs(Derivative(f(x, y), y), Tuple(y), Tuple(x + z))
    assert f(x, x**2).diff(x) == Subs(Derivative(f(y, x**2), y), Tuple(y), Tuple(x)) \
            + 2*x*Subs(Derivative(f(x, y), y), Tuple(y), Tuple(x**2))