示例#1
0
def test_list():
    assert sstr([x]) == "[x]"
    assert str([x]) == "[Symbol('x')]"
    assert sstr([x**2, x*y + 1]) == "[x**2, x*y + 1]"
    assert str([x**2, x*y + 1]) == "[Pow(Symbol('x'), Integer(2)), Add(Mul(Symbol('x'), Symbol('y')), Integer(1))]"
    assert sstr([x**2, [y + x]]) == "[x**2, [x + y]]"
    assert str([x**2, [y + x]]) == "[Pow(Symbol('x'), Integer(2)), [Add(Symbol('x'), Symbol('y'))]]"
示例#2
0
def test_Differential():
    tp = TensorProduct(R2.dx, R2.dy)
    assert sstr(LieDerivative(R2.e_x, tp)) == 'LieDerivative(e_x, TensorProduct(dx, dy))'

    g = Function('g')
    s_field = g(R2.x, R2.y)
    assert sstr(Differential(s_field)) == 'd(g(x, y))'
示例#3
0
def test_Dict():
    assert str(Dict({1: 1 + x})) == sstr({1: 1 + x}) == "{1: x + 1}"
    assert str(Dict({
        1: x**2,
        2: y * x
    })) in ("{1: x**2, 2: x*y}", "{2: x*y, 1: x**2}")
    assert sstr(Dict({1: x**2, 2: y * x})) == "{1: x**2, 2: x*y}"
示例#4
0
def test_set():
    assert sstr(set()) == 'set()'
    assert sstr(frozenset()) == 'frozenset()'

    assert sstr({1, 2, 3}) == '{1, 2, 3}'
    assert sstr(frozenset({1, 2, 3})) == 'frozenset({1, 2, 3})'
    assert sstr({1, x, x**2, x**3, x**4}) == '{1, x, x**2, x**3, x**4}'
示例#5
0
def test_sstrrepr():
    assert sstr('abc') == 'abc'
    assert sstrrepr('abc') == "'abc'"

    e = ['a', 'b', 'c', x]
    assert sstr(e) == "[a, b, c, x]"
    assert sstrrepr(e) == "['a', 'b', 'c', x]"
示例#6
0
def test_sstrrepr():
    assert sstr('abc') == 'abc'
    assert sstrrepr('abc') == "'abc'"

    e = ['a', 'b', 'c', x]
    assert sstr(e) == "[a, b, c, x]"
    assert sstrrepr(e) == "['a', 'b', 'c', x]"
示例#7
0
def test_tuple():
    assert sstr((x,)) == "(x,)"
    assert str((x,)) == "(Symbol('x'),)"
    assert sstr((x + y, 1 + x)) == "(x + y, x + 1)"
    assert str((x + y, 1 + x)) == "(Add(Symbol('x'), Symbol('y')), Add(Symbol('x'), Integer(1)))"
    assert sstr((x + y, (1 + x, x**2))) == "(x + y, (x + 1, x**2))"
    assert str((x + y, (1 + x, x**2))) == "(Add(Symbol('x'), Symbol('y')), (Add(Symbol('x'), Integer(1)), Pow(Symbol('x'), Integer(2))))"
示例#8
0
def test_list():
    assert sstr([x]) == "[x]"
    assert str([x]) == "[Symbol('x')]"
    assert sstr([x**2, x*y + 1]) == "[x**2, x*y + 1]"
    assert str([x**2, x*y + 1]) == "[Pow(Symbol('x'), Integer(2)), Add(Mul(Symbol('x'), Symbol('y')), Integer(1))]"
    assert sstr([x**2, [y + x]]) == "[x**2, [x + y]]"
    assert str([x**2, [y + x]]) == "[Pow(Symbol('x'), Integer(2)), [Add(Symbol('x'), Symbol('y'))]]"
示例#9
0
def test_Differential():
    tp = TensorProduct(R2.dx, R2.dy)
    assert sstr(LieDerivative(R2.e_x, tp)) == 'LieDerivative(e_x, TensorProduct(dx, dy))'

    g = Function('g')
    s_field = g(R2.x, R2.y)
    assert sstr(Differential(s_field)) == 'd(g(x, y))'
