Beispiel #1
0
def test_Term():
    a = Term(4 * x * y**2 / z / t**3)
    b = Term(2 * x**3 * y**5 / t**3)

    assert a == Term(4, Factors({x: 1, y: 2}), Factors({z: 1, t: 3}))
    assert b == Term(2, Factors({x: 3, y: 5}), Factors({t: 3}))

    assert a.as_expr() == 4 * x * y**2 / z / t**3
    assert b.as_expr() == 2 * x**3 * y**5 / t**3

    assert a.inv() == \
        Term(Rational(1, 4), Factors({z: 1, t: 3}), Factors({x: 1, y: 2}))
    assert b.inv() == Term(Rational(1, 2), Factors({t: 3}),
                           Factors({
                               x: 3,
                               y: 5
                           }))

    assert a.mul(b) == a*b == \
        Term(8, Factors({x: 4, y: 7}), Factors({z: 1, t: 6}))
    assert a.quo(b) == a / b == Term(2, Factors({}), Factors({
        x: 2,
        y: 3,
        z: 1
    }))

    assert a.pow(3) == a**3 == \
        Term(64, Factors({x: 3, y: 6}), Factors({z: 3, t: 9}))
    assert b.pow(3) == b**3 == Term(8, Factors({x: 9, y: 15}), Factors({t: 9}))

    assert a.pow(-3) == a**(-3) == \
        Term(Rational(1, 64), Factors({z: 3, t: 9}), Factors({x: 3, y: 6}))
    assert b.pow(-3) == b**(-3) == \
        Term(Rational(1, 8), Factors({t: 9}), Factors({x: 9, y: 15}))

    assert a.gcd(b) == Term(2, Factors({x: 1, y: 2}), Factors({t: 3}))
    assert a.lcm(b) == Term(4, Factors({x: 3, y: 5}), Factors({z: 1, t: 3}))

    a = Term(4 * x * y**2 / z / t**3)
    b = Term(2 * x**3 * y**5 * t**7)

    assert a.mul(b) == Term(8, Factors({x: 4, y: 7, t: 4}), Factors({z: 1}))

    assert Term((2 * x + 2)**3) == Term(8, Factors({x + 1: 3}), Factors({}))
    assert Term((2*x + 2)*(3*x + 6)**2) == \
        Term(18, Factors({x + 1: 1, x + 2: 2}), Factors({}))

    A = Symbol('A', commutative=False)
    pytest.raises(NonCommutativeExpression, lambda: Term(A))

    f1, f2 = Factors({x: 2}), Factors()
    assert Term(2, numer=f1) == Term(2, f1, f2)
    assert Term(2, denom=f1) == Term(2, f2, f1)

    pytest.raises(TypeError, lambda: a * 2)
    pytest.raises(TypeError, lambda: a / 3)
    pytest.raises(TypeError, lambda: a**3.1)
Beispiel #2
0
def test_Term():
    a = Term(4*x*y**2/z/t**3)
    b = Term(2*x**3*y**5/t**3)

    assert a == Term(4, Factors({x: 1, y: 2}), Factors({z: 1, t: 3}))
    assert b == Term(2, Factors({x: 3, y: 5}), Factors({t: 3}))

    assert a.as_expr() == 4*x*y**2/z/t**3
    assert b.as_expr() == 2*x**3*y**5/t**3

    assert a.inv() == \
        Term(Rational(1, 4), Factors({z: 1, t: 3}), Factors({x: 1, y: 2}))
    assert b.inv() == Term(Rational(1, 2), Factors({t: 3}), Factors({x: 3, y: 5}))

    assert a.mul(b) == a*b == \
        Term(8, Factors({x: 4, y: 7}), Factors({z: 1, t: 6}))
    assert a.quo(b) == a/b == Term(2, Factors({}), Factors({x: 2, y: 3, z: 1}))

    assert a.pow(3) == a**3 == \
        Term(64, Factors({x: 3, y: 6}), Factors({z: 3, t: 9}))
    assert b.pow(3) == b**3 == Term(8, Factors({x: 9, y: 15}), Factors({t: 9}))

    assert a.pow(-3) == a**(-3) == \
        Term(Rational(1, 64), Factors({z: 3, t: 9}), Factors({x: 3, y: 6}))
    assert b.pow(-3) == b**(-3) == \
        Term(Rational(1, 8), Factors({t: 9}), Factors({x: 9, y: 15}))

    assert a.gcd(b) == Term(2, Factors({x: 1, y: 2}), Factors({t: 3}))
    assert a.lcm(b) == Term(4, Factors({x: 3, y: 5}), Factors({z: 1, t: 3}))

    a = Term(4*x*y**2/z/t**3)
    b = Term(2*x**3*y**5*t**7)

    assert a.mul(b) == Term(8, Factors({x: 4, y: 7, t: 4}), Factors({z: 1}))

    assert Term((2*x + 2)**3) == Term(8, Factors({x + 1: 3}), Factors({}))
    assert Term((2*x + 2)*(3*x + 6)**2) == \
        Term(18, Factors({x + 1: 1, x + 2: 2}), Factors({}))

    A = Symbol('A', commutative=False)
    pytest.raises(NonCommutativeExpression, lambda: Term(A))

    f1, f2 = Factors({x: 2}), Factors()
    assert Term(2, numer=f1) == Term(2, f1, f2)
    assert Term(2, denom=f1) == Term(2, f2, f1)

    pytest.raises(TypeError, lambda: a*2)
    pytest.raises(TypeError, lambda: a/3)
    pytest.raises(TypeError, lambda: a**3.1)
