diff --git a/doc/modules/index.rst b/doc/modules/index.rst index d84daff90d..664c8154a5 100644 --- a/doc/modules/index.rst +++ b/doc/modules/index.rst @@ -45,7 +45,6 @@ access any SymPy module, or use this contens: solvers/solvers.rst solvers/diophantine.rst solvers/inequalities.rst - solvers/solveset.rst tensor/index.rst utilities/index.rst parsing.rst diff --git a/doc/modules/solvers/solveset.rst b/doc/modules/solvers/solveset.rst deleted file mode 100644 index afb3ab6578..0000000000 --- a/doc/modules/solvers/solveset.rst +++ /dev/null @@ -1,55 +0,0 @@ -Solveset -========== - -.. module:: sympy.solvers.solveset - -This module contains functions to solve a single equation for a single variable. - -Use :func:`solveset` to solve equations or expressions (assumed to be equal to 0) for a single variable. -Solving an equation like x**2 == 1 can be done as follows:: - - >>> from sympy.solvers.solveset import * - >>> from sympy import Symbol, Eq - >>> x = Symbol('x') - >>> solveset(Eq(x**2, 1), x) - {-1, 1} - -Or one may manually rewrite the equation as an expression equal to 0:: - - >>> solveset(x**2 - 1, x) - {-1, 1} - -The first argument for :func:`solveset` is an expression (equal to zero) or an equation and the second argument -is the symbol that we want to solve the equation for. - -.. autofunction:: sympy.solvers.solveset.solveset - -.. autofunction:: sympy.solvers.solveset.solveset_real - -.. autofunction:: sympy.solvers.solveset.solveset_complex - -.. autofunction:: sympy.solvers.solveset.invert_real - -.. autofunction:: sympy.solvers.solveset.invert_complex - -.. autofunction:: sympy.solvers.solveset.domain_check - -Diophantine Equations (DEs) ---------------------------- - -See :ref:`diophantine-docs` - -Inequalities ------------- - -See :ref:`inequality-docs` - -Ordinary Differential equations (ODEs) --------------------------------------- - -See :ref:`ode-docs`. - -Partial Differential Equations (PDEs) -------------------------------------- - -See :ref:`pde-docs`. diff --git a/sympy/assumptions/assume.py b/sympy/assumptions/assume.py index 451b25802a..5b22e47205 100644 --- a/sympy/assumptions/assume.py +++ b/sympy/assumptions/assume.py @@ -20,16 +20,16 @@ class to create your own local assumptions contexts. It is basically a thin ======== >>> from sympy import AppliedPredicate, Q - >>> from sympy.assumptions.assume import global_assumptions - >>> global_assumptions - AssumptionsContext() + >>> from sympy.assumptions.assume import global_assumptions, AssumptionsContext + >>> global_assumptions == AssumptionsContext([]) + True >>> from sympy.abc import x >>> global_assumptions.add(Q.real(x)) - >>> global_assumptions - AssumptionsContext([Q.real(x)]) + >>> global_assumptions == AssumptionsContext([Q.real(x)]) + True >>> global_assumptions.remove(Q.real(x)) - >>> global_assumptions - AssumptionsContext() + >>> global_assumptions == AssumptionsContext([]) + True >>> global_assumptions.clear() """ diff --git a/sympy/categories/baseclasses.py b/sympy/categories/baseclasses.py index e221de0b9f..dd5a1bb3e4 100644 --- a/sympy/categories/baseclasses.py +++ b/sympy/categories/baseclasses.py @@ -570,7 +570,7 @@ class Diagram(Basic): {g*f:A-->C: EmptySet(), id:A-->A: EmptySet(), id:B-->B: EmptySet(), id:C-->C: EmptySet(), f:A-->B: EmptySet(), g:B-->C: EmptySet()} >>> d = Diagram([f, g], {g * f: "unique"}) - >>> pprint(d.conclusions) + >>> pprint(d.conclusions, use_unicode=False) {g*f:A-->C: {unique}} References diff --git a/sympy/categories/diagram_drawing.py b/sympy/categories/diagram_drawing.py index ef7d55517f..6b6d48f87b 100644 --- a/sympy/categories/diagram_drawing.py +++ b/sympy/categories/diagram_drawing.py @@ -1313,6 +1313,7 @@ def morphisms(self): >>> from sympy.categories import Object, NamedMorphism >>> from sympy.categories import Diagram, DiagramGrid + >>> from sympy.sets import EmptySet >>> A = Object("A") >>> B = Object("B") >>> C = Object("C") @@ -1320,10 +1321,11 @@ def morphisms(self): >>> g = NamedMorphism(B, C, "g") >>> diagram = Diagram([f, g]) >>> grid = DiagramGrid(diagram) - >>> grid.morphisms - {NamedMorphism(Object("A"), Object("B"), "f"): EmptySet(), - NamedMorphism(Object("B"), Object("C"), "g"): EmptySet()} - + >>> grid.morphisms == {NamedMorphism(Object("A"), + ... Object("B"), "f"): EmptySet(), + ... NamedMorphism(Object("B"), + ... Object("C"), "g"): EmptySet()} + True """ return self._morphisms diff --git a/sympy/combinatorics/perm_groups.py b/sympy/combinatorics/perm_groups.py index 14c9c9e5f9..94b9303196 100644 --- a/sympy/combinatorics/perm_groups.py +++ b/sympy/combinatorics/perm_groups.py @@ -2057,10 +2057,10 @@ def orbit(self, alpha, action='tuples'): >>> from sympy.combinatorics.perm_groups import PermutationGroup >>> a = Permutation([1, 2, 0, 4, 5, 6, 3]) >>> G = PermutationGroup([a]) - >>> G.orbit(0) - set([0, 1, 2]) - >>> G.orbit([0, 4], 'union') - set([0, 1, 2, 3, 4, 5, 6]) + >>> G.orbit(0) == {0, 1, 2} + True + >>> G.orbit([0, 4], 'union') == {0, 1, 2, 3, 4, 5, 6} + True See Also ======== @@ -2156,8 +2156,8 @@ def orbits(self, rep=False): >>> a = Permutation(1, 5)(2, 3)(4, 0, 6) >>> b = Permutation(1, 5)(3, 4)(2, 6, 0) >>> G = PermutationGroup([a, b]) - >>> G.orbits() - [set([0, 2, 3, 4, 6]), set([1, 5])] + >>> G.orbits() ==[{0, 2, 3, 4, 6}, {1, 5}] + True """ return _orbits(self._degree, self._generators) @@ -3233,10 +3233,10 @@ def _orbit(degree, generators, alpha, action='tuples'): >>> from sympy.combinatorics.perm_groups import PermutationGroup, _orbit >>> a = Permutation([1, 2, 0, 4, 5, 6, 3]) >>> G = PermutationGroup([a]) - >>> _orbit(G.degree, G.generators, 0) - set([0, 1, 2]) - >>> _orbit(G.degree, G.generators, [0, 4], 'union') - set([0, 1, 2, 3, 4, 5, 6]) + >>> _orbit(G.degree, G.generators, 0) == {0, 1, 2} + True + >>> _orbit(G.degree, G.generators, [0, 4], 'union') == {0, 1, 2, 3, 4, 5, 6} + True See Also ======== @@ -3296,8 +3296,8 @@ def _orbits(degree, generators): >>> from sympy.combinatorics.perm_groups import PermutationGroup, _orbits >>> a = Permutation([0, 2, 1]) >>> b = Permutation([1, 0, 2]) - >>> _orbits(a.size, [a, b]) - [set([0, 1, 2])] + >>> _orbits(a.size, [a, b]) == [{0, 1, 2}] + True """ seen = set() # elements that have already appeared in orbits diff --git a/sympy/combinatorics/permutations.py b/sympy/combinatorics/permutations.py index 5ba7ab8681..cda71be5e2 100644 --- a/sympy/combinatorics/permutations.py +++ b/sympy/combinatorics/permutations.py @@ -1533,10 +1533,10 @@ def atoms(self): ======== >>> from sympy.combinatorics import Permutation - >>> Permutation([0, 1, 2, 3, 4, 5]).atoms() - set([0, 1, 2, 3, 4, 5]) - >>> Permutation([[0, 1], [2, 3], [4, 5]]).atoms() - set([0, 1, 2, 3, 4, 5]) + >>> Permutation([0, 1, 2, 3, 4, 5]).atoms() == {0, 1, 2, 3, 4, 5} + True + >>> Permutation([[0, 1], [2, 3], [4, 5]]).atoms() == {0, 1, 2, 3, 4, 5} + True """ return set(self.array_form) diff --git a/sympy/combinatorics/tensor_can.py b/sympy/combinatorics/tensor_can.py index 065f98f3db..96ce98359c 100644 --- a/sympy/combinatorics/tensor_can.py +++ b/sympy/combinatorics/tensor_can.py @@ -49,7 +49,7 @@ def dummy_sgs(dummies, sym, n): ======== >>> from sympy.combinatorics.tensor_can import dummy_sgs - >>> dummy_sgs(range(2, 8), 0, 8) + >>> dummy_sgs(list(range(2, 8)), 0, 8) [[0, 1, 3, 2, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 5, 4, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 7, 6, 8, 9], [0, 1, 4, 5, 2, 3, 6, 7, 8, 9], [0, 1, 2, 3, 6, 7, 4, 5, 8, 9]] diff --git a/sympy/concrete/expr_with_limits.py b/sympy/concrete/expr_with_limits.py index e4b5df5161..e427148bbe 100644 --- a/sympy/concrete/expr_with_limits.py +++ b/sympy/concrete/expr_with_limits.py @@ -175,8 +175,8 @@ def free_symbols(self): >>> from sympy import Sum >>> from sympy.abc import x, y - >>> Sum(x, (x, y, 1)).free_symbols - set([y]) + >>> Sum(x, (x, y, 1)).free_symbols == {y} + True """ # don't test for any special values -- nominal free symbols # should be returned, e.g. don't return set() if the diff --git a/sympy/core/add.py b/sympy/core/add.py index 9a02e620bd..0ea86b3e43 100644 --- a/sympy/core/add.py +++ b/sympy/core/add.py @@ -277,13 +277,15 @@ def as_coefficients_dict(a): Examples ======== + >>> from collections import defaultdict >>> from sympy.abc import a, x - >>> (3*x + a*x + 4).as_coefficients_dict() - {1: 4, x: 3, a*x: 1} - >>> _[a] + >>> d = (3*x + a*x + 4).as_coefficients_dict() + >>> d == defaultdict(int, {1: 4, x: 3, a*x: 1}) + True + >>> d[a] 0 - >>> (3*a*x).as_coefficients_dict() - {a*x: 3} + >>> (3*a*x).as_coefficients_dict() == defaultdict(int, {a*x: 3}) + True """ d = defaultdict(list) diff --git a/sympy/core/basic.py b/sympy/core/basic.py index 2650ed1db6..6edf7669e9 100644 --- a/sympy/core/basic.py +++ b/sympy/core/basic.py @@ -119,7 +119,7 @@ def _hashable_content(self): @property def assumptions0(self): - """ + r""" Return object `type` assumptions. For example: @@ -138,11 +138,12 @@ def assumptions0(self): >>> x.assumptions0 {'commutative': True} >>> x = Symbol("x", positive=True) - >>> x.assumptions0 - {'commutative': True, 'complex': True, 'extended_real': True, - 'hermitian': True, 'imaginary': False, 'negative': False, - 'nonnegative': True, 'nonpositive': False, 'nonzero': True, - 'positive': True, 'zero': False} + >>> x.assumptions0 == \ + ... {'commutative': True, 'complex': True, 'extended_real': True, + ... 'hermitian': True, 'imaginary': False, 'negative': False, + ... 'nonnegative': True, 'nonpositive': False, 'nonzero': True, + ... 'positive': True, 'zero': False} + True """ return {} @@ -406,8 +407,8 @@ def atoms(self, *types): >>> from sympy import I, pi, sin >>> from sympy.abc import x, y - >>> (1 + x + 2*sin(y + I*pi)).atoms() - set([1, 2, I, pi, x, y]) + >>> (1 + x + 2*sin(y + I*pi)).atoms() == {1, 2, I, pi, x, y} + True If one or more types are given, the results will contain only those types of atoms. @@ -416,25 +417,25 @@ def atoms(self, *types): ======== >>> from sympy import Number, NumberSymbol, Symbol - >>> (1 + x + 2*sin(y + I*pi)).atoms(Symbol) - set([x, y]) + >>> (1 + x + 2*sin(y + I*pi)).atoms(Symbol) == {x, y} + True - >>> (1 + x + 2*sin(y + I*pi)).atoms(Number) - set([1, 2]) + >>> (1 + x + 2*sin(y + I*pi)).atoms(Number) == {1, 2} + True - >>> (1 + x + 2*sin(y + I*pi)).atoms(Number, NumberSymbol) - set([1, 2, pi]) + >>> (1 + x + 2*sin(y + I*pi)).atoms(Number, NumberSymbol) == {1, 2, pi} + True - >>> (1 + x + 2*sin(y + I*pi)).atoms(Number, NumberSymbol, I) - set([1, 2, I, pi]) + >>> (1 + x + 2*sin(y + I*pi)).atoms(Number, NumberSymbol, I) == {1, 2, I, pi} + True Note that I (imaginary unit) and zoo (complex infinity) are special types of number symbols and are not part of the NumberSymbol class. The type can be given implicitly, too: - >>> (1 + x + 2*sin(y + I*pi)).atoms(x) # x is a Symbol - set([x, y]) + >>> (1 + x + 2*sin(y + I*pi)).atoms(x) == {x, y} + True Be careful to check your assumptions when using the implicit option since ``S(1).is_Integer = True`` but ``type(S(1))`` is ``One``, a special type @@ -442,11 +443,11 @@ def atoms(self, *types): integers in an expression: >>> from sympy import S - >>> (1 + x + 2*sin(y + I*pi)).atoms(S(1)) - set([1]) + >>> (1 + x + 2*sin(y + I*pi)).atoms(S(1)) == {1} + True - >>> (1 + x + 2*sin(y + I*pi)).atoms(S(2)) - set([1, 2]) + >>> (1 + x + 2*sin(y + I*pi)).atoms(S(2)) == {1, 2} + True Finally, arguments to atoms() can select more than atomic atoms: any sympy type (loaded in core/__init__.py) can be listed as an argument @@ -456,14 +457,13 @@ def atoms(self, *types): >>> from sympy import Function, Mul >>> from sympy.core.function import AppliedUndef >>> f = Function('f') - >>> (1 + f(x) + 2*sin(y + I*pi)).atoms(Function) - set([f(x), sin(y + I*pi)]) - >>> (1 + f(x) + 2*sin(y + I*pi)).atoms(AppliedUndef) - set([f(x)]) - - >>> (1 + x + 2*sin(y + I*pi)).atoms(Mul) - set([I*pi, 2*sin(y + I*pi)]) + >>> (1 + f(x) + 2*sin(y + I*pi)).atoms(Function) == {f(x), sin(y + I*pi)} + True + >>> (1 + f(x) + 2*sin(y + I*pi)).atoms(AppliedUndef) == {f(x)} + True + >>> (1 + x + 2*sin(y + I*pi)).atoms(Mul) == {I*pi, 2*sin(y + I*pi)} + True """ if types: types = tuple( @@ -1443,13 +1443,13 @@ def match(self, pattern, old=False): >>> q = Wild("q") >>> r = Wild("r") >>> e = (x+y)**(x+y) - >>> e.match(p**p) - {p_: x + y} - >>> e.match(p**q) - {p_: x + y, q_: x + y} + >>> e.match(p**p) == {p: x + y} + True + >>> e.match(p**q) == {p: x + y, q: x + y} + True >>> e = (2*x)**2 - >>> e.match(p*q**r) - {p_: 4, q_: x, r_: 2} + >>> e.match(p*q**r) == {p: 4, q: x, r: 2} + True >>> (p*q**r).xreplace(e.match(p*q**r)) 4*x**2 @@ -1682,13 +1682,12 @@ def _atomic(e): >>> from sympy.abc import x, y >>> from sympy.core.basic import _atomic >>> f = Function('f') - >>> _atomic(x + y) - set([x, y]) - >>> _atomic(x + f(y)) - set([x, f(y)]) - >>> _atomic(Derivative(f(x), x) + cos(x) + y) - set([y, cos(x), Derivative(f(x), x)]) - + >>> _atomic(x + y) == {x, y} + True + >>> _atomic(x + f(y)) == {x, f(y)} + True + >>> _atomic(Derivative(f(x), x) + cos(x) + y) == {y, cos(x), Derivative(f(x), x)} + True """ from sympy import Derivative, Function, Symbol pot = preorder_traversal(e) diff --git a/sympy/core/compatibility.py b/sympy/core/compatibility.py index e1635138db..1de093dc9c 100644 --- a/sympy/core/compatibility.py +++ b/sympy/core/compatibility.py @@ -166,10 +166,10 @@ class MyClass(object, metaclass=Meta): are the base classes. Note that if the base class is just ``object``, you may omit it. - >>> MyClass.__mro__ - (, <... 'object'>) - >>> type(MyClass) - + >>> MyClass.__mro__ == (MyClass, object) + True + >>> type(MyClass) is Meta + True """ # This requires a bit of explanation: the basic idea is to make a dummy diff --git a/sympy/core/evalf.py b/sympy/core/evalf.py index 3e65cb7c6e..ad50a0eb2d 100644 --- a/sympy/core/evalf.py +++ b/sympy/core/evalf.py @@ -139,11 +139,11 @@ def scaled_zero(mag, sign=1): >>> from sympy.core.evalf import scaled_zero >>> from sympy import Float >>> z, p = scaled_zero(100) - >>> z, p - (([0], 1, 100, 1), -1) + >>> (z, p) == (([0], 1, 100, 1), -1) + True >>> ok = scaled_zero(z) - >>> ok - (0, 1, 100, 1) + >>> ok == (0, 1, 100, 1) + True >>> Float(ok) 1.26765060022823e+30 >>> Float(ok, p) diff --git a/sympy/core/expr.py b/sympy/core/expr.py index a94e0def59..9dac097daf 100644 --- a/sympy/core/expr.py +++ b/sympy/core/expr.py @@ -1044,13 +1044,13 @@ def args_cnc(self, cset=False, warn=True, split_1=True): >>> (-2*x*y).args_cnc() [[-1, 2, x, y], []] >>> (-2.5*x).args_cnc() - [[-1, 2.5, x], []] + [[-1, 2.50000000000000, x], []] >>> (-2*x*A*B*y).args_cnc() [[-1, 2, x, y], [A, B]] >>> (-2*x*A*B*y).args_cnc(split_1=False) [[-2, x, y], [A, B]] - >>> (-2*x*y).args_cnc(cset=True) - [set([-1, 2, x, y]), []] + >>> (-2*x*y).args_cnc(cset=True) == [{-1, 2, x, y}, []] + True The arg is always treated as a Mul: @@ -1682,12 +1682,13 @@ def as_coefficients_dict(self): ======== >>> from sympy.abc import a, x - >>> (3*x + a*x + 4).as_coefficients_dict() - {1: 4, x: 3, a*x: 1} - >>> _[a] + >>> d = (3*x + a*x + 4).as_coefficients_dict() + >>> d == {1: 4, x: 3, a*x: 1} + True + >>> d[a] 0 - >>> (3*a*x).as_coefficients_dict() - {a*x: 3} + >>> (3*a*x).as_coefficients_dict() == {a*x: 3} + True """ c, m = self.as_coeff_Mul() @@ -3174,14 +3175,14 @@ def round(self, p=0): >>> pi.round(2) 3.14 >>> (2*pi + E*I).round() - 6. + 3.*I + 6.0 + 3.0*I The round method has a chopping effect: >>> (2*pi + I/10).round() 6. >>> (pi/10 + 2*I).round() - 2.*I + 2.0*I >>> (pi/10 + E*I).round(2) 0.31 + 2.72*I diff --git a/sympy/core/exprtools.py b/sympy/core/exprtools.py index fb3d9e4e3f..7a05999d50 100644 --- a/sympy/core/exprtools.py +++ b/sympy/core/exprtools.py @@ -94,10 +94,10 @@ def __init__(self, factors=None): # Factors >>> Factors(e.as_powers_dict()) Factors({2: 1, x: 3}) >>> f = _ - >>> f.factors # underlying dictionary - {2: 1, x: 3} - >>> f.gens # base of each factor - frozenset([2, x]) + >>> f.factors == {2: 1, x: 3} + True + >>> f.gens == frozenset([2, x]) # base of each factor + True >>> Factors(0) Factors({0: 1}) >>> Factors(I) diff --git a/sympy/core/numbers.py b/sympy/core/numbers.py index c37ae64934..e08f2cfe32 100644 --- a/sympy/core/numbers.py +++ b/sympy/core/numbers.py @@ -647,8 +647,8 @@ class Float(Number): An actual mpf tuple also contains the number of bits in c as the last element of the tuple: - >>> _._mpf_ - (1, 5, 0, 3) + >>> _._mpf_ == (1, 5, 0, 3) + True This is not needed for instantiation and is not the same thing as the precision. The mpf tuple and the precision are two separate quantities diff --git a/sympy/core/operations.py b/sympy/core/operations.py index 9a3b3a6d21..c8e0d80d0b 100644 --- a/sympy/core/operations.py +++ b/sympy/core/operations.py @@ -141,8 +141,8 @@ def _matches_commutative(self, expr, repl_dict={}, old=False): >>> b = Wild("b") >>> c = Wild("c") >>> x, y, z = symbols("x y z") - >>> (a+sin(b)*c)._matches_commutative(x+sin(y)*z) - {a_: x, b_: y, c_: z} + >>> (a+sin(b)*c)._matches_commutative(x+sin(y)*z) == {a: x, b: y, c: z} + True In the example above, "a+sin(b)*c" is the pattern, and "x+sin(y)*z" is the expression. @@ -150,14 +150,14 @@ def _matches_commutative(self, expr, repl_dict={}, old=False): The repl_dict contains parts that were already matched. For example here: - >>> (x+sin(b)*c)._matches_commutative(x+sin(y)*z, repl_dict={a: x}) - {a_: x, b_: y, c_: z} + >>> (x+sin(b)*c)._matches_commutative(x+sin(y)*z, repl_dict={a: x}) == {a: x, b: y, c: z} + True the only function of the repl_dict is to return it in the result, e.g. if you omit it: - >>> (x+sin(b)*c)._matches_commutative(x+sin(y)*z) - {b_: y, c_: z} + >>> (x+sin(b)*c)._matches_commutative(x+sin(y)*z) == {b: y, c: z} + True the "a: x" is not returned in the result, but otherwise it is equivalent. diff --git a/sympy/core/power.py b/sympy/core/power.py index 31f50ac379..5d5b81ca62 100644 --- a/sympy/core/power.py +++ b/sympy/core/power.py @@ -521,7 +521,7 @@ def _check(ct1, ct2, old): if old == self.base: return new**self.exp._subs(old, new) - if old.func is self.func and self.base is old.base: + if old.func is self.func and self.base == old.base: if self.exp.is_Add is False: ct1 = self.exp.as_independent(Symbol, as_Add=False) ct2 = old.exp.as_independent(Symbol, as_Add=False) diff --git a/sympy/core/symbol.py b/sympy/core/symbol.py index 8bd4173858..16d9a11f2a 100644 --- a/sympy/core/symbol.py +++ b/sympy/core/symbol.py @@ -242,8 +242,8 @@ class Wild(Symbol): >>> from sympy import symbols >>> a, b = symbols('a b', cls=Wild) - >>> (2 + 3*y).match(a*x + b*y) - {a_: 2/x, b_: 3} + >>> (2 + 3*y).match(a*x + b*y) == {a: 2/x, b: 3} + True This is technically correct, because (2/x)*x + 3*y == 2 + 3*y, but you probably @@ -261,14 +261,14 @@ class Wild(Symbol): >>> E = 2*x**3*y*z >>> a, b = symbols('a b', cls=Wild) - >>> E.match(a*b) - {a_: 2*y*z, b_: x**3} + >>> E.match(a*b) == {a: 2*y*z, b: x**3} + True >>> a = Wild('a', exclude=[x, y]) - >>> E.match(a*b) - {a_: z, b_: 2*x**3*y} + >>> E.match(a*b) == {a: z, b: 2*x**3*y} + True >>> a = Wild('a', exclude=[x, y, z]) - >>> E.match(a*b) - {a_: 2, b_: x**3*y*z} + >>> E.match(a*b) == {a: 2, b: x**3*y*z} + True """ is_Wild = True @@ -336,8 +336,8 @@ def symbols(names, **args): (a, b, c) >>> symbols(['a', 'b', 'c']) [a, b, c] - >>> symbols(set(['a', 'b', 'c'])) - set([a, b, c]) + >>> symbols(set(['a', 'b', 'c'])) == {a, b, c} + True If an iterable container is needed for a single symbol, set the ``seq`` argument to ``True`` or terminate the symbol name with a comma:: diff --git a/sympy/core/sympify.py b/sympy/core/sympify.py index 899992147d..8813f13898 100644 --- a/sympy/core/sympify.py +++ b/sympy/core/sympify.py @@ -139,8 +139,9 @@ def sympify(a, locals=None, convert_xor=True, strict=False, rational=False, names that are defined in abc). >>> from sympy.abc import _clash1 - >>> _clash1 - {'E': E, 'I': I, 'N': N, 'O': O, 'Q': Q, 'S': S} + >>> _clash1 == {'E': Symbol('E'), 'I': Symbol('I'), 'O': Symbol('O'), + ... 'N': Symbol('N'), 'Q': Symbol('Q'), 'S': Symbol('S')} + True >>> sympify('E & Q', _clash1) And(E, Q) diff --git a/sympy/core/tests/test_subs.py b/sympy/core/tests/test_subs.py index c0e9977d06..03e8c1b42b 100644 --- a/sympy/core/tests/test_subs.py +++ b/sympy/core/tests/test_subs.py @@ -635,3 +635,16 @@ def test_issue_5217(): assert z.subs(sub) == 1 - s assert q == 4*x**2*y**2 assert q.subs(sub) == 2*y**2*s + + +def test_pow_eval_subs_no_cache(): + from sympy.core.cache import clear_cache + + s = 1/sqrt(x**2) + # This bug only appeared when the cache was turned off. + clear_cache() + + # This used to fail with a wrong result. + # It incorrectly returned 1/sqrt(x**2) before. + result = s.subs(sqrt(x**2), y) + assert result == 1/y diff --git a/sympy/crypto/crypto.py b/sympy/crypto/crypto.py index 0bfbaabace..88102ce410 100644 --- a/sympy/crypto/crypto.py +++ b/sympy/crypto/crypto.py @@ -1305,9 +1305,18 @@ def lfsr_sequence(key, fill, n): >>> F = FF(2) >>> fill = [F(1), F(1), F(0), F(1)] >>> key = [F(1), F(0), F(0), F(1)] - >>> lfsr_sequence(key, fill, 10) - [1 mod 2, 1 mod 2, 0 mod 2, 1 mod 2, 0 mod 2, 1 mod 2, 1 mod 2, 0 mod 2, 0 mod 2, 1 mod 2] - + >>> for i in lfsr_sequence(key, fill, 10): + ... print(i) + 1 mod 2 + 1 mod 2 + 0 mod 2 + 1 mod 2 + 0 mod 2 + 1 mod 2 + 1 mod 2 + 0 mod 2 + 0 mod 2 + 1 mod 2 """ if not isinstance(key, list): raise TypeError("key must be a list") @@ -1583,9 +1592,8 @@ def decipher_elgamal(ct, prk): ======== >>> from sympy.crypto.crypto import decipher_elgamal - >>> decipher_elgamal((835, 271), (1031, 14, 636)) - 100 - + >>> decipher_elgamal((835, 271), (1031, 14, 636)) == 100 + True """ u = igcdex(ct[0] ** prk[2], prk[0])[0] return u * ct[1] % prk[0] diff --git a/sympy/diffgeom/diffgeom.py b/sympy/diffgeom/diffgeom.py index d40c8cbced..6c66276240 100644 --- a/sympy/diffgeom/diffgeom.py +++ b/sympy/diffgeom/diffgeom.py @@ -538,15 +538,15 @@ class BaseVectorField(Expr): Vector field: >>> v = BaseVectorField(R2_r, 1) - >>> pprint(v(s_field)) + >>> pprint(v(s_field), use_unicode=False) / d \| |-----(g(x, xi_2))|| \dxi_2 /|xi_2=y - >>> pprint(v(s_field).rcall(point_r).doit()) + >>> pprint(v(s_field).rcall(point_r).doit(), use_unicode=False) d ---(g(x0, y0)) dy0 - >>> pprint(v(s_field).rcall(point_p).doit()) + >>> pprint(v(s_field).rcall(point_p).doit(), use_unicode=False) / d \| |-----(g(r0*cos(theta0), xi_2))|| \dxi_2 /|xi_2=r0*sin(theta0) @@ -704,11 +704,11 @@ class Differential(Expr): >>> dg = Differential(s_field) >>> dg d(g(x, y)) - >>> pprint(dg(e_x)) + >>> pprint(dg(e_x), use_unicode=False) / d \| |-----(g(xi_1, y))|| \dxi_1 /|xi_1=x - >>> pprint(dg(e_y)) + >>> pprint(dg(e_y), use_unicode=False) / d \| |-----(g(x, xi_2))|| \dxi_2 /|xi_2=y diff --git a/sympy/functions/combinatorial/numbers.py b/sympy/functions/combinatorial/numbers.py index fc42c1e3e4..8a7f58ee37 100644 --- a/sympy/functions/combinatorial/numbers.py +++ b/sympy/functions/combinatorial/numbers.py @@ -851,7 +851,7 @@ class catalan(Function): and evaluated with arbitrary precision: >>> catalan(I).evalf(20) - 0.39764993382373624267 - 0.020884341620842555705*I + 0.39764993382373624267 - 0.020884341620842555704*I References ========== diff --git a/sympy/functions/special/bessel.py b/sympy/functions/special/bessel.py index 38b966f2aa..7b7075561e 100644 --- a/sympy/functions/special/bessel.py +++ b/sympy/functions/special/bessel.py @@ -698,7 +698,7 @@ def jn_zeros(n, k, method="sympy", dps=15): >>> from sympy import jn_zeros >>> jn_zeros(2, 4, dps=5) - [5.7635, 9.095, 12.323, 15.515] + [5.7635, 9.0950, 12.323, 15.515] See Also ======== diff --git a/sympy/functions/special/tests/test_hyper.py b/sympy/functions/special/tests/test_hyper.py index b337e5c1f6..d7308ded9a 100644 --- a/sympy/functions/special/tests/test_hyper.py +++ b/sympy/functions/special/tests/test_hyper.py @@ -2,7 +2,7 @@ cos, sqrt, symbols, oo, Derivative, gamma, O) from sympy.series.limits import limit from sympy.abc import x, z, k -from sympy.utilities.pytest import raises +from sympy.utilities.pytest import raises, slow from sympy.utilities.randtest import ( random_complex_number as randcplx, verify_numerically as tn, @@ -206,6 +206,7 @@ def test_hyper_unpolarify(): assert hyper([0, 1], [0], a).argument == a +@slow def test_hyperrep(): from sympy.functions.special.hyper import (HyperRep, HyperRep_atanh, HyperRep_power1, HyperRep_power2, HyperRep_log1, HyperRep_asin1, diff --git a/sympy/geometry/curve.py b/sympy/geometry/curve.py index ab82a3ff5e..be6ba75c18 100644 --- a/sympy/geometry/curve.py +++ b/sympy/geometry/curve.py @@ -96,10 +96,10 @@ def free_symbols(self): >>> from sympy.abc import t, a >>> from sympy.geometry import Curve - >>> Curve((t, t**2), (t, 0, 2)).free_symbols - set() - >>> Curve((t, t**2), (t, a, 2)).free_symbols - set([a]) + >>> Curve((t, t**2), (t, 0, 2)).free_symbols == set() + True + >>> Curve((t, t**2), (t, a, 2)).free_symbols == {a} + True """ free = set() for a in self.functions + self.limits[1:]: diff --git a/sympy/geometry/polygon.py b/sympy/geometry/polygon.py index 1201b39d69..cb0cf62d4a 100644 --- a/sympy/geometry/polygon.py +++ b/sympy/geometry/polygon.py @@ -1387,12 +1387,12 @@ def angles(self): Examples ======== - >>> from sympy import RegularPolygon, Point + >>> from sympy import RegularPolygon, Point, sqrt, pi >>> r = RegularPolygon(Point(0, 0), 5, 3) - >>> r.angles - {Point(-5/2, -5*sqrt(3)/2): pi/3, - Point(-5/2, 5*sqrt(3)/2): pi/3, - Point(5, 0): pi/3} + >>> r.angles == {Point(-5/2, -5*sqrt(3)/2): pi/3, + ... Point(-5/2, 5*sqrt(3)/2): pi/3, + ... Point(5, 0): pi/3} + True """ ret = {} ang = self.interior_angle diff --git a/sympy/integrals/heurisch.py b/sympy/integrals/heurisch.py index 02e85df37d..5e526ba848 100644 --- a/sympy/integrals/heurisch.py +++ b/sympy/integrals/heurisch.py @@ -42,8 +42,8 @@ def components(f, x): >>> from sympy.abc import x, y >>> from sympy.integrals.heurisch import components - >>> components(sin(x)*cos(x)**2, x) - set([x, sin(x), cos(x)]) + >>> components(sin(x)*cos(x)**2, x) == {x, sin(x), cos(x)} + True See Also ======== diff --git a/sympy/integrals/integrals.py b/sympy/integrals/integrals.py index 5ee8bd6a5a..b02330f342 100644 --- a/sympy/integrals/integrals.py +++ b/sympy/integrals/integrals.py @@ -90,8 +90,8 @@ def free_symbols(self): >>> from sympy import Integral >>> from sympy.abc import x, y - >>> Integral(x, (x, y, 1)).free_symbols - set([y]) + >>> Integral(x, (x, y, 1)).free_symbols == {y} + True See Also ======== @@ -567,8 +567,8 @@ def _eval_derivative(self, sym): The previous must be true since there is no y in the evaluated integral: - >>> i.free_symbols - set([x]) + >>> i.free_symbols == {x} + True >>> i.doit() 2*x**3/3 - x/2 - 1/6 diff --git a/sympy/integrals/meijerint.py b/sympy/integrals/meijerint.py index 6428e7ada5..babfc4f564 100644 --- a/sympy/integrals/meijerint.py +++ b/sympy/integrals/meijerint.py @@ -339,14 +339,14 @@ def _exponents(expr, x): >>> from sympy.integrals.meijerint import _exponents >>> from sympy.abc import x, y >>> from sympy import sin - >>> _exponents(x, x) - set([1]) - >>> _exponents(x**2, x) - set([2]) - >>> _exponents(x**2 + x, x) - set([1, 2]) - >>> _exponents(x**3*sin(x + x**y) + 1/x, x) - set([-1, 1, 3, y]) + >>> _exponents(x, x) == {1} + True + >>> _exponents(x**2, x) == {2} + True + >>> _exponents(x**2 + x, x) == {1, 2} + True + >>> _exponents(x**3*sin(x + x**y) + 1/x, x) == {-1, 1, 3, y} + True """ def _exponents_(expr, x, res): if expr == x: @@ -376,12 +376,12 @@ def _find_splitting_points(expr, x): >>> from sympy.integrals.meijerint import _find_splitting_points as fsp >>> from sympy import sin >>> from sympy.abc import a, x - >>> fsp(x, x) - set([0]) - >>> fsp((x-1)**3, x) - set([1]) - >>> fsp(sin(x+3)*x, x) - set([-3, 0]) + >>> fsp(x, x) == {0} + True + >>> fsp((x-1)**3, x) == {1} + True + >>> fsp(sin(x+3)*x, x) == {-3, 0} + True """ p, q = [Wild(n, exclude=[x]) for n in 'pq'] diff --git a/sympy/integrals/risch.py b/sympy/integrals/risch.py index 283316e0e4..f9fa578dbc 100644 --- a/sympy/integrals/risch.py +++ b/sympy/integrals/risch.py @@ -1596,7 +1596,7 @@ def risch_integrate(f, x, extension=None, handle_first='log', First, we try integrating exp(-x**2). Except for a constant factor of 2/sqrt(pi), this is the famous error function. - >>> pprint(risch_integrate(exp(-x**2), x)) + >>> pprint(risch_integrate(exp(-x**2), x), use_unicode=False) / | | 2 @@ -1613,7 +1613,7 @@ def risch_integrate(f, x, extension=None, handle_first='log', For example, >>> pprint(risch_integrate((2*log(x)**2 - log(x) - x**2)/(log(x)**3 - - ... x**2*log(x)), x)) + ... x**2*log(x)), x), use_unicode=False) / | log(-x + log(x)) log(x + log(x)) | 1 @@ -1631,11 +1631,11 @@ def risch_integrate(f, x, extension=None, handle_first='log', nested exponentials and logarithms, as well as exponentials with bases other than E. - >>> pprint(risch_integrate(exp(x)*exp(exp(x)), x)) + >>> pprint(risch_integrate(exp(x)*exp(exp(x)), x), use_unicode=False) / x\ \e / e - >>> pprint(risch_integrate(exp(exp(x)), x)) + >>> pprint(risch_integrate(exp(exp(x)), x), use_unicode=False) / | | / x\ @@ -1644,10 +1644,10 @@ def risch_integrate(f, x, extension=None, handle_first='log', | / - >>> pprint(risch_integrate(x*x**x*log(x) + x**x + x*x**x, x)) + >>> pprint(risch_integrate(x*x**x*log(x) + x**x + x*x**x, x), use_unicode=False) x x*x - >>> pprint(risch_integrate(x**x*log(x), x)) + >>> pprint(risch_integrate(x**x*log(x), x), use_unicode=False) / | | x @@ -1655,7 +1655,7 @@ def risch_integrate(f, x, extension=None, handle_first='log', | / - >>> pprint(risch_integrate(-1/(x*log(x)*log(log(x))**2), x)) + >>> pprint(risch_integrate(-1/(x*log(x)*log(log(x))**2), x), use_unicode=False) 1 ----------- log(log(x)) diff --git a/sympy/integrals/tests/test_failing_integrals.py b/sympy/integrals/tests/test_failing_integrals.py index 4f283f3148..d418d58277 100644 --- a/sympy/integrals/tests/test_failing_integrals.py +++ b/sympy/integrals/tests/test_failing_integrals.py @@ -44,7 +44,10 @@ def test_issue_4212(): @XFAIL +@slow def test_issue_4326(): + if ON_TRAVIS: + skip("Too slow for travis.") assert integrate(((h*(x - R + b))/b)*sqrt(R**2 - x**2), (x, R - b, R)).has(Integral) @@ -112,7 +115,10 @@ def test_issue_4891(): @XFAIL +@slow def test_issue_1796a(): + if ON_TRAVIS: + skip("Too slow for travis.") assert not integrate(exp(2*b*x)*exp(-a*x**2), x).has(Integral) diff --git a/sympy/integrals/tests/test_integrals.py b/sympy/integrals/tests/test_integrals.py index 0e23a5b657..8570700ed5 100644 --- a/sympy/integrals/tests/test_integrals.py +++ b/sympy/integrals/tests/test_integrals.py @@ -351,6 +351,7 @@ def NS(e, n=15, **options): return sstr(sympify(e).evalf(n, **options), full_prec=True) +@slow def test_evalf_integrals(): assert NS(Integral(x, (x, 2, 5)), 15) == '10.5000000000000' gauss = Integral(exp(-x**2), (x, -oo, oo)) @@ -843,6 +844,7 @@ def test_issue_4199(): Integral(exp(-I*2*pi*ypos*x)*x, (x, -oo, oo)) +@slow def test_issue_3940(): a, b, c, d = symbols('a:d', positive=True, finite=True) assert integrate(exp(-x**2 + I*c*x), x) == \ diff --git a/sympy/integrals/tests/test_meijerint.py b/sympy/integrals/tests/test_meijerint.py index e114334646..b7e9f4b461 100644 --- a/sympy/integrals/tests/test_meijerint.py +++ b/sympy/integrals/tests/test_meijerint.py @@ -528,6 +528,7 @@ def E(expr): (x, 0, oo)))) == polygamma(0, k) +@slow def test_expint(): """ Test various exponential integrals. """ from sympy import (expint, unpolarify, Symbol, Ci, Si, Shi, Chi, diff --git a/sympy/integrals/tests/test_transforms.py b/sympy/integrals/tests/test_transforms.py index cc9e22a6e5..b2934f455b 100644 --- a/sympy/integrals/tests/test_transforms.py +++ b/sympy/integrals/tests/test_transforms.py @@ -161,6 +161,7 @@ def test_mellin_transform(): (-gamma(s + S(1)/2)/(sqrt(pi)*s), (-S(1)/2, 0), True) +@slow def test_mellin_transform_bessel(): from sympy import Max MT = mellin_transform @@ -303,6 +304,7 @@ def test_expint(): (expint(2, x)*Heaviside(x)).rewrite(Ei).rewrite(expint).expand() +@slow def test_inverse_mellin_transform(): from sympy import (sin, simplify, Max, Min, expand, powsimp, exp_polar, cos, cot) @@ -438,6 +440,7 @@ def mysimp(expr): assert IMT(pi/cos(pi*s), s, x, (0, S(1)/2)) == sqrt(x)/(x + 1) +@slow def test_laplace_transform(): from sympy import fresnels, fresnelc LT = laplace_transform diff --git a/sympy/logic/algorithms/dpll.py b/sympy/logic/algorithms/dpll.py index 61b2e0e387..39d1c32581 100644 --- a/sympy/logic/algorithms/dpll.py +++ b/sympy/logic/algorithms/dpll.py @@ -23,11 +23,10 @@ def dpll_satisfiable(expr): >>> from sympy.abc import A, B >>> from sympy.logic.algorithms.dpll import dpll_satisfiable - >>> dpll_satisfiable(A & ~B) - {A: True, B: False} + >>> dpll_satisfiable(A & ~B) == {A: True, B: False} + True >>> dpll_satisfiable(A & ~A) False - """ clauses = conjuncts(to_cnf(expr)) if False in clauses: @@ -208,8 +207,8 @@ def unit_propagate_int_repr(clauses, s): representation >>> from sympy.logic.algorithms.dpll import unit_propagate_int_repr - >>> unit_propagate_int_repr([{1, 2}, {3, -2}, {2}], 2) - [set([3])] + >>> unit_propagate_int_repr([{1, 2}, {3, -2}, {2}], 2) == [{3}] + True """ negated = {-s} diff --git a/sympy/logic/algorithms/dpll2.py b/sympy/logic/algorithms/dpll2.py index bbec92d876..fa80d03c04 100644 --- a/sympy/logic/algorithms/dpll2.py +++ b/sympy/logic/algorithms/dpll2.py @@ -29,11 +29,10 @@ def dpll_satisfiable(expr, all_models=False): >>> from sympy.abc import A, B >>> from sympy.logic.algorithms.dpll2 import dpll_satisfiable - >>> dpll_satisfiable(A & ~B) - {A: True, B: False} + >>> dpll_satisfiable(A & ~B) == {A: True, B: False} + True >>> dpll_satisfiable(A & ~A) False - """ clauses = conjuncts(to_cnf(expr)) if False in clauses: @@ -181,8 +180,9 @@ def _find_model(self): >>> from sympy.abc import A, B, C >>> l = SATSolver([{2, -3}, {1}, {3, -3}, {2, -2}, ... {3, -2}], {1, 2, 3}, set(), [A, B, C]) - >>> list(l._find_model()) - [{A: True, B: False, C: False}, {A: True, B: True, C: True}] + >>> list(l._find_model()) == [{A: True, B: False, C: False}, + ... {A: True, B: True, C: True}] + True """ # We use this variable to keep track of if we should flip a @@ -274,8 +274,8 @@ def _current_level(self): 0 >>> l._current_level.flipped False - >>> l._current_level.var_settings - set([1, 2]) + >>> l._current_level.var_settings == {1, 2} + True """ return self.levels[-1] @@ -336,8 +336,8 @@ def _assign_literal(self, lit): ... {3, -2}], {1, 2, 3}, set()) >>> next(l._find_model()) {1: True, 2: False, 3: False} - >>> l.var_settings - set([-3, -2, 1]) + >>> l.var_settings == {-3, -2, 1} + True >>> l = SATSolver([{2, -3}, {1}, {3, -3}, {2, -2}, ... {3, -2}], {1, 2, 3}, set()) @@ -346,8 +346,8 @@ def _assign_literal(self, lit): ... next(l._find_model()) ... except StopIteration: ... pass - >>> l.var_settings - set([-1]) + >>> l.var_settings == {-1} + True """ self.var_settings.add(lit) self._current_level.var_settings.add(lit) @@ -386,12 +386,12 @@ def _undo(self): >>> next(l._find_model()) {1: True, 2: False, 3: False} >>> level = l._current_level - >>> level.decision, level.var_settings, level.flipped - (-3, set([-3, -2]), False) + >>> (level.decision, level.var_settings, level.flipped) == (-3, {-3, -2}, False) + True >>> l._undo() >>> level = l._current_level - >>> level.decision, level.var_settings, level.flipped - (0, set([1]), False) + >>> (level.decision, level.var_settings, level.flipped) == (0, {1}, False) + True """ # Undo the variable settings for lit in self._current_level.var_settings: @@ -416,21 +416,21 @@ def _simplify(self): Examples ======== + >>> from collections import defaultdict >>> from sympy.logic.algorithms.dpll2 import SATSolver >>> l = SATSolver([{2, -3}, {1}, {3, -3}, {2, -2}, ... {3, -2}], {1, 2, 3}, set()) >>> l.variable_set [False, False, False, False] - >>> l.sentinels - {-3: set([0, 2]), -2: set([3, 4]), 2: set([0, 3]), 3: set([2, 4])} + >>> l.sentinels == defaultdict(int, {-3: {0, 2}, -2: {3, 4}, 2: {0, 3}, 3: {2, 4}}) + True >>> l._simplify() >>> l.variable_set [False, True, False, False] - >>> l.sentinels - {-3: set([0, 2]), -2: set([3, 4]), -1: set(), 2: set([0, 3]), - ...3: set([2, 4])} + >>> l.sentinels == defaultdict(int, {-3: {0, 2}, -2: {3, 4}, -1: set(), 2: {0, 3}, 3: {2, 4}}) + True """ changed = True while changed: @@ -480,13 +480,13 @@ def _vsids_decay(self): >>> l = SATSolver([{2, -3}, {1}, {3, -3}, {2, -2}, ... {3, -2}], {1, 2, 3}, set()) - >>> l.lit_scores - {-3: -2.0, -2: -2.0, -1: 0.0, 1: 0.0, 2: -2.0, 3: -2.0} + >>> l.lit_scores == {-3: -2.0, -2: -2.0, -1: 0.0, 1: 0.0, 2: -2.0, 3: -2.0} + True >>> l._vsids_decay() - >>> l.lit_scores - {-3: -1.0, -2: -1.0, -1: 0.0, 1: 0.0, 2: -1.0, 3: -1.0} + >>> l.lit_scores == {-3: -1.0, -2: -1.0, -1: 0.0, 1: 0.0, 2: -1.0, 3: -1.0} + True """ # We divide every literal score by 2 for a decay factor # Note: This doesn't change the heap property @@ -562,15 +562,15 @@ def _vsids_clause_added(self, cls): >>> l.num_learned_clauses 0 - >>> l.lit_scores - {-3: -2.0, -2: -2.0, -1: 0.0, 1: 0.0, 2: -2.0, 3: -2.0} + >>> l.lit_scores == {-3: -2.0, -2: -2.0, -1: 0.0, 1: 0.0, 2: -2.0, 3: -2.0} + True >>> l._vsids_clause_added({2, -3}) >>> l.num_learned_clauses 1 - >>> l.lit_scores - {-3: -1.0, -2: -2.0, -1: 0.0, 1: 0.0, 2: -1.0, 3: -2.0} + >>> l.lit_scores == {-3: -1.0, -2: -2.0, -1: 0.0, 1: 0.0, 2: -1.0, 3: -2.0} + True """ self.num_learned_clauses += 1 for lit in cls: @@ -585,6 +585,7 @@ def _simple_add_learned_clause(self, cls): Examples ======== + >>> from collections import defaultdict >>> from sympy.logic.algorithms.dpll2 import SATSolver >>> l = SATSolver([{2, -3}, {1}, {3, -3}, {2, -2}, ... {3, -2}], {1, 2, 3}, set()) @@ -593,15 +594,15 @@ def _simple_add_learned_clause(self, cls): 0 >>> l.clauses [[2, -3], [1], [3, -3], [2, -2], [3, -2]] - >>> l.sentinels - {-3: set([0, 2]), -2: set([3, 4]), 2: set([0, 3]), 3: set([2, 4])} + >>> l.sentinels == defaultdict(int, {-3: {0, 2}, -2: {3, 4}, 2: {0, 3}, 3: {2, 4}}) + True >>> l._simple_add_learned_clause([3]) >>> l.clauses [[2, -3], [1], [3, -3], [2, -2], [3, -2], [3]] - >>> l.sentinels - {-3: set([0, 2]), -2: set([3, 4]), 2: set([0, 3]), 3: set([2, 4, 5])} + >>> l.sentinels == defaultdict(int, {-3: {0, 2}, -2: {3, 4}, 2: {0, 3}, 3: {2, 4, 5}}) + True """ cls_num = len(self.clauses) self.clauses.append(cls) diff --git a/sympy/logic/boolalg.py b/sympy/logic/boolalg.py index fd2bd091d0..6335e5cce6 100644 --- a/sympy/logic/boolalg.py +++ b/sympy/logic/boolalg.py @@ -912,13 +912,12 @@ def conjuncts(expr): Examples ======== - >>> from sympy.logic.boolalg import conjuncts + >>> from sympy.logic.boolalg import conjuncts, Or >>> from sympy.abc import A, B - >>> conjuncts(A & B) - frozenset([A, B]) - >>> conjuncts(A | B) - frozenset([Or(A, B)]) - + >>> conjuncts(A & B) == frozenset([A, B]) + True + >>> conjuncts(A | B) == frozenset([Or(A, B)]) + True """ return And.make_args(expr) @@ -929,13 +928,12 @@ def disjuncts(expr): Examples ======== - >>> from sympy.logic.boolalg import disjuncts + >>> from sympy.logic.boolalg import disjuncts, And >>> from sympy.abc import A, B - >>> disjuncts(A | B) - frozenset([A, B]) - >>> disjuncts(A & B) - frozenset([And(A, B)]) - + >>> disjuncts(A | B) == frozenset([A, B]) + True + >>> disjuncts(A & B) == frozenset([And(A, B)]) + True """ return Or.make_args(expr) @@ -1591,8 +1589,10 @@ def _finger(eq): >>> from sympy import And, Or, Not >>> from sympy.abc import a, b, x, y >>> eq = Or(And(Not(y), a), And(Not(y), b), And(x, y)) - >>> dict(finger(eq)) - {(0, 0, 1, 0, 2): [x], (0, 0, 1, 0, 3): [a, b], (0, 0, 1, 2, 8): [y]} + >>> dict(finger(eq)) == {(0, 0, 1, 0, 2): [x], + ... (0, 0, 1, 0, 3): [a, b], + ... (0, 0, 1, 2, 8): [y]} + True So y and x have unique fingerprints, but a and b do not. """ @@ -1637,20 +1637,22 @@ def bool_map(bool1, bool2): >>> from sympy.abc import w, x, y, z, a, b, c, d >>> function1 = SOPform(['x','z','y'],[[1, 0, 1], [0, 0, 1]]) >>> function2 = SOPform(['a','b','c'],[[1, 0, 1], [1, 0, 0]]) - >>> bool_map(function1, function2) - (And(Not(z), y), {y: a, z: b}) + >>> bool_map(function1, function2) == (And(Not(z), y), {y: a, z: b}) + True The results are not necessarily unique, but they are canonical. Here, ``(w, z)`` could be ``(a, d)`` or ``(d, a)``: >>> eq = Or(And(Not(y), w), And(Not(y), z), And(x, y)) >>> eq2 = Or(And(Not(c), a), And(Not(c), d), And(b, c)) - >>> bool_map(eq, eq2) - (Or(And(Not(y), w), And(Not(y), z), And(x, y)), {w: a, x: b, y: c, z: d}) + >>> bool_map(eq, eq2) == (Or(And(Not(y), w), And(Not(y), z), + ... And(x, y)), {w: a, x: b, y: c, z: d}) + True >>> eq = And(Xor(a, b), c, And(c,d)) - >>> bool_map(eq, eq.subs(c, x)) - (And(Or(Not(a), Not(b)), Or(a, b), c, d), {a: a, b: b, c: d, d: x}) - + >>> bool_map(eq, eq.subs(c, x)) == (And(Or(Not(a), Not(b)), + ... Or(a, b), c, d), + ... {a: a, b: b, c: d, d: x}) + True """ def match(function1, function2): diff --git a/sympy/logic/inference.py b/sympy/logic/inference.py index 1cbf5ce333..4cefdf7180 100644 --- a/sympy/logic/inference.py +++ b/sympy/logic/inference.py @@ -50,8 +50,8 @@ def satisfiable(expr, algorithm="dpll2", all_models=False): >>> from sympy.abc import A, B >>> from sympy.logic.inference import satisfiable - >>> satisfiable(A & ~B) - {A: True, B: False} + >>> satisfiable(A & ~B) == {A: True, B: False} + True >>> satisfiable(A & ~A) False >>> satisfiable(True) @@ -59,10 +59,10 @@ def satisfiable(expr, algorithm="dpll2", all_models=False): >>> next(satisfiable(A & ~A, all_models=True)) False >>> models = satisfiable((A >> B) & B, all_models=True) - >>> next(models) - {A: False, B: True} - >>> next(models) - {A: True, B: True} + >>> next(models) == {A: False, B: True} + True + >>> next(models) == {A: True, B: True} + True >>> def use_models(models): ... for model in models: ... if model: @@ -75,7 +75,6 @@ def satisfiable(expr, algorithm="dpll2", all_models=False): {A: False} >>> use_models(satisfiable(A ^ A, all_models=True)) UNSAT - """ expr = to_cnf(expr) if algorithm == "dpll": diff --git a/sympy/matrices/expressions/matadd.py b/sympy/matrices/expressions/matadd.py index 92c75aeb47..59d606fccc 100644 --- a/sympy/matrices/expressions/matadd.py +++ b/sympy/matrices/expressions/matadd.py @@ -88,11 +88,11 @@ def merge_explicit(matadd): >>> B = eye(2) >>> C = Matrix([[1, 2], [3, 4]]) >>> X = MatAdd(A, B, C) - >>> pprint(X) + >>> pprint(X, use_unicode=False) A + [1 0] + [1 2] [ ] [ ] [0 1] [3 4] - >>> pprint(merge_explicit(X)) + >>> pprint(merge_explicit(X), use_unicode=False) A + [2 2] [ ] [3 5] diff --git a/sympy/matrices/expressions/matmul.py b/sympy/matrices/expressions/matmul.py index 33c6d5740e..0b0842063f 100644 --- a/sympy/matrices/expressions/matmul.py +++ b/sympy/matrices/expressions/matmul.py @@ -142,21 +142,21 @@ def merge_explicit(matmul): >>> B = Matrix([[1, 1], [1, 1]]) >>> C = Matrix([[1, 2], [3, 4]]) >>> X = MatMul(A, B, C) - >>> pprint(X) + >>> pprint(X, use_unicode=False) A*[1 1]*[1 2] [ ] [ ] [1 1] [3 4] - >>> pprint(merge_explicit(X)) + >>> pprint(merge_explicit(X), use_unicode=False) A*[4 6] [ ] [4 6] >>> X = MatMul(B, A, C) - >>> pprint(X) + >>> pprint(X, use_unicode=False) [1 1]*A*[1 2] [ ] [ ] [1 1] [3 4] - >>> pprint(merge_explicit(X)) + >>> pprint(merge_explicit(X), use_unicode=False) [1 1]*A*[1 2] [ ] [ ] [1 1] [3 4] diff --git a/sympy/matrices/matrices.py b/sympy/matrices/matrices.py index 1151d8aa08..35083c296d 100644 --- a/sympy/matrices/matrices.py +++ b/sympy/matrices/matrices.py @@ -50,7 +50,7 @@ class DeferredVector(Symbol, NotIterable): >>> from sympy import DeferredVector, lambdify >>> X = DeferredVector( 'X' ) - >>> X + >>> print(X) X >>> expr = (X[0] + 2, X[2] + 3) >>> func = lambdify( X, expr) @@ -1107,8 +1107,8 @@ def atoms(self, *types): >>> from sympy.matrices import Matrix >>> Matrix([[x]]) Matrix([[x]]) - >>> _.atoms() - set([x]) + >>> _.atoms() == {x} + True """ if types: @@ -1130,8 +1130,8 @@ def free_symbols(self): >>> from sympy.abc import x >>> from sympy.matrices import Matrix - >>> Matrix([[x], [1]]).free_symbols - set([x]) + >>> Matrix([[x], [1]]).free_symbols == {x} + True """ return set().union(*[i.free_symbols for i in self]) diff --git a/sympy/ntheory/factor_.py b/sympy/ntheory/factor_.py index bd566fcdd1..d358f22271 100644 --- a/sympy/ntheory/factor_.py +++ b/sympy/ntheory/factor_.py @@ -82,12 +82,13 @@ def smoothness_p(n, m=-1, power=0, visual=None): This string can also be generated directly from a factorization dictionary and vice versa: - >>> factorint(17*9) - {3: 2, 17: 1} - >>> smoothness_p(_) + >>> f = factorint(17*9) + >>> f == {3: 2, 17: 1} + True + >>> smoothness_p(f) 'p**i=3**2 has p-1 B=2, B-pow=2\\np**i=17**1 has p-1 B=2, B-pow=16' - >>> smoothness_p(_) - {3: 2, 17: 1} + >>> smoothness_p(_) == {3: 2, 17: 1} + True The table of the output logic is: @@ -533,8 +534,8 @@ def pollard_pm1(n, B=10, a=2, retries=0, seed=1234): ... M = ilcm(M, i) ... >>> {igcd(pow(a, M, n) - 1, n) for a in range(2, 256) if - ... igcd(pow(a, M, n) - 1, n) != n} - set([1009]) + ... igcd(pow(a, M, n) - 1, n) != n} == {1009} + True But does aM % d for every divisor of n give 1? @@ -576,8 +577,8 @@ def pollard_pm1(n, B=10, a=2, retries=0, seed=1234): The B and B-pow are the same for the p - 1 factorizations of the divisors because those factorizations had a very large prime factor: - >>> factorint(4410317 - 1) - {2: 2, 617: 1, 1787: 1} + >>> factorint(4410317 - 1) == {2: 2, 617: 1, 1787: 1} + True >>> factorint(4869863-1) {2: 1, 2434931: 1} @@ -854,7 +855,7 @@ def factorint(n, limit=None, use_trial=True, use_rho=True, use_pm1=True, factorization of the integer. For example: >>> from sympy import pprint - >>> pprint(factorint(4200, visual=True)) + >>> pprint(factorint(4200, visual=True), use_unicode=False) 3 1 2 1 2 *3 *5 *7 @@ -871,11 +872,11 @@ def factorint(n, limit=None, use_trial=True, use_rho=True, use_pm1=True, >>> from sympy import Mul, Pow >>> regular = factorint(1764); regular {2: 2, 3: 2, 7: 2} - >>> pprint(factorint(regular)) + >>> pprint(factorint(regular), use_unicode=False) 2 2 2 2 *3 *7 - >>> visual = factorint(1764, visual=True); pprint(visual) + >>> visual = factorint(1764, visual=True); pprint(visual, use_unicode=False) 2 2 2 2 *3 *7 >>> print(factorint(visual)) diff --git a/sympy/ntheory/generate.py b/sympy/ntheory/generate.py index d8d598120a..e0acf2ee10 100644 --- a/sympy/ntheory/generate.py +++ b/sympy/ntheory/generate.py @@ -507,8 +507,8 @@ def primorial(n, nth=True): In this case two new primes are the factors: - >>> factorint(primorial(4) - 1) - {11: 1, 19: 1} + >>> factorint(primorial(4) - 1) == {11: 1, 19: 1} + True Here, some primes smaller and larger than the primes multiplied together are obtained: diff --git a/sympy/ntheory/multinomial.py b/sympy/ntheory/multinomial.py index 77ebe4a84e..a2730c9c5b 100644 --- a/sympy/ntheory/multinomial.py +++ b/sympy/ntheory/multinomial.py @@ -11,9 +11,10 @@ def binomial_coefficients(n): ======== >>> from sympy.ntheory import binomial_coefficients - >>> binomial_coefficients(9) - {(0, 9): 1, (1, 8): 9, (2, 7): 36, (3, 6): 84, - (4, 5): 126, (5, 4): 126, (6, 3): 84, (7, 2): 36, (8, 1): 9, (9, 0): 1} + >>> binomial_coefficients(9) == {(0, 9): 1, (1, 8): 9, (2, 7): 36, + ... (3, 6): 84, (4, 5): 126, (5, 4): 126, (6, 3): 84, + ... (7, 2): 36, (8, 1): 9, (9, 0): 1} + True See Also ======== @@ -60,8 +61,9 @@ def multinomial_coefficients0(m, n, _tuple=tuple, _zip=zip): For example: >>> from sympy import multinomial_coefficients - >>> multinomial_coefficients(2, 5) # indirect doctest - {(0, 5): 1, (1, 4): 5, (2, 3): 10, (3, 2): 10, (4, 1): 5, (5, 0): 1} + >>> multinomial_coefficients(2, 5) == {(0, 5): 1, (1, 4): 5, + ... (2, 3): 10, (3, 2): 10, (4, 1): 5, (5, 0): 1} + True The algorithm is based on the following result: @@ -118,8 +120,9 @@ def multinomial_coefficients(m, n): For example: >>> from sympy.ntheory import multinomial_coefficients - >>> multinomial_coefficients(2, 5) # indirect doctest - {(0, 5): 1, (1, 4): 5, (2, 3): 10, (3, 2): 10, (4, 1): 5, (5, 0): 1} + >>> multinomial_coefficients(2, 5) == {(0, 5): 1, (1, 4): 5, + ... (2, 3): 10, (3, 2): 10, (4, 1): 5, (5, 0): 1} + True The algorithm is based on the following result: diff --git a/sympy/plotting/tests/test_plot.py b/sympy/plotting/tests/test_plot.py index d6918d7caf..26b9604560 100644 --- a/sympy/plotting/tests/test_plot.py +++ b/sympy/plotting/tests/test_plot.py @@ -10,6 +10,7 @@ from tempfile import NamedTemporaryFile import os +import errno import sys @@ -30,8 +31,6 @@ def wrapper(*args, **kwargs): # XXX: We could implement this as a context manager instead -# That would need rewriting the plot_and_save() function -# entirely class TmpFileManager: tmp_files = [] @@ -43,202 +42,224 @@ def tmp_file(cls, name=''): @classmethod def cleanup(cls): - map(os.remove, cls.tmp_files) + for f in cls.tmp_files: + try: + os.remove(f) + except OSError as e: + if e.errno != errno.ENOENT: + raise -def plot_and_save(name): - tmp_file = TmpFileManager.tmp_file +def test_matplotlib_intro(): + """Examples from the 'introduction' notebook.""" + matplotlib = import_module('matplotlib', min_module_version='1.1.0', catch=(RuntimeError,)) + if not matplotlib: + skip("Matplotlib not the default backend") + try: + name = 'test' + tmp_file = TmpFileManager.tmp_file - x = Symbol('x') - y = Symbol('y') - z = Symbol('z') - - ### - # Examples from the 'introduction' notebook - ### + x = Symbol('x') + y = Symbol('y') + z = Symbol('z') + + p = plot(x) + p = plot(x*sin(x), x*cos(x)) + p.extend(p) + p[0].line_color = lambda a: a + p[1].line_color = 'b' + p.title = 'Big title' + p.xlabel = 'the x axis' + p[1].label = 'straight line' + p.legend = True + p.aspect_ratio = (1, 1) + p.xlim = (-15, 20) + p.save(tmp_file('%s_basic_options_and_colors' % name)) + + p.extend(plot(x + 1)) + p.append(plot(x + 3, x**2)[1]) + p.save(tmp_file('%s_plot_extend_append' % name)) + + p[2] = plot(x**2, (x, -2, 3)) + p.save(tmp_file('%s_plot_setitem' % name)) + + p = plot(sin(x), (x, -2*pi, 4*pi)) + p.save(tmp_file('%s_line_explicit' % name)) + + p = plot(sin(x)) + p.save(tmp_file('%s_line_default_range' % name)) + + p = plot((x**2, (x, -5, 5)), (x**3, (x, -3, 3))) + p.save(tmp_file('%s_line_multiple_range' % name)) + + raises(ValueError, lambda: plot(x, y)) + + # parametric 2d plots. + + # Single plot with default range. + plot_parametric(sin(x), cos(x)).save(tmp_file()) + + # Single plot with range. + p = plot_parametric(sin(x), cos(x), (x, -5, 5)) + p.save(tmp_file('%s_parametric_range' % name)) + + # Multiple plots with same range. + p = plot_parametric((sin(x), cos(x)), (x, sin(x))) + p.save(tmp_file('%s_parametric_multiple' % name)) + + # Multiple plots with different ranges. + p = plot_parametric((sin(x), cos(x), (x, -3, 3)), (x, sin(x), (x, -5, 5))) + p.save(tmp_file('%s_parametric_multiple_ranges' % name)) - p = plot(x) - p = plot(x*sin(x), x*cos(x)) - p.extend(p) - p[0].line_color = lambda a: a - p[1].line_color = 'b' - p.title = 'Big title' - p.xlabel = 'the x axis' - p[1].label = 'straight line' - p.legend = True - p.aspect_ratio = (1, 1) - p.xlim = (-15, 20) - p.save(tmp_file('%s_basic_options_and_colors' % name)) - - p.extend(plot(x + 1)) - p.append(plot(x + 3, x**2)[1]) - p.save(tmp_file('%s_plot_extend_append' % name)) - - p[2] = plot(x**2, (x, -2, 3)) - p.save(tmp_file('%s_plot_setitem' % name)) - - p = plot(sin(x), (x, -2*pi, 4*pi)) - p.save(tmp_file('%s_line_explicit' % name)) - - p = plot(sin(x)) - p.save(tmp_file('%s_line_default_range' % name)) - - p = plot((x**2, (x, -5, 5)), (x**3, (x, -3, 3))) - p.save(tmp_file('%s_line_multiple_range' % name)) - - raises(ValueError, lambda: plot(x, y)) - - #parametric 2d plots. - #Single plot with default range. - plot_parametric(sin(x), cos(x)).save(tmp_file()) - - #Single plot with range. - p = plot_parametric(sin(x), cos(x), (x, -5, 5)) - p.save(tmp_file('%s_parametric_range' % name)) - - #Multiple plots with same range. - p = plot_parametric((sin(x), cos(x)), (x, sin(x))) - p.save(tmp_file('%s_parametric_multiple' % name)) - - #Multiple plots with different ranges. - p = plot_parametric((sin(x), cos(x), (x, -3, 3)), (x, sin(x), (x, -5, 5))) - p.save(tmp_file('%s_parametric_multiple_ranges' % name)) - - #depth of recursion specified. - p = plot_parametric(x, sin(x), depth=13) - p.save(tmp_file('%s_recursion_depth' % name)) - - #No adaptive sampling. - p = plot_parametric(cos(x), sin(x), adaptive=False, nb_of_points=500) - p.save(tmp_file('%s_adaptive' % name)) - - #3d parametric plots - p = plot3d_parametric_line(sin(x), cos(x), x) - p.save(tmp_file('%s_3d_line' % name)) - - p = plot3d_parametric_line( - (sin(x), cos(x), x, (x, -5, 5)), (cos(x), sin(x), x, (x, -3, 3))) - p.save(tmp_file('%s_3d_line_multiple' % name)) - - p = plot3d_parametric_line(sin(x), cos(x), x, nb_of_points=30) - p.save(tmp_file('%s_3d_line_points' % name)) - - # 3d surface single plot. - p = plot3d(x * y) - p.save(tmp_file('%s_surface' % name)) - - # Multiple 3D plots with same range. - p = plot3d(-x * y, x * y, (x, -5, 5)) - p.save(tmp_file('%s_surface_multiple' % name)) - - # Multiple 3D plots with different ranges. - p = plot3d( - (x * y, (x, -3, 3), (y, -3, 3)), (-x * y, (x, -3, 3), (y, -3, 3))) - p.save(tmp_file('%s_surface_multiple_ranges' % name)) - - # Single Parametric 3D plot - p = plot3d_parametric_surface(sin(x + y), cos(x - y), x - y) - p.save(tmp_file('%s_parametric_surface' % name)) - - # Multiple Parametric 3D plots. - p = plot3d_parametric_surface( - (x*sin(z), x*cos(z), z, (x, -5, 5), (z, -5, 5)), - (sin(x + y), cos(x - y), x - y, (x, -5, 5), (y, -5, 5))) - p.save(tmp_file('%s_parametric_surface' % name)) - - ### - # Examples from the 'colors' notebook - ### - - p = plot(sin(x)) - p[0].line_color = lambda a: a - p.save(tmp_file('%s_colors_line_arity1' % name)) - - p[0].line_color = lambda a, b: b - p.save(tmp_file('%s_colors_line_arity2' % name)) - - p = plot(x*sin(x), x*cos(x), (x, 0, 10)) - p[0].line_color = lambda a: a - p.save(tmp_file('%s_colors_param_line_arity1' % name)) - - p[0].line_color = lambda a, b: a - p.save(tmp_file('%s_colors_param_line_arity2a' % name)) - - p[0].line_color = lambda a, b: b - p.save(tmp_file('%s_colors_param_line_arity2b' % name)) - - p = plot3d_parametric_line(sin(x) + 0.1*sin(x)*cos(7*x), - cos(x) + 0.1*cos(x)*cos(7*x), - 0.1*sin(7*x), - (x, 0, 2*pi)) - p[0].line_color = lambda a: sin(4*a) - p.save(tmp_file('%s_colors_3d_line_arity1' % name)) - p[0].line_color = lambda a, b: b - p.save(tmp_file('%s_colors_3d_line_arity2' % name)) - p[0].line_color = lambda a, b, c: c - p.save(tmp_file('%s_colors_3d_line_arity3' % name)) - - p = plot3d(sin(x)*y, (x, 0, 6*pi), (y, -5, 5)) - p[0].surface_color = lambda a: a - p.save(tmp_file('%s_colors_surface_arity1' % name)) - p[0].surface_color = lambda a, b: b - p.save(tmp_file('%s_colors_surface_arity2' % name)) - p[0].surface_color = lambda a, b, c: c - p.save(tmp_file('%s_colors_surface_arity3a' % name)) - p[0].surface_color = lambda a, b, c: sqrt((a - 3*pi)**2 + b**2) - p.save(tmp_file('%s_colors_surface_arity3b' % name)) - - p = plot3d_parametric_surface(x * cos(4 * y), x * sin(4 * y), y, - (x, -1, 1), (y, -1, 1)) - p[0].surface_color = lambda a: a - p.save(tmp_file('%s_colors_param_surf_arity1' % name)) - p[0].surface_color = lambda a, b: a*b - p.save(tmp_file('%s_colors_param_surf_arity2' % name)) - p[0].surface_color = lambda a, b, c: sqrt(a**2 + b**2 + c**2) - p.save(tmp_file('%s_colors_param_surf_arity3' % name)) - - ### - # Examples from the 'advanced' notebook - ### - - i = Integral(log((sin(x)**2 + 1)*sqrt(x**2 + 1)), (x, 0, y)) - p = plot(i, (y, 1, 5)) - p.save(tmp_file('%s_advanced_integral' % name)) - - s = summation(1/x**y, (x, 1, oo)) - p = plot(s, (y, 2, 10)) - p.save(tmp_file('%s_advanced_inf_sum' % name)) - - p = plot(summation(1/x, (x, 1, y)), (y, 2, 10), show=False) - p[0].only_integers = True - p[0].steps = True - p.save(tmp_file('%s_advanced_fin_sum' % name)) - - ### - # Test expressions that can not be translated to np and generate complex - # results. - ### - plot(sin(x) + I*cos(x)).save(tmp_file()) - plot(sqrt(sqrt(-x))).save(tmp_file()) - plot(LambertW(x)).save(tmp_file()) - plot(sqrt(LambertW(x))).save(tmp_file()) - - #Characteristic function of a StudentT distribution with nu=10 - plot((meijerg(((1 / 2,), ()), ((5, 0, 1 / 2), ()), 5 * x**2 * exp_polar(-I*pi)/2) - + meijerg(((1/2,), ()), ((5, 0, 1/2), ()), - 5*x**2 * exp_polar(I*pi)/2)) / (48 * pi), (x, 1e-6, 1e-2)).save(tmp_file()) - -def test_matplotlib(): + # depth of recursion specified. + p = plot_parametric(x, sin(x), depth=13) + p.save(tmp_file('%s_recursion_depth' % name)) + # No adaptive sampling. + p = plot_parametric(cos(x), sin(x), adaptive=False, nb_of_points=500) + p.save(tmp_file('%s_adaptive' % name)) + + # 3d parametric plots + p = plot3d_parametric_line(sin(x), cos(x), x) + p.save(tmp_file('%s_3d_line' % name)) + + p = plot3d_parametric_line( + (sin(x), cos(x), x, (x, -5, 5)), (cos(x), sin(x), x, (x, -3, 3))) + p.save(tmp_file('%s_3d_line_multiple' % name)) + + p = plot3d_parametric_line(sin(x), cos(x), x, nb_of_points=30) + p.save(tmp_file('%s_3d_line_points' % name)) + + # 3d surface single plot. + p = plot3d(x * y) + p.save(tmp_file('%s_surface' % name)) + + # Multiple 3D plots with same range. + p = plot3d(-x * y, x * y, (x, -5, 5)) + p.save(tmp_file('%s_surface_multiple' % name)) + + # Multiple 3D plots with different ranges. + p = plot3d( + (x * y, (x, -3, 3), (y, -3, 3)), (-x * y, (x, -3, 3), (y, -3, 3))) + p.save(tmp_file('%s_surface_multiple_ranges' % name)) + + # Single Parametric 3D plot + p = plot3d_parametric_surface(sin(x + y), cos(x - y), x - y) + p.save(tmp_file('%s_parametric_surface' % name)) + + # Multiple Parametric 3D plots. + p = plot3d_parametric_surface( + (x*sin(z), x*cos(z), z, (x, -5, 5), (z, -5, 5)), + (sin(x + y), cos(x - y), x - y, (x, -5, 5), (y, -5, 5))) + p.save(tmp_file('%s_parametric_surface' % name)) + finally: + TmpFileManager.cleanup() + + +def test_matplotlib_colors(): + """Examples from the 'colors' notebook.""" + matplotlib = import_module('matplotlib', min_module_version='1.1.0', catch=(RuntimeError,)) + if not matplotlib: + skip("Matplotlib not the default backend") + try: + name = 'test' + tmp_file = TmpFileManager.tmp_file + + x = Symbol('x') + y = Symbol('y') + z = Symbol('z') + + p = plot(sin(x)) + p[0].line_color = lambda a: a + p.save(tmp_file('%s_colors_line_arity1' % name)) + + p[0].line_color = lambda a, b: b + p.save(tmp_file('%s_colors_line_arity2' % name)) + + p = plot(x*sin(x), x*cos(x), (x, 0, 10)) + p[0].line_color = lambda a: a + p.save(tmp_file('%s_colors_param_line_arity1' % name)) + + p[0].line_color = lambda a, b: a + p.save(tmp_file('%s_colors_param_line_arity2a' % name)) + + p[0].line_color = lambda a, b: b + p.save(tmp_file('%s_colors_param_line_arity2b' % name)) + + p = plot3d_parametric_line(sin(x) + 0.1*sin(x)*cos(7*x), + cos(x) + 0.1*cos(x)*cos(7*x), 0.1*sin(7*x), (x, 0, 2*pi)) + p[0].line_color = lambda a: sin(4*a) + p.save(tmp_file('%s_colors_3d_line_arity1' % name)) + p[0].line_color = lambda a, b: b + p.save(tmp_file('%s_colors_3d_line_arity2' % name)) + p[0].line_color = lambda a, b, c: c + p.save(tmp_file('%s_colors_3d_line_arity3' % name)) + + p = plot3d(sin(x)*y, (x, 0, 6*pi), (y, -5, 5)) + p[0].surface_color = lambda a: a + p.save(tmp_file('%s_colors_surface_arity1' % name)) + p[0].surface_color = lambda a, b: b + p.save(tmp_file('%s_colors_surface_arity2' % name)) + p[0].surface_color = lambda a, b, c: c + p.save(tmp_file('%s_colors_surface_arity3a' % name)) + p[0].surface_color = lambda a, b, c: sqrt((a - 3*pi)**2 + b**2) + p.save(tmp_file('%s_colors_surface_arity3b' % name)) + + p = plot3d_parametric_surface(x * cos(4 * y), x * sin(4 * y), y, + (x, -1, 1), (y, -1, 1)) + p[0].surface_color = lambda a: a + p.save(tmp_file('%s_colors_param_surf_arity1' % name)) + p[0].surface_color = lambda a, b: a*b + p.save(tmp_file('%s_colors_param_surf_arity2' % name)) + p[0].surface_color = lambda a, b, c: sqrt(a**2 + b**2 + c**2) + p.save(tmp_file('%s_colors_param_surf_arity3' % name)) + finally: + TmpFileManager.cleanup() + + +def test_matplotlib_advanced(): + """Examples from the 'advanced' notebook.""" matplotlib = import_module('matplotlib', min_module_version='1.1.0', catch=(RuntimeError,)) - if matplotlib: - try: - plot_and_save('test') - finally: - # clean up - TmpFileManager.cleanup() - else: + if not matplotlib: skip("Matplotlib not the default backend") + try: + name = 'test' + tmp_file = TmpFileManager.tmp_file + + x = Symbol('x') + y = Symbol('y') + z = Symbol('z') + + i = Integral(log((sin(x)**2 + 1)*sqrt(x**2 + 1)), (x, 0, y)) + p = plot(i, (y, 1, 5)) + p.save(tmp_file('%s_advanced_integral' % name)) + + s = summation(1/x**y, (x, 1, oo)) + p = plot(s, (y, 2, 10)) + p.save(tmp_file('%s_advanced_inf_sum' % name)) + + p = plot(summation(1/x, (x, 1, y)), (y, 2, 10), show=False) + p[0].only_integers = True + p[0].steps = True + p.save(tmp_file('%s_advanced_fin_sum' % name)) + + ### + # Test expressions that can not be translated to np and + # generate complex results. + ### + plot(sin(x) + I*cos(x)).save(tmp_file()) + plot(sqrt(sqrt(-x))).save(tmp_file()) + plot(LambertW(x)).save(tmp_file()) + plot(sqrt(LambertW(x))).save(tmp_file()) + + # Characteristic function of a StudentT distribution with nu=10 + plot((meijerg(((1 / 2,), ()), ((5, 0, 1 / 2), ()), + 5 * x**2 * exp_polar(-I*pi)/2) + + meijerg(((1/2,), ()), ((5, 0, 1/2), ()), + 5*x**2 * exp_polar(I*pi)/2)) / (48 * pi), (x, 1e-6, 1e-2)).save(tmp_file()) + finally: + TmpFileManager.cleanup() + # Tests for exception handling in experimental_lambdify def test_experimental_lambify(): @@ -254,6 +275,7 @@ def test_experimental_lambify(): f = lambdify([x], x + 1) assert f(1) == 2 + @disable_print def test_append_issue_7140(): matplotlib = import_module('matplotlib', min_module_version='1.1.0', catch=(RuntimeError,)) diff --git a/sympy/polys/agca/ideals.py b/sympy/polys/agca/ideals.py index cdc884d188..1d3c0cc768 100644 --- a/sympy/polys/agca/ideals.py +++ b/sympy/polys/agca/ideals.py @@ -305,7 +305,8 @@ def gens(self): >>> from sympy import QQ >>> from sympy.abc import x, y - >>> list(QQ.old_poly_ring(x, y).ideal(x, y, x**2 + y).gens) + >>> from sympy.printing import sstr + >>> print(sstr(list(QQ.old_poly_ring(x, y).ideal(x, y, x**2 + y).gens))) [x, y, x**2 + y] """ return (x[0] for x in self._module.gens) @@ -355,8 +356,9 @@ def in_terms_of_generators(self, e): >>> from sympy.abc import x >>> from sympy import QQ + >>> from sympy.printing import sstr >>> I = QQ.old_poly_ring(x).ideal(x**2 + 1, x) - >>> I.in_terms_of_generators(1) + >>> print(sstr(I.in_terms_of_generators(1))) [1, -x] """ return self._module.in_terms_of_generators([e]) diff --git a/sympy/polys/agca/modules.py b/sympy/polys/agca/modules.py index 7532579cf6..9479aa46d8 100644 --- a/sympy/polys/agca/modules.py +++ b/sympy/polys/agca/modules.py @@ -644,6 +644,7 @@ def intersect(self, other, **options): >>> from sympy.abc import x, y >>> from sympy import QQ + >>> from sympy.printing import sstr >>> F = QQ.old_poly_ring(x, y).free_module(2) >>> F.submodule([x, x]).intersect(F.submodule([y, y])) <[x*y, x*y]> @@ -655,7 +656,7 @@ def intersect(self, other, **options): vectors, expressing the generators of ``res`` in terms of the generators of ``self`` (``rela``) and ``other`` (``relb``). - >>> F.submodule([x, x]).intersect(F.submodule([y, y]), relations=True) + >>> print(sstr(F.submodule([x, x]).intersect(F.submodule([y, y]), relations=True))) (<[x*y, x*y]>, [(y,)], [(x,)]) The above result says: the intersection module is generated by the @@ -679,6 +680,7 @@ def module_quotient(self, other, **options): >>> from sympy import QQ >>> from sympy.abc import x, y + >>> from sympy.printing import sstr >>> F = QQ.old_poly_ring(x, y).free_module(2) >>> S = F.submodule([x*y, x*y]) >>> T = F.submodule([x, x]) @@ -693,7 +695,7 @@ def module_quotient(self, other, **options): the ideal, multiplied by the generator of ``other`` in terms of generators of ``self``. - >>> S.module_quotient(T, relations=True) + >>> print(sstr(S.module_quotient(T, relations=True))) (, [[1]]) This means that the quotient ideal is generated by the single element @@ -828,9 +830,10 @@ def in_terms_of_generators(self, e): >>> from sympy.abc import x >>> from sympy import QQ + >>> from sympy.printing import sstr >>> F = QQ.old_poly_ring(x).free_module(2) >>> M = F.submodule([1, 0], [1, 1]) - >>> M.in_terms_of_generators([x, x**2]) + >>> print(sstr(M.in_terms_of_generators([x, x**2]))) [-x**2 + x, x**2] """ try: diff --git a/sympy/polys/densearith.py b/sympy/polys/densearith.py index 1088c31dda..cfde874e0c 100644 --- a/sympy/polys/densearith.py +++ b/sympy/polys/densearith.py @@ -1709,7 +1709,7 @@ def dup_max_norm(f, K): >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) - >>> R.dup_max_norm(-x**2 + 2*x - 3) + >>> print(R.dup_max_norm(-x**2 + 2*x - 3)) 3 """ @@ -1729,7 +1729,7 @@ def dmp_max_norm(f, u, K): >>> from sympy.polys import ring, ZZ >>> R, x,y = ring("x,y", ZZ) - >>> R.dmp_max_norm(2*x*y - x - 3) + >>> print(R.dmp_max_norm(2*x*y - x - 3)) 3 """ @@ -1751,7 +1751,7 @@ def dup_l1_norm(f, K): >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) - >>> R.dup_l1_norm(2*x**3 - 3*x**2 + 1) + >>> print(R.dup_l1_norm(2*x**3 - 3*x**2 + 1)) 6 """ @@ -1771,7 +1771,7 @@ def dmp_l1_norm(f, u, K): >>> from sympy.polys import ring, ZZ >>> R, x,y = ring("x,y", ZZ) - >>> R.dmp_l1_norm(2*x*y - x - 3) + >>> print(R.dmp_l1_norm(2*x*y - x - 3)) 6 """ diff --git a/sympy/polys/densebasic.py b/sympy/polys/densebasic.py index bf396d25e7..7accac3121 100644 --- a/sympy/polys/densebasic.py +++ b/sympy/polys/densebasic.py @@ -21,9 +21,9 @@ def poly_LC(f, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import poly_LC - >>> poly_LC([], ZZ) + >>> print(poly_LC([], ZZ)) 0 - >>> poly_LC([ZZ(1), ZZ(2), ZZ(3)], ZZ) + >>> print(poly_LC([ZZ(1), ZZ(2), ZZ(3)], ZZ)) 1 """ @@ -43,9 +43,9 @@ def poly_TC(f, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import poly_TC - >>> poly_TC([], ZZ) + >>> print(poly_TC([], ZZ)) 0 - >>> poly_TC([ZZ(1), ZZ(2), ZZ(3)], ZZ) + >>> print(poly_TC([ZZ(1), ZZ(2), ZZ(3)], ZZ)) 3 """ @@ -70,7 +70,7 @@ def dmp_ground_LC(f, u, K): >>> f = ZZ.map([[[1], [2, 3]]]) - >>> dmp_ground_LC(f, 2, ZZ) + >>> print(dmp_ground_LC(f, 2, ZZ)) 1 """ @@ -93,7 +93,7 @@ def dmp_ground_TC(f, u, K): >>> f = ZZ.map([[[1], [2, 3]]]) - >>> dmp_ground_TC(f, 2, ZZ) + >>> print(dmp_ground_TC(f, 2, ZZ)) 3 """ @@ -113,10 +113,11 @@ def dmp_true_LT(f, u, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_true_LT + >>> from sympy.printing import pprint >>> f = ZZ.map([[4], [2, 0], [3, 0, 0]]) - >>> dmp_true_LT(f, 1, ZZ) + >>> pprint(dmp_true_LT(f, 1, ZZ)) ((2, 0), 4) """ @@ -378,10 +379,11 @@ def dup_reverse(f): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dup_reverse + >>> from sympy.printing import pprint >>> f = ZZ.map([1, 2, 3, 0]) - >>> dup_reverse(f) + >>> pprint(dup_reverse(f)) [3, 2, 1] """ @@ -416,10 +418,11 @@ def dmp_copy(f, u): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_copy + >>> from sympy.printing import pprint >>> f = ZZ.map([[1], [1, 2]]) - >>> dmp_copy(f, 1) + >>> pprint(dmp_copy(f, 1)) [[1], [1, 2]] """ @@ -463,10 +466,11 @@ def dmp_to_tuple(f, u): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_to_tuple + >>> from sympy.printing import pprint >>> f = ZZ.map([[1], [1, 2]]) - >>> dmp_to_tuple(f, 1) + >>> pprint(dmp_to_tuple(f, 1)) ((1,), (1, 2)) """ @@ -486,8 +490,9 @@ def dup_normal(f, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dup_normal + >>> from sympy.printing import pprint - >>> dup_normal([0, 1.5, 2, 3], ZZ) + >>> pprint(dup_normal([0, 1.5, 2, 3], ZZ)) [1, 2, 3] """ @@ -503,8 +508,9 @@ def dmp_normal(f, u, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_normal + >>> from sympy.printing import pprint - >>> dmp_normal([[], [0, 1.5, 2]], 1, ZZ) + >>> pprint(dmp_normal([[], [0, 1.5, 2]], 1, ZZ)) [[1, 2]] """ @@ -526,12 +532,13 @@ def dup_convert(f, K0, K1): >>> from sympy.polys.rings import ring >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dup_convert + >>> from sympy.printing import pprint >>> R, x = ring("x", ZZ) - >>> dup_convert([R(1), R(2)], R.to_domain(), ZZ) + >>> pprint(dup_convert([R(1), R(2)], R.to_domain(), ZZ)) [1, 2] - >>> dup_convert([ZZ(1), ZZ(2)], ZZ, R.to_domain()) + >>> pprint(dup_convert([ZZ(1), ZZ(2)], ZZ, R.to_domain())) [1, 2] """ @@ -551,12 +558,13 @@ def dmp_convert(f, u, K0, K1): >>> from sympy.polys.rings import ring >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_convert + >>> from sympy.printing import pprint >>> R, x = ring("x", ZZ) - >>> dmp_convert([[R(1)], [R(2)]], 1, R.to_domain(), ZZ) + >>> pprint(dmp_convert([[R(1)], [R(2)]], 1, R.to_domain(), ZZ)) [[1], [2]] - >>> dmp_convert([[ZZ(1)], [ZZ(2)]], 1, ZZ, R.to_domain()) + >>> pprint(dmp_convert([[ZZ(1)], [ZZ(2)]], 1, ZZ, R.to_domain())) [[1], [2]] """ @@ -623,9 +631,9 @@ def dup_nth(f, n, K): >>> f = ZZ.map([1, 2, 3]) - >>> dup_nth(f, 0, ZZ) + >>> print(dup_nth(f, 0, ZZ)) 3 - >>> dup_nth(f, 4, ZZ) + >>> print(dup_nth(f, 4, ZZ)) 0 """ @@ -646,10 +654,11 @@ def dmp_nth(f, n, u, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_nth + >>> from sympy.printing import pprint >>> f = ZZ.map([[1], [2], [3]]) - >>> dmp_nth(f, 0, 1, ZZ) + >>> pprint(dmp_nth(f, 0, 1, ZZ)) [3] >>> dmp_nth(f, 4, 1, ZZ) [] @@ -675,7 +684,7 @@ def dmp_ground_nth(f, N, u, K): >>> f = ZZ.map([[1], [2, 3]]) - >>> dmp_ground_nth(f, (0, 1), 1, ZZ) + >>> print(dmp_ground_nth(f, (0, 1), 1, ZZ)) 2 """ @@ -767,8 +776,9 @@ def dmp_one(u, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_one + >>> from sympy.printing import pprint - >>> dmp_one(2, ZZ) + >>> pprint(dmp_one(2, ZZ)) [[[1]]] """ @@ -838,10 +848,11 @@ def dmp_zeros(n, u, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_zeros + >>> from sympy.printing import pprint >>> dmp_zeros(3, 2, ZZ) [[[[]]], [[[]]], [[[]]]] - >>> dmp_zeros(3, -1, ZZ) + >>> pprint(dmp_zeros(3, -1, ZZ)) [0, 0, 0] """ @@ -863,10 +874,11 @@ def dmp_grounds(c, n, u): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_grounds + >>> from sympy.printing import pprint - >>> dmp_grounds(ZZ(4), 3, 2) + >>> pprint(dmp_grounds(ZZ(4), 3, 2)) [[[[4]]], [[[4]]], [[[4]]]] - >>> dmp_grounds(ZZ(4), 3, -1) + >>> pprint(dmp_grounds(ZZ(4), 3, -1)) [4, 4, 4] """ @@ -926,8 +938,9 @@ def dup_from_dict(f, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dup_from_dict + >>> from sympy.printing import pprint - >>> dup_from_dict({(0,): ZZ(7), (2,): ZZ(5), (4,): ZZ(1)}, ZZ) + >>> pprint(dup_from_dict({(0,): ZZ(7), (2,): ZZ(5), (4,): ZZ(1)}, ZZ)) [1, 0, 5, 0, 7] >>> dup_from_dict({}, ZZ) [] @@ -959,8 +972,9 @@ def dup_from_raw_dict(f, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dup_from_raw_dict + >>> from sympy.printing import pprint - >>> dup_from_raw_dict({0: ZZ(7), 2: ZZ(5), 4: ZZ(1)}, ZZ) + >>> pprint(dup_from_raw_dict({0: ZZ(7), 2: ZZ(5), 4: ZZ(1)}, ZZ)) [1, 0, 5, 0, 7] """ @@ -984,8 +998,9 @@ def dmp_from_dict(f, u, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_from_dict + >>> from sympy.printing import pprint - >>> dmp_from_dict({(0, 0): ZZ(3), (0, 1): ZZ(2), (2, 1): ZZ(1)}, 1, ZZ) + >>> pprint(dmp_from_dict({(0, 0): ZZ(3), (0, 1): ZZ(2), (2, 1): ZZ(1)}, 1, ZZ)) [[1, 0], [], [2, 3]] >>> dmp_from_dict({}, 0, ZZ) [] @@ -1028,8 +1043,8 @@ def dup_to_dict(f, K=None, zero=False): >>> from sympy.polys.densebasic import dup_to_dict - >>> dup_to_dict([1, 0, 5, 0, 7]) - {(0,): 7, (2,): 5, (4,): 1} + >>> dup_to_dict([1, 0, 5, 0, 7]) == {(0,): 7, (2,): 5, (4,): 1} + True >>> dup_to_dict([]) {} @@ -1080,8 +1095,8 @@ def dmp_to_dict(f, u, K=None, zero=False): >>> from sympy.polys.densebasic import dmp_to_dict - >>> dmp_to_dict([[1, 0], [], [2, 3]], 1) - {(0, 0): 3, (0, 1): 2, (2, 1): 1} + >>> dmp_to_dict([[1, 0], [], [2, 3]], 1) == {(0, 0): 3, (0, 1): 2, (2, 1): 1} + True >>> dmp_to_dict([], 0) {} @@ -1115,14 +1130,15 @@ def dmp_swap(f, i, j, u, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_swap + >>> from sympy.printing import pprint >>> f = ZZ.map([[[2], [1, 0]], []]) - >>> dmp_swap(f, 0, 1, 2, ZZ) + >>> pprint(dmp_swap(f, 0, 1, 2, ZZ)) [[[2], []], [[1, 0], []]] - >>> dmp_swap(f, 1, 2, 2, ZZ) + >>> pprint(dmp_swap(f, 1, 2, 2, ZZ)) [[[1], [2, 0]], [[]]] - >>> dmp_swap(f, 0, 2, 2, ZZ) + >>> pprint(dmp_swap(f, 0, 2, 2, ZZ)) [[[1, 0]], [[2, 0], []]] """ @@ -1150,12 +1166,13 @@ def dmp_permute(f, P, u, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_permute + >>> from sympy.printing import pprint >>> f = ZZ.map([[[2], [1, 0]], []]) - >>> dmp_permute(f, [1, 0, 2], 2, ZZ) + >>> pprint(dmp_permute(f, [1, 0, 2], 2, ZZ)) [[[2], []], [[1, 0], []]] - >>> dmp_permute(f, [1, 2, 0], 2, ZZ) + >>> pprint(dmp_permute(f, [1, 2, 0], 2, ZZ)) [[[1], []], [[2, 0], []]] """ @@ -1181,8 +1198,9 @@ def dmp_nest(f, l, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_nest + >>> from sympy.printing import pprint - >>> dmp_nest([[ZZ(1)]], 2, ZZ) + >>> pprint(dmp_nest([[ZZ(1)]], 2, ZZ)) [[[[1]]]] """ @@ -1204,10 +1222,11 @@ def dmp_raise(f, l, u, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_raise + >>> from sympy.printing import pprint >>> f = ZZ.map([[], [1, 2]]) - >>> dmp_raise(f, 2, 1, ZZ) + >>> pprint(dmp_raise(f, 2, 1, ZZ)) [[[[]]], [[[1]], [[2]]]] """ @@ -1236,10 +1255,11 @@ def dup_deflate(f, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dup_deflate + >>> from sympy.printing import pprint >>> f = ZZ.map([1, 0, 0, 1, 0, 0, 1]) - >>> dup_deflate(f, ZZ) + >>> pprint(dup_deflate(f, ZZ)) (3, [1, 1, 1]) """ @@ -1269,10 +1289,11 @@ def dmp_deflate(f, u, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_deflate + >>> from sympy.printing import pprint >>> f = ZZ.map([[1, 0, 0, 2], [], [3, 0, 0, 4]]) - >>> dmp_deflate(f, 1, ZZ) + >>> pprint(dmp_deflate(f, 1, ZZ)) ((2, 3), [[1, 2], [3, 4]]) """ @@ -1313,11 +1334,12 @@ def dup_multi_deflate(polys, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dup_multi_deflate + >>> from sympy.printing import pprint >>> f = ZZ.map([1, 0, 2, 0, 3]) >>> g = ZZ.map([4, 0, 0]) - >>> dup_multi_deflate((f, g), ZZ) + >>> pprint(dup_multi_deflate((f, g), ZZ)) (2, ([1, 2, 3], [4, 0])) """ @@ -1352,11 +1374,12 @@ def dmp_multi_deflate(polys, u, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_multi_deflate + >>> from sympy.printing import pprint >>> f = ZZ.map([[1, 0, 0, 2], [], [3, 0, 0, 4]]) >>> g = ZZ.map([[1, 0, 2], [], [3, 0, 4]]) - >>> dmp_multi_deflate((f, g), 1, ZZ) + >>> pprint(dmp_multi_deflate((f, g), 1, ZZ)) ((2, 1), ([[1, 0, 0, 2], [3, 0, 0, 4]], [[1, 0, 2], [3, 0, 4]])) """ @@ -1408,10 +1431,11 @@ def dup_inflate(f, m, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dup_inflate + >>> from sympy.printing import pprint >>> f = ZZ.map([1, 1, 1]) - >>> dup_inflate(f, 3, ZZ) + >>> pprint(dup_inflate(f, 3, ZZ)) [1, 0, 0, 1, 0, 0, 1] """ @@ -1460,10 +1484,11 @@ def dmp_inflate(f, M, u, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_inflate + >>> from sympy.printing import pprint >>> f = ZZ.map([[1, 2], [3, 4]]) - >>> dmp_inflate(f, (2, 3), 1, ZZ) + >>> pprint(dmp_inflate(f, (2, 3), 1, ZZ)) [[1, 0, 0, 2], [], [3, 0, 0, 4]] """ @@ -1487,10 +1512,11 @@ def dmp_exclude(f, u, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_exclude + >>> from sympy.printing import pprint >>> f = ZZ.map([[[1]], [[1], [2]]]) - >>> dmp_exclude(f, 2, ZZ) + >>> pprint(dmp_exclude(f, 2, ZZ)) ([2], [[1], [1, 2]], 1) """ @@ -1533,10 +1559,11 @@ def dmp_include(f, J, u, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_include + >>> from sympy.printing import pprint >>> f = ZZ.map([[1], [1, 2]]) - >>> dmp_include(f, [2], 1, ZZ) + >>> pprint(dmp_include(f, [2], 1, ZZ)) [[[1]], [[1], [2]]] """ @@ -1568,12 +1595,13 @@ def dmp_inject(f, u, K, front=False): >>> from sympy.polys.rings import ring >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_inject + >>> from sympy.printing import pprint >>> R, x,y = ring("x,y", ZZ) - >>> dmp_inject([R(1), x + 2], 0, R.to_domain()) + >>> pprint(dmp_inject([R(1), x + 2], 0, R.to_domain())) ([[[1]], [[1], [2]]], 2) - >>> dmp_inject([R(1), x + 2], 0, R.to_domain(), front=True) + >>> pprint(dmp_inject([R(1), x + 2], 0, R.to_domain(), front=True)) ([[[1]], [[1, 2]]], 2) """ @@ -1640,10 +1668,11 @@ def dup_terms_gcd(f, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dup_terms_gcd + >>> from sympy.printing import pprint >>> f = ZZ.map([1, 0, 1, 0, 0]) - >>> dup_terms_gcd(f, ZZ) + >>> pprint(dup_terms_gcd(f, ZZ)) (2, [1, 0, 1]) """ @@ -1670,10 +1699,11 @@ def dmp_terms_gcd(f, u, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_terms_gcd + >>> from sympy.printing import pprint >>> f = ZZ.map([[1, 0], [1, 0, 0], [], []]) - >>> dmp_terms_gcd(f, 1, ZZ) + >>> pprint(dmp_terms_gcd(f, 1, ZZ)) ((2, 1), [[1], [1, 0]]) """ @@ -1722,12 +1752,13 @@ def dmp_list_terms(f, u, K, order=None): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.densebasic import dmp_list_terms + >>> from sympy.printing import pprint >>> f = ZZ.map([[1, 1], [2, 3]]) - >>> dmp_list_terms(f, 1, ZZ) + >>> pprint(dmp_list_terms(f, 1, ZZ)) [((1, 1), 1), ((1, 0), 1), ((0, 1), 2), ((0, 0), 3)] - >>> dmp_list_terms(f, 1, ZZ, order='grevlex') + >>> pprint(dmp_list_terms(f, 1, ZZ, order='grevlex')) [((1, 1), 1), ((1, 0), 1), ((0, 1), 2), ((0, 0), 3)] """ diff --git a/sympy/polys/densetools.py b/sympy/polys/densetools.py index c837a76465..e78a665049 100644 --- a/sympy/polys/densetools.py +++ b/sympy/polys/densetools.py @@ -274,7 +274,7 @@ def dup_eval(f, a, K): >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) - >>> R.dup_eval(x**2 + 2*x + 3, 2) + >>> print(R.dup_eval(x**2 + 2*x + 3, 2)) 11 """ @@ -380,7 +380,7 @@ def dmp_eval_tail(f, A, u, K): >>> R.dmp_eval_tail(f, [2]) 7*x + 4 - >>> R.dmp_eval_tail(f, [2, 2]) + >>> print(R.dmp_eval_tail(f, [2, 2])) 18 """ @@ -585,13 +585,13 @@ def dup_content(f, K): >>> R, x = ring("x", ZZ) >>> f = 6*x**2 + 8*x + 12 - >>> R.dup_content(f) + >>> print(R.dup_content(f)) 2 >>> R, x = ring("x", QQ) >>> f = 6*x**2 + 8*x + 12 - >>> R.dup_content(f) + >>> print(R.dup_content(f)) 2 """ @@ -627,13 +627,13 @@ def dmp_ground_content(f, u, K): >>> R, x,y = ring("x,y", ZZ) >>> f = 2*x*y + 6*x + 4*y + 12 - >>> R.dmp_ground_content(f) + >>> print(R.dmp_ground_content(f)) 2 >>> R, x,y = ring("x,y", QQ) >>> f = 2*x*y + 6*x + 4*y + 12 - >>> R.dmp_ground_content(f) + >>> print(R.dmp_ground_content(f)) 2 """ @@ -668,17 +668,18 @@ def dup_primitive(f, K): ======== >>> from sympy.polys import ring, ZZ, QQ + >>> from sympy.printing import pprint >>> R, x = ring("x", ZZ) >>> f = 6*x**2 + 8*x + 12 - >>> R.dup_primitive(f) + >>> pprint(R.dup_primitive(f)) (2, 3*x**2 + 4*x + 6) >>> R, x = ring("x", QQ) >>> f = 6*x**2 + 8*x + 12 - >>> R.dup_primitive(f) + >>> pprint(R.dup_primitive(f)) (2, 3*x**2 + 4*x + 6) """ @@ -701,17 +702,18 @@ def dmp_ground_primitive(f, u, K): ======== >>> from sympy.polys import ring, ZZ, QQ + >>> from sympy.printing import pprint >>> R, x,y = ring("x,y", ZZ) >>> f = 2*x*y + 6*x + 4*y + 12 - >>> R.dmp_ground_primitive(f) + >>> pprint(R.dmp_ground_primitive(f)) (2, x*y + 3*x + 2*y + 6) >>> R, x,y = ring("x,y", QQ) >>> f = 2*x*y + 6*x + 4*y + 12 - >>> R.dmp_ground_primitive(f) + >>> pprint(R.dmp_ground_primitive(f)) (2, x*y + 3*x + 2*y + 6) """ @@ -737,9 +739,10 @@ def dup_extract(f, g, K): ======== >>> from sympy.polys import ring, ZZ + >>> from sympy.printing import pprint >>> R, x = ring("x", ZZ) - >>> R.dup_extract(6*x**2 + 12*x + 18, 4*x**2 + 8*x + 12) + >>> pprint(R.dup_extract(6*x**2 + 12*x + 18, 4*x**2 + 8*x + 12)) (2, 3*x**2 + 6*x + 9, 2*x**2 + 4*x + 6) """ @@ -763,9 +766,10 @@ def dmp_ground_extract(f, g, u, K): ======== >>> from sympy.polys import ring, ZZ + >>> from sympy.printing import pprint >>> R, x,y = ring("x,y", ZZ) - >>> R.dmp_ground_extract(6*x*y + 12*x + 18, 4*x*y + 8*x + 12) + >>> pprint(R.dmp_ground_extract(6*x*y + 12*x + 18, 4*x*y + 8*x + 12)) (2, 3*x*y + 6*x + 9, 2*x*y + 4*x + 6) """ @@ -1174,13 +1178,14 @@ def dup_clear_denoms(f, K0, K1=None, convert=False): ======== >>> from sympy.polys import ring, QQ + >>> from sympy.printing import pprint >>> R, x = ring("x", QQ) >>> f = QQ(1,2)*x + QQ(1,3) - >>> R.dup_clear_denoms(f, convert=False) + >>> pprint(R.dup_clear_denoms(f, convert=False)) (6, 3*x + 2) - >>> R.dup_clear_denoms(f, convert=True) + >>> pprint(R.dup_clear_denoms(f, convert=True)) (6, 3*x + 2) """ @@ -1228,13 +1233,14 @@ def dmp_clear_denoms(f, u, K0, K1=None, convert=False): ======== >>> from sympy.polys import ring, QQ + >>> from sympy.printing import pprint >>> R, x,y = ring("x,y", QQ) >>> f = QQ(1,2)*x + QQ(1,3)*y + 1 - >>> R.dmp_clear_denoms(f, convert=False) + >>> pprint(R.dmp_clear_denoms(f, convert=False)) (6, 3*x + 2*y + 6) - >>> R.dmp_clear_denoms(f, convert=True) + >>> pprint(R.dmp_clear_denoms(f, convert=True)) (6, 3*x + 2*y + 6) """ diff --git a/sympy/polys/distributedmodules.py b/sympy/polys/distributedmodules.py index 5287e2f1da..b658ae5b19 100644 --- a/sympy/polys/distributedmodules.py +++ b/sympy/polys/distributedmodules.py @@ -167,8 +167,9 @@ def sdm_from_dict(d, O): >>> from sympy.polys.distributedmodules import sdm_from_dict >>> from sympy.polys import QQ, lex + >>> from sympy.printing import pprint >>> dic = {(1, 1, 0): QQ(1), (1, 0, 0): QQ(2), (0, 1, 0): QQ(0)} - >>> sdm_from_dict(dic, lex) + >>> pprint(sdm_from_dict(dic, lex)) [((1, 1, 0), 1), ((1, 0, 0), 2)] """ return sdm_strip(sdm_sort(list(d.items()), O)) @@ -200,7 +201,8 @@ def sdm_add(f, g, O, K): >>> from sympy.polys.distributedmodules import sdm_add >>> from sympy.polys import lex, QQ - >>> sdm_add([((1, 1, 1), QQ(1))], [((2, 0, 0), QQ(1))], lex, QQ) + >>> from sympy.printing import pprint + >>> pprint(sdm_add([((1, 1, 1), QQ(1))], [((2, 0, 0), QQ(1))], lex, QQ)) [((2, 0, 0), 1), ((1, 1, 1), 1)] `(xy f_1) + (-xy f_1)` = 0` @@ -210,12 +212,12 @@ def sdm_add(f, g, O, K): `(f_1) + (2f_1) = 3f_1` - >>> sdm_add([((1, 0, 0), QQ(1))], [((1, 0, 0), QQ(2))], lex, QQ) + >>> pprint(sdm_add([((1, 0, 0), QQ(1))], [((1, 0, 0), QQ(2))], lex, QQ)) [((1, 0, 0), 3)] `(yf_1) + (xf_1) = xf_1 + yf_1` - >>> sdm_add([((1, 0, 1), QQ(1))], [((1, 1, 0), QQ(1))], lex, QQ) + >>> pprint(sdm_add([((1, 0, 1), QQ(1))], [((1, 1, 0), QQ(1))], lex, QQ)) [((1, 1, 0), 1), ((1, 0, 1), 1)] """ h = dict(f) @@ -263,8 +265,9 @@ def sdm_LT(f): >>> from sympy.polys.distributedmodules import sdm_LT, sdm_from_dict >>> from sympy.polys import QQ, lex + >>> from sympy.printing import pprint >>> dic = {(1, 2, 3): QQ(1), (4, 0, 0): QQ(2), (4, 0, 1): QQ(3)} - >>> sdm_LT(sdm_from_dict(dic, lex)) + >>> pprint(sdm_LT(sdm_from_dict(dic, lex))) ((4, 0, 1), 3) """ return f[0] @@ -284,6 +287,7 @@ def sdm_mul_term(f, term, O, K): >>> from sympy.polys.distributedmodules import sdm_mul_term >>> from sympy.polys import lex, QQ + >>> from sympy.printing import pprint >>> sdm_mul_term([((1, 0, 0), QQ(1))], ((0, 0), QQ(0)), lex, QQ) [] @@ -294,13 +298,13 @@ def sdm_mul_term(f, term, O, K): `(x) (f_1) = xf_1` - >>> sdm_mul_term([((1, 0, 0), QQ(1))], ((1, 0), QQ(1)), lex, QQ) + >>> pprint(sdm_mul_term([((1, 0, 0), QQ(1))], ((1, 0), QQ(1)), lex, QQ)) [((1, 1, 0), 1)] `(2xy) (3x f_1 + 4y f_2) = 8xy^2 f_2 + 6x^2y f_1` >>> f = [((2, 0, 1), QQ(4)), ((1, 1, 0), QQ(3))] - >>> sdm_mul_term(f, ((1, 1), QQ(2)), lex, QQ) + >>> pprint(sdm_mul_term(f, ((1, 1), QQ(2)), lex, QQ)) [((2, 1, 2), 8), ((1, 2, 1), 6)] """ X, c = term @@ -352,7 +356,8 @@ def sdm_from_vector(vec, O, K, **opts): >>> from sympy.polys.distributedmodules import sdm_from_vector >>> from sympy.abc import x, y, z >>> from sympy.polys import QQ, lex - >>> sdm_from_vector([x**2+y**2, 2*z], lex, QQ) + >>> from sympy.printing import pprint + >>> pprint(sdm_from_vector([x**2+y**2, 2*z], lex, QQ)) [((1, 0, 0, 1), 2), ((0, 2, 0, 0), 1), ((0, 0, 2, 0), 1)] """ dics, gens = parallel_dict_from_expr(sympify(vec), **opts) @@ -420,12 +425,13 @@ def sdm_spoly(f, g, O, K, phantom=None): >>> from sympy.polys.distributedmodules import sdm_spoly >>> from sympy.polys import QQ, lex + >>> from sympy.printing import pprint >>> f = [((2, 1, 1), QQ(1)), ((1, 0, 1), QQ(1))] >>> g = [((2, 3, 0), QQ(1))] >>> h = [((1, 2, 3), QQ(1))] >>> sdm_spoly(f, h, lex, QQ) [] - >>> sdm_spoly(f, g, lex, QQ) + >>> pprint(sdm_spoly(f, g, lex, QQ)) [((1, 2, 1), 1)] """ if not f or not g: diff --git a/sympy/polys/domains/field.py b/sympy/polys/domains/field.py index 4a5462367f..b987749553 100644 --- a/sympy/polys/domains/field.py +++ b/sympy/polys/domains/field.py @@ -47,11 +47,11 @@ def gcd(self, a, b): >>> from sympy import S, gcd, primitive >>> from sympy.abc import x - >>> QQ.gcd(QQ(2, 3), QQ(4, 9)) + >>> print(QQ.gcd(QQ(2, 3), QQ(4, 9))) 2/9 - >>> gcd(S(2)/3, S(4)/9) + >>> print(gcd(S(2)/3, S(4)/9)) 2/9 - >>> primitive(2*x/3 + S(4)/9) + >>> print(primitive(2*x/3 + S(4)/9)) (2/9, 3*x + 2) """ @@ -72,9 +72,9 @@ def lcm(self, a, b): >>> from sympy.polys.domains import QQ >>> from sympy import S, lcm - >>> QQ.lcm(QQ(2, 3), QQ(4, 9)) + >>> print(QQ.lcm(QQ(2, 3), QQ(4, 9))) 4/3 - >>> lcm(S(2)/3, S(4)/9) + >>> print(lcm(S(2)/3, S(4)/9)) 4/3 """ diff --git a/sympy/polys/domains/old_fractionfield.py b/sympy/polys/domains/old_fractionfield.py index 59077521fd..6a217fb8e8 100644 --- a/sympy/polys/domains/old_fractionfield.py +++ b/sympy/polys/domains/old_fractionfield.py @@ -114,15 +114,15 @@ def from_FractionField(K1, a, K0): >>> from sympy.polys.polyclasses import DMF >>> from sympy.polys.domains import ZZ, QQ >>> from sympy.abc import x + >>> from sympy.printing import sstr >>> f = DMF(([ZZ(1), ZZ(2)], [ZZ(1), ZZ(1)]), ZZ) >>> QQx = QQ.old_frac_field(x) >>> ZZx = ZZ.old_frac_field(x) - >>> QQx.from_FractionField(f, ZZx) + >>> print(sstr(QQx.from_FractionField(f, ZZx))) (x + 2)/(x + 1) - """ if K1.gens == K0.gens: if K1.dom == K0.dom: diff --git a/sympy/polys/domains/old_polynomialring.py b/sympy/polys/domains/old_polynomialring.py index b45983cd05..7044ff36ad 100644 --- a/sympy/polys/domains/old_polynomialring.py +++ b/sympy/polys/domains/old_polynomialring.py @@ -171,9 +171,10 @@ def _sdm_to_vector(self, s, n): >>> from sympy import QQ, ilex >>> from sympy.abc import x, y + >>> from sympy.printing import sstr >>> R = QQ.old_poly_ring(x, y, order=ilex) >>> L = [((1, 1, 1), QQ(1)), ((0, 1, 0), QQ(1)), ((0, 0, 1), QQ(2))] - >>> R._sdm_to_vector(L, 2) + >>> print(sstr(R._sdm_to_vector(L, 2))) [x + 2*y, x*y] """ dics = self._sdm_to_dics(s, n) @@ -270,10 +271,11 @@ def _vector_to_sdm(self, v, order): """ >>> from sympy import lex, QQ >>> from sympy.abc import x, y + >>> from sympy.printing import pprint >>> R = QQ.old_poly_ring(x, y) >>> f = R.convert(x + 2*y) >>> g = R.convert(x * y) - >>> R._vector_to_sdm([f, g], lex) + >>> pprint(R._vector_to_sdm([f, g], lex)) [((1, 1, 1), 1), ((0, 1, 0), 1), ((0, 0, 1), 2)] """ return _vector_to_sdm_helper(v, order) @@ -335,10 +337,11 @@ def _vector_to_sdm(self, v, order): >>> from sympy import ilex, QQ >>> from sympy.abc import x, y + >>> from sympy.printing import pprint >>> R = QQ.old_poly_ring(x, y, order=ilex) >>> f = R.convert((x + 2*y) / (1 + x)) >>> g = R.convert(x * y) - >>> R._vector_to_sdm([f, g], ilex) + >>> pprint(R._vector_to_sdm([f, g], ilex)) [((0, 0, 1), 2), ((0, 1, 0), 1), ((1, 1, 1), 1), ((1, 2, 1), 1)] """ diff --git a/sympy/polys/euclidtools.py b/sympy/polys/euclidtools.py index 015c9c4e74..7f3ae19593 100644 --- a/sympy/polys/euclidtools.py +++ b/sympy/polys/euclidtools.py @@ -329,9 +329,10 @@ def dup_inner_subresultants(f, g, K): ======== >>> from sympy.polys import ring, ZZ + >>> from sympy.printing import pprint >>> R, x = ring("x", ZZ) - >>> R.dup_inner_subresultants(x**2 + 1, x**2 - 1) + >>> pprint(R.dup_inner_subresultants(x**2 + 1, x**2 - 1)) ([x**2 + 1, x**2 - 1, -2], [1, 1, 4]) References @@ -418,9 +419,10 @@ def dup_prs_resultant(f, g, K): ======== >>> from sympy.polys import ring, ZZ + >>> from sympy.printing import pprint >>> R, x = ring("x", ZZ) - >>> R.dup_prs_resultant(x**2 + 1, x**2 - 1) + >>> pprint(R.dup_prs_resultant(x**2 + 1, x**2 - 1)) (4, [x**2 + 1, x**2 - 1, -2]) """ @@ -445,7 +447,7 @@ def dup_resultant(f, g, K, includePRS=False): >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) - >>> R.dup_resultant(x**2 + 1, x**2 - 1) + >>> print(R.dup_resultant(x**2 + 1, x**2 - 1)) 4 """ @@ -816,7 +818,7 @@ def dup_discriminant(f, K): >>> from sympy.polys import ring, ZZ >>> R, x = ring("x", ZZ) - >>> R.dup_discriminant(x**2 + 2*x + 3) + >>> print(R.dup_discriminant(x**2 + 2*x + 3)) -8 """ diff --git a/sympy/polys/factortools.py b/sympy/polys/factortools.py index d796c4fcd4..587d59db28 100644 --- a/sympy/polys/factortools.py +++ b/sympy/polys/factortools.py @@ -571,9 +571,10 @@ def dup_zz_factor(f, K): Consider polynomial `f = 2*x**4 - 2`:: >>> from sympy.polys import ring, ZZ + >>> from sympy.printing import pprint >>> R, x = ring("x", ZZ) - >>> R.dup_zz_factor(2*x**4 - 2) + >>> pprint(R.dup_zz_factor(2*x**4 - 2)) (2, [(x - 1, 1), (x + 1, 1), (x**2 + 1, 1)]) In result we got the following factorization:: @@ -1053,9 +1054,10 @@ def dmp_zz_factor(f, u, K): Consider polynomial `f = 2*(x**2 - y**2)`:: >>> from sympy.polys import ring, ZZ + >>> from sympy.printing import pprint >>> R, x,y = ring("x,y", ZZ) - >>> R.dmp_zz_factor(2*x**2 - 2*y**2) + >>> pprint(R.dmp_zz_factor(2*x**2 - 2*y**2)) (2, [(x - y, 1), (x + y, 1)]) In result we got the following factorization:: diff --git a/sympy/polys/galoistools.py b/sympy/polys/galoistools.py index 46a7599fbd..16f39c655d 100644 --- a/sympy/polys/galoistools.py +++ b/sympy/polys/galoistools.py @@ -27,13 +27,14 @@ def gf_crt(U, M, K=None): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_crt >>> from sympy.ntheory.modular import solve_congruence + >>> from sympy.printing import pprint - >>> gf_crt([49, 76, 65], [99, 97, 95], ZZ) + >>> pprint(gf_crt([49, 76, 65], [99, 97, 95], ZZ)) 639985 This is the correct result because:: - >>> [639985 % m for m in [99, 97, 95]] + >>> pprint([639985 % m for m in [99, 97, 95]]) [49, 76, 65] Note: this is a low-level routine with no error checking. @@ -65,8 +66,9 @@ def gf_crt1(M, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_crt1 + >>> from sympy.printing import pprint - >>> gf_crt1([99, 97, 95], ZZ) + >>> pprint(gf_crt1([99, 97, 95], ZZ)) (912285, [9215, 9405, 9603], [62, 24, 12]) """ @@ -96,7 +98,7 @@ def gf_crt2(U, M, p, E, S, K): >>> E = [9215, 9405, 9603] >>> S = [62, 24, 12] - >>> gf_crt2(U, M, p, E, S, ZZ) + >>> print(gf_crt2(U, M, p, E, S, ZZ)) 639985 """ @@ -240,8 +242,9 @@ def gf_normal(f, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_normal + >>> from sympy.printing import pprint - >>> gf_normal([5, 10, 21, -3], 5, ZZ) + >>> pprint(gf_normal([5, 10, 21, -3], 5, ZZ)) [1, 2] """ @@ -257,8 +260,9 @@ def gf_from_dict(f, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_from_dict + >>> from sympy.printing import pprint - >>> gf_from_dict({10: ZZ(4), 4: ZZ(33), 0: ZZ(-1)}, 5, ZZ) + >>> pprint(gf_from_dict({10: ZZ(4), 4: ZZ(33), 0: ZZ(-1)}, 5, ZZ)) [4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 4] """ @@ -285,11 +289,10 @@ def gf_to_dict(f, p, symmetric=True): >>> from sympy.polys.galoistools import gf_to_dict - >>> gf_to_dict([4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 4], 5) - {0: -1, 4: -2, 10: -1} - >>> gf_to_dict([4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 4], 5, symmetric=False) - {0: 4, 4: 3, 10: 4} - + >>> gf_to_dict([4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 4], 5) == {0: -1, 4: -2, 10: -1} + True + >>> gf_to_dict([4, 0, 0, 0, 0, 0, 3, 0, 0, 0, 4], 5, symmetric=False) == {0: 4, 4: 3, 10: 4} + True """ n, result = gf_degree(f), {} @@ -353,8 +356,9 @@ def gf_neg(f, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_neg + >>> from sympy.printing import pprint - >>> gf_neg([3, 2, 1, 0], 5, ZZ) + >>> pprint(gf_neg([3, 2, 1, 0], 5, ZZ)) [2, 3, 4, 0] """ @@ -370,8 +374,9 @@ def gf_add_ground(f, a, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_add_ground + >>> from sympy.printing import pprint - >>> gf_add_ground([3, 2, 4], 2, 5, ZZ) + >>> pprint(gf_add_ground([3, 2, 4], 2, 5, ZZ)) [3, 2, 1] """ @@ -398,8 +403,9 @@ def gf_sub_ground(f, a, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_sub_ground + >>> from sympy.printing import pprint - >>> gf_sub_ground([3, 2, 4], 2, 5, ZZ) + >>> pprint(gf_sub_ground([3, 2, 4], 2, 5, ZZ)) [3, 2, 2] """ @@ -446,8 +452,9 @@ def gf_quo_ground(f, a, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_quo_ground + >>> from sympy.printing import pprint - >>> gf_quo_ground(ZZ.map([3, 2, 4]), ZZ(2), 5, ZZ) + >>> pprint(gf_quo_ground(ZZ.map([3, 2, 4]), ZZ(2), 5, ZZ)) [4, 1, 2] """ @@ -533,8 +540,9 @@ def gf_mul(f, g, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_mul + >>> from sympy.printing import pprint - >>> gf_mul([3, 2, 4], [2, 2, 2], 5, ZZ) + >>> pprint(gf_mul([3, 2, 4], [2, 2, 2], 5, ZZ)) [1, 0, 3, 2, 3] """ @@ -564,8 +572,9 @@ def gf_sqr(f, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_sqr + >>> from sympy.printing import pprint - >>> gf_sqr([3, 2, 4], 5, ZZ) + >>> pprint(gf_sqr([3, 2, 4], 5, ZZ)) [4, 2, 3, 1, 1] """ @@ -607,7 +616,8 @@ def gf_add_mul(f, g, h, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_add_mul - >>> gf_add_mul([3, 2, 4], [2, 2, 2], [1, 4], 5, ZZ) + >>> from sympy.printing import pprint + >>> pprint(gf_add_mul([3, 2, 4], [2, 2, 2], [1, 4], 5, ZZ)) [2, 3, 2, 2] """ return gf_add(f, gf_mul(g, h, p, K), p, K) @@ -622,8 +632,9 @@ def gf_sub_mul(f, g, h, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_sub_mul + >>> from sympy.printing import pprint - >>> gf_sub_mul([3, 2, 4], [2, 2, 2], [1, 4], 5, ZZ) + >>> pprint(gf_sub_mul([3, 2, 4], [2, 2, 2], [1, 4], 5, ZZ)) [3, 3, 2, 1] """ @@ -639,8 +650,9 @@ def gf_expand(F, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_expand + >>> from sympy.printing import pprint - >>> gf_expand([([3, 2, 4], 1), ([2, 2], 2), ([3, 1], 3)], 5, ZZ) + >>> pprint(gf_expand([([3, 2, 4], 1), ([2, 2], 2), ([3, 1], 3)], 5, ZZ)) [4, 3, 0, 3, 0, 1, 4, 1] """ @@ -670,13 +682,14 @@ def gf_div(f, g, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_div, gf_add_mul + >>> from sympy.printing import pprint - >>> gf_div(ZZ.map([1, 0, 1, 1]), ZZ.map([1, 1, 0]), 2, ZZ) + >>> pprint(gf_div(ZZ.map([1, 0, 1, 1]), ZZ.map([1, 1, 0]), 2, ZZ)) ([1, 1], [1]) As result we obtained quotient ``x + 1`` and remainder ``1``, thus:: - >>> gf_add_mul(ZZ.map([1]), ZZ.map([1, 1]), ZZ.map([1, 1, 0]), 2, ZZ) + >>> pprint(gf_add_mul(ZZ.map([1]), ZZ.map([1, 1]), ZZ.map([1, 1, 0]), 2, ZZ)) [1, 0, 1, 1] References @@ -721,8 +734,9 @@ def gf_rem(f, g, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_rem + >>> from sympy.printing import pprint - >>> gf_rem(ZZ.map([1, 0, 1, 1]), ZZ.map([1, 1, 0]), 2, ZZ) + >>> pprint(gf_rem(ZZ.map([1, 0, 1, 1]), ZZ.map([1, 1, 0]), 2, ZZ)) [1] """ @@ -738,10 +752,11 @@ def gf_quo(f, g, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_quo + >>> from sympy.printing import pprint - >>> gf_quo(ZZ.map([1, 0, 1, 1]), ZZ.map([1, 1, 0]), 2, ZZ) + >>> pprint(gf_quo(ZZ.map([1, 0, 1, 1]), ZZ.map([1, 1, 0]), 2, ZZ)) [1, 1] - >>> gf_quo(ZZ.map([1, 0, 3, 2, 3]), ZZ.map([2, 2, 2]), 5, ZZ) + >>> pprint(gf_quo(ZZ.map([1, 0, 3, 2, 3]), ZZ.map([2, 2, 2]), 5, ZZ)) [3, 2, 4] """ @@ -777,11 +792,12 @@ def gf_exquo(f, g, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_exquo + >>> from sympy.printing import pprint - >>> gf_exquo(ZZ.map([1, 0, 3, 2, 3]), ZZ.map([2, 2, 2]), 5, ZZ) + >>> pprint(gf_exquo(ZZ.map([1, 0, 3, 2, 3]), ZZ.map([2, 2, 2]), 5, ZZ)) [3, 2, 4] - >>> gf_exquo(ZZ.map([1, 0, 1, 1]), ZZ.map([1, 1, 0]), 2, ZZ) + >>> pprint(gf_exquo(ZZ.map([1, 0, 1, 1]), ZZ.map([1, 1, 0]), 2, ZZ)) Traceback (most recent call last): ... ExactQuotientFailed: [1, 1, 0] does not divide [1, 0, 1, 1] @@ -804,8 +820,9 @@ def gf_lshift(f, n, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_lshift + >>> from sympy.printing import pprint - >>> gf_lshift([3, 2, 4], 4, ZZ) + >>> pprint(gf_lshift([3, 2, 4], 4, ZZ)) [3, 2, 4, 0, 0, 0, 0] """ @@ -844,8 +861,9 @@ def gf_pow(f, n, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_pow + >>> from sympy.printing import pprint - >>> gf_pow([3, 2, 4], 3, 5, ZZ) + >>> pprint(gf_pow([3, 2, 4], 3, 5, ZZ)) [2, 4, 4, 2, 2, 1, 4] """ @@ -882,8 +900,9 @@ def gf_frobenius_monomial_base(g, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_frobenius_monomial_base + >>> from sympy.printing import pprint >>> g = ZZ.map([1, 0, 2, 1]) - >>> gf_frobenius_monomial_base(g, 5, ZZ) + >>> pprint(gf_frobenius_monomial_base(g, 5, ZZ)) [[1], [4, 4, 2], [1, 2]] """ @@ -921,12 +940,13 @@ def gf_frobenius_map(f, g, b, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_frobenius_monomial_base, gf_frobenius_map + >>> from sympy.printing import pprint >>> f = ZZ.map([2, 1 , 0, 1]) >>> g = ZZ.map([1, 0, 2, 1]) >>> p = 5 >>> b = gf_frobenius_monomial_base(g, p, ZZ) >>> r = gf_frobenius_map(f, g, b, p, ZZ) - >>> gf_frobenius_map(f, g, b, p, ZZ) + >>> pprint(gf_frobenius_map(f, g, b, p, ZZ)) [4, 0, 3] """ m = gf_degree(g) @@ -1016,8 +1036,9 @@ def gf_gcd(f, g, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_gcd + >>> from sympy.printing import pprint - >>> gf_gcd(ZZ.map([3, 2, 4]), ZZ.map([2, 2, 3]), 5, ZZ) + >>> pprint(gf_gcd(ZZ.map([3, 2, 4]), ZZ.map([2, 2, 3]), 5, ZZ)) [1, 3] """ @@ -1036,8 +1057,9 @@ def gf_lcm(f, g, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_lcm + >>> from sympy.printing import pprint - >>> gf_lcm(ZZ.map([3, 2, 4]), ZZ.map([2, 2, 3]), 5, ZZ) + >>> pprint(gf_lcm(ZZ.map([3, 2, 4]), ZZ.map([2, 2, 3]), 5, ZZ)) [1, 2, 0, 4] """ @@ -1059,8 +1081,9 @@ def gf_cofactors(f, g, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_cofactors + >>> from sympy.printing import pprint - >>> gf_cofactors(ZZ.map([3, 2, 4]), ZZ.map([2, 2, 3]), 5, ZZ) + >>> pprint(gf_cofactors(ZZ.map([3, 2, 4]), ZZ.map([2, 2, 3]), 5, ZZ)) ([1, 3], [3, 3], [2, 1]) """ @@ -1086,9 +1109,10 @@ def gf_gcdex(f, g, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_gcdex, gf_mul, gf_add + >>> from sympy.printing import pprint >>> s, t, g = gf_gcdex(ZZ.map([1, 8, 7]), ZZ.map([1, 7, 1, 7]), 11, ZZ) - >>> s, t, g + >>> pprint((s, t, g)) ([5, 6], [6], [1, 7]) As result we obtained polynomials ``s = 5*x + 6`` and ``t = 6``, and @@ -1148,8 +1172,9 @@ def gf_monic(f, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_monic + >>> from sympy.printing import pprint - >>> gf_monic(ZZ.map([3, 2, 4]), 5, ZZ) + >>> pprint(gf_monic(ZZ.map([3, 2, 4]), 5, ZZ)) (3, [1, 4, 3]) """ @@ -1173,8 +1198,9 @@ def gf_diff(f, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_diff + >>> from sympy.printing import pprint - >>> gf_diff([3, 2, 4], 5, ZZ) + >>> pprint(gf_diff([3, 2, 4], 5, ZZ)) [1, 2] """ @@ -1204,7 +1230,7 @@ def gf_eval(f, a, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_eval - >>> gf_eval([3, 2, 4], 2, 5, ZZ) + >>> print(gf_eval([3, 2, 4], 2, 5, ZZ)) 0 """ @@ -1227,8 +1253,9 @@ def gf_multi_eval(f, A, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_multi_eval + >>> from sympy.printing import pprint - >>> gf_multi_eval([3, 2, 4], [0, 1, 2, 3, 4], 5, ZZ) + >>> pprint(gf_multi_eval([3, 2, 4], [0, 1, 2, 3, 4], 5, ZZ)) [4, 4, 0, 2, 0] """ @@ -1244,8 +1271,9 @@ def gf_compose(f, g, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_compose + >>> from sympy.printing import pprint - >>> gf_compose([3, 2, 4], [2, 2, 2], 5, ZZ) + >>> pprint(gf_compose([3, 2, 4], [2, 2, 2], 5, ZZ)) [2, 4, 0, 3, 0] """ @@ -1273,8 +1301,9 @@ def gf_compose_mod(g, h, f, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_compose_mod + >>> from sympy.printing import pprint - >>> gf_compose_mod(ZZ.map([3, 2, 4]), ZZ.map([2, 2, 2]), ZZ.map([4, 3]), 5, ZZ) + >>> pprint(gf_compose_mod(ZZ.map([3, 2, 4]), ZZ.map([2, 2, 2]), ZZ.map([4, 3]), 5, ZZ)) [4] """ @@ -1312,8 +1341,9 @@ def gf_trace_map(a, b, c, n, f, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_trace_map + >>> from sympy.printing import pprint - >>> gf_trace_map([1, 2], [4, 4], [1, 1], 4, [3, 2, 4], 5, ZZ) + >>> pprint(gf_trace_map([1, 2], [4, 4], [1, 1], 4, [3, 2, 4], 5, ZZ)) ([1, 3], [1, 3]) References @@ -1546,8 +1576,9 @@ def gf_sqf_part(f, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_sqf_part + >>> from sympy.printing import pprint - >>> gf_sqf_part(ZZ.map([1, 1, 3, 0, 1, 0, 2, 2, 1]), 5, ZZ) + >>> pprint(gf_sqf_part(ZZ.map([1, 1, 3, 0, 1, 0, 2, 2, 1]), 5, ZZ)) [1, 4, 3] """ @@ -1579,6 +1610,7 @@ def gf_sqf_list(f, p, K, all=False): ... gf_from_dict, gf_diff, gf_sqf_list, gf_pow, ... ) ... # doctest: +NORMALIZE_WHITESPACE + >>> from sympy.printing import pprint >>> f = gf_from_dict({11: ZZ(1), 0: ZZ(1)}, 11, ZZ) @@ -1590,7 +1622,7 @@ def gf_sqf_list(f, p, K, all=False): This phenomenon doesn't happen in characteristic zero. However we can still compute square-free decomposition of ``f`` using ``gf_sqf()``:: - >>> gf_sqf_list(f, 11, ZZ) + >>> pprint(gf_sqf_list(f, 11, ZZ)) (1, [([1, 1], 11)]) We obtained factorization ``f = (x + 1)**11``. This is correct because:: @@ -1659,12 +1691,13 @@ def gf_Qmatrix(f, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_Qmatrix + >>> from sympy.printing import pprint - >>> gf_Qmatrix([3, 2, 4], 5, ZZ) + >>> pprint(gf_Qmatrix([3, 2, 4], 5, ZZ)) [[1, 0], [3, 4]] - >>> gf_Qmatrix([1, 0, 0, 0, 1], 5, ZZ) + >>> pprint(gf_Qmatrix([1, 0, 0, 0, 1], 5, ZZ)) [[1, 0, 0, 0], [0, 4, 0, 0], [0, 0, 1, 0], @@ -1699,11 +1732,12 @@ def gf_Qbasis(Q, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_Qmatrix, gf_Qbasis + >>> from sympy.printing import pprint - >>> gf_Qbasis(gf_Qmatrix([1, 0, 0, 0, 1], 5, ZZ), 5, ZZ) + >>> pprint(gf_Qbasis(gf_Qmatrix([1, 0, 0, 0, 1], 5, ZZ), 5, ZZ)) [[1, 0, 0, 0], [0, 0, 1, 0]] - >>> gf_Qbasis(gf_Qmatrix([3, 2, 4], 5, ZZ), 5, ZZ) + >>> pprint(gf_Qbasis(gf_Qmatrix([3, 2, 4], 5, ZZ), 5, ZZ)) [[1, 0]] """ @@ -1761,8 +1795,9 @@ def gf_berlekamp(f, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_berlekamp + >>> from sympy.printing import pprint - >>> gf_berlekamp([1, 0, 0, 0, 1], 5, ZZ) + >>> pprint(gf_berlekamp([1, 0, 0, 0, 1], 5, ZZ)) [[1, 0, 2], [1, 0, 3]] """ @@ -1810,6 +1845,7 @@ def gf_ddf_zassenhaus(f, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_from_dict + >>> from sympy.printing import pprint >>> f = gf_from_dict({15: ZZ(1), 0: ZZ(-1)}, 11, ZZ) @@ -1817,7 +1853,7 @@ def gf_ddf_zassenhaus(f, p, K): >>> from sympy.polys.galoistools import gf_ddf_zassenhaus - >>> gf_ddf_zassenhaus(f, 11, ZZ) + >>> pprint(gf_ddf_zassenhaus(f, 11, ZZ)) [([1, 0, 0, 0, 0, 10], 1), ([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], 2)] which means ``x**15 - 1 = (x**5 - 1) (x**10 + x**5 + 1)``. To obtain @@ -1867,8 +1903,9 @@ def gf_edf_zassenhaus(f, n, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_edf_zassenhaus + >>> from sympy.printing import pprint - >>> gf_edf_zassenhaus([1,1,1,1], 1, 5, ZZ) + >>> pprint(gf_edf_zassenhaus([1,1,1,1], 1, 5, ZZ)) [[1, 1], [1, 2], [1, 3]] References @@ -1927,10 +1964,11 @@ def gf_ddf_shoup(f, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_ddf_shoup, gf_from_dict + >>> from sympy.printing import pprint >>> f = gf_from_dict({6: ZZ(1), 5: ZZ(-1), 4: ZZ(1), 3: ZZ(1), 1: ZZ(-1)}, 3, ZZ) - >>> gf_ddf_shoup(f, 3, ZZ) + >>> pprint(gf_ddf_shoup(f, 3, ZZ)) [([1, 1, 0], 1), ([1, 1, 0, 1, 2], 2)] References @@ -2002,8 +2040,9 @@ def gf_edf_shoup(f, n, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_edf_shoup + >>> from sympy.printing import pprint - >>> gf_edf_shoup(ZZ.map([1, 2837, 2277]), 1, 2917, ZZ) + >>> pprint(gf_edf_shoup(ZZ.map([1, 2837, 2277]), 1, 2917, ZZ)) [[1, 852], [1, 1985]] References @@ -2057,8 +2096,9 @@ def gf_zassenhaus(f, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_zassenhaus + >>> from sympy.printing import pprint - >>> gf_zassenhaus(ZZ.map([1, 4, 3]), 5, ZZ) + >>> pprint(gf_zassenhaus(ZZ.map([1, 4, 3]), 5, ZZ)) [[1, 1], [1, 3]] """ @@ -2079,8 +2119,9 @@ def gf_shoup(f, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_shoup + >>> from sympy.printing import pprint - >>> gf_shoup(ZZ.map([1, 4, 3]), 5, ZZ) + >>> pprint(gf_shoup(ZZ.map([1, 4, 3]), 5, ZZ)) [[1, 1], [1, 3]] """ @@ -2107,8 +2148,9 @@ def gf_factor_sqf(f, p, K, method=None): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_factor_sqf + >>> from sympy.printing import pprint - >>> gf_factor_sqf(ZZ.map([3, 2, 4]), 5, ZZ) + >>> pprint(gf_factor_sqf(ZZ.map([3, 2, 4]), 5, ZZ)) (3, [[1, 1], [1, 3]]) """ @@ -2149,8 +2191,9 @@ def gf_factor(f, p, K): >>> from sympy.polys.domains import ZZ >>> from sympy.polys.galoistools import gf_factor + >>> from sympy.printing import pprint - >>> gf_factor(ZZ.map([5, 2, 7, 2]), 11, ZZ) + >>> pprint(gf_factor(ZZ.map([5, 2, 7, 2]), 11, ZZ)) (5, [([1, 2], 1), ([1, 8], 2)]) We arrived with factorization ``f = 5 (x + 2) (x + 8)**2``. We didn't @@ -2332,7 +2375,8 @@ def gf_csolve(f, n): Solve [1, 1, 7] congruent 0 mod(189): >>> from sympy.polys.galoistools import gf_csolve - >>> gf_csolve([1, 1, 7], 189) + >>> from sympy.printing import pprint + >>> pprint(gf_csolve([1, 1, 7], 189)) [13, 49, 76, 112, 139, 175] References diff --git a/sympy/polys/polyclasses.py b/sympy/polys/polyclasses.py index 96417efcf0..10bc0d3014 100644 --- a/sympy/polys/polyclasses.py +++ b/sympy/polys/polyclasses.py @@ -361,8 +361,9 @@ def exclude(f): >>> from sympy.polys.polyclasses import DMP >>> from sympy.polys.domains import ZZ + >>> from sympy.printing import sstr - >>> DMP([[[ZZ(1)]], [[ZZ(1)], [ZZ(2)]]], ZZ).exclude() + >>> print(sstr(DMP([[[ZZ(1)]], [[ZZ(1)], [ZZ(2)]]], ZZ).exclude())) ([2], DMP([[1], [1, 2]], ZZ, None)) """ @@ -378,11 +379,12 @@ def permute(f, P): >>> from sympy.polys.polyclasses import DMP >>> from sympy.polys.domains import ZZ + >>> from sympy.printing import sstr - >>> DMP([[[ZZ(2)], [ZZ(1), ZZ(0)]], [[]]], ZZ).permute([1, 0, 2]) + >>> print(sstr(DMP([[[ZZ(2)], [ZZ(1), ZZ(0)]], [[]]], ZZ).permute([1, 0, 2]))) DMP([[[2], []], [[1, 0], []]], ZZ, None) - >>> DMP([[[ZZ(2)], [ZZ(1), ZZ(0)]], [[]]], ZZ).permute([1, 2, 0]) + >>> print(sstr(DMP([[[ZZ(2)], [ZZ(1), ZZ(0)]], [[]]], ZZ).permute([1, 2, 0]))) DMP([[[1], []], [[2, 0], []]], ZZ, None) """ diff --git a/sympy/polys/polyoptions.py b/sympy/polys/polyoptions.py index c2f5bd951c..f904f2e737 100644 --- a/sympy/polys/polyoptions.py +++ b/sympy/polys/polyoptions.py @@ -80,16 +80,17 @@ class Options(dict): Examples ======== + >>> from sympy.polys.domains import ZZ >>> from sympy.polys.polyoptions import Options >>> from sympy.polys.polyoptions import build_options >>> from sympy.abc import x, y, z - >>> Options((x, y, z), {'domain': 'ZZ'}) - {'auto': False, 'domain': ZZ, 'gens': (x, y, z)} + >>> Options((x, y, z), {'domain': 'ZZ'}) == {'auto': False, 'domain': ZZ, 'gens': (x, y, z)} + True - >>> build_options((x, y, z), {'domain': 'ZZ'}) - {'auto': False, 'domain': ZZ, 'gens': (x, y, z)} + >>> build_options((x, y, z), {'domain': 'ZZ'}) == {'auto': False, 'domain': ZZ, 'gens': (x, y, z)} + True **Options** diff --git a/sympy/polys/polyroots.py b/sympy/polys/polyroots.py index e5482d6f39..c6d08afddd 100644 --- a/sympy/polys/polyroots.py +++ b/sympy/polys/polyroots.py @@ -820,27 +820,26 @@ def roots(f, *gens, **flags): Examples ======== - >>> from sympy import Poly, roots + >>> from sympy import Poly, roots, sqrt >>> from sympy.abc import x, y - >>> roots(x**2 - 1, x) - {-1: 1, 1: 1} + >>> roots(x**2 - 1, x) == {-1: 1, 1: 1} + True >>> p = Poly(x**2-1, x) - >>> roots(p) - {-1: 1, 1: 1} + >>> roots(p) == {-1: 1, 1: 1} + True >>> p = Poly(x**2-y, x, y) - >>> roots(Poly(p, x)) - {-sqrt(y): 1, sqrt(y): 1} + >>> roots(Poly(p, x)) == {-sqrt(y): 1, sqrt(y): 1} + True - >>> roots(x**2 - y, x) - {-sqrt(y): 1, sqrt(y): 1} - - >>> roots([1, 0, -1]) - {-1: 1, 1: 1} + >>> roots(x**2 - y, x) == {-sqrt(y): 1, sqrt(y): 1} + True + >>> roots([1, 0, -1]) == {-1: 1, 1: 1} + True References ========== diff --git a/sympy/polys/polytools.py b/sympy/polys/polytools.py index c738307259..c0348e14c7 100644 --- a/sympy/polys/polytools.py +++ b/sympy/polys/polytools.py @@ -216,13 +216,12 @@ def free_symbols(self): >>> from sympy import Poly >>> from sympy.abc import x, y - >>> Poly(x**2 + 1).free_symbols - set([x]) - >>> Poly(x**2 + y).free_symbols - set([x, y]) - >>> Poly(x**2 + y, x).free_symbols - set([x, y]) - + >>> Poly(x**2 + 1).free_symbols == {x} + True + >>> Poly(x**2 + y).free_symbols == {x, y} + True + >>> Poly(x**2 + y, x).free_symbols == {x, y} + True """ symbols = set() @@ -242,13 +241,12 @@ def free_symbols_in_domain(self): >>> from sympy import Poly >>> from sympy.abc import x, y - >>> Poly(x**2 + 1).free_symbols_in_domain - set() - >>> Poly(x**2 + y).free_symbols_in_domain - set() - >>> Poly(x**2 + y, x).free_symbols_in_domain - set([y]) - + >>> Poly(x**2 + 1).free_symbols_in_domain == set() + True + >>> Poly(x**2 + y).free_symbols_in_domain == set() + True + >>> Poly(x**2 + y, x).free_symbols_in_domain == {y} + True """ domain, symbols = self.rep.dom, set() @@ -1967,7 +1965,7 @@ def LM(f, order=None): >>> from sympy import Poly >>> from sympy.abc import x, y - >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).LM() + >>> print(Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).LM()) x**2*y**0 """ @@ -1983,7 +1981,7 @@ def EM(f, order=None): >>> from sympy import Poly >>> from sympy.abc import x, y - >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).EM() + >>> print(Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).EM()) x**0*y**1 """ @@ -1998,8 +1996,9 @@ def LT(f, order=None): >>> from sympy import Poly >>> from sympy.abc import x, y + >>> from sympy.printing import sstr - >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).LT() + >>> print(sstr(Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).LT())) (x**2*y**0, 4) """ @@ -2015,8 +2014,9 @@ def ET(f, order=None): >>> from sympy import Poly >>> from sympy.abc import x, y + >>> from sympy.printing import sstr - >>> Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).ET() + >>> print(sstr(Poly(4*x**2 + 2*x*y**2 + x*y + 3*y, x, y).ET())) (x**0*y**1, 3) """ @@ -4016,13 +4016,12 @@ def free_symbols(self): >>> from sympy import PurePoly >>> from sympy.abc import x, y - >>> PurePoly(x**2 + 1).free_symbols - set() - >>> PurePoly(x**2 + y).free_symbols - set() - >>> PurePoly(x**2 + y, x).free_symbols - set([y]) - + >>> PurePoly(x**2 + 1).free_symbols == set() + True + >>> PurePoly(x**2 + y).free_symbols == set() + True + >>> PurePoly(x**2 + y, x).free_symbols == {y} + True """ return self.free_symbols_in_domain diff --git a/sympy/polys/rings.py b/sympy/polys/rings.py index 9936650cb3..09f00ba54e 100644 --- a/sympy/polys/rings.py +++ b/sympy/polys/rings.py @@ -449,11 +449,12 @@ def add(self, *objs): >>> from sympy.polys.rings import ring >>> from sympy.polys.domains import ZZ + >>> from sympy.printing import pprint >>> R, x = ring("x", ZZ) >>> R.add([ x**2 + 2*i + 3 for i in range(4) ]) 4*x**2 + 24 - >>> _.factor_list() + >>> pprint(_.factor_list()) (4, [(x**2 + 6, 1)]) """ @@ -476,11 +477,12 @@ def mul(self, *objs): >>> from sympy.polys.rings import ring >>> from sympy.polys.domains import ZZ + >>> from sympy.printing import pprint >>> R, x = ring("x", ZZ) >>> R.mul([ x**2 + 2*i + 3 for i in range(4) ]) x**8 + 24*x**6 + 206*x**4 + 744*x**2 + 945 - >>> _.factor_list() + >>> pprint(_.factor_list()) (1, [(x**2 + 3, 1), (x**2 + 5, 1), (x**2 + 7, 1), (x**2 + 9, 1)]) """ @@ -1641,11 +1643,11 @@ def coeff(self, element): >>> _, x, y, z = ring("x,y,z", ZZ) >>> f = 3*x**2*y - x*y*z + 7*z**3 + 23 - >>> f.coeff(x**2*y) + >>> print(f.coeff(x**2*y)) 3 - >>> f.coeff(x*y) + >>> print(f.coeff(x*y)) 0 - >>> f.coeff(1) + >>> print(f.coeff(1)) 23 """ @@ -1749,13 +1751,14 @@ def coeffs(self, order=None): >>> from sympy.polys.rings import ring >>> from sympy.polys.domains import ZZ >>> from sympy.polys.orderings import lex, grlex + >>> from sympy.printing import pprint >>> _, x, y = ring("x, y", ZZ, lex) >>> f = x*y**7 + 2*x**2*y**3 - >>> f.coeffs() + >>> pprint(f.coeffs()) [2, 1] - >>> f.coeffs(grlex) + >>> pprint(f.coeffs(grlex)) [1, 2] """ @@ -1799,13 +1802,14 @@ def terms(self, order=None): >>> from sympy.polys.rings import ring >>> from sympy.polys.domains import ZZ >>> from sympy.polys.orderings import lex, grlex + >>> from sympy.printing import pprint >>> _, x, y = ring("x, y", ZZ, lex) >>> f = x*y**7 + 2*x**2*y**3 - >>> f.terms() + >>> pprint(f.terms()) [((2, 3), 2), ((1, 7), 1)] - >>> f.terms(grlex) + >>> pprint(f.terms(grlex)) [((1, 7), 1), ((2, 3), 2)] """ diff --git a/sympy/polys/sqfreetools.py b/sympy/polys/sqfreetools.py index e0bd101d2d..a945e5d1e6 100644 --- a/sympy/polys/sqfreetools.py +++ b/sympy/polys/sqfreetools.py @@ -278,13 +278,14 @@ def dup_sqf_list(f, K, all=False): ======== >>> from sympy.polys import ring, ZZ + >>> from sympy.printing import pprint >>> R, x = ring("x", ZZ) >>> f = 2*x**5 + 16*x**4 + 50*x**3 + 76*x**2 + 56*x + 16 - >>> R.dup_sqf_list(f) + >>> pprint(R.dup_sqf_list(f)) (2, [(x + 1, 2), (x + 2, 3)]) - >>> R.dup_sqf_list(f, all=True) + >>> pprint(R.dup_sqf_list(f, all=True)) (2, [(1, 1), (x + 1, 2), (x + 2, 3)]) """ @@ -363,13 +364,14 @@ def dmp_sqf_list(f, u, K, all=False): ======== >>> from sympy.polys import ring, ZZ + >>> from sympy.printing import pprint >>> R, x,y = ring("x,y", ZZ) >>> f = x**5 + 2*x**4*y + x**3*y**2 - >>> R.dmp_sqf_list(f) + >>> pprint(R.dmp_sqf_list(f)) (1, [(x + y, 2), (x, 3)]) - >>> R.dmp_sqf_list(f, all=True) + >>> pprint(R.dmp_sqf_list(f, all=True)) (1, [(1, 1), (x + y, 2), (x, 3)]) """ diff --git a/sympy/printing/dot.py b/sympy/printing/dot.py index d4b9e07f58..e6b8c819a8 100644 --- a/sympy/printing/dot.py +++ b/sympy/printing/dot.py @@ -33,12 +33,12 @@ def styleof(expr, styles=default_styles): >>> styles = [(Basic, {'color': 'blue', 'shape': 'ellipse'}), ... (Expr, {'color': 'black'})] - >>> styleof(Basic(1), styles) - {'color': 'blue', 'shape': 'ellipse'} + >>> styleof(Basic(1), styles) == {'color': 'blue', 'shape': 'ellipse'} + True >>> x = Symbol('x') - >>> styleof(x + 1, styles) # this is an Expr - {'color': 'black', 'shape': 'ellipse'} + >>> styleof(x + 1, styles) == {'color': 'black', 'shape': 'ellipse'} # this is an Expr + True """ style = dict() for typ, sty in styles: diff --git a/sympy/series/tests/test_gruntz.py b/sympy/series/tests/test_gruntz.py index baa22e2e32..a20cde1941 100644 --- a/sympy/series/tests/test_gruntz.py +++ b/sympy/series/tests/test_gruntz.py @@ -82,6 +82,7 @@ def test_gruntz_evaluation(): assert gruntz(exp(exp(exp(x + exp(-x)))) / exp(exp(x)), x) == oo +@slow def test_gruntz_eval_special(): # Gruntz, p. 126 assert gruntz(exp(x)*(sin(1/x + exp(-x)) - sin(1/x + exp(-x**2))), x) == 1 diff --git a/sympy/sets/fancysets.py b/sympy/sets/fancysets.py index 20b64e7646..de96d1e3b4 100644 --- a/sympy/sets/fancysets.py +++ b/sympy/sets/fancysets.py @@ -2,7 +2,7 @@ from sympy.core.basic import Basic from sympy.core.compatibility import as_int, with_metaclass, range -from sympy.sets.sets import Set, Interval, Intersection, EmptySet +from sympy.sets.sets import Set, Interval, Intersection, EmptySet, FiniteSet from sympy.core.singleton import Singleton, S from sympy.core.sympify import _sympify from sympy.core.function import Lambda @@ -272,23 +272,31 @@ def _intersect(self, other): return imageset(Lambda(t, f.subs(a, solns[0][0])), S.Integers) if other == S.Reals: - from sympy.solvers.solveset import solveset_real + from sympy.solvers.diophantine import diophantine from sympy.core.function import expand_complex - if len(self.lamda.variables) > 1: - return None + if len(self.lamda.variables) > 1 or self.base_set is not S.Integers: + return f = self.lamda.expr n = self.lamda.variables[0] - n_ = Dummy(n.name, extended_real=True) + n_ = Dummy(n.name, integer=True) f_ = f.subs(n, n_) re, im = f_.as_real_imag() im = expand_complex(im) - return imageset(Lambda(n_, re), - self.base_set.intersect( - solveset_real(im, n_))) + sols = list(diophantine(im, n_)) + if not sols: + return S.EmptySet + elif all(s[0].has(n_) is False for s in sols): + s = FiniteSet(*[s[0] for s in sols]) + elif len(sols) == 1 and sols[0][0].has(n_): + s = imageset(Lambda(n_, sols[0][0]), S.Integers) + else: + return + + return imageset(Lambda(n_, re), self.base_set.intersect(s)) class Range(Set): diff --git a/sympy/sets/sets.py b/sympy/sets/sets.py index a5db3b747c..acc84e89e8 100644 --- a/sympy/sets/sets.py +++ b/sympy/sets/sets.py @@ -515,7 +515,7 @@ class ProductSet(Set): Examples ======== - >>> from sympy import Interval, FiniteSet, ProductSet + >>> from sympy import Interval, FiniteSet, ProductSet, Symbol >>> I = Interval(0, 5); S = FiniteSet(1, 2, 3) >>> ProductSet(I, S) [0, 5] x {1, 2, 3} @@ -526,10 +526,10 @@ class ProductSet(Set): >>> Interval(0, 1) * Interval(0, 1) # The unit square [0, 1] x [0, 1] - >>> coin = FiniteSet('H', 'T') - >>> set(coin**2) - set([(H, H), (H, T), (T, H), (T, T)]) - + >>> H, T = Symbol('H'), Symbol('T') + >>> coin = FiniteSet(H, T) + >>> set(coin**2) == {(H, H), (H, T), (T, H), (T, T)} + True Notes ===== diff --git a/sympy/simplify/hyperexpand.py b/sympy/simplify/hyperexpand.py index 2560e73a14..d32cfea10c 100644 --- a/sympy/simplify/hyperexpand.py +++ b/sympy/simplify/hyperexpand.py @@ -630,13 +630,11 @@ def compute_buckets(self): >>> from sympy.simplify.hyperexpand import G_Function >>> from sympy.abc import y - >>> from sympy import S, symbols + >>> from sympy import S, symbols, Rational >>> a, b = [1, 3, 2, S(3)/2], [1 + y, y, 2, y + 3] - >>> G_Function(a, b, [2], [y]).compute_buckets() - ({0: [3, 2, 1], 1/2: [3/2]}, - {0: [2], y: [y, y + 1, y + 3]}, {0: [2]}, {y: [y]}) - + >>> G_Function(a, b, [2], [y]).compute_buckets() == ({0: [3, 2, 1], Rational(1, 2): [Rational(3, 2)]}, {0: [2], y: [y, y + 1, y + 3]}, {0: [2]}, {y: [y]}) + True """ dicts = pan, pap, pbm, pbq = [defaultdict(list) for i in range(4)] for dic, lis in zip(dicts, (self.an, self.ap, self.bm, self.bq)): @@ -1431,11 +1429,18 @@ def reduce_order(func): >>> from sympy.simplify.hyperexpand import reduce_order, Hyper_Function >>> reduce_order(Hyper_Function((1, 2), (3, 4))) (Hyper_Function((1, 2), (3, 4)), []) - >>> reduce_order(Hyper_Function((1,), (1,))) - (Hyper_Function((), ()), []) - >>> reduce_order(Hyper_Function((2, 4), (3, 3))) - (Hyper_Function((2,), (3,)), []) + >>> r = reduce_order(Hyper_Function((1,), (1,))) + >>> r[0] + Hyper_Function((), ()) + >>> for i in r[1]: + ... print(i) + + >>> r = reduce_order(Hyper_Function((2, 4), (3, 3))) + >>> r[0] + Hyper_Function((2,), (3,)) + >>> for i in r[1]: + ... print(i) + """ nap, nbq, operators = _reduce_order(func.ap, func.bq, ReduceOrder, default_sort_key) @@ -1510,30 +1515,38 @@ def devise_plan(target, origin, z): Very simple plans: - >>> devise_plan(Hyper_Function((2,), ()), Hyper_Function((1,), ()), z) - [] - >>> devise_plan(Hyper_Function((), (2,)), Hyper_Function((), (1,)), z) - [] + >>> for i in devise_plan(Hyper_Function((2,), ()), Hyper_Function((1,), ()), z): + ... print(i) + + >>> for i in devise_plan(Hyper_Function((), (2,)), Hyper_Function((), (1,)), z): + ... print(i) + Several buckets: >>> from sympy import S - >>> devise_plan(Hyper_Function((1, S.Half), ()), - ... Hyper_Function((2, S('3/2')), ()), z) #doctest: +NORMALIZE_WHITESPACE - [, - ] + >>> for i in devise_plan(Hyper_Function((1, S.Half), ()), + ... Hyper_Function((2, S('3/2')), ()), z): + ... print(i) + + A slightly more complicated plan: - >>> devise_plan(Hyper_Function((1, 3), ()), Hyper_Function((2, 2), ()), z) - [, ] + >>> for i in devise_plan(Hyper_Function((1, 3), ()), Hyper_Function((2, 2), ()), z): + ... print(i) + + Another more complicated plan: (note that the ap have to be shifted first!) - >>> devise_plan(Hyper_Function((1, -1), (2,)), Hyper_Function((3, -2), (4,)), z) - [, , - , - , ] + >>> for i in devise_plan(Hyper_Function((1, -1), (2,)), Hyper_Function((3, -2), (4,)), z): + ... print(i) + + + + + """ abuckets, bbuckets, nabuckets, nbbuckets = [sift(params, _mod1) for params in (target.ap, target.bq, origin.ap, origin.bq)] @@ -2062,31 +2075,39 @@ def devise_plan_meijer(fro, to, z): Very simple plans: - >>> devise_plan_meijer(G_Function([0], [], [], []), - ... G_Function([1], [], [], []), z) - [] - >>> devise_plan_meijer(G_Function([0], [], [], []), - ... G_Function([-1], [], [], []), z) - [] - >>> devise_plan_meijer(G_Function([], [1], [], []), - ... G_Function([], [2], [], []), z) - [] + >>> for i in devise_plan_meijer(G_Function([0], [], [], []), + ... G_Function([1], [], [], []), z): + ... print(i) + + >>> for i in devise_plan_meijer(G_Function([0], [], [], []), + ... G_Function([-1], [], [], []), z): + ... print(i) + + >>> for i in devise_plan_meijer(G_Function([], [1], [], []), + ... G_Function([], [2], [], []), z): + ... print(i) + Slightly more complicated plans: - >>> devise_plan_meijer(G_Function([0], [], [], []), - ... G_Function([2], [], [], []), z) - [, - ] - >>> devise_plan_meijer(G_Function([0], [], [0], []), - ... G_Function([-1], [], [1], []), z) - [, ] + >>> for i in devise_plan_meijer(G_Function([0], [], [], []), + ... G_Function([2], [], [], []), z): + ... print(i) + + + >>> for i in devise_plan_meijer(G_Function([0], [], [0], []), + ... G_Function([-1], [], [1], []), z): + ... print(i) + + Order matters: - >>> devise_plan_meijer(G_Function([0], [], [0], []), - ... G_Function([1], [], [1], []), z) - [, ] + >>> for i in devise_plan_meijer(G_Function([0], [], [0], []), + ... G_Function([1], [], [1], []), z): + ... print(i) + + """ # TODO for now, we use the following simple heuristic: inverse-shift # when possible, shift otherwise. Give up if we cannot make progress. diff --git a/sympy/simplify/simplify.py b/sympy/simplify/simplify.py index aabe17205b..e16defa5df 100755 --- a/sympy/simplify/simplify.py +++ b/sympy/simplify/simplify.py @@ -619,10 +619,10 @@ def separatevars(expr, symbols=[], dict=False, force=False): >>> e = 2*x**2*z*sin(y)+2*z*x**2 >>> separatevars(e) 2*x**2*z*(sin(y) + 1) - >>> separatevars(e, symbols=(x, y), dict=True) - {'coeff': 2*z, x: x**2, y: sin(y) + 1} - >>> separatevars(e, [x, y, alpha], dict=True) - {'coeff': 2*z, alpha: 1, x: x**2, y: sin(y) + 1} + >>> separatevars(e, symbols=(x, y), dict=True) == {'coeff': 2*z, x: x**2, y: sin(y) + 1} + True + >>> separatevars(e, [x, y, alpha], dict=True) == {'coeff': 2*z, alpha: 1, x: x**2, y: sin(y) + 1} + True If the expression is not really separable, or is only partially separable, separatevars will do the best it can to separate it @@ -1838,7 +1838,8 @@ def radsimp(expr, symbolic=True, max_terms=4): >>> r2 = sqrt(2) >>> r5 = sqrt(5) - >>> ans = radsimp(1/(y*r2 + x*r2 + a*r5 + b*r5)); pprint(ans) + >>> ans = radsimp(1/(y*r2 + x*r2 + a*r5 + b*r5)) + >>> pprint(ans, use_unicode=False) ___ ___ ___ ___ \/ 5 *a + \/ 5 *b - \/ 2 *x - \/ 2 *y ------------------------------------------ @@ -1846,7 +1847,7 @@ def radsimp(expr, symbolic=True, max_terms=4): 5*a + 10*a*b + 5*b - 2*x - 4*x*y - 2*y >>> n, d = fraction(ans) - >>> pprint(factor_terms(signsimp(collect_sqrt(n))/d, radical=True)) + >>> pprint(factor_terms(signsimp(collect_sqrt(n))/d, radical=True), use_unicode=False) ___ ___ \/ 5 *(a + b) - \/ 2 *(x + y) ------------------------------------------ @@ -2142,13 +2143,15 @@ def polarify(eq, subs=True, lift=False): changed to their polar_lift()ed versions. Note that lift=True implies subs=False. - >>> from sympy import polarify, sin, I + >>> from sympy import polarify, sin, I, default_sort_key >>> from sympy.abc import x, y >>> expr = (-x)**y >>> expr.expand() (-x)**y - >>> polarify(expr) - ((_x*exp_polar(I*pi))**_y, {_x: x, _y: y}) + >>> polarify(expr)[0] + (_x*exp_polar(I*pi))**_y + >>> sorted(polarify(expr)[1].items(), key=default_sort_key) + [(_x, x), (_y, y)] >>> polarify(expr)[0].expand() _x**_y*exp_polar(_y*I*pi) >>> polarify(x, lift=True) diff --git a/sympy/solvers/bivariate.py b/sympy/solvers/bivariate.py index c966a19d69..ea2162d07c 100644 --- a/sympy/solvers/bivariate.py +++ b/sympy/solvers/bivariate.py @@ -25,9 +25,8 @@ def _filtered_gens(poly, symbol): >>> from sympy.solvers.bivariate import _filtered_gens >>> from sympy import Poly, exp >>> from sympy.abc import x - >>> _filtered_gens(Poly(x + 1/x + exp(x)), x) - set([x, exp(x)]) - + >>> _filtered_gens(Poly(x + 1/x + exp(x)), x) == {x, exp(x)} + True """ gens = {g for g in poly.gens if symbol in g.free_symbols} for g in list(gens): diff --git a/sympy/solvers/diophantine.py b/sympy/solvers/diophantine.py index 01067bc527..62614c59cf 100644 --- a/sympy/solvers/diophantine.py +++ b/sympy/solvers/diophantine.py @@ -50,10 +50,12 @@ def diophantine(eq, param=symbols("t", integer=True)): Examples ======== + >>> from sympy import Symbol >>> from sympy.solvers.diophantine import diophantine >>> from sympy.abc import x, y, z - >>> diophantine(x**2 - y**2) - set([(-t, -t), (t, -t)]) + >>> t = Symbol('t', integer=True) + >>> diophantine(x**2 - y**2, t) == {(-t, -t), (t, -t)} + True See Also ======== @@ -63,6 +65,9 @@ def diophantine(eq, param=symbols("t", integer=True)): if isinstance(eq, Eq): eq = eq.lhs - eq.rhs + if eq == 0: + return set([(param,)]) + eq = Poly(eq).as_expr() if not eq.is_polynomial() or eq.is_number: raise TypeError("Equation input format not supported") @@ -150,13 +155,14 @@ def diop_solve(eq, param=symbols("t", integer=True)): >>> from sympy.solvers.diophantine import diop_solve >>> from sympy.abc import x, y, z, w + >>> from sympy.printing import pprint >>> diop_solve(2*x + 3*y - 5) (3*t - 5, -2*t + 5) >>> diop_solve(4*x + 3*y -4*z + 5) (3*t + 4*z - 5, -4*t - 4*z + 5, z) >>> diop_solve(x + 3*y - 4*z + w -6) (t, -t - 3*y + 4*z + 6, y, z) - >>> diop_solve(x**2 + y**2 - 5) + >>> pprint(diop_solve(x**2 + y**2 - 5)) set([(-2, -1), (-2, 1), (2, -1), (2, 1)]) See Also @@ -219,12 +225,12 @@ def classify_diop(eq): >>> from sympy.solvers.diophantine import classify_diop >>> from sympy.abc import x, y, z, w, t - >>> classify_diop(4*x + 6*y - 4) - ([x, y], {1: -4, x: 4, y: 6}, 'linear') - >>> classify_diop(x + 3*y -4*z + 5) - ([x, y, z], {1: 5, x: 1, y: 3, z: -4}, 'linear') - >>> classify_diop(x**2 + y**2 - x*y + x + 5) - ([x, y], {1: 5, x: 1, x**2: 1, y: 0, y**2: 1, x*y: -1}, 'binary_quadratic') + >>> classify_diop(4*x + 6*y - 4) == ([x, y], {1: -4, x: 4, y: 6}, 'linear') + True + >>> classify_diop(x + 3*y -4*z + 5) == ([x, y, z], {1: 5, x: 1, y: 3, z: -4}, 'linear') + True + >>> classify_diop(x**2 + y**2 - x*y + x + 5) == ([x, y], {1: 5, x: 1, x**2: 1, y: 0, y**2: 1, x*y: -1}, 'binary_quadratic') + True """ eq = eq.expand(force=True) var = list(eq.free_symbols) @@ -540,7 +546,8 @@ def diop_quadratic(eq, param=symbols("t", integer=True)): >>> from sympy.abc import x, y, t >>> from sympy.solvers.diophantine import diop_quadratic - >>> diop_quadratic(x**2 + y**2 + 2*x + 2*y + 2, t) + >>> from sympy.printing import pprint + >>> pprint(diop_quadratic(x**2 + y**2 + 2*x + 2*y + 2, t)) set([(-1, -1)]) References @@ -1003,10 +1010,10 @@ def cornacchia(a, b, m): ======== >>> from sympy.solvers.diophantine import cornacchia - >>> cornacchia(2, 3, 35) # equation 2x**2 + 3y**2 = 35 - set([(2, 3), (4, 1)]) - >>> cornacchia(1, 1, 25) # equation x**2 + y**2 = 25 - set([(4, 3)]) + >>> cornacchia(2, 3, 35) == {(2, 3), (4, 1)} # equation 2x**2 + 3y**2 = 35 + True + >>> cornacchia(1, 1, 25) == {(4, 3)} # equation x**2 + y**2 = 25 + True References =========== @@ -2387,8 +2394,8 @@ def diop_general_sum_of_squares(eq, limit=1): >>> from sympy.solvers.diophantine import diop_general_sum_of_squares >>> from sympy.abc import a, b, c, d, e, f - >>> diop_general_sum_of_squares(a**2 + b**2 + c**2 + d**2 + e**2 - 2345) - set([(0, 48, 5, 4, 0)]) + >>> diop_general_sum_of_squares(a**2 + b**2 + c**2 + d**2 + e**2 - 2345) == {(0, 48, 5, 4, 0)} + True Reference ========= diff --git a/sympy/solvers/ode.py b/sympy/solvers/ode.py index a3dcb89078..a120f81c93 100644 --- a/sympy/solvers/ode.py +++ b/sympy/solvers/ode.py @@ -554,7 +554,7 @@ def dsolve(eq, func=None, hint="default", simplify=True, Examples ======== - >>> from sympy import Function, dsolve, Eq, Derivative, sin, cos, symbols + >>> from sympy import Function, dsolve, Eq, Derivative, sin, cos, symbols, exp >>> from sympy.abc import x >>> f = Function('f') >>> dsolve(Derivative(f(x), x, x) + 9*f(x), f(x)) @@ -573,8 +573,9 @@ def dsolve(eq, func=None, hint="default", simplify=True, Eq(y(t), C1*y0 + C2(y0*Integral(8*exp(Integral(7*t, t))*exp(Integral(12*t, t))/x0**2, t) + exp(Integral(7*t, t))*exp(Integral(12*t, t))/x0))] >>> eq = (Eq(Derivative(x(t),t),x(t)*y(t)*sin(t)), Eq(Derivative(y(t),t),y(t)**2*sin(t))) - >>> dsolve(eq) - set([Eq(x(t), -exp(C1)/(C2*exp(C1) - cos(t))), Eq(y(t), -1/(C1 - cos(t)))]) + >>> C1, C2 = symbols('C1, C2') + >>> dsolve(eq) == {Eq(x(t), -exp(C1)/(C2*exp(C1) - cos(t))), Eq(y(t), -1/(C1 - cos(t)))} + True """ if iterable(eq): match = classify_sysode(eq, func) @@ -1310,7 +1311,7 @@ def classify_sysode(eq, funcs=None, **kwargs): Examples ======== - >>> from sympy import Function, Eq, symbols, diff + >>> from sympy import Function, Eq, symbols, diff, Derivative >>> from sympy.solvers.ode import classify_sysode >>> from sympy.abc import t >>> f, x, y = symbols('f, x, y', function=True) @@ -1318,18 +1319,11 @@ def classify_sysode(eq, funcs=None, **kwargs): >>> x1 = diff(x(t), t) ; y1 = diff(y(t), t) >>> x2 = diff(x(t), t, t) ; y2 = diff(y(t), t, t) >>> eq = (Eq(5*x1, 12*x(t) - 6*y(t)), Eq(2*y1, 11*x(t) + 3*y(t))) - >>> classify_sysode(eq) - {'eq': [-12*x(t) + 6*y(t) + 5*Derivative(x(t), t), -11*x(t) - 3*y(t) + 2*Derivative(y(t), t)], - 'func': [x(t), y(t)], 'func_coeff': {(0, x(t), 0): -12, (0, x(t), 1): 5, (0, y(t), 0): 6, - (0, y(t), 1): 0, (1, x(t), 0): -11, (1, x(t), 1): 0, (1, y(t), 0): -3, (1, y(t), 1): 2}, - 'is_linear': True, 'no_of_equation': 2, 'order': {x(t): 1, y(t): 1}, 'type_of_equation': 'type1'} + >>> classify_sysode(eq) == {'eq': [-12*x(t) + 6*y(t) + 5*Derivative(x(t), t), -11*x(t) - 3*y(t) + 2*Derivative(y(t), t)], 'func': [x(t), y(t)], 'func_coeff': {(0, x(t), 0): -12, (0, x(t), 1): 5, (0, y(t), 0): 6, (0, y(t), 1): 0, (1, x(t), 0): -11, (1, x(t), 1): 0, (1, y(t), 0): -3, (1, y(t), 1): 2}, 'is_linear': True, 'no_of_equation': 2, 'order': {x(t): 1, y(t): 1}, 'type_of_equation': 'type1'} + True >>> eq = (Eq(diff(x(t),t), 5*t*x(t) + t**2*y(t)), Eq(diff(y(t),t), -t**2*x(t) + 5*t*y(t))) - >>> classify_sysode(eq) - {'eq': [-t**2*y(t) - 5*t*x(t) + Derivative(x(t), t), t**2*x(t) - 5*t*y(t) + Derivative(y(t), t)], - 'func': [x(t), y(t)], 'func_coeff': {(0, x(t), 0): -5*t, (0, x(t), 1): 1, (0, y(t), 0): -t**2, - (0, y(t), 1): 0, (1, x(t), 0): t**2, (1, x(t), 1): 0, (1, y(t), 0): -5*t, (1, y(t), 1): 1}, - 'is_linear': True, 'no_of_equation': 2, 'order': {x(t): 1, y(t): 1}, 'type_of_equation': 'type4'} - + >>> classify_sysode(eq) == {'eq': [-t**2*y(t) - 5*t*x(t) + Derivative(x(t), t), t**2*x(t) - 5*t*y(t) + Derivative(y(t), t)], 'func': [x(t), y(t)], 'func_coeff': {(0, x(t), 0): -5*t, (0, x(t), 1): 1, (0, y(t), 0): -t**2, (0, y(t), 1): 0, (1, x(t), 0): t**2, (1, x(t), 1): 0, (1, y(t), 0): -5*t, (1, y(t), 1): 1}, 'is_linear': True, 'no_of_equation': 2, 'order': {x(t): 1, y(t): 1}, 'type_of_equation': 'type4'} + True """ # Sympify equations and convert iterables of equations into @@ -1989,7 +1983,7 @@ def odesimp(eq, func, order, constants, hint): >>> eq = dsolve(x*f(x).diff(x) - f(x) - x*sin(f(x)/x), f(x), ... hint='1st_homogeneous_coeff_subs_indep_div_dep_Integral', ... simplify=False) - >>> pprint(eq, wrap_line=False) + >>> pprint(eq, wrap_line=False, use_unicode=False) x ---- f(x) @@ -2007,7 +2001,7 @@ def odesimp(eq, func, order, constants, hint): / >>> pprint(odesimp(eq, f(x), 1, {C1}, - ... hint='1st_homogeneous_coeff_subs_indep_div_dep' + ... hint='1st_homogeneous_coeff_subs_indep_div_dep', use_unicode=False ... )) #doctest: +SKIP x --------- = C1 @@ -2711,10 +2705,10 @@ def constant_renumber(expr, symbolname, startnumber, endnumber): C0 + 2*C1 + C2 >>> constant_renumber(C0 + 2*C1 + C2, 'C', 0, 1) C1 + 3*C2 - >>> pprint(C2 + C1*x + C3*x**2) + >>> pprint(C2 + C1*x + C3*x**2, use_unicode=False) 2 C1*x + C2 + C3*x - >>> pprint(constant_renumber(C2 + C1*x + C3*x**2, 'C', 1, 3)) + >>> pprint(constant_renumber(C2 + C1*x + C3*x**2, 'C', 1, 3), use_unicode=False) 2 C1 + C2*x + C3*x @@ -2823,7 +2817,7 @@ def ode_1st_exact(eq, func, order, match): >>> x, y, t, x0, y0, C1= symbols('x,y,t,x0,y0,C1') >>> P, Q, F= map(Function, ['P', 'Q', 'F']) >>> pprint(Eq(Eq(F(x, y), Integral(P(t, y), (t, x0, x)) + - ... Integral(Q(x0, t), (t, y0, y))), C1)) + ... Integral(Q(x0, t), (t, y0, y))), C1), use_unicode=False) x y / / | | @@ -2897,7 +2891,7 @@ def ode_1st_homogeneous_coeff_best(eq, func, order, match): >>> from sympy.abc import x >>> f = Function('f') >>> pprint(dsolve(2*x*f(x) + (x**2 + f(x)**2)*f(x).diff(x), f(x), - ... hint='1st_homogeneous_coeff_best', simplify=False)) + ... hint='1st_homogeneous_coeff_best', simplify=False), use_unicode=False) / 2 \ | 3*x | log|----- + 1| @@ -2966,12 +2960,12 @@ def ode_1st_homogeneous_coeff_subs_dep_div_indep(eq, func, order, match): >>> from sympy.abc import x >>> f, g, h = map(Function, ['f', 'g', 'h']) >>> genform = g(f(x)/x) + h(f(x)/x)*f(x).diff(x) - >>> pprint(genform) + >>> pprint(genform, use_unicode=False) /f(x)\ /f(x)\ d g|----| + h|----|*--(f(x)) \ x / \ x / dx >>> pprint(dsolve(genform, f(x), - ... hint='1st_homogeneous_coeff_subs_dep_div_indep_Integral')) + ... hint='1st_homogeneous_coeff_subs_dep_div_indep_Integral'), use_unicode=False) f(x) ---- x @@ -2996,7 +2990,7 @@ def ode_1st_homogeneous_coeff_subs_dep_div_indep(eq, func, order, match): >>> from sympy.abc import x >>> f = Function('f') >>> pprint(dsolve(2*x*f(x) + (x**2 + f(x)**2)*f(x).diff(x), f(x), - ... hint='1st_homogeneous_coeff_subs_dep_div_indep', simplify=False)) + ... hint='1st_homogeneous_coeff_subs_dep_div_indep', simplify=False), use_unicode=False) / 3 \ |3*f(x) f (x)| log|------ + -----| @@ -3059,12 +3053,12 @@ def ode_1st_homogeneous_coeff_subs_indep_div_dep(eq, func, order, match): >>> from sympy.abc import x >>> f, g, h = map(Function, ['f', 'g', 'h']) >>> genform = g(x/f(x)) + h(x/f(x))*f(x).diff(x) - >>> pprint(genform) + >>> pprint(genform, use_unicode=False) / x \ / x \ d g|----| + h|----|*--(f(x)) \f(x)/ \f(x)/ dx >>> pprint(dsolve(genform, f(x), - ... hint='1st_homogeneous_coeff_subs_indep_div_dep_Integral')) + ... hint='1st_homogeneous_coeff_subs_indep_div_dep_Integral'), use_unicode=False) x ---- f(x) @@ -3092,7 +3086,7 @@ def ode_1st_homogeneous_coeff_subs_indep_div_dep(eq, func, order, match): >>> f = Function('f') >>> pprint(dsolve(2*x*f(x) + (x**2 + f(x)**2)*f(x).diff(x), f(x), ... hint='1st_homogeneous_coeff_subs_indep_div_dep', - ... simplify=False)) + ... simplify=False), use_unicode=False) / 2 \ | 3*x | log|----- + 1| @@ -3237,11 +3231,11 @@ def ode_1st_linear(eq, func, order, match): >>> from sympy.abc import x >>> f, P, Q = map(Function, ['f', 'P', 'Q']) >>> genform = Eq(f(x).diff(x) + P(x)*f(x), Q(x)) - >>> pprint(genform) + >>> pprint(genform, use_unicode=False) d P(x)*f(x) + --(f(x)) = Q(x) dx - >>> pprint(dsolve(genform, f(x), hint='1st_linear_Integral')) + >>> pprint(dsolve(genform, f(x), hint='1st_linear_Integral'), use_unicode=False) / / \ | | | | | / | / @@ -3259,7 +3253,7 @@ def ode_1st_linear(eq, func, order, match): >>> f = Function('f') >>> pprint(dsolve(Eq(x*diff(f(x), x) - f(x), x**2*sin(x)), - ... f(x), '1st_linear')) + ... f(x), '1st_linear'), use_unicode=False) f(x) = x*(C1 - cos(x)) References @@ -3298,11 +3292,11 @@ def ode_Bernoulli(eq, func, order, match): >>> from sympy.abc import x, n >>> f, P, Q = map(Function, ['f', 'P', 'Q']) >>> genform = Eq(f(x).diff(x) + P(x)*f(x), Q(x)*f(x)**n) - >>> pprint(genform) + >>> pprint(genform, use_unicode=False) d n P(x)*f(x) + --(f(x)) = Q(x)*f (x) dx - >>> pprint(dsolve(genform, f(x), hint='Bernoulli_Integral')) #doctest: +SKIP + >>> pprint(dsolve(genform, f(x), hint='Bernoulli_Integral'), use_unicode=False) #doctest: +SKIP 1 ---- 1 - n @@ -3322,7 +3316,7 @@ def ode_Bernoulli(eq, func, order, match): :py:meth:`~sympy.solvers.ode.ode_separable`). >>> pprint(dsolve(Eq(f(x).diff(x) + P(x)*f(x), Q(x)*f(x)), f(x), - ... hint='separable_Integral')) + ... hint='separable_Integral'), use_unicode=False) f(x) / | / @@ -3341,7 +3335,7 @@ def ode_Bernoulli(eq, func, order, match): >>> f = Function('f') >>> pprint(dsolve(Eq(x*f(x).diff(x) + f(x), log(x)*f(x)**2), - ... f(x), hint='Bernoulli')) + ... f(x), hint='Bernoulli'), use_unicode=False) 1 f(x) = ------------------- / log(x) 1\ @@ -3387,7 +3381,7 @@ def ode_Riccati_special_minus2(eq, func, order, match): >>> y = f(x) >>> genform = a*y.diff(x) - (b*y**2 + c*y/x + d/x**2) >>> sol = dsolve(genform, y) - >>> pprint(sol, wrap_line=False) + >>> pprint(sol, wrap_line=False, use_unicode=False) / / __________________ \\ | __________________ | / 2 || | / 2 | \/ 4*b*d - (a + c) *log(x)|| @@ -3433,13 +3427,13 @@ def ode_Liouville(eq, func, order, match): >>> f, g, h = map(Function, ['f', 'g', 'h']) >>> genform = Eq(diff(f(x),x,x) + g(f(x))*diff(f(x),x)**2 + ... h(x)*diff(f(x),x), 0) - >>> pprint(genform) + >>> pprint(genform, use_unicode=False) 2 2 /d \ d d g(f(x))*|--(f(x))| + h(x)*--(f(x)) + ---(f(x)) = 0 \dx / dx 2 dx - >>> pprint(dsolve(genform, f(x), hint='Liouville_Integral')) + >>> pprint(dsolve(genform, f(x), hint='Liouville_Integral'), use_unicode=False) f(x) / / | | @@ -3459,7 +3453,7 @@ def ode_Liouville(eq, func, order, match): >>> from sympy.abc import x >>> f = Function('f') >>> pprint(dsolve(diff(f(x), x, x) + diff(f(x), x)**2/f(x) + - ... diff(f(x), x)/x, f(x), hint='Liouville')) + ... diff(f(x), x)/x, f(x), hint='Liouville'), use_unicode=False) ________________ ________________ [f(x) = -\/ C1 + C2*log(x) , f(x) = \/ C1 + C2*log(x) ] @@ -3510,7 +3504,7 @@ def ode_2nd_power_series_ordinary(eq, func, order, match): >>> from sympy.abc import x, y >>> f = Function("f") >>> eq = f(x).diff(x, 2) + f(x) - >>> pprint(dsolve(eq, hint='2nd_power_series_ordinary')) + >>> pprint(dsolve(eq, hint='2nd_power_series_ordinary'), use_unicode=False) / 4 2 \ / 2 \ |x x | | x | / 6\ f(x) = C2*|-- - -- + 1| + C1*x*|- -- + 1| + O\x / @@ -3674,7 +3668,7 @@ def ode_2nd_power_series_regular(eq, func, order, match): >>> from sympy.abc import x, y >>> f = Function("f") >>> eq = x*(f(x).diff(x, 2)) + 2*(f(x).diff(x)) + x*f(x) - >>> pprint(dsolve(eq)) + >>> pprint(dsolve(eq), use_unicode=False) / 6 4 2 \ | x x x | / 4 2 \ C1*|- --- + -- - -- + 1| @@ -3834,8 +3828,8 @@ def _nth_linear_match(eq, func, order): >>> f = Function('f') >>> _nth_linear_match(f(x).diff(x, 3) + 2*f(x).diff(x) + ... x*f(x).diff(x, 2) + cos(x)*f(x).diff(x) + x - f(x) - - ... sin(x), f(x), 3) - {-1: x - sin(x), 0: -1, 1: cos(x) + 2, 2: x, 3: 1} + ... sin(x), f(x), 3) == {-1: x - sin(x), 0: -1, 1: cos(x) + 2, 2: x, 3: 1} + True >>> _nth_linear_match(f(x).diff(x, 3) + 2*f(x).diff(x) + ... x*f(x).diff(x, 2) + cos(x)*f(x).diff(x) + x - f(x) - ... sin(f(x)), f(x), 3) == None @@ -3911,7 +3905,7 @@ def ode_nth_linear_euler_eq_homogeneous(eq, func, order, match, returns='sol'): >>> f = Function('f') >>> eq = f(x).diff(x, 2)*x**2 - 4*f(x).diff(x)*x + 6*f(x) >>> pprint(dsolve(eq, f(x), - ... hint='nth_linear_euler_eq_homogeneous')) + ... hint='nth_linear_euler_eq_homogeneous'), use_unicode=False) 2 f(x) = x *(C1 + C2*x) @@ -4152,11 +4146,11 @@ def ode_almost_linear(eq, func, order, match): >>> from sympy.abc import x, y, n >>> f, g, k, l = map(Function, ['f', 'g', 'k', 'l']) >>> genform = Eq(f(x)*(l(y).diff(y)) + k(x)*l(y) + g(x)) - >>> pprint(genform) + >>> pprint(genform, use_unicode=False) d f(x)*--(l(y)) + g(x) + k(x)*l(y) = 0 dy - >>> pprint(dsolve(genform, hint = 'almost_linear')) + >>> pprint(dsolve(genform, hint = 'almost_linear'), use_unicode=False) / // -y*g(x) \\ | || -------- for k(x) = 0|| | || f(x) || -y*k(x) @@ -4185,7 +4179,7 @@ def ode_almost_linear(eq, func, order, match): >>> eq = x*d + x*f(x) + 1 >>> dsolve(eq, f(x), hint='almost_linear') Eq(f(x), (C1 - Ei(x))*exp(-x)) - >>> pprint(dsolve(eq, f(x), hint='almost_linear')) + >>> pprint(dsolve(eq, f(x), hint='almost_linear'), use_unicode=False) -x f(x) = (C1 - Ei(x))*e @@ -4321,7 +4315,7 @@ def ode_linear_coefficients(eq, func, order, match): >>> eq = (x + f(x) + 1)*df + (f(x) - 6*x + 1) >>> dsolve(eq, hint='linear_coefficients') [Eq(f(x), -x - sqrt(C1 + 7*x**2) - 1), Eq(f(x), -x + sqrt(C1 + 7*x**2) - 1)] - >>> pprint(dsolve(eq, hint='linear_coefficients')) + >>> pprint(dsolve(eq, hint='linear_coefficients'), use_unicode=False) ___________ ___________ / 2 / 2 [f(x) = -x - \/ C1 + 7*x - 1, f(x) = -x + \/ C1 + 7*x - 1] @@ -4355,12 +4349,12 @@ def ode_separable_reduced(eq, func, order, match): >>> from sympy.abc import x, n >>> f, g = map(Function, ['f', 'g']) >>> genform = f(x).diff(x) + (f(x)/x)*g(x**n*f(x)) - >>> pprint(genform) + >>> pprint(genform, use_unicode=False) / n \ d f(x)*g\x *f(x)/ --(f(x)) + --------------- dx x - >>> pprint(dsolve(genform, hint='separable_reduced')) + >>> pprint(dsolve(genform, hint='separable_reduced'), use_unicode=False) n x *f(x) / @@ -4386,7 +4380,7 @@ def ode_separable_reduced(eq, func, order, match): >>> eq = (x - x**2*f(x))*d - f(x) >>> dsolve(eq, hint='separable_reduced') [Eq(f(x), (-sqrt(C1*x**2 + 1) + 1)/x), Eq(f(x), (sqrt(C1*x**2 + 1) + 1)/x)] - >>> pprint(dsolve(eq, hint='separable_reduced')) + >>> pprint(dsolve(eq, hint='separable_reduced'), use_unicode=False) ___________ ___________ / 2 / 2 - \/ C1*x + 1 + 1 \/ C1*x + 1 + 1 @@ -4439,7 +4433,7 @@ def ode_1st_power_series(eq, func, order, match): >>> from sympy.abc import x >>> f = Function('f') >>> eq = exp(x)*(f(x).diff(x)) - f(x) - >>> pprint(dsolve(eq, hint='1st_power_series')) + >>> pprint(dsolve(eq, hint='1st_power_series'), use_unicode=False) 3 4 5 C1*x C1*x C1*x / 6\ f(x) = C1 + C1*x - ----- + ----- + ----- + O\x / @@ -4551,7 +4545,7 @@ def ode_nth_linear_constant_coeff_homogeneous(eq, func, order, match, >>> f = Function('f') >>> pprint(dsolve(f(x).diff(x, 4) + 2*f(x).diff(x, 3) - ... 2*f(x).diff(x, 2) - 6*f(x).diff(x) + 5*f(x), f(x), - ... hint='nth_linear_constant_coeff_homogeneous')) + ... hint='nth_linear_constant_coeff_homogeneous'), use_unicode=False) x -2*x f(x) = (C1 + C2*x)*e + (C3*sin(x) + C4*cos(x))*e @@ -4684,7 +4678,7 @@ def ode_nth_linear_constant_coeff_undetermined_coefficients(eq, func, order, mat >>> f = Function('f') >>> pprint(dsolve(f(x).diff(x, 2) + 2*f(x).diff(x) + f(x) - ... 4*exp(-x)*x**2 + cos(2*x), f(x), - ... hint='nth_linear_constant_coeff_undetermined_coefficients')) + ... hint='nth_linear_constant_coeff_undetermined_coefficients'), use_unicode=False) / 4\ | x | -x 4*sin(2*x) 3*cos(2*x) f(x) = |C1 + C2*x + --|*e - ---------- + ---------- @@ -4841,8 +4835,8 @@ def _undetermined_coefficients_match(expr, x): >>> from sympy import log, exp >>> from sympy.solvers.ode import _undetermined_coefficients_match >>> from sympy.abc import x - >>> _undetermined_coefficients_match(9*x*exp(x) + exp(-x), x) - {'test': True, 'trialset': set([x*exp(x), exp(-x), exp(x)])} + >>> _undetermined_coefficients_match(9*x*exp(x) + exp(-x), x) == {'test': True, 'trialset': {x*exp(x), exp(-x), exp(x)}} + True >>> _undetermined_coefficients_match(log(x), x) {'test': False} @@ -5007,7 +5001,7 @@ def ode_nth_linear_constant_coeff_variation_of_parameters(eq, func, order, match >>> f = Function('f') >>> pprint(dsolve(f(x).diff(x, 3) - 3*f(x).diff(x, 2) + ... 3*f(x).diff(x) - f(x) - exp(x)*log(x), f(x), - ... hint='nth_linear_constant_coeff_variation_of_parameters')) + ... hint='nth_linear_constant_coeff_variation_of_parameters'), use_unicode=False) / 3 \ | 2 x *(6*log(x) - 11)| x f(x) = |C1 + C2*x + C3*x + ------------------|*e @@ -5108,11 +5102,11 @@ def ode_separable(eq, func, order, match): >>> from sympy.abc import x >>> a, b, c, d, f = map(Function, ['a', 'b', 'c', 'd', 'f']) >>> genform = Eq(a(x)*b(f(x))*f(x).diff(x), c(x)*d(f(x))) - >>> pprint(genform) + >>> pprint(genform, use_unicode=False) d a(x)*b(f(x))*--(f(x)) = c(x)*d(f(x)) dx - >>> pprint(dsolve(genform, f(x), hint='separable_Integral')) + >>> pprint(dsolve(genform, f(x), hint='separable_Integral'), use_unicode=False) f(x) / / | | @@ -5129,7 +5123,7 @@ def ode_separable(eq, func, order, match): >>> from sympy.abc import x >>> f = Function('f') >>> pprint(dsolve(Eq(f(x)*f(x).diff(x) + x, 3*x*f(x)**2), f(x), - ... hint='separable', simplify=False)) + ... hint='separable', simplify=False), use_unicode=False) / 2 \ 2 log\3*f (x) - 1/ x ---------------- = C1 + -- @@ -5265,7 +5259,7 @@ def ode_lie_group(eq, func, order, match): >>> from sympy.abc import x >>> f = Function('f') >>> pprint(dsolve(f(x).diff(x) + 2*x*f(x) - x*exp(-x**2), f(x), - ... hint='lie_group')) + ... hint='lie_group'), use_unicode=False) / 2\ 2 | x | -x f(x) = |C1 + --|*e @@ -5477,7 +5471,7 @@ def infinitesimals(eq, func=None, order=None, hint='default', match=None): >>> xi = xi(x, y) >>> genform = Eq(eta.diff(x) + (eta.diff(y) - xi.diff(x))*h ... - (xi.diff(y))*h**2 - xi*(h.diff(x)) - eta*(h.diff(y)), 0) - >>> pprint(genform) + >>> pprint(genform, use_unicode=False) /d d \ d 2 d |--(eta(x, y)) - --(xi(x, y))|*h(x, y) - eta(x, y)*--(h(x, y)) - h (x, y)*--(x \dy dx / dy dy @@ -5497,13 +5491,13 @@ def infinitesimals(eq, func=None, order=None, hint='default', match=None): Examples ======== - >>> from sympy import Function, diff + >>> from sympy import Function, diff, exp >>> from sympy.solvers.ode import infinitesimals - >>> from sympy.abc import x + >>> from sympy.abc import x, eta, xi >>> f = Function('f') >>> eq = f(x).diff(x) - x**2*f(x) - >>> infinitesimals(eq) - [{eta(x, f(x)): exp(x**3/3), xi(x, f(x)): 0}] + >>> infinitesimals(eq) == [{eta(x, f(x)): exp(x**3/3), xi(x, f(x)): 0}] + True References ========== diff --git a/sympy/solvers/pde.py b/sympy/solvers/pde.py index d006f7aa9f..52bc6dcd51 100644 --- a/sympy/solvers/pde.py +++ b/sympy/solvers/pde.py @@ -506,12 +506,12 @@ def pde_1st_linear_constant_coeff_homogeneous(eq, func, order, match, solvefun): >>> ux = u.diff(x) >>> uy = u.diff(y) >>> genform = a*ux + b*uy + c*u - >>> pprint(genform) + >>> pprint(genform, use_unicode=False) d d a*--(f(x, y)) + b*--(f(x, y)) + c*f(x, y) dx dy - >>> pprint(pdsolve(genform)) + >>> pprint(pdsolve(genform), use_unicode=False) -c*(a*x + b*y) --------------- 2 2 @@ -529,7 +529,7 @@ def pde_1st_linear_constant_coeff_homogeneous(eq, func, order, match, solvefun): >>> f = Function('f') >>> pdsolve(f(x,y) + f(x,y).diff(x) + f(x,y).diff(y)) Eq(f(x, y), F(x - y)*exp(-x/2 - y/2)) - >>> pprint(pdsolve(f(x,y) + f(x,y).diff(x) + f(x,y).diff(y))) + >>> pprint(pdsolve(f(x,y) + f(x,y).diff(x) + f(x,y).diff(y)), use_unicode=False) x y - - - - 2 2 @@ -577,11 +577,11 @@ def pde_1st_linear_constant_coeff(eq, func, order, match, solvefun): >>> ux = u.diff(x) >>> uy = u.diff(y) >>> genform = a*u + b*ux + c*uy - G(x,y) - >>> pprint(genform) + >>> pprint(genform, use_unicode=False) d d a*f(x, y) + b*--(f(x, y)) + c*--(f(x, y)) - G(x, y) dx dy - >>> pprint(pdsolve(genform, hint='1st_linear_constant_coeff_Integral')) + >>> pprint(pdsolve(genform, hint='1st_linear_constant_coeff_Integral'), use_unicode=False) // b*x + c*y \ || / | || | | @@ -621,7 +621,7 @@ def pde_1st_linear_constant_coeff(eq, func, order, match, solvefun): ======== >>> from sympy.solvers.pde import pdsolve - >>> from sympy import Function, diff, pprint, exp + >>> from sympy import Function, diff, exp >>> from sympy.abc import x,y >>> f = Function('f') >>> eq = -2*f(x,y).diff(x) + 4*f(x,y).diff(y) + 5*f(x,y) - exp(x + 3*y) @@ -689,7 +689,7 @@ def pde_1st_linear_variable_coeff(eq, func, order, match, solvefun): >>> ux = u.diff(x) >>> uy = u.diff(y) >>> genform = a(x, y)*u + b(x, y)*ux + c(x, y)*uy - G(x,y) - >>> pprint(genform) + >>> pprint(genform, use_unicode=False) d d -G(x, y) + a(x, y)*f(x, y) + b(x, y)*--(f(x, y)) + c(x, y)*--(f(x, y)) dx dy diff --git a/sympy/solvers/solvers.py b/sympy/solvers/solvers.py index 76880f6193..86725a93ff 100644 --- a/sympy/solvers/solvers.py +++ b/sympy/solvers/solvers.py @@ -87,17 +87,17 @@ def denoms(eq, symbols=None): >>> from sympy.abc import x, y, z >>> from sympy import sqrt - >>> denoms(x/y) - set([y]) + >>> denoms(x/y) == {y} + True - >>> denoms(x/(y*z)) - set([y, z]) + >>> denoms(x/(y*z)) == {y, z} + True - >>> denoms(3/x + y/z) - set([x, z]) + >>> denoms(3/x + y/z) == {x, z} + True - >>> denoms(x/2 + y/z) - set([2, z]) + >>> denoms(x/2 + y/z) == {2, z} + True """ pot = preorder_traversal(eq) @@ -439,7 +439,7 @@ def solve(f, *symbols, **flags): The output varies according to the input and can be seen by example:: - >>> from sympy import solve, Poly, Eq, Function, exp + >>> from sympy import solve, Poly, Eq, Function, exp, I, sqrt >>> from sympy.abc import x, y, z, a, b >>> f = Function('f') @@ -452,13 +452,13 @@ def solve(f, *symbols, **flags): >>> solve(x - 3, dict=True) [{x: 3}] - >>> solve([x - 3, y - 1], dict=True) - [{x: 3, y: 1}] + >>> solve([x - 3, y - 1], dict=True) == [{x: 3, y: 1}] + True * to get a list of symbols and set of solution(s) use flag set=True - >>> solve([x**2 - 3, y - 1], set=True) - ([x, y], set([(-sqrt(3), 1), (sqrt(3), 1)])) + >>> solve([x**2 - 3, y - 1], set=True) == ([x, y], {(-sqrt(3), 1), (sqrt(3), 1)}) + True * single expression and single symbol that is in the expression @@ -470,10 +470,10 @@ def solve(f, *symbols, **flags): [3] >>> solve(Poly(x - 3), x) [3] - >>> solve(x**2 - y**2, x, set=True) - ([x], set([(-y,), (y,)])) - >>> solve(x**4 - 1, x, set=True) - ([x], set([(-1,), (1,), (-I,), (I,)])) + >>> solve(x**2 - y**2, x, set=True) == ([x], {(-y,), (y,)}) + True + >>> solve(x**4 - 1, x, set=True) == ([x], {(-1,), (1,), (-I,), (I,)}) + True * single expression with no symbol that is in the expression @@ -491,12 +491,12 @@ def solve(f, *symbols, **flags): >>> solve(x - 3) [3] - >>> solve(x**2 - y**2) - [{x: -y}, {x: y}] - >>> solve(z**2*x**2 - z**2*y**2) - [{x: -y}, {x: y}, {z: 0}] - >>> solve(z**2*x - z**2*y**2) - [{x: y**2}, {z: 0}] + >>> solve(x**2 - y**2) == [{x: -y}, {x: y}] + True + >>> solve(z**2*x**2 - z**2*y**2) == [{x: -y}, {x: y}, {z: 0}] + True + >>> solve(z**2*x - z**2*y**2) == [{x: y**2}, {z: 0}] + True * when an object other than a Symbol is given as a symbol, it is isolated algebraically and an implicit solution may be obtained. @@ -511,14 +511,14 @@ def solve(f, *symbols, **flags): [x + f(x)] >>> solve(f(x).diff(x) - f(x) - x, f(x)) [-x + Derivative(f(x), x)] - >>> solve(x + exp(x)**2, exp(x), set=True) - ([exp(x)], set([(-sqrt(-x),), (sqrt(-x),)])) + >>> solve(x + exp(x)**2, exp(x), set=True) == ([exp(x)], {(-sqrt(-x),), (sqrt(-x),)}) + True >>> from sympy import Indexed, IndexedBase, Tuple, sqrt >>> A = IndexedBase('A') >>> eqs = Tuple(A[1] + A[2] - 3, A[1] - A[2] + 1) - >>> solve(eqs, eqs.atoms(Indexed)) - {A[1]: 1, A[2]: 2} + >>> solve(eqs, eqs.atoms(Indexed)) == {A[1]: 1, A[2]: 2} + True * To solve for a *symbol* implicitly, use 'implicit=True': @@ -532,17 +532,17 @@ def solve(f, *symbols, **flags): >>> solve(x + 2 + sqrt(3), x + 2) [-sqrt(3)] - >>> solve((x + 2 + sqrt(3), x + 4 + y), y, x + 2) - {y: -2 + sqrt(3), x + 2: -sqrt(3)} + >>> solve((x + 2 + sqrt(3), x + 4 + y), y, x + 2) == {y: -2 + sqrt(3), x + 2: -sqrt(3)} + True * Nothing heroic is done in this implicit solving so you may end up with a symbol still in the solution: >>> eqs = (x*y + 3*y + sqrt(3), x + 4 + y) - >>> solve(eqs, y, x + 2) - {y: -sqrt(3)/(x + 3), x + 2: (-2*x - 6 + sqrt(3))/(x + 3)} - >>> solve(eqs, y*x, x) - {x: -y - 4, x*y: -3*y - sqrt(3)} + >>> solve(eqs, y, x + 2) == {y: -sqrt(3)/(x + 3), x + 2: (-2*x - 6 + sqrt(3))/(x + 3)} + True + >>> solve(eqs, y*x, x) == {x: -y - 4, x*y: -3*y - sqrt(3)} + True * if you attempt to solve for a number remember that the number you have obtained does not necessarily mean that the value is @@ -570,23 +570,23 @@ def solve(f, *symbols, **flags): * that are linear - >>> solve((a + b)*x - b + 2, a, b) - {a: -2, b: 2} + >>> solve((a + b)*x - b + 2, a, b) == {a: -2, b: 2} + True * that are nonlinear - >>> solve((a + b)*x - b**2 + 2, a, b, set=True) - ([a, b], set([(-sqrt(2), sqrt(2)), (sqrt(2), -sqrt(2))])) + >>> solve((a + b)*x - b**2 + 2, a, b, set=True) == ([a, b], {(-sqrt(2), sqrt(2)), (sqrt(2), -sqrt(2))}) + True * if there is no linear solution then the first successful attempt for a nonlinear solution will be returned - >>> solve(x**2 - y**2, x, y) - [{x: -y}, {x: y}] + >>> solve(x**2 - y**2, x, y) == [{x: -y}, {x: y}] + True >>> solve(x**2 - y**2/exp(x), x, y) [{x: 2*LambertW(y/2)}] - >>> solve(x**2 - y**2/exp(x), y, x) - [{y: -x*sqrt(exp(x))}, {y: x*sqrt(exp(x))}] + >>> solve(x**2 - y**2/exp(x), y, x) == [{y: -x*sqrt(exp(x))}, {y: x*sqrt(exp(x))}] + True * iterable of one or more of the above @@ -603,12 +603,12 @@ def solve(f, *symbols, **flags): >>> solve([x - 3], x) {x: 3} - >>> solve((x + 5*y - 2, -3*x + 6*y - 15), x, y) - {x: -3, y: 1} - >>> solve((x + 5*y - 2, -3*x + 6*y - 15), x, y, z) - {x: -3, y: 1} - >>> solve((x + 5*y - 2, -3*x + 6*y - z), z, x, y) - {x: -5*y + 2, z: 21*y - 6} + >>> solve((x + 5*y - 2, -3*x + 6*y - 15), x, y) == {x: -3, y: 1} + True + >>> solve((x + 5*y - 2, -3*x + 6*y - 15), x, y, z) == {x: -3, y: 1} + True + >>> solve((x + 5*y - 2, -3*x + 6*y - z), z, x, y) == {x: -5*y + 2, z: 21*y - 6} + True * without a solution @@ -617,16 +617,16 @@ def solve(f, *symbols, **flags): * when the system is not linear - >>> solve([x**2 + y -2, y**2 - 4], x, y, set=True) - ([x, y], set([(-2, -2), (0, 2), (2, -2)])) + >>> solve([x**2 + y -2, y**2 - 4], x, y, set=True) == ([x, y], {(-2, -2), (0, 2), (2, -2)}) + True * if no symbols are given, all free symbols will be selected and a list of mappings returned - >>> solve([x - 2, x**2 + y]) - [{x: 2, y: -4}] - >>> solve([x - 2, x**2 + f(x)], {f(x), x}) - [{x: 2, f(x): -4}] + >>> solve([x - 2, x**2 + y]) == [{x: 2, y: -4}] + True + >>> solve([x - 2, x**2 + f(x)], {f(x), x}) == [{x: 2, f(x): -4}] + True * if any equation doesn't depend on the symbol(s) given it will be eliminated from the equation set and an answer may be given @@ -2071,8 +2071,8 @@ def solve_linear_system(system, *symbols, **flags): -2 x + y == 14 >>> system = Matrix(( (1, 4, 2), (-2, 1, 14))) - >>> solve_linear_system(system, x, y) - {x: -6, y: 2} + >>> solve_linear_system(system, x, y) == {x: -6, y: 2} + True A degenerate system returns an empty dictionary. @@ -2249,15 +2249,15 @@ def solve_undetermined_coeffs(equ, coeffs, sym, **flags): SymPy expressions. Specification of parameters and variable is obligatory for efficiency and simplicity reason. - >>> from sympy import Eq + >>> from sympy import Eq, Rational >>> from sympy.abc import a, b, c, x >>> from sympy.solvers import solve_undetermined_coeffs - >>> solve_undetermined_coeffs(Eq(2*a*x + a+b, x), [a, b], x) - {a: 1/2, b: -1/2} + >>> solve_undetermined_coeffs(Eq(2*a*x + a+b, x), [a, b], x) == {a: Rational(1, 2), b: -Rational(1, 2)} + True - >>> solve_undetermined_coeffs(Eq(a*c*x + a+b, x), [a, b], x) - {a: 1/c, b: -1/c} + >>> solve_undetermined_coeffs(Eq(a*c*x + a+b, x), [a, b], x) == {a: 1/c, b: -1/c} + True """ if isinstance(equ, Equality): @@ -2288,15 +2288,16 @@ def solve_linear_system_LU(matrix, syms): Examples ======== - >>> from sympy import Matrix + >>> from sympy import Matrix, Rational >>> from sympy.abc import x, y, z >>> from sympy.solvers.solvers import solve_linear_system_LU >>> solve_linear_system_LU(Matrix([ ... [1, 2, 0, 1], ... [3, 2, 2, 1], - ... [2, 0, 0, 1]]), [x, y, z]) - {x: 1/2, y: 1/4, z: -1/2} + ... [2, 0, 0, 1]]), [x, y, z]) == {x: Rational(1, 2), + ... y: Rational(1, 4), z: -Rational(1, 2)} + True See Also ======== diff --git a/sympy/solvers/solveset.py b/sympy/solvers/solveset.py deleted file mode 100644 index 81d7563eaf..0000000000 --- a/sympy/solvers/solveset.py +++ /dev/null @@ -1,874 +0,0 @@ -""" -This module contains functions to solve a single equation for a single variable. -""" -from __future__ import print_function, division - -from sympy.core.sympify import sympify -from sympy.core import S, Pow, Dummy, pi, Expr, Wild, Mul -from sympy.core.numbers import I, Number, Rational -from sympy.core.function import (Lambda, expand, expand_complex) -from sympy.core.relational import Eq -from sympy.simplify.simplify import fraction, trigsimp -from sympy.functions import (log, Abs, tan, cot, exp, - arg, Piecewise, piecewise_fold) -from sympy.functions.elementary.trigonometric import (TrigonometricFunction, - HyperbolicFunction) -from sympy.sets import FiniteSet, EmptySet, imageset, Union -from sympy.polys import (roots, Poly, degree, together, PolynomialError, - RootOf) -from sympy.solvers.solvers import checksol, denoms -from sympy.utilities import filldedent - -import warnings - - -def invert_real(f_x, y, x): - """ Inverts a real valued function - - Reduces the real valued equation ``f(x) = y`` to a set of equations ``{g(x) - = h_1(y), g(x) = h_2(y), ..., g(x) = h_n(y) }`` where ``g(x)`` is a simpler - function than ``f(x)``. The return value is a tuple ``(g(x), set_h)``, - where ``g(x)`` is a function of ``x`` and ``set_h`` is the set of - functions ``{h_1(y), h_2(y), ..., h_n(y)}``. - Here, ``y`` is not necessarily a symbol. - - Examples - ======== - - >>> from sympy.solvers.solveset import invert_real - >>> from sympy import tan, Abs, exp - >>> from sympy.abc import x, y, n - >>> invert_real(exp(Abs(x)), y, x) - (x, {-log(y), log(y)}) - >>> invert_real(exp(x), 1, x) - (x, {0}) - >>> invert_real(Abs(x**31 + x), y, x) - (x**31 + x, {-y, y}) - >>> invert_real(tan(x), y, x) - (x, ImageSet(Lambda(_n, _n*pi + atan(y)), Integers())) - - See Also - ======== - invert_complex - """ - y = sympify(y) - if not y.has(x): - return _invert_real(f_x, FiniteSet(y), x) - else: - raise ValueError(" y should be independent of x ") - - -def _invert_real(f, g_ys, symbol): - """ Helper function for invert_real """ - - if not f.has(symbol): - raise ValueError("Inverse of constant function doesn't exist") - - if f is symbol: - return (f, g_ys) - - n = Dummy('n') - if hasattr(f, 'inverse') and not isinstance(f, TrigonometricFunction): - if len(f.args) > 1: - raise ValueError("Only functions with one argument are supported.") - return _invert_real(f.args[0], - imageset(Lambda(n, f.inverse()(n)), g_ys), symbol) - - if isinstance(f, Abs): - return _invert_real(f.args[0], - Union(g_ys, imageset(Lambda(n, -n), g_ys)), symbol) - - if f.is_Add: - # f = g + h - g, h = f.as_independent(symbol) - if g != S.Zero: - return _invert_real(h, imageset(Lambda(n, n - g), g_ys), symbol) - - if f.is_Mul: - # f = g*h - g, h = f.as_independent(symbol) - - if g != S.One: - return _invert_real(h, imageset(Lambda(n, n/g), g_ys), symbol) - - if f.is_Pow: - base, expo = f.args - base_has_sym = base.has(symbol) - expo_has_sym = expo.has(symbol) - - if not expo_has_sym: - res = imageset(Lambda(n, Pow(n, 1/expo)), g_ys) - if expo.is_rational: - numer, denom = expo.as_numer_denom() - if numer == S.One or numer == - S.One: - return _invert_real(base, res, symbol) - else: - if numer % 2 == 0: - n = Dummy('n') - neg_res = imageset(Lambda(n, -n), res) - return _invert_real(base, res + neg_res, symbol) - else: - return _invert_real(base, res, symbol) - else: - if not base.is_positive: - raise ValueError("x**w where w is irrational is not" - "defined for negative x") - return _invert_real(base, res, symbol) - - if not base_has_sym: - return _invert_real(expo, imageset(Lambda(n, log(n)/log(base)), - g_ys), symbol) - - if isinstance(f, tan) or isinstance(f, cot): - n = Dummy('n') - if isinstance(g_ys, FiniteSet): - tan_cot_invs = Union(*[imageset(Lambda(n, n*pi + f.inverse()(g_y)), - S.Integers) for g_y in g_ys]) - return _invert_real(f.args[0], tan_cot_invs, symbol) - - return (f, g_ys) - - -def invert_complex(f_x, y, x): - """ Inverts a complex valued function. - - Reduces the complex valued equation ``f(x) = y`` to a set of equations - ``{g(x) = h_1(y), g(x) = h_2(y), ..., g(x) = h_n(y) }`` where ``g(x)`` is - a simpler function than ``f(x)``. The return value is a tuple ``(g(x), - set_h)``, where ``g(x)`` is a function of ``x`` and ``set_h`` is - the set of function ``{h_1(y), h_2(y), ..., h_n(y)}``. - Here, ``y`` is not necessarily a symbol. - - Note that `invert\_complex` and `invert\_real` don't always produce the - same result even for a seemingly simple function like ``exp(x)`` because - the complex extension of real valued ``log`` is multivariate in the complex - system and has infinitely many branches. If you are working with real - values only or you are not sure with function to use you should use - `invert\_real`. - - - Examples - ======== - - >>> from sympy.solvers.solveset import invert_complex - >>> from sympy.abc import x, y - >>> from sympy import exp, log - >>> invert_complex(log(x), y, x) - (x, {exp(y)}) - >>> invert_complex(log(x), 0, x) # Second parameter is not a symbol - (x, {1}) - >>> invert_complex(exp(x), y, x) - (x, ImageSet(Lambda(_n, I*(2*_n*pi + arg(y)) + log(Abs(y))), Integers())) - - See Also - ======== - invert_real - """ - y = sympify(y) - if not y.has(x): - return _invert_complex(f_x, FiniteSet(y), x) - else: - raise ValueError(" y should be independent of x ") - - -def _invert_complex(f, g_ys, symbol): - """ Helper function for invert_complex """ - - if not f.has(symbol): - raise ValueError("Inverse of constant function doesn't exist") - - if f is symbol: - return (f, g_ys) - - n = Dummy('n') - if f.is_Add: - # f = g + h - g, h = f.as_independent(symbol) - if g != S.Zero: - return _invert_complex(h, imageset(Lambda(n, n - g), g_ys), symbol) - - if f.is_Mul: - # f = g*h - g, h = f.as_independent(symbol) - - if g != S.One: - return _invert_complex(h, imageset(Lambda(n, n/g), g_ys), symbol) - - if hasattr(f, 'inverse') and \ - not isinstance(f, TrigonometricFunction) and \ - not isinstance(f, HyperbolicFunction) and \ - not isinstance(f, exp): - if len(f.args) > 1: - raise ValueError("Only functions with one argument are supported.") - return _invert_complex(f.args[0], - imageset(Lambda(n, f.inverse()(n)), g_ys), symbol) - - if isinstance(f, exp): - if isinstance(g_ys, FiniteSet): - exp_invs = Union(*[imageset(Lambda(n, I*(2*n*pi + arg(g_y)) + - log(Abs(g_y))), S.Integers) - for g_y in g_ys]) - return _invert_complex(f.args[0], exp_invs, symbol) - return (f, g_ys) - - -def domain_check(f, symbol, p): - """Returns False if point p is infinite or any subexpression of f - is infinite or becomes so after replacing symbol with p. If none of - these conditions is met then True will be returned. - - Examples - ======== - - >>> from sympy import Mul, oo - >>> from sympy.abc import x - >>> from sympy.solvers.solveset import domain_check - >>> g = 1/(1 + (1/(x + 1))**2) - >>> domain_check(g, x, -1) - False - >>> domain_check(x**2, x, 0) - True - >>> domain_check(1/x, x, oo) - False - - * The function relies on the assumption that the original form - of the equation has not been changed by automatic simplification. - - >>> domain_check(x/x, x, 0) # x/x is automatically simplified to 1 - True - - * To deal with automatic evaluations use evaluate=False: - - >>> domain_check(Mul(x, 1/x, evaluate=False), x, 0) - False - """ - f, p = sympify(f), sympify(p) - if p.is_infinite: - return False - return _domain_check(f, symbol, p) - - -def _domain_check(f, symbol, p): - # helper for domain check - if f.is_Atom and f.is_finite: - return True - elif f.subs(symbol, p).is_infinite: - return False - else: - return all([_domain_check(g, symbol, p) - for g in f.args]) - - -def _is_finite_with_finite_vars(f): - """ - Return True if the given expression is finite when all free symbols - (that are not already specified as finite) are made finite. - """ - reps = {s: Dummy(s.name, finite=True, **s.assumptions0) - for s in f.free_symbols if s.is_finite is None} - return f.xreplace(reps).is_finite - - -def _is_function_class_equation(func_class, f, symbol): - """ Tests whether the equation is an equation of the given function class. - - The given equation belongs to the given function class if it is - comprised of functions of the function class which are multiplied by - or added to expressions independent of the symbol. In addition, the - arguments of all such functions must be linear in the symbol as well. - - Examples - ======== - - >>> from sympy.solvers.solveset import _is_function_class_equation - >>> from sympy import tan, sin, tanh, sinh, exp - >>> from sympy.abc import x - >>> from sympy.functions.elementary.trigonometric import (TrigonometricFunction, - ... HyperbolicFunction) - >>> _is_function_class_equation(TrigonometricFunction, exp(x) + tan(x), x) - False - >>> _is_function_class_equation(TrigonometricFunction, tan(x) + sin(x), x) - True - >>> _is_function_class_equation(TrigonometricFunction, tan(x**2), x) - False - >>> _is_function_class_equation(TrigonometricFunction, tan(x + 2), x) - True - >>> _is_function_class_equation(HyperbolicFunction, tanh(x) + sinh(x), x) - True - """ - if f.is_Mul or f.is_Add: - return all(_is_function_class_equation(func_class, arg, symbol) - for arg in f.args) - - if f.is_Pow: - if not f.exp.has(symbol): - return _is_function_class_equation(func_class, f.base, symbol) - else: - return False - - if not f.has(symbol): - return True - - if isinstance(f, func_class): - try: - g = Poly(f.args[0], symbol) - return g.degree() <= 1 - except PolynomialError: - return False - else: - return False - - -def solveset_real(f, symbol): - """ Solves a real valued equation. - - Parameters - ========== - - f : Expr - The target equation - symbol : Symbol - The variable for which the equation is solved - - Returns - ======= - - Set - A set of values for `symbol` for which `f` is equal to - zero. An `EmptySet` is returned if no solution is found. - - `solveset_real` claims to be complete in the set of the solution it - returns. - - Raises - ====== - - NotImplementedError - The algorithms for to find the solution of the given equation are - not yet implemented. - ValueError - The input is not valid. - RuntimeError - It is a bug, please report to the github issue tracker. - - - See Also - ======= - - solveset_complex : solver for complex domain - - Examples - ======== - - >>> from sympy import Symbol, exp, sin, sqrt, I - >>> from sympy.solvers.solveset import solveset_real - >>> x = Symbol('x', extended_real=True) - >>> a = Symbol('a', extended_real=True, finite=True, positive=True) - >>> solveset_real(x**2 - 1, x) - {-1, 1} - >>> solveset_real(sqrt(5*x + 6) - 2 - x, x) - {-1, 2} - >>> solveset_real(x - I, x) - EmptySet() - >>> solveset_real(x - a, x) - {a} - >>> solveset_real(exp(x) - a, x) - {log(a)} - - * In case the equation has infinitely many solutions an infinitely indexed - `ImageSet` is returned. - - >>> solveset_real(sin(x) - 1, x) - ImageSet(Lambda(_n, 2*_n*pi + pi/2), Integers()) - - * If the equation is true for any arbitrary value of the symbol a `S.Reals` - set is returned. - - >>> solveset_real(x - x, x) - (-oo, oo) - - """ - if not symbol.is_Symbol: - raise ValueError(" %s is not a symbol" % (symbol)) - - f = sympify(f) - if not isinstance(f, (Expr, Number)): - raise ValueError(" %s is not a valid sympy expression" % (f)) - - original_eq = f - f = together(f) - - if f.has(Piecewise): - f = piecewise_fold(f) - result = EmptySet() - - if f.expand().is_zero: - return S.Reals - elif not f.has(symbol): - return EmptySet() - elif f.is_Mul and all([_is_finite_with_finite_vars(m) for m in f.args]): - # if f(x) and g(x) are both finite we can say that the solution of - # f(x)*g(x) == 0 is same as Union(f(x) == 0, g(x) == 0) is not true in - # general. g(x) can grow to infinitely large for the values where - # f(x) == 0. To be sure that we not are silently allowing any - # wrong solutions we are using this technique only if both f and g and - # finite for a finite input. - result = Union(*[solveset_real(m, symbol) for m in f.args]) - elif _is_function_class_equation(TrigonometricFunction, f, symbol) or \ - _is_function_class_equation(HyperbolicFunction, f, symbol): - result = _solve_real_trig(f, symbol) - elif f.is_Piecewise: - result = EmptySet() - expr_set_pairs = f.as_expr_set_pairs() - for (expr, in_set) in expr_set_pairs: - solns = solveset_real(expr, symbol).intersect(in_set) - result = result + solns - else: - lhs, rhs_s = invert_real(f, 0, symbol) - if lhs == symbol: - result = rhs_s - elif isinstance(rhs_s, FiniteSet): - equations = [lhs - rhs for rhs in rhs_s] - for equation in equations: - if equation == f: - if any(_has_rational_power(g, symbol)[0] - for g in equation.args): - result += _solve_radical(equation, - symbol, - solveset_real) - elif equation.has(Abs): - result += _solve_abs(f, symbol) - else: - result += _solve_as_rational(equation, symbol, - solveset_solver=solveset_real, - as_poly_solver=_solve_as_poly_real) - else: - result += solveset_real(equation, symbol) - else: - raise NotImplementedError - - if isinstance(result, FiniteSet): - result = [s for s in result - if isinstance(s, RootOf) - or domain_check(original_eq, symbol, s)] - return FiniteSet(*result).intersect(S.Reals) - else: - return result.intersect(S.Reals) - - -def _solve_as_rational(f, symbol, solveset_solver, as_poly_solver): - """ solve rational functions""" - f = together(f, deep=True) - g, h = fraction(f) - if not h.has(symbol): - return as_poly_solver(g, symbol) - else: - valid_solns = solveset_solver(g, symbol) - invalid_solns = solveset_solver(h, symbol) - return valid_solns - invalid_solns - - -def _solve_real_trig(f, symbol): - """ Helper to solve trigonometric equations """ - f = trigsimp(f) - f = f.rewrite(exp) - f = together(f) - g, h = fraction(f) - y = Dummy('y') - g, h = g.expand(), h.expand() - g, h = g.subs(exp(I*symbol), y), h.subs(exp(I*symbol), y) - if g.has(symbol) or h.has(symbol): - raise NotImplementedError - - solns = solveset_complex(g, y) - solveset_complex(h, y) - - if isinstance(solns, FiniteSet): - return Union(*[invert_complex(exp(I*symbol), s, symbol)[1] - for s in solns]) - elif solns is S.EmptySet: - return S.EmptySet - else: - raise NotImplementedError - - -def _solve_as_poly(f, symbol, solveset_solver, invert_func): - """ - Solve the equation using polynomial techniques if it already is a - polynomial equation or, with a change of variables, can be made so. - """ - result = None - if f.is_polynomial(symbol): - - solns = roots(f, symbol, cubics=True, quartics=True, - quintics=True, domain='EX') - num_roots = sum(solns.values()) - if degree(f, symbol) <= num_roots: - result = FiniteSet(*solns.keys()) - else: - poly = Poly(f, symbol) - solns = poly.all_roots() - if poly.degree() <= len(solns): - result = FiniteSet(*solns) - else: - raise NotImplementedError("Couldn't find all roots " - "of the equation %s" % f) - else: - poly = Poly(f) - if poly is None: - raise NotImplementedError("Could not convert %s to Poly" % f) - gens = [g for g in poly.gens if g.has(symbol)] - - if len(gens) == 1: - poly = Poly(poly, gens[0]) - gen = poly.gen - deg = poly.degree() - poly = Poly(poly.as_expr(), poly.gen, composite=True) - poly_solns = FiniteSet(*roots(poly, cubics=True, quartics=True, - quintics=True).keys()) - - if len(poly_solns) < deg: - raise NotImplementedError("Couldn't find all the roots of " - "the equation %s" % f) - - if gen != symbol: - y = Dummy('y') - lhs, rhs_s = invert_func(gen, y, symbol) - if lhs is symbol: - result = Union(*[rhs_s.subs(y, s) for s in poly_solns]) - else: - raise NotImplementedError( - "inversion of %s not handled" % gen) - else: - raise NotImplementedError("multiple generators not handled" - " by solveset") - - if result is not None: - if isinstance(result, FiniteSet): - # this is to simplify solutions like -sqrt(-I) to sqrt(2)/2 - # - sqrt(2)*I/2. We are not expanding for solution with free - # variables because that makes the solution more complicated. For - # example expand_complex(a) returns re(a) + I*im(a) - if all([s.free_symbols == set() and not isinstance(s, RootOf) - for s in result]): - s = Dummy('s') - result = imageset(Lambda(s, expand_complex(s)), result) - return result - else: - raise NotImplementedError - - -def _solve_as_poly_real(f, symbol): - """ - Solve real valued equation with methods to solve polynomial - equations. - """ - return _solve_as_poly(f, symbol, - solveset_solver=solveset_real, - invert_func=invert_real) - - -def _solve_as_poly_complex(f, symbol): - """ - Solve complex valued equation with methods to solve polynomial - equations. - """ - return _solve_as_poly(f, symbol, - solveset_solver=solveset_complex, - invert_func=invert_complex) - - -def _has_rational_power(expr, symbol): - """ - Returns (bool, den) where bool is True if the term has a - non-integer rational power and den is the denominator of the - expression's exponent. - - Examples - ======== - - >>> from sympy.solvers.solveset import _has_rational_power - >>> from sympy import sqrt - >>> from sympy.abc import x - >>> _has_rational_power(sqrt(x), x) - (True, 2) - >>> _has_rational_power(x**2, x) - (False, 1) - """ - a, p, q = Wild('a'), Wild('p'), Wild('q') - pattern_match = expr.match(a*p**q) - if pattern_match is None or pattern_match[a] is S.Zero: - return (False, S.One) - elif p not in pattern_match.keys() or a not in pattern_match.keys(): - return (False, S.One) - elif isinstance(pattern_match[q], Rational) \ - and pattern_match[p].has(symbol): - if not pattern_match[q].q == S.One: - return (True, pattern_match[q].q) - - if not isinstance(pattern_match[a], Pow) \ - or isinstance(pattern_match[a], Mul): - return (False, S.One) - else: - return _has_rational_power(pattern_match[a], symbol) - - -def _solve_radical(f, symbol, solveset_solver): - """ Helper function to solve equations with radicals """ - from sympy.solvers.solvers import unrad - eq, cov = unrad(f) - if not cov: - result = solveset_solver(eq, symbol) - \ - Union(*[solveset_solver(g, symbol) for g in denoms(f, [symbol])]) - else: - y, yeq = cov - if not solveset_solver(y - I, y): - yreal = Dummy('yreal', extended_real=True) - yeq = yeq.xreplace({y: yreal}) - eq = eq.xreplace({y: yreal}) - y = yreal - g_y_s = solveset_solver(yeq, symbol) - f_y_sols = solveset_solver(eq, y) - result = Union(*[imageset(Lambda(y, g_y), f_y_sols) - for g_y in g_y_s]) - - return FiniteSet(*[s for s in result if checksol(f, symbol, s) is True]) - - -def _solve_abs(f, symbol): - """ Helper function to solve equation involving absolute value function """ - from sympy.solvers.inequalities import solve_univariate_inequality - assert f.has(Abs) - p, q, r = Wild('p'), Wild('q'), Wild('r') - pattern_match = f.match(p*Abs(q) + r) - if not pattern_match[p].is_zero: - f_p, f_q, f_r = pattern_match[p], pattern_match[q], pattern_match[r] - q_pos_cond = solve_univariate_inequality(f_q >= 0, symbol, - relational=False) - q_neg_cond = solve_univariate_inequality(f_q < 0, symbol, - relational=False) - - sols_q_pos = solveset_real(f_p*f_q + f_r, - symbol).intersect(q_pos_cond) - sols_q_neg = solveset_real(f_p*(-f_q) + f_r, - symbol).intersect(q_neg_cond) - return Union(sols_q_pos, sols_q_neg) - else: - raise NotImplementedError - - -def solveset_complex(f, symbol): - """ Solve a complex valued equation. - - Parameters - ========== - - f : Expr - The target equation - symbol : Symbol - The variable for which the equation is solved - - Returns - ======= - - Set - A set of values for `symbol` for which `f` equal to - zero. An `EmptySet` is returned if no solution is found. - - `solveset_complex` claims to be complete in the solution set that - it returns. - - Raises - ====== - - NotImplementedError - The algorithms for to find the solution of the given equation are - not yet implemented. - ValueError - The input is not valid. - RuntimeError - It is a bug, please report to the github issue tracker. - - See Also - ======== - - solveset_real: solver for real domain - - Examples - ======== - - >>> from sympy import Symbol, exp - >>> from sympy.solvers.solveset import solveset_complex - >>> from sympy.abc import x, a, b, c - >>> solveset_complex(a*x**2 + b*x +c, x) - {-b/(2*a) - sqrt(-4*a*c + b**2)/(2*a), -b/(2*a) + sqrt(-4*a*c + b**2)/(2*a)} - - * Due to the fact that complex extension of my real valued functions are - multivariate even some simple equations can have infinitely many - solution. - - >>> solveset_complex(exp(x) - 1, x) - ImageSet(Lambda(_n, 2*_n*I*pi), Integers()) - - """ - if not symbol.is_Symbol: - raise ValueError(" %s is not a symbol" % (symbol)) - - f = sympify(f) - original_eq = f - if not isinstance(f, (Expr, Number)): - raise ValueError(" %s is not a valid sympy expression" % (f)) - - f = together(f) - # Without this equations like a + 4*x**2 - E keep oscillating - # into form a/4 + x**2 - E/4 and (a + 4*x**2 - E)/4 - if not fraction(f)[1].has(symbol): - f = expand(f) - - if f.is_zero: - raise NotImplementedError("S.Complex set is not yet implemented") - elif not f.has(symbol): - result = EmptySet() - elif f.is_Mul and all([_is_finite_with_finite_vars(m) for m in f.args]): - result = Union(*[solveset_complex(m, symbol) for m in f.args]) - else: - lhs, rhs_s = invert_complex(f, 0, symbol) - if lhs == symbol: - result = rhs_s - elif isinstance(rhs_s, FiniteSet): - equations = [lhs - rhs for rhs in rhs_s] - result = EmptySet() - for equation in equations: - if equation == f: - if any(_has_rational_power(g, symbol)[0] - for g in equation.args): - result += _solve_radical(equation, - symbol, - solveset_complex) - else: - result += _solve_as_rational(equation, symbol, - solveset_solver=solveset_complex, - as_poly_solver=_solve_as_poly_complex) - else: - result += solveset_complex(equation, symbol) - else: - raise NotImplementedError - - if isinstance(result, FiniteSet): - result = [s for s in result - if isinstance(s, RootOf) - or domain_check(original_eq, symbol, s)] - return FiniteSet(*result) - else: - return result - - -def solveset(f, symbol=None): - """Solves a given inequality or equation with set as output - - Parameters - ========== - - f : Expr or a relational. - The target equation or inequality - symbol : Symbol - The variable for which the equation is solved - - Returns - ======= - - Set - A set of values for `symbol` for which `f` is True or is equal to - zero. An `EmptySet` is returned if no solution is found. - - `solveset` claims to be complete in the solution set that it returns. - - Raises - ====== - - NotImplementedError - The algorithms for to find the solution of the given equation are - not yet implemented. - ValueError - The input is not valid. - RuntimeError - It is a bug, please report to the github issue tracker. - - - `solveset` uses two underlying functions `solveset_real` and - `solveset_complex` to solve equations. They are - the solvers for real and complex domain respectively. The domain of - the solver is decided by the assumption on the variable for which the - equation is being solved. - - - See Also - ======== - - solveset_real: solver for real domain - solveset_complex: solver for complex domain - - Examples - ======== - - >>> from sympy import exp, Symbol, Eq, pprint - >>> from sympy.solvers.solveset import solveset - >>> from sympy.abc import x - - * Symbols in Sympy are complex by default. A complex variable - will lead to the solving of the equation in complex domain. - - >>> pprint(solveset(exp(x) - 1, x), use_unicode=False) - {2*n*I*pi | n in Integers()} - - * If you want to solve equation in real domain by the `solveset` - interface, then specify the variable to real. Alternatively use - `solveset\_real`. - - >>> x = Symbol('x', extended_real=True) - >>> solveset(exp(x) - 1, x) - {0} - >>> solveset(Eq(exp(x), 1), x) - {0} - - * Inequalities are always solved in the real domain irrespective of - the assumption on the variable for which the inequality is solved. - - >>> solveset(exp(x) > 1, x) - (0, oo) - - """ - - from sympy.solvers.inequalities import solve_univariate_inequality - - if symbol is None: - free_symbols = f.free_symbols - if len(free_symbols) == 1: - symbol = free_symbols.pop() - else: - raise ValueError(filldedent(''' - The independent variable must be specified for a - multivariate equation.''')) - elif not symbol.is_Symbol: - raise ValueError('A Symbol must be given, not type %s: %s' % (type(symbol), symbol)) - - real = (symbol.is_extended_real is True) - - f = sympify(f) - - if isinstance(f, Eq): - f = f.lhs - f.rhs - - if f.is_Relational: - if real is False: - warnings.warn(filldedent(''' - The variable you are solving for is complex - but will assumed to be real since solving complex - inequalities is not supported. - ''')) - return solve_univariate_inequality(f, symbol, relational=False) - - if isinstance(f, (Expr, Number)): - if real is True: - return solveset_real(f, symbol) - else: - return solveset_complex(f, symbol) diff --git a/sympy/solvers/tests/test_ode.py b/sympy/solvers/tests/test_ode.py index 1f07e59e38..4367c342ce 100644 --- a/sympy/solvers/tests/test_ode.py +++ b/sympy/solvers/tests/test_ode.py @@ -876,6 +876,7 @@ def test_old_ode_tests(): assert checkodesol(eq11, sol11, order=1, solve_for_func=False)[0] +@slow def test_1st_linear(): # Type: first order linear form f'(x)+p(x)f(x)=q(x) eq = Eq(f(x).diff(x) + x*f(x), x**2) @@ -1432,6 +1433,8 @@ def test_nth_linear_constant_coeff_homogeneous_RootOf(): @slow @XFAIL def test_nth_linear_constant_coeff_homogeneous_RootOf_sol(): + if ON_TRAVIS: + skip("Too slow for travis.") eq = f(x).diff(x, 5) + 11*f(x).diff(x) - 2*f(x) sol = Eq(f(x), C1*exp(x*RootOf(x**5 + 11*x - 2, 0)) + diff --git a/sympy/solvers/tests/test_solveset.py b/sympy/solvers/tests/test_solveset.py deleted file mode 100644 index cc437cf36f..0000000000 --- a/sympy/solvers/tests/test_solveset.py +++ /dev/null @@ -1,805 +0,0 @@ -from sympy import ( - Abs, Dummy, Eq, Gt, - LambertW, Piecewise, Poly, Rational, S, Symbol, - acos, atan, atanh, cos, erf, erfinv, erfc, erfcinv, - exp, log, pi, sin, sinh, sqrt, symbols, - tan, tanh, atan2, arg, - Lambda, imageset, cot, acot, I, EmptySet, Union, E, Interval, oo) - -from sympy.core.function import nfloat -from sympy.functions.elementary.complexes import im, re -from sympy.functions.elementary.hyperbolic import HyperbolicFunction -from sympy.functions.elementary.trigonometric import TrigonometricFunction - -from sympy.polys.rootoftools import RootOf - -from sympy.sets import FiniteSet - -from sympy.utilities.pytest import XFAIL, raises, skip, slow -from sympy.utilities.randtest import verify_numerically as tn - -from sympy.solvers.solveset import ( - solveset_real, domain_check, solveset_complex, - _is_function_class_equation, invert_real, invert_complex, solveset) - -a = Symbol('a', extended_real=True) -b = Symbol('b', extended_real=True) -c = Symbol('c', extended_real=True) -x = Symbol('x', extended_real=True) -y = Symbol('y', extended_real=True) -z = Symbol('z', extended_real=True) -q = Symbol('q', extended_real=True) -m = Symbol('m', extended_real=True) -n = Symbol('n', extended_real=True) - - -def test_invert_real(): - x = Symbol('x', extended_real=True) - x = Dummy(extended_real=True) - n = Symbol('n') - d = Dummy() - assert solveset(abs(x) - n, x) == solveset(abs(x) - d, x) == EmptySet() - - n = Symbol('n', extended_real=True) - assert invert_real(x + 3, y, x) == (x, FiniteSet(y - 3)) - assert invert_real(x*3, y, x) == (x, FiniteSet(y / 3)) - - assert invert_real(exp(x), y, x) == (x, FiniteSet(log(y))) - assert invert_real(exp(3*x), y, x) == (x, FiniteSet(log(y) / 3)) - assert invert_real(exp(x + 3), y, x) == (x, FiniteSet(log(y) - 3)) - - assert invert_real(exp(x) + 3, y, x) == (x, FiniteSet(log(y - 3))) - assert invert_real(exp(x)*3, y, x) == (x, FiniteSet(log(y / 3))) - - assert invert_real(log(x), y, x) == (x, FiniteSet(exp(y))) - assert invert_real(log(3*x), y, x) == (x, FiniteSet(exp(y) / 3)) - assert invert_real(log(x + 3), y, x) == (x, FiniteSet(exp(y) - 3)) - - assert invert_real(Abs(x), y, x) == (x, FiniteSet(-y, y)) - - assert invert_real(2**x, y, x) == (x, FiniteSet(log(y)/log(2))) - assert invert_real(2**exp(x), y, x) == (x, FiniteSet(log(log(y)/log(2)))) - - assert invert_real(x**2, y, x) == (x, FiniteSet(sqrt(y), -sqrt(y))) - assert invert_real(x**Rational(1, 2), y, x) == (x, FiniteSet(y**2)) - - raises(ValueError, lambda: invert_real(x, x, x)) - raises(ValueError, lambda: invert_real(x**pi, y, x)) - raises(ValueError, lambda: invert_real(S.One, y, x)) - - assert invert_real(x**31 + x, y, x) == (x**31 + x, FiniteSet(y)) - - assert invert_real(Abs(x**31 + x + 1), y, x) == (x**31 + x, - FiniteSet(-y - 1, y - 1)) - - assert invert_real(tan(x), y, x) == \ - (x, imageset(Lambda(n, n*pi + atan(y)), S.Integers)) - - assert invert_real(tan(exp(x)), y, x) == \ - (x, imageset(Lambda(n, log(n*pi + atan(y))), S.Integers)) - - assert invert_real(cot(x), y, x) == \ - (x, imageset(Lambda(n, n*pi + acot(y)), S.Integers)) - assert invert_real(cot(exp(x)), y, x) == \ - (x, imageset(Lambda(n, log(n*pi + acot(y))), S.Integers)) - - assert invert_real(tan(tan(x)), y, x) == \ - (tan(x), imageset(Lambda(n, n*pi + atan(y)), S.Integers)) - - x = Symbol('x', positive=True) - assert invert_real(x**pi, y, x) == (x, FiniteSet(y**(1/pi))) - - -def test_invert_complex(): - assert invert_complex(x + 3, y, x) == (x, FiniteSet(y - 3)) - assert invert_complex(x*3, y, x) == (x, FiniteSet(y / 3)) - - assert invert_complex(exp(x), y, x) == \ - (x, imageset(Lambda(n, I*(2*pi*n + arg(y)) + log(Abs(y))), S.Integers)) - - assert invert_complex(log(x), y, x) == (x, FiniteSet(exp(y))) - - raises(ValueError, lambda: invert_real(S.One, y, x)) - raises(ValueError, lambda: invert_complex(x, x, x)) - - # https://github.com/skirpichev/omg/issues/16 - assert invert_complex(sinh(x), 0, x) != (x, FiniteSet(0)) - - -def test_domain_check(): - assert domain_check(1/(1 + (1/(x+1))**2), x, -1) is False - assert domain_check(x**2, x, 0) is True - assert domain_check(x, x, oo) is False - assert domain_check(0, x, oo) is False - - -def test_is_function_class_equation(): - from sympy.abc import x, a - assert _is_function_class_equation(TrigonometricFunction, - tan(x), x) is True - assert _is_function_class_equation(TrigonometricFunction, - tan(x) - 1, x) is True - assert _is_function_class_equation(TrigonometricFunction, - tan(x) + sin(x), x) is True - assert _is_function_class_equation(TrigonometricFunction, - tan(x) + sin(x) - a, x) is True - assert _is_function_class_equation(TrigonometricFunction, - sin(x)*tan(x) + sin(x), x) is True - assert _is_function_class_equation(TrigonometricFunction, - sin(x)*tan(x + a) + sin(x), x) is True - assert _is_function_class_equation(TrigonometricFunction, - sin(x)*tan(x*a) + sin(x), x) is True - assert _is_function_class_equation(TrigonometricFunction, - a*tan(x) - 1, x) is True - assert _is_function_class_equation(TrigonometricFunction, - tan(x)**2 + sin(x) - 1, x) is True - assert _is_function_class_equation(TrigonometricFunction, - tan(x**2), x) is False - assert _is_function_class_equation(TrigonometricFunction, - tan(x**2) + sin(x), x) is False - assert _is_function_class_equation(TrigonometricFunction, - tan(x)**sin(x), x) is False - assert _is_function_class_equation(TrigonometricFunction, - tan(sin(x)) + sin(x), x) is False - assert _is_function_class_equation(HyperbolicFunction, - tanh(x), x) is True - assert _is_function_class_equation(HyperbolicFunction, - tanh(x) - 1, x) is True - assert _is_function_class_equation(HyperbolicFunction, - tanh(x) + sinh(x), x) is True - assert _is_function_class_equation(HyperbolicFunction, - tanh(x) + sinh(x) - a, x) is True - assert _is_function_class_equation(HyperbolicFunction, - sinh(x)*tanh(x) + sinh(x), x) is True - assert _is_function_class_equation(HyperbolicFunction, - sinh(x)*tanh(x + a) + sinh(x), x) is True - assert _is_function_class_equation(HyperbolicFunction, - sinh(x)*tanh(x*a) + sinh(x), x) is True - assert _is_function_class_equation(HyperbolicFunction, - a*tanh(x) - 1, x) is True - assert _is_function_class_equation(HyperbolicFunction, - tanh(x)**2 + sinh(x) - 1, x) is True - assert _is_function_class_equation(HyperbolicFunction, - tanh(x**2), x) is False - assert _is_function_class_equation(HyperbolicFunction, - tanh(x**2) + sinh(x), x) is False - assert _is_function_class_equation(HyperbolicFunction, - tanh(x)**sinh(x), x) is False - assert _is_function_class_equation(HyperbolicFunction, - tanh(sinh(x)) + sinh(x), x) is False - - -def test_garbage_input(): - raises(ValueError, lambda: solveset_real([x], x)) - raises(ValueError, lambda: solveset_real(x, pi)) - raises(ValueError, lambda: solveset_real(x, x**2)) - - raises(ValueError, lambda: solveset_complex([x], x)) - raises(ValueError, lambda: solveset_complex(x, pi)) - - -def test_solve_mul(): - assert solveset_real((a*x + b)*(exp(x) - 3), x) == \ - FiniteSet(-b/a, log(3)) - assert solveset_real((2*x + 8)*(8 + exp(x)), x) == FiniteSet(S(-4)) - assert solveset_real(x/log(x), x) == EmptySet() - - -def test_solve_invert(): - assert solveset_real(exp(x) - 3, x) == FiniteSet(log(3)) - assert solveset_real(log(x) - 3, x) == FiniteSet(exp(3)) - - assert solveset_real(3**(x + 2), x) == FiniteSet() - assert solveset_real(3**(2 - x), x) == FiniteSet() - - b = Symbol('b', positive=True) - y = Symbol('y', positive=True) - assert solveset_real(y - b*exp(a/x), x) == FiniteSet(a/log(y/b)) - # issue 4504 - assert solveset_real(2**x - 10, x) == FiniteSet(log(10)/log(2)) - - -def test_errorinverses(): - assert solveset_real(erf(x) - S.One/2, x) == \ - FiniteSet(erfinv(S.One/2)) - assert solveset_real(erfinv(x) - 2, x) == \ - FiniteSet(erf(2)) - assert solveset_real(erfc(x) - S.One, x) == \ - FiniteSet(erfcinv(S.One)) - assert solveset_real(erfcinv(x) - 2, x) == FiniteSet(erfc(2)) - - -def test_solve_polynomial(): - assert solveset_real(3*x - 2, x) == FiniteSet(Rational(2, 3)) - - assert solveset_real(x**2 - 1, x) == FiniteSet(-S(1), S(1)) - assert solveset_real(x - y**3, x) == FiniteSet(y ** 3) - - a11, a12, a21, a22, b1, b2 = symbols('a11, a12, a21, a22, b1, b2') - - assert solveset_real(x**3 - 15*x - 4, x) == FiniteSet( - -2 + 3 ** Rational(1, 2), - S(4), - -2 - 3 ** Rational(1, 2)) - - assert solveset_real(sqrt(x) - 1, x) == FiniteSet(1) - assert solveset_real(sqrt(x) - 2, x) == FiniteSet(4) - assert solveset_real(x**Rational(1, 4) - 2, x) == FiniteSet(16) - assert solveset_real(x**Rational(1, 3) - 3, x) == FiniteSet(27) - assert len(solveset_real(x**5 + x**3 + 1, x)) == 1 - assert len(solveset_real(-2*x**3 + 4*x**2 - 2*x + 6, x)) > 0 - - -def test_return_root_of(): - f = x**5 - 15*x**3 - 5*x**2 + 10*x + 20 - s = list(solveset_complex(f, x)) - for root in s: - assert root.func == RootOf - - # if one uses solve to get the roots of a polynomial that has a RootOf - # solution, make sure that the use of nfloat during the solve process - # doesn't fail. Note: if you want numerical solutions to a polynomial - # it is *much* faster to use nroots to get them than to solve the - # equation only to get RootOf solutions which are then numerically - # evaluated. So for eq = x**5 + 3*x + 7 do Poly(eq).nroots() rather - # than [i.n() for i in solve(eq)] to get the numerical roots of eq. - assert nfloat(list(solveset_complex(x**5 + 3*x**3 + 7, x))[0], - exponent=False) == RootOf(x**5 + 3*x**3 + 7, 0).n() - - sol = list(solveset_complex(x**6 - 2*x + 2, x)) - assert all(isinstance(i, RootOf) for i in sol) and len(sol) == 6 - - f = x**5 - 15*x**3 - 5*x**2 + 10*x + 20 - s = list(solveset_complex(f, x)) - for root in s: - assert root.func == RootOf - - s = x**5 + 4*x**3 + 3*x**2 + S(7)/4 - assert solveset_complex(s, x) == \ - FiniteSet(*Poly(s*4, domain='ZZ').all_roots()) - - # XXX: this comparison should work without converting the FiniteSet to list - # See #7876 - eq = x*(x - 1)**2*(x + 1)*(x**6 - x + 1) - assert list(solveset_complex(eq, x)) == \ - list(FiniteSet(-1, 0, 1, RootOf(x**6 - x + 1, 0), - RootOf(x**6 - x + 1, 1), - RootOf(x**6 - x + 1, 2), - RootOf(x**6 - x + 1, 3), - RootOf(x**6 - x + 1, 4), - RootOf(x**6 - x + 1, 5))) - - -def test__has_rational_power(): - from sympy.solvers.solveset import _has_rational_power - assert _has_rational_power(sqrt(2), x)[0] is False - assert _has_rational_power(x*sqrt(2), x)[0] is False - - assert _has_rational_power(x**2*sqrt(x), x) == (True, 2) - assert _has_rational_power(sqrt(2)*x**(S(1)/3), x) == (True, 3) - assert _has_rational_power(sqrt(x)*x**(S(1)/3), x) == (True, 6) - - -def test_solveset_sqrt_1(): - assert solveset_real(sqrt(5*x + 6) - 2 - x, x) == \ - FiniteSet(-S(1), S(2)) - assert solveset_real(sqrt(x - 1) - x + 7, x) == FiniteSet(10) - assert solveset_real(sqrt(x - 2) - 5, x) == FiniteSet(27) - assert solveset_real(sqrt(x) - 2 - 5, x) == FiniteSet(49) - assert solveset_real(sqrt(x**3), x) == FiniteSet(0) - assert solveset_real(sqrt(x - 1), x) == FiniteSet(1) - - -def test_solveset_sqrt_2(): - # http://tutorial.math.lamar.edu/Classes/Alg/SolveRadicalEqns.aspx#Solve_Rad_Ex2_a - assert solveset_real(sqrt(2*x - 1) - sqrt(x - 4) - 2, x) == \ - FiniteSet(S(5), S(13)) - assert solveset_real(sqrt(x + 7) + 2 - sqrt(3 - x), x) == \ - FiniteSet(-6) - - # http://www.purplemath.com/modules/solverad.htm - assert solveset_real(sqrt(17*x - sqrt(x**2 - 5)) - 7, x) == \ - FiniteSet(3) - - eq = x + 1 - (x**4 + 4*x**3 - x)**Rational(1, 4) - assert solveset_real(eq, x) == FiniteSet(-S(1)/2, -S(1)/3) - - eq = sqrt(2*x + 9) - sqrt(x + 1) - sqrt(x + 4) - assert solveset_real(eq, x) == FiniteSet(0) - - eq = sqrt(x + 4) + sqrt(2*x - 1) - 3*sqrt(x - 1) - assert solveset_real(eq, x) == FiniteSet(5) - - eq = sqrt(x)*sqrt(x - 7) - 12 - assert solveset_real(eq, x) == FiniteSet(16) - - eq = sqrt(x - 3) + sqrt(x) - 3 - assert solveset_real(eq, x) == FiniteSet(4) - - eq = sqrt(2*x**2 - 7) - (3 - x) - assert solveset_real(eq, x) == FiniteSet(-S(8), S(2)) - - # others - eq = sqrt(9*x**2 + 4) - (3*x + 2) - assert solveset_real(eq, x) == FiniteSet(0) - - assert solveset_real(sqrt(x - 3) - sqrt(x) - 3, x) == FiniteSet() - - eq = (2*x - 5)**Rational(1, 3) - 3 - assert solveset_real(eq, x) == FiniteSet(16) - - assert solveset_real(sqrt(x) + sqrt(sqrt(x)) - 4, x) == \ - FiniteSet((-S.Half + sqrt(17)/2)**4) - - eq = sqrt(x) - sqrt(x - 1) + sqrt(sqrt(x)) - assert solveset_real(eq, x) == FiniteSet() - - eq = (sqrt(x) + sqrt(x + 1) + sqrt(1 - x) - 6*sqrt(5)/5) - ans = solveset_real(eq, x) - ra = S('''-1484/375 - 4*(-1/2 + sqrt(3)*I/2)*(-12459439/52734375 + - 114*sqrt(12657)/78125)**(1/3) - 172564/(140625*(-1/2 + - sqrt(3)*I/2)*(-12459439/52734375 + 114*sqrt(12657)/78125)**(1/3))''') - rb = S(4)/5 - assert all(abs(eq.subs(x, i).n()) < 1e-10 for i in (ra, rb)) and \ - len(ans) == 2 and \ - {i.n(chop=True) for i in ans} == \ - {i.n(chop=True) for i in (ra, rb)} - - assert solveset_real(sqrt(x) + x**Rational(1, 3) + - x**Rational(1, 4), x) == FiniteSet(0) - - assert solveset_real(x/sqrt(x**2 + 1), x) == FiniteSet(0) - - eq = (x - y**3)/((y**2)*sqrt(1 - y**2)) - assert solveset_real(eq, x) == FiniteSet(y**3) - - # issue 4497 - assert solveset_real(1/(5 + x)**(S(1)/5) - 9, x) == \ - FiniteSet(-295244/S(59049)) - - -@XFAIL -def test_solve_sqrt_fail(): - # this only works if we check real_root(eq.subs(x, S(1)/3)) - # but checksol doesn't work like that - eq = (x**3 - 3*x**2)**Rational(1, 3) + 1 - x - assert solveset_real(eq, x) == FiniteSet(S(1)/3) - - -@slow -def test_solve_sqrt_3(): - R = Symbol('R') - eq = sqrt(2)*R*sqrt(1/(R + 1)) + (R + 1)*(sqrt(2)*sqrt(1/(R + 1)) - 1) - sol = solveset_complex(eq, R) - - assert sol == FiniteSet(*[S(5)/3 + 4*sqrt(10)*cos(atan(3*sqrt(111)/251)/3)/3, - -sqrt(10)*cos(atan(3*sqrt(111)/251)/3)/3 + 40*re(1/((-S(1)/2 - - sqrt(3)*I/2)*(S(251)/27 + sqrt(111)*I/9)**(S(1)/3)))/9 + - sqrt(30)*sin(atan(3*sqrt(111)/251)/3)/3 + S(5)/3 + - I*(-sqrt(30)*cos(atan(3*sqrt(111)/251)/3)/3 - - sqrt(10)*sin(atan(3*sqrt(111)/251)/3)/3 + 40*im(1/((-S(1)/2 - - sqrt(3)*I/2)*(S(251)/27 + sqrt(111)*I/9)**(S(1)/3)))/9)]) - - # the number of real roots will depend on the value of m: for m=1 there are 4 - # and for m=-1 there are none. - eq = -sqrt((m - q)**2 + (-m/(2*q) + S(1)/2)**2) + sqrt((-m**2/2 - sqrt( - 4*m**4 - 4*m**2 + 8*m + 1)/4 - S(1)/4)**2 + (m**2/2 - m - sqrt( - 4*m**4 - 4*m**2 + 8*m + 1)/4 - S(1)/4)**2) - raises(NotImplementedError, lambda: solveset_real(eq, q)) - - -def test_solve_polynomial_symbolic_param(): - assert solveset_complex((x**2 - 1)**2 - a, x) == \ - FiniteSet(sqrt(1 + sqrt(a)), -sqrt(1 + sqrt(a)), - sqrt(1 - sqrt(a)), -sqrt(1 - sqrt(a))) - - # By attempt to make Set.contains behave symbolically SetDifference on - # FiniteSet isn't working very well. - # Simple operations like `FiniteSet(a) - FiniteSet(-b)` raises `TypeError` - # The likely course of action will making such operations return - # SetDifference object. That will also change the expected output of - # the given tests. Till the SetDifference becomes well behaving again the - # following tests are kept as comments. - - # # issue 4508 - # assert solveset_complex(y - b*x/(a + x), x) == \ - # FiniteSet(-a*y/(y - b)) - # - # # issue 4507 - # assert solveset_complex(y - b/(1 + a*x), x) == \ - # FiniteSet((b - y)/(a*y)) - - -def test_solve_rational(): - assert solveset_real(1/x + 1, x) == FiniteSet(-S.One) - assert solveset_real(1/exp(x) - 1, x) == FiniteSet(0) - assert solveset_real(x*(1 - 5/x), x) == FiniteSet(5) - assert solveset_real(2*x/(x + 2) - 1, x) == FiniteSet(2) - assert solveset_real((x**2/(7 - x)).diff(x), x) == \ - FiniteSet(S(0), S(14)) - - -def test_solveset_real_gen_is_pow(): - assert solveset_real(sqrt(1) + 1, x) == EmptySet() - - -def test_no_sol(): - assert solveset_real(4, x) == EmptySet() - assert solveset_real(exp(x), x) == EmptySet() - assert solveset_real(x**2 + 1, x) == EmptySet() - assert solveset_real(-3*a/sqrt(x), x) == EmptySet() - assert solveset_real(1/x, x) == EmptySet() - assert solveset_real(-(1 + x)/(2 + x)**2 + 1/(2 + x), x) == \ - EmptySet() - - -def test_sol_zero_real(): - assert solveset_real(0, x) == S.Reals - assert solveset_real(-x**2 - 2*x + (x + 1)**2 - 1, x) == S.Reals - - -def test_no_sol_rational_extragenous(): - assert solveset_real((x/(x + 1) + 3)**(-2), x) == EmptySet() - assert solveset_real((x - 1)/(1 + 1/(x - 1)), x) == EmptySet() - - -def test_solve_polynomial_cv_1a(): - """ - Test for solving on equations that can be converted to - a polynomial equation using the change of variable y -> x**Rational(p, q) - """ - assert solveset_real(sqrt(x) - 1, x) == FiniteSet(1) - assert solveset_real(sqrt(x) - 2, x) == FiniteSet(4) - assert solveset_real(x**Rational(1, 4) - 2, x) == FiniteSet(16) - assert solveset_real(x**Rational(1, 3) - 3, x) == FiniteSet(27) - assert solveset_real(x*(x**(S(1) / 3) - 3), x) == \ - FiniteSet(S(0), S(27)) - - -def test_solveset_real_rational(): - """Test solveset_real for rational functions""" - assert solveset_real((x - y**3) / ((y**2)*sqrt(1 - y**2)), x) \ - == FiniteSet(y**3) - # issue 4486 - assert solveset_real(2*x/(x + 2) - 1, x) == FiniteSet(2) - - -def test_solveset_real_log(): - assert solveset_real(log((x-1)*(x+1)), x) == \ - FiniteSet(sqrt(2), -sqrt(2)) - - -def test_poly_gens(): - assert solveset_real(4**(2*(x**2) + 2*x) - 8, x) == \ - FiniteSet(-Rational(3, 2), S.Half) - - -@XFAIL -def test_uselogcombine_1(): - assert solveset_real(log(x - 3) + log(x + 3), x) == \ - FiniteSet(sqrt(10)) - assert solveset_real(log(x + 1) - log(2*x - 1), x) == FiniteSet(2) - assert solveset_real(log(x + 3) + log(1 + 3/x) - 3) == FiniteSet( - -3 + sqrt(-12 + exp(3))*exp(S(3)/2)/2 + exp(3)/2, - -sqrt(-12 + exp(3))*exp(S(3)/2)/2 - 3 + exp(3)/2) - - -@XFAIL -def test_uselogcombine_2(): - eq = z - log(x) + log(y/(x*(-1 + y**2/x**2))) - assert solveset_real(eq, x) == \ - FiniteSet(-sqrt(y*(y - exp(z))), sqrt(y*(y - exp(z)))) - - -def test_solve_abs(): - assert solveset_real(Abs(x) - 2, x) == FiniteSet(-2, 2) - assert solveset_real(Abs(x + 3) - 2*Abs(x - 3), x) == \ - FiniteSet(1, 9) - assert solveset_real(2*Abs(x) - Abs(x - 1), x) == \ - FiniteSet(-1, Rational(1, 3)) - - assert solveset_real(Abs(x - 7) - 8, x) == FiniteSet(-S(1), S(15)) - - -@XFAIL -def test_rewrite_trigh(): - # if this import passes then the test below should also pass - from sympy import sech - assert solveset_real(sinh(x) + sech(x), x) == FiniteSet( - 2*atanh(-S.Half + sqrt(5)/2 - sqrt(-2*sqrt(5) + 2)/2), - 2*atanh(-S.Half + sqrt(5)/2 + sqrt(-2*sqrt(5) + 2)/2), - 2*atanh(-sqrt(5)/2 - S.Half + sqrt(2 + 2*sqrt(5))/2), - 2*atanh(-sqrt(2 + 2*sqrt(5))/2 - sqrt(5)/2 - S.Half)) - - -@XFAIL -def test_real_imag_splitting1(): - a, b = symbols('a b', extended_real=True, finite=True) - s = solveset_real(sqrt(a**2 + b**2) - 3, a) - assert s != S.EmptySet - # FiniteSet(-sqrt(-b**2 + 9), sqrt(-b**2 + 9)) - # fails now because whether it is real or not depends - # on the value of b, e.g. b = 4 gives an imaginary value - - -def test_real_imag_splitting(): - a, b = symbols('a b', extended_real=True, finite=True) - assert solveset_real(sqrt(a**2 - b**2) - 3, a) == \ - FiniteSet(-sqrt(b**2 + 9), sqrt(b**2 + 9)) - - -def test_solve_only_exp_1(): - y = Symbol('y', positive=True, finite=True) - assert solveset_real(exp(x) - y, x) == FiniteSet(log(y)) - - -@XFAIL -def test_only_exp_2(): - assert solveset_real(exp(x) + exp(-x) - 4, x) == \ - FiniteSet(log(-sqrt(3) + 2), log(sqrt(3) + 2)) - assert solveset_real(exp(x) + exp(-x) - y, x) != S.EmptySet - # FiniteSet(log(y/2 - sqrt((y - 2)*(y + 2))/2), - # log(y/2 + sqrt((y - 2)*(y + 2))/2)) - # fails now because whether it is real or not depends - # on whether y >= 2 - - -@XFAIL -def test_only_exp_3(): - assert solveset_real(exp(x/y)*exp(-z/y) - 2, y) == \ - FiniteSet((x - z)/log(2)) - assert solveset_real(sqrt(exp(x)) + sqrt(exp(-x)) - 4, x) == \ - FiniteSet(2*log(-sqrt(3) + 2), 2*log(sqrt(3) + 2)) - - -def test_atan2(): - # The .inverse() method on atan2 works only if x.is_extended_real is True and the - # second argument is a real constant - assert solveset_real(atan2(x, 2) - pi/3, x) == FiniteSet(2*sqrt(3)) - - -def test_piecewise(): - eq = Piecewise((x - 2, Gt(x, 2)), (2 - x, True)) - 3 - assert set(solveset_real(eq, x)) == set(FiniteSet(-1, 5)) - absxm3 = Piecewise( - (x - 3, S(0) <= x - 3), - (3 - x, S(0) > x - 3) - ) - y = Symbol('y', positive=True) - assert solveset_real(absxm3 - y, x) == FiniteSet(-y + 3, y + 3) - - -def test_solveset_complex_polynomial(): - from sympy.abc import x, a, b, c - assert solveset_complex(a*x**2 + b*x + c, x) == \ - FiniteSet(-b/(2*a) - sqrt(-4*a*c + b**2)/(2*a), - -b/(2*a) + sqrt(-4*a*c + b**2)/(2*a)) - - assert solveset_complex(x - y**3, y) == FiniteSet( - (-x**Rational(1, 3))/2 + I*sqrt(3)*x**Rational(1, 3)/2, - x**Rational(1, 3), - (-x**Rational(1, 3))/2 - I*sqrt(3)*x**Rational(1, 3)/2) - - assert solveset_complex(x + 1/x - 1, x) == \ - FiniteSet(Rational(1, 2) + I*sqrt(3)/2, Rational(1, 2) - I*sqrt(3)/2) - - -def test_sol_zero_complex(): - # This should return the complex set after it is implemented - raises(NotImplementedError, lambda: solveset_complex(0, x)) - - -def test_solveset_complex_rational(): - assert solveset_complex((x - 1)*(x - I)/(x - 3), x) == \ - FiniteSet(1, I) - - assert solveset_complex((x - y**3)/((y**2)*sqrt(1 - y**2)), x) == \ - FiniteSet(y**3) - assert solveset_complex(-x**2 - I, x) == \ - FiniteSet(-sqrt(2)/2 + sqrt(2)*I/2, sqrt(2)/2 - sqrt(2)*I/2) - - -def test_solve_quintics(): - skip("This test is too slow") - f = x**5 - 110*x**3 - 55*x**2 + 2310*x + 979 - s = solveset_complex(f, x) - for root in s: - res = f.subs(x, root.n()).n() - assert tn(res, 0) - - f = x**5 + 15*x + 12 - s = solveset_complex(f, x) - for root in s: - res = f.subs(x, root.n()).n() - assert tn(res, 0) - - -def test_solveset_complex_exp(): - from sympy.abc import x, n - assert solveset_complex(exp(x) - 1, x) == \ - imageset(Lambda(n, I*2*n*pi), S.Integers) - assert solveset_complex(exp(x) - I, x) == \ - imageset(Lambda(n, I*(2*n*pi + pi/2)), S.Integers) - - -def test_solve_complex_log(): - assert solveset_complex(log(x), x) == FiniteSet(1) - assert solveset_complex(1 - log(a + 4*x**2), x) == \ - FiniteSet(-sqrt(-a/4 + E/4), sqrt(-a/4 + E/4)) - - -def test_solve_complex_sqrt(): - assert solveset_complex(sqrt(5*x + 6) - 2 - x, x) == \ - FiniteSet(-S(1), S(2)) - assert solveset_complex(sqrt(5*x + 6) - (2 + 2*I) - x, x) == \ - FiniteSet(-S(2), 3 - 4*I) - assert solveset_complex(4*x*(1 - a * sqrt(x)), x) == \ - FiniteSet(S(0), 1 / a ** 2) - - -def test_solveset_complex_tan(): - s = solveset_complex(tan(x).rewrite(exp), x) - assert s == imageset(Lambda(n, pi*n), S.Integers) - \ - imageset(Lambda(n, pi*n + pi/2), S.Integers) - - -def test_solve_trig(): - from sympy.abc import n - assert solveset_real(sin(x), x) == \ - Union(imageset(Lambda(n, 2*pi*n), S.Integers), - imageset(Lambda(n, 2*pi*n + pi), S.Integers)) - - assert solveset_real(sin(x) - 1, x) == \ - imageset(Lambda(n, 2*pi*n + pi/2), S.Integers) - - assert solveset_real(cos(x), x) == \ - Union(imageset(Lambda(n, 2*pi*n - pi/2), S.Integers), - imageset(Lambda(n, 2*pi*n + pi/2), S.Integers)) - - assert solveset_real(sin(x) + cos(x), x) == \ - Union(imageset(Lambda(n, 2*n*pi - pi/4), S.Integers), - imageset(Lambda(n, 2*n*pi + 3*pi/4), S.Integers)) - - assert solveset_real(sin(x)**2 + cos(x)**2, x) == S.EmptySet - - -def test_solve_invalid_sol(): - assert 0 not in solveset_real(sin(x)/x, x) - assert 0 not in solveset_complex((exp(x) - 1)/x, x) - - -def test_solve_complex_unsolvable(): - raises(NotImplementedError, lambda: solveset_complex(cos(x) - S.Half, x)) - - -@XFAIL -def test_solve_trig_simplified(): - from sympy.abc import n - assert solveset_real(sin(x), x) == \ - imageset(Lambda(n, n*pi), S.Integers) - - assert solveset_real(cos(x), x) == \ - imageset(Lambda(n, n*pi + pi/2), S.Integers) - - assert solveset_real(cos(x) + sin(x), x) == \ - imageset(Lambda(n, n*pi - pi/4), S.Integers) - - -@XFAIL -def test_solve_lambert(): - assert solveset_real(x*exp(x) - 1, x) == FiniteSet(LambertW(1)) - assert solveset_real(x + 2**x, x) == \ - FiniteSet(-LambertW(log(2))/log(2)) - - # issue 4739 - assert solveset_real(exp(log(5)*x) - 2**x, x) == FiniteSet(0) - ans = solveset_real(3*x + 5 + 2**(-5*x + 3), x) - assert ans == FiniteSet(-Rational(5, 3) + - LambertW(-10240*2**(S(1)/3)*log(2)/3)/(5*log(2))) - - eq = 2*(3*x + 4)**5 - 6*7**(3*x + 9) - result = solveset_real(eq, x) - ans = FiniteSet((log(2401) + - 5*LambertW(-log(7**(7*3**Rational(1, 5)/5))))/(3*log(7))/-1) - assert result == ans - assert solveset_real(eq.expand(), x) == result - - assert solveset_real(5*x - 1 + 3*exp(2 - 7*x), x) == \ - FiniteSet(Rational(1, 5) + LambertW(-21*exp(Rational(3, 5))/5)/7) - - assert solveset_real(2*x + 5 + log(3*x - 2), x) == \ - FiniteSet(Rational(2, 3) + LambertW(2*exp(-Rational(19, 3))/3)/2) - - assert solveset_real(3*x + log(4*x), x) == \ - FiniteSet(LambertW(Rational(3, 4))/3) - - assert solveset_complex(x**z*y**z - 2, z) == \ - FiniteSet(log(2)/(log(x) + log(y))) - - assert solveset_real(x**x - 2) == FiniteSet(exp(LambertW(log(2)))) - - a = Symbol('a') - assert solveset_real(-a*x + 2*x*log(x), x) == FiniteSet(exp(a/2)) - a = Symbol('a', extended_real=True) - assert solveset_real(a/x + exp(x/2), x) == \ - FiniteSet(2*LambertW(-a/2)) - assert solveset_real((a/x + exp(x/2)).diff(x), x) == \ - FiniteSet(4*LambertW(sqrt(2)*sqrt(a)/4)) - - assert solveset_real(1/(1/x - y + exp(y)), x) == EmptySet() - # coverage test - p = Symbol('p', positive=True) - w = Symbol('w') - assert solveset_real((1/p + 1)**(p + 1), p) == EmptySet() - assert solveset_real(tanh(x + 3)*tanh(x - 3) - 1, x) == EmptySet() - assert solveset_real(2*x**w - 4*y**w, w) == \ - solveset_real((x/y)**w - 2, w) - - assert solveset_real((x**2 - 2*x + 1).subs(x, log(x) + 3*x), x) == \ - FiniteSet(LambertW(3*S.Exp1)/3) - assert solveset_real((x**2 - 2*x + 1).subs(x, (log(x) + 3*x)**2 - 1), x) == \ - FiniteSet(LambertW(3*exp(-sqrt(2)))/3, LambertW(3*exp(sqrt(2)))/3) - assert solveset_real((x**2 - 2*x - 2).subs(x, log(x) + 3*x), x) == \ - FiniteSet(LambertW(3*exp(1 + sqrt(3)))/3, LambertW(3*exp(-sqrt(3) + 1))/3) - assert solveset_real(x*log(x) + 3*x + 1, x) == \ - FiniteSet(exp(-3 + LambertW(-exp(3)))) - eq = (x*exp(x) - 3).subs(x, x*exp(x)) - assert solveset_real(eq, x) == \ - FiniteSet(LambertW(3*exp(-LambertW(3)))) - - assert solveset_real(3*log(a**(3*x + 5)) + a**(3*x + 5), x) == \ - FiniteSet(-((log(a**5) + LambertW(S(1)/3))/(3*log(a)))) - p = symbols('p', positive=True) - assert solveset_real(3*log(p**(3*x + 5)) + p**(3*x + 5), x) == \ - FiniteSet( - log((-3**(S(1)/3) - 3**(S(5)/6)*I)*LambertW(S(1)/3)**(S(1)/3)/(2*p**(S(5)/3)))/log(p), - log((-3**(S(1)/3) + 3**(S(5)/6)*I)*LambertW(S(1)/3)**(S(1)/3)/(2*p**(S(5)/3)))/log(p), - log((3*LambertW(S(1)/3)/p**5)**(1/(3*log(p)))),) # checked numerically - # check collection - b = Symbol('b') - eq = 3*log(a**(3*x + 5)) + b*log(a**(3*x + 5)) + a**(3*x + 5) - assert solveset_real(eq, x) == FiniteSet( - -((log(a**5) + LambertW(1/(b + 3)))/(3*log(a)))) - - # issue 4271 - assert solveset_real((a/x + exp(x/2)).diff(x, 2), x) == FiniteSet( - 6*LambertW((-1)**(S(1)/3)*a**(S(1)/3)/3)) - - assert solveset_real(x**3 - 3**x, x) == \ - FiniteSet(-3/log(3)*LambertW(-log(3)/3)) - assert solveset_real(x**2 - 2**x, x) == FiniteSet(2) - assert solveset_real(-x**2 + 2**x, x) == FiniteSet(2) - assert solveset_real(3**cos(x) - cos(x)**3) == FiniteSet( - acos(-3*LambertW(-log(3)/3)/log(3))) - - assert solveset_real(4**(x/2) - 2**(x/3), x) == FiniteSet(0) - assert solveset_real(5**(x/2) - 2**(x/3), x) == FiniteSet(0) - b = sqrt(6)*sqrt(log(2))/sqrt(log(5)) - assert solveset_real(5**(x/2) - 2**(3/x), x) == FiniteSet(-b, b) - - -def test_solveset(): - x = Symbol('x', extended_real=True) - raises(ValueError, lambda: solveset(x + y)) - - assert solveset(exp(x) - 1) == FiniteSet(0) - assert solveset(exp(x) - 1, x) == FiniteSet(0) - assert solveset(Eq(exp(x), 1), x) == FiniteSet(0) - - assert solveset(x - 1 >= 0, x) == Interval(1, oo) - assert solveset(exp(x) - 1 >= 0, x) == Interval(0, oo) - - x = Symbol('x') - assert solveset(exp(x) - 1, x) == imageset(Lambda(n, 2*I*pi*n), S.Integers) - assert solveset(Eq(exp(x), 1), x) == imageset(Lambda(n, 2*I*pi*n), - S.Integers) - - -def test_improve_coverage(): - from sympy.solvers.solveset import _has_rational_power - x = Symbol('x', extended_real=True) - y = exp(x+1/x**2) - raises(NotImplementedError, lambda: solveset(y**2+y, x)) - - assert _has_rational_power(sin(x)*exp(x) + 1, x) == (False, S.One) - assert _has_rational_power((sin(x)**2)*(exp(x) + 1)**3, x) == (False, S.One) diff --git a/sympy/stats/frv_types.py b/sympy/stats/frv_types.py index c2bd5f4d4c..3f4e767344 100644 --- a/sympy/stats/frv_types.py +++ b/sympy/stats/frv_types.py @@ -86,16 +86,17 @@ def DiscreteUniform(name, items): ======== >>> from sympy.stats import DiscreteUniform, density - >>> from sympy import symbols + >>> from sympy import symbols, Rational, Symbol + + >>> s3, s5 = Rational(1, 3), Rational(1, 5) >>> X = DiscreteUniform('X', symbols('a b c')) # equally likely over a, b, c - >>> density(X).dict - {a: 1/3, b: 1/3, c: 1/3} + >>> density(X).dict == {Symbol('a'): s3, Symbol('b'): s3, Symbol('c'): s3} + True >>> Y = DiscreteUniform('Y', list(range(5))) # distribution over a range - >>> density(Y).dict - {0: 1/5, 1: 1/5, 2: 1/5, 3: 1/5, 4: 1/5} - + >>> density(Y).dict == {0: s5, 1: s5, 2: s5, 3: s5, 4: s5} + True """ return rv(name, DiscreteUniformDistribution, *items) @@ -164,15 +165,15 @@ def Bernoulli(name, p, succ=1, fail=0): Returns a RandomSymbol >>> from sympy.stats import Bernoulli, density - >>> from sympy import S + >>> from sympy import S, Symbol >>> X = Bernoulli('X', S(3)/4) # 1-0 Bernoulli variable, probability = 3/4 - >>> density(X).dict - {0: 1/4, 1: 3/4} + >>> density(X).dict == {0: S(1)/4, 1: S(3)/4} + True >>> X = Bernoulli('X', S.Half, 'Heads', 'Tails') # A fair coin toss - >>> density(X).dict - {Heads: 1/2, Tails: 1/2} + >>> density(X).dict == {Symbol('Heads'): S(1)/2, Symbol('Tails'): S(1)/2} + True """ return rv(name, BernoulliDistribution, p, succ, fail) @@ -187,15 +188,17 @@ def Coin(name, p=S.Half): Returns a RandomSymbol. >>> from sympy.stats import Coin, density - >>> from sympy import Rational + >>> from sympy import Rational, Symbol + + >>> H, T = Symbol('H'), Symbol('T') >>> C = Coin('C') # A fair coin toss - >>> density(C).dict - {H: 1/2, T: 1/2} + >>> density(C).dict == {H: Rational(1, 2), T: Rational(1, 2)} + True >>> C2 = Coin('C2', Rational(3, 5)) # An unfair coin - >>> density(C2).dict - {H: 3/5, T: 2/5} + >>> density(C2).dict == {H: Rational(3, 5), T: Rational(2, 5)} + True """ return rv(name, BernoulliDistribution, p, 'H', 'T') @@ -295,11 +298,14 @@ def Rademacher(name): Examples ======== + >>> from sympy.core.numbers import Rational >>> from sympy.stats import Rademacher, density + >>> s2 = Rational(1, 2) + >>> X = Rademacher('X') - >>> density(X).dict - {-1: 1/2, 1: 1/2} + >>> density(X).dict == {-1: s2, 1: s2} + True See Also ======== diff --git a/sympy/stats/rv.py b/sympy/stats/rv.py index 9faccc3182..bbd932725a 100644 --- a/sympy/stats/rv.py +++ b/sympy/stats/rv.py @@ -708,7 +708,7 @@ def cdf(expr, condition=None, evaluate=True, **kwargs): ======== >>> from sympy.stats import density, Die, Normal, cdf - >>> from sympy import Symbol + >>> from sympy import Symbol, Rational >>> D = Die('D', 6) >>> X = Normal('X', 0, 1) @@ -717,8 +717,9 @@ def cdf(expr, condition=None, evaluate=True, **kwargs): {1: 1/6, 2: 1/6, 3: 1/6, 4: 1/6, 5: 1/6, 6: 1/6} >>> cdf(D) {1: 1/6, 2: 1/3, 3: 1/2, 4: 2/3, 5: 5/6, 6: 1} - >>> cdf(3*D, D>2) - {9: 1/4, 12: 1/2, 15: 3/4, 18: 1} + >>> cdf(3*D, D>2) == {9: Rational(1, 4), 12: Rational(1, 2), + ... 15: Rational(3, 4), 18: 1} + True >>> cdf(X) Lambda(_z, erf(sqrt(2)*_z/2)/2 + 1/2) diff --git a/sympy/tensor/index_methods.py b/sympy/tensor/index_methods.py index 720258cd94..a1b9b4e137 100644 --- a/sympy/tensor/index_methods.py +++ b/sympy/tensor/index_methods.py @@ -32,8 +32,8 @@ def _remove_repeated(inds): >>> from sympy.tensor.index_methods import _remove_repeated >>> l1 = [1, 2, 3, 2] - >>> _remove_repeated(l1) - (set([1, 3]), (2,)) + >>> _remove_repeated(l1) == ({1, 3}, (2,)) + True """ sum_index = {} @@ -54,10 +54,10 @@ def _get_indices_Mul(expr, return_dummies=False): >>> i, j, k = map(Idx, ['i', 'j', 'k']) >>> x = IndexedBase('x') >>> y = IndexedBase('y') - >>> _get_indices_Mul(x[i, k]*y[j, k]) - (set([i, j]), {}) - >>> _get_indices_Mul(x[i, k]*y[j, k], return_dummies=True) - (set([i, j]), {}, (k,)) + >>> _get_indices_Mul(x[i, k]*y[j, k]) == ({i, j}, {}) + True + >>> _get_indices_Mul(x[i, k]*y[j, k], return_dummies=True) == ({i, j}, {}, (k,)) + True """ @@ -111,13 +111,12 @@ def _get_indices_Pow(expr): >>> A = IndexedBase('A') >>> x = IndexedBase('x') >>> i, j, k = map(Idx, ['i', 'j', 'k']) - >>> _get_indices_Pow(exp(A[i, j]*x[j])) - (set([i]), {}) - >>> _get_indices_Pow(Pow(x[i], x[i])) - (set([i]), {}) - >>> _get_indices_Pow(Pow(A[i, j]*x[j], x[i])) - (set([i]), {}) - + >>> _get_indices_Pow(exp(A[i, j]*x[j])) == ({i}, {}) + True + >>> _get_indices_Pow(Pow(x[i], x[i])) == ({i}, {}) + True + >>> _get_indices_Pow(Pow(A[i, j]*x[j], x[i])) == ({i}, {}) + True """ base, exp = expr.as_base_exp() binds, bsyms = get_indices(base) @@ -150,9 +149,8 @@ def _get_indices_Add(expr): >>> i, j, k = map(Idx, ['i', 'j', 'k']) >>> x = IndexedBase('x') >>> y = IndexedBase('y') - >>> _get_indices_Add(x[i] + x[k]*y[i, k]) - (set([i]), {}) - + >>> _get_indices_Add(x[i] + x[k]*y[i, k]) == ({i}, {}) + True """ inds = list(map(get_indices, expr.args)) @@ -193,14 +191,14 @@ def get_indices(expr): The indices of the total expression is determined, Repeated indices imply a summation, for instance the trace of a matrix A: - >>> get_indices(A[i, i]) - (set(), {}) + >>> get_indices(A[i, i]) == (set(), {}) + True In the case of many terms, the terms are required to have identical outer indices. Else an IndexConformanceException is raised. - >>> get_indices(x[i] + A[i, j]*y[j]) - (set([i]), {}) + >>> get_indices(x[i] + A[i, j]*y[j]) == ({i}, {}) + True :Exceptions: @@ -215,8 +213,8 @@ def get_indices(expr): level. This implies that dummies inside parenthesis are assumed to be summed first, so that the following expression is handled gracefully: - >>> get_indices((x[i] + A[i, j]*y[j])*x[j]) - (set([i, j]), {}) + >>> get_indices((x[i] + A[i, j]*y[j])*x[j]) == ({i, j}, {}) + True This is correct and may appear convenient, but you need to be careful with this as SymPy will happily .expand() the product, if requested. The @@ -306,10 +304,10 @@ def get_contraction_structure(expr): >>> from sympy.tensor import IndexedBase, Idx >>> x, y, A = map(IndexedBase, ['x', 'y', 'A']) >>> i, j, k, l = map(Idx, ['i', 'j', 'k', 'l']) - >>> get_contraction_structure(x[i]*y[i] + A[j, j]) - {(i,): set([x[i]*y[i]]), (j,): set([A[j, j]])} - >>> get_contraction_structure(x[i]*y[j]) - {None: set([x[i]*y[j]])} + >>> get_contraction_structure(x[i]*y[i] + A[j, j]) == {(i,): {x[i]*y[i]}, (j,): {A[j, j]}} + True + >>> get_contraction_structure(x[i]*y[j]) == {None: {x[i]*y[j]}} + True A multiplication of contracted factors results in nested dicts representing the internal contractions. @@ -320,13 +318,13 @@ def get_contraction_structure(expr): In this case, the product has no contractions: - >>> d[None] - set([x[i, i]*y[j, j]]) + >>> d[None] == {x[i, i]*y[j, j]} + True Factors are contracted "first": - >>> sorted(d[x[i, i]*y[j, j]], key=default_sort_key) - [{(i,): set([x[i, i]])}, {(j,): set([y[j, j]])}] + >>> sorted(d[x[i, i]*y[j, j]], key=default_sort_key) == [{(i,): {x[i, i]}}, {(j,): {y[j, j]}}] + True A parenthesized Add object is also returned as a nested dictionary. The term containing the parenthesis is a Mul with a contraction among the @@ -336,22 +334,22 @@ def get_contraction_structure(expr): >>> d = get_contraction_structure(x[i]*(y[i] + A[i, j]*x[j])) >>> sorted(d.keys(), key=default_sort_key) [(x[j]*A[i, j] + y[i])*x[i], (i,)] - >>> d[(i,)] - set([(x[j]*A[i, j] + y[i])*x[i]]) - >>> d[x[i]*(A[i, j]*x[j] + y[i])] - [{None: set([y[i]]), (j,): set([x[j]*A[i, j]])}] + >>> d[(i,)] == {(x[j]*A[i, j] + y[i])*x[i]} + True + >>> d[x[i]*(A[i, j]*x[j] + y[i])] ==[{None: {y[i]}, (j,): {x[j]*A[i, j]}}] + True Powers with contractions in either base or exponent will also be found as keys in the dictionary, mapping to a list of results from recursive calls: >>> d = get_contraction_structure(A[j, j]**A[i, i]) - >>> d[None] - set([A[j, j]**A[i, i]]) + >>> d[None] == {A[j, j]**A[i, i]} + True >>> nested_contractions = d[A[j, j]**A[i, i]] - >>> nested_contractions[0] - {(j,): set([A[j, j]])} - >>> nested_contractions[1] - {(i,): set([A[i, i]])} + >>> nested_contractions[0] == {(j,): {A[j, j]}} + True + >>> nested_contractions[1] == {(i,): {A[i, i]}} + True The description of the contraction structure may appear complicated when represented with a string in the above examples, but it is easy to iterate diff --git a/sympy/tensor/indexed.py b/sympy/tensor/indexed.py index fad892a2ab..0606711d53 100644 --- a/sympy/tensor/indexed.py +++ b/sympy/tensor/indexed.py @@ -79,10 +79,10 @@ get_indices() and get_contraction_structure(): >>> from sympy.tensor import get_indices, get_contraction_structure - >>> get_indices(A[i, j, j]) - (set([i]), {}) - >>> get_contraction_structure(A[i, j, j]) - {(j,): set([A[i, j, j]])} + >>> get_indices(A[i, j, j]) == ({i}, {}) + True + >>> get_contraction_structure(A[i, j, j]) == {(j,): {A[i, j, j]}} + True See the appropriate docstrings for a detailed explanation of the output. diff --git a/sympy/tensor/tensor.py b/sympy/tensor/tensor.py index b4a6496e9e..15ce32912c 100644 --- a/sympy/tensor/tensor.py +++ b/sympy/tensor/tensor.py @@ -1119,10 +1119,11 @@ def parse_data(data): ======== >>> from sympy.tensor.tensor import _TensorDataLazyEvaluator - >>> _TensorDataLazyEvaluator.parse_data([1, 3, -6, 12]) + >>> from sympy.printing import sstr + >>> print(sstr(_TensorDataLazyEvaluator.parse_data([1, 3, -6, 12]))) [1 3 -6 12] - >>> _TensorDataLazyEvaluator.parse_data([[1, 2], [4, 7]]) + >>> print(sstr(_TensorDataLazyEvaluator.parse_data([[1, 2], [4, 7]]))) [[1 2] [4 7]] """ @@ -1379,6 +1380,7 @@ class TensorIndexType(Basic): ======== >>> from sympy.tensor.tensor import TensorIndexType + >>> from sympy.printing import sstr >>> Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') >>> Lorentz.metric metric(Lorentz,Lorentz) @@ -1387,9 +1389,9 @@ class TensorIndexType(Basic): fixed basis: >>> Lorentz.data = [1, -1, -1, -1] - >>> Lorentz + >>> print(sstr(Lorentz)) TensorIndexType(Lorentz, 0) - >>> Lorentz.data + >>> print(sstr(Lorentz.data)) [[1 0 0 0] [0 -1 0 0] [0 0 -1 0] @@ -2017,6 +2019,7 @@ class TensorHead(Basic): examples. >>> from sympy.tensor.tensor import tensor_indices, tensorhead + >>> from sympy.printing import sstr >>> Lorentz.data = [1, -1, -1, -1] >>> i0, i1 = tensor_indices('i0:2', Lorentz) >>> A.data = [[j+2*i for j in range(4)] for i in range(4)] @@ -2036,7 +2039,7 @@ class TensorHead(Basic): To view the data, just type: - >>> A.data + >>> print(sstr(A.data)) [[0 1 2 3] [2 3 4 5] [4 5 6 7] @@ -2045,13 +2048,13 @@ class TensorHead(Basic): Turning to a tensor expression, covariant indices get the corresponding components data corrected by the metric: - >>> A(i0, -i1).data + >>> print(sstr(A(i0, -i1).data)) [[0 -1 -2 -3] [2 -3 -4 -5] [4 -5 -6 -7] [6 -7 -8 -9]] - >>> A(-i0, -i1).data + >>> print(sstr(A(-i0, -i1).data)) [[0 -1 -2 -3] [-2 3 4 5] [-4 5 6 7] @@ -2059,7 +2062,7 @@ class TensorHead(Basic): while if all indices are contravariant, the ``ndarray`` remains the same - >>> A(i0, i1).data + >>> print(sstr(A(i0, i1).data)) [[0 1 2 3] [2 3 4 5] [4 5 6 7] @@ -2097,7 +2100,7 @@ class TensorHead(Basic): Now it is possible to retrieve the contravariant form of the Electromagnetic tensor: - >>> F(i0, i1).data + >>> print(sstr(F(i0, i1).data)) [[0 -E_x/c -E_y/c -E_z/c] [E_x/c 0 -B_z B_y] [E_y/c B_z 0 -B_x] @@ -2105,7 +2108,7 @@ class TensorHead(Basic): and the mixed contravariant-covariant form: - >>> F(i0, -i1).data + >>> print(sstr(F(i0, -i1).data)) [[0 E_x/c E_y/c E_z/c] [E_x/c 0 B_z -B_y] [E_y/c -B_z 0 B_x] @@ -2135,9 +2138,9 @@ class TensorHead(Basic): The contravariant and covariant components are, respectively: - >>> P(i0).data + >>> print(sstr(P(i0).data)) [E p_x p_y p_z] - >>> P(-i0).data + >>> print(sstr(P(-i0).data)) [E -p_x -p_y -p_z] The contraction of a 1-index tensor by itself is usually indicated by a diff --git a/sympy/tests/test_wester.py b/sympy/tests/test_wester.py index 8df11e8a25..a285dbc7a9 100644 --- a/sympy/tests/test_wester.py +++ b/sympy/tests/test_wester.py @@ -1898,6 +1898,8 @@ def test_R18(): @slow @XFAIL def test_R19(): + if ON_TRAVIS: + skip("Too slow for travis.") k = symbols('k', integer=True, positive=True) Sm = Sum(1/((3*k + 1)*(3*k + 2)*(3*k + 3)), (k, 0, oo)) T = Sm.doit() @@ -2298,6 +2300,8 @@ def test_V12(): @slow @XFAIL def test_V13(): + if ON_TRAVIS: + skip("Too slow for travis.") r1 = integrate(1/(6 + 3*cos(x) + 4*sin(x)), x) # expression not simplified, returns: -sqrt(11)*I*log(tan(x/2) + 4/3 # - sqrt(11)*I/3)/11 + sqrt(11)*I*log(tan(x/2) + 4/3 + sqrt(11)*I/3)/11 @@ -2335,6 +2339,8 @@ def test_V16(): @slow @XFAIL def test_V17(): + if ON_TRAVIS: + skip("Too slow for travis.") r1 = integrate((diff(f(x), x)*g(x) - f(x)*diff(g(x), x))/(f(x)**2 - g(x)**2), x) # integral not calculated @@ -2383,6 +2389,8 @@ def test_W5(): @XFAIL @slow def test_W6(): + if ON_TRAVIS: + skip("Too slow for travis.") # integral is not calculated assert integrate(sqrt(2 - 2*cos(2*x))/2, (x, -3*pi/4, -pi/4)) == sqrt(2) @@ -2830,6 +2838,8 @@ def test_Y2(): @slow @XFAIL def test_Y3(): + if ON_TRAVIS: + skip("Too slow.") t = symbols('t', extended_real=True, positive=True) w = symbols('w', extended_real=True) s = symbols('s') diff --git a/sympy/utilities/autowrap.py b/sympy/utilities/autowrap.py index fcd128ea97..d6d52efd79 100644 --- a/sympy/utilities/autowrap.py +++ b/sympy/utilities/autowrap.py @@ -830,8 +830,8 @@ def ufuncify(args, expr, language=None, backend='numpy', tempdir=None, >>> from sympy.abc import x, y >>> import numpy as np >>> f = ufuncify((x, y), y + x**2) - >>> type(f) - numpy.ufunc + >>> type(f) is np.ufunc + True >>> f([1, 2, 3], 2) array([ 3., 6., 11.]) >>> f(np.arange(5), 3) diff --git a/sympy/utilities/codegen.py b/sympy/utilities/codegen.py index 0b9ca502a4..eee929b73c 100644 --- a/sympy/utilities/codegen.py +++ b/sympy/utilities/codegen.py @@ -1485,8 +1485,8 @@ def make_routine(name, expr, argument_sequence=None, language="F95"): [x, y, f, g] >>> [arg.name for arg in r.result_variables] [f, g] - >>> r.local_vars - set() + >>> r.local_vars == set() + True Another more complicated example with a mixture of specified and automatically-assigned names. Also has Matrix output. diff --git a/sympy/utilities/exceptions.py b/sympy/utilities/exceptions.py index ca8bc16e35..c07211010d 100644 --- a/sympy/utilities/exceptions.py +++ b/sympy/utilities/exceptions.py @@ -37,10 +37,10 @@ class SymPyDeprecationWarning(DeprecationWarning): To provide additional information, create an instance of this class in this way: - >>> SymPyDeprecationWarning( + >>> print(SymPyDeprecationWarning( ... feature="Such and such", ... last_supported_version="1.2.3", - ... useinstead="this other feature") + ... useinstead="this other feature")) Such and such has been deprecated. It will be last supported in SymPy version 1.2.3. Use this other feature instead. @@ -51,36 +51,36 @@ class in this way: ``useinstead`` can be omitted. In this case the corresponding sentence will not be shown: - >>> SymPyDeprecationWarning(feature="Such and such", - ... useinstead="this other feature") + >>> print(SymPyDeprecationWarning(feature="Such and such", + ... useinstead="this other feature")) Such and such has been deprecated. Use this other feature instead. You can still provide the argument value. If it is a string, it will be appended to the end of the message: - >>> SymPyDeprecationWarning( + >>> print(SymPyDeprecationWarning( ... feature="Such and such", ... useinstead="this other feature", - ... value="Contact the developers for further information.") + ... value="Contact the developers for further information.")) Such and such has been deprecated. Use this other feature instead. Contact the developers for further information. If, however, the argument value does not hold a string, a string representation of the object will be appended to the message: - >>> SymPyDeprecationWarning( + >>> print(SymPyDeprecationWarning( ... feature="Such and such", ... useinstead="this other feature", - ... value=[1,2,3]) + ... value=[1,2,3])) Such and such has been deprecated. Use this other feature instead. ([1, 2, 3]) To associate an issue with a deprecation, use the ``issue`` flag. - >>> SymPyDeprecationWarning( + >>> print(SymPyDeprecationWarning( ... feature="Old feature", ... useinstead="new feature", - ... issue=5241) + ... issue=5241)) Old feature has been deprecated. Use new feature instead. See https://github.com/sympy/sympy/issues/5241 for more info. @@ -92,10 +92,10 @@ class in this way: which it was deprecated. Use the ``deprecated_since_version`` flag for this. - >>> SymPyDeprecationWarning( + >>> print(SymPyDeprecationWarning( ... feature="Old feature", ... useinstead="new feature", - ... deprecated_since_version="0.7.2") + ... deprecated_since_version="0.7.2")) Old feature has been deprecated since SymPy 0.7.2. Use new feature instead. diff --git a/sympy/utilities/iterables.py b/sympy/utilities/iterables.py index 619091b1c6..80cac74069 100644 --- a/sympy/utilities/iterables.py +++ b/sympy/utilities/iterables.py @@ -121,9 +121,8 @@ def reshape(seq, how): >>> reshape(tuple(seq), ([1], 1, (2,))) (([1], 2, (3, 4)), ([5], 6, (7, 8))) - >>> reshape(list(range(12)), [2, [3], {2}, (1, (3,), 1)]) - [[0, 1, [2, 3, 4], set([5, 6]), (7, (8, 9, 10), 11)]] - + >>> reshape(list(range(12)), [2, [3], {2}, (1, (3,), 1)]) == [[0, 1, [2, 3, 4], {5, 6}, (7, (8, 9, 10), 11)]] + True """ m = sum(flatten(how)) n, rem = divmod(len(seq), m) @@ -199,8 +198,8 @@ def multiset(seq): ======== >>> from sympy.utilities.iterables import multiset - >>> multiset('mississippi') - {'i': 4, 'm': 1, 'p': 2, 's': 4} + >>> multiset('mississippi') == {'i': 4, 'm': 1, 'p': 2, 's': 4} + True See Also ======== @@ -596,7 +595,7 @@ def capture(func): ... >>> 'hello' in capture(foo) # foo, not foo() True - >>> capture(lambda: pprint(2/x)) + >>> capture(lambda: pprint(2/x, use_unicode=False)) '2\\n-\\nx\\n' """ @@ -622,26 +621,29 @@ def sift(seq, keyfunc): Examples ======== + >>> from collections import defaultdict >>> from sympy.utilities import sift >>> from sympy.abc import x, y - >>> from sympy import sqrt, exp + >>> from sympy import sqrt, exp, E - >>> sift(range(5), lambda x: x % 2) - {0: [0, 2, 4], 1: [1, 3]} + >>> sift(range(5), lambda x: x % 2) == defaultdict(int, {0: [0, 2, 4], 1: [1, 3]}) + True sift() returns a defaultdict() object, so any key that has no matches will give []. - >>> sift([x], lambda x: x.is_commutative) - {True: [x]} - >>> _[False] + >>> dl = sift([x], lambda x: x.is_commutative) + >>> dl == defaultdict(list, {True: [x]}) + True + >>> dl[False] [] Sometimes you won't know how many keys you will get: >>> sift([sqrt(x), exp(x), (y**x)**2], - ... lambda x: x.as_base_exp()[0]) - {E: [exp(x)], x: [sqrt(x)], y: [y**(2*x)]} + ... lambda x: x.as_base_exp()[0]) == defaultdict(list, + ... {E: [exp(x)], x: [sqrt(x)], y: [y**(2*x)]}) + True If you need to sort the sifted items it might be better to use ``ordered`` which can economically apply multiple sort keys @@ -1774,8 +1776,8 @@ def necklaces(n, k, free=False): >>> B = [show('ABC', i) for i in bracelets(3, 3)] >>> N = [show('ABC', i) for i in necklaces(3, 3)] - >>> set(N) - set(B) - set(['ACB']) + >>> set(N) - set(B) == {'ACB'} + True >>> list(necklaces(4, 2)) [(0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 1), diff --git a/sympy/utilities/lambdify.py b/sympy/utilities/lambdify.py index 0c186e471a..b3c39c1db6 100644 --- a/sympy/utilities/lambdify.py +++ b/sympy/utilities/lambdify.py @@ -228,10 +228,10 @@ def lambdify(args, expr, modules=None, printer=None, use_imps=True, >>> f = lambdify((x,y), tan(x*y), "numpy") >>> f(1, 2) - -2.18503986326 + -2.1850398632615189 >>> from numpy import array >>> f(array([1, 2, 3]), array([2, 3, 5])) - [-2.18503986 -0.29100619 -0.8559934 ] + array([-2.18503986, -0.29100619, -0.8559934 ]) (3) Use a dictionary defining custom functions: diff --git a/sympy/vector/coordsysrect.py b/sympy/vector/coordsysrect.py index a66ea3bde4..ba335c61ae 100644 --- a/sympy/vector/coordsysrect.py +++ b/sympy/vector/coordsysrect.py @@ -304,12 +304,12 @@ def scalar_map(self, other): ======== >>> from sympy.vector import CoordSysCartesian - >>> from sympy import Symbol + >>> from sympy import Symbol, cos, sin >>> A = CoordSysCartesian('A') >>> q = Symbol('q') >>> B = A.orient_new_axis('B', q, A.k) - >>> A.scalar_map(B) - {A.x: B.x*cos(q) - B.y*sin(q), A.y: B.x*sin(q) + B.y*cos(q), A.z: B.z} + >>> A.scalar_map(B) == {A.x: B.x*cos(q) - B.y*sin(q), A.y: B.x*sin(q) + B.y*cos(q), A.z: B.z} + True """ diff --git a/sympy/vector/vector.py b/sympy/vector/vector.py index a27e417cee..4f4835619f 100755 --- a/sympy/vector/vector.py +++ b/sympy/vector/vector.py @@ -33,8 +33,8 @@ def components(self): >>> from sympy.vector import CoordSysCartesian >>> C = CoordSysCartesian('C') >>> v = 3*C.i + 4*C.j + 5*C.k - >>> v.components - {C.i: 3, C.j: 4, C.k: 5} + >>> v.components == {C.i: 3, C.j: 4, C.k: 5} + True """ #The '_components' attribute is defined according to the