示例#10
0
def test_tuple():
    assert sstr((x,)) == "(x,)"
    assert str((x,)) == "(Symbol('x'),)"
    assert sstr((x + y, 1 + x)) == "(x + y, x + 1)"
    assert str((x + y, 1 + x)) == "(Add(Symbol('x'), Symbol('y')), Add(Symbol('x'), Integer(1)))"
    assert sstr((x + y, (1 + x, x**2))) == "(x + y, (x + 1, x**2))"
    assert str((x + y, (1 + x, x**2))) == "(Add(Symbol('x'), Symbol('y')), (Add(Symbol('x'), Integer(1)), Pow(Symbol('x'), Integer(2))))"
示例#11
0
def test_noncommutative():
    A, B, C = symbols('A,B,C', commutative=False)

    assert sstr(A*B*C**-1) == "A*B*C**(-1)"
    assert sstr(C**-1*A*B) == "C**(-1)*A*B"
    assert sstr(A*C**-1*B) == "A*C**(-1)*B"
    assert sstr(sqrt(A)) == "sqrt(A)"
    assert sstr(1/sqrt(A)) == "A**(-1/2)"
示例#12
0
def test_set():
    assert sstr(set()) == 'set()'
    assert sstr(frozenset()) == 'frozenset()'

    assert sstr({1, 2, 3}) == '{1, 2, 3}'
    assert sstr(frozenset({1, 2, 3})) == 'frozenset({1, 2, 3})'
    assert sstr(
        {1, x, x**2, x**3, x**4}) == '{1, x, x**2, x**3, x**4}'
示例#13
0
def test_noncommutative():
    A, B, C = symbols('A,B,C', commutative=False)

    assert sstr(A * B * C**-1) == "A*B*C**(-1)"
    assert sstr(C**-1 * A * B) == "C**(-1)*A*B"
    assert sstr(A * C**-1 * B) == "A*C**(-1)*B"
    assert sstr(sqrt(A)) == "sqrt(A)"
    assert sstr(1 / sqrt(A)) == "A**(-1/2)"
示例#14
0
def test_intcurve_diffequ():
    start_point = R2_r.point([1, 0])
    vector_field = -R2.y*R2.e_x + R2.x*R2.e_y
    equations, init_cond = intcurve_diffequ(vector_field, t, start_point)
    assert sstr(equations) == '[f_1(t) + Derivative(f_0(t), t), -f_0(t) + Derivative(f_1(t), t)]'
    assert sstr(init_cond) == '[f_0(0) - 1, f_1(0)]'
    equations, init_cond = intcurve_diffequ(vector_field, t, start_point, R2_p)
    assert sstr(
        equations) == '[Derivative(f_0(t), t), Derivative(f_1(t), t) - 1]'
    assert sstr(init_cond) == '[f_0(0) - 1, f_1(0)]'
示例#15
0
def test_printmethod():
    class R(Abs):
        def _diofantstr(self, printer):
            return "foo(%s)" % printer._print(self.args[0])
    assert sstr(R(x)) == "foo(x)"

    class R(Abs):
        def _diofantstr(self, printer):
            return "foo"
    assert sstr(R(x)) == "foo"
示例#16
0
def test_dict():
    assert sstr({1: 1 + x}) == "{1: x + 1}"
    assert str({1: 1 + x}) == "{1: Add(Symbol('x'), Integer(1))}"
    assert str({
        1: x**2,
        2: y * x
    }) in (
        "{1: Pow(Symbol('x'), Integer(2)), 2: Mul(Symbol('x'), Symbol('y'))}",
        "{1: Mul(Symbol('x'), Symbol('y')), 2: Pow(Symbol('x'), Integer(2))}")
    assert sstr({1: x**2, 2: y * x}) == "{1: x**2, 2: x*y}"
示例#17
0
def test_printmethod():
    class R(Abs):
        def _diofantstr(self, printer):
            return "foo(%s)" % printer._print(self.args[0])
    assert sstr(R(x)) == "foo(x)"

    class R(Abs):
        def _diofantstr(self, printer):
            return "foo"
    assert sstr(R(x)) == "foo"