Beispiel #3
0
def test_Term():
    a = Term(4 * x * y**2 / z / t**3)
    b = Term(2 * x**3 * y**5 / t**3)

    assert a == Term(4, Factors({x: 1, y: 2}), Factors({z: 1, t: 3}))
    assert b == Term(2, Factors({x: 3, y: 5}), Factors({t: 3}))

    assert a.as_expr() == 4 * x * y**2 / z / t**3
    assert b.as_expr() == 2 * x**3 * y**5 / t**3

    assert a.inv() == \
        Term(Rational(1, 4), Factors({z: 1, t: 3}), Factors({x: 1, y: 2}))
    assert b.inv() == Term(Rational(1, 2), Factors({t: 3}),
                           Factors({
                               x: 3,
                               y: 5
                           }))

    assert a.mul(b) == a*b == \
        Term(8, Factors({x: 4, y: 7}), Factors({z: 1, t: 6}))
    assert a.quo(b) == a / b == Term(2, Factors({}), Factors({
        x: 2,
        y: 3,
        z: 1
    }))

    assert a.pow(3) == a**3 == \
        Term(64, Factors({x: 3, y: 6}), Factors({z: 3, t: 9}))
    assert b.pow(3) == b**3 == Term(8, Factors({x: 9, y: 15}), Factors({t: 9}))

    assert a.pow(-3) == a**(-3) == \
        Term(Rational(1, 64), Factors({z: 3, t: 9}), Factors({x: 3, y: 6}))
    assert b.pow(-3) == b**(-3) == \
        Term(Rational(1, 8), Factors({t: 9}), Factors({x: 9, y: 15}))

    assert a.gcd(b) == Term(2, Factors({x: 1, y: 2}), Factors({t: 3}))
    assert a.lcm(b) == Term(4, Factors({x: 3, y: 5}), Factors({z: 1, t: 3}))

    a = Term(4 * x * y**2 / z / t**3)
    b = Term(2 * x**3 * y**5 * t**7)

    assert a.mul(b) == Term(8, Factors({x: 4, y: 7, t: 4}), Factors({z: 1}))

    assert Term((2 * x + 2)**3) == Term(8, Factors({x + 1: 3}), Factors({}))
    assert Term((2*x + 2)*(3*x + 6)**2) == \
        Term(18, Factors({x + 1: 1, x + 2: 2}), Factors({}))
