def test_deprecated_print_cyclic(): p = Permutation(0, 1, 2) try: Permutation.print_cyclic = True with warns_deprecated_sympy(): assert sstr(p) == '(0 1 2)' with warns_deprecated_sympy(): assert srepr(p) == 'Permutation(0, 1, 2)' with warns_deprecated_sympy(): assert pretty(p) == '(0 1 2)' with warns_deprecated_sympy(): assert latex(p) == r'\left( 0\; 1\; 2\right)' Permutation.print_cyclic = False with warns_deprecated_sympy(): assert sstr(p) == 'Permutation([1, 2, 0])' with warns_deprecated_sympy(): assert srepr(p) == 'Permutation([1, 2, 0])' with warns_deprecated_sympy(): assert pretty(p, use_unicode=False) == '/0 1 2\\\n\\1 2 0/' with warns_deprecated_sympy(): assert latex(p) == \ r'\begin{pmatrix} 0 & 1 & 2 \\ 1 & 2 & 0 \end{pmatrix}' finally: Permutation.print_cyclic = None
def test_set(): from sympy.abc import x, y s = set() assert srepr(s) == "set()" s = {x, y} assert srepr(s) in ("{Symbol('x'), Symbol('y')}", "{Symbol('y'), Symbol('x')}")
def test_Symbol_two_assumptions(): x = Symbol('x', negative=0, integer=1) # order could vary s1 = "Symbol('x', integer=True, negative=False)" s2 = "Symbol('x', negative=False, integer=True)" assert srepr(x) in (s1, s2) assert eval(srepr(x), ENV) == x
def test_PolynomialRingBase(): assert srepr(ZZ.old_poly_ring(x)) == \ "GlobalPolynomialRing(ZZ, Symbol('x'))" assert srepr(ZZ[x].old_poly_ring(y)) == \ "GlobalPolynomialRing(ZZ[x], Symbol('y'))" assert srepr(QQ.frac_field(x).old_poly_ring(y)) == \ "GlobalPolynomialRing(FractionField(FracField((Symbol('x'),), QQ, lex)), Symbol('y'))"
def test_PolyRing(): assert srepr(ring("x", ZZ, lex)[0]) == "PolyRing((Symbol('x'),), ZZ, lex)" assert srepr( ring("x,y", QQ, grlex)[0]) == "PolyRing((Symbol('x'), Symbol('y')), QQ, grlex)" assert srepr( ring("x,y,z", ZZ["t"], lex) [0]) == "PolyRing((Symbol('x'), Symbol('y'), Symbol('z')), ZZ[t], lex)"
def test_Add(): sT(x + y, "Add(Symbol('x'), Symbol('y'))") assert srepr( x**2 + 1, order='lex') == "Add(Pow(Symbol('x'), Integer(2)), Integer(1))" assert srepr( x**2 + 1, order='old') == "Add(Integer(1), Pow(Symbol('x'), Integer(2)))"
def test_FractionField(): assert ( srepr(QQ.frac_field(x)) == "FractionField(FracField((Symbol('x'),), QQ, lex))" ) assert ( srepr(QQ.frac_field(x, y, order=grlex)) == "FractionField(FracField((Symbol('x'), Symbol('y')), QQ, grlex))" )
def test_printmethod(): class R(oo.__class__): def _sympyrepr(self, printer): return "foo" assert srepr(R()) == "foo" class R(Abs): def _sympyrepr(self, printer): return "foo(%s)" % printer._print(self.args[0]) assert srepr(R(x)) == "foo(Symbol('x'))"
def test_Mul(): sT(3 * x**3 * y, "Mul(Integer(3), Pow(Symbol('x'), Integer(3)), Symbol('y'))") assert srepr( 3 * x**3 * y, order='old' ) == "Mul(Integer(3), Symbol('y'), Pow(Symbol('x'), Integer(3)))" assert srepr( sympify('(x+4)*2*x*7', evaluate=False), order='none' ) == "Mul(Add(Symbol('x'), Integer(4)), Integer(2), Symbol('x'), Integer(7))"
def test_FracField(): assert srepr(field("x", ZZ, lex)[0]) == "FracField((Symbol('x'),), ZZ, lex)" assert srepr( field("x,y", QQ, grlex)[0]) == "FracField((Symbol('x'), Symbol('y')), QQ, grlex)" assert srepr( field("x,y,z", ZZ["t"], lex)[0] ) == "FracField((Symbol('x'), Symbol('y'), Symbol('z')), ZZ[t], lex)"
def test_Add(): sT(x + y, "Add(Symbol('x'), Symbol('y'))") assert ( srepr(x ** 2 + 1, order="lex") == "Add(Pow(Symbol('x'), Integer(2)), Integer(1))" ) assert ( srepr(x ** 2 + 1, order="old") == "Add(Integer(1), Pow(Symbol('x'), Integer(2)))" )
def test_Add(): sT(x + y, "Add(Symbol('x'), Symbol('y'))") assert srepr( x**2 + 1, order='lex') == "Add(Pow(Symbol('x'), Integer(2)), Integer(1))" assert srepr( x**2 + 1, order='old') == "Add(Integer(1), Pow(Symbol('x'), Integer(2)))" assert srepr( sympify('x + 3 - 2', evaluate=False), order='none' ) == "Add(Symbol('x'), Integer(3), Mul(Integer(-1), Integer(2)))"
def test_diffgeom(): from sympy.diffgeom import Manifold, Patch, CoordSystem, BaseScalarField m = Manifold('M', 2) p = Patch('P', m) rect = CoordSystem('rect', p) assert srepr( rect ) == "CoordSystem(Str('rect'), Patch(Str('P'), Manifold(Str('M'), Integer(2))), ('rect_0', 'rect_1'))" b = BaseScalarField(rect, 0) assert srepr( b ) == "BaseScalarField(CoordSystem(Str('rect'), Patch(Str('P'), Manifold(Str('M'), Integer(2))), ('rect_0', 'rect_1')), Integer(0))"
def test_printmethod(): class R(oo.__class__): def _sympyrepr(self, printer): return "foo" assert srepr(R()) == "foo" class R(abs): def _sympyrepr(self, printer): return "foo(%s)" % printer._print(self.args[0]) assert srepr(R(x)) == "foo(Symbol('x'))"
def test_dict(): from sympy.abc import x, y, z d = {} assert srepr(d) == "{}" d = {x: y} assert srepr(d) == "{Symbol('x'): Symbol('y')}" d = {x: y, y: z} assert srepr(d) in ( "{Symbol('x'): Symbol('y'), Symbol('y'): Symbol('z')}", "{Symbol('y'): Symbol('z'), Symbol('x'): Symbol('y')}", ) d = {x: {y: z}} assert srepr(d) == "{Symbol('x'): {Symbol('y'): Symbol('z')}}"
def test_printing_non_cyclic(): p1 = Permutation([0, 1, 2, 3, 4, 5]) assert srepr(p1, perm_cyclic=False) == 'Permutation([], size=6)' assert sstr(p1, perm_cyclic=False) == 'Permutation([], size=6)' p2 = Permutation([0, 1, 2]) assert srepr(p2, perm_cyclic=False) == 'Permutation([0, 1, 2])' assert sstr(p2, perm_cyclic=False) == 'Permutation([0, 1, 2])' p3 = Permutation([0, 2, 1]) assert srepr(p3, perm_cyclic=False) == 'Permutation([0, 2, 1])' assert sstr(p3, perm_cyclic=False) == 'Permutation([0, 2, 1])' p4 = Permutation([0, 1, 3, 2, 4, 5, 6, 7]) assert srepr(p4, perm_cyclic=False) == 'Permutation([0, 1, 3, 2], size=8)'
def test_printing_non_cyclic(): from sympy.printing import sstr, srepr p1 = Permutation([0, 1, 2, 3, 4, 5]) assert srepr(p1, perm_cyclic=False) == "Permutation([], size=6)" assert sstr(p1, perm_cyclic=False) == "Permutation([], size=6)" p2 = Permutation([0, 1, 2]) assert srepr(p2, perm_cyclic=False) == "Permutation([0, 1, 2])" assert sstr(p2, perm_cyclic=False) == "Permutation([0, 1, 2])" p3 = Permutation([0, 2, 1]) assert srepr(p3, perm_cyclic=False) == "Permutation([0, 2, 1])" assert sstr(p3, perm_cyclic=False) == "Permutation([0, 2, 1])" p4 = Permutation([0, 1, 3, 2, 4, 5, 6, 7]) assert srepr(p4, perm_cyclic=False) == "Permutation([0, 1, 3, 2], size=8)"
def sT(expr, string): """ sT := sreprTest from sympy/printing/tests/test_repr.py """ assert srepr(expr) == string assert eval(string) == expr
def test_Dummy_from_Symbol(): # should not get the full dictionary of assumptions n = Symbol('n', integer=True) d = n.as_dummy() s1 = "Dummy('n', dummy_index=%s, integer=True)" % str(d.dummy_index) s2 = "Dummy('n', integer=True, dummy_index=%s)" % str(d.dummy_index) assert srepr(d) in (s1, s2)
def test_issue_20733(): expr = 1 / ((x - 9) * (x - 8) * (x - 7) * (x - 4)**2 * (x - 3)**3 * (x - 2)) assert str(expr.evalf(1, subs={x: 1})) == '-4.e-5' assert str(expr.evalf(2, subs={x: 1})) == '-4.1e-5' assert str(expr.evalf(11, subs={x: 1})) == '-4.1335978836e-5' assert str(expr.evalf(20, subs={x: 1})) == '-0.000041335978835978835979' expr = Mul(*((x - i) for i in range(2, 1000))) assert srepr(expr.evalf(2, subs={x: 1})) == "Float('4.0271e+2561', precision=10)" assert srepr(expr.evalf( 10, subs={x: 1})) == "Float('4.02790050126e+2561', precision=37)" assert srepr( expr.evalf(53, subs={x: 1}) ) == "Float('4.0279005012722099453824067459760158730668154575647110393e+2561', precision=179)"
def sT(expr, string): """ sT := sreprTest Tests that srepr delivers the expected string and that the condition eval(srepr(expr))==expr holds. """ assert srepr(expr) == string assert eval(string, ENV) == expr
def sT(expr, string, import_stmt=None): """ sT := sreprTest Tests that srepr delivers the expected string and that the condition eval(srepr(expr))==expr holds. """ if import_stmt is None: ENV2 = ENV else: ENV2 = ENV.copy() exec(import_stmt, ENV2) assert srepr(expr) == string assert eval(string, ENV2) == expr
def test_PolyElement(): R, x, y = ring("x,y", ZZ) assert srepr(3*x**2*y + 1) == "PolyElement(PolyRing((Symbol('x'), Symbol('y')), ZZ, lex), [((2, 1), 3), ((0, 0), 1)])"
def test_FiniteExtension(): assert srepr(FiniteExtension(Poly(x**2 + 1, x))) == \ "FiniteExtension(Poly(x**2 + 1, x, domain='ZZ'))"
def test_S_srepr_is_identity(): p = Piecewise((10, Eq(x, 0)), (12, True)) q = S(srepr(p)) assert p == q
def test_Mul(): sT(3 * x**3 * y, "Mul(Integer(3), Pow(Symbol('x'), Integer(3)), Symbol('y'))") assert srepr( 3 * x**3 * y, order='old' ) == "Mul(Integer(3), Symbol('y'), Pow(Symbol('x'), Integer(3)))"
def test_BooleanAtom(): assert srepr(true) == "S.true" assert srepr(false) == "S.false"
def test_Dummy_from_Symbol(): # should not get the full dictionary of assumptions n = Symbol('n', integer=True) d = n.as_dummy() assert srepr(d) == "Dummy('n', integer=True)"
def test_Dummy_assumption(): d = Dummy('d', nonzero=True) assert d == eval(srepr(d)) s1 = "Dummy('d', dummy_index=%s, nonzero=True)" % str(d.dummy_index) s2 = "Dummy('d', nonzero=True, dummy_index=%s)" % str(d.dummy_index) assert srepr(d) in (s1, s2)
def test_more_than_255_args_issue_10259(): from sympy import Add, Mul for op in (Add, Mul): expr = op(*symbols('x:256')) assert eval(srepr(expr)) == expr
def test_more_than_255_args_issue_10259(): from sympy.core.add import Add from sympy.core.mul import Mul for op in (Add, Mul): expr = op(*symbols('x:256')) assert eval(srepr(expr)) == expr
def test_FracElement(): F, x, y = field("x,y", ZZ) assert srepr((3*x**2*y + 1)/(x - y**2)) == "FracElement(FracField((Symbol('x'), Symbol('y')), ZZ, lex), [((2, 1), 3), ((0, 0), 1)], [((1, 0), 1), ((0, 2), -1)])"
def test_DMP(): assert srepr(DMP([1, 2], ZZ)) == 'DMP([1, 2], ZZ)' assert srepr(ZZ.old_poly_ring(x)([1, 2])) == \ "DMP([1, 2], ZZ, ring=GlobalPolynomialRing(ZZ, Symbol('x')))"
def test_FractionField(): assert srepr(QQ.frac_field(x)) == \ "FractionField(FracField((Symbol('x'),), QQ, lex))" assert srepr(QQ.frac_field(x, y, order=grlex)) == \ "FractionField(FracField((Symbol('x'), Symbol('y')), QQ, grlex))"
def __repr__(self): from sympy.printing import srepr return type(self).__name__ + srepr(tuple(self))
def test_settins(): raises(TypeError, lambda: srepr(x, method="garbage"))
def test_ExtensionElement(): A = FiniteExtension(Poly(x**2 + 1, x)) assert srepr(A.generator) == \ "ExtElem(DMP([1, 0], ZZ, ring=GlobalPolynomialRing(ZZ, Symbol('x'))), FiniteExtension(Poly(x**2 + 1, x, domain='ZZ')))"
def test_Mul(): sT(3*x**3*y, "Mul(Integer(3), Pow(Symbol('x'), Integer(3)), Symbol('y'))") assert srepr(3*x**3*y, order='old') == "Mul(Integer(3), Symbol('y'), Pow(Symbol('x'), Integer(3)))"
def test_Add(): sT(x + y, "Add(Symbol('x'), Symbol('y'))") assert srepr(x**2 + 1, order='lex') == "Add(Pow(Symbol('x'), Integer(2)), Integer(1))" assert srepr(x**2 + 1, order='old') == "Add(Integer(1), Pow(Symbol('x'), Integer(2)))"
def test_PolyRing(): assert srepr(ring("x", ZZ, lex)[0]) == "PolyRing((Symbol('x'),), ZZ, lex)" assert srepr(ring("x,y", QQ, grlex)[0]) == "PolyRing((Symbol('x'), Symbol('y')), QQ, grlex)" assert srepr(ring("x,y,z", ZZ["t"], lex)[0]) == "PolyRing((Symbol('x'), Symbol('y'), Symbol('z')), ZZ[t], lex)"
def test_Dummy(): # cannot use sT here d = Dummy('d', nonzero=True) assert srepr(d) == "Dummy('d', nonzero=True)"
def test_FracField(): assert srepr(field("x", ZZ, lex)[0]) == "FracField((Symbol('x'),), ZZ, lex)" assert srepr(field("x,y", QQ, grlex)[0]) == "FracField((Symbol('x'), Symbol('y')), QQ, grlex)" assert srepr(field("x,y,z", ZZ["t"], lex)[0]) == "FracField((Symbol('x'), Symbol('y'), Symbol('z')), ZZ[t], lex)"
def test_BooleanAtom(): assert srepr(true) == "true" assert srepr(false) == "false"
def test_Cycle(): # FIXME: sT fails because Cycle is not immutable and calling srepr(Cycle(1, 2)) # adds keys to the Cycle dict (GH-17661) #import_stmt = "from sympy.combinatorics import Cycle" #sT(Cycle(1, 2), "Cycle(1, 2)", import_stmt) assert srepr(Cycle(1, 2)) == "Cycle(1, 2)"