示例#18
0
def test_Matrix_str():
    M = Matrix([[x**+1, 1], [y, x + y]])
    assert str(M) == "Matrix([\n[x,     1],\n[y, x + y]])"
    assert sstr(M) == "Matrix([\n[x,     1],\n[y, x + y]])"
    M = Matrix([[1]])
    assert str(M) == sstr(M) == "Matrix([[1]])"
    M = Matrix([[1, 2]])
    assert str(M) == sstr(M) == "Matrix([[1, 2]])"
    M = Matrix()
    assert str(M) == sstr(M) == "Matrix(0, 0, [])"
    M = Matrix(0, 1, lambda i, j: 0)
    assert str(M) == sstr(M) == "Matrix(0, 1, [])"
示例#19
0
def test_Matrix_str():
    M = Matrix([[x**+1, 1], [y, x + y]])
    assert str(M) == "Matrix([\n[x,     1],\n[y, x + y]])"
    assert sstr(M) == "Matrix([\n[x,     1],\n[y, x + y]])"
    M = Matrix([[1]])
    assert str(M) == sstr(M) == "Matrix([[1]])"
    M = Matrix([[1, 2]])
    assert str(M) == sstr(M) == "Matrix([[1, 2]])"
    M = Matrix()
    assert str(M) == sstr(M) == "Matrix(0, 0, [])"
    M = Matrix(0, 1, lambda i, j: 0)
    assert str(M) == sstr(M) == "Matrix(0, 1, [])"
示例#20
0
def test_intcurve_diffequ():
    start_point = R2_r.point([1, 0])
    vector_field = -R2.y*R2.e_x + R2.x*R2.e_y
    equations, init_cond = intcurve_diffequ(vector_field, t, start_point)
    assert sstr(equations) == '[f_1(t) + Derivative(f_0(t), t), -f_0(t) + Derivative(f_1(t), t)]'
    assert sstr(init_cond) == '[f_0(0) - 1, f_1(0)]'
    equations, init_cond = intcurve_diffequ(vector_field, t, start_point, R2_p)
    assert sstr(
        equations) == '[Derivative(f_0(t), t), Derivative(f_1(t), t) - 1]'
    assert sstr(init_cond) == '[f_0(0) - 1, f_1(0)]'

    start_point = R2_r.point([x, y])
    vector_field = R2_r.e_x
    assert intcurve_series(vector_field, t, start_point,
                           n=3) == Matrix([[t + x], [y]])