Beispiel #4
0
def test_Factors():
    assert Factors() == Factors({}) == Factors(Integer(1))
    assert Factors(Integer(1)) == Factors(Factors(Integer(1)))
    assert Factors().as_expr() == 1
    assert Factors({
        x: 2,
        y: 3,
        sin(x): 4
    }).as_expr() == x**2 * y**3 * sin(x)**4
    assert Factors(+oo) == Factors({oo: 1})
    assert Factors(-oo) == Factors({oo: 1, -1: 1})

    f1 = Factors({oo: 1})
    f2 = Factors({oo: 1})
    assert hash(f1) == hash(f2)

    a = Factors({x: 5, y: 3, z: 7})
    b = Factors({y: 4, z: 3, t: 10})

    assert a.mul(b) == a * b == Factors({x: 5, y: 7, z: 10, t: 10})

    assert a.div(b) == divmod(a, b) == \
        (Factors({x: 5, z: 4}), Factors({y: 1, t: 10}))
    assert a.quo(b) == a / b == Factors({x: 5, z: 4})
    assert a.rem(b) == a % b == Factors({y: 1, t: 10})

    assert a.pow(3) == a**3 == Factors({x: 15, y: 9, z: 21})
    assert b.pow(3) == b**3 == Factors({y: 12, z: 9, t: 30})

    pytest.raises(ValueError, lambda: a.pow(3.1))
    pytest.raises(ValueError, lambda: a.pow(Factors(3.1)))

    assert a.pow(0) == Factors()

    assert a.gcd(b) == Factors({y: 3, z: 3})
    assert a.lcm(b) == a.lcm(b.as_expr()) == Factors({x: 5, y: 4, z: 7, t: 10})

    a = Factors({x: 4, y: 7, t: 7})
    b = Factors({z: 1, t: 3})

    assert a.normal(b) == (Factors({x: 4, y: 7, t: 4}), Factors({z: 1}))

    assert Factors(sqrt(2) * x).as_expr() == sqrt(2) * x

    assert Factors(-I) * I == Factors()
    assert Factors({Integer(-1): Integer(3)})*Factors({Integer(-1): Integer(1), I: Integer(5)}) == \
        Factors(I)

    assert Factors(Integer(2)**x).div(Integer(3)**x) == \
        (Factors({Integer(2): x}), Factors({Integer(3): x}))
    assert Factors(2**(2*x + 2)).div(Integer(8)) == \
        (Factors({Integer(2): 2*x + 2}), Factors({Integer(8): Integer(1)}))

    # coverage
    # /!\ things break if this is not True
    assert Factors({Integer(-1): Rational(3, 2)}) == Factors({I: 1, -1: 1})
    assert Factors({
        I: Integer(1),
        Integer(-1): Rational(1, 3)
    }).as_expr() == I * cbrt(-1)

    assert Factors(-1.) == Factors({Integer(-1): Integer(1), Float(1.): 1})
    assert Factors(-2.) == Factors({Integer(-1): Integer(1), Float(2.): 1})
    assert Factors((-2.)**x) == Factors({Float(-2.): x})
    assert Factors(Integer(-2)) == Factors({
        Integer(-1): Integer(1),
        Integer(2): 1
    })
    assert Factors(Rational(1, 2)) == Factors({Integer(2): -1})
    assert Factors(Rational(3, 2)) == Factors({
        Integer(3): 1,
        Integer(2): Integer(-1)
    })
    assert Factors({I: Integer(1)}) == Factors(I)
    assert Factors({-1.0: 2, I: 1}) == Factors({Float(1.0): 1, I: 1})
    assert Factors({-1: -Rational(3, 2)}).as_expr() == I
    A = symbols('A', commutative=False)
    assert Factors(2 * A**2) == Factors({Integer(2): 1, A**2: 1})
    assert Factors(I) == Factors({I: 1})
    assert Factors(x).normal(Integer(2)) == (Factors(x), Factors(Integer(2)))
    assert Factors(x).normal(Integer(0)) == (Factors(), Factors(Integer(0)))
    pytest.raises(ZeroDivisionError, lambda: Factors(x).div(Integer(0)))
    assert Factors(x).mul(Integer(2)) == Factors(2 * x)
    assert Factors(x).mul(Integer(0)).is_zero
    assert Factors(x).mul(1 / x).is_one
    assert Factors(x**sqrt(8)).as_expr() == x**(2 * sqrt(2))
    assert Factors(x)**Factors(Integer(2)) == Factors(x**2)
    assert Factors(x).gcd(Integer(0)) == Factors(x)
    assert Factors(x).lcm(Integer(0)).is_zero
    assert Factors(Integer(0)).div(x) == (Factors(Integer(0)), Factors())
    assert Factors(x).div(x) == (Factors(), Factors())
    assert Factors({x: .2}) / Factors({x: .2}) == Factors()
    assert Factors(x) != Factors()
    assert Factors(x) == x
    assert Factors(Integer(0)).normal(x) == (Factors(Integer(0)), Factors())
    n, d = x**(2 + y), x**2
    f = Factors(n)
    assert f.div(d) == f.normal(d) == (Factors(x**y), Factors())
    assert f.gcd(d) == Factors()
    d = x**y
    assert f.div(d) == f.normal(d) == (Factors(x**2), Factors())
    assert f.gcd(d) == Factors(d)
    n = d = 2**x
    f = Factors(n)
    assert f.div(d) == f.normal(d) == (Factors(), Factors())
    assert f.gcd(d) == Factors(d)
    n, d = 2**x, 2**y
    f = Factors(n)
    assert f.div(d) == f.normal(d) == (Factors({Integer(2): x
                                                }), Factors({Integer(2): y}))
    assert f.gcd(d) == Factors()

    assert f.div(f) == (Factors(), Factors())

    # extraction of constant only
    n = x**(x + 3)
    assert Factors(n).normal(x**-3) == (Factors({x: x + 6}), Factors({}))
    assert Factors(n).normal(x**3) == (Factors({x: x}), Factors({}))
    assert Factors(n).normal(x**4) == (Factors({x: x}), Factors({x: 1}))
    assert Factors(n).normal(x**(y - 3)) == \
        (Factors({x: x + 6}), Factors({x: y}))
    assert Factors(n).normal(x**(y + 3)) == (Factors({x: x}), Factors({x: y}))
    assert Factors(n).normal(x**(y + 4)) == \
        (Factors({x: x}), Factors({x: y + 1}))

    assert Factors(n).div(x**-3) == (Factors({x: x + 6}), Factors({}))
    assert Factors(n).div(x**3) == (Factors({x: x}), Factors({}))
    assert Factors(n).div(x**4) == (Factors({x: x}), Factors({x: 1}))
    assert Factors(n).div(x**(y - 3)) == \
        (Factors({x: x + 6}), Factors({x: y}))
    assert Factors(n).div(x**(y + 3)) == (Factors({x: x}), Factors({x: y}))
    assert Factors(n).div(x**(y + 4)) == \
        (Factors({x: x}), Factors({x: y + 1}))

    assert Factors({I: I}).as_expr() == (-1)**(I / 2)
    assert Factors({-1: Rational(4, 3)}).as_expr() == -cbrt(-1)