示例#21
0
 def __repr__(self):
     lines = sstr(self._diofant_matrix()).split('\n')
     t = " : %s -> %s" % (self.domain, self.codomain)
     s = ' ' * len(t)
     n = len(lines)
     for i in range(n // 2):
         lines[i] += s
     lines[n // 2] += t
     for i in range(n // 2 + 1, n):
         lines[i] += s
     return '\n'.join(lines)
示例#22
0
def test_full_prec():
    assert sstr(Float(0.3), full_prec=True) == "0.300000000000000"
    assert sstr(Float(0.3), full_prec="auto") == "0.300000000000000"
    assert sstr(Float(0.3), full_prec=False) == "0.3"
    assert sstr(Float(0.3) * x, full_prec=True) in [
        "0.300000000000000*x", "x*0.300000000000000"
    ]
    assert sstr(Float(0.3) * x, full_prec="auto") in ["0.3*x", "x*0.3"]
    assert sstr(Float(0.3) * x, full_prec=False) in ["0.3*x", "x*0.3"]
示例#23
0
def test_Permutation_Cycle():
    # general principle: economically, canonically show all moved elements
    # and the size of the permutation.

    for p, s in [
        (Cycle(), 'Cycle()'),
        (Cycle(2), 'Cycle(2)'),
        (Cycle(2, 1), 'Cycle(1, 2)'),
        (Cycle(1, 2)(5)(6, 7)(10), 'Cycle(1, 2)(6, 7)(10)'),
        (Cycle(3, 4)(1, 2)(3, 4), 'Cycle(1, 2)(4)'),
    ]:
        assert str(p) == s

    Permutation.print_cyclic = False
    for p, s in [
        (Permutation([]), 'Permutation([])'),
        (Permutation([], size=1), 'Permutation([0])'),
        (Permutation([], size=2), 'Permutation([0, 1])'),
        (Permutation([], size=10), 'Permutation([], size=10)'),
        (Permutation([1, 0, 2]), 'Permutation([1, 0, 2])'),
        (Permutation([1, 0, 2, 3, 4, 5]), 'Permutation([1, 0], size=6)'),
        (Permutation([1, 0, 2, 3, 4, 5],
                     size=10), 'Permutation([1, 0], size=10)'),
    ]:
        assert str(p) == s

    Permutation.print_cyclic = True
    for p, s in [
        (Permutation([]), 'Permutation()'),
        (Permutation([], size=1), 'Permutation(0)'),
        (Permutation([], size=2), 'Permutation(1)'),
        (Permutation([], size=10), 'Permutation(9)'),
        (Permutation([1, 0, 2]), 'Permutation(2)(0, 1)'),
        (Permutation([1, 0, 2, 3, 4, 5]), 'Permutation(5)(0, 1)'),
        (Permutation([1, 0, 2, 3, 4, 5], size=10), 'Permutation(9)(0, 1)'),
        (Permutation([0, 1, 3, 2, 4, 5], size=10), 'Permutation(9)(2, 3)'),
    ]:
        assert str(p) == s

    assert str(AbelianGroup(3, 4)) == ("PermutationGroup([\n    "
                                       "Permutation(6)(0, 1, 2),\n"
                                       "    Permutation(3, 4, 5, 6)])")
    assert sstr(Cycle(1, 2)) == repr(Cycle(1, 2))
示例#24
0
def test_full_prec():
    assert sstr(Float(0.3), full_prec=True) == "0.300000000000000"
    assert sstr(Float(0.3), full_prec="auto") == "0.300000000000000"
    assert sstr(Float(0.3), full_prec=False) == "0.3"
    assert sstr(Float(0.3)*x, full_prec=True) in [
        "0.300000000000000*x",
        "x*0.300000000000000"
    ]
    assert sstr(Float(0.3)*x, full_prec="auto") in [
        "0.3*x",
        "x*0.3"
    ]
    assert sstr(Float(0.3)*x, full_prec=False) in [
        "0.3*x",
        "x*0.3"
    ]
示例#25
0
def test_PrettyPoly():
    F = QQ.frac_field(x, y)
    R = QQ.poly_ring(x, y)
    assert sstr(F.convert(x / (x + y))) == sstr(x / (x + y))
    assert sstr(R.convert(x + y)) == sstr(x + y)
示例#26
0
def test_infinity():
    assert sstr(oo * I) == "oo*I"
示例#27
0
def test_SparseMatrix():
    M = SparseMatrix([[x**+1, 1], [y, x + y]])
    assert str(M) == "Matrix([\n[x,     1],\n[y, x + y]])"
    assert sstr(M) == "Matrix([\n[x,     1],\n[y, x + y]])"
示例#28
0
def test_SparseMatrix():
    M = SparseMatrix([[x**+1, 1], [y, x + y]])
    assert str(M) == "Matrix([\n[x,     1],\n[y, x + y]])"
    assert sstr(M) == "Matrix([\n[x,     1],\n[y, x + y]])"
示例#29
0
def test_infinity():
    assert sstr(oo*I) == "oo*I"
示例#30
0
def test_DMP():
    assert sstr(DMP([[0], [], [0, 1, 2], [3]], ZZ)) == 'DMP([[1, 2], [3]], ZZ)'
示例#31
0
def test_Geometry():
    assert sstr(Point(0, 0)) == 'Point2D(0, 0)'
    assert sstr(Circle(Point(0, 0), 3)) == 'Circle(Point2D(0, 0), 3)'
示例#32
0
def test_PrettyPoly():
    from diofant.polys.domains import QQ
    F = QQ.frac_field(x, y)
    R = QQ[x, y]
    assert sstr(F.convert(x / (x + y))) == sstr(x / (x + y))
    assert sstr(R.convert(x + y)) == sstr(x + y)
示例#33
0
def test_Permutation_Cycle():
    # general principle: economically, canonically show all moved elements
    # and the size of the permutation.

    for p, s in [
        (Cycle(),
         'Cycle()'),
        (Cycle(2),
         'Cycle(2)'),
        (Cycle(2, 1),
         'Cycle(1, 2)'),
        (Cycle(1, 2)(5)(6, 7)(10),
         'Cycle(1, 2)(6, 7)(10)'),
        (Cycle(3, 4)(1, 2)(3, 4),
         'Cycle(1, 2)(4)'),
    ]:
        assert str(p) == s

    Permutation.print_cyclic = False
    for p, s in [
        (Permutation([]),
         'Permutation([])'),
        (Permutation([], size=1),
         'Permutation([0])'),
        (Permutation([], size=2),
         'Permutation([0, 1])'),
        (Permutation([], size=10),
         'Permutation([], size=10)'),
        (Permutation([1, 0, 2]),
         'Permutation([1, 0, 2])'),
        (Permutation([1, 0, 2, 3, 4, 5]),
         'Permutation([1, 0], size=6)'),
        (Permutation([1, 0, 2, 3, 4, 5], size=10),
         'Permutation([1, 0], size=10)'),
    ]:
        assert str(p) == s

    Permutation.print_cyclic = True
    for p, s in [
        (Permutation([]),
         'Permutation()'),
        (Permutation([], size=1),
         'Permutation(0)'),
        (Permutation([], size=2),
         'Permutation(1)'),
        (Permutation([], size=10),
         'Permutation(9)'),
        (Permutation([1, 0, 2]),
         'Permutation(2)(0, 1)'),
        (Permutation([1, 0, 2, 3, 4, 5]),
         'Permutation(5)(0, 1)'),
        (Permutation([1, 0, 2, 3, 4, 5], size=10),
         'Permutation(9)(0, 1)'),
        (Permutation([0, 1, 3, 2, 4, 5], size=10),
         'Permutation(9)(2, 3)'),
    ]:
        assert str(p) == s

    assert str(AbelianGroup(3, 4)) == ("PermutationGroup([\n    "
                                       "Permutation(6)(0, 1, 2),\n"
                                       "    Permutation(3, 4, 5, 6)])")
    assert sstr(Cycle(1, 2)) == repr(Cycle(1, 2))
示例#34
0
def test_settings():
    pytest.raises(TypeError, lambda: sstr(Integer(4), method="garbage"))
示例#35
0
def test_Dict():
    assert str(Dict({1: 1 + x})) == sstr({1: 1 + x}) == "{1: x + 1}"
    assert str(Dict({1: x**2, 2: y*x})) in (
        "{1: x**2, 2: x*y}", "{2: x*y, 1: x**2}")
    assert sstr(Dict({1: x**2, 2: y*x})) == "{1: x**2, 2: x*y}"
示例#36
0
def test_dict():
    assert sstr({1: 1 + x}) == "{1: x + 1}"
    assert str({1: 1 + x}) == "{1: Add(Symbol('x'), Integer(1))}"
    assert str({1: x**2, 2: y*x}) in ("{1: Pow(Symbol('x'), Integer(2)), 2: Mul(Symbol('x'), Symbol('y'))}", "{1: Mul(Symbol('x'), Symbol('y')), 2: Pow(Symbol('x'), Integer(2))}")
    assert sstr({1: x**2, 2: y*x}) == "{1: x**2, 2: x*y}"
示例#37
0
def test_PrettyPoly():
    F = QQ.frac_field(x, y)
    R = QQ.poly_ring(x, y)
    assert sstr(F.convert(x/(x + y))) == sstr(x/(x + y))
    assert sstr(R.convert(x + y)) == sstr(x + y)
示例#38
0
 def __str__(self):
     """String representation of a GeometryEntity."""
     from diofant.printing import sstr
     return type(self).__name__ + sstr(self.args)
示例#39
0
def test_settings():
    pytest.raises(TypeError, lambda: sstr(Integer(4), method="garbage"))
示例#40
0
def test_ImmutableDenseNDimArray():
    m = [2 * i + j for i in range(2) for j in range(2)]
    assert sstr(ImmutableDenseNDimArray(m, (2, 2))) == '[[0, 1], [2, 3]]'
示例#41
0
def test_true_false():
    assert str(true) == repr(true) == sstr(true) == "true"
    assert str(false) == repr(false) == sstr(false) == "false"
示例#42
0
def test_Geometry():
    assert sstr(Point(0, 0)) == 'Point2D(0, 0)'
    assert sstr(Circle(Point(0, 0), 3)) == 'Circle(Point2D(0, 0), 3)'
示例#43
0
def test_ImmutableDenseNDimArray():
    m = [2*i + j for i in range(2) for j in range(2)]
    assert sstr(ImmutableDenseNDimArray(m, (2, 2))) == '[[0, 1], [2, 3]]'
示例#44
0
def test_true_false():
    assert str(true) == repr(true) == sstr(true) == "true"
    assert str(false) == repr(false) == sstr(false) == "false"