Beispiel #5
0
def test_Factors():
    assert Factors() == Factors({}) == Factors(Integer(1))
    assert Factors(Integer(1)) == Factors(Factors(Integer(1)))
    assert Factors().as_expr() == 1
    assert Factors({x: 2, y: 3, sin(x): 4}).as_expr() == x**2*y**3*sin(x)**4
    assert Factors(+oo) == Factors({oo: 1})
    assert Factors(-oo) == Factors({oo: 1, -1: 1})

    f1 = Factors({oo: 1})
    f2 = Factors({oo: 1})
    assert hash(f1) == hash(f2)

    a = Factors({x: 5, y: 3, z: 7})
    b = Factors({      y: 4, z: 3, t: 10})

    assert a.mul(b) == a*b == Factors({x: 5, y: 7, z: 10, t: 10})

    assert a.div(b) == divmod(a, b) == \
        (Factors({x: 5, z: 4}), Factors({y: 1, t: 10}))
    assert a.quo(b) == a/b == Factors({x: 5, z: 4})
    assert a.rem(b) == a % b == Factors({y: 1, t: 10})

    assert a.pow(3) == a**3 == Factors({x: 15, y: 9, z: 21})
    assert b.pow(3) == b**3 == Factors({y: 12, z: 9, t: 30})

    pytest.raises(ValueError, lambda: a.pow(3.1))
    pytest.raises(ValueError, lambda: a.pow(Factors(3.1)))

    assert a.pow(0) == Factors()

    assert a.gcd(b) == Factors({y: 3, z: 3})
    assert a.lcm(b) == a.lcm(b.as_expr()) == Factors({x: 5, y: 4, z: 7, t: 10})

    a = Factors({x: 4, y: 7, t: 7})
    b = Factors({z: 1, t: 3})

    assert a.normal(b) == (Factors({x: 4, y: 7, t: 4}), Factors({z: 1}))

    assert Factors(sqrt(2)*x).as_expr() == sqrt(2)*x

    assert Factors(-I)*I == Factors()
    assert Factors({Integer(-1): Integer(3)})*Factors({Integer(-1): Integer(1), I: Integer(5)}) == \
        Factors(I)

    assert Factors(Integer(2)**x).div(Integer(3)**x) == \
        (Factors({Integer(2): x}), Factors({Integer(3): x}))
    assert Factors(2**(2*x + 2)).div(Integer(8)) == \
        (Factors({Integer(2): 2*x + 2}), Factors({Integer(8): Integer(1)}))

    # coverage
    # /!\ things break if this is not True
    assert Factors({Integer(-1): Rational(3, 2)}) == Factors({I: 1, -1: 1})
    assert Factors({I: Integer(1), Integer(-1): Rational(1, 3)}).as_expr() == I*cbrt(-1)

    assert Factors(-1.) == Factors({Integer(-1): Integer(1), Float(1.): 1})
    assert Factors(-2.) == Factors({Integer(-1): Integer(1), Float(2.): 1})
    assert Factors((-2.)**x) == Factors({Float(-2.): x})
    assert Factors(Integer(-2)) == Factors({Integer(-1): Integer(1), Integer(2): 1})
    assert Factors(Rational(1, 2)) == Factors({Integer(2): -1})
    assert Factors(Rational(3, 2)) == Factors({Integer(3): 1, Integer(2): Integer(-1)})
    assert Factors({I: Integer(1)}) == Factors(I)
    assert Factors({-1.0: 2, I: 1}) == Factors({Float(1.0): 1, I: 1})
    assert Factors({-1: -Rational(3, 2)}).as_expr() == I
    A = symbols('A', commutative=False)
    assert Factors(2*A**2) == Factors({Integer(2): 1, A**2: 1})
    assert Factors(I) == Factors({I: 1})
    assert Factors(x).normal(Integer(2)) == (Factors(x), Factors(Integer(2)))
    assert Factors(x).normal(Integer(0)) == (Factors(), Factors(Integer(0)))
    pytest.raises(ZeroDivisionError, lambda: Factors(x).div(Integer(0)))
    assert Factors(x).mul(Integer(2)) == Factors(2*x)
    assert Factors(x).mul(Integer(0)).is_zero
    assert Factors(x).mul(1/x).is_one
    assert Factors(x**sqrt(8)).as_expr() == x**(2*sqrt(2))
    assert Factors(x)**Factors(Integer(2)) == Factors(x**2)
    assert Factors(x).gcd(Integer(0)) == Factors(x)
    assert Factors(x).lcm(Integer(0)).is_zero
    assert Factors(Integer(0)).div(x) == (Factors(Integer(0)), Factors())
    assert Factors(x).div(x) == (Factors(), Factors())
    assert Factors({x: .2})/Factors({x: .2}) == Factors()
    assert Factors(x) != Factors()
    assert Factors(x) == x
    assert Factors(Integer(0)).normal(x) == (Factors(Integer(0)), Factors())
    n, d = x**(2 + y), x**2
    f = Factors(n)
    assert f.div(d) == f.normal(d) == (Factors(x**y), Factors())
    assert f.gcd(d) == Factors()
    d = x**y
    assert f.div(d) == f.normal(d) == (Factors(x**2), Factors())
    assert f.gcd(d) == Factors(d)
    n = d = 2**x
    f = Factors(n)
    assert f.div(d) == f.normal(d) == (Factors(), Factors())
    assert f.gcd(d) == Factors(d)
    n, d = 2**x, 2**y
    f = Factors(n)
    assert f.div(d) == f.normal(d) == (Factors({Integer(2): x}), Factors({Integer(2): y}))
    assert f.gcd(d) == Factors()

    assert f.div(f) == (Factors(), Factors())

    # extraction of constant only
    n = x**(x + 3)
    assert Factors(n).normal(x**-3) == (Factors({x: x + 6}), Factors({}))
    assert Factors(n).normal(x**3) == (Factors({x: x}), Factors({}))
    assert Factors(n).normal(x**4) == (Factors({x: x}), Factors({x: 1}))
    assert Factors(n).normal(x**(y - 3)) == \
        (Factors({x: x + 6}), Factors({x: y}))
    assert Factors(n).normal(x**(y + 3)) == (Factors({x: x}), Factors({x: y}))
    assert Factors(n).normal(x**(y + 4)) == \
        (Factors({x: x}), Factors({x: y + 1}))

    assert Factors(n).div(x**-3) == (Factors({x: x + 6}), Factors({}))
    assert Factors(n).div(x**3) == (Factors({x: x}), Factors({}))
    assert Factors(n).div(x**4) == (Factors({x: x}), Factors({x: 1}))
    assert Factors(n).div(x**(y - 3)) == \
        (Factors({x: x + 6}), Factors({x: y}))
    assert Factors(n).div(x**(y + 3)) == (Factors({x: x}), Factors({x: y}))
    assert Factors(n).div(x**(y + 4)) == \
        (Factors({x: x}), Factors({x: y + 1}))

    assert Factors({I: I}).as_expr() == (-1)**(I/2)
    assert Factors({-1: Rational(4, 3)}).as_expr() == -cbrt(-1)