Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +3 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/__init__.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/_print_helpers.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/add.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/assumptions.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/backend.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/cache.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/compatibility.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/containers.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/core.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/evalf.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/exprtools.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/facts.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/intfunc.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/mul.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/multidimensional.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/parameters.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/power.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/random.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/relational.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/rules.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/singleton.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/symbol.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/__pycache__/sympify.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/benchmarks/bench_assumptions.py +12 -0
- .venv/lib/python3.11/site-packages/sympy/core/benchmarks/bench_basic.py +15 -0
- .venv/lib/python3.11/site-packages/sympy/core/benchmarks/bench_expand.py +23 -0
- .venv/lib/python3.11/site-packages/sympy/core/benchmarks/bench_numbers.py +92 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/__init__.py +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/__pycache__/test_assumptions.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/__pycache__/test_containers.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/__pycache__/test_facts.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/__pycache__/test_kind.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/__pycache__/test_power.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/__pycache__/test_random.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/__pycache__/test_subs.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/__pycache__/test_traversal.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/test_arit.py +2472 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/test_assumptions.py +1335 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/test_basic.py +333 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/test_cache.py +91 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/test_compatibility.py +6 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/test_complex.py +226 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/test_constructor_postprocessor.py +87 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/test_containers.py +217 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/test_count_ops.py +155 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/test_diff.py +160 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/test_equal.py +89 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/test_eval.py +95 -0
- .venv/lib/python3.11/site-packages/sympy/core/tests/test_evalf.py +734 -0
.gitattributes
CHANGED
|
@@ -439,3 +439,6 @@ tuning-competition-baseline/.venv/lib/python3.11/site-packages/nvidia/cudnn/lib/
|
|
| 439 |
.venv/lib/python3.11/site-packages/transformers/models/whisper/__pycache__/modeling_whisper.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 440 |
.venv/lib/python3.11/site-packages/transformers/utils/__pycache__/dummy_tf_objects.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 441 |
.venv/lib/python3.11/site-packages/transformers/utils/__pycache__/dummy_pt_objects.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
.venv/lib/python3.11/site-packages/transformers/models/whisper/__pycache__/modeling_whisper.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 440 |
.venv/lib/python3.11/site-packages/transformers/utils/__pycache__/dummy_tf_objects.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 441 |
.venv/lib/python3.11/site-packages/transformers/utils/__pycache__/dummy_pt_objects.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 442 |
+
.venv/lib/python3.11/site-packages/sympy/simplify/__pycache__/hyperexpand.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 443 |
+
.venv/lib/python3.11/site-packages/sympy/crypto/__pycache__/crypto.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
| 444 |
+
.venv/lib/python3.11/site-packages/sympy/simplify/tests/__pycache__/test_simplify.cpython-311.pyc filter=lfs diff=lfs merge=lfs -text
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (4.29 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/_print_helpers.cpython-311.pyc
ADDED
|
Binary file (2.6 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/add.cpython-311.pyc
ADDED
|
Binary file (62.5 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/assumptions.cpython-311.pyc
ADDED
|
Binary file (23.9 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/backend.cpython-311.pyc
ADDED
|
Binary file (5.84 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/cache.cpython-311.pyc
ADDED
|
Binary file (9.64 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/compatibility.cpython-311.pyc
ADDED
|
Binary file (1.44 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/containers.cpython-311.pyc
ADDED
|
Binary file (21.3 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/core.cpython-311.pyc
ADDED
|
Binary file (1.3 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/evalf.cpython-311.pyc
ADDED
|
Binary file (74.8 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/exprtools.cpython-311.pyc
ADDED
|
Binary file (70.1 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/facts.cpython-311.pyc
ADDED
|
Binary file (27.1 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/intfunc.cpython-311.pyc
ADDED
|
Binary file (15.4 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/mul.cpython-311.pyc
ADDED
|
Binary file (98.6 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/multidimensional.cpython-311.pyc
ADDED
|
Binary file (5.66 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/parameters.cpython-311.pyc
ADDED
|
Binary file (5.87 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/power.cpython-311.pyc
ADDED
|
Binary file (91.2 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/random.cpython-311.pyc
ADDED
|
Binary file (9.91 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/relational.cpython-311.pyc
ADDED
|
Binary file (64.9 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/rules.cpython-311.pyc
ADDED
|
Binary file (2.5 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/singleton.cpython-311.pyc
ADDED
|
Binary file (7.77 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/symbol.cpython-311.pyc
ADDED
|
Binary file (40 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/__pycache__/sympify.cpython-311.pyc
ADDED
|
Binary file (21.9 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/benchmarks/bench_assumptions.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core import Symbol, Integer
|
| 2 |
+
|
| 3 |
+
x = Symbol('x')
|
| 4 |
+
i3 = Integer(3)
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def timeit_x_is_integer():
|
| 8 |
+
x.is_integer
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def timeit_Integer_is_irrational():
|
| 12 |
+
i3.is_irrational
|
.venv/lib/python3.11/site-packages/sympy/core/benchmarks/bench_basic.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core import symbols, S
|
| 2 |
+
|
| 3 |
+
x, y = symbols('x,y')
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def timeit_Symbol_meth_lookup():
|
| 7 |
+
x.diff # no call, just method lookup
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def timeit_S_lookup():
|
| 11 |
+
S.Exp1
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def timeit_Symbol_eq_xy():
|
| 15 |
+
x == y
|
.venv/lib/python3.11/site-packages/sympy/core/benchmarks/bench_expand.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core import symbols, I
|
| 2 |
+
|
| 3 |
+
x, y, z = symbols('x,y,z')
|
| 4 |
+
|
| 5 |
+
p = 3*x**2*y*z**7 + 7*x*y*z**2 + 4*x + x*y**4
|
| 6 |
+
e = (x + y + z + 1)**32
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def timeit_expand_nothing_todo():
|
| 10 |
+
p.expand()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def bench_expand_32():
|
| 14 |
+
"""(x+y+z+1)**32 -> expand"""
|
| 15 |
+
e.expand()
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def timeit_expand_complex_number_1():
|
| 19 |
+
((2 + 3*I)**1000).expand(complex=True)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def timeit_expand_complex_number_2():
|
| 23 |
+
((2 + 3*I/4)**1000).expand(complex=True)
|
.venv/lib/python3.11/site-packages/sympy/core/benchmarks/bench_numbers.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.numbers import Integer, Rational, pi, oo
|
| 2 |
+
from sympy.core.intfunc import integer_nthroot, igcd
|
| 3 |
+
from sympy.core.singleton import S
|
| 4 |
+
|
| 5 |
+
i3 = Integer(3)
|
| 6 |
+
i4 = Integer(4)
|
| 7 |
+
r34 = Rational(3, 4)
|
| 8 |
+
q45 = Rational(4, 5)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def timeit_Integer_create():
|
| 12 |
+
Integer(2)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def timeit_Integer_int():
|
| 16 |
+
int(i3)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def timeit_neg_one():
|
| 20 |
+
-S.One
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def timeit_Integer_neg():
|
| 24 |
+
-i3
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def timeit_Integer_abs():
|
| 28 |
+
abs(i3)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def timeit_Integer_sub():
|
| 32 |
+
i3 - i3
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def timeit_abs_pi():
|
| 36 |
+
abs(pi)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def timeit_neg_oo():
|
| 40 |
+
-oo
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def timeit_Integer_add_i1():
|
| 44 |
+
i3 + 1
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def timeit_Integer_add_ij():
|
| 48 |
+
i3 + i4
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def timeit_Integer_add_Rational():
|
| 52 |
+
i3 + r34
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def timeit_Integer_mul_i4():
|
| 56 |
+
i3*4
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def timeit_Integer_mul_ij():
|
| 60 |
+
i3*i4
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def timeit_Integer_mul_Rational():
|
| 64 |
+
i3*r34
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def timeit_Integer_eq_i3():
|
| 68 |
+
i3 == 3
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def timeit_Integer_ed_Rational():
|
| 72 |
+
i3 == r34
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def timeit_integer_nthroot():
|
| 76 |
+
integer_nthroot(100, 2)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def timeit_number_igcd_23_17():
|
| 80 |
+
igcd(23, 17)
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def timeit_number_igcd_60_3600():
|
| 84 |
+
igcd(60, 3600)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def timeit_Rational_add_r1():
|
| 88 |
+
r34 + 1
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def timeit_Rational_add_rq():
|
| 92 |
+
r34 + q45
|
.venv/lib/python3.11/site-packages/sympy/core/tests/__init__.py
ADDED
|
File without changes
|
.venv/lib/python3.11/site-packages/sympy/core/tests/__pycache__/test_assumptions.cpython-311.pyc
ADDED
|
Binary file (67.3 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/tests/__pycache__/test_containers.cpython-311.pyc
ADDED
|
Binary file (21.9 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/tests/__pycache__/test_facts.cpython-311.pyc
ADDED
|
Binary file (21.5 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/tests/__pycache__/test_kind.cpython-311.pyc
ADDED
|
Binary file (4.98 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/tests/__pycache__/test_power.cpython-311.pyc
ADDED
|
Binary file (67.2 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/tests/__pycache__/test_random.cpython-311.pyc
ADDED
|
Binary file (3.16 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/tests/__pycache__/test_subs.cpython-311.pyc
ADDED
|
Binary file (92.2 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/tests/__pycache__/test_traversal.cpython-311.pyc
ADDED
|
Binary file (10.3 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/sympy/core/tests/test_arit.py
ADDED
|
@@ -0,0 +1,2472 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.add import Add
|
| 2 |
+
from sympy.core.basic import Basic
|
| 3 |
+
from sympy.core.mod import Mod
|
| 4 |
+
from sympy.core.mul import Mul
|
| 5 |
+
from sympy.core.numbers import (Float, I, Integer, Rational, comp, nan,
|
| 6 |
+
oo, pi, zoo)
|
| 7 |
+
from sympy.core.power import Pow
|
| 8 |
+
from sympy.core.singleton import S
|
| 9 |
+
from sympy.core.symbol import (Dummy, Symbol, symbols)
|
| 10 |
+
from sympy.core.sympify import sympify
|
| 11 |
+
from sympy.functions.combinatorial.factorials import factorial
|
| 12 |
+
from sympy.functions.elementary.complexes import (im, re, sign)
|
| 13 |
+
from sympy.functions.elementary.exponential import (exp, log)
|
| 14 |
+
from sympy.functions.elementary.integers import floor
|
| 15 |
+
from sympy.functions.elementary.miscellaneous import (Max, sqrt)
|
| 16 |
+
from sympy.functions.elementary.trigonometric import (atan, cos, sin)
|
| 17 |
+
from sympy.polys.polytools import Poly
|
| 18 |
+
from sympy.sets.sets import FiniteSet
|
| 19 |
+
|
| 20 |
+
from sympy.core.parameters import distribute, evaluate
|
| 21 |
+
from sympy.core.expr import unchanged
|
| 22 |
+
from sympy.utilities.iterables import permutations
|
| 23 |
+
from sympy.testing.pytest import XFAIL, raises, warns
|
| 24 |
+
from sympy.utilities.exceptions import SymPyDeprecationWarning
|
| 25 |
+
from sympy.core.random import verify_numerically
|
| 26 |
+
from sympy.functions.elementary.trigonometric import asin
|
| 27 |
+
|
| 28 |
+
from itertools import product
|
| 29 |
+
|
| 30 |
+
a, c, x, y, z = symbols('a,c,x,y,z')
|
| 31 |
+
b = Symbol("b", positive=True)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def same_and_same_prec(a, b):
|
| 35 |
+
# stricter matching for Floats
|
| 36 |
+
return a == b and a._prec == b._prec
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def test_bug1():
|
| 40 |
+
assert re(x) != x
|
| 41 |
+
x.series(x, 0, 1)
|
| 42 |
+
assert re(x) != x
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def test_Symbol():
|
| 46 |
+
e = a*b
|
| 47 |
+
assert e == a*b
|
| 48 |
+
assert a*b*b == a*b**2
|
| 49 |
+
assert a*b*b + c == c + a*b**2
|
| 50 |
+
assert a*b*b - c == -c + a*b**2
|
| 51 |
+
|
| 52 |
+
x = Symbol('x', complex=True, real=False)
|
| 53 |
+
assert x.is_imaginary is None # could be I or 1 + I
|
| 54 |
+
x = Symbol('x', complex=True, imaginary=False)
|
| 55 |
+
assert x.is_real is None # could be 1 or 1 + I
|
| 56 |
+
x = Symbol('x', real=True)
|
| 57 |
+
assert x.is_complex
|
| 58 |
+
x = Symbol('x', imaginary=True)
|
| 59 |
+
assert x.is_complex
|
| 60 |
+
x = Symbol('x', real=False, imaginary=False)
|
| 61 |
+
assert x.is_complex is None # might be a non-number
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def test_arit0():
|
| 65 |
+
p = Rational(5)
|
| 66 |
+
e = a*b
|
| 67 |
+
assert e == a*b
|
| 68 |
+
e = a*b + b*a
|
| 69 |
+
assert e == 2*a*b
|
| 70 |
+
e = a*b + b*a + a*b + p*b*a
|
| 71 |
+
assert e == 8*a*b
|
| 72 |
+
e = a*b + b*a + a*b + p*b*a + a
|
| 73 |
+
assert e == a + 8*a*b
|
| 74 |
+
e = a + a
|
| 75 |
+
assert e == 2*a
|
| 76 |
+
e = a + b + a
|
| 77 |
+
assert e == b + 2*a
|
| 78 |
+
e = a + b*b + a + b*b
|
| 79 |
+
assert e == 2*a + 2*b**2
|
| 80 |
+
e = a + Rational(2) + b*b + a + b*b + p
|
| 81 |
+
assert e == 7 + 2*a + 2*b**2
|
| 82 |
+
e = (a + b*b + a + b*b)*p
|
| 83 |
+
assert e == 5*(2*a + 2*b**2)
|
| 84 |
+
e = (a*b*c + c*b*a + b*a*c)*p
|
| 85 |
+
assert e == 15*a*b*c
|
| 86 |
+
e = (a*b*c + c*b*a + b*a*c)*p - Rational(15)*a*b*c
|
| 87 |
+
assert e == Rational(0)
|
| 88 |
+
e = Rational(50)*(a - a)
|
| 89 |
+
assert e == Rational(0)
|
| 90 |
+
e = b*a - b - a*b + b
|
| 91 |
+
assert e == Rational(0)
|
| 92 |
+
e = a*b + c**p
|
| 93 |
+
assert e == a*b + c**5
|
| 94 |
+
e = a/b
|
| 95 |
+
assert e == a*b**(-1)
|
| 96 |
+
e = a*2*2
|
| 97 |
+
assert e == 4*a
|
| 98 |
+
e = 2 + a*2/2
|
| 99 |
+
assert e == 2 + a
|
| 100 |
+
e = 2 - a - 2
|
| 101 |
+
assert e == -a
|
| 102 |
+
e = 2*a*2
|
| 103 |
+
assert e == 4*a
|
| 104 |
+
e = 2/a/2
|
| 105 |
+
assert e == a**(-1)
|
| 106 |
+
e = 2**a**2
|
| 107 |
+
assert e == 2**(a**2)
|
| 108 |
+
e = -(1 + a)
|
| 109 |
+
assert e == -1 - a
|
| 110 |
+
e = S.Half*(1 + a)
|
| 111 |
+
assert e == S.Half + a/2
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def test_div():
|
| 115 |
+
e = a/b
|
| 116 |
+
assert e == a*b**(-1)
|
| 117 |
+
e = a/b + c/2
|
| 118 |
+
assert e == a*b**(-1) + Rational(1)/2*c
|
| 119 |
+
e = (1 - b)/(b - 1)
|
| 120 |
+
assert e == (1 + -b)*((-1) + b)**(-1)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def test_pow_arit():
|
| 124 |
+
n1 = Rational(1)
|
| 125 |
+
n2 = Rational(2)
|
| 126 |
+
n5 = Rational(5)
|
| 127 |
+
e = a*a
|
| 128 |
+
assert e == a**2
|
| 129 |
+
e = a*a*a
|
| 130 |
+
assert e == a**3
|
| 131 |
+
e = a*a*a*a**Rational(6)
|
| 132 |
+
assert e == a**9
|
| 133 |
+
e = a*a*a*a**Rational(6) - a**Rational(9)
|
| 134 |
+
assert e == Rational(0)
|
| 135 |
+
e = a**(b - b)
|
| 136 |
+
assert e == Rational(1)
|
| 137 |
+
e = (a + Rational(1) - a)**b
|
| 138 |
+
assert e == Rational(1)
|
| 139 |
+
|
| 140 |
+
e = (a + b + c)**n2
|
| 141 |
+
assert e == (a + b + c)**2
|
| 142 |
+
assert e.expand() == 2*b*c + 2*a*c + 2*a*b + a**2 + c**2 + b**2
|
| 143 |
+
|
| 144 |
+
e = (a + b)**n2
|
| 145 |
+
assert e == (a + b)**2
|
| 146 |
+
assert e.expand() == 2*a*b + a**2 + b**2
|
| 147 |
+
|
| 148 |
+
e = (a + b)**(n1/n2)
|
| 149 |
+
assert e == sqrt(a + b)
|
| 150 |
+
assert e.expand() == sqrt(a + b)
|
| 151 |
+
|
| 152 |
+
n = n5**(n1/n2)
|
| 153 |
+
assert n == sqrt(5)
|
| 154 |
+
e = n*a*b - n*b*a
|
| 155 |
+
assert e == Rational(0)
|
| 156 |
+
e = n*a*b + n*b*a
|
| 157 |
+
assert e == 2*a*b*sqrt(5)
|
| 158 |
+
assert e.diff(a) == 2*b*sqrt(5)
|
| 159 |
+
assert e.diff(a) == 2*b*sqrt(5)
|
| 160 |
+
e = a/b**2
|
| 161 |
+
assert e == a*b**(-2)
|
| 162 |
+
|
| 163 |
+
assert sqrt(2*(1 + sqrt(2))) == (2*(1 + 2**S.Half))**S.Half
|
| 164 |
+
|
| 165 |
+
x = Symbol('x')
|
| 166 |
+
y = Symbol('y')
|
| 167 |
+
|
| 168 |
+
assert ((x*y)**3).expand() == y**3 * x**3
|
| 169 |
+
assert ((x*y)**-3).expand() == y**-3 * x**-3
|
| 170 |
+
|
| 171 |
+
assert (x**5*(3*x)**(3)).expand() == 27 * x**8
|
| 172 |
+
assert (x**5*(-3*x)**(3)).expand() == -27 * x**8
|
| 173 |
+
assert (x**5*(3*x)**(-3)).expand() == x**2 * Rational(1, 27)
|
| 174 |
+
assert (x**5*(-3*x)**(-3)).expand() == x**2 * Rational(-1, 27)
|
| 175 |
+
|
| 176 |
+
# expand_power_exp
|
| 177 |
+
_x = Symbol('x', zero=False)
|
| 178 |
+
_y = Symbol('y', zero=False)
|
| 179 |
+
assert (_x**(y**(x + exp(x + y)) + z)).expand(deep=False) == \
|
| 180 |
+
_x**z*_x**(y**(x + exp(x + y)))
|
| 181 |
+
assert (_x**(_y**(x + exp(x + y)) + z)).expand() == \
|
| 182 |
+
_x**z*_x**(_y**x*_y**(exp(x)*exp(y)))
|
| 183 |
+
|
| 184 |
+
n = Symbol('n', even=False)
|
| 185 |
+
k = Symbol('k', even=True)
|
| 186 |
+
o = Symbol('o', odd=True)
|
| 187 |
+
|
| 188 |
+
assert unchanged(Pow, -1, x)
|
| 189 |
+
assert unchanged(Pow, -1, n)
|
| 190 |
+
assert (-2)**k == 2**k
|
| 191 |
+
assert (-1)**k == 1
|
| 192 |
+
assert (-1)**o == -1
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def test_pow2():
|
| 196 |
+
# x**(2*y) is always (x**y)**2 but is only (x**2)**y if
|
| 197 |
+
# x.is_positive or y.is_integer
|
| 198 |
+
# let x = 1 to see why the following are not true.
|
| 199 |
+
assert (-x)**Rational(2, 3) != x**Rational(2, 3)
|
| 200 |
+
assert (-x)**Rational(5, 7) != -x**Rational(5, 7)
|
| 201 |
+
assert ((-x)**2)**Rational(1, 3) != ((-x)**Rational(1, 3))**2
|
| 202 |
+
assert sqrt(x**2) != x
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
def test_pow3():
|
| 206 |
+
assert sqrt(2)**3 == 2 * sqrt(2)
|
| 207 |
+
assert sqrt(2)**3 == sqrt(8)
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def test_mod_pow():
|
| 211 |
+
for s, t, u, v in [(4, 13, 497, 445), (4, -3, 497, 365),
|
| 212 |
+
(3.2, 2.1, 1.9, 0.1031015682350942), (S(3)/2, 5, S(5)/6, S(3)/32)]:
|
| 213 |
+
assert pow(S(s), t, u) == v
|
| 214 |
+
assert pow(S(s), S(t), u) == v
|
| 215 |
+
assert pow(S(s), t, S(u)) == v
|
| 216 |
+
assert pow(S(s), S(t), S(u)) == v
|
| 217 |
+
assert pow(S(2), S(10000000000), S(3)) == 1
|
| 218 |
+
assert pow(x, y, z) == x**y%z
|
| 219 |
+
raises(TypeError, lambda: pow(S(4), "13", 497))
|
| 220 |
+
raises(TypeError, lambda: pow(S(4), 13, "497"))
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
def test_pow_E():
|
| 224 |
+
assert 2**(y/log(2)) == S.Exp1**y
|
| 225 |
+
assert 2**(y/log(2)/3) == S.Exp1**(y/3)
|
| 226 |
+
assert 3**(1/log(-3)) != S.Exp1
|
| 227 |
+
assert (3 + 2*I)**(1/(log(-3 - 2*I) + I*pi)) == S.Exp1
|
| 228 |
+
assert (4 + 2*I)**(1/(log(-4 - 2*I) + I*pi)) == S.Exp1
|
| 229 |
+
assert (3 + 2*I)**(1/(log(-3 - 2*I, 3)/2 + I*pi/log(3)/2)) == 9
|
| 230 |
+
assert (3 + 2*I)**(1/(log(3 + 2*I, 3)/2)) == 9
|
| 231 |
+
# every time tests are run they will affirm with a different random
|
| 232 |
+
# value that this identity holds
|
| 233 |
+
while 1:
|
| 234 |
+
b = x._random()
|
| 235 |
+
r, i = b.as_real_imag()
|
| 236 |
+
if i:
|
| 237 |
+
break
|
| 238 |
+
assert verify_numerically(b**(1/(log(-b) + sign(i)*I*pi).n()), S.Exp1)
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
def test_pow_issue_3516():
|
| 242 |
+
assert 4**Rational(1, 4) == sqrt(2)
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
def test_pow_im():
|
| 246 |
+
for m in (-2, -1, 2):
|
| 247 |
+
for d in (3, 4, 5):
|
| 248 |
+
b = m*I
|
| 249 |
+
for i in range(1, 4*d + 1):
|
| 250 |
+
e = Rational(i, d)
|
| 251 |
+
assert (b**e - b.n()**e.n()).n(2, chop=1e-10) == 0
|
| 252 |
+
|
| 253 |
+
e = Rational(7, 3)
|
| 254 |
+
assert (2*x*I)**e == 4*2**Rational(1, 3)*(I*x)**e # same as Wolfram Alpha
|
| 255 |
+
im = symbols('im', imaginary=True)
|
| 256 |
+
assert (2*im*I)**e == 4*2**Rational(1, 3)*(I*im)**e
|
| 257 |
+
|
| 258 |
+
args = [I, I, I, I, 2]
|
| 259 |
+
e = Rational(1, 3)
|
| 260 |
+
ans = 2**e
|
| 261 |
+
assert Mul(*args, evaluate=False)**e == ans
|
| 262 |
+
assert Mul(*args)**e == ans
|
| 263 |
+
args = [I, I, I, 2]
|
| 264 |
+
e = Rational(1, 3)
|
| 265 |
+
ans = 2**e*(-I)**e
|
| 266 |
+
assert Mul(*args, evaluate=False)**e == ans
|
| 267 |
+
assert Mul(*args)**e == ans
|
| 268 |
+
args.append(-3)
|
| 269 |
+
ans = (6*I)**e
|
| 270 |
+
assert Mul(*args, evaluate=False)**e == ans
|
| 271 |
+
assert Mul(*args)**e == ans
|
| 272 |
+
args.append(-1)
|
| 273 |
+
ans = (-6*I)**e
|
| 274 |
+
assert Mul(*args, evaluate=False)**e == ans
|
| 275 |
+
assert Mul(*args)**e == ans
|
| 276 |
+
|
| 277 |
+
args = [I, I, 2]
|
| 278 |
+
e = Rational(1, 3)
|
| 279 |
+
ans = (-2)**e
|
| 280 |
+
assert Mul(*args, evaluate=False)**e == ans
|
| 281 |
+
assert Mul(*args)**e == ans
|
| 282 |
+
args.append(-3)
|
| 283 |
+
ans = (6)**e
|
| 284 |
+
assert Mul(*args, evaluate=False)**e == ans
|
| 285 |
+
assert Mul(*args)**e == ans
|
| 286 |
+
args.append(-1)
|
| 287 |
+
ans = (-6)**e
|
| 288 |
+
assert Mul(*args, evaluate=False)**e == ans
|
| 289 |
+
assert Mul(*args)**e == ans
|
| 290 |
+
assert Mul(Pow(-1, Rational(3, 2), evaluate=False), I, I) == I
|
| 291 |
+
assert Mul(I*Pow(I, S.Half, evaluate=False)) == sqrt(I)*I
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
def test_real_mul():
|
| 295 |
+
assert Float(0) * pi * x == 0
|
| 296 |
+
assert set((Float(1) * pi * x).args) == {Float(1), pi, x}
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
def test_ncmul():
|
| 300 |
+
A = Symbol("A", commutative=False)
|
| 301 |
+
B = Symbol("B", commutative=False)
|
| 302 |
+
C = Symbol("C", commutative=False)
|
| 303 |
+
assert A*B != B*A
|
| 304 |
+
assert A*B*C != C*B*A
|
| 305 |
+
assert A*b*B*3*C == 3*b*A*B*C
|
| 306 |
+
assert A*b*B*3*C != 3*b*B*A*C
|
| 307 |
+
assert A*b*B*3*C == 3*A*B*C*b
|
| 308 |
+
|
| 309 |
+
assert A + B == B + A
|
| 310 |
+
assert (A + B)*C != C*(A + B)
|
| 311 |
+
|
| 312 |
+
assert C*(A + B)*C != C*C*(A + B)
|
| 313 |
+
|
| 314 |
+
assert A*A == A**2
|
| 315 |
+
assert (A + B)*(A + B) == (A + B)**2
|
| 316 |
+
|
| 317 |
+
assert A**-1 * A == 1
|
| 318 |
+
assert A/A == 1
|
| 319 |
+
assert A/(A**2) == 1/A
|
| 320 |
+
|
| 321 |
+
assert A/(1 + A) == A/(1 + A)
|
| 322 |
+
|
| 323 |
+
assert set((A + B + 2*(A + B)).args) == \
|
| 324 |
+
{A, B, 2*(A + B)}
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
def test_mul_add_identity():
|
| 328 |
+
m = Mul(1, 2)
|
| 329 |
+
assert isinstance(m, Rational) and m.p == 2 and m.q == 1
|
| 330 |
+
m = Mul(1, 2, evaluate=False)
|
| 331 |
+
assert isinstance(m, Mul) and m.args == (1, 2)
|
| 332 |
+
m = Mul(0, 1)
|
| 333 |
+
assert m is S.Zero
|
| 334 |
+
m = Mul(0, 1, evaluate=False)
|
| 335 |
+
assert isinstance(m, Mul) and m.args == (0, 1)
|
| 336 |
+
m = Add(0, 1)
|
| 337 |
+
assert m is S.One
|
| 338 |
+
m = Add(0, 1, evaluate=False)
|
| 339 |
+
assert isinstance(m, Add) and m.args == (0, 1)
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
def test_ncpow():
|
| 343 |
+
x = Symbol('x', commutative=False)
|
| 344 |
+
y = Symbol('y', commutative=False)
|
| 345 |
+
z = Symbol('z', commutative=False)
|
| 346 |
+
a = Symbol('a')
|
| 347 |
+
b = Symbol('b')
|
| 348 |
+
c = Symbol('c')
|
| 349 |
+
|
| 350 |
+
assert (x**2)*(y**2) != (y**2)*(x**2)
|
| 351 |
+
assert (x**-2)*y != y*(x**2)
|
| 352 |
+
assert 2**x*2**y != 2**(x + y)
|
| 353 |
+
assert 2**x*2**y*2**z != 2**(x + y + z)
|
| 354 |
+
assert 2**x*2**(2*x) == 2**(3*x)
|
| 355 |
+
assert 2**x*2**(2*x)*2**x == 2**(4*x)
|
| 356 |
+
assert exp(x)*exp(y) != exp(y)*exp(x)
|
| 357 |
+
assert exp(x)*exp(y)*exp(z) != exp(y)*exp(x)*exp(z)
|
| 358 |
+
assert exp(x)*exp(y)*exp(z) != exp(x + y + z)
|
| 359 |
+
assert x**a*x**b != x**(a + b)
|
| 360 |
+
assert x**a*x**b*x**c != x**(a + b + c)
|
| 361 |
+
assert x**3*x**4 == x**7
|
| 362 |
+
assert x**3*x**4*x**2 == x**9
|
| 363 |
+
assert x**a*x**(4*a) == x**(5*a)
|
| 364 |
+
assert x**a*x**(4*a)*x**a == x**(6*a)
|
| 365 |
+
|
| 366 |
+
|
| 367 |
+
def test_powerbug():
|
| 368 |
+
x = Symbol("x")
|
| 369 |
+
assert x**1 != (-x)**1
|
| 370 |
+
assert x**2 == (-x)**2
|
| 371 |
+
assert x**3 != (-x)**3
|
| 372 |
+
assert x**4 == (-x)**4
|
| 373 |
+
assert x**5 != (-x)**5
|
| 374 |
+
assert x**6 == (-x)**6
|
| 375 |
+
|
| 376 |
+
assert x**128 == (-x)**128
|
| 377 |
+
assert x**129 != (-x)**129
|
| 378 |
+
|
| 379 |
+
assert (2*x)**2 == (-2*x)**2
|
| 380 |
+
|
| 381 |
+
|
| 382 |
+
def test_Mul_doesnt_expand_exp():
|
| 383 |
+
x = Symbol('x')
|
| 384 |
+
y = Symbol('y')
|
| 385 |
+
assert unchanged(Mul, exp(x), exp(y))
|
| 386 |
+
assert unchanged(Mul, 2**x, 2**y)
|
| 387 |
+
assert x**2*x**3 == x**5
|
| 388 |
+
assert 2**x*3**x == 6**x
|
| 389 |
+
assert x**(y)*x**(2*y) == x**(3*y)
|
| 390 |
+
assert sqrt(2)*sqrt(2) == 2
|
| 391 |
+
assert 2**x*2**(2*x) == 2**(3*x)
|
| 392 |
+
assert sqrt(2)*2**Rational(1, 4)*5**Rational(3, 4) == 10**Rational(3, 4)
|
| 393 |
+
assert (x**(-log(5)/log(3))*x)/(x*x**( - log(5)/log(3))) == sympify(1)
|
| 394 |
+
|
| 395 |
+
|
| 396 |
+
def test_Mul_is_integer():
|
| 397 |
+
k = Symbol('k', integer=True)
|
| 398 |
+
n = Symbol('n', integer=True)
|
| 399 |
+
nr = Symbol('nr', rational=False)
|
| 400 |
+
ir = Symbol('ir', irrational=True)
|
| 401 |
+
nz = Symbol('nz', integer=True, zero=False)
|
| 402 |
+
e = Symbol('e', even=True)
|
| 403 |
+
o = Symbol('o', odd=True)
|
| 404 |
+
i2 = Symbol('2', prime=True, even=True)
|
| 405 |
+
|
| 406 |
+
assert (k/3).is_integer is None
|
| 407 |
+
assert (nz/3).is_integer is None
|
| 408 |
+
assert (nr/3).is_integer is False
|
| 409 |
+
assert (ir/3).is_integer is False
|
| 410 |
+
assert (x*k*n).is_integer is None
|
| 411 |
+
assert (e/2).is_integer is True
|
| 412 |
+
assert (e**2/2).is_integer is True
|
| 413 |
+
assert (2/k).is_integer is None
|
| 414 |
+
assert (2/k**2).is_integer is None
|
| 415 |
+
assert ((-1)**k*n).is_integer is True
|
| 416 |
+
assert (3*k*e/2).is_integer is True
|
| 417 |
+
assert (2*k*e/3).is_integer is None
|
| 418 |
+
assert (e/o).is_integer is None
|
| 419 |
+
assert (o/e).is_integer is False
|
| 420 |
+
assert (o/i2).is_integer is False
|
| 421 |
+
assert Mul(k, 1/k, evaluate=False).is_integer is None
|
| 422 |
+
assert Mul(2., S.Half, evaluate=False).is_integer is None
|
| 423 |
+
assert (2*sqrt(k)).is_integer is None
|
| 424 |
+
assert (2*k**n).is_integer is None
|
| 425 |
+
|
| 426 |
+
s = 2**2**2**Pow(2, 1000, evaluate=False)
|
| 427 |
+
m = Mul(s, s, evaluate=False)
|
| 428 |
+
assert m.is_integer
|
| 429 |
+
|
| 430 |
+
# broken in 1.6 and before, see #20161
|
| 431 |
+
xq = Symbol('xq', rational=True)
|
| 432 |
+
yq = Symbol('yq', rational=True)
|
| 433 |
+
assert (xq*yq).is_integer is None
|
| 434 |
+
e_20161 = Mul(-1,Mul(1,Pow(2,-1,evaluate=False),evaluate=False),evaluate=False)
|
| 435 |
+
assert e_20161.is_integer is not True # expand(e_20161) -> -1/2, but no need to see that in the assumption without evaluation
|
| 436 |
+
|
| 437 |
+
|
| 438 |
+
def test_Add_Mul_is_integer():
|
| 439 |
+
x = Symbol('x')
|
| 440 |
+
|
| 441 |
+
k = Symbol('k', integer=True)
|
| 442 |
+
n = Symbol('n', integer=True)
|
| 443 |
+
nk = Symbol('nk', integer=False)
|
| 444 |
+
nr = Symbol('nr', rational=False)
|
| 445 |
+
nz = Symbol('nz', integer=True, zero=False)
|
| 446 |
+
|
| 447 |
+
assert (-nk).is_integer is None
|
| 448 |
+
assert (-nr).is_integer is False
|
| 449 |
+
assert (2*k).is_integer is True
|
| 450 |
+
assert (-k).is_integer is True
|
| 451 |
+
|
| 452 |
+
assert (k + nk).is_integer is False
|
| 453 |
+
assert (k + n).is_integer is True
|
| 454 |
+
assert (k + x).is_integer is None
|
| 455 |
+
assert (k + n*x).is_integer is None
|
| 456 |
+
assert (k + n/3).is_integer is None
|
| 457 |
+
assert (k + nz/3).is_integer is None
|
| 458 |
+
assert (k + nr/3).is_integer is False
|
| 459 |
+
|
| 460 |
+
assert ((1 + sqrt(3))*(-sqrt(3) + 1)).is_integer is not False
|
| 461 |
+
assert (1 + (1 + sqrt(3))*(-sqrt(3) + 1)).is_integer is not False
|
| 462 |
+
|
| 463 |
+
|
| 464 |
+
def test_Add_Mul_is_finite():
|
| 465 |
+
x = Symbol('x', extended_real=True, finite=False)
|
| 466 |
+
|
| 467 |
+
assert sin(x).is_finite is True
|
| 468 |
+
assert (x*sin(x)).is_finite is None
|
| 469 |
+
assert (x*atan(x)).is_finite is False
|
| 470 |
+
assert (1024*sin(x)).is_finite is True
|
| 471 |
+
assert (sin(x)*exp(x)).is_finite is None
|
| 472 |
+
assert (sin(x)*cos(x)).is_finite is True
|
| 473 |
+
assert (x*sin(x)*exp(x)).is_finite is None
|
| 474 |
+
|
| 475 |
+
assert (sin(x) - 67).is_finite is True
|
| 476 |
+
assert (sin(x) + exp(x)).is_finite is not True
|
| 477 |
+
assert (1 + x).is_finite is False
|
| 478 |
+
assert (1 + x**2 + (1 + x)*(1 - x)).is_finite is None
|
| 479 |
+
assert (sqrt(2)*(1 + x)).is_finite is False
|
| 480 |
+
assert (sqrt(2)*(1 + x)*(1 - x)).is_finite is False
|
| 481 |
+
|
| 482 |
+
|
| 483 |
+
def test_Mul_is_even_odd():
|
| 484 |
+
x = Symbol('x', integer=True)
|
| 485 |
+
y = Symbol('y', integer=True)
|
| 486 |
+
|
| 487 |
+
k = Symbol('k', odd=True)
|
| 488 |
+
n = Symbol('n', odd=True)
|
| 489 |
+
m = Symbol('m', even=True)
|
| 490 |
+
|
| 491 |
+
assert (2*x).is_even is True
|
| 492 |
+
assert (2*x).is_odd is False
|
| 493 |
+
|
| 494 |
+
assert (3*x).is_even is None
|
| 495 |
+
assert (3*x).is_odd is None
|
| 496 |
+
|
| 497 |
+
assert (k/3).is_integer is None
|
| 498 |
+
assert (k/3).is_even is None
|
| 499 |
+
assert (k/3).is_odd is None
|
| 500 |
+
|
| 501 |
+
assert (2*n).is_even is True
|
| 502 |
+
assert (2*n).is_odd is False
|
| 503 |
+
|
| 504 |
+
assert (2*m).is_even is True
|
| 505 |
+
assert (2*m).is_odd is False
|
| 506 |
+
|
| 507 |
+
assert (-n).is_even is False
|
| 508 |
+
assert (-n).is_odd is True
|
| 509 |
+
|
| 510 |
+
assert (k*n).is_even is False
|
| 511 |
+
assert (k*n).is_odd is True
|
| 512 |
+
|
| 513 |
+
assert (k*m).is_even is True
|
| 514 |
+
assert (k*m).is_odd is False
|
| 515 |
+
|
| 516 |
+
assert (k*n*m).is_even is True
|
| 517 |
+
assert (k*n*m).is_odd is False
|
| 518 |
+
|
| 519 |
+
assert (k*m*x).is_even is True
|
| 520 |
+
assert (k*m*x).is_odd is False
|
| 521 |
+
|
| 522 |
+
# issue 6791:
|
| 523 |
+
assert (x/2).is_integer is None
|
| 524 |
+
assert (k/2).is_integer is False
|
| 525 |
+
assert (m/2).is_integer is True
|
| 526 |
+
|
| 527 |
+
assert (x*y).is_even is None
|
| 528 |
+
assert (x*x).is_even is None
|
| 529 |
+
assert (x*(x + k)).is_even is True
|
| 530 |
+
assert (x*(x + m)).is_even is None
|
| 531 |
+
|
| 532 |
+
assert (x*y).is_odd is None
|
| 533 |
+
assert (x*x).is_odd is None
|
| 534 |
+
assert (x*(x + k)).is_odd is False
|
| 535 |
+
assert (x*(x + m)).is_odd is None
|
| 536 |
+
|
| 537 |
+
# issue 8648
|
| 538 |
+
assert (m**2/2).is_even
|
| 539 |
+
assert (m**2/3).is_even is False
|
| 540 |
+
assert (2/m**2).is_odd is False
|
| 541 |
+
assert (2/m).is_odd is None
|
| 542 |
+
|
| 543 |
+
|
| 544 |
+
@XFAIL
|
| 545 |
+
def test_evenness_in_ternary_integer_product_with_odd():
|
| 546 |
+
# Tests that oddness inference is independent of term ordering.
|
| 547 |
+
# Term ordering at the point of testing depends on SymPy's symbol order, so
|
| 548 |
+
# we try to force a different order by modifying symbol names.
|
| 549 |
+
x = Symbol('x', integer=True)
|
| 550 |
+
y = Symbol('y', integer=True)
|
| 551 |
+
k = Symbol('k', odd=True)
|
| 552 |
+
assert (x*y*(y + k)).is_even is True
|
| 553 |
+
assert (y*x*(x + k)).is_even is True
|
| 554 |
+
|
| 555 |
+
|
| 556 |
+
def test_evenness_in_ternary_integer_product_with_even():
|
| 557 |
+
x = Symbol('x', integer=True)
|
| 558 |
+
y = Symbol('y', integer=True)
|
| 559 |
+
m = Symbol('m', even=True)
|
| 560 |
+
assert (x*y*(y + m)).is_even is None
|
| 561 |
+
|
| 562 |
+
|
| 563 |
+
@XFAIL
|
| 564 |
+
def test_oddness_in_ternary_integer_product_with_odd():
|
| 565 |
+
# Tests that oddness inference is independent of term ordering.
|
| 566 |
+
# Term ordering at the point of testing depends on SymPy's symbol order, so
|
| 567 |
+
# we try to force a different order by modifying symbol names.
|
| 568 |
+
x = Symbol('x', integer=True)
|
| 569 |
+
y = Symbol('y', integer=True)
|
| 570 |
+
k = Symbol('k', odd=True)
|
| 571 |
+
assert (x*y*(y + k)).is_odd is False
|
| 572 |
+
assert (y*x*(x + k)).is_odd is False
|
| 573 |
+
|
| 574 |
+
|
| 575 |
+
def test_oddness_in_ternary_integer_product_with_even():
|
| 576 |
+
x = Symbol('x', integer=True)
|
| 577 |
+
y = Symbol('y', integer=True)
|
| 578 |
+
m = Symbol('m', even=True)
|
| 579 |
+
assert (x*y*(y + m)).is_odd is None
|
| 580 |
+
|
| 581 |
+
|
| 582 |
+
def test_Mul_is_rational():
|
| 583 |
+
x = Symbol('x')
|
| 584 |
+
n = Symbol('n', integer=True)
|
| 585 |
+
m = Symbol('m', integer=True, nonzero=True)
|
| 586 |
+
|
| 587 |
+
assert (n/m).is_rational is True
|
| 588 |
+
assert (x/pi).is_rational is None
|
| 589 |
+
assert (x/n).is_rational is None
|
| 590 |
+
assert (m/pi).is_rational is False
|
| 591 |
+
|
| 592 |
+
r = Symbol('r', rational=True)
|
| 593 |
+
assert (pi*r).is_rational is None
|
| 594 |
+
|
| 595 |
+
# issue 8008
|
| 596 |
+
z = Symbol('z', zero=True)
|
| 597 |
+
i = Symbol('i', imaginary=True)
|
| 598 |
+
assert (z*i).is_rational is True
|
| 599 |
+
bi = Symbol('i', imaginary=True, finite=True)
|
| 600 |
+
assert (z*bi).is_zero is True
|
| 601 |
+
|
| 602 |
+
|
| 603 |
+
def test_Add_is_rational():
|
| 604 |
+
x = Symbol('x')
|
| 605 |
+
n = Symbol('n', rational=True)
|
| 606 |
+
m = Symbol('m', rational=True)
|
| 607 |
+
|
| 608 |
+
assert (n + m).is_rational is True
|
| 609 |
+
assert (x + pi).is_rational is None
|
| 610 |
+
assert (x + n).is_rational is None
|
| 611 |
+
assert (n + pi).is_rational is False
|
| 612 |
+
|
| 613 |
+
|
| 614 |
+
def test_Add_is_even_odd():
|
| 615 |
+
x = Symbol('x', integer=True)
|
| 616 |
+
|
| 617 |
+
k = Symbol('k', odd=True)
|
| 618 |
+
n = Symbol('n', odd=True)
|
| 619 |
+
m = Symbol('m', even=True)
|
| 620 |
+
|
| 621 |
+
assert (k + 7).is_even is True
|
| 622 |
+
assert (k + 7).is_odd is False
|
| 623 |
+
|
| 624 |
+
assert (-k + 7).is_even is True
|
| 625 |
+
assert (-k + 7).is_odd is False
|
| 626 |
+
|
| 627 |
+
assert (k - 12).is_even is False
|
| 628 |
+
assert (k - 12).is_odd is True
|
| 629 |
+
|
| 630 |
+
assert (-k - 12).is_even is False
|
| 631 |
+
assert (-k - 12).is_odd is True
|
| 632 |
+
|
| 633 |
+
assert (k + n).is_even is True
|
| 634 |
+
assert (k + n).is_odd is False
|
| 635 |
+
|
| 636 |
+
assert (k + m).is_even is False
|
| 637 |
+
assert (k + m).is_odd is True
|
| 638 |
+
|
| 639 |
+
assert (k + n + m).is_even is True
|
| 640 |
+
assert (k + n + m).is_odd is False
|
| 641 |
+
|
| 642 |
+
assert (k + n + x + m).is_even is None
|
| 643 |
+
assert (k + n + x + m).is_odd is None
|
| 644 |
+
|
| 645 |
+
|
| 646 |
+
def test_Mul_is_negative_positive():
|
| 647 |
+
x = Symbol('x', real=True)
|
| 648 |
+
y = Symbol('y', extended_real=False, complex=True)
|
| 649 |
+
z = Symbol('z', zero=True)
|
| 650 |
+
|
| 651 |
+
e = 2*z
|
| 652 |
+
assert e.is_Mul and e.is_positive is False and e.is_negative is False
|
| 653 |
+
|
| 654 |
+
neg = Symbol('neg', negative=True)
|
| 655 |
+
pos = Symbol('pos', positive=True)
|
| 656 |
+
nneg = Symbol('nneg', nonnegative=True)
|
| 657 |
+
npos = Symbol('npos', nonpositive=True)
|
| 658 |
+
|
| 659 |
+
assert neg.is_negative is True
|
| 660 |
+
assert (-neg).is_negative is False
|
| 661 |
+
assert (2*neg).is_negative is True
|
| 662 |
+
|
| 663 |
+
assert (2*pos)._eval_is_extended_negative() is False
|
| 664 |
+
assert (2*pos).is_negative is False
|
| 665 |
+
|
| 666 |
+
assert pos.is_negative is False
|
| 667 |
+
assert (-pos).is_negative is True
|
| 668 |
+
assert (2*pos).is_negative is False
|
| 669 |
+
|
| 670 |
+
assert (pos*neg).is_negative is True
|
| 671 |
+
assert (2*pos*neg).is_negative is True
|
| 672 |
+
assert (-pos*neg).is_negative is False
|
| 673 |
+
assert (pos*neg*y).is_negative is False # y.is_real=F; !real -> !neg
|
| 674 |
+
|
| 675 |
+
assert nneg.is_negative is False
|
| 676 |
+
assert (-nneg).is_negative is None
|
| 677 |
+
assert (2*nneg).is_negative is False
|
| 678 |
+
|
| 679 |
+
assert npos.is_negative is None
|
| 680 |
+
assert (-npos).is_negative is False
|
| 681 |
+
assert (2*npos).is_negative is None
|
| 682 |
+
|
| 683 |
+
assert (nneg*npos).is_negative is None
|
| 684 |
+
|
| 685 |
+
assert (neg*nneg).is_negative is None
|
| 686 |
+
assert (neg*npos).is_negative is False
|
| 687 |
+
|
| 688 |
+
assert (pos*nneg).is_negative is False
|
| 689 |
+
assert (pos*npos).is_negative is None
|
| 690 |
+
|
| 691 |
+
assert (npos*neg*nneg).is_negative is False
|
| 692 |
+
assert (npos*pos*nneg).is_negative is None
|
| 693 |
+
|
| 694 |
+
assert (-npos*neg*nneg).is_negative is None
|
| 695 |
+
assert (-npos*pos*nneg).is_negative is False
|
| 696 |
+
|
| 697 |
+
assert (17*npos*neg*nneg).is_negative is False
|
| 698 |
+
assert (17*npos*pos*nneg).is_negative is None
|
| 699 |
+
|
| 700 |
+
assert (neg*npos*pos*nneg).is_negative is False
|
| 701 |
+
|
| 702 |
+
assert (x*neg).is_negative is None
|
| 703 |
+
assert (nneg*npos*pos*x*neg).is_negative is None
|
| 704 |
+
|
| 705 |
+
assert neg.is_positive is False
|
| 706 |
+
assert (-neg).is_positive is True
|
| 707 |
+
assert (2*neg).is_positive is False
|
| 708 |
+
|
| 709 |
+
assert pos.is_positive is True
|
| 710 |
+
assert (-pos).is_positive is False
|
| 711 |
+
assert (2*pos).is_positive is True
|
| 712 |
+
|
| 713 |
+
assert (pos*neg).is_positive is False
|
| 714 |
+
assert (2*pos*neg).is_positive is False
|
| 715 |
+
assert (-pos*neg).is_positive is True
|
| 716 |
+
assert (-pos*neg*y).is_positive is False # y.is_real=F; !real -> !neg
|
| 717 |
+
|
| 718 |
+
assert nneg.is_positive is None
|
| 719 |
+
assert (-nneg).is_positive is False
|
| 720 |
+
assert (2*nneg).is_positive is None
|
| 721 |
+
|
| 722 |
+
assert npos.is_positive is False
|
| 723 |
+
assert (-npos).is_positive is None
|
| 724 |
+
assert (2*npos).is_positive is False
|
| 725 |
+
|
| 726 |
+
assert (nneg*npos).is_positive is False
|
| 727 |
+
|
| 728 |
+
assert (neg*nneg).is_positive is False
|
| 729 |
+
assert (neg*npos).is_positive is None
|
| 730 |
+
|
| 731 |
+
assert (pos*nneg).is_positive is None
|
| 732 |
+
assert (pos*npos).is_positive is False
|
| 733 |
+
|
| 734 |
+
assert (npos*neg*nneg).is_positive is None
|
| 735 |
+
assert (npos*pos*nneg).is_positive is False
|
| 736 |
+
|
| 737 |
+
assert (-npos*neg*nneg).is_positive is False
|
| 738 |
+
assert (-npos*pos*nneg).is_positive is None
|
| 739 |
+
|
| 740 |
+
assert (17*npos*neg*nneg).is_positive is None
|
| 741 |
+
assert (17*npos*pos*nneg).is_positive is False
|
| 742 |
+
|
| 743 |
+
assert (neg*npos*pos*nneg).is_positive is None
|
| 744 |
+
|
| 745 |
+
assert (x*neg).is_positive is None
|
| 746 |
+
assert (nneg*npos*pos*x*neg).is_positive is None
|
| 747 |
+
|
| 748 |
+
|
| 749 |
+
def test_Mul_is_negative_positive_2():
|
| 750 |
+
a = Symbol('a', nonnegative=True)
|
| 751 |
+
b = Symbol('b', nonnegative=True)
|
| 752 |
+
c = Symbol('c', nonpositive=True)
|
| 753 |
+
d = Symbol('d', nonpositive=True)
|
| 754 |
+
|
| 755 |
+
assert (a*b).is_nonnegative is True
|
| 756 |
+
assert (a*b).is_negative is False
|
| 757 |
+
assert (a*b).is_zero is None
|
| 758 |
+
assert (a*b).is_positive is None
|
| 759 |
+
|
| 760 |
+
assert (c*d).is_nonnegative is True
|
| 761 |
+
assert (c*d).is_negative is False
|
| 762 |
+
assert (c*d).is_zero is None
|
| 763 |
+
assert (c*d).is_positive is None
|
| 764 |
+
|
| 765 |
+
assert (a*c).is_nonpositive is True
|
| 766 |
+
assert (a*c).is_positive is False
|
| 767 |
+
assert (a*c).is_zero is None
|
| 768 |
+
assert (a*c).is_negative is None
|
| 769 |
+
|
| 770 |
+
|
| 771 |
+
def test_Mul_is_nonpositive_nonnegative():
|
| 772 |
+
x = Symbol('x', real=True)
|
| 773 |
+
|
| 774 |
+
k = Symbol('k', negative=True)
|
| 775 |
+
n = Symbol('n', positive=True)
|
| 776 |
+
u = Symbol('u', nonnegative=True)
|
| 777 |
+
v = Symbol('v', nonpositive=True)
|
| 778 |
+
|
| 779 |
+
assert k.is_nonpositive is True
|
| 780 |
+
assert (-k).is_nonpositive is False
|
| 781 |
+
assert (2*k).is_nonpositive is True
|
| 782 |
+
|
| 783 |
+
assert n.is_nonpositive is False
|
| 784 |
+
assert (-n).is_nonpositive is True
|
| 785 |
+
assert (2*n).is_nonpositive is False
|
| 786 |
+
|
| 787 |
+
assert (n*k).is_nonpositive is True
|
| 788 |
+
assert (2*n*k).is_nonpositive is True
|
| 789 |
+
assert (-n*k).is_nonpositive is False
|
| 790 |
+
|
| 791 |
+
assert u.is_nonpositive is None
|
| 792 |
+
assert (-u).is_nonpositive is True
|
| 793 |
+
assert (2*u).is_nonpositive is None
|
| 794 |
+
|
| 795 |
+
assert v.is_nonpositive is True
|
| 796 |
+
assert (-v).is_nonpositive is None
|
| 797 |
+
assert (2*v).is_nonpositive is True
|
| 798 |
+
|
| 799 |
+
assert (u*v).is_nonpositive is True
|
| 800 |
+
|
| 801 |
+
assert (k*u).is_nonpositive is True
|
| 802 |
+
assert (k*v).is_nonpositive is None
|
| 803 |
+
|
| 804 |
+
assert (n*u).is_nonpositive is None
|
| 805 |
+
assert (n*v).is_nonpositive is True
|
| 806 |
+
|
| 807 |
+
assert (v*k*u).is_nonpositive is None
|
| 808 |
+
assert (v*n*u).is_nonpositive is True
|
| 809 |
+
|
| 810 |
+
assert (-v*k*u).is_nonpositive is True
|
| 811 |
+
assert (-v*n*u).is_nonpositive is None
|
| 812 |
+
|
| 813 |
+
assert (17*v*k*u).is_nonpositive is None
|
| 814 |
+
assert (17*v*n*u).is_nonpositive is True
|
| 815 |
+
|
| 816 |
+
assert (k*v*n*u).is_nonpositive is None
|
| 817 |
+
|
| 818 |
+
assert (x*k).is_nonpositive is None
|
| 819 |
+
assert (u*v*n*x*k).is_nonpositive is None
|
| 820 |
+
|
| 821 |
+
assert k.is_nonnegative is False
|
| 822 |
+
assert (-k).is_nonnegative is True
|
| 823 |
+
assert (2*k).is_nonnegative is False
|
| 824 |
+
|
| 825 |
+
assert n.is_nonnegative is True
|
| 826 |
+
assert (-n).is_nonnegative is False
|
| 827 |
+
assert (2*n).is_nonnegative is True
|
| 828 |
+
|
| 829 |
+
assert (n*k).is_nonnegative is False
|
| 830 |
+
assert (2*n*k).is_nonnegative is False
|
| 831 |
+
assert (-n*k).is_nonnegative is True
|
| 832 |
+
|
| 833 |
+
assert u.is_nonnegative is True
|
| 834 |
+
assert (-u).is_nonnegative is None
|
| 835 |
+
assert (2*u).is_nonnegative is True
|
| 836 |
+
|
| 837 |
+
assert v.is_nonnegative is None
|
| 838 |
+
assert (-v).is_nonnegative is True
|
| 839 |
+
assert (2*v).is_nonnegative is None
|
| 840 |
+
|
| 841 |
+
assert (u*v).is_nonnegative is None
|
| 842 |
+
|
| 843 |
+
assert (k*u).is_nonnegative is None
|
| 844 |
+
assert (k*v).is_nonnegative is True
|
| 845 |
+
|
| 846 |
+
assert (n*u).is_nonnegative is True
|
| 847 |
+
assert (n*v).is_nonnegative is None
|
| 848 |
+
|
| 849 |
+
assert (v*k*u).is_nonnegative is True
|
| 850 |
+
assert (v*n*u).is_nonnegative is None
|
| 851 |
+
|
| 852 |
+
assert (-v*k*u).is_nonnegative is None
|
| 853 |
+
assert (-v*n*u).is_nonnegative is True
|
| 854 |
+
|
| 855 |
+
assert (17*v*k*u).is_nonnegative is True
|
| 856 |
+
assert (17*v*n*u).is_nonnegative is None
|
| 857 |
+
|
| 858 |
+
assert (k*v*n*u).is_nonnegative is True
|
| 859 |
+
|
| 860 |
+
assert (x*k).is_nonnegative is None
|
| 861 |
+
assert (u*v*n*x*k).is_nonnegative is None
|
| 862 |
+
|
| 863 |
+
|
| 864 |
+
def test_Add_is_negative_positive():
|
| 865 |
+
x = Symbol('x', real=True)
|
| 866 |
+
|
| 867 |
+
k = Symbol('k', negative=True)
|
| 868 |
+
n = Symbol('n', positive=True)
|
| 869 |
+
u = Symbol('u', nonnegative=True)
|
| 870 |
+
v = Symbol('v', nonpositive=True)
|
| 871 |
+
|
| 872 |
+
assert (k - 2).is_negative is True
|
| 873 |
+
assert (k + 17).is_negative is None
|
| 874 |
+
assert (-k - 5).is_negative is None
|
| 875 |
+
assert (-k + 123).is_negative is False
|
| 876 |
+
|
| 877 |
+
assert (k - n).is_negative is True
|
| 878 |
+
assert (k + n).is_negative is None
|
| 879 |
+
assert (-k - n).is_negative is None
|
| 880 |
+
assert (-k + n).is_negative is False
|
| 881 |
+
|
| 882 |
+
assert (k - n - 2).is_negative is True
|
| 883 |
+
assert (k + n + 17).is_negative is None
|
| 884 |
+
assert (-k - n - 5).is_negative is None
|
| 885 |
+
assert (-k + n + 123).is_negative is False
|
| 886 |
+
|
| 887 |
+
assert (-2*k + 123*n + 17).is_negative is False
|
| 888 |
+
|
| 889 |
+
assert (k + u).is_negative is None
|
| 890 |
+
assert (k + v).is_negative is True
|
| 891 |
+
assert (n + u).is_negative is False
|
| 892 |
+
assert (n + v).is_negative is None
|
| 893 |
+
|
| 894 |
+
assert (u - v).is_negative is False
|
| 895 |
+
assert (u + v).is_negative is None
|
| 896 |
+
assert (-u - v).is_negative is None
|
| 897 |
+
assert (-u + v).is_negative is None
|
| 898 |
+
|
| 899 |
+
assert (u - v + n + 2).is_negative is False
|
| 900 |
+
assert (u + v + n + 2).is_negative is None
|
| 901 |
+
assert (-u - v + n + 2).is_negative is None
|
| 902 |
+
assert (-u + v + n + 2).is_negative is None
|
| 903 |
+
|
| 904 |
+
assert (k + x).is_negative is None
|
| 905 |
+
assert (k + x - n).is_negative is None
|
| 906 |
+
|
| 907 |
+
assert (k - 2).is_positive is False
|
| 908 |
+
assert (k + 17).is_positive is None
|
| 909 |
+
assert (-k - 5).is_positive is None
|
| 910 |
+
assert (-k + 123).is_positive is True
|
| 911 |
+
|
| 912 |
+
assert (k - n).is_positive is False
|
| 913 |
+
assert (k + n).is_positive is None
|
| 914 |
+
assert (-k - n).is_positive is None
|
| 915 |
+
assert (-k + n).is_positive is True
|
| 916 |
+
|
| 917 |
+
assert (k - n - 2).is_positive is False
|
| 918 |
+
assert (k + n + 17).is_positive is None
|
| 919 |
+
assert (-k - n - 5).is_positive is None
|
| 920 |
+
assert (-k + n + 123).is_positive is True
|
| 921 |
+
|
| 922 |
+
assert (-2*k + 123*n + 17).is_positive is True
|
| 923 |
+
|
| 924 |
+
assert (k + u).is_positive is None
|
| 925 |
+
assert (k + v).is_positive is False
|
| 926 |
+
assert (n + u).is_positive is True
|
| 927 |
+
assert (n + v).is_positive is None
|
| 928 |
+
|
| 929 |
+
assert (u - v).is_positive is None
|
| 930 |
+
assert (u + v).is_positive is None
|
| 931 |
+
assert (-u - v).is_positive is None
|
| 932 |
+
assert (-u + v).is_positive is False
|
| 933 |
+
|
| 934 |
+
assert (u - v - n - 2).is_positive is None
|
| 935 |
+
assert (u + v - n - 2).is_positive is None
|
| 936 |
+
assert (-u - v - n - 2).is_positive is None
|
| 937 |
+
assert (-u + v - n - 2).is_positive is False
|
| 938 |
+
|
| 939 |
+
assert (n + x).is_positive is None
|
| 940 |
+
assert (n + x - k).is_positive is None
|
| 941 |
+
|
| 942 |
+
z = (-3 - sqrt(5) + (-sqrt(10)/2 - sqrt(2)/2)**2)
|
| 943 |
+
assert z.is_zero
|
| 944 |
+
z = sqrt(1 + sqrt(3)) + sqrt(3 + 3*sqrt(3)) - sqrt(10 + 6*sqrt(3))
|
| 945 |
+
assert z.is_zero
|
| 946 |
+
|
| 947 |
+
|
| 948 |
+
def test_Add_is_nonpositive_nonnegative():
|
| 949 |
+
x = Symbol('x', real=True)
|
| 950 |
+
|
| 951 |
+
k = Symbol('k', negative=True)
|
| 952 |
+
n = Symbol('n', positive=True)
|
| 953 |
+
u = Symbol('u', nonnegative=True)
|
| 954 |
+
v = Symbol('v', nonpositive=True)
|
| 955 |
+
|
| 956 |
+
assert (u - 2).is_nonpositive is None
|
| 957 |
+
assert (u + 17).is_nonpositive is False
|
| 958 |
+
assert (-u - 5).is_nonpositive is True
|
| 959 |
+
assert (-u + 123).is_nonpositive is None
|
| 960 |
+
|
| 961 |
+
assert (u - v).is_nonpositive is None
|
| 962 |
+
assert (u + v).is_nonpositive is None
|
| 963 |
+
assert (-u - v).is_nonpositive is None
|
| 964 |
+
assert (-u + v).is_nonpositive is True
|
| 965 |
+
|
| 966 |
+
assert (u - v - 2).is_nonpositive is None
|
| 967 |
+
assert (u + v + 17).is_nonpositive is None
|
| 968 |
+
assert (-u - v - 5).is_nonpositive is None
|
| 969 |
+
assert (-u + v - 123).is_nonpositive is True
|
| 970 |
+
|
| 971 |
+
assert (-2*u + 123*v - 17).is_nonpositive is True
|
| 972 |
+
|
| 973 |
+
assert (k + u).is_nonpositive is None
|
| 974 |
+
assert (k + v).is_nonpositive is True
|
| 975 |
+
assert (n + u).is_nonpositive is False
|
| 976 |
+
assert (n + v).is_nonpositive is None
|
| 977 |
+
|
| 978 |
+
assert (k - n).is_nonpositive is True
|
| 979 |
+
assert (k + n).is_nonpositive is None
|
| 980 |
+
assert (-k - n).is_nonpositive is None
|
| 981 |
+
assert (-k + n).is_nonpositive is False
|
| 982 |
+
|
| 983 |
+
assert (k - n + u + 2).is_nonpositive is None
|
| 984 |
+
assert (k + n + u + 2).is_nonpositive is None
|
| 985 |
+
assert (-k - n + u + 2).is_nonpositive is None
|
| 986 |
+
assert (-k + n + u + 2).is_nonpositive is False
|
| 987 |
+
|
| 988 |
+
assert (u + x).is_nonpositive is None
|
| 989 |
+
assert (v - x - n).is_nonpositive is None
|
| 990 |
+
|
| 991 |
+
assert (u - 2).is_nonnegative is None
|
| 992 |
+
assert (u + 17).is_nonnegative is True
|
| 993 |
+
assert (-u - 5).is_nonnegative is False
|
| 994 |
+
assert (-u + 123).is_nonnegative is None
|
| 995 |
+
|
| 996 |
+
assert (u - v).is_nonnegative is True
|
| 997 |
+
assert (u + v).is_nonnegative is None
|
| 998 |
+
assert (-u - v).is_nonnegative is None
|
| 999 |
+
assert (-u + v).is_nonnegative is None
|
| 1000 |
+
|
| 1001 |
+
assert (u - v + 2).is_nonnegative is True
|
| 1002 |
+
assert (u + v + 17).is_nonnegative is None
|
| 1003 |
+
assert (-u - v - 5).is_nonnegative is None
|
| 1004 |
+
assert (-u + v - 123).is_nonnegative is False
|
| 1005 |
+
|
| 1006 |
+
assert (2*u - 123*v + 17).is_nonnegative is True
|
| 1007 |
+
|
| 1008 |
+
assert (k + u).is_nonnegative is None
|
| 1009 |
+
assert (k + v).is_nonnegative is False
|
| 1010 |
+
assert (n + u).is_nonnegative is True
|
| 1011 |
+
assert (n + v).is_nonnegative is None
|
| 1012 |
+
|
| 1013 |
+
assert (k - n).is_nonnegative is False
|
| 1014 |
+
assert (k + n).is_nonnegative is None
|
| 1015 |
+
assert (-k - n).is_nonnegative is None
|
| 1016 |
+
assert (-k + n).is_nonnegative is True
|
| 1017 |
+
|
| 1018 |
+
assert (k - n - u - 2).is_nonnegative is False
|
| 1019 |
+
assert (k + n - u - 2).is_nonnegative is None
|
| 1020 |
+
assert (-k - n - u - 2).is_nonnegative is None
|
| 1021 |
+
assert (-k + n - u - 2).is_nonnegative is None
|
| 1022 |
+
|
| 1023 |
+
assert (u - x).is_nonnegative is None
|
| 1024 |
+
assert (v + x + n).is_nonnegative is None
|
| 1025 |
+
|
| 1026 |
+
|
| 1027 |
+
def test_Pow_is_integer():
|
| 1028 |
+
x = Symbol('x')
|
| 1029 |
+
|
| 1030 |
+
k = Symbol('k', integer=True)
|
| 1031 |
+
n = Symbol('n', integer=True, nonnegative=True)
|
| 1032 |
+
m = Symbol('m', integer=True, positive=True)
|
| 1033 |
+
|
| 1034 |
+
assert (k**2).is_integer is True
|
| 1035 |
+
assert (k**(-2)).is_integer is None
|
| 1036 |
+
assert ((m + 1)**(-2)).is_integer is False
|
| 1037 |
+
assert (m**(-1)).is_integer is None # issue 8580
|
| 1038 |
+
|
| 1039 |
+
assert (2**k).is_integer is None
|
| 1040 |
+
assert (2**(-k)).is_integer is None
|
| 1041 |
+
|
| 1042 |
+
assert (2**n).is_integer is True
|
| 1043 |
+
assert (2**(-n)).is_integer is None
|
| 1044 |
+
|
| 1045 |
+
assert (2**m).is_integer is True
|
| 1046 |
+
assert (2**(-m)).is_integer is False
|
| 1047 |
+
|
| 1048 |
+
assert (x**2).is_integer is None
|
| 1049 |
+
assert (2**x).is_integer is None
|
| 1050 |
+
|
| 1051 |
+
assert (k**n).is_integer is True
|
| 1052 |
+
assert (k**(-n)).is_integer is None
|
| 1053 |
+
|
| 1054 |
+
assert (k**x).is_integer is None
|
| 1055 |
+
assert (x**k).is_integer is None
|
| 1056 |
+
|
| 1057 |
+
assert (k**(n*m)).is_integer is True
|
| 1058 |
+
assert (k**(-n*m)).is_integer is None
|
| 1059 |
+
|
| 1060 |
+
assert sqrt(3).is_integer is False
|
| 1061 |
+
assert sqrt(.3).is_integer is False
|
| 1062 |
+
assert Pow(3, 2, evaluate=False).is_integer is True
|
| 1063 |
+
assert Pow(3, 0, evaluate=False).is_integer is True
|
| 1064 |
+
assert Pow(3, -2, evaluate=False).is_integer is False
|
| 1065 |
+
assert Pow(S.Half, 3, evaluate=False).is_integer is False
|
| 1066 |
+
# decided by re-evaluating
|
| 1067 |
+
assert Pow(3, S.Half, evaluate=False).is_integer is False
|
| 1068 |
+
assert Pow(3, S.Half, evaluate=False).is_integer is False
|
| 1069 |
+
assert Pow(4, S.Half, evaluate=False).is_integer is True
|
| 1070 |
+
assert Pow(S.Half, -2, evaluate=False).is_integer is True
|
| 1071 |
+
|
| 1072 |
+
assert ((-1)**k).is_integer
|
| 1073 |
+
|
| 1074 |
+
# issue 8641
|
| 1075 |
+
x = Symbol('x', real=True, integer=False)
|
| 1076 |
+
assert (x**2).is_integer is None
|
| 1077 |
+
|
| 1078 |
+
# issue 10458
|
| 1079 |
+
x = Symbol('x', positive=True)
|
| 1080 |
+
assert (1/(x + 1)).is_integer is False
|
| 1081 |
+
assert (1/(-x - 1)).is_integer is False
|
| 1082 |
+
assert (-1/(x + 1)).is_integer is False
|
| 1083 |
+
# issue 23287
|
| 1084 |
+
assert (x**2/2).is_integer is None
|
| 1085 |
+
|
| 1086 |
+
# issue 8648-like
|
| 1087 |
+
k = Symbol('k', even=True)
|
| 1088 |
+
assert (k**3/2).is_integer
|
| 1089 |
+
assert (k**3/8).is_integer
|
| 1090 |
+
assert (k**3/16).is_integer is None
|
| 1091 |
+
assert (2/k).is_integer is None
|
| 1092 |
+
assert (2/k**2).is_integer is False
|
| 1093 |
+
o = Symbol('o', odd=True)
|
| 1094 |
+
assert (k/o).is_integer is None
|
| 1095 |
+
o = Symbol('o', odd=True, prime=True)
|
| 1096 |
+
assert (k/o).is_integer is False
|
| 1097 |
+
|
| 1098 |
+
|
| 1099 |
+
def test_Pow_is_real():
|
| 1100 |
+
x = Symbol('x', real=True)
|
| 1101 |
+
y = Symbol('y', positive=True)
|
| 1102 |
+
|
| 1103 |
+
assert (x**2).is_real is True
|
| 1104 |
+
assert (x**3).is_real is True
|
| 1105 |
+
assert (x**x).is_real is None
|
| 1106 |
+
assert (y**x).is_real is True
|
| 1107 |
+
|
| 1108 |
+
assert (x**Rational(1, 3)).is_real is None
|
| 1109 |
+
assert (y**Rational(1, 3)).is_real is True
|
| 1110 |
+
|
| 1111 |
+
assert sqrt(-1 - sqrt(2)).is_real is False
|
| 1112 |
+
|
| 1113 |
+
i = Symbol('i', imaginary=True)
|
| 1114 |
+
assert (i**i).is_real is None
|
| 1115 |
+
assert (I**i).is_extended_real is True
|
| 1116 |
+
assert ((-I)**i).is_extended_real is True
|
| 1117 |
+
assert (2**i).is_real is None # (2**(pi/log(2) * I)) is real, 2**I is not
|
| 1118 |
+
assert (2**I).is_real is False
|
| 1119 |
+
assert (2**-I).is_real is False
|
| 1120 |
+
assert (i**2).is_extended_real is True
|
| 1121 |
+
assert (i**3).is_extended_real is False
|
| 1122 |
+
assert (i**x).is_real is None # could be (-I)**(2/3)
|
| 1123 |
+
e = Symbol('e', even=True)
|
| 1124 |
+
o = Symbol('o', odd=True)
|
| 1125 |
+
k = Symbol('k', integer=True)
|
| 1126 |
+
assert (i**e).is_extended_real is True
|
| 1127 |
+
assert (i**o).is_extended_real is False
|
| 1128 |
+
assert (i**k).is_real is None
|
| 1129 |
+
assert (i**(4*k)).is_extended_real is True
|
| 1130 |
+
|
| 1131 |
+
x = Symbol("x", nonnegative=True)
|
| 1132 |
+
y = Symbol("y", nonnegative=True)
|
| 1133 |
+
assert im(x**y).expand(complex=True) is S.Zero
|
| 1134 |
+
assert (x**y).is_real is True
|
| 1135 |
+
i = Symbol('i', imaginary=True)
|
| 1136 |
+
assert (exp(i)**I).is_extended_real is True
|
| 1137 |
+
assert log(exp(i)).is_imaginary is None # i could be 2*pi*I
|
| 1138 |
+
c = Symbol('c', complex=True)
|
| 1139 |
+
assert log(c).is_real is None # c could be 0 or 2, too
|
| 1140 |
+
assert log(exp(c)).is_real is None # log(0), log(E), ...
|
| 1141 |
+
n = Symbol('n', negative=False)
|
| 1142 |
+
assert log(n).is_real is None
|
| 1143 |
+
n = Symbol('n', nonnegative=True)
|
| 1144 |
+
assert log(n).is_real is None
|
| 1145 |
+
|
| 1146 |
+
assert sqrt(-I).is_real is False # issue 7843
|
| 1147 |
+
|
| 1148 |
+
i = Symbol('i', integer=True)
|
| 1149 |
+
assert (1/(i-1)).is_real is None
|
| 1150 |
+
assert (1/(i-1)).is_extended_real is None
|
| 1151 |
+
|
| 1152 |
+
# test issue 20715
|
| 1153 |
+
from sympy.core.parameters import evaluate
|
| 1154 |
+
x = S(-1)
|
| 1155 |
+
with evaluate(False):
|
| 1156 |
+
assert x.is_negative is True
|
| 1157 |
+
|
| 1158 |
+
f = Pow(x, -1)
|
| 1159 |
+
with evaluate(False):
|
| 1160 |
+
assert f.is_imaginary is False
|
| 1161 |
+
|
| 1162 |
+
|
| 1163 |
+
def test_real_Pow():
|
| 1164 |
+
k = Symbol('k', integer=True, nonzero=True)
|
| 1165 |
+
assert (k**(I*pi/log(k))).is_real
|
| 1166 |
+
|
| 1167 |
+
|
| 1168 |
+
def test_Pow_is_finite():
|
| 1169 |
+
xe = Symbol('xe', extended_real=True)
|
| 1170 |
+
xr = Symbol('xr', real=True)
|
| 1171 |
+
p = Symbol('p', positive=True)
|
| 1172 |
+
n = Symbol('n', negative=True)
|
| 1173 |
+
i = Symbol('i', integer=True)
|
| 1174 |
+
|
| 1175 |
+
assert (xe**2).is_finite is None # xe could be oo
|
| 1176 |
+
assert (xr**2).is_finite is True
|
| 1177 |
+
|
| 1178 |
+
assert (xe**xe).is_finite is None
|
| 1179 |
+
assert (xr**xe).is_finite is None
|
| 1180 |
+
assert (xe**xr).is_finite is None
|
| 1181 |
+
# FIXME: The line below should be True rather than None
|
| 1182 |
+
# assert (xr**xr).is_finite is True
|
| 1183 |
+
assert (xr**xr).is_finite is None
|
| 1184 |
+
|
| 1185 |
+
assert (p**xe).is_finite is None
|
| 1186 |
+
assert (p**xr).is_finite is True
|
| 1187 |
+
|
| 1188 |
+
assert (n**xe).is_finite is None
|
| 1189 |
+
assert (n**xr).is_finite is True
|
| 1190 |
+
|
| 1191 |
+
assert (sin(xe)**2).is_finite is True
|
| 1192 |
+
assert (sin(xr)**2).is_finite is True
|
| 1193 |
+
|
| 1194 |
+
assert (sin(xe)**xe).is_finite is None # xe, xr could be -pi
|
| 1195 |
+
assert (sin(xr)**xr).is_finite is None
|
| 1196 |
+
|
| 1197 |
+
# FIXME: Should the line below be True rather than None?
|
| 1198 |
+
assert (sin(xe)**exp(xe)).is_finite is None
|
| 1199 |
+
assert (sin(xr)**exp(xr)).is_finite is True
|
| 1200 |
+
|
| 1201 |
+
assert (1/sin(xe)).is_finite is None # if zero, no, otherwise yes
|
| 1202 |
+
assert (1/sin(xr)).is_finite is None
|
| 1203 |
+
|
| 1204 |
+
assert (1/exp(xe)).is_finite is None # xe could be -oo
|
| 1205 |
+
assert (1/exp(xr)).is_finite is True
|
| 1206 |
+
|
| 1207 |
+
assert (1/S.Pi).is_finite is True
|
| 1208 |
+
|
| 1209 |
+
assert (1/(i-1)).is_finite is None
|
| 1210 |
+
|
| 1211 |
+
|
| 1212 |
+
def test_Pow_is_even_odd():
|
| 1213 |
+
x = Symbol('x')
|
| 1214 |
+
|
| 1215 |
+
k = Symbol('k', even=True)
|
| 1216 |
+
n = Symbol('n', odd=True)
|
| 1217 |
+
m = Symbol('m', integer=True, nonnegative=True)
|
| 1218 |
+
p = Symbol('p', integer=True, positive=True)
|
| 1219 |
+
|
| 1220 |
+
assert ((-1)**n).is_odd
|
| 1221 |
+
assert ((-1)**k).is_odd
|
| 1222 |
+
assert ((-1)**(m - p)).is_odd
|
| 1223 |
+
|
| 1224 |
+
assert (k**2).is_even is True
|
| 1225 |
+
assert (n**2).is_even is False
|
| 1226 |
+
assert (2**k).is_even is None
|
| 1227 |
+
assert (x**2).is_even is None
|
| 1228 |
+
|
| 1229 |
+
assert (k**m).is_even is None
|
| 1230 |
+
assert (n**m).is_even is False
|
| 1231 |
+
|
| 1232 |
+
assert (k**p).is_even is True
|
| 1233 |
+
assert (n**p).is_even is False
|
| 1234 |
+
|
| 1235 |
+
assert (m**k).is_even is None
|
| 1236 |
+
assert (p**k).is_even is None
|
| 1237 |
+
|
| 1238 |
+
assert (m**n).is_even is None
|
| 1239 |
+
assert (p**n).is_even is None
|
| 1240 |
+
|
| 1241 |
+
assert (k**x).is_even is None
|
| 1242 |
+
assert (n**x).is_even is None
|
| 1243 |
+
|
| 1244 |
+
assert (k**2).is_odd is False
|
| 1245 |
+
assert (n**2).is_odd is True
|
| 1246 |
+
assert (3**k).is_odd is None
|
| 1247 |
+
|
| 1248 |
+
assert (k**m).is_odd is None
|
| 1249 |
+
assert (n**m).is_odd is True
|
| 1250 |
+
|
| 1251 |
+
assert (k**p).is_odd is False
|
| 1252 |
+
assert (n**p).is_odd is True
|
| 1253 |
+
|
| 1254 |
+
assert (m**k).is_odd is None
|
| 1255 |
+
assert (p**k).is_odd is None
|
| 1256 |
+
|
| 1257 |
+
assert (m**n).is_odd is None
|
| 1258 |
+
assert (p**n).is_odd is None
|
| 1259 |
+
|
| 1260 |
+
assert (k**x).is_odd is None
|
| 1261 |
+
assert (n**x).is_odd is None
|
| 1262 |
+
|
| 1263 |
+
|
| 1264 |
+
def test_Pow_is_negative_positive():
|
| 1265 |
+
r = Symbol('r', real=True)
|
| 1266 |
+
|
| 1267 |
+
k = Symbol('k', integer=True, positive=True)
|
| 1268 |
+
n = Symbol('n', even=True)
|
| 1269 |
+
m = Symbol('m', odd=True)
|
| 1270 |
+
|
| 1271 |
+
x = Symbol('x')
|
| 1272 |
+
|
| 1273 |
+
assert (2**r).is_positive is True
|
| 1274 |
+
assert ((-2)**r).is_positive is None
|
| 1275 |
+
assert ((-2)**n).is_positive is True
|
| 1276 |
+
assert ((-2)**m).is_positive is False
|
| 1277 |
+
|
| 1278 |
+
assert (k**2).is_positive is True
|
| 1279 |
+
assert (k**(-2)).is_positive is True
|
| 1280 |
+
|
| 1281 |
+
assert (k**r).is_positive is True
|
| 1282 |
+
assert ((-k)**r).is_positive is None
|
| 1283 |
+
assert ((-k)**n).is_positive is True
|
| 1284 |
+
assert ((-k)**m).is_positive is False
|
| 1285 |
+
|
| 1286 |
+
assert (2**r).is_negative is False
|
| 1287 |
+
assert ((-2)**r).is_negative is None
|
| 1288 |
+
assert ((-2)**n).is_negative is False
|
| 1289 |
+
assert ((-2)**m).is_negative is True
|
| 1290 |
+
|
| 1291 |
+
assert (k**2).is_negative is False
|
| 1292 |
+
assert (k**(-2)).is_negative is False
|
| 1293 |
+
|
| 1294 |
+
assert (k**r).is_negative is False
|
| 1295 |
+
assert ((-k)**r).is_negative is None
|
| 1296 |
+
assert ((-k)**n).is_negative is False
|
| 1297 |
+
assert ((-k)**m).is_negative is True
|
| 1298 |
+
|
| 1299 |
+
assert (2**x).is_positive is None
|
| 1300 |
+
assert (2**x).is_negative is None
|
| 1301 |
+
|
| 1302 |
+
|
| 1303 |
+
def test_Pow_is_zero():
|
| 1304 |
+
z = Symbol('z', zero=True)
|
| 1305 |
+
e = z**2
|
| 1306 |
+
assert e.is_zero
|
| 1307 |
+
assert e.is_positive is False
|
| 1308 |
+
assert e.is_negative is False
|
| 1309 |
+
|
| 1310 |
+
assert Pow(0, 0, evaluate=False).is_zero is False
|
| 1311 |
+
assert Pow(0, 3, evaluate=False).is_zero
|
| 1312 |
+
assert Pow(0, oo, evaluate=False).is_zero
|
| 1313 |
+
assert Pow(0, -3, evaluate=False).is_zero is False
|
| 1314 |
+
assert Pow(0, -oo, evaluate=False).is_zero is False
|
| 1315 |
+
assert Pow(2, 2, evaluate=False).is_zero is False
|
| 1316 |
+
|
| 1317 |
+
a = Symbol('a', zero=False)
|
| 1318 |
+
assert Pow(a, 3).is_zero is False # issue 7965
|
| 1319 |
+
|
| 1320 |
+
assert Pow(2, oo, evaluate=False).is_zero is False
|
| 1321 |
+
assert Pow(2, -oo, evaluate=False).is_zero
|
| 1322 |
+
assert Pow(S.Half, oo, evaluate=False).is_zero
|
| 1323 |
+
assert Pow(S.Half, -oo, evaluate=False).is_zero is False
|
| 1324 |
+
|
| 1325 |
+
# All combinations of real/complex base/exponent
|
| 1326 |
+
h = S.Half
|
| 1327 |
+
T = True
|
| 1328 |
+
F = False
|
| 1329 |
+
N = None
|
| 1330 |
+
|
| 1331 |
+
pow_iszero = [
|
| 1332 |
+
['**', 0, h, 1, 2, -h, -1,-2,-2*I,-I/2,I/2,1+I,oo,-oo,zoo],
|
| 1333 |
+
[ 0, F, T, T, T, F, F, F, F, F, F, N, T, F, N],
|
| 1334 |
+
[ h, F, F, F, F, F, F, F, F, F, F, F, T, F, N],
|
| 1335 |
+
[ 1, F, F, F, F, F, F, F, F, F, F, F, F, F, N],
|
| 1336 |
+
[ 2, F, F, F, F, F, F, F, F, F, F, F, F, T, N],
|
| 1337 |
+
[ -h, F, F, F, F, F, F, F, F, F, F, F, T, F, N],
|
| 1338 |
+
[ -1, F, F, F, F, F, F, F, F, F, F, F, F, F, N],
|
| 1339 |
+
[ -2, F, F, F, F, F, F, F, F, F, F, F, F, T, N],
|
| 1340 |
+
[-2*I, F, F, F, F, F, F, F, F, F, F, F, F, T, N],
|
| 1341 |
+
[-I/2, F, F, F, F, F, F, F, F, F, F, F, T, F, N],
|
| 1342 |
+
[ I/2, F, F, F, F, F, F, F, F, F, F, F, T, F, N],
|
| 1343 |
+
[ 1+I, F, F, F, F, F, F, F, F, F, F, F, F, T, N],
|
| 1344 |
+
[ oo, F, F, F, F, T, T, T, F, F, F, F, F, T, N],
|
| 1345 |
+
[ -oo, F, F, F, F, T, T, T, F, F, F, F, F, T, N],
|
| 1346 |
+
[ zoo, F, F, F, F, T, T, T, N, N, N, N, F, T, N]
|
| 1347 |
+
]
|
| 1348 |
+
|
| 1349 |
+
def test_table(table):
|
| 1350 |
+
n = len(table[0])
|
| 1351 |
+
for row in range(1, n):
|
| 1352 |
+
base = table[row][0]
|
| 1353 |
+
for col in range(1, n):
|
| 1354 |
+
exp = table[0][col]
|
| 1355 |
+
is_zero = table[row][col]
|
| 1356 |
+
# The actual test here:
|
| 1357 |
+
assert Pow(base, exp, evaluate=False).is_zero is is_zero
|
| 1358 |
+
|
| 1359 |
+
test_table(pow_iszero)
|
| 1360 |
+
|
| 1361 |
+
# A zero symbol...
|
| 1362 |
+
zo, zo2 = symbols('zo, zo2', zero=True)
|
| 1363 |
+
|
| 1364 |
+
# All combinations of finite symbols
|
| 1365 |
+
zf, zf2 = symbols('zf, zf2', finite=True)
|
| 1366 |
+
wf, wf2 = symbols('wf, wf2', nonzero=True)
|
| 1367 |
+
xf, xf2 = symbols('xf, xf2', real=True)
|
| 1368 |
+
yf, yf2 = symbols('yf, yf2', nonzero=True)
|
| 1369 |
+
af, af2 = symbols('af, af2', positive=True)
|
| 1370 |
+
bf, bf2 = symbols('bf, bf2', nonnegative=True)
|
| 1371 |
+
cf, cf2 = symbols('cf, cf2', negative=True)
|
| 1372 |
+
df, df2 = symbols('df, df2', nonpositive=True)
|
| 1373 |
+
|
| 1374 |
+
# Without finiteness:
|
| 1375 |
+
zi, zi2 = symbols('zi, zi2')
|
| 1376 |
+
wi, wi2 = symbols('wi, wi2', zero=False)
|
| 1377 |
+
xi, xi2 = symbols('xi, xi2', extended_real=True)
|
| 1378 |
+
yi, yi2 = symbols('yi, yi2', zero=False, extended_real=True)
|
| 1379 |
+
ai, ai2 = symbols('ai, ai2', extended_positive=True)
|
| 1380 |
+
bi, bi2 = symbols('bi, bi2', extended_nonnegative=True)
|
| 1381 |
+
ci, ci2 = symbols('ci, ci2', extended_negative=True)
|
| 1382 |
+
di, di2 = symbols('di, di2', extended_nonpositive=True)
|
| 1383 |
+
|
| 1384 |
+
pow_iszero_sym = [
|
| 1385 |
+
['**',zo,wf,yf,af,cf,zf,xf,bf,df,zi,wi,xi,yi,ai,bi,ci,di],
|
| 1386 |
+
[ zo2, F, N, N, T, F, N, N, N, F, N, N, N, N, T, N, F, F],
|
| 1387 |
+
[ wf2, F, F, F, F, F, F, F, F, F, N, N, N, N, N, N, N, N],
|
| 1388 |
+
[ yf2, F, F, F, F, F, F, F, F, F, N, N, N, N, N, N, N, N],
|
| 1389 |
+
[ af2, F, F, F, F, F, F, F, F, F, N, N, N, N, N, N, N, N],
|
| 1390 |
+
[ cf2, F, F, F, F, F, F, F, F, F, N, N, N, N, N, N, N, N],
|
| 1391 |
+
[ zf2, N, N, N, N, F, N, N, N, N, N, N, N, N, N, N, N, N],
|
| 1392 |
+
[ xf2, N, N, N, N, F, N, N, N, N, N, N, N, N, N, N, N, N],
|
| 1393 |
+
[ bf2, N, N, N, N, F, N, N, N, N, N, N, N, N, N, N, N, N],
|
| 1394 |
+
[ df2, N, N, N, N, F, N, N, N, N, N, N, N, N, N, N, N, N],
|
| 1395 |
+
[ zi2, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N],
|
| 1396 |
+
[ wi2, F, N, N, F, N, N, N, F, N, N, N, N, N, N, N, N, N],
|
| 1397 |
+
[ xi2, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N],
|
| 1398 |
+
[ yi2, F, N, N, F, N, N, N, F, N, N, N, N, N, N, N, N, N],
|
| 1399 |
+
[ ai2, F, N, N, F, N, N, N, F, N, N, N, N, N, N, N, N, N],
|
| 1400 |
+
[ bi2, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N],
|
| 1401 |
+
[ ci2, F, N, N, F, N, N, N, F, N, N, N, N, N, N, N, N, N],
|
| 1402 |
+
[ di2, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N, N]
|
| 1403 |
+
]
|
| 1404 |
+
|
| 1405 |
+
test_table(pow_iszero_sym)
|
| 1406 |
+
|
| 1407 |
+
# In some cases (x**x).is_zero is different from (x**y).is_zero even if y
|
| 1408 |
+
# has the same assumptions as x.
|
| 1409 |
+
assert (zo ** zo).is_zero is False
|
| 1410 |
+
assert (wf ** wf).is_zero is False
|
| 1411 |
+
assert (yf ** yf).is_zero is False
|
| 1412 |
+
assert (af ** af).is_zero is False
|
| 1413 |
+
assert (cf ** cf).is_zero is False
|
| 1414 |
+
assert (zf ** zf).is_zero is None
|
| 1415 |
+
assert (xf ** xf).is_zero is None
|
| 1416 |
+
assert (bf ** bf).is_zero is False # None in table
|
| 1417 |
+
assert (df ** df).is_zero is None
|
| 1418 |
+
assert (zi ** zi).is_zero is None
|
| 1419 |
+
assert (wi ** wi).is_zero is None
|
| 1420 |
+
assert (xi ** xi).is_zero is None
|
| 1421 |
+
assert (yi ** yi).is_zero is None
|
| 1422 |
+
assert (ai ** ai).is_zero is False # None in table
|
| 1423 |
+
assert (bi ** bi).is_zero is False # None in table
|
| 1424 |
+
assert (ci ** ci).is_zero is None
|
| 1425 |
+
assert (di ** di).is_zero is None
|
| 1426 |
+
|
| 1427 |
+
|
| 1428 |
+
def test_Pow_is_nonpositive_nonnegative():
|
| 1429 |
+
x = Symbol('x', real=True)
|
| 1430 |
+
|
| 1431 |
+
k = Symbol('k', integer=True, nonnegative=True)
|
| 1432 |
+
l = Symbol('l', integer=True, positive=True)
|
| 1433 |
+
n = Symbol('n', even=True)
|
| 1434 |
+
m = Symbol('m', odd=True)
|
| 1435 |
+
|
| 1436 |
+
assert (x**(4*k)).is_nonnegative is True
|
| 1437 |
+
assert (2**x).is_nonnegative is True
|
| 1438 |
+
assert ((-2)**x).is_nonnegative is None
|
| 1439 |
+
assert ((-2)**n).is_nonnegative is True
|
| 1440 |
+
assert ((-2)**m).is_nonnegative is False
|
| 1441 |
+
|
| 1442 |
+
assert (k**2).is_nonnegative is True
|
| 1443 |
+
assert (k**(-2)).is_nonnegative is None
|
| 1444 |
+
assert (k**k).is_nonnegative is True
|
| 1445 |
+
|
| 1446 |
+
assert (k**x).is_nonnegative is None # NOTE (0**x).is_real = U
|
| 1447 |
+
assert (l**x).is_nonnegative is True
|
| 1448 |
+
assert (l**x).is_positive is True
|
| 1449 |
+
assert ((-k)**x).is_nonnegative is None
|
| 1450 |
+
|
| 1451 |
+
assert ((-k)**m).is_nonnegative is None
|
| 1452 |
+
|
| 1453 |
+
assert (2**x).is_nonpositive is False
|
| 1454 |
+
assert ((-2)**x).is_nonpositive is None
|
| 1455 |
+
assert ((-2)**n).is_nonpositive is False
|
| 1456 |
+
assert ((-2)**m).is_nonpositive is True
|
| 1457 |
+
|
| 1458 |
+
assert (k**2).is_nonpositive is None
|
| 1459 |
+
assert (k**(-2)).is_nonpositive is None
|
| 1460 |
+
|
| 1461 |
+
assert (k**x).is_nonpositive is None
|
| 1462 |
+
assert ((-k)**x).is_nonpositive is None
|
| 1463 |
+
assert ((-k)**n).is_nonpositive is None
|
| 1464 |
+
|
| 1465 |
+
|
| 1466 |
+
assert (x**2).is_nonnegative is True
|
| 1467 |
+
i = symbols('i', imaginary=True)
|
| 1468 |
+
assert (i**2).is_nonpositive is True
|
| 1469 |
+
assert (i**4).is_nonpositive is False
|
| 1470 |
+
assert (i**3).is_nonpositive is False
|
| 1471 |
+
assert (I**i).is_nonnegative is True
|
| 1472 |
+
assert (exp(I)**i).is_nonnegative is True
|
| 1473 |
+
|
| 1474 |
+
assert ((-l)**n).is_nonnegative is True
|
| 1475 |
+
assert ((-l)**m).is_nonpositive is True
|
| 1476 |
+
assert ((-k)**n).is_nonnegative is None
|
| 1477 |
+
assert ((-k)**m).is_nonpositive is None
|
| 1478 |
+
|
| 1479 |
+
|
| 1480 |
+
def test_Mul_is_imaginary_real():
|
| 1481 |
+
r = Symbol('r', real=True)
|
| 1482 |
+
p = Symbol('p', positive=True)
|
| 1483 |
+
i1 = Symbol('i1', imaginary=True)
|
| 1484 |
+
i2 = Symbol('i2', imaginary=True)
|
| 1485 |
+
x = Symbol('x')
|
| 1486 |
+
|
| 1487 |
+
assert I.is_imaginary is True
|
| 1488 |
+
assert I.is_real is False
|
| 1489 |
+
assert (-I).is_imaginary is True
|
| 1490 |
+
assert (-I).is_real is False
|
| 1491 |
+
assert (3*I).is_imaginary is True
|
| 1492 |
+
assert (3*I).is_real is False
|
| 1493 |
+
assert (I*I).is_imaginary is False
|
| 1494 |
+
assert (I*I).is_real is True
|
| 1495 |
+
|
| 1496 |
+
e = (p + p*I)
|
| 1497 |
+
j = Symbol('j', integer=True, zero=False)
|
| 1498 |
+
assert (e**j).is_real is None
|
| 1499 |
+
assert (e**(2*j)).is_real is None
|
| 1500 |
+
assert (e**j).is_imaginary is None
|
| 1501 |
+
assert (e**(2*j)).is_imaginary is None
|
| 1502 |
+
|
| 1503 |
+
assert (e**-1).is_imaginary is False
|
| 1504 |
+
assert (e**2).is_imaginary
|
| 1505 |
+
assert (e**3).is_imaginary is False
|
| 1506 |
+
assert (e**4).is_imaginary is False
|
| 1507 |
+
assert (e**5).is_imaginary is False
|
| 1508 |
+
assert (e**-1).is_real is False
|
| 1509 |
+
assert (e**2).is_real is False
|
| 1510 |
+
assert (e**3).is_real is False
|
| 1511 |
+
assert (e**4).is_real is True
|
| 1512 |
+
assert (e**5).is_real is False
|
| 1513 |
+
assert (e**3).is_complex
|
| 1514 |
+
|
| 1515 |
+
assert (r*i1).is_imaginary is None
|
| 1516 |
+
assert (r*i1).is_real is None
|
| 1517 |
+
|
| 1518 |
+
assert (x*i1).is_imaginary is None
|
| 1519 |
+
assert (x*i1).is_real is None
|
| 1520 |
+
|
| 1521 |
+
assert (i1*i2).is_imaginary is False
|
| 1522 |
+
assert (i1*i2).is_real is True
|
| 1523 |
+
|
| 1524 |
+
assert (r*i1*i2).is_imaginary is False
|
| 1525 |
+
assert (r*i1*i2).is_real is True
|
| 1526 |
+
|
| 1527 |
+
# Github's issue 5874:
|
| 1528 |
+
nr = Symbol('nr', real=False, complex=True) # e.g. I or 1 + I
|
| 1529 |
+
a = Symbol('a', real=True, nonzero=True)
|
| 1530 |
+
b = Symbol('b', real=True)
|
| 1531 |
+
assert (i1*nr).is_real is None
|
| 1532 |
+
assert (a*nr).is_real is False
|
| 1533 |
+
assert (b*nr).is_real is None
|
| 1534 |
+
|
| 1535 |
+
ni = Symbol('ni', imaginary=False, complex=True) # e.g. 2 or 1 + I
|
| 1536 |
+
a = Symbol('a', real=True, nonzero=True)
|
| 1537 |
+
b = Symbol('b', real=True)
|
| 1538 |
+
assert (i1*ni).is_real is False
|
| 1539 |
+
assert (a*ni).is_real is None
|
| 1540 |
+
assert (b*ni).is_real is None
|
| 1541 |
+
|
| 1542 |
+
|
| 1543 |
+
def test_Mul_hermitian_antihermitian():
|
| 1544 |
+
xz, yz = symbols('xz, yz', zero=True, antihermitian=True)
|
| 1545 |
+
xf, yf = symbols('xf, yf', hermitian=False, antihermitian=False, finite=True)
|
| 1546 |
+
xh, yh = symbols('xh, yh', hermitian=True, antihermitian=False, nonzero=True)
|
| 1547 |
+
xa, ya = symbols('xa, ya', hermitian=False, antihermitian=True, zero=False, finite=True)
|
| 1548 |
+
assert (xz*xh).is_hermitian is True
|
| 1549 |
+
assert (xz*xh).is_antihermitian is True
|
| 1550 |
+
assert (xz*xa).is_hermitian is True
|
| 1551 |
+
assert (xz*xa).is_antihermitian is True
|
| 1552 |
+
assert (xf*yf).is_hermitian is None
|
| 1553 |
+
assert (xf*yf).is_antihermitian is None
|
| 1554 |
+
assert (xh*yh).is_hermitian is True
|
| 1555 |
+
assert (xh*yh).is_antihermitian is False
|
| 1556 |
+
assert (xh*ya).is_hermitian is False
|
| 1557 |
+
assert (xh*ya).is_antihermitian is True
|
| 1558 |
+
assert (xa*ya).is_hermitian is True
|
| 1559 |
+
assert (xa*ya).is_antihermitian is False
|
| 1560 |
+
|
| 1561 |
+
a = Symbol('a', hermitian=True, zero=False)
|
| 1562 |
+
b = Symbol('b', hermitian=True)
|
| 1563 |
+
c = Symbol('c', hermitian=False)
|
| 1564 |
+
d = Symbol('d', antihermitian=True)
|
| 1565 |
+
e1 = Mul(a, b, c, evaluate=False)
|
| 1566 |
+
e2 = Mul(b, a, c, evaluate=False)
|
| 1567 |
+
e3 = Mul(a, b, c, d, evaluate=False)
|
| 1568 |
+
e4 = Mul(b, a, c, d, evaluate=False)
|
| 1569 |
+
e5 = Mul(a, c, evaluate=False)
|
| 1570 |
+
e6 = Mul(a, c, d, evaluate=False)
|
| 1571 |
+
assert e1.is_hermitian is None
|
| 1572 |
+
assert e2.is_hermitian is None
|
| 1573 |
+
assert e1.is_antihermitian is None
|
| 1574 |
+
assert e2.is_antihermitian is None
|
| 1575 |
+
assert e3.is_antihermitian is None
|
| 1576 |
+
assert e4.is_antihermitian is None
|
| 1577 |
+
assert e5.is_antihermitian is None
|
| 1578 |
+
assert e6.is_antihermitian is None
|
| 1579 |
+
|
| 1580 |
+
|
| 1581 |
+
def test_Add_is_comparable():
|
| 1582 |
+
assert (x + y).is_comparable is False
|
| 1583 |
+
assert (x + 1).is_comparable is False
|
| 1584 |
+
assert (Rational(1, 3) - sqrt(8)).is_comparable is True
|
| 1585 |
+
|
| 1586 |
+
|
| 1587 |
+
def test_Mul_is_comparable():
|
| 1588 |
+
assert (x*y).is_comparable is False
|
| 1589 |
+
assert (x*2).is_comparable is False
|
| 1590 |
+
assert (sqrt(2)*Rational(1, 3)).is_comparable is True
|
| 1591 |
+
|
| 1592 |
+
|
| 1593 |
+
def test_Pow_is_comparable():
|
| 1594 |
+
assert (x**y).is_comparable is False
|
| 1595 |
+
assert (x**2).is_comparable is False
|
| 1596 |
+
assert (sqrt(Rational(1, 3))).is_comparable is True
|
| 1597 |
+
|
| 1598 |
+
|
| 1599 |
+
def test_Add_is_positive_2():
|
| 1600 |
+
e = Rational(1, 3) - sqrt(8)
|
| 1601 |
+
assert e.is_positive is False
|
| 1602 |
+
assert e.is_negative is True
|
| 1603 |
+
|
| 1604 |
+
e = pi - 1
|
| 1605 |
+
assert e.is_positive is True
|
| 1606 |
+
assert e.is_negative is False
|
| 1607 |
+
|
| 1608 |
+
|
| 1609 |
+
def test_Add_is_irrational():
|
| 1610 |
+
i = Symbol('i', irrational=True)
|
| 1611 |
+
|
| 1612 |
+
assert i.is_irrational is True
|
| 1613 |
+
assert i.is_rational is False
|
| 1614 |
+
|
| 1615 |
+
assert (i + 1).is_irrational is True
|
| 1616 |
+
assert (i + 1).is_rational is False
|
| 1617 |
+
|
| 1618 |
+
|
| 1619 |
+
def test_Mul_is_irrational():
|
| 1620 |
+
expr = Mul(1, 2, 3, evaluate=False)
|
| 1621 |
+
assert expr.is_irrational is False
|
| 1622 |
+
expr = Mul(1, I, I, evaluate=False)
|
| 1623 |
+
assert expr.is_rational is None # I * I = -1 but *no evaluation allowed*
|
| 1624 |
+
# sqrt(2) * I * I = -sqrt(2) is irrational but
|
| 1625 |
+
# this can't be determined without evaluating the
|
| 1626 |
+
# expression and the eval_is routines shouldn't do that
|
| 1627 |
+
expr = Mul(sqrt(2), I, I, evaluate=False)
|
| 1628 |
+
assert expr.is_irrational is None
|
| 1629 |
+
|
| 1630 |
+
|
| 1631 |
+
def test_issue_3531():
|
| 1632 |
+
# https://github.com/sympy/sympy/issues/3531
|
| 1633 |
+
# https://github.com/sympy/sympy/pull/18116
|
| 1634 |
+
class MightyNumeric(tuple):
|
| 1635 |
+
def __rtruediv__(self, other):
|
| 1636 |
+
return "something"
|
| 1637 |
+
|
| 1638 |
+
assert sympify(1)/MightyNumeric((1, 2)) == "something"
|
| 1639 |
+
|
| 1640 |
+
|
| 1641 |
+
def test_issue_3531b():
|
| 1642 |
+
class Foo:
|
| 1643 |
+
def __init__(self):
|
| 1644 |
+
self.field = 1.0
|
| 1645 |
+
|
| 1646 |
+
def __mul__(self, other):
|
| 1647 |
+
self.field = self.field * other
|
| 1648 |
+
|
| 1649 |
+
def __rmul__(self, other):
|
| 1650 |
+
self.field = other * self.field
|
| 1651 |
+
f = Foo()
|
| 1652 |
+
x = Symbol("x")
|
| 1653 |
+
assert f*x == x*f
|
| 1654 |
+
|
| 1655 |
+
|
| 1656 |
+
def test_bug3():
|
| 1657 |
+
a = Symbol("a")
|
| 1658 |
+
b = Symbol("b", positive=True)
|
| 1659 |
+
e = 2*a + b
|
| 1660 |
+
f = b + 2*a
|
| 1661 |
+
assert e == f
|
| 1662 |
+
|
| 1663 |
+
|
| 1664 |
+
def test_suppressed_evaluation():
|
| 1665 |
+
a = Add(0, 3, 2, evaluate=False)
|
| 1666 |
+
b = Mul(1, 3, 2, evaluate=False)
|
| 1667 |
+
c = Pow(3, 2, evaluate=False)
|
| 1668 |
+
assert a != 6
|
| 1669 |
+
assert a.func is Add
|
| 1670 |
+
assert a.args == (0, 3, 2)
|
| 1671 |
+
assert b != 6
|
| 1672 |
+
assert b.func is Mul
|
| 1673 |
+
assert b.args == (1, 3, 2)
|
| 1674 |
+
assert c != 9
|
| 1675 |
+
assert c.func is Pow
|
| 1676 |
+
assert c.args == (3, 2)
|
| 1677 |
+
|
| 1678 |
+
|
| 1679 |
+
def test_AssocOp_doit():
|
| 1680 |
+
a = Add(x,x, evaluate=False)
|
| 1681 |
+
b = Mul(y,y, evaluate=False)
|
| 1682 |
+
c = Add(b,b, evaluate=False)
|
| 1683 |
+
d = Mul(a,a, evaluate=False)
|
| 1684 |
+
assert c.doit(deep=False).func == Mul
|
| 1685 |
+
assert c.doit(deep=False).args == (2,y,y)
|
| 1686 |
+
assert c.doit().func == Mul
|
| 1687 |
+
assert c.doit().args == (2, Pow(y,2))
|
| 1688 |
+
assert d.doit(deep=False).func == Pow
|
| 1689 |
+
assert d.doit(deep=False).args == (a, 2*S.One)
|
| 1690 |
+
assert d.doit().func == Mul
|
| 1691 |
+
assert d.doit().args == (4*S.One, Pow(x,2))
|
| 1692 |
+
|
| 1693 |
+
|
| 1694 |
+
def test_Add_Mul_Expr_args():
|
| 1695 |
+
nonexpr = [Basic(), Poly(x, x), FiniteSet(x)]
|
| 1696 |
+
for typ in [Add, Mul]:
|
| 1697 |
+
for obj in nonexpr:
|
| 1698 |
+
# The cache can mess with the stacklevel check
|
| 1699 |
+
with warns(SymPyDeprecationWarning, test_stacklevel=False):
|
| 1700 |
+
typ(obj, 1)
|
| 1701 |
+
|
| 1702 |
+
|
| 1703 |
+
def test_Add_as_coeff_mul():
|
| 1704 |
+
# issue 5524. These should all be (1, self)
|
| 1705 |
+
assert (x + 1).as_coeff_mul() == (1, (x + 1,))
|
| 1706 |
+
assert (x + 2).as_coeff_mul() == (1, (x + 2,))
|
| 1707 |
+
assert (x + 3).as_coeff_mul() == (1, (x + 3,))
|
| 1708 |
+
|
| 1709 |
+
assert (x - 1).as_coeff_mul() == (1, (x - 1,))
|
| 1710 |
+
assert (x - 2).as_coeff_mul() == (1, (x - 2,))
|
| 1711 |
+
assert (x - 3).as_coeff_mul() == (1, (x - 3,))
|
| 1712 |
+
|
| 1713 |
+
n = Symbol('n', integer=True)
|
| 1714 |
+
assert (n + 1).as_coeff_mul() == (1, (n + 1,))
|
| 1715 |
+
assert (n + 2).as_coeff_mul() == (1, (n + 2,))
|
| 1716 |
+
assert (n + 3).as_coeff_mul() == (1, (n + 3,))
|
| 1717 |
+
|
| 1718 |
+
assert (n - 1).as_coeff_mul() == (1, (n - 1,))
|
| 1719 |
+
assert (n - 2).as_coeff_mul() == (1, (n - 2,))
|
| 1720 |
+
assert (n - 3).as_coeff_mul() == (1, (n - 3,))
|
| 1721 |
+
|
| 1722 |
+
|
| 1723 |
+
def test_Pow_as_coeff_mul_doesnt_expand():
|
| 1724 |
+
assert exp(x + y).as_coeff_mul() == (1, (exp(x + y),))
|
| 1725 |
+
assert exp(x + exp(x + y)) != exp(x + exp(x)*exp(y))
|
| 1726 |
+
|
| 1727 |
+
def test_issue_24751():
|
| 1728 |
+
expr = Add(-2, -3, evaluate=False)
|
| 1729 |
+
expr1 = Add(-1, expr, evaluate=False)
|
| 1730 |
+
assert int(expr1) == int((-3 - 2) - 1)
|
| 1731 |
+
|
| 1732 |
+
|
| 1733 |
+
def test_issue_3514_18626():
|
| 1734 |
+
assert sqrt(S.Half) * sqrt(6) == 2 * sqrt(3)/2
|
| 1735 |
+
assert S.Half*sqrt(6)*sqrt(2) == sqrt(3)
|
| 1736 |
+
assert sqrt(6)/2*sqrt(2) == sqrt(3)
|
| 1737 |
+
assert sqrt(6)*sqrt(2)/2 == sqrt(3)
|
| 1738 |
+
assert sqrt(8)**Rational(2, 3) == 2
|
| 1739 |
+
|
| 1740 |
+
|
| 1741 |
+
def test_make_args():
|
| 1742 |
+
assert Add.make_args(x) == (x,)
|
| 1743 |
+
assert Mul.make_args(x) == (x,)
|
| 1744 |
+
|
| 1745 |
+
assert Add.make_args(x*y*z) == (x*y*z,)
|
| 1746 |
+
assert Mul.make_args(x*y*z) == (x*y*z).args
|
| 1747 |
+
|
| 1748 |
+
assert Add.make_args(x + y + z) == (x + y + z).args
|
| 1749 |
+
assert Mul.make_args(x + y + z) == (x + y + z,)
|
| 1750 |
+
|
| 1751 |
+
assert Add.make_args((x + y)**z) == ((x + y)**z,)
|
| 1752 |
+
assert Mul.make_args((x + y)**z) == ((x + y)**z,)
|
| 1753 |
+
|
| 1754 |
+
|
| 1755 |
+
def test_issue_5126():
|
| 1756 |
+
assert (-2)**x*(-3)**x != 6**x
|
| 1757 |
+
i = Symbol('i', integer=1)
|
| 1758 |
+
assert (-2)**i*(-3)**i == 6**i
|
| 1759 |
+
|
| 1760 |
+
|
| 1761 |
+
def test_Rational_as_content_primitive():
|
| 1762 |
+
c, p = S.One, S.Zero
|
| 1763 |
+
assert (c*p).as_content_primitive() == (c, p)
|
| 1764 |
+
c, p = S.Half, S.One
|
| 1765 |
+
assert (c*p).as_content_primitive() == (c, p)
|
| 1766 |
+
|
| 1767 |
+
|
| 1768 |
+
def test_Add_as_content_primitive():
|
| 1769 |
+
assert (x + 2).as_content_primitive() == (1, x + 2)
|
| 1770 |
+
|
| 1771 |
+
assert (3*x + 2).as_content_primitive() == (1, 3*x + 2)
|
| 1772 |
+
assert (3*x + 3).as_content_primitive() == (3, x + 1)
|
| 1773 |
+
assert (3*x + 6).as_content_primitive() == (3, x + 2)
|
| 1774 |
+
|
| 1775 |
+
assert (3*x + 2*y).as_content_primitive() == (1, 3*x + 2*y)
|
| 1776 |
+
assert (3*x + 3*y).as_content_primitive() == (3, x + y)
|
| 1777 |
+
assert (3*x + 6*y).as_content_primitive() == (3, x + 2*y)
|
| 1778 |
+
|
| 1779 |
+
assert (3/x + 2*x*y*z**2).as_content_primitive() == (1, 3/x + 2*x*y*z**2)
|
| 1780 |
+
assert (3/x + 3*x*y*z**2).as_content_primitive() == (3, 1/x + x*y*z**2)
|
| 1781 |
+
assert (3/x + 6*x*y*z**2).as_content_primitive() == (3, 1/x + 2*x*y*z**2)
|
| 1782 |
+
|
| 1783 |
+
assert (2*x/3 + 4*y/9).as_content_primitive() == \
|
| 1784 |
+
(Rational(2, 9), 3*x + 2*y)
|
| 1785 |
+
assert (2*x/3 + 2.5*y).as_content_primitive() == \
|
| 1786 |
+
(Rational(1, 3), 2*x + 7.5*y)
|
| 1787 |
+
|
| 1788 |
+
# the coefficient may sort to a position other than 0
|
| 1789 |
+
p = 3 + x + y
|
| 1790 |
+
assert (2*p).expand().as_content_primitive() == (2, p)
|
| 1791 |
+
assert (2.0*p).expand().as_content_primitive() == (1, 2.*p)
|
| 1792 |
+
p *= -1
|
| 1793 |
+
assert (2*p).expand().as_content_primitive() == (2, p)
|
| 1794 |
+
|
| 1795 |
+
|
| 1796 |
+
def test_Mul_as_content_primitive():
|
| 1797 |
+
assert (2*x).as_content_primitive() == (2, x)
|
| 1798 |
+
assert (x*(2 + 2*x)).as_content_primitive() == (2, x*(1 + x))
|
| 1799 |
+
assert (x*(2 + 2*y)*(3*x + 3)**2).as_content_primitive() == \
|
| 1800 |
+
(18, x*(1 + y)*(x + 1)**2)
|
| 1801 |
+
assert ((2 + 2*x)**2*(3 + 6*x) + S.Half).as_content_primitive() == \
|
| 1802 |
+
(S.Half, 24*(x + 1)**2*(2*x + 1) + 1)
|
| 1803 |
+
|
| 1804 |
+
|
| 1805 |
+
def test_Pow_as_content_primitive():
|
| 1806 |
+
assert (x**y).as_content_primitive() == (1, x**y)
|
| 1807 |
+
assert ((2*x + 2)**y).as_content_primitive() == \
|
| 1808 |
+
(1, (Mul(2, (x + 1), evaluate=False))**y)
|
| 1809 |
+
assert ((2*x + 2)**3).as_content_primitive() == (8, (x + 1)**3)
|
| 1810 |
+
|
| 1811 |
+
|
| 1812 |
+
def test_issue_5460():
|
| 1813 |
+
u = Mul(2, (1 + x), evaluate=False)
|
| 1814 |
+
assert (2 + u).args == (2, u)
|
| 1815 |
+
|
| 1816 |
+
|
| 1817 |
+
def test_product_irrational():
|
| 1818 |
+
assert (I*pi).is_irrational is False
|
| 1819 |
+
# The following used to be deduced from the above bug:
|
| 1820 |
+
assert (I*pi).is_positive is False
|
| 1821 |
+
|
| 1822 |
+
|
| 1823 |
+
def test_issue_5919():
|
| 1824 |
+
assert (x/(y*(1 + y))).expand() == x/(y**2 + y)
|
| 1825 |
+
|
| 1826 |
+
|
| 1827 |
+
def test_Mod():
|
| 1828 |
+
assert Mod(x, 1).func is Mod
|
| 1829 |
+
assert pi % pi is S.Zero
|
| 1830 |
+
assert Mod(5, 3) == 2
|
| 1831 |
+
assert Mod(-5, 3) == 1
|
| 1832 |
+
assert Mod(5, -3) == -1
|
| 1833 |
+
assert Mod(-5, -3) == -2
|
| 1834 |
+
assert type(Mod(3.2, 2, evaluate=False)) == Mod
|
| 1835 |
+
assert 5 % x == Mod(5, x)
|
| 1836 |
+
assert x % 5 == Mod(x, 5)
|
| 1837 |
+
assert x % y == Mod(x, y)
|
| 1838 |
+
assert (x % y).subs({x: 5, y: 3}) == 2
|
| 1839 |
+
assert Mod(nan, 1) is nan
|
| 1840 |
+
assert Mod(1, nan) is nan
|
| 1841 |
+
assert Mod(nan, nan) is nan
|
| 1842 |
+
|
| 1843 |
+
assert Mod(0, x) == 0
|
| 1844 |
+
with raises(ZeroDivisionError):
|
| 1845 |
+
Mod(x, 0)
|
| 1846 |
+
|
| 1847 |
+
k = Symbol('k', integer=True)
|
| 1848 |
+
m = Symbol('m', integer=True, positive=True)
|
| 1849 |
+
assert (x**m % x).func is Mod
|
| 1850 |
+
assert (k**(-m) % k).func is Mod
|
| 1851 |
+
assert k**m % k == 0
|
| 1852 |
+
assert (-2*k)**m % k == 0
|
| 1853 |
+
|
| 1854 |
+
# Float handling
|
| 1855 |
+
point3 = Float(3.3) % 1
|
| 1856 |
+
assert (x - 3.3) % 1 == Mod(1.*x + 1 - point3, 1)
|
| 1857 |
+
assert Mod(-3.3, 1) == 1 - point3
|
| 1858 |
+
assert Mod(0.7, 1) == Float(0.7)
|
| 1859 |
+
e = Mod(1.3, 1)
|
| 1860 |
+
assert comp(e, .3) and e.is_Float
|
| 1861 |
+
e = Mod(1.3, .7)
|
| 1862 |
+
assert comp(e, .6) and e.is_Float
|
| 1863 |
+
e = Mod(1.3, Rational(7, 10))
|
| 1864 |
+
assert comp(e, .6) and e.is_Float
|
| 1865 |
+
e = Mod(Rational(13, 10), 0.7)
|
| 1866 |
+
assert comp(e, .6) and e.is_Float
|
| 1867 |
+
e = Mod(Rational(13, 10), Rational(7, 10))
|
| 1868 |
+
assert comp(e, .6) and e.is_Rational
|
| 1869 |
+
|
| 1870 |
+
# check that sign is right
|
| 1871 |
+
r2 = sqrt(2)
|
| 1872 |
+
r3 = sqrt(3)
|
| 1873 |
+
for i in [-r3, -r2, r2, r3]:
|
| 1874 |
+
for j in [-r3, -r2, r2, r3]:
|
| 1875 |
+
assert verify_numerically(i % j, i.n() % j.n())
|
| 1876 |
+
for _x in range(4):
|
| 1877 |
+
for _y in range(9):
|
| 1878 |
+
reps = [(x, _x), (y, _y)]
|
| 1879 |
+
assert Mod(3*x + y, 9).subs(reps) == (3*_x + _y) % 9
|
| 1880 |
+
|
| 1881 |
+
# denesting
|
| 1882 |
+
t = Symbol('t', real=True)
|
| 1883 |
+
assert Mod(Mod(x, t), t) == Mod(x, t)
|
| 1884 |
+
assert Mod(-Mod(x, t), t) == Mod(-x, t)
|
| 1885 |
+
assert Mod(Mod(x, 2*t), t) == Mod(x, t)
|
| 1886 |
+
assert Mod(-Mod(x, 2*t), t) == Mod(-x, t)
|
| 1887 |
+
assert Mod(Mod(x, t), 2*t) == Mod(x, t)
|
| 1888 |
+
assert Mod(-Mod(x, t), -2*t) == -Mod(x, t)
|
| 1889 |
+
for i in [-4, -2, 2, 4]:
|
| 1890 |
+
for j in [-4, -2, 2, 4]:
|
| 1891 |
+
for k in range(4):
|
| 1892 |
+
assert Mod(Mod(x, i), j).subs({x: k}) == (k % i) % j
|
| 1893 |
+
assert Mod(-Mod(x, i), j).subs({x: k}) == -(k % i) % j
|
| 1894 |
+
|
| 1895 |
+
# known difference
|
| 1896 |
+
assert Mod(5*sqrt(2), sqrt(5)) == 5*sqrt(2) - 3*sqrt(5)
|
| 1897 |
+
p = symbols('p', positive=True)
|
| 1898 |
+
assert Mod(2, p + 3) == 2
|
| 1899 |
+
assert Mod(-2, p + 3) == p + 1
|
| 1900 |
+
assert Mod(2, -p - 3) == -p - 1
|
| 1901 |
+
assert Mod(-2, -p - 3) == -2
|
| 1902 |
+
assert Mod(p + 5, p + 3) == 2
|
| 1903 |
+
assert Mod(-p - 5, p + 3) == p + 1
|
| 1904 |
+
assert Mod(p + 5, -p - 3) == -p - 1
|
| 1905 |
+
assert Mod(-p - 5, -p - 3) == -2
|
| 1906 |
+
assert Mod(p + 1, p - 1).func is Mod
|
| 1907 |
+
|
| 1908 |
+
# handling sums
|
| 1909 |
+
assert (x + 3) % 1 == Mod(x, 1)
|
| 1910 |
+
assert (x + 3.0) % 1 == Mod(1.*x, 1)
|
| 1911 |
+
assert (x - S(33)/10) % 1 == Mod(x + S(7)/10, 1)
|
| 1912 |
+
|
| 1913 |
+
a = Mod(.6*x + y, .3*y)
|
| 1914 |
+
b = Mod(0.1*y + 0.6*x, 0.3*y)
|
| 1915 |
+
# Test that a, b are equal, with 1e-14 accuracy in coefficients
|
| 1916 |
+
eps = 1e-14
|
| 1917 |
+
assert abs((a.args[0] - b.args[0]).subs({x: 1, y: 1})) < eps
|
| 1918 |
+
assert abs((a.args[1] - b.args[1]).subs({x: 1, y: 1})) < eps
|
| 1919 |
+
|
| 1920 |
+
assert (x + 1) % x == 1 % x
|
| 1921 |
+
assert (x + y) % x == y % x
|
| 1922 |
+
assert (x + y + 2) % x == (y + 2) % x
|
| 1923 |
+
assert (a + 3*x + 1) % (2*x) == Mod(a + x + 1, 2*x)
|
| 1924 |
+
assert (12*x + 18*y) % (3*x) == 3*Mod(6*y, x)
|
| 1925 |
+
|
| 1926 |
+
# gcd extraction
|
| 1927 |
+
assert (-3*x) % (-2*y) == -Mod(3*x, 2*y)
|
| 1928 |
+
assert (.6*pi) % (.3*x*pi) == 0.3*pi*Mod(2, x)
|
| 1929 |
+
assert (.6*pi) % (.31*x*pi) == pi*Mod(0.6, 0.31*x)
|
| 1930 |
+
assert (6*pi) % (.3*x*pi) == 0.3*pi*Mod(20, x)
|
| 1931 |
+
assert (6*pi) % (.31*x*pi) == pi*Mod(6, 0.31*x)
|
| 1932 |
+
assert (6*pi) % (.42*x*pi) == pi*Mod(6, 0.42*x)
|
| 1933 |
+
assert (12*x) % (2*y) == 2*Mod(6*x, y)
|
| 1934 |
+
assert (12*x) % (3*5*y) == 3*Mod(4*x, 5*y)
|
| 1935 |
+
assert (12*x) % (15*x*y) == 3*x*Mod(4, 5*y)
|
| 1936 |
+
assert (-2*pi) % (3*pi) == pi
|
| 1937 |
+
assert (2*x + 2) % (x + 1) == 0
|
| 1938 |
+
assert (x*(x + 1)) % (x + 1) == (x + 1)*Mod(x, 1)
|
| 1939 |
+
assert Mod(5.0*x, 0.1*y) == 0.1*Mod(50*x, y)
|
| 1940 |
+
i = Symbol('i', integer=True)
|
| 1941 |
+
assert (3*i*x) % (2*i*y) == i*Mod(3*x, 2*y)
|
| 1942 |
+
assert Mod(4*i, 4) == 0
|
| 1943 |
+
|
| 1944 |
+
# issue 8677
|
| 1945 |
+
n = Symbol('n', integer=True, positive=True)
|
| 1946 |
+
assert factorial(n) % n == 0
|
| 1947 |
+
assert factorial(n + 2) % n == 0
|
| 1948 |
+
assert (factorial(n + 4) % (n + 5)).func is Mod
|
| 1949 |
+
|
| 1950 |
+
# Wilson's theorem
|
| 1951 |
+
assert factorial(18042, evaluate=False) % 18043 == 18042
|
| 1952 |
+
p = Symbol('n', prime=True)
|
| 1953 |
+
assert factorial(p - 1) % p == p - 1
|
| 1954 |
+
assert factorial(p - 1) % -p == -1
|
| 1955 |
+
assert (factorial(3, evaluate=False) % 4).doit() == 2
|
| 1956 |
+
n = Symbol('n', composite=True, odd=True)
|
| 1957 |
+
assert factorial(n - 1) % n == 0
|
| 1958 |
+
|
| 1959 |
+
# symbolic with known parity
|
| 1960 |
+
n = Symbol('n', even=True)
|
| 1961 |
+
assert Mod(n, 2) == 0
|
| 1962 |
+
n = Symbol('n', odd=True)
|
| 1963 |
+
assert Mod(n, 2) == 1
|
| 1964 |
+
|
| 1965 |
+
# issue 10963
|
| 1966 |
+
assert (x**6000%400).args[1] == 400
|
| 1967 |
+
|
| 1968 |
+
#issue 13543
|
| 1969 |
+
assert Mod(Mod(x + 1, 2) + 1, 2) == Mod(x, 2)
|
| 1970 |
+
|
| 1971 |
+
x1 = Symbol('x1', integer=True)
|
| 1972 |
+
assert Mod(Mod(x1 + 2, 4)*(x1 + 4), 4) == Mod(x1*(x1 + 2), 4)
|
| 1973 |
+
assert Mod(Mod(x1 + 2, 4)*4, 4) == 0
|
| 1974 |
+
|
| 1975 |
+
# issue 15493
|
| 1976 |
+
i, j = symbols('i j', integer=True, positive=True)
|
| 1977 |
+
assert Mod(3*i, 2) == Mod(i, 2)
|
| 1978 |
+
assert Mod(8*i/j, 4) == 4*Mod(2*i/j, 1)
|
| 1979 |
+
assert Mod(8*i, 4) == 0
|
| 1980 |
+
|
| 1981 |
+
# rewrite
|
| 1982 |
+
assert Mod(x, y).rewrite(floor) == x - y*floor(x/y)
|
| 1983 |
+
assert ((x - Mod(x, y))/y).rewrite(floor) == floor(x/y)
|
| 1984 |
+
|
| 1985 |
+
# issue 21373
|
| 1986 |
+
from sympy.functions.elementary.hyperbolic import sinh
|
| 1987 |
+
from sympy.functions.elementary.piecewise import Piecewise
|
| 1988 |
+
|
| 1989 |
+
x_r, y_r = symbols('x_r y_r', real=True)
|
| 1990 |
+
assert (Piecewise((x_r, y_r > x_r), (y_r, True)) / z) % 1
|
| 1991 |
+
expr = exp(sinh(Piecewise((x_r, y_r > x_r), (y_r, True)) / z))
|
| 1992 |
+
expr.subs({1: 1.0})
|
| 1993 |
+
sinh(Piecewise((x_r, y_r > x_r), (y_r, True)) * z ** -1.0).is_zero
|
| 1994 |
+
|
| 1995 |
+
# issue 24215
|
| 1996 |
+
from sympy.abc import phi
|
| 1997 |
+
assert Mod(4.0*Mod(phi, 1) , 2) == 2.0*(Mod(2*(Mod(phi, 1)), 1))
|
| 1998 |
+
|
| 1999 |
+
xi = symbols('x', integer=True)
|
| 2000 |
+
assert unchanged(Mod, xi, 2)
|
| 2001 |
+
assert Mod(3*xi, 2) == Mod(xi, 2)
|
| 2002 |
+
assert unchanged(Mod, 3*x, 2)
|
| 2003 |
+
|
| 2004 |
+
|
| 2005 |
+
def test_Mod_Pow():
|
| 2006 |
+
# modular exponentiation
|
| 2007 |
+
assert isinstance(Mod(Pow(2, 2, evaluate=False), 3), Integer)
|
| 2008 |
+
|
| 2009 |
+
assert Mod(Pow(4, 13, evaluate=False), 497) == Mod(Pow(4, 13), 497)
|
| 2010 |
+
assert Mod(Pow(2, 10000000000, evaluate=False), 3) == 1
|
| 2011 |
+
assert Mod(Pow(32131231232, 9**10**6, evaluate=False),10**12) == \
|
| 2012 |
+
pow(32131231232,9**10**6,10**12)
|
| 2013 |
+
assert Mod(Pow(33284959323, 123**999, evaluate=False),11**13) == \
|
| 2014 |
+
pow(33284959323,123**999,11**13)
|
| 2015 |
+
assert Mod(Pow(78789849597, 333**555, evaluate=False),12**9) == \
|
| 2016 |
+
pow(78789849597,333**555,12**9)
|
| 2017 |
+
|
| 2018 |
+
# modular nested exponentiation
|
| 2019 |
+
expr = Pow(2, 2, evaluate=False)
|
| 2020 |
+
expr = Pow(2, expr, evaluate=False)
|
| 2021 |
+
assert Mod(expr, 3**10) == 16
|
| 2022 |
+
expr = Pow(2, expr, evaluate=False)
|
| 2023 |
+
assert Mod(expr, 3**10) == 6487
|
| 2024 |
+
expr = Pow(2, expr, evaluate=False)
|
| 2025 |
+
assert Mod(expr, 3**10) == 32191
|
| 2026 |
+
expr = Pow(2, expr, evaluate=False)
|
| 2027 |
+
assert Mod(expr, 3**10) == 18016
|
| 2028 |
+
expr = Pow(2, expr, evaluate=False)
|
| 2029 |
+
assert Mod(expr, 3**10) == 5137
|
| 2030 |
+
|
| 2031 |
+
expr = Pow(2, 2, evaluate=False)
|
| 2032 |
+
expr = Pow(expr, 2, evaluate=False)
|
| 2033 |
+
assert Mod(expr, 3**10) == 16
|
| 2034 |
+
expr = Pow(expr, 2, evaluate=False)
|
| 2035 |
+
assert Mod(expr, 3**10) == 256
|
| 2036 |
+
expr = Pow(expr, 2, evaluate=False)
|
| 2037 |
+
assert Mod(expr, 3**10) == 6487
|
| 2038 |
+
expr = Pow(expr, 2, evaluate=False)
|
| 2039 |
+
assert Mod(expr, 3**10) == 38281
|
| 2040 |
+
expr = Pow(expr, 2, evaluate=False)
|
| 2041 |
+
assert Mod(expr, 3**10) == 15928
|
| 2042 |
+
|
| 2043 |
+
expr = Pow(2, 2, evaluate=False)
|
| 2044 |
+
expr = Pow(expr, expr, evaluate=False)
|
| 2045 |
+
assert Mod(expr, 3**10) == 256
|
| 2046 |
+
expr = Pow(expr, expr, evaluate=False)
|
| 2047 |
+
assert Mod(expr, 3**10) == 9229
|
| 2048 |
+
expr = Pow(expr, expr, evaluate=False)
|
| 2049 |
+
assert Mod(expr, 3**10) == 25708
|
| 2050 |
+
expr = Pow(expr, expr, evaluate=False)
|
| 2051 |
+
assert Mod(expr, 3**10) == 26608
|
| 2052 |
+
expr = Pow(expr, expr, evaluate=False)
|
| 2053 |
+
# XXX This used to fail in a nondeterministic way because of overflow
|
| 2054 |
+
# error.
|
| 2055 |
+
assert Mod(expr, 3**10) == 1966
|
| 2056 |
+
|
| 2057 |
+
|
| 2058 |
+
def test_Mod_is_integer():
|
| 2059 |
+
p = Symbol('p', integer=True)
|
| 2060 |
+
q1 = Symbol('q1', integer=True)
|
| 2061 |
+
q2 = Symbol('q2', integer=True, nonzero=True)
|
| 2062 |
+
assert Mod(x, y).is_integer is None
|
| 2063 |
+
assert Mod(p, q1).is_integer is None
|
| 2064 |
+
assert Mod(x, q2).is_integer is None
|
| 2065 |
+
assert Mod(p, q2).is_integer
|
| 2066 |
+
|
| 2067 |
+
|
| 2068 |
+
def test_Mod_is_nonposneg():
|
| 2069 |
+
n = Symbol('n', integer=True)
|
| 2070 |
+
k = Symbol('k', integer=True, positive=True)
|
| 2071 |
+
assert (n%3).is_nonnegative
|
| 2072 |
+
assert Mod(n, -3).is_nonpositive
|
| 2073 |
+
assert Mod(n, k).is_nonnegative
|
| 2074 |
+
assert Mod(n, -k).is_nonpositive
|
| 2075 |
+
assert Mod(k, n).is_nonnegative is None
|
| 2076 |
+
|
| 2077 |
+
|
| 2078 |
+
def test_issue_6001():
|
| 2079 |
+
A = Symbol("A", commutative=False)
|
| 2080 |
+
eq = A + A**2
|
| 2081 |
+
# it doesn't matter whether it's True or False; they should
|
| 2082 |
+
# just all be the same
|
| 2083 |
+
assert (
|
| 2084 |
+
eq.is_commutative ==
|
| 2085 |
+
(eq + 1).is_commutative ==
|
| 2086 |
+
(A + 1).is_commutative)
|
| 2087 |
+
|
| 2088 |
+
B = Symbol("B", commutative=False)
|
| 2089 |
+
# Although commutative terms could cancel we return True
|
| 2090 |
+
# meaning "there are non-commutative symbols; aftersubstitution
|
| 2091 |
+
# that definition can change, e.g. (A*B).subs(B,A**-1) -> 1
|
| 2092 |
+
assert (sqrt(2)*A).is_commutative is False
|
| 2093 |
+
assert (sqrt(2)*A*B).is_commutative is False
|
| 2094 |
+
|
| 2095 |
+
|
| 2096 |
+
def test_polar():
|
| 2097 |
+
from sympy.functions.elementary.complexes import polar_lift
|
| 2098 |
+
p = Symbol('p', polar=True)
|
| 2099 |
+
x = Symbol('x')
|
| 2100 |
+
assert p.is_polar
|
| 2101 |
+
assert x.is_polar is None
|
| 2102 |
+
assert S.One.is_polar is None
|
| 2103 |
+
assert (p**x).is_polar is True
|
| 2104 |
+
assert (x**p).is_polar is None
|
| 2105 |
+
assert ((2*p)**x).is_polar is True
|
| 2106 |
+
assert (2*p).is_polar is True
|
| 2107 |
+
assert (-2*p).is_polar is not True
|
| 2108 |
+
assert (polar_lift(-2)*p).is_polar is True
|
| 2109 |
+
|
| 2110 |
+
q = Symbol('q', polar=True)
|
| 2111 |
+
assert (p*q)**2 == p**2 * q**2
|
| 2112 |
+
assert (2*q)**2 == 4 * q**2
|
| 2113 |
+
assert ((p*q)**x).expand() == p**x * q**x
|
| 2114 |
+
|
| 2115 |
+
|
| 2116 |
+
def test_issue_6040():
|
| 2117 |
+
a, b = Pow(1, 2, evaluate=False), S.One
|
| 2118 |
+
assert a != b
|
| 2119 |
+
assert b != a
|
| 2120 |
+
assert not (a == b)
|
| 2121 |
+
assert not (b == a)
|
| 2122 |
+
|
| 2123 |
+
|
| 2124 |
+
def test_issue_6082():
|
| 2125 |
+
# Comparison is symmetric
|
| 2126 |
+
assert Basic.compare(Max(x, 1), Max(x, 2)) == \
|
| 2127 |
+
- Basic.compare(Max(x, 2), Max(x, 1))
|
| 2128 |
+
# Equal expressions compare equal
|
| 2129 |
+
assert Basic.compare(Max(x, 1), Max(x, 1)) == 0
|
| 2130 |
+
# Basic subtypes (such as Max) compare different than standard types
|
| 2131 |
+
assert Basic.compare(Max(1, x), frozenset((1, x))) != 0
|
| 2132 |
+
|
| 2133 |
+
|
| 2134 |
+
def test_issue_6077():
|
| 2135 |
+
assert x**2.0/x == x**1.0
|
| 2136 |
+
assert x/x**2.0 == x**-1.0
|
| 2137 |
+
assert x*x**2.0 == x**3.0
|
| 2138 |
+
assert x**1.5*x**2.5 == x**4.0
|
| 2139 |
+
|
| 2140 |
+
assert 2**(2.0*x)/2**x == 2**(1.0*x)
|
| 2141 |
+
assert 2**x/2**(2.0*x) == 2**(-1.0*x)
|
| 2142 |
+
assert 2**x*2**(2.0*x) == 2**(3.0*x)
|
| 2143 |
+
assert 2**(1.5*x)*2**(2.5*x) == 2**(4.0*x)
|
| 2144 |
+
|
| 2145 |
+
|
| 2146 |
+
def test_mul_flatten_oo():
|
| 2147 |
+
p = symbols('p', positive=True)
|
| 2148 |
+
n, m = symbols('n,m', negative=True)
|
| 2149 |
+
x_im = symbols('x_im', imaginary=True)
|
| 2150 |
+
assert n*oo is -oo
|
| 2151 |
+
assert n*m*oo is oo
|
| 2152 |
+
assert p*oo is oo
|
| 2153 |
+
assert x_im*oo != I*oo # i could be +/- 3*I -> +/-oo
|
| 2154 |
+
|
| 2155 |
+
|
| 2156 |
+
def test_add_flatten():
|
| 2157 |
+
# see https://github.com/sympy/sympy/issues/2633#issuecomment-29545524
|
| 2158 |
+
a = oo + I*oo
|
| 2159 |
+
b = oo - I*oo
|
| 2160 |
+
assert a + b is nan
|
| 2161 |
+
assert a - b is nan
|
| 2162 |
+
# FIXME: This evaluates as:
|
| 2163 |
+
# >>> 1/a
|
| 2164 |
+
# 0*(oo + oo*I)
|
| 2165 |
+
# which should not simplify to 0. Should be fixed in Pow.eval
|
| 2166 |
+
#assert (1/a).simplify() == (1/b).simplify() == 0
|
| 2167 |
+
|
| 2168 |
+
a = Pow(2, 3, evaluate=False)
|
| 2169 |
+
assert a + a == 16
|
| 2170 |
+
|
| 2171 |
+
|
| 2172 |
+
def test_issue_5160_6087_6089_6090():
|
| 2173 |
+
# issue 6087
|
| 2174 |
+
assert ((-2*x*y**y)**3.2).n(2) == (2**3.2*(-x*y**y)**3.2).n(2)
|
| 2175 |
+
# issue 6089
|
| 2176 |
+
A, B, C = symbols('A,B,C', commutative=False)
|
| 2177 |
+
assert (2.*B*C)**3 == 8.0*(B*C)**3
|
| 2178 |
+
assert (-2.*B*C)**3 == -8.0*(B*C)**3
|
| 2179 |
+
assert (-2*B*C)**2 == 4*(B*C)**2
|
| 2180 |
+
# issue 5160
|
| 2181 |
+
assert sqrt(-1.0*x) == 1.0*sqrt(-x)
|
| 2182 |
+
assert sqrt(1.0*x) == 1.0*sqrt(x)
|
| 2183 |
+
# issue 6090
|
| 2184 |
+
assert (-2*x*y*A*B)**2 == 4*x**2*y**2*(A*B)**2
|
| 2185 |
+
|
| 2186 |
+
|
| 2187 |
+
def test_float_int_round():
|
| 2188 |
+
assert int(float(sqrt(10))) == int(sqrt(10))
|
| 2189 |
+
assert int(pi**1000) % 10 == 2
|
| 2190 |
+
assert int(Float('1.123456789012345678901234567890e20', '')) == \
|
| 2191 |
+
int(112345678901234567890)
|
| 2192 |
+
assert int(Float('1.123456789012345678901234567890e25', '')) == \
|
| 2193 |
+
int(11234567890123456789012345)
|
| 2194 |
+
# decimal forces float so it's not an exact integer ending in 000000
|
| 2195 |
+
assert int(Float('1.123456789012345678901234567890e35', '')) == \
|
| 2196 |
+
112345678901234567890123456789000192
|
| 2197 |
+
assert int(Float('123456789012345678901234567890e5', '')) == \
|
| 2198 |
+
12345678901234567890123456789000000
|
| 2199 |
+
assert Integer(Float('1.123456789012345678901234567890e20', '')) == \
|
| 2200 |
+
112345678901234567890
|
| 2201 |
+
assert Integer(Float('1.123456789012345678901234567890e25', '')) == \
|
| 2202 |
+
11234567890123456789012345
|
| 2203 |
+
# decimal forces float so it's not an exact integer ending in 000000
|
| 2204 |
+
assert Integer(Float('1.123456789012345678901234567890e35', '')) == \
|
| 2205 |
+
112345678901234567890123456789000192
|
| 2206 |
+
assert Integer(Float('123456789012345678901234567890e5', '')) == \
|
| 2207 |
+
12345678901234567890123456789000000
|
| 2208 |
+
assert same_and_same_prec(Float('123000e-2',''), Float('1230.00', ''))
|
| 2209 |
+
assert same_and_same_prec(Float('123000e2',''), Float('12300000', ''))
|
| 2210 |
+
|
| 2211 |
+
assert int(1 + Rational('.9999999999999999999999999')) == 1
|
| 2212 |
+
assert int(pi/1e20) == 0
|
| 2213 |
+
assert int(1 + pi/1e20) == 1
|
| 2214 |
+
assert int(Add(1.2, -2, evaluate=False)) == int(1.2 - 2)
|
| 2215 |
+
assert int(Add(1.2, +2, evaluate=False)) == int(1.2 + 2)
|
| 2216 |
+
assert int(Add(1 + Float('.99999999999999999', ''), evaluate=False)) == 1
|
| 2217 |
+
raises(TypeError, lambda: float(x))
|
| 2218 |
+
raises(TypeError, lambda: float(sqrt(-1)))
|
| 2219 |
+
|
| 2220 |
+
assert int(12345678901234567890 + cos(1)**2 + sin(1)**2) == \
|
| 2221 |
+
12345678901234567891
|
| 2222 |
+
|
| 2223 |
+
|
| 2224 |
+
def test_issue_6611a():
|
| 2225 |
+
assert Mul.flatten([3**Rational(1, 3),
|
| 2226 |
+
Pow(-Rational(1, 9), Rational(2, 3), evaluate=False)]) == \
|
| 2227 |
+
([Rational(1, 3), (-1)**Rational(2, 3)], [], None)
|
| 2228 |
+
|
| 2229 |
+
|
| 2230 |
+
def test_denest_add_mul():
|
| 2231 |
+
# when working with evaluated expressions make sure they denest
|
| 2232 |
+
eq = x + 1
|
| 2233 |
+
eq = Add(eq, 2, evaluate=False)
|
| 2234 |
+
eq = Add(eq, 2, evaluate=False)
|
| 2235 |
+
assert Add(*eq.args) == x + 5
|
| 2236 |
+
eq = x*2
|
| 2237 |
+
eq = Mul(eq, 2, evaluate=False)
|
| 2238 |
+
eq = Mul(eq, 2, evaluate=False)
|
| 2239 |
+
assert Mul(*eq.args) == 8*x
|
| 2240 |
+
# but don't let them denest unnecessarily
|
| 2241 |
+
eq = Mul(-2, x - 2, evaluate=False)
|
| 2242 |
+
assert 2*eq == Mul(-4, x - 2, evaluate=False)
|
| 2243 |
+
assert -eq == Mul(2, x - 2, evaluate=False)
|
| 2244 |
+
|
| 2245 |
+
|
| 2246 |
+
def test_mul_coeff():
|
| 2247 |
+
# It is important that all Numbers be removed from the seq;
|
| 2248 |
+
# This can be tricky when powers combine to produce those numbers
|
| 2249 |
+
p = exp(I*pi/3)
|
| 2250 |
+
assert p**2*x*p*y*p*x*p**2 == x**2*y
|
| 2251 |
+
|
| 2252 |
+
|
| 2253 |
+
def test_mul_zero_detection():
|
| 2254 |
+
nz = Dummy(real=True, zero=False)
|
| 2255 |
+
r = Dummy(extended_real=True)
|
| 2256 |
+
c = Dummy(real=False, complex=True)
|
| 2257 |
+
c2 = Dummy(real=False, complex=True)
|
| 2258 |
+
i = Dummy(imaginary=True)
|
| 2259 |
+
e = nz*r*c
|
| 2260 |
+
assert e.is_imaginary is None
|
| 2261 |
+
assert e.is_extended_real is None
|
| 2262 |
+
e = nz*c
|
| 2263 |
+
assert e.is_imaginary is None
|
| 2264 |
+
assert e.is_extended_real is False
|
| 2265 |
+
e = nz*i*c
|
| 2266 |
+
assert e.is_imaginary is False
|
| 2267 |
+
assert e.is_extended_real is None
|
| 2268 |
+
# check for more than one complex; it is important to use
|
| 2269 |
+
# uniquely named Symbols to ensure that two factors appear
|
| 2270 |
+
# e.g. if the symbols have the same name they just become
|
| 2271 |
+
# a single factor, a power.
|
| 2272 |
+
e = nz*i*c*c2
|
| 2273 |
+
assert e.is_imaginary is None
|
| 2274 |
+
assert e.is_extended_real is None
|
| 2275 |
+
|
| 2276 |
+
# _eval_is_extended_real and _eval_is_zero both employ trapping of the
|
| 2277 |
+
# zero value so args should be tested in both directions and
|
| 2278 |
+
# TO AVOID GETTING THE CACHED RESULT, Dummy MUST BE USED
|
| 2279 |
+
|
| 2280 |
+
# real is unknown
|
| 2281 |
+
def test(z, b, e):
|
| 2282 |
+
if z.is_zero and b.is_finite:
|
| 2283 |
+
assert e.is_extended_real and e.is_zero
|
| 2284 |
+
else:
|
| 2285 |
+
assert e.is_extended_real is None
|
| 2286 |
+
if b.is_finite:
|
| 2287 |
+
if z.is_zero:
|
| 2288 |
+
assert e.is_zero
|
| 2289 |
+
else:
|
| 2290 |
+
assert e.is_zero is None
|
| 2291 |
+
elif b.is_finite is False:
|
| 2292 |
+
if z.is_zero is None:
|
| 2293 |
+
assert e.is_zero is None
|
| 2294 |
+
else:
|
| 2295 |
+
assert e.is_zero is False
|
| 2296 |
+
|
| 2297 |
+
|
| 2298 |
+
for iz, ib in product(*[[True, False, None]]*2):
|
| 2299 |
+
z = Dummy('z', nonzero=iz)
|
| 2300 |
+
b = Dummy('f', finite=ib)
|
| 2301 |
+
e = Mul(z, b, evaluate=False)
|
| 2302 |
+
test(z, b, e)
|
| 2303 |
+
z = Dummy('nz', nonzero=iz)
|
| 2304 |
+
b = Dummy('f', finite=ib)
|
| 2305 |
+
e = Mul(b, z, evaluate=False)
|
| 2306 |
+
test(z, b, e)
|
| 2307 |
+
|
| 2308 |
+
# real is True
|
| 2309 |
+
def test(z, b, e):
|
| 2310 |
+
if z.is_zero and not b.is_finite:
|
| 2311 |
+
assert e.is_extended_real is None
|
| 2312 |
+
else:
|
| 2313 |
+
assert e.is_extended_real is True
|
| 2314 |
+
|
| 2315 |
+
for iz, ib in product(*[[True, False, None]]*2):
|
| 2316 |
+
z = Dummy('z', nonzero=iz, extended_real=True)
|
| 2317 |
+
b = Dummy('b', finite=ib, extended_real=True)
|
| 2318 |
+
e = Mul(z, b, evaluate=False)
|
| 2319 |
+
test(z, b, e)
|
| 2320 |
+
z = Dummy('z', nonzero=iz, extended_real=True)
|
| 2321 |
+
b = Dummy('b', finite=ib, extended_real=True)
|
| 2322 |
+
e = Mul(b, z, evaluate=False)
|
| 2323 |
+
test(z, b, e)
|
| 2324 |
+
|
| 2325 |
+
|
| 2326 |
+
def test_Mul_with_zero_infinite():
|
| 2327 |
+
zer = Dummy(zero=True)
|
| 2328 |
+
inf = Dummy(finite=False)
|
| 2329 |
+
|
| 2330 |
+
e = Mul(zer, inf, evaluate=False)
|
| 2331 |
+
assert e.is_extended_positive is None
|
| 2332 |
+
assert e.is_hermitian is None
|
| 2333 |
+
|
| 2334 |
+
e = Mul(inf, zer, evaluate=False)
|
| 2335 |
+
assert e.is_extended_positive is None
|
| 2336 |
+
assert e.is_hermitian is None
|
| 2337 |
+
|
| 2338 |
+
|
| 2339 |
+
def test_Mul_does_not_cancel_infinities():
|
| 2340 |
+
a, b = symbols('a b')
|
| 2341 |
+
assert ((zoo + 3*a)/(3*a + zoo)) is nan
|
| 2342 |
+
assert ((b - oo)/(b - oo)) is nan
|
| 2343 |
+
# issue 13904
|
| 2344 |
+
expr = (1/(a+b) + 1/(a-b))/(1/(a+b) - 1/(a-b))
|
| 2345 |
+
assert expr.subs(b, a) is nan
|
| 2346 |
+
|
| 2347 |
+
|
| 2348 |
+
def test_Mul_does_not_distribute_infinity():
|
| 2349 |
+
a, b = symbols('a b')
|
| 2350 |
+
assert ((1 + I)*oo).is_Mul
|
| 2351 |
+
assert ((a + b)*(-oo)).is_Mul
|
| 2352 |
+
assert ((a + 1)*zoo).is_Mul
|
| 2353 |
+
assert ((1 + I)*oo).is_finite is False
|
| 2354 |
+
z = (1 + I)*oo
|
| 2355 |
+
assert ((1 - I)*z).expand() is oo
|
| 2356 |
+
|
| 2357 |
+
|
| 2358 |
+
def test_issue_8247_8354():
|
| 2359 |
+
from sympy.functions.elementary.trigonometric import tan
|
| 2360 |
+
z = sqrt(1 + sqrt(3)) + sqrt(3 + 3*sqrt(3)) - sqrt(10 + 6*sqrt(3))
|
| 2361 |
+
assert z.is_positive is False # it's 0
|
| 2362 |
+
z = S('''-2**(1/3)*(3*sqrt(93) + 29)**2 - 4*(3*sqrt(93) + 29)**(4/3) +
|
| 2363 |
+
12*sqrt(93)*(3*sqrt(93) + 29)**(1/3) + 116*(3*sqrt(93) + 29)**(1/3) +
|
| 2364 |
+
174*2**(1/3)*sqrt(93) + 1678*2**(1/3)''')
|
| 2365 |
+
assert z.is_positive is False # it's 0
|
| 2366 |
+
z = 2*(-3*tan(19*pi/90) + sqrt(3))*cos(11*pi/90)*cos(19*pi/90) - \
|
| 2367 |
+
sqrt(3)*(-3 + 4*cos(19*pi/90)**2)
|
| 2368 |
+
assert z.is_positive is not True # it's zero and it shouldn't hang
|
| 2369 |
+
z = S('''9*(3*sqrt(93) + 29)**(2/3)*((3*sqrt(93) +
|
| 2370 |
+
29)**(1/3)*(-2**(2/3)*(3*sqrt(93) + 29)**(1/3) - 2) - 2*2**(1/3))**3 +
|
| 2371 |
+
72*(3*sqrt(93) + 29)**(2/3)*(81*sqrt(93) + 783) + (162*sqrt(93) +
|
| 2372 |
+
1566)*((3*sqrt(93) + 29)**(1/3)*(-2**(2/3)*(3*sqrt(93) + 29)**(1/3) -
|
| 2373 |
+
2) - 2*2**(1/3))**2''')
|
| 2374 |
+
assert z.is_positive is False # it's 0 (and a single _mexpand isn't enough)
|
| 2375 |
+
|
| 2376 |
+
|
| 2377 |
+
def test_Add_is_zero():
|
| 2378 |
+
x, y = symbols('x y', zero=True)
|
| 2379 |
+
assert (x + y).is_zero
|
| 2380 |
+
|
| 2381 |
+
# Issue 15873
|
| 2382 |
+
e = -2*I + (1 + I)**2
|
| 2383 |
+
assert e.is_zero is None
|
| 2384 |
+
|
| 2385 |
+
|
| 2386 |
+
def test_issue_14392():
|
| 2387 |
+
assert (sin(zoo)**2).as_real_imag() == (nan, nan)
|
| 2388 |
+
|
| 2389 |
+
|
| 2390 |
+
def test_divmod():
|
| 2391 |
+
assert divmod(x, y) == (x//y, x % y)
|
| 2392 |
+
assert divmod(x, 3) == (x//3, x % 3)
|
| 2393 |
+
assert divmod(3, x) == (3//x, 3 % x)
|
| 2394 |
+
|
| 2395 |
+
|
| 2396 |
+
def test__neg__():
|
| 2397 |
+
assert -(x*y) == -x*y
|
| 2398 |
+
assert -(-x*y) == x*y
|
| 2399 |
+
assert -(1.*x) == -1.*x
|
| 2400 |
+
assert -(-1.*x) == 1.*x
|
| 2401 |
+
assert -(2.*x) == -2.*x
|
| 2402 |
+
assert -(-2.*x) == 2.*x
|
| 2403 |
+
with distribute(False):
|
| 2404 |
+
eq = -(x + y)
|
| 2405 |
+
assert eq.is_Mul and eq.args == (-1, x + y)
|
| 2406 |
+
with evaluate(False):
|
| 2407 |
+
eq = -(x + y)
|
| 2408 |
+
assert eq.is_Mul and eq.args == (-1, x + y)
|
| 2409 |
+
|
| 2410 |
+
|
| 2411 |
+
def test_issue_18507():
|
| 2412 |
+
assert Mul(zoo, zoo, 0) is nan
|
| 2413 |
+
|
| 2414 |
+
|
| 2415 |
+
def test_issue_17130():
|
| 2416 |
+
e = Add(b, -b, I, -I, evaluate=False)
|
| 2417 |
+
assert e.is_zero is None # ideally this would be True
|
| 2418 |
+
|
| 2419 |
+
|
| 2420 |
+
def test_issue_21034():
|
| 2421 |
+
e = -I*log((re(asin(5)) + I*im(asin(5)))/sqrt(re(asin(5))**2 + im(asin(5))**2))/pi
|
| 2422 |
+
assert e.round(2)
|
| 2423 |
+
|
| 2424 |
+
|
| 2425 |
+
def test_issue_22021():
|
| 2426 |
+
from sympy.calculus.accumulationbounds import AccumBounds
|
| 2427 |
+
# these objects are special cases in Mul
|
| 2428 |
+
from sympy.tensor.tensor import TensorIndexType, tensor_indices, tensor_heads
|
| 2429 |
+
L = TensorIndexType("L")
|
| 2430 |
+
i = tensor_indices("i", L)
|
| 2431 |
+
A, B = tensor_heads("A B", [L])
|
| 2432 |
+
e = A(i) + B(i)
|
| 2433 |
+
assert -e == -1*e
|
| 2434 |
+
e = zoo + x
|
| 2435 |
+
assert -e == -1*e
|
| 2436 |
+
a = AccumBounds(1, 2)
|
| 2437 |
+
e = a + x
|
| 2438 |
+
assert -e == -1*e
|
| 2439 |
+
for args in permutations((zoo, a, x)):
|
| 2440 |
+
e = Add(*args, evaluate=False)
|
| 2441 |
+
assert -e == -1*e
|
| 2442 |
+
assert 2*Add(1, x, x, evaluate=False) == 4*x + 2
|
| 2443 |
+
|
| 2444 |
+
|
| 2445 |
+
def test_issue_22244():
|
| 2446 |
+
assert -(zoo*x) == zoo*x
|
| 2447 |
+
|
| 2448 |
+
|
| 2449 |
+
def test_issue_22453():
|
| 2450 |
+
from sympy.utilities.iterables import cartes
|
| 2451 |
+
e = Symbol('e', extended_positive=True)
|
| 2452 |
+
for a, b in cartes(*[[oo, -oo, 3]]*2):
|
| 2453 |
+
if a == b == 3:
|
| 2454 |
+
continue
|
| 2455 |
+
i = a + I*b
|
| 2456 |
+
assert i**(1 + e) is S.ComplexInfinity
|
| 2457 |
+
assert i**-e is S.Zero
|
| 2458 |
+
assert unchanged(Pow, i, e)
|
| 2459 |
+
assert 1/(oo + I*oo) is S.Zero
|
| 2460 |
+
r, i = [Dummy(infinite=True, extended_real=True) for _ in range(2)]
|
| 2461 |
+
assert 1/(r + I*i) is S.Zero
|
| 2462 |
+
assert 1/(3 + I*i) is S.Zero
|
| 2463 |
+
assert 1/(r + I*3) is S.Zero
|
| 2464 |
+
|
| 2465 |
+
|
| 2466 |
+
def test_issue_22613():
|
| 2467 |
+
assert (0**(x - 2)).as_content_primitive() == (1, 0**(x - 2))
|
| 2468 |
+
assert (0**(x + 2)).as_content_primitive() == (1, 0**(x + 2))
|
| 2469 |
+
|
| 2470 |
+
|
| 2471 |
+
def test_issue_25176():
|
| 2472 |
+
assert sqrt(-4*3**(S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.factorials import factorial
|
| 4 |
+
from sympy.functions.elementary.exponential import (exp, log)
|
| 5 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 6 |
+
from sympy.functions.elementary.trigonometric import (asin, sin)
|
| 7 |
+
from sympy.simplify.simplify import simplify
|
| 8 |
+
from sympy.core import Symbol, S, Rational, Integer, Dummy, Wild, Pow
|
| 9 |
+
from sympy.core.assumptions import (assumptions, check_assumptions,
|
| 10 |
+
failing_assumptions, common_assumptions, _generate_assumption_rules,
|
| 11 |
+
_load_pre_generated_assumption_rules)
|
| 12 |
+
from sympy.core.facts import InconsistentAssumptions
|
| 13 |
+
from sympy.core.random import seed
|
| 14 |
+
from sympy.combinatorics import Permutation
|
| 15 |
+
from sympy.combinatorics.perm_groups import PermutationGroup
|
| 16 |
+
|
| 17 |
+
from sympy.testing.pytest import raises, XFAIL
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def test_symbol_unset():
|
| 21 |
+
x = Symbol('x', real=True, integer=True)
|
| 22 |
+
assert x.is_real is True
|
| 23 |
+
assert x.is_integer is True
|
| 24 |
+
assert x.is_imaginary is False
|
| 25 |
+
assert x.is_noninteger is False
|
| 26 |
+
assert x.is_number is False
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def test_zero():
|
| 30 |
+
z = Integer(0)
|
| 31 |
+
assert z.is_commutative is True
|
| 32 |
+
assert z.is_integer is True
|
| 33 |
+
assert z.is_rational is True
|
| 34 |
+
assert z.is_algebraic is True
|
| 35 |
+
assert z.is_transcendental is False
|
| 36 |
+
assert z.is_real is True
|
| 37 |
+
assert z.is_complex is True
|
| 38 |
+
assert z.is_noninteger is False
|
| 39 |
+
assert z.is_irrational is False
|
| 40 |
+
assert z.is_imaginary is False
|
| 41 |
+
assert z.is_positive is False
|
| 42 |
+
assert z.is_negative is False
|
| 43 |
+
assert z.is_nonpositive is True
|
| 44 |
+
assert z.is_nonnegative is True
|
| 45 |
+
assert z.is_even is True
|
| 46 |
+
assert z.is_odd is False
|
| 47 |
+
assert z.is_finite is True
|
| 48 |
+
assert z.is_infinite is False
|
| 49 |
+
assert z.is_comparable is True
|
| 50 |
+
assert z.is_prime is False
|
| 51 |
+
assert z.is_composite is False
|
| 52 |
+
assert z.is_number is True
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def test_one():
|
| 56 |
+
z = Integer(1)
|
| 57 |
+
assert z.is_commutative is True
|
| 58 |
+
assert z.is_integer is True
|
| 59 |
+
assert z.is_rational is True
|
| 60 |
+
assert z.is_algebraic is True
|
| 61 |
+
assert z.is_transcendental is False
|
| 62 |
+
assert z.is_real is True
|
| 63 |
+
assert z.is_complex is True
|
| 64 |
+
assert z.is_noninteger is False
|
| 65 |
+
assert z.is_irrational is False
|
| 66 |
+
assert z.is_imaginary is False
|
| 67 |
+
assert z.is_positive is True
|
| 68 |
+
assert z.is_negative is False
|
| 69 |
+
assert z.is_nonpositive is False
|
| 70 |
+
assert z.is_nonnegative is True
|
| 71 |
+
assert z.is_even is False
|
| 72 |
+
assert z.is_odd is True
|
| 73 |
+
assert z.is_finite is True
|
| 74 |
+
assert z.is_infinite is False
|
| 75 |
+
assert z.is_comparable is True
|
| 76 |
+
assert z.is_prime is False
|
| 77 |
+
assert z.is_number is True
|
| 78 |
+
assert z.is_composite is False # issue 8807
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def test_negativeone():
|
| 82 |
+
z = Integer(-1)
|
| 83 |
+
assert z.is_commutative is True
|
| 84 |
+
assert z.is_integer is True
|
| 85 |
+
assert z.is_rational is True
|
| 86 |
+
assert z.is_algebraic is True
|
| 87 |
+
assert z.is_transcendental is False
|
| 88 |
+
assert z.is_real is True
|
| 89 |
+
assert z.is_complex is True
|
| 90 |
+
assert z.is_noninteger is False
|
| 91 |
+
assert z.is_irrational is False
|
| 92 |
+
assert z.is_imaginary is False
|
| 93 |
+
assert z.is_positive is False
|
| 94 |
+
assert z.is_negative is True
|
| 95 |
+
assert z.is_nonpositive is True
|
| 96 |
+
assert z.is_nonnegative is False
|
| 97 |
+
assert z.is_even is False
|
| 98 |
+
assert z.is_odd is True
|
| 99 |
+
assert z.is_finite is True
|
| 100 |
+
assert z.is_infinite is False
|
| 101 |
+
assert z.is_comparable is True
|
| 102 |
+
assert z.is_prime is False
|
| 103 |
+
assert z.is_composite is False
|
| 104 |
+
assert z.is_number is True
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def test_infinity():
|
| 108 |
+
oo = S.Infinity
|
| 109 |
+
|
| 110 |
+
assert oo.is_commutative is True
|
| 111 |
+
assert oo.is_integer is False
|
| 112 |
+
assert oo.is_rational is False
|
| 113 |
+
assert oo.is_algebraic is False
|
| 114 |
+
assert oo.is_transcendental is False
|
| 115 |
+
assert oo.is_extended_real is True
|
| 116 |
+
assert oo.is_real is False
|
| 117 |
+
assert oo.is_complex is False
|
| 118 |
+
assert oo.is_noninteger is True
|
| 119 |
+
assert oo.is_irrational is False
|
| 120 |
+
assert oo.is_imaginary is False
|
| 121 |
+
assert oo.is_nonzero is False
|
| 122 |
+
assert oo.is_positive is False
|
| 123 |
+
assert oo.is_negative is False
|
| 124 |
+
assert oo.is_nonpositive is False
|
| 125 |
+
assert oo.is_nonnegative is False
|
| 126 |
+
assert oo.is_extended_nonzero is True
|
| 127 |
+
assert oo.is_extended_positive is True
|
| 128 |
+
assert oo.is_extended_negative is False
|
| 129 |
+
assert oo.is_extended_nonpositive is False
|
| 130 |
+
assert oo.is_extended_nonnegative is True
|
| 131 |
+
assert oo.is_even is False
|
| 132 |
+
assert oo.is_odd is False
|
| 133 |
+
assert oo.is_finite is False
|
| 134 |
+
assert oo.is_infinite is True
|
| 135 |
+
assert oo.is_comparable is True
|
| 136 |
+
assert oo.is_prime is False
|
| 137 |
+
assert oo.is_composite is False
|
| 138 |
+
assert oo.is_number is True
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def test_neg_infinity():
|
| 142 |
+
mm = S.NegativeInfinity
|
| 143 |
+
|
| 144 |
+
assert mm.is_commutative is True
|
| 145 |
+
assert mm.is_integer is False
|
| 146 |
+
assert mm.is_rational is False
|
| 147 |
+
assert mm.is_algebraic is False
|
| 148 |
+
assert mm.is_transcendental is False
|
| 149 |
+
assert mm.is_extended_real is True
|
| 150 |
+
assert mm.is_real is False
|
| 151 |
+
assert mm.is_complex is False
|
| 152 |
+
assert mm.is_noninteger is True
|
| 153 |
+
assert mm.is_irrational is False
|
| 154 |
+
assert mm.is_imaginary is False
|
| 155 |
+
assert mm.is_nonzero is False
|
| 156 |
+
assert mm.is_positive is False
|
| 157 |
+
assert mm.is_negative is False
|
| 158 |
+
assert mm.is_nonpositive is False
|
| 159 |
+
assert mm.is_nonnegative is False
|
| 160 |
+
assert mm.is_extended_nonzero is True
|
| 161 |
+
assert mm.is_extended_positive is False
|
| 162 |
+
assert mm.is_extended_negative is True
|
| 163 |
+
assert mm.is_extended_nonpositive is True
|
| 164 |
+
assert mm.is_extended_nonnegative is False
|
| 165 |
+
assert mm.is_even is False
|
| 166 |
+
assert mm.is_odd is False
|
| 167 |
+
assert mm.is_finite is False
|
| 168 |
+
assert mm.is_infinite is True
|
| 169 |
+
assert mm.is_comparable is True
|
| 170 |
+
assert mm.is_prime is False
|
| 171 |
+
assert mm.is_composite is False
|
| 172 |
+
assert mm.is_number is True
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
def test_zoo():
|
| 176 |
+
zoo = S.ComplexInfinity
|
| 177 |
+
assert zoo.is_complex is False
|
| 178 |
+
assert zoo.is_real is False
|
| 179 |
+
assert zoo.is_prime is False
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
def test_nan():
|
| 183 |
+
nan = S.NaN
|
| 184 |
+
|
| 185 |
+
assert nan.is_commutative is True
|
| 186 |
+
assert nan.is_integer is None
|
| 187 |
+
assert nan.is_rational is None
|
| 188 |
+
assert nan.is_algebraic is None
|
| 189 |
+
assert nan.is_transcendental is None
|
| 190 |
+
assert nan.is_real is None
|
| 191 |
+
assert nan.is_complex is None
|
| 192 |
+
assert nan.is_noninteger is None
|
| 193 |
+
assert nan.is_irrational is None
|
| 194 |
+
assert nan.is_imaginary is None
|
| 195 |
+
assert nan.is_positive is None
|
| 196 |
+
assert nan.is_negative is None
|
| 197 |
+
assert nan.is_nonpositive is None
|
| 198 |
+
assert nan.is_nonnegative is None
|
| 199 |
+
assert nan.is_even is None
|
| 200 |
+
assert nan.is_odd is None
|
| 201 |
+
assert nan.is_finite is None
|
| 202 |
+
assert nan.is_infinite is None
|
| 203 |
+
assert nan.is_comparable is False
|
| 204 |
+
assert nan.is_prime is None
|
| 205 |
+
assert nan.is_composite is None
|
| 206 |
+
assert nan.is_number is True
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
def test_pos_rational():
|
| 210 |
+
r = Rational(3, 4)
|
| 211 |
+
assert r.is_commutative is True
|
| 212 |
+
assert r.is_integer is False
|
| 213 |
+
assert r.is_rational is True
|
| 214 |
+
assert r.is_algebraic is True
|
| 215 |
+
assert r.is_transcendental is False
|
| 216 |
+
assert r.is_real is True
|
| 217 |
+
assert r.is_complex is True
|
| 218 |
+
assert r.is_noninteger is True
|
| 219 |
+
assert r.is_irrational is False
|
| 220 |
+
assert r.is_imaginary is False
|
| 221 |
+
assert r.is_positive is True
|
| 222 |
+
assert r.is_negative is False
|
| 223 |
+
assert r.is_nonpositive is False
|
| 224 |
+
assert r.is_nonnegative is True
|
| 225 |
+
assert r.is_even is False
|
| 226 |
+
assert r.is_odd is False
|
| 227 |
+
assert r.is_finite is True
|
| 228 |
+
assert r.is_infinite is False
|
| 229 |
+
assert r.is_comparable is True
|
| 230 |
+
assert r.is_prime is False
|
| 231 |
+
assert r.is_composite is False
|
| 232 |
+
|
| 233 |
+
r = Rational(1, 4)
|
| 234 |
+
assert r.is_nonpositive is False
|
| 235 |
+
assert r.is_positive is True
|
| 236 |
+
assert r.is_negative is False
|
| 237 |
+
assert r.is_nonnegative is True
|
| 238 |
+
r = Rational(5, 4)
|
| 239 |
+
assert r.is_negative is False
|
| 240 |
+
assert r.is_positive is True
|
| 241 |
+
assert r.is_nonpositive is False
|
| 242 |
+
assert r.is_nonnegative is True
|
| 243 |
+
r = Rational(5, 3)
|
| 244 |
+
assert r.is_nonnegative is True
|
| 245 |
+
assert r.is_positive is True
|
| 246 |
+
assert r.is_negative is False
|
| 247 |
+
assert r.is_nonpositive is False
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
def test_neg_rational():
|
| 251 |
+
r = Rational(-3, 4)
|
| 252 |
+
assert r.is_positive is False
|
| 253 |
+
assert r.is_nonpositive is True
|
| 254 |
+
assert r.is_negative is True
|
| 255 |
+
assert r.is_nonnegative is False
|
| 256 |
+
r = Rational(-1, 4)
|
| 257 |
+
assert r.is_nonpositive is True
|
| 258 |
+
assert r.is_positive is False
|
| 259 |
+
assert r.is_negative is True
|
| 260 |
+
assert r.is_nonnegative is False
|
| 261 |
+
r = Rational(-5, 4)
|
| 262 |
+
assert r.is_negative is True
|
| 263 |
+
assert r.is_positive is False
|
| 264 |
+
assert r.is_nonpositive is True
|
| 265 |
+
assert r.is_nonnegative is False
|
| 266 |
+
r = Rational(-5, 3)
|
| 267 |
+
assert r.is_nonnegative is False
|
| 268 |
+
assert r.is_positive is False
|
| 269 |
+
assert r.is_negative is True
|
| 270 |
+
assert r.is_nonpositive is True
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
def test_pi():
|
| 274 |
+
z = S.Pi
|
| 275 |
+
assert z.is_commutative is True
|
| 276 |
+
assert z.is_integer is False
|
| 277 |
+
assert z.is_rational is False
|
| 278 |
+
assert z.is_algebraic is False
|
| 279 |
+
assert z.is_transcendental is True
|
| 280 |
+
assert z.is_real is True
|
| 281 |
+
assert z.is_complex is True
|
| 282 |
+
assert z.is_noninteger is True
|
| 283 |
+
assert z.is_irrational is True
|
| 284 |
+
assert z.is_imaginary is False
|
| 285 |
+
assert z.is_positive is True
|
| 286 |
+
assert z.is_negative is False
|
| 287 |
+
assert z.is_nonpositive is False
|
| 288 |
+
assert z.is_nonnegative is True
|
| 289 |
+
assert z.is_even is False
|
| 290 |
+
assert z.is_odd is False
|
| 291 |
+
assert z.is_finite is True
|
| 292 |
+
assert z.is_infinite is False
|
| 293 |
+
assert z.is_comparable is True
|
| 294 |
+
assert z.is_prime is False
|
| 295 |
+
assert z.is_composite is False
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
def test_E():
|
| 299 |
+
z = S.Exp1
|
| 300 |
+
assert z.is_commutative is True
|
| 301 |
+
assert z.is_integer is False
|
| 302 |
+
assert z.is_rational is False
|
| 303 |
+
assert z.is_algebraic is False
|
| 304 |
+
assert z.is_transcendental is True
|
| 305 |
+
assert z.is_real is True
|
| 306 |
+
assert z.is_complex is True
|
| 307 |
+
assert z.is_noninteger is True
|
| 308 |
+
assert z.is_irrational is True
|
| 309 |
+
assert z.is_imaginary is False
|
| 310 |
+
assert z.is_positive is True
|
| 311 |
+
assert z.is_negative is False
|
| 312 |
+
assert z.is_nonpositive is False
|
| 313 |
+
assert z.is_nonnegative is True
|
| 314 |
+
assert z.is_even is False
|
| 315 |
+
assert z.is_odd is False
|
| 316 |
+
assert z.is_finite is True
|
| 317 |
+
assert z.is_infinite is False
|
| 318 |
+
assert z.is_comparable is True
|
| 319 |
+
assert z.is_prime is False
|
| 320 |
+
assert z.is_composite is False
|
| 321 |
+
|
| 322 |
+
|
| 323 |
+
def test_I():
|
| 324 |
+
z = S.ImaginaryUnit
|
| 325 |
+
assert z.is_commutative is True
|
| 326 |
+
assert z.is_integer is False
|
| 327 |
+
assert z.is_rational is False
|
| 328 |
+
assert z.is_algebraic is True
|
| 329 |
+
assert z.is_transcendental is False
|
| 330 |
+
assert z.is_real is False
|
| 331 |
+
assert z.is_complex is True
|
| 332 |
+
assert z.is_noninteger is False
|
| 333 |
+
assert z.is_irrational is False
|
| 334 |
+
assert z.is_imaginary is True
|
| 335 |
+
assert z.is_positive is False
|
| 336 |
+
assert z.is_negative is False
|
| 337 |
+
assert z.is_nonpositive is False
|
| 338 |
+
assert z.is_nonnegative is False
|
| 339 |
+
assert z.is_even is False
|
| 340 |
+
assert z.is_odd is False
|
| 341 |
+
assert z.is_finite is True
|
| 342 |
+
assert z.is_infinite is False
|
| 343 |
+
assert z.is_comparable is False
|
| 344 |
+
assert z.is_prime is False
|
| 345 |
+
assert z.is_composite is False
|
| 346 |
+
|
| 347 |
+
|
| 348 |
+
def test_symbol_real_false():
|
| 349 |
+
# issue 3848
|
| 350 |
+
a = Symbol('a', real=False)
|
| 351 |
+
|
| 352 |
+
assert a.is_real is False
|
| 353 |
+
assert a.is_integer is False
|
| 354 |
+
assert a.is_zero is False
|
| 355 |
+
|
| 356 |
+
assert a.is_negative is False
|
| 357 |
+
assert a.is_positive is False
|
| 358 |
+
assert a.is_nonnegative is False
|
| 359 |
+
assert a.is_nonpositive is False
|
| 360 |
+
assert a.is_nonzero is False
|
| 361 |
+
|
| 362 |
+
assert a.is_extended_negative is None
|
| 363 |
+
assert a.is_extended_positive is None
|
| 364 |
+
assert a.is_extended_nonnegative is None
|
| 365 |
+
assert a.is_extended_nonpositive is None
|
| 366 |
+
assert a.is_extended_nonzero is None
|
| 367 |
+
|
| 368 |
+
|
| 369 |
+
def test_symbol_extended_real_false():
|
| 370 |
+
# issue 3848
|
| 371 |
+
a = Symbol('a', extended_real=False)
|
| 372 |
+
|
| 373 |
+
assert a.is_real is False
|
| 374 |
+
assert a.is_integer is False
|
| 375 |
+
assert a.is_zero is False
|
| 376 |
+
|
| 377 |
+
assert a.is_negative is False
|
| 378 |
+
assert a.is_positive is False
|
| 379 |
+
assert a.is_nonnegative is False
|
| 380 |
+
assert a.is_nonpositive is False
|
| 381 |
+
assert a.is_nonzero is False
|
| 382 |
+
|
| 383 |
+
assert a.is_extended_negative is False
|
| 384 |
+
assert a.is_extended_positive is False
|
| 385 |
+
assert a.is_extended_nonnegative is False
|
| 386 |
+
assert a.is_extended_nonpositive is False
|
| 387 |
+
assert a.is_extended_nonzero is False
|
| 388 |
+
|
| 389 |
+
|
| 390 |
+
def test_symbol_imaginary():
|
| 391 |
+
a = Symbol('a', imaginary=True)
|
| 392 |
+
|
| 393 |
+
assert a.is_real is False
|
| 394 |
+
assert a.is_integer is False
|
| 395 |
+
assert a.is_negative is False
|
| 396 |
+
assert a.is_positive is False
|
| 397 |
+
assert a.is_nonnegative is False
|
| 398 |
+
assert a.is_nonpositive is False
|
| 399 |
+
assert a.is_zero is False
|
| 400 |
+
assert a.is_nonzero is False # since nonzero -> real
|
| 401 |
+
|
| 402 |
+
|
| 403 |
+
def test_symbol_zero():
|
| 404 |
+
x = Symbol('x', zero=True)
|
| 405 |
+
assert x.is_positive is False
|
| 406 |
+
assert x.is_nonpositive
|
| 407 |
+
assert x.is_negative is False
|
| 408 |
+
assert x.is_nonnegative
|
| 409 |
+
assert x.is_zero is True
|
| 410 |
+
# TODO Change to x.is_nonzero is None
|
| 411 |
+
# See https://github.com/sympy/sympy/pull/9583
|
| 412 |
+
assert x.is_nonzero is False
|
| 413 |
+
assert x.is_finite is True
|
| 414 |
+
|
| 415 |
+
|
| 416 |
+
def test_symbol_positive():
|
| 417 |
+
x = Symbol('x', positive=True)
|
| 418 |
+
assert x.is_positive is True
|
| 419 |
+
assert x.is_nonpositive is False
|
| 420 |
+
assert x.is_negative is False
|
| 421 |
+
assert x.is_nonnegative is True
|
| 422 |
+
assert x.is_zero is False
|
| 423 |
+
assert x.is_nonzero is True
|
| 424 |
+
|
| 425 |
+
|
| 426 |
+
def test_neg_symbol_positive():
|
| 427 |
+
x = -Symbol('x', positive=True)
|
| 428 |
+
assert x.is_positive is False
|
| 429 |
+
assert x.is_nonpositive is True
|
| 430 |
+
assert x.is_negative is True
|
| 431 |
+
assert x.is_nonnegative is False
|
| 432 |
+
assert x.is_zero is False
|
| 433 |
+
assert x.is_nonzero is True
|
| 434 |
+
|
| 435 |
+
|
| 436 |
+
def test_symbol_nonpositive():
|
| 437 |
+
x = Symbol('x', nonpositive=True)
|
| 438 |
+
assert x.is_positive is False
|
| 439 |
+
assert x.is_nonpositive is True
|
| 440 |
+
assert x.is_negative is None
|
| 441 |
+
assert x.is_nonnegative is None
|
| 442 |
+
assert x.is_zero is None
|
| 443 |
+
assert x.is_nonzero is None
|
| 444 |
+
|
| 445 |
+
|
| 446 |
+
def test_neg_symbol_nonpositive():
|
| 447 |
+
x = -Symbol('x', nonpositive=True)
|
| 448 |
+
assert x.is_positive is None
|
| 449 |
+
assert x.is_nonpositive is None
|
| 450 |
+
assert x.is_negative is False
|
| 451 |
+
assert x.is_nonnegative is True
|
| 452 |
+
assert x.is_zero is None
|
| 453 |
+
assert x.is_nonzero is None
|
| 454 |
+
|
| 455 |
+
|
| 456 |
+
def test_symbol_falsepositive():
|
| 457 |
+
x = Symbol('x', positive=False)
|
| 458 |
+
assert x.is_positive is False
|
| 459 |
+
assert x.is_nonpositive is None
|
| 460 |
+
assert x.is_negative is None
|
| 461 |
+
assert x.is_nonnegative is None
|
| 462 |
+
assert x.is_zero is None
|
| 463 |
+
assert x.is_nonzero is None
|
| 464 |
+
|
| 465 |
+
|
| 466 |
+
def test_symbol_falsepositive_mul():
|
| 467 |
+
# To test pull request 9379
|
| 468 |
+
# Explicit handling of arg.is_positive=False was added to Mul._eval_is_positive
|
| 469 |
+
x = 2*Symbol('x', positive=False)
|
| 470 |
+
assert x.is_positive is False # This was None before
|
| 471 |
+
assert x.is_nonpositive is None
|
| 472 |
+
assert x.is_negative is None
|
| 473 |
+
assert x.is_nonnegative is None
|
| 474 |
+
assert x.is_zero is None
|
| 475 |
+
assert x.is_nonzero is None
|
| 476 |
+
|
| 477 |
+
|
| 478 |
+
@XFAIL
|
| 479 |
+
def test_symbol_infinitereal_mul():
|
| 480 |
+
ix = Symbol('ix', infinite=True, extended_real=True)
|
| 481 |
+
assert (-ix).is_extended_positive is None
|
| 482 |
+
|
| 483 |
+
|
| 484 |
+
def test_neg_symbol_falsepositive():
|
| 485 |
+
x = -Symbol('x', positive=False)
|
| 486 |
+
assert x.is_positive is None
|
| 487 |
+
assert x.is_nonpositive is None
|
| 488 |
+
assert x.is_negative is False
|
| 489 |
+
assert x.is_nonnegative is None
|
| 490 |
+
assert x.is_zero is None
|
| 491 |
+
assert x.is_nonzero is None
|
| 492 |
+
|
| 493 |
+
|
| 494 |
+
def test_neg_symbol_falsenegative():
|
| 495 |
+
# To test pull request 9379
|
| 496 |
+
# Explicit handling of arg.is_negative=False was added to Mul._eval_is_positive
|
| 497 |
+
x = -Symbol('x', negative=False)
|
| 498 |
+
assert x.is_positive is False # This was None before
|
| 499 |
+
assert x.is_nonpositive is None
|
| 500 |
+
assert x.is_negative is None
|
| 501 |
+
assert x.is_nonnegative is None
|
| 502 |
+
assert x.is_zero is None
|
| 503 |
+
assert x.is_nonzero is None
|
| 504 |
+
|
| 505 |
+
|
| 506 |
+
def test_symbol_falsepositive_real():
|
| 507 |
+
x = Symbol('x', positive=False, real=True)
|
| 508 |
+
assert x.is_positive is False
|
| 509 |
+
assert x.is_nonpositive is True
|
| 510 |
+
assert x.is_negative is None
|
| 511 |
+
assert x.is_nonnegative is None
|
| 512 |
+
assert x.is_zero is None
|
| 513 |
+
assert x.is_nonzero is None
|
| 514 |
+
|
| 515 |
+
|
| 516 |
+
def test_neg_symbol_falsepositive_real():
|
| 517 |
+
x = -Symbol('x', positive=False, real=True)
|
| 518 |
+
assert x.is_positive is None
|
| 519 |
+
assert x.is_nonpositive is None
|
| 520 |
+
assert x.is_negative is False
|
| 521 |
+
assert x.is_nonnegative is True
|
| 522 |
+
assert x.is_zero is None
|
| 523 |
+
assert x.is_nonzero is None
|
| 524 |
+
|
| 525 |
+
|
| 526 |
+
def test_symbol_falsenonnegative():
|
| 527 |
+
x = Symbol('x', nonnegative=False)
|
| 528 |
+
assert x.is_positive is False
|
| 529 |
+
assert x.is_nonpositive is None
|
| 530 |
+
assert x.is_negative is None
|
| 531 |
+
assert x.is_nonnegative is False
|
| 532 |
+
assert x.is_zero is False
|
| 533 |
+
assert x.is_nonzero is None
|
| 534 |
+
|
| 535 |
+
|
| 536 |
+
@XFAIL
|
| 537 |
+
def test_neg_symbol_falsenonnegative():
|
| 538 |
+
x = -Symbol('x', nonnegative=False)
|
| 539 |
+
assert x.is_positive is None
|
| 540 |
+
assert x.is_nonpositive is False # this currently returns None
|
| 541 |
+
assert x.is_negative is False # this currently returns None
|
| 542 |
+
assert x.is_nonnegative is None
|
| 543 |
+
assert x.is_zero is False # this currently returns None
|
| 544 |
+
assert x.is_nonzero is True # this currently returns None
|
| 545 |
+
|
| 546 |
+
|
| 547 |
+
def test_symbol_falsenonnegative_real():
|
| 548 |
+
x = Symbol('x', nonnegative=False, real=True)
|
| 549 |
+
assert x.is_positive is False
|
| 550 |
+
assert x.is_nonpositive is True
|
| 551 |
+
assert x.is_negative is True
|
| 552 |
+
assert x.is_nonnegative is False
|
| 553 |
+
assert x.is_zero is False
|
| 554 |
+
assert x.is_nonzero is True
|
| 555 |
+
|
| 556 |
+
|
| 557 |
+
def test_neg_symbol_falsenonnegative_real():
|
| 558 |
+
x = -Symbol('x', nonnegative=False, real=True)
|
| 559 |
+
assert x.is_positive is True
|
| 560 |
+
assert x.is_nonpositive is False
|
| 561 |
+
assert x.is_negative is False
|
| 562 |
+
assert x.is_nonnegative is True
|
| 563 |
+
assert x.is_zero is False
|
| 564 |
+
assert x.is_nonzero is True
|
| 565 |
+
|
| 566 |
+
|
| 567 |
+
def test_prime():
|
| 568 |
+
assert S.NegativeOne.is_prime is False
|
| 569 |
+
assert S(-2).is_prime is False
|
| 570 |
+
assert S(-4).is_prime is False
|
| 571 |
+
assert S.Zero.is_prime is False
|
| 572 |
+
assert S.One.is_prime is False
|
| 573 |
+
assert S(2).is_prime is True
|
| 574 |
+
assert S(17).is_prime is True
|
| 575 |
+
assert S(4).is_prime is False
|
| 576 |
+
|
| 577 |
+
|
| 578 |
+
def test_composite():
|
| 579 |
+
assert S.NegativeOne.is_composite is False
|
| 580 |
+
assert S(-2).is_composite is False
|
| 581 |
+
assert S(-4).is_composite is False
|
| 582 |
+
assert S.Zero.is_composite is False
|
| 583 |
+
assert S(2).is_composite is False
|
| 584 |
+
assert S(17).is_composite is False
|
| 585 |
+
assert S(4).is_composite is True
|
| 586 |
+
x = Dummy(integer=True, positive=True, prime=False)
|
| 587 |
+
assert x.is_composite is None # x could be 1
|
| 588 |
+
assert (x + 1).is_composite is None
|
| 589 |
+
x = Dummy(positive=True, even=True, prime=False)
|
| 590 |
+
assert x.is_integer is True
|
| 591 |
+
assert x.is_composite is True
|
| 592 |
+
|
| 593 |
+
|
| 594 |
+
def test_prime_symbol():
|
| 595 |
+
x = Symbol('x', prime=True)
|
| 596 |
+
assert x.is_prime is True
|
| 597 |
+
assert x.is_integer is True
|
| 598 |
+
assert x.is_positive is True
|
| 599 |
+
assert x.is_negative is False
|
| 600 |
+
assert x.is_nonpositive is False
|
| 601 |
+
assert x.is_nonnegative is True
|
| 602 |
+
|
| 603 |
+
x = Symbol('x', prime=False)
|
| 604 |
+
assert x.is_prime is False
|
| 605 |
+
assert x.is_integer is None
|
| 606 |
+
assert x.is_positive is None
|
| 607 |
+
assert x.is_negative is None
|
| 608 |
+
assert x.is_nonpositive is None
|
| 609 |
+
assert x.is_nonnegative is None
|
| 610 |
+
|
| 611 |
+
|
| 612 |
+
def test_symbol_noncommutative():
|
| 613 |
+
x = Symbol('x', commutative=True)
|
| 614 |
+
assert x.is_complex is None
|
| 615 |
+
|
| 616 |
+
x = Symbol('x', commutative=False)
|
| 617 |
+
assert x.is_integer is False
|
| 618 |
+
assert x.is_rational is False
|
| 619 |
+
assert x.is_algebraic is False
|
| 620 |
+
assert x.is_irrational is False
|
| 621 |
+
assert x.is_real is False
|
| 622 |
+
assert x.is_complex is False
|
| 623 |
+
|
| 624 |
+
|
| 625 |
+
def test_other_symbol():
|
| 626 |
+
x = Symbol('x', integer=True)
|
| 627 |
+
assert x.is_integer is True
|
| 628 |
+
assert x.is_real is True
|
| 629 |
+
assert x.is_finite is True
|
| 630 |
+
|
| 631 |
+
x = Symbol('x', integer=True, nonnegative=True)
|
| 632 |
+
assert x.is_integer is True
|
| 633 |
+
assert x.is_nonnegative is True
|
| 634 |
+
assert x.is_negative is False
|
| 635 |
+
assert x.is_positive is None
|
| 636 |
+
assert x.is_finite is True
|
| 637 |
+
|
| 638 |
+
x = Symbol('x', integer=True, nonpositive=True)
|
| 639 |
+
assert x.is_integer is True
|
| 640 |
+
assert x.is_nonpositive is True
|
| 641 |
+
assert x.is_positive is False
|
| 642 |
+
assert x.is_negative is None
|
| 643 |
+
assert x.is_finite is True
|
| 644 |
+
|
| 645 |
+
x = Symbol('x', odd=True)
|
| 646 |
+
assert x.is_odd is True
|
| 647 |
+
assert x.is_even is False
|
| 648 |
+
assert x.is_integer is True
|
| 649 |
+
assert x.is_finite is True
|
| 650 |
+
|
| 651 |
+
x = Symbol('x', odd=False)
|
| 652 |
+
assert x.is_odd is False
|
| 653 |
+
assert x.is_even is None
|
| 654 |
+
assert x.is_integer is None
|
| 655 |
+
assert x.is_finite is None
|
| 656 |
+
|
| 657 |
+
x = Symbol('x', even=True)
|
| 658 |
+
assert x.is_even is True
|
| 659 |
+
assert x.is_odd is False
|
| 660 |
+
assert x.is_integer is True
|
| 661 |
+
assert x.is_finite is True
|
| 662 |
+
|
| 663 |
+
x = Symbol('x', even=False)
|
| 664 |
+
assert x.is_even is False
|
| 665 |
+
assert x.is_odd is None
|
| 666 |
+
assert x.is_integer is None
|
| 667 |
+
assert x.is_finite is None
|
| 668 |
+
|
| 669 |
+
x = Symbol('x', integer=True, nonnegative=True)
|
| 670 |
+
assert x.is_integer is True
|
| 671 |
+
assert x.is_nonnegative is True
|
| 672 |
+
assert x.is_finite is True
|
| 673 |
+
|
| 674 |
+
x = Symbol('x', integer=True, nonpositive=True)
|
| 675 |
+
assert x.is_integer is True
|
| 676 |
+
assert x.is_nonpositive is True
|
| 677 |
+
assert x.is_finite is True
|
| 678 |
+
|
| 679 |
+
x = Symbol('x', rational=True)
|
| 680 |
+
assert x.is_real is True
|
| 681 |
+
assert x.is_finite is True
|
| 682 |
+
|
| 683 |
+
x = Symbol('x', rational=False)
|
| 684 |
+
assert x.is_real is None
|
| 685 |
+
assert x.is_finite is None
|
| 686 |
+
|
| 687 |
+
x = Symbol('x', irrational=True)
|
| 688 |
+
assert x.is_real is True
|
| 689 |
+
assert x.is_finite is True
|
| 690 |
+
|
| 691 |
+
x = Symbol('x', irrational=False)
|
| 692 |
+
assert x.is_real is None
|
| 693 |
+
assert x.is_finite is None
|
| 694 |
+
|
| 695 |
+
with raises(AttributeError):
|
| 696 |
+
x.is_real = False
|
| 697 |
+
|
| 698 |
+
x = Symbol('x', algebraic=True)
|
| 699 |
+
assert x.is_transcendental is False
|
| 700 |
+
x = Symbol('x', transcendental=True)
|
| 701 |
+
assert x.is_algebraic is False
|
| 702 |
+
assert x.is_rational is False
|
| 703 |
+
assert x.is_integer is False
|
| 704 |
+
|
| 705 |
+
|
| 706 |
+
def test_evaluate_false():
|
| 707 |
+
# Previously this failed because the assumptions query would make new
|
| 708 |
+
# expressions and some of the evaluation logic would fail under
|
| 709 |
+
# evaluate(False).
|
| 710 |
+
from sympy.core.parameters import evaluate
|
| 711 |
+
from sympy.abc import x, h
|
| 712 |
+
f = 2**x**7
|
| 713 |
+
with evaluate(False):
|
| 714 |
+
fh = f.xreplace({x: x+h})
|
| 715 |
+
assert fh.exp.is_rational is None
|
| 716 |
+
|
| 717 |
+
|
| 718 |
+
def test_issue_3825():
|
| 719 |
+
"""catch: hash instability"""
|
| 720 |
+
x = Symbol("x")
|
| 721 |
+
y = Symbol("y")
|
| 722 |
+
a1 = x + y
|
| 723 |
+
a2 = y + x
|
| 724 |
+
a2.is_comparable
|
| 725 |
+
|
| 726 |
+
h1 = hash(a1)
|
| 727 |
+
h2 = hash(a2)
|
| 728 |
+
assert h1 == h2
|
| 729 |
+
|
| 730 |
+
|
| 731 |
+
def test_issue_4822():
|
| 732 |
+
z = (-1)**Rational(1, 3)*(1 - I*sqrt(3))
|
| 733 |
+
assert z.is_real in [True, None]
|
| 734 |
+
|
| 735 |
+
|
| 736 |
+
def test_hash_vs_typeinfo():
|
| 737 |
+
"""seemingly different typeinfo, but in fact equal"""
|
| 738 |
+
|
| 739 |
+
# the following two are semantically equal
|
| 740 |
+
x1 = Symbol('x', even=True)
|
| 741 |
+
x2 = Symbol('x', integer=True, odd=False)
|
| 742 |
+
|
| 743 |
+
assert hash(x1) == hash(x2)
|
| 744 |
+
assert x1 == x2
|
| 745 |
+
|
| 746 |
+
|
| 747 |
+
def test_hash_vs_typeinfo_2():
|
| 748 |
+
"""different typeinfo should mean !eq"""
|
| 749 |
+
# the following two are semantically different
|
| 750 |
+
x = Symbol('x')
|
| 751 |
+
x1 = Symbol('x', even=True)
|
| 752 |
+
|
| 753 |
+
assert x != x1
|
| 754 |
+
assert hash(x) != hash(x1) # This might fail with very low probability
|
| 755 |
+
|
| 756 |
+
|
| 757 |
+
def test_hash_vs_eq():
|
| 758 |
+
"""catch: different hash for equal objects"""
|
| 759 |
+
a = 1 + S.Pi # important: do not fold it into a Number instance
|
| 760 |
+
ha = hash(a) # it should be Add/Mul/... to trigger the bug
|
| 761 |
+
|
| 762 |
+
a.is_positive # this uses .evalf() and deduces it is positive
|
| 763 |
+
assert a.is_positive is True
|
| 764 |
+
|
| 765 |
+
# be sure that hash stayed the same
|
| 766 |
+
assert ha == hash(a)
|
| 767 |
+
|
| 768 |
+
# now b should be the same expression
|
| 769 |
+
b = a.expand(trig=True)
|
| 770 |
+
hb = hash(b)
|
| 771 |
+
|
| 772 |
+
assert a == b
|
| 773 |
+
assert ha == hb
|
| 774 |
+
|
| 775 |
+
|
| 776 |
+
def test_Add_is_pos_neg():
|
| 777 |
+
# these cover lines not covered by the rest of tests in core
|
| 778 |
+
n = Symbol('n', extended_negative=True, infinite=True)
|
| 779 |
+
nn = Symbol('n', extended_nonnegative=True, infinite=True)
|
| 780 |
+
np = Symbol('n', extended_nonpositive=True, infinite=True)
|
| 781 |
+
p = Symbol('p', extended_positive=True, infinite=True)
|
| 782 |
+
r = Dummy(extended_real=True, finite=False)
|
| 783 |
+
x = Symbol('x')
|
| 784 |
+
xf = Symbol('xf', finite=True)
|
| 785 |
+
assert (n + p).is_extended_positive is None
|
| 786 |
+
assert (n + x).is_extended_positive is None
|
| 787 |
+
assert (p + x).is_extended_positive is None
|
| 788 |
+
assert (n + p).is_extended_negative is None
|
| 789 |
+
assert (n + x).is_extended_negative is None
|
| 790 |
+
assert (p + x).is_extended_negative is None
|
| 791 |
+
|
| 792 |
+
assert (n + xf).is_extended_positive is False
|
| 793 |
+
assert (p + xf).is_extended_positive is True
|
| 794 |
+
assert (n + xf).is_extended_negative is True
|
| 795 |
+
assert (p + xf).is_extended_negative is False
|
| 796 |
+
|
| 797 |
+
assert (x - S.Infinity).is_extended_negative is None # issue 7798
|
| 798 |
+
# issue 8046, 16.2
|
| 799 |
+
assert (p + nn).is_extended_positive
|
| 800 |
+
assert (n + np).is_extended_negative
|
| 801 |
+
assert (p + r).is_extended_positive is None
|
| 802 |
+
|
| 803 |
+
|
| 804 |
+
def test_Add_is_imaginary():
|
| 805 |
+
nn = Dummy(nonnegative=True)
|
| 806 |
+
assert (I*nn + I).is_imaginary # issue 8046, 17
|
| 807 |
+
|
| 808 |
+
|
| 809 |
+
def test_Add_is_algebraic():
|
| 810 |
+
a = Symbol('a', algebraic=True)
|
| 811 |
+
b = Symbol('a', algebraic=True)
|
| 812 |
+
na = Symbol('na', algebraic=False)
|
| 813 |
+
nb = Symbol('nb', algebraic=False)
|
| 814 |
+
x = Symbol('x')
|
| 815 |
+
assert (a + b).is_algebraic
|
| 816 |
+
assert (na + nb).is_algebraic is None
|
| 817 |
+
assert (a + na).is_algebraic is False
|
| 818 |
+
assert (a + x).is_algebraic is None
|
| 819 |
+
assert (na + x).is_algebraic is None
|
| 820 |
+
|
| 821 |
+
|
| 822 |
+
def test_Mul_is_algebraic():
|
| 823 |
+
a = Symbol('a', algebraic=True)
|
| 824 |
+
b = Symbol('b', algebraic=True)
|
| 825 |
+
na = Symbol('na', algebraic=False)
|
| 826 |
+
an = Symbol('an', algebraic=True, nonzero=True)
|
| 827 |
+
nb = Symbol('nb', algebraic=False)
|
| 828 |
+
x = Symbol('x')
|
| 829 |
+
assert (a*b).is_algebraic is True
|
| 830 |
+
assert (na*nb).is_algebraic is None
|
| 831 |
+
assert (a*na).is_algebraic is None
|
| 832 |
+
assert (an*na).is_algebraic is False
|
| 833 |
+
assert (a*x).is_algebraic is None
|
| 834 |
+
assert (na*x).is_algebraic is None
|
| 835 |
+
|
| 836 |
+
|
| 837 |
+
def test_Pow_is_algebraic():
|
| 838 |
+
e = Symbol('e', algebraic=True)
|
| 839 |
+
|
| 840 |
+
assert Pow(1, e, evaluate=False).is_algebraic
|
| 841 |
+
assert Pow(0, e, evaluate=False).is_algebraic
|
| 842 |
+
|
| 843 |
+
a = Symbol('a', algebraic=True)
|
| 844 |
+
azf = Symbol('azf', algebraic=True, zero=False)
|
| 845 |
+
na = Symbol('na', algebraic=False)
|
| 846 |
+
ia = Symbol('ia', algebraic=True, irrational=True)
|
| 847 |
+
ib = Symbol('ib', algebraic=True, irrational=True)
|
| 848 |
+
r = Symbol('r', rational=True)
|
| 849 |
+
x = Symbol('x')
|
| 850 |
+
assert (a**2).is_algebraic is True
|
| 851 |
+
assert (a**r).is_algebraic is None
|
| 852 |
+
assert (azf**r).is_algebraic is True
|
| 853 |
+
assert (a**x).is_algebraic is None
|
| 854 |
+
assert (na**r).is_algebraic is None
|
| 855 |
+
assert (ia**r).is_algebraic is True
|
| 856 |
+
assert (ia**ib).is_algebraic is False
|
| 857 |
+
|
| 858 |
+
assert (a**e).is_algebraic is None
|
| 859 |
+
|
| 860 |
+
# Gelfond-Schneider constant:
|
| 861 |
+
assert Pow(2, sqrt(2), evaluate=False).is_algebraic is False
|
| 862 |
+
|
| 863 |
+
assert Pow(S.GoldenRatio, sqrt(3), evaluate=False).is_algebraic is False
|
| 864 |
+
|
| 865 |
+
# issue 8649
|
| 866 |
+
t = Symbol('t', real=True, transcendental=True)
|
| 867 |
+
n = Symbol('n', integer=True)
|
| 868 |
+
assert (t**n).is_algebraic is None
|
| 869 |
+
assert (t**n).is_integer is None
|
| 870 |
+
|
| 871 |
+
assert (pi**3).is_algebraic is False
|
| 872 |
+
r = Symbol('r', zero=True)
|
| 873 |
+
assert (pi**r).is_algebraic is True
|
| 874 |
+
|
| 875 |
+
|
| 876 |
+
def test_Mul_is_prime_composite():
|
| 877 |
+
x = Symbol('x', positive=True, integer=True)
|
| 878 |
+
y = Symbol('y', positive=True, integer=True)
|
| 879 |
+
assert (x*y).is_prime is None
|
| 880 |
+
assert ( (x+1)*(y+1) ).is_prime is False
|
| 881 |
+
assert ( (x+1)*(y+1) ).is_composite is True
|
| 882 |
+
|
| 883 |
+
x = Symbol('x', positive=True)
|
| 884 |
+
assert ( (x+1)*(y+1) ).is_prime is None
|
| 885 |
+
assert ( (x+1)*(y+1) ).is_composite is None
|
| 886 |
+
|
| 887 |
+
|
| 888 |
+
def test_Pow_is_pos_neg():
|
| 889 |
+
z = Symbol('z', real=True)
|
| 890 |
+
w = Symbol('w', nonpositive=True)
|
| 891 |
+
|
| 892 |
+
assert (S.NegativeOne**S(2)).is_positive is True
|
| 893 |
+
assert (S.One**z).is_positive is True
|
| 894 |
+
assert (S.NegativeOne**S(3)).is_positive is False
|
| 895 |
+
assert (S.Zero**S.Zero).is_positive is True # 0**0 is 1
|
| 896 |
+
assert (w**S(3)).is_positive is False
|
| 897 |
+
assert (w**S(2)).is_positive is None
|
| 898 |
+
assert (I**2).is_positive is False
|
| 899 |
+
assert (I**4).is_positive is True
|
| 900 |
+
|
| 901 |
+
# tests emerging from #16332 issue
|
| 902 |
+
p = Symbol('p', zero=True)
|
| 903 |
+
q = Symbol('q', zero=False, real=True)
|
| 904 |
+
j = Symbol('j', zero=False, even=True)
|
| 905 |
+
x = Symbol('x', zero=True)
|
| 906 |
+
y = Symbol('y', zero=True)
|
| 907 |
+
assert (p**q).is_positive is False
|
| 908 |
+
assert (p**q).is_negative is False
|
| 909 |
+
assert (p**j).is_positive is False
|
| 910 |
+
assert (x**y).is_positive is True # 0**0
|
| 911 |
+
assert (x**y).is_negative is False
|
| 912 |
+
|
| 913 |
+
|
| 914 |
+
def test_Pow_is_prime_composite():
|
| 915 |
+
x = Symbol('x', positive=True, integer=True)
|
| 916 |
+
y = Symbol('y', positive=True, integer=True)
|
| 917 |
+
assert (x**y).is_prime is None
|
| 918 |
+
assert ( x**(y+1) ).is_prime is False
|
| 919 |
+
assert ( x**(y+1) ).is_composite is None
|
| 920 |
+
assert ( (x+1)**(y+1) ).is_composite is True
|
| 921 |
+
assert ( (-x-1)**(2*y) ).is_composite is True
|
| 922 |
+
|
| 923 |
+
x = Symbol('x', positive=True)
|
| 924 |
+
assert (x**y).is_prime is None
|
| 925 |
+
|
| 926 |
+
|
| 927 |
+
def test_Mul_is_infinite():
|
| 928 |
+
x = Symbol('x')
|
| 929 |
+
f = Symbol('f', finite=True)
|
| 930 |
+
i = Symbol('i', infinite=True)
|
| 931 |
+
z = Dummy(zero=True)
|
| 932 |
+
nzf = Dummy(finite=True, zero=False)
|
| 933 |
+
from sympy.core.mul import Mul
|
| 934 |
+
assert (x*f).is_finite is None
|
| 935 |
+
assert (x*i).is_finite is None
|
| 936 |
+
assert (f*i).is_finite is None
|
| 937 |
+
assert (x*f*i).is_finite is None
|
| 938 |
+
assert (z*i).is_finite is None
|
| 939 |
+
assert (nzf*i).is_finite is False
|
| 940 |
+
assert (z*f).is_finite is True
|
| 941 |
+
assert Mul(0, f, evaluate=False).is_finite is True
|
| 942 |
+
assert Mul(0, i, evaluate=False).is_finite is None
|
| 943 |
+
|
| 944 |
+
assert (x*f).is_infinite is None
|
| 945 |
+
assert (x*i).is_infinite is None
|
| 946 |
+
assert (f*i).is_infinite is None
|
| 947 |
+
assert (x*f*i).is_infinite is None
|
| 948 |
+
assert (z*i).is_infinite is S.NaN.is_infinite
|
| 949 |
+
assert (nzf*i).is_infinite is True
|
| 950 |
+
assert (z*f).is_infinite is False
|
| 951 |
+
assert Mul(0, f, evaluate=False).is_infinite is False
|
| 952 |
+
assert Mul(0, i, evaluate=False).is_infinite is S.NaN.is_infinite
|
| 953 |
+
|
| 954 |
+
|
| 955 |
+
def test_Add_is_infinite():
|
| 956 |
+
x = Symbol('x')
|
| 957 |
+
f = Symbol('f', finite=True)
|
| 958 |
+
i = Symbol('i', infinite=True)
|
| 959 |
+
i2 = Symbol('i2', infinite=True)
|
| 960 |
+
z = Dummy(zero=True)
|
| 961 |
+
nzf = Dummy(finite=True, zero=False)
|
| 962 |
+
from sympy.core.add import Add
|
| 963 |
+
assert (x+f).is_finite is None
|
| 964 |
+
assert (x+i).is_finite is None
|
| 965 |
+
assert (f+i).is_finite is False
|
| 966 |
+
assert (x+f+i).is_finite is None
|
| 967 |
+
assert (z+i).is_finite is False
|
| 968 |
+
assert (nzf+i).is_finite is False
|
| 969 |
+
assert (z+f).is_finite is True
|
| 970 |
+
assert (i+i2).is_finite is None
|
| 971 |
+
assert Add(0, f, evaluate=False).is_finite is True
|
| 972 |
+
assert Add(0, i, evaluate=False).is_finite is False
|
| 973 |
+
|
| 974 |
+
assert (x+f).is_infinite is None
|
| 975 |
+
assert (x+i).is_infinite is None
|
| 976 |
+
assert (f+i).is_infinite is True
|
| 977 |
+
assert (x+f+i).is_infinite is None
|
| 978 |
+
assert (z+i).is_infinite is True
|
| 979 |
+
assert (nzf+i).is_infinite is True
|
| 980 |
+
assert (z+f).is_infinite is False
|
| 981 |
+
assert (i+i2).is_infinite is None
|
| 982 |
+
assert Add(0, f, evaluate=False).is_infinite is False
|
| 983 |
+
assert Add(0, i, evaluate=False).is_infinite is True
|
| 984 |
+
|
| 985 |
+
|
| 986 |
+
def test_special_is_rational():
|
| 987 |
+
i = Symbol('i', integer=True)
|
| 988 |
+
i2 = Symbol('i2', integer=True)
|
| 989 |
+
ni = Symbol('ni', integer=True, nonzero=True)
|
| 990 |
+
r = Symbol('r', rational=True)
|
| 991 |
+
rn = Symbol('r', rational=True, nonzero=True)
|
| 992 |
+
nr = Symbol('nr', irrational=True)
|
| 993 |
+
x = Symbol('x')
|
| 994 |
+
assert sqrt(3).is_rational is False
|
| 995 |
+
assert (3 + sqrt(3)).is_rational is False
|
| 996 |
+
assert (3*sqrt(3)).is_rational is False
|
| 997 |
+
assert exp(3).is_rational is False
|
| 998 |
+
assert exp(ni).is_rational is False
|
| 999 |
+
assert exp(rn).is_rational is False
|
| 1000 |
+
assert exp(x).is_rational is None
|
| 1001 |
+
assert exp(log(3), evaluate=False).is_rational is True
|
| 1002 |
+
assert log(exp(3), evaluate=False).is_rational is True
|
| 1003 |
+
assert log(3).is_rational is False
|
| 1004 |
+
assert log(ni + 1).is_rational is False
|
| 1005 |
+
assert log(rn + 1).is_rational is False
|
| 1006 |
+
assert log(x).is_rational is None
|
| 1007 |
+
assert (sqrt(3) + sqrt(5)).is_rational is None
|
| 1008 |
+
assert (sqrt(3) + S.Pi).is_rational is False
|
| 1009 |
+
assert (x**i).is_rational is None
|
| 1010 |
+
assert (i**i).is_rational is True
|
| 1011 |
+
assert (i**i2).is_rational is None
|
| 1012 |
+
assert (r**i).is_rational is None
|
| 1013 |
+
assert (r**r).is_rational is None
|
| 1014 |
+
assert (r**x).is_rational is None
|
| 1015 |
+
assert (nr**i).is_rational is None # issue 8598
|
| 1016 |
+
assert (nr**Symbol('z', zero=True)).is_rational
|
| 1017 |
+
assert sin(1).is_rational is False
|
| 1018 |
+
assert sin(ni).is_rational is False
|
| 1019 |
+
assert sin(rn).is_rational is False
|
| 1020 |
+
assert sin(x).is_rational is None
|
| 1021 |
+
assert asin(r).is_rational is False
|
| 1022 |
+
assert sin(asin(3), evaluate=False).is_rational is True
|
| 1023 |
+
|
| 1024 |
+
|
| 1025 |
+
@XFAIL
|
| 1026 |
+
def test_issue_6275():
|
| 1027 |
+
x = Symbol('x')
|
| 1028 |
+
# both zero or both Muls...but neither "change would be very appreciated.
|
| 1029 |
+
# This is similar to x/x => 1 even though if x = 0, it is really nan.
|
| 1030 |
+
assert isinstance(x*0, type(0*S.Infinity))
|
| 1031 |
+
if 0*S.Infinity is S.NaN:
|
| 1032 |
+
b = Symbol('b', finite=None)
|
| 1033 |
+
assert (b*0).is_zero is None
|
| 1034 |
+
|
| 1035 |
+
|
| 1036 |
+
def test_sanitize_assumptions():
|
| 1037 |
+
# issue 6666
|
| 1038 |
+
for cls in (Symbol, Dummy, Wild):
|
| 1039 |
+
x = cls('x', real=1, positive=0)
|
| 1040 |
+
assert x.is_real is True
|
| 1041 |
+
assert x.is_positive is False
|
| 1042 |
+
assert cls('', real=True, positive=None).is_positive is None
|
| 1043 |
+
raises(ValueError, lambda: cls('', commutative=None))
|
| 1044 |
+
raises(ValueError, lambda: Symbol._sanitize({"commutative": None}))
|
| 1045 |
+
|
| 1046 |
+
|
| 1047 |
+
def test_special_assumptions():
|
| 1048 |
+
e = -3 - sqrt(5) + (-sqrt(10)/2 - sqrt(2)/2)**2
|
| 1049 |
+
assert simplify(e < 0) is S.false
|
| 1050 |
+
assert simplify(e > 0) is S.false
|
| 1051 |
+
assert (e == 0) is False # it's not a literal 0
|
| 1052 |
+
assert e.equals(0) is True
|
| 1053 |
+
|
| 1054 |
+
|
| 1055 |
+
def test_inconsistent():
|
| 1056 |
+
# cf. issues 5795 and 5545
|
| 1057 |
+
raises(InconsistentAssumptions, lambda: Symbol('x', real=True,
|
| 1058 |
+
commutative=False))
|
| 1059 |
+
|
| 1060 |
+
|
| 1061 |
+
def test_issue_6631():
|
| 1062 |
+
assert ((-1)**(I)).is_real is True
|
| 1063 |
+
assert ((-1)**(I*2)).is_real is True
|
| 1064 |
+
assert ((-1)**(I/2)).is_real is True
|
| 1065 |
+
assert ((-1)**(I*S.Pi)).is_real is True
|
| 1066 |
+
assert (I**(I + 2)).is_real is True
|
| 1067 |
+
|
| 1068 |
+
|
| 1069 |
+
def test_issue_2730():
|
| 1070 |
+
assert (1/(1 + I)).is_real is False
|
| 1071 |
+
|
| 1072 |
+
|
| 1073 |
+
def test_issue_4149():
|
| 1074 |
+
assert (3 + I).is_complex
|
| 1075 |
+
assert (3 + I).is_imaginary is False
|
| 1076 |
+
assert (3*I + S.Pi*I).is_imaginary
|
| 1077 |
+
# as Zero.is_imaginary is False, see issue 7649
|
| 1078 |
+
y = Symbol('y', real=True)
|
| 1079 |
+
assert (3*I + S.Pi*I + y*I).is_imaginary is None
|
| 1080 |
+
p = Symbol('p', positive=True)
|
| 1081 |
+
assert (3*I + S.Pi*I + p*I).is_imaginary
|
| 1082 |
+
n = Symbol('n', negative=True)
|
| 1083 |
+
assert (-3*I - S.Pi*I + n*I).is_imaginary
|
| 1084 |
+
|
| 1085 |
+
i = Symbol('i', imaginary=True)
|
| 1086 |
+
assert ([(i**a).is_imaginary for a in range(4)] ==
|
| 1087 |
+
[False, True, False, True])
|
| 1088 |
+
|
| 1089 |
+
# tests from the PR #7887:
|
| 1090 |
+
e = S("-sqrt(3)*I/2 + 0.866025403784439*I")
|
| 1091 |
+
assert e.is_real is False
|
| 1092 |
+
assert e.is_imaginary
|
| 1093 |
+
|
| 1094 |
+
|
| 1095 |
+
def test_issue_2920():
|
| 1096 |
+
n = Symbol('n', negative=True)
|
| 1097 |
+
assert sqrt(n).is_imaginary
|
| 1098 |
+
|
| 1099 |
+
|
| 1100 |
+
def test_issue_7899():
|
| 1101 |
+
x = Symbol('x', real=True)
|
| 1102 |
+
assert (I*x).is_real is None
|
| 1103 |
+
assert ((x - I)*(x - 1)).is_zero is None
|
| 1104 |
+
assert ((x - I)*(x - 1)).is_real is None
|
| 1105 |
+
|
| 1106 |
+
|
| 1107 |
+
@XFAIL
|
| 1108 |
+
def test_issue_7993():
|
| 1109 |
+
x = Dummy(integer=True)
|
| 1110 |
+
y = Dummy(noninteger=True)
|
| 1111 |
+
assert (x - y).is_zero is False
|
| 1112 |
+
|
| 1113 |
+
|
| 1114 |
+
def test_issue_8075():
|
| 1115 |
+
raises(InconsistentAssumptions, lambda: Dummy(zero=True, finite=False))
|
| 1116 |
+
raises(InconsistentAssumptions, lambda: Dummy(zero=True, infinite=True))
|
| 1117 |
+
|
| 1118 |
+
|
| 1119 |
+
def test_issue_8642():
|
| 1120 |
+
x = Symbol('x', real=True, integer=False)
|
| 1121 |
+
assert (x*2).is_integer is None, (x*2).is_integer
|
| 1122 |
+
|
| 1123 |
+
|
| 1124 |
+
def test_issues_8632_8633_8638_8675_8992():
|
| 1125 |
+
p = Dummy(integer=True, positive=True)
|
| 1126 |
+
nn = Dummy(integer=True, nonnegative=True)
|
| 1127 |
+
assert (p - S.Half).is_positive
|
| 1128 |
+
assert (p - 1).is_nonnegative
|
| 1129 |
+
assert (nn + 1).is_positive
|
| 1130 |
+
assert (-p + 1).is_nonpositive
|
| 1131 |
+
assert (-nn - 1).is_negative
|
| 1132 |
+
prime = Dummy(prime=True)
|
| 1133 |
+
assert (prime - 2).is_nonnegative
|
| 1134 |
+
assert (prime - 3).is_nonnegative is None
|
| 1135 |
+
even = Dummy(positive=True, even=True)
|
| 1136 |
+
assert (even - 2).is_nonnegative
|
| 1137 |
+
|
| 1138 |
+
p = Dummy(positive=True)
|
| 1139 |
+
assert (p/(p + 1) - 1).is_negative
|
| 1140 |
+
assert ((p + 2)**3 - S.Half).is_positive
|
| 1141 |
+
n = Dummy(negative=True)
|
| 1142 |
+
assert (n - 3).is_nonpositive
|
| 1143 |
+
|
| 1144 |
+
|
| 1145 |
+
def test_issue_9115_9150():
|
| 1146 |
+
n = Dummy('n', integer=True, nonnegative=True)
|
| 1147 |
+
assert (factorial(n) >= 1) == True
|
| 1148 |
+
assert (factorial(n) < 1) == False
|
| 1149 |
+
|
| 1150 |
+
assert factorial(n + 1).is_even is None
|
| 1151 |
+
assert factorial(n + 2).is_even is True
|
| 1152 |
+
assert factorial(n + 2) >= 2
|
| 1153 |
+
|
| 1154 |
+
|
| 1155 |
+
def test_issue_9165():
|
| 1156 |
+
z = Symbol('z', zero=True)
|
| 1157 |
+
f = Symbol('f', finite=False)
|
| 1158 |
+
assert 0/z is S.NaN
|
| 1159 |
+
assert 0*(1/z) is S.NaN
|
| 1160 |
+
assert 0*f is S.NaN
|
| 1161 |
+
|
| 1162 |
+
|
| 1163 |
+
def test_issue_10024():
|
| 1164 |
+
x = Dummy('x')
|
| 1165 |
+
assert Mod(x, 2*pi).is_zero is None
|
| 1166 |
+
|
| 1167 |
+
|
| 1168 |
+
def test_issue_10302():
|
| 1169 |
+
x = Symbol('x')
|
| 1170 |
+
r = Symbol('r', real=True)
|
| 1171 |
+
u = -(3*2**pi)**(1/pi) + 2*3**(1/pi)
|
| 1172 |
+
i = u + u*I
|
| 1173 |
+
|
| 1174 |
+
assert i.is_real is None # w/o simplification this should fail
|
| 1175 |
+
assert (u + i).is_zero is None
|
| 1176 |
+
assert (1 + i).is_zero is False
|
| 1177 |
+
|
| 1178 |
+
a = Dummy('a', zero=True)
|
| 1179 |
+
assert (a + I).is_zero is False
|
| 1180 |
+
assert (a + r*I).is_zero is None
|
| 1181 |
+
assert (a + I).is_imaginary
|
| 1182 |
+
assert (a + x + I).is_imaginary is None
|
| 1183 |
+
assert (a + r*I + I).is_imaginary is None
|
| 1184 |
+
|
| 1185 |
+
|
| 1186 |
+
def test_complex_reciprocal_imaginary():
|
| 1187 |
+
assert (1 / (4 + 3*I)).is_imaginary is False
|
| 1188 |
+
|
| 1189 |
+
|
| 1190 |
+
def test_issue_16313():
|
| 1191 |
+
x = Symbol('x', extended_real=False)
|
| 1192 |
+
k = Symbol('k', real=True)
|
| 1193 |
+
l = Symbol('l', real=True, zero=False)
|
| 1194 |
+
assert (-x).is_real is False
|
| 1195 |
+
assert (k*x).is_real is None # k can be zero also
|
| 1196 |
+
assert (l*x).is_real is False
|
| 1197 |
+
assert (l*x*x).is_real is None # since x*x can be a real number
|
| 1198 |
+
assert (-x).is_positive is False
|
| 1199 |
+
|
| 1200 |
+
|
| 1201 |
+
def test_issue_16579():
|
| 1202 |
+
# extended_real -> finite | infinite
|
| 1203 |
+
x = Symbol('x', extended_real=True, infinite=False)
|
| 1204 |
+
y = Symbol('y', extended_real=True, finite=False)
|
| 1205 |
+
assert x.is_finite is True
|
| 1206 |
+
assert y.is_infinite is True
|
| 1207 |
+
|
| 1208 |
+
# With PR 16978, complex now implies finite
|
| 1209 |
+
c = Symbol('c', complex=True)
|
| 1210 |
+
assert c.is_finite is True
|
| 1211 |
+
raises(InconsistentAssumptions, lambda: Dummy(complex=True, finite=False))
|
| 1212 |
+
|
| 1213 |
+
# Now infinite == !finite
|
| 1214 |
+
nf = Symbol('nf', finite=False)
|
| 1215 |
+
assert nf.is_infinite is True
|
| 1216 |
+
|
| 1217 |
+
|
| 1218 |
+
def test_issue_17556():
|
| 1219 |
+
z = I*oo
|
| 1220 |
+
assert z.is_imaginary is False
|
| 1221 |
+
assert z.is_finite is False
|
| 1222 |
+
|
| 1223 |
+
|
| 1224 |
+
def test_issue_21651():
|
| 1225 |
+
k = Symbol('k', positive=True, integer=True)
|
| 1226 |
+
exp = 2*2**(-k)
|
| 1227 |
+
assert exp.is_integer is None
|
| 1228 |
+
|
| 1229 |
+
|
| 1230 |
+
def test_assumptions_copy():
|
| 1231 |
+
assert assumptions(Symbol('x'), {"commutative": True}
|
| 1232 |
+
) == {'commutative': True}
|
| 1233 |
+
assert assumptions(Symbol('x'), ['integer']) == {}
|
| 1234 |
+
assert assumptions(Symbol('x'), ['commutative']
|
| 1235 |
+
) == {'commutative': True}
|
| 1236 |
+
assert assumptions(Symbol('x')) == {'commutative': True}
|
| 1237 |
+
assert assumptions(1)['positive']
|
| 1238 |
+
assert assumptions(3 + I) == {
|
| 1239 |
+
'algebraic': True,
|
| 1240 |
+
'commutative': True,
|
| 1241 |
+
'complex': True,
|
| 1242 |
+
'composite': False,
|
| 1243 |
+
'even': False,
|
| 1244 |
+
'extended_negative': False,
|
| 1245 |
+
'extended_nonnegative': False,
|
| 1246 |
+
'extended_nonpositive': False,
|
| 1247 |
+
'extended_nonzero': False,
|
| 1248 |
+
'extended_positive': False,
|
| 1249 |
+
'extended_real': False,
|
| 1250 |
+
'finite': True,
|
| 1251 |
+
'imaginary': False,
|
| 1252 |
+
'infinite': False,
|
| 1253 |
+
'integer': False,
|
| 1254 |
+
'irrational': False,
|
| 1255 |
+
'negative': False,
|
| 1256 |
+
'noninteger': False,
|
| 1257 |
+
'nonnegative': False,
|
| 1258 |
+
'nonpositive': False,
|
| 1259 |
+
'nonzero': False,
|
| 1260 |
+
'odd': False,
|
| 1261 |
+
'positive': False,
|
| 1262 |
+
'prime': False,
|
| 1263 |
+
'rational': False,
|
| 1264 |
+
'real': False,
|
| 1265 |
+
'transcendental': False,
|
| 1266 |
+
'zero': False}
|
| 1267 |
+
|
| 1268 |
+
|
| 1269 |
+
def test_check_assumptions():
|
| 1270 |
+
assert check_assumptions(1, 0) is False
|
| 1271 |
+
x = Symbol('x', positive=True)
|
| 1272 |
+
assert check_assumptions(1, x) is True
|
| 1273 |
+
assert check_assumptions(1, 1) is True
|
| 1274 |
+
assert check_assumptions(-1, 1) is False
|
| 1275 |
+
i = Symbol('i', integer=True)
|
| 1276 |
+
# don't know if i is positive (or prime, etc...)
|
| 1277 |
+
assert check_assumptions(i, 1) is None
|
| 1278 |
+
assert check_assumptions(Dummy(integer=None), integer=True) is None
|
| 1279 |
+
assert check_assumptions(Dummy(integer=None), integer=False) is None
|
| 1280 |
+
assert check_assumptions(Dummy(integer=False), integer=True) is False
|
| 1281 |
+
assert check_assumptions(Dummy(integer=True), integer=False) is False
|
| 1282 |
+
# no T/F assumptions to check
|
| 1283 |
+
assert check_assumptions(Dummy(integer=False), integer=None) is True
|
| 1284 |
+
raises(ValueError, lambda: check_assumptions(2*x, x, positive=True))
|
| 1285 |
+
|
| 1286 |
+
|
| 1287 |
+
def test_failing_assumptions():
|
| 1288 |
+
x = Symbol('x', positive=True)
|
| 1289 |
+
y = Symbol('y')
|
| 1290 |
+
assert failing_assumptions(6*x + y, **x.assumptions0) == \
|
| 1291 |
+
{'real': None, 'imaginary': None, 'complex': None, 'hermitian': None,
|
| 1292 |
+
'positive': None, 'nonpositive': None, 'nonnegative': None, 'nonzero': None,
|
| 1293 |
+
'negative': None, 'zero': None, 'extended_real': None, 'finite': None,
|
| 1294 |
+
'infinite': None, 'extended_negative': None, 'extended_nonnegative': None,
|
| 1295 |
+
'extended_nonpositive': None, 'extended_nonzero': None,
|
| 1296 |
+
'extended_positive': None }
|
| 1297 |
+
|
| 1298 |
+
|
| 1299 |
+
def test_common_assumptions():
|
| 1300 |
+
assert common_assumptions([0, 1, 2]
|
| 1301 |
+
) == {'algebraic': True, 'irrational': False, 'hermitian':
|
| 1302 |
+
True, 'extended_real': True, 'real': True, 'extended_negative':
|
| 1303 |
+
False, 'extended_nonnegative': True, 'integer': True,
|
| 1304 |
+
'rational': True, 'imaginary': False, 'complex': True,
|
| 1305 |
+
'commutative': True,'noninteger': False, 'composite': False,
|
| 1306 |
+
'infinite': False, 'nonnegative': True, 'finite': True,
|
| 1307 |
+
'transcendental': False,'negative': False}
|
| 1308 |
+
assert common_assumptions([0, 1, 2], 'positive integer'.split()
|
| 1309 |
+
) == {'integer': True}
|
| 1310 |
+
assert common_assumptions([0, 1, 2], []) == {}
|
| 1311 |
+
assert common_assumptions([], ['integer']) == {}
|
| 1312 |
+
assert common_assumptions([0], ['integer']) == {'integer': True}
|
| 1313 |
+
|
| 1314 |
+
def test_pre_generated_assumption_rules_are_valid():
|
| 1315 |
+
# check the pre-generated assumptions match freshly generated assumptions
|
| 1316 |
+
# if this check fails, consider updating the assumptions
|
| 1317 |
+
# see sympy.core.assumptions._generate_assumption_rules
|
| 1318 |
+
pre_generated_assumptions =_load_pre_generated_assumption_rules()
|
| 1319 |
+
generated_assumptions =_generate_assumption_rules()
|
| 1320 |
+
assert pre_generated_assumptions._to_python() == generated_assumptions._to_python(), "pre-generated assumptions are invalid, see sympy.core.assumptions._generate_assumption_rules"
|
| 1321 |
+
|
| 1322 |
+
|
| 1323 |
+
def test_ask_shuffle():
|
| 1324 |
+
grp = PermutationGroup(Permutation(1, 0, 2), Permutation(2, 1, 3))
|
| 1325 |
+
|
| 1326 |
+
seed(123)
|
| 1327 |
+
first = grp.random()
|
| 1328 |
+
seed(123)
|
| 1329 |
+
simplify(I)
|
| 1330 |
+
second = grp.random()
|
| 1331 |
+
seed(123)
|
| 1332 |
+
simplify(-I)
|
| 1333 |
+
third = grp.random()
|
| 1334 |
+
|
| 1335 |
+
assert first == second == third
|
.venv/lib/python3.11/site-packages/sympy/core/tests/test_basic.py
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""This tests sympy/core/basic.py with (ideally) no reference to subclasses
|
| 2 |
+
of Basic or Atom."""
|
| 3 |
+
import collections
|
| 4 |
+
from typing import TypeVar, Generic
|
| 5 |
+
|
| 6 |
+
from sympy.assumptions.ask import Q
|
| 7 |
+
from sympy.core.basic import (Basic, Atom, as_Basic,
|
| 8 |
+
_atomic, _aresame)
|
| 9 |
+
from sympy.core.containers import Tuple
|
| 10 |
+
from sympy.core.function import Function, Lambda
|
| 11 |
+
from sympy.core.numbers import I, pi, Float
|
| 12 |
+
from sympy.core.singleton import S
|
| 13 |
+
from sympy.core.symbol import symbols, Symbol, Dummy
|
| 14 |
+
from sympy.concrete.summations import Sum
|
| 15 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
| 16 |
+
from sympy.functions.special.gamma_functions import gamma
|
| 17 |
+
from sympy.integrals.integrals import Integral
|
| 18 |
+
from sympy.functions.elementary.exponential import exp
|
| 19 |
+
from sympy.testing.pytest import raises, warns_deprecated_sympy
|
| 20 |
+
|
| 21 |
+
b1 = Basic()
|
| 22 |
+
b2 = Basic(b1)
|
| 23 |
+
b3 = Basic(b2)
|
| 24 |
+
b21 = Basic(b2, b1)
|
| 25 |
+
T = TypeVar('T')
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def test__aresame():
|
| 29 |
+
assert not _aresame(Basic(Tuple()), Basic())
|
| 30 |
+
for i, j in [(S(2), S(2.)), (1., Float(1))]:
|
| 31 |
+
for do in range(2):
|
| 32 |
+
assert not _aresame(Basic(i), Basic(j))
|
| 33 |
+
assert not _aresame(i, j)
|
| 34 |
+
i, j = j, i
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def test_structure():
|
| 38 |
+
assert b21.args == (b2, b1)
|
| 39 |
+
assert b21.func(*b21.args) == b21
|
| 40 |
+
assert bool(b1)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def test_immutable():
|
| 44 |
+
assert not hasattr(b1, '__dict__')
|
| 45 |
+
with raises(AttributeError):
|
| 46 |
+
b1.x = 1
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def test_equality():
|
| 50 |
+
instances = [b1, b2, b3, b21, Basic(b1, b1, b1), Basic]
|
| 51 |
+
for i, b_i in enumerate(instances):
|
| 52 |
+
for j, b_j in enumerate(instances):
|
| 53 |
+
assert (b_i == b_j) == (i == j)
|
| 54 |
+
assert (b_i != b_j) == (i != j)
|
| 55 |
+
|
| 56 |
+
assert Basic() != []
|
| 57 |
+
assert not(Basic() == [])
|
| 58 |
+
assert Basic() != 0
|
| 59 |
+
assert not(Basic() == 0)
|
| 60 |
+
|
| 61 |
+
class Foo:
|
| 62 |
+
"""
|
| 63 |
+
Class that is unaware of Basic, and relies on both classes returning
|
| 64 |
+
the NotImplemented singleton for equivalence to evaluate to False.
|
| 65 |
+
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
b = Basic()
|
| 69 |
+
foo = Foo()
|
| 70 |
+
|
| 71 |
+
assert b != foo
|
| 72 |
+
assert foo != b
|
| 73 |
+
assert not b == foo
|
| 74 |
+
assert not foo == b
|
| 75 |
+
|
| 76 |
+
class Bar:
|
| 77 |
+
"""
|
| 78 |
+
Class that considers itself equal to any instance of Basic, and relies
|
| 79 |
+
on Basic returning the NotImplemented singleton in order to achieve
|
| 80 |
+
a symmetric equivalence relation.
|
| 81 |
+
|
| 82 |
+
"""
|
| 83 |
+
def __eq__(self, other):
|
| 84 |
+
if isinstance(other, Basic):
|
| 85 |
+
return True
|
| 86 |
+
return NotImplemented
|
| 87 |
+
|
| 88 |
+
def __ne__(self, other):
|
| 89 |
+
return not self == other
|
| 90 |
+
|
| 91 |
+
bar = Bar()
|
| 92 |
+
|
| 93 |
+
assert b == bar
|
| 94 |
+
assert bar == b
|
| 95 |
+
assert not b != bar
|
| 96 |
+
assert not bar != b
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def test_matches_basic():
|
| 100 |
+
instances = [Basic(b1, b1, b2), Basic(b1, b2, b1), Basic(b2, b1, b1),
|
| 101 |
+
Basic(b1, b2), Basic(b2, b1), b2, b1]
|
| 102 |
+
for i, b_i in enumerate(instances):
|
| 103 |
+
for j, b_j in enumerate(instances):
|
| 104 |
+
if i == j:
|
| 105 |
+
assert b_i.matches(b_j) == {}
|
| 106 |
+
else:
|
| 107 |
+
assert b_i.matches(b_j) is None
|
| 108 |
+
assert b1.match(b1) == {}
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def test_has():
|
| 112 |
+
assert b21.has(b1)
|
| 113 |
+
assert b21.has(b3, b1)
|
| 114 |
+
assert b21.has(Basic)
|
| 115 |
+
assert not b1.has(b21, b3)
|
| 116 |
+
assert not b21.has()
|
| 117 |
+
assert not b21.has(str)
|
| 118 |
+
assert not Symbol("x").has("x")
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def test_subs():
|
| 122 |
+
assert b21.subs(b2, b1) == Basic(b1, b1)
|
| 123 |
+
assert b21.subs(b2, b21) == Basic(b21, b1)
|
| 124 |
+
assert b3.subs(b2, b1) == b2
|
| 125 |
+
|
| 126 |
+
assert b21.subs([(b2, b1), (b1, b2)]) == Basic(b2, b2)
|
| 127 |
+
|
| 128 |
+
assert b21.subs({b1: b2, b2: b1}) == Basic(b2, b2)
|
| 129 |
+
assert b21.subs(collections.ChainMap({b1: b2}, {b2: b1})) == Basic(b2, b2)
|
| 130 |
+
assert b21.subs(collections.OrderedDict([(b2, b1), (b1, b2)])) == Basic(b2, b2)
|
| 131 |
+
|
| 132 |
+
raises(ValueError, lambda: b21.subs('bad arg'))
|
| 133 |
+
raises(ValueError, lambda: b21.subs(b1, b2, b3))
|
| 134 |
+
# dict(b1=foo) creates a string 'b1' but leaves foo unchanged; subs
|
| 135 |
+
# will convert the first to a symbol but will raise an error if foo
|
| 136 |
+
# cannot be sympified; sympification is strict if foo is not string
|
| 137 |
+
raises(ValueError, lambda: b21.subs(b1='bad arg'))
|
| 138 |
+
|
| 139 |
+
assert Symbol("text").subs({"text": b1}) == b1
|
| 140 |
+
assert Symbol("s").subs({"s": 1}) == 1
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def test_subs_with_unicode_symbols():
|
| 144 |
+
expr = Symbol('var1')
|
| 145 |
+
replaced = expr.subs('var1', 'x')
|
| 146 |
+
assert replaced.name == 'x'
|
| 147 |
+
|
| 148 |
+
replaced = expr.subs('var1', 'x')
|
| 149 |
+
assert replaced.name == 'x'
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
def test_atoms():
|
| 153 |
+
assert b21.atoms() == {Basic()}
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def test_free_symbols_empty():
|
| 157 |
+
assert b21.free_symbols == set()
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def test_doit():
|
| 161 |
+
assert b21.doit() == b21
|
| 162 |
+
assert b21.doit(deep=False) == b21
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def test_S():
|
| 166 |
+
assert repr(S) == 'S'
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
def test_xreplace():
|
| 170 |
+
assert b21.xreplace({b2: b1}) == Basic(b1, b1)
|
| 171 |
+
assert b21.xreplace({b2: b21}) == Basic(b21, b1)
|
| 172 |
+
assert b3.xreplace({b2: b1}) == b2
|
| 173 |
+
assert Basic(b1, b2).xreplace({b1: b2, b2: b1}) == Basic(b2, b1)
|
| 174 |
+
assert Atom(b1).xreplace({b1: b2}) == Atom(b1)
|
| 175 |
+
assert Atom(b1).xreplace({Atom(b1): b2}) == b2
|
| 176 |
+
raises(TypeError, lambda: b1.xreplace())
|
| 177 |
+
raises(TypeError, lambda: b1.xreplace([b1, b2]))
|
| 178 |
+
for f in (exp, Function('f')):
|
| 179 |
+
assert f.xreplace({}) == f
|
| 180 |
+
assert f.xreplace({}, hack2=True) == f
|
| 181 |
+
assert f.xreplace({f: b1}) == b1
|
| 182 |
+
assert f.xreplace({f: b1}, hack2=True) == b1
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def test_sorted_args():
|
| 186 |
+
x = symbols('x')
|
| 187 |
+
assert b21._sorted_args == b21.args
|
| 188 |
+
raises(AttributeError, lambda: x._sorted_args)
|
| 189 |
+
|
| 190 |
+
def test_call():
|
| 191 |
+
x, y = symbols('x y')
|
| 192 |
+
# See the long history of this in issues 5026 and 5105.
|
| 193 |
+
|
| 194 |
+
raises(TypeError, lambda: sin(x)({ x : 1, sin(x) : 2}))
|
| 195 |
+
raises(TypeError, lambda: sin(x)(1))
|
| 196 |
+
|
| 197 |
+
# No effect as there are no callables
|
| 198 |
+
assert sin(x).rcall(1) == sin(x)
|
| 199 |
+
assert (1 + sin(x)).rcall(1) == 1 + sin(x)
|
| 200 |
+
|
| 201 |
+
# Effect in the pressence of callables
|
| 202 |
+
l = Lambda(x, 2*x)
|
| 203 |
+
assert (l + x).rcall(y) == 2*y + x
|
| 204 |
+
assert (x**l).rcall(2) == x**4
|
| 205 |
+
# TODO UndefinedFunction does not subclass Expr
|
| 206 |
+
#f = Function('f')
|
| 207 |
+
#assert (2*f)(x) == 2*f(x)
|
| 208 |
+
|
| 209 |
+
assert (Q.real & Q.positive).rcall(x) == Q.real(x) & Q.positive(x)
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
def test_rewrite():
|
| 213 |
+
x, y, z = symbols('x y z')
|
| 214 |
+
a, b = symbols('a b')
|
| 215 |
+
f1 = sin(x) + cos(x)
|
| 216 |
+
assert f1.rewrite(cos,exp) == exp(I*x)/2 + sin(x) + exp(-I*x)/2
|
| 217 |
+
assert f1.rewrite([cos],sin) == sin(x) + sin(x + pi/2, evaluate=False)
|
| 218 |
+
f2 = sin(x) + cos(y)/gamma(z)
|
| 219 |
+
assert f2.rewrite(sin,exp) == -I*(exp(I*x) - exp(-I*x))/2 + cos(y)/gamma(z)
|
| 220 |
+
|
| 221 |
+
assert f1.rewrite() == f1
|
| 222 |
+
|
| 223 |
+
def test_literal_evalf_is_number_is_zero_is_comparable():
|
| 224 |
+
x = symbols('x')
|
| 225 |
+
f = Function('f')
|
| 226 |
+
|
| 227 |
+
# issue 5033
|
| 228 |
+
assert f.is_number is False
|
| 229 |
+
# issue 6646
|
| 230 |
+
assert f(1).is_number is False
|
| 231 |
+
i = Integral(0, (x, x, x))
|
| 232 |
+
# expressions that are symbolically 0 can be difficult to prove
|
| 233 |
+
# so in case there is some easy way to know if something is 0
|
| 234 |
+
# it should appear in the is_zero property for that object;
|
| 235 |
+
# if is_zero is true evalf should always be able to compute that
|
| 236 |
+
# zero
|
| 237 |
+
assert i.n() == 0
|
| 238 |
+
assert i.is_zero
|
| 239 |
+
assert i.is_number is False
|
| 240 |
+
assert i.evalf(2, strict=False) == 0
|
| 241 |
+
|
| 242 |
+
# issue 10268
|
| 243 |
+
n = sin(1)**2 + cos(1)**2 - 1
|
| 244 |
+
assert n.is_comparable is False
|
| 245 |
+
assert n.n(2).is_comparable is False
|
| 246 |
+
assert n.n(2).n(2).is_comparable
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
def test_as_Basic():
|
| 250 |
+
assert as_Basic(1) is S.One
|
| 251 |
+
assert as_Basic(()) == Tuple()
|
| 252 |
+
raises(TypeError, lambda: as_Basic([]))
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
def test_atomic():
|
| 256 |
+
g, h = map(Function, 'gh')
|
| 257 |
+
x = symbols('x')
|
| 258 |
+
assert _atomic(g(x + h(x))) == {g(x + h(x))}
|
| 259 |
+
assert _atomic(g(x + h(x)), recursive=True) == {h(x), x, g(x + h(x))}
|
| 260 |
+
assert _atomic(1) == set()
|
| 261 |
+
assert _atomic(Basic(S(1), S(2))) == set()
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
def test_as_dummy():
|
| 265 |
+
u, v, x, y, z, _0, _1 = symbols('u v x y z _0 _1')
|
| 266 |
+
assert Lambda(x, x + 1).as_dummy() == Lambda(_0, _0 + 1)
|
| 267 |
+
assert Lambda(x, x + _0).as_dummy() == Lambda(_1, _0 + _1)
|
| 268 |
+
eq = (1 + Sum(x, (x, 1, x)))
|
| 269 |
+
ans = 1 + Sum(_0, (_0, 1, x))
|
| 270 |
+
once = eq.as_dummy()
|
| 271 |
+
assert once == ans
|
| 272 |
+
twice = once.as_dummy()
|
| 273 |
+
assert twice == ans
|
| 274 |
+
assert Integral(x + _0, (x, x + 1), (_0, 1, 2)
|
| 275 |
+
).as_dummy() == Integral(_0 + _1, (_0, x + 1), (_1, 1, 2))
|
| 276 |
+
for T in (Symbol, Dummy):
|
| 277 |
+
d = T('x', real=True)
|
| 278 |
+
D = d.as_dummy()
|
| 279 |
+
assert D != d and D.func == Dummy and D.is_real is None
|
| 280 |
+
assert Dummy().as_dummy().is_commutative
|
| 281 |
+
assert Dummy(commutative=False).as_dummy().is_commutative is False
|
| 282 |
+
|
| 283 |
+
|
| 284 |
+
def test_canonical_variables():
|
| 285 |
+
x, i0, i1 = symbols('x _:2')
|
| 286 |
+
assert Integral(x, (x, x + 1)).canonical_variables == {x: i0}
|
| 287 |
+
assert Integral(x, (x, x + 1), (i0, 1, 2)).canonical_variables == {
|
| 288 |
+
x: i0, i0: i1}
|
| 289 |
+
assert Integral(x, (x, x + i0)).canonical_variables == {x: i1}
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
def test_replace_exceptions():
|
| 293 |
+
from sympy.core.symbol import Wild
|
| 294 |
+
x, y = symbols('x y')
|
| 295 |
+
e = (x**2 + x*y)
|
| 296 |
+
raises(TypeError, lambda: e.replace(sin, 2))
|
| 297 |
+
b = Wild('b')
|
| 298 |
+
c = Wild('c')
|
| 299 |
+
raises(TypeError, lambda: e.replace(b*c, c.is_real))
|
| 300 |
+
raises(TypeError, lambda: e.replace(b.is_real, 1))
|
| 301 |
+
raises(TypeError, lambda: e.replace(lambda d: d.is_Number, 1))
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
def test_ManagedProperties():
|
| 305 |
+
# ManagedProperties is now deprecated. Here we do our best to check that if
|
| 306 |
+
# someone is using it then it does work in the way that it previously did
|
| 307 |
+
# but gives a deprecation warning.
|
| 308 |
+
from sympy.core.assumptions import ManagedProperties
|
| 309 |
+
|
| 310 |
+
myclasses = []
|
| 311 |
+
|
| 312 |
+
class MyMeta(ManagedProperties):
|
| 313 |
+
def __init__(cls, *args, **kwargs):
|
| 314 |
+
myclasses.append('executed')
|
| 315 |
+
super().__init__(*args, **kwargs)
|
| 316 |
+
|
| 317 |
+
code = """
|
| 318 |
+
class MySubclass(Basic, metaclass=MyMeta):
|
| 319 |
+
pass
|
| 320 |
+
"""
|
| 321 |
+
with warns_deprecated_sympy():
|
| 322 |
+
exec(code)
|
| 323 |
+
|
| 324 |
+
assert myclasses == ['executed']
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
def test_generic():
|
| 328 |
+
# https://github.com/sympy/sympy/issues/25399
|
| 329 |
+
class A(Symbol, Generic[T]):
|
| 330 |
+
pass
|
| 331 |
+
|
| 332 |
+
class B(A[T]):
|
| 333 |
+
pass
|
.venv/lib/python3.11/site-packages/sympy/core/tests/test_cache.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
from sympy.core.cache import cacheit, cached_property, lazy_function
|
| 3 |
+
from sympy.testing.pytest import raises
|
| 4 |
+
|
| 5 |
+
def test_cacheit_doc():
|
| 6 |
+
@cacheit
|
| 7 |
+
def testfn():
|
| 8 |
+
"test docstring"
|
| 9 |
+
pass
|
| 10 |
+
|
| 11 |
+
assert testfn.__doc__ == "test docstring"
|
| 12 |
+
assert testfn.__name__ == "testfn"
|
| 13 |
+
|
| 14 |
+
def test_cacheit_unhashable():
|
| 15 |
+
@cacheit
|
| 16 |
+
def testit(x):
|
| 17 |
+
return x
|
| 18 |
+
|
| 19 |
+
assert testit(1) == 1
|
| 20 |
+
assert testit(1) == 1
|
| 21 |
+
a = {}
|
| 22 |
+
assert testit(a) == {}
|
| 23 |
+
a[1] = 2
|
| 24 |
+
assert testit(a) == {1: 2}
|
| 25 |
+
|
| 26 |
+
def test_cachit_exception():
|
| 27 |
+
# Make sure the cache doesn't call functions multiple times when they
|
| 28 |
+
# raise TypeError
|
| 29 |
+
|
| 30 |
+
a = []
|
| 31 |
+
|
| 32 |
+
@cacheit
|
| 33 |
+
def testf(x):
|
| 34 |
+
a.append(0)
|
| 35 |
+
raise TypeError
|
| 36 |
+
|
| 37 |
+
raises(TypeError, lambda: testf(1))
|
| 38 |
+
assert len(a) == 1
|
| 39 |
+
|
| 40 |
+
a.clear()
|
| 41 |
+
# Unhashable type
|
| 42 |
+
raises(TypeError, lambda: testf([]))
|
| 43 |
+
assert len(a) == 1
|
| 44 |
+
|
| 45 |
+
@cacheit
|
| 46 |
+
def testf2(x):
|
| 47 |
+
a.append(0)
|
| 48 |
+
raise TypeError("Error")
|
| 49 |
+
|
| 50 |
+
a.clear()
|
| 51 |
+
raises(TypeError, lambda: testf2(1))
|
| 52 |
+
assert len(a) == 1
|
| 53 |
+
|
| 54 |
+
a.clear()
|
| 55 |
+
# Unhashable type
|
| 56 |
+
raises(TypeError, lambda: testf2([]))
|
| 57 |
+
assert len(a) == 1
|
| 58 |
+
|
| 59 |
+
def test_cached_property():
|
| 60 |
+
class A:
|
| 61 |
+
def __init__(self, value):
|
| 62 |
+
self.value = value
|
| 63 |
+
self.calls = 0
|
| 64 |
+
|
| 65 |
+
@cached_property
|
| 66 |
+
def prop(self):
|
| 67 |
+
self.calls = self.calls + 1
|
| 68 |
+
return self.value
|
| 69 |
+
|
| 70 |
+
a = A(2)
|
| 71 |
+
assert a.calls == 0
|
| 72 |
+
assert a.prop == 2
|
| 73 |
+
assert a.calls == 1
|
| 74 |
+
assert a.prop == 2
|
| 75 |
+
assert a.calls == 1
|
| 76 |
+
b = A(None)
|
| 77 |
+
assert b.prop == None
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def test_lazy_function():
|
| 81 |
+
module_name='xmlrpc.client'
|
| 82 |
+
function_name = 'gzip_decode'
|
| 83 |
+
lazy = lazy_function(module_name, function_name)
|
| 84 |
+
assert lazy(b'') == b''
|
| 85 |
+
assert module_name in sys.modules
|
| 86 |
+
assert function_name in str(lazy)
|
| 87 |
+
repr_lazy = repr(lazy)
|
| 88 |
+
assert 'LazyFunction' in repr_lazy
|
| 89 |
+
assert function_name in repr_lazy
|
| 90 |
+
|
| 91 |
+
lazy = lazy_function('sympy.core.cache', 'cheap')
|
.venv/lib/python3.11/site-packages/sympy/core/tests/test_compatibility.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.testing.pytest import warns_deprecated_sympy
|
| 2 |
+
|
| 3 |
+
def test_compatibility_submodule():
|
| 4 |
+
# Test the sympy.core.compatibility deprecation warning
|
| 5 |
+
with warns_deprecated_sympy():
|
| 6 |
+
import sympy.core.compatibility # noqa:F401
|
.venv/lib/python3.11/site-packages/sympy/core/tests/test_complex.py
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.function import expand_complex
|
| 2 |
+
from sympy.core.numbers import (I, Integer, Rational, pi)
|
| 3 |
+
from sympy.core.power import Pow
|
| 4 |
+
from sympy.core.singleton import S
|
| 5 |
+
from sympy.core.symbol import (Symbol, symbols)
|
| 6 |
+
from sympy.functions.elementary.complexes import (Abs, conjugate, im, re, sign)
|
| 7 |
+
from sympy.functions.elementary.exponential import exp
|
| 8 |
+
from sympy.functions.elementary.hyperbolic import (cosh, coth, sinh, tanh)
|
| 9 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 10 |
+
from sympy.functions.elementary.trigonometric import (cos, cot, sin, tan)
|
| 11 |
+
|
| 12 |
+
def test_complex():
|
| 13 |
+
a = Symbol("a", real=True)
|
| 14 |
+
b = Symbol("b", real=True)
|
| 15 |
+
e = (a + I*b)*(a - I*b)
|
| 16 |
+
assert e.expand() == a**2 + b**2
|
| 17 |
+
assert sqrt(I) == Pow(I, S.Half)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def test_conjugate():
|
| 21 |
+
a = Symbol("a", real=True)
|
| 22 |
+
b = Symbol("b", real=True)
|
| 23 |
+
c = Symbol("c", imaginary=True)
|
| 24 |
+
d = Symbol("d", imaginary=True)
|
| 25 |
+
x = Symbol('x')
|
| 26 |
+
z = a + I*b + c + I*d
|
| 27 |
+
zc = a - I*b - c + I*d
|
| 28 |
+
assert conjugate(z) == zc
|
| 29 |
+
assert conjugate(exp(z)) == exp(zc)
|
| 30 |
+
assert conjugate(exp(I*x)) == exp(-I*conjugate(x))
|
| 31 |
+
assert conjugate(z**5) == zc**5
|
| 32 |
+
assert conjugate(abs(x)) == abs(x)
|
| 33 |
+
assert conjugate(sign(z)) == sign(zc)
|
| 34 |
+
assert conjugate(sin(z)) == sin(zc)
|
| 35 |
+
assert conjugate(cos(z)) == cos(zc)
|
| 36 |
+
assert conjugate(tan(z)) == tan(zc)
|
| 37 |
+
assert conjugate(cot(z)) == cot(zc)
|
| 38 |
+
assert conjugate(sinh(z)) == sinh(zc)
|
| 39 |
+
assert conjugate(cosh(z)) == cosh(zc)
|
| 40 |
+
assert conjugate(tanh(z)) == tanh(zc)
|
| 41 |
+
assert conjugate(coth(z)) == coth(zc)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def test_abs1():
|
| 45 |
+
a = Symbol("a", real=True)
|
| 46 |
+
b = Symbol("b", real=True)
|
| 47 |
+
assert abs(a) == Abs(a)
|
| 48 |
+
assert abs(-a) == abs(a)
|
| 49 |
+
assert abs(a + I*b) == sqrt(a**2 + b**2)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def test_abs2():
|
| 53 |
+
a = Symbol("a", real=False)
|
| 54 |
+
b = Symbol("b", real=False)
|
| 55 |
+
assert abs(a) != a
|
| 56 |
+
assert abs(-a) != a
|
| 57 |
+
assert abs(a + I*b) != sqrt(a**2 + b**2)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def test_evalc():
|
| 61 |
+
x = Symbol("x", real=True)
|
| 62 |
+
y = Symbol("y", real=True)
|
| 63 |
+
z = Symbol("z")
|
| 64 |
+
assert ((x + I*y)**2).expand(complex=True) == x**2 + 2*I*x*y - y**2
|
| 65 |
+
assert expand_complex(z**(2*I)) == (re((re(z) + I*im(z))**(2*I)) +
|
| 66 |
+
I*im((re(z) + I*im(z))**(2*I)))
|
| 67 |
+
assert expand_complex(
|
| 68 |
+
z**(2*I), deep=False) == I*im(z**(2*I)) + re(z**(2*I))
|
| 69 |
+
|
| 70 |
+
assert exp(I*x) != cos(x) + I*sin(x)
|
| 71 |
+
assert exp(I*x).expand(complex=True) == cos(x) + I*sin(x)
|
| 72 |
+
assert exp(I*x + y).expand(complex=True) == exp(y)*cos(x) + I*sin(x)*exp(y)
|
| 73 |
+
|
| 74 |
+
assert sin(I*x).expand(complex=True) == I * sinh(x)
|
| 75 |
+
assert sin(x + I*y).expand(complex=True) == sin(x)*cosh(y) + \
|
| 76 |
+
I * sinh(y) * cos(x)
|
| 77 |
+
|
| 78 |
+
assert cos(I*x).expand(complex=True) == cosh(x)
|
| 79 |
+
assert cos(x + I*y).expand(complex=True) == cos(x)*cosh(y) - \
|
| 80 |
+
I * sinh(y) * sin(x)
|
| 81 |
+
|
| 82 |
+
assert tan(I*x).expand(complex=True) == tanh(x) * I
|
| 83 |
+
assert tan(x + I*y).expand(complex=True) == (
|
| 84 |
+
sin(2*x)/(cos(2*x) + cosh(2*y)) +
|
| 85 |
+
I*sinh(2*y)/(cos(2*x) + cosh(2*y)))
|
| 86 |
+
|
| 87 |
+
assert sinh(I*x).expand(complex=True) == I * sin(x)
|
| 88 |
+
assert sinh(x + I*y).expand(complex=True) == sinh(x)*cos(y) + \
|
| 89 |
+
I * sin(y) * cosh(x)
|
| 90 |
+
|
| 91 |
+
assert cosh(I*x).expand(complex=True) == cos(x)
|
| 92 |
+
assert cosh(x + I*y).expand(complex=True) == cosh(x)*cos(y) + \
|
| 93 |
+
I * sin(y) * sinh(x)
|
| 94 |
+
|
| 95 |
+
assert tanh(I*x).expand(complex=True) == tan(x) * I
|
| 96 |
+
assert tanh(x + I*y).expand(complex=True) == (
|
| 97 |
+
(sinh(x)*cosh(x) + I*cos(y)*sin(y)) /
|
| 98 |
+
(sinh(x)**2 + cos(y)**2)).expand()
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def test_pythoncomplex():
|
| 102 |
+
x = Symbol("x")
|
| 103 |
+
assert 4j*x != 4*x*I
|
| 104 |
+
assert 4j*x == 4.0*x*I
|
| 105 |
+
assert 4.1j*x != 4*x*I
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def test_rootcomplex():
|
| 109 |
+
R = Rational
|
| 110 |
+
assert ((+1 + I)**R(1, 2)).expand(
|
| 111 |
+
complex=True) == 2**R(1, 4)*cos( pi/8) + 2**R(1, 4)*sin( pi/8)*I
|
| 112 |
+
assert ((-1 - I)**R(1, 2)).expand(
|
| 113 |
+
complex=True) == 2**R(1, 4)*cos(3*pi/8) - 2**R(1, 4)*sin(3*pi/8)*I
|
| 114 |
+
assert (sqrt(-10)*I).as_real_imag() == (-sqrt(10), 0)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def test_expand_inverse():
|
| 118 |
+
assert (1/(1 + I)).expand(complex=True) == (1 - I)/2
|
| 119 |
+
assert ((1 + 2*I)**(-2)).expand(complex=True) == (-3 - 4*I)/25
|
| 120 |
+
assert ((1 + I)**(-8)).expand(complex=True) == Rational(1, 16)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def test_expand_complex():
|
| 124 |
+
assert ((2 + 3*I)**10).expand(complex=True) == -341525 - 145668*I
|
| 125 |
+
# the following two tests are to ensure the SymPy uses an efficient
|
| 126 |
+
# algorithm for calculating powers of complex numbers. They should execute
|
| 127 |
+
# in something like 0.01s.
|
| 128 |
+
assert ((2 + 3*I)**1000).expand(complex=True) == \
|
| 129 |
+
-81079464736246615951519029367296227340216902563389546989376269312984127074385455204551402940331021387412262494620336565547972162814110386834027871072723273110439771695255662375718498785908345629702081336606863762777939617745464755635193139022811989314881997210583159045854968310911252660312523907616129080027594310008539817935736331124833163907518549408018652090650537035647520296539436440394920287688149200763245475036722326561143851304795139005599209239350981457301460233967137708519975586996623552182807311159141501424576682074392689622074945519232029999 + \
|
| 130 |
+
46938745946789557590804551905243206242164799136976022474337918748798900569942573265747576032611189047943842446167719177749107138603040963603119861476016947257034472364028585381714774667326478071264878108114128915685688115488744955550920239128462489496563930809677159214598114273887061533057125164518549173898349061972857446844052995037423459472376202251620778517659247970283904820245958198842631651569984310559418135975795868314764489884749573052997832686979294085577689571149679540256349988338406458116270429842222666345146926395233040564229555893248370000*I
|
| 131 |
+
assert ((2 + 3*I/4)**1000).expand(complex=True) == \
|
| 132 |
+
Integer(1)*37079892761199059751745775382463070250205990218394308874593455293485167797989691280095867197640410033222367257278387021789651672598831503296531725827158233077451476545928116965316544607115843772405184272449644892857783761260737279675075819921259597776770965829089907990486964515784097181964312256560561065607846661496055417619388874421218472707497847700629822858068783288579581649321248495739224020822198695759609598745114438265083593711851665996586461937988748911532242908776883696631067311443171682974330675406616373422505939887984366289623091300746049101284856530270685577940283077888955692921951247230006346681086274961362500646889925803654263491848309446197554307105991537357310209426736453173441104334496173618419659521888945605315751089087820455852582920963561495787655250624781448951403353654348109893478206364632640344111022531861683064175862889459084900614967785405977231549003280842218501570429860550379522498497412180001/114813069527425452423283320117768198402231770208869520047764273682576626139237031385665948631650626991844596463898746277344711896086305533142593135616665318539129989145312280000688779148240044871428926990063486244781615463646388363947317026040466353970904996558162398808944629605623311649536164221970332681344168908984458505602379484807914058900934776500429002716706625830522008132236281291761267883317206598995396418127021779858404042159853183251540889433902091920554957783589672039160081957216630582755380425583726015528348786419432054508915275783882625175435528800822842770817965453762184851149029376 + \
|
| 133 |
+
I*421638390580169706973991429333213477486930178424989246669892530737775352519112934278994501272111385966211392610029433824534634841747911783746811994443436271013377059560245191441549885048056920190833693041257216263519792201852046825443439142932464031501882145407459174948712992271510309541474392303461939389368955986650538525895866713074543004916049550090364398070215427272240155060576252568700906004691224321432509053286859100920489253598392100207663785243368195857086816912514025693453058403158416856847185079684216151337200057494966741268925263085619240941610301610538225414050394612058339070756009433535451561664522479191267503989904464718368605684297071150902631208673621618217106272361061676184840810762902463998065947687814692402219182668782278472952758690939877465065070481351343206840649517150634973307937551168752642148704904383991876969408056379195860410677814566225456558230131911142229028179902418223009651437985670625/1793954211366022694113801876840128100034871409513586250746316776290259783425578615401030447369541046747571819748417910583511123376348523955353017744010395602173906080395504375010762174191250701116076984219741972574712741619474818186676828531882286780795390571221287481389759837587864244524002565968286448146002639202882164150037179450123657170327105882819203167448541028601906377066191895183769810676831353109303069033234715310287563158747705988305326397404720186258671215368588625611876280581509852855552819149745718992630449787803625851701801184123166018366180137512856918294030710215034138299203584
|
| 134 |
+
assert ((2 + 3*I)**-1000).expand(complex=True) == \
|
| 135 |
+
Integer(1)*-81079464736246615951519029367296227340216902563389546989376269312984127074385455204551402940331021387412262494620336565547972162814110386834027871072723273110439771695255662375718498785908345629702081336606863762777939617745464755635193139022811989314881997210583159045854968310911252660312523907616129080027594310008539817935736331124833163907518549408018652090650537035647520296539436440394920287688149200763245475036722326561143851304795139005599209239350981457301460233967137708519975586996623552182807311159141501424576682074392689622074945519232029999/8777125472973511649630750050295188683351430110097915876250894978429797369155961290321829625004920141758416719066805645579710744290541337680113772670040386863849283653078324415471816788604945889094925784900885812724984087843737442111926413818245854362613018058774368703971604921858023116665586358870612944209398056562604561248859926344335598822815885851096698226775053153403320782439987679978321289537645645163767251396759519805603090332694449553371530571613352311006350058217982509738362083094920649452123351717366337410243853659113315547584871655479914439219520157174729130746351059075207407866012574386726064196992865627149566238044625779078186624347183905913357718850537058578084932880569701242598663149911276357125355850792073635533676541250531086757377369962506979378337216411188347761901006460813413505861461267545723590468627854202034450569581626648934062198718362303420281555886394558137408159453103395918783625713213314350531051312551733021627153081075080140680608080529736975658786227362251632725009435866547613598753584705455955419696609282059191031962604169242974038517575645939316377801594539335940001 - Integer(1)*46938745946789557590804551905243206242164799136976022474337918748798900569942573265747576032611189047943842446167719177749107138603040963603119861476016947257034472364028585381714774667326478071264878108114128915685688115488744955550920239128462489496563930809677159214598114273887061533057125164518549173898349061972857446844052995037423459472376202251620778517659247970283904820245958198842631651569984310559418135975795868314764489884749573052997832686979294085577689571149679540256349988338406458116270429842222666345146926395233040564229555893248370000*I/8777125472973511649630750050295188683351430110097915876250894978429797369155961290321829625004920141758416719066805645579710744290541337680113772670040386863849283653078324415471816788604945889094925784900885812724984087843737442111926413818245854362613018058774368703971604921858023116665586358870612944209398056562604561248859926344335598822815885851096698226775053153403320782439987679978321289537645645163767251396759519805603090332694449553371530571613352311006350058217982509738362083094920649452123351717366337410243853659113315547584871655479914439219520157174729130746351059075207407866012574386726064196992865627149566238044625779078186624347183905913357718850537058578084932880569701242598663149911276357125355850792073635533676541250531086757377369962506979378337216411188347761901006460813413505861461267545723590468627854202034450569581626648934062198718362303420281555886394558137408159453103395918783625713213314350531051312551733021627153081075080140680608080529736975658786227362251632725009435866547613598753584705455955419696609282059191031962604169242974038517575645939316377801594539335940001
|
| 136 |
+
assert ((2 + 3*I/4)**-1000).expand(complex=True) == \
|
| 137 |
+
Integer(1)*4257256305661027385394552848555894604806501409793288342610746813288539790051927148781268212212078237301273165351052934681382567968787279534591114913777456610214738290619922068269909423637926549603264174216950025398244509039145410016404821694746262142525173737175066432954496592560621330313807235750500564940782099283410261748370262433487444897446779072067625787246390824312580440138770014838135245148574339248259670887549732495841810961088930810608893772914812838358159009303794863047635845688453859317690488124382253918725010358589723156019888846606295866740117645571396817375322724096486161308083462637370825829567578309445855481578518239186117686659177284332344643124760453112513611749309168470605289172320376911472635805822082051716625171429727162039621902266619821870482519063133136820085579315127038372190224739238686708451840610064871885616258831386810233957438253532027049148030157164346719204500373766157143311767338973363806106967439378604898250533766359989107510507493549529158818602327525235240510049484816090584478644771183158342479140194633579061295740839490629457435283873180259847394582069479062820225159699506175855369539201399183443253793905149785994830358114153241481884290274629611529758663543080724574566578220908907477622643689220814376054314972190402285121776593824615083669045183404206291739005554569305329760211752815718335731118664756831942466773261465213581616104242113894521054475516019456867271362053692785300826523328020796670205463390909136593859765912483565093461468865534470710132881677639651348709376/2103100954337624833663208713697737151593634525061637972297915388685604042449504336765884978184588688426595940401280828953096857809292320006227881797146858511436638446932833617514351442216409828605662238790280753075176269765767010004889778647709740770757817960711900340755635772183674511158570690702969774966791073165467918123298694584729211212414462628433370481195120564586361368504153395406845170075275051749019600057116719726628746724489572189061061036426955163696859127711110719502594479795200686212257570291758725259007379710596548777812659422174199194837355646482046783616494013289495563083118517507178847555801163089723056310287760875135196081975602765511153122381201303871673391366630940702817360340900568748719988954847590748960761446218262344767250783946365392689256634180417145926390656439421745644011831124277463643383712803287985472471755648426749842410972650924240795946699346613614779460399530274263580007672855851663196114585312432954432654691485867618908420370875753749297487803461900447407917655296784879220450937110470920633595689721819488638484547259978337741496090602390463594556401615298457456112485536498177883358587125449801777718900375736758266215245325999241624148841915093787519330809347240990363802360596034171167818310322276373120180985148650099673289383722502488957717848531612020897298448601714154586319660314294591620415272119454982220034319689607295960162971300417552364254983071768070124456169427638371140064235083443242844616326538396503937972586505546495649094344512270582463639152160238137952390380581401171977159154009407415523525096743009110916334144716516647041176989758534635251844947906038080852185583742296318878233394998111078843229681030277039104786225656992262073797524057992347971177720807155842376332851559276430280477639539393920006008737472164850104411971830120295750221200029811143140323763349636629725073624360001 - Integer(1)*3098214262599218784594285246258841485430681674561917573155883806818465520660668045042109232930382494608383663464454841313154390741655282039877410154577448327874989496074260116195788919037407420625081798124301494353693248757853222257918294662198297114746822817460991242508743651430439120439020484502408313310689912381846149597061657483084652685283853595100434135149479564507015504022249330340259111426799121454516345905101620532787348293877485702600390665276070250119465888154331218827342488849948540687659846652377277250614246402784754153678374932540789808703029043827352976139228402417432199779415751301480406673762521987999573209628597459357964214510139892316208670927074795773830798600837815329291912002136924506221066071242281626618211060464126372574400100990746934953437169840312584285942093951405864225230033279614235191326102697164613004299868695519642598882914862568516635347204441042798206770888274175592401790040170576311989738272102077819127459014286741435419468254146418098278519775722104890854275995510700298782146199325790002255362719776098816136732897323406228294203133323296591166026338391813696715894870956511298793595675308998014158717167429941371979636895553724830981754579086664608880698350866487717403917070872269853194118364230971216854931998642990452908852258008095741042117326241406479532880476938937997238098399302185675832474590293188864060116934035867037219176916416481757918864533515526389079998129329045569609325290897577497835388451456680707076072624629697883854217331728051953671643278797380171857920000*I/2103100954337624833663208713697737151593634525061637972297915388685604042449504336765884978184588688426595940401280828953096857809292320006227881797146858511436638446932833617514351442216409828605662238790280753075176269765767010004889778647709740770757817960711900340755635772183674511158570690702969774966791073165467918123298694584729211212414462628433370481195120564586361368504153395406845170075275051749019600057116719726628746724489572189061061036426955163696859127711110719502594479795200686212257570291758725259007379710596548777812659422174199194837355646482046783616494013289495563083118517507178847555801163089723056310287760875135196081975602765511153122381201303871673391366630940702817360340900568748719988954847590748960761446218262344767250783946365392689256634180417145926390656439421745644011831124277463643383712803287985472471755648426749842410972650924240795946699346613614779460399530274263580007672855851663196114585312432954432654691485867618908420370875753749297487803461900447407917655296784879220450937110470920633595689721819488638484547259978337741496090602390463594556401615298457456112485536498177883358587125449801777718900375736758266215245325999241624148841915093787519330809347240990363802360596034171167818310322276373120180985148650099673289383722502488957717848531612020897298448601714154586319660314294591620415272119454982220034319689607295960162971300417552364254983071768070124456169427638371140064235083443242844616326538396503937972586505546495649094344512270582463639152160238137952390380581401171977159154009407415523525096743009110916334144716516647041176989758534635251844947906038080852185583742296318878233394998111078843229681030277039104786225656992262073797524057992347971177720807155842376332851559276430280477639539393920006008737472164850104411971830120295750221200029811143140323763349636629725073624360001
|
| 138 |
+
|
| 139 |
+
a = Symbol('a', real=True)
|
| 140 |
+
b = Symbol('b', real=True)
|
| 141 |
+
assert exp(a*(2 + I*b)).expand(complex=True) == \
|
| 142 |
+
I*exp(2*a)*sin(a*b) + exp(2*a)*cos(a*b)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def test_expand():
|
| 146 |
+
f = (16 - 2*sqrt(29))**2
|
| 147 |
+
assert f.expand() == 372 - 64*sqrt(29)
|
| 148 |
+
f = (Integer(1)/2 + I/2)**10
|
| 149 |
+
assert f.expand() == I/32
|
| 150 |
+
f = (Integer(1)/2 + I)**10
|
| 151 |
+
assert f.expand() == Integer(237)/1024 - 779*I/256
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def test_re_im1652():
|
| 155 |
+
x = Symbol('x')
|
| 156 |
+
assert re(x) == re(conjugate(x))
|
| 157 |
+
assert im(x) == - im(conjugate(x))
|
| 158 |
+
assert im(x)*re(conjugate(x)) + im(conjugate(x)) * re(x) == 0
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
def test_issue_5084():
|
| 162 |
+
x = Symbol('x')
|
| 163 |
+
assert ((x + x*I)/(1 + I)).as_real_imag() == (re((x + I*x)/(1 + I)
|
| 164 |
+
), im((x + I*x)/(1 + I)))
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
def test_issue_5236():
|
| 168 |
+
assert (cos(1 + I)**3).as_real_imag() == (-3*sin(1)**2*sinh(1)**2*cos(1)*cosh(1) +
|
| 169 |
+
cos(1)**3*cosh(1)**3, -3*cos(1)**2*cosh(1)**2*sin(1)*sinh(1) + sin(1)**3*sinh(1)**3)
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def test_real_imag():
|
| 173 |
+
x, y, z = symbols('x, y, z')
|
| 174 |
+
X, Y, Z = symbols('X, Y, Z', commutative=False)
|
| 175 |
+
a = Symbol('a', real=True)
|
| 176 |
+
assert (2*a*x).as_real_imag() == (2*a*re(x), 2*a*im(x))
|
| 177 |
+
|
| 178 |
+
# issue 5395:
|
| 179 |
+
assert (x*x.conjugate()).as_real_imag() == (Abs(x)**2, 0)
|
| 180 |
+
assert im(x*x.conjugate()) == 0
|
| 181 |
+
assert im(x*y.conjugate()*z*y) == im(x*z)*Abs(y)**2
|
| 182 |
+
assert im(x*y.conjugate()*x*y) == im(x**2)*Abs(y)**2
|
| 183 |
+
assert im(Z*y.conjugate()*X*y) == im(Z*X)*Abs(y)**2
|
| 184 |
+
assert im(X*X.conjugate()) == im(X*X.conjugate(), evaluate=False)
|
| 185 |
+
assert (sin(x)*sin(x).conjugate()).as_real_imag() == \
|
| 186 |
+
(Abs(sin(x))**2, 0)
|
| 187 |
+
|
| 188 |
+
# issue 6573:
|
| 189 |
+
assert (x**2).as_real_imag() == (re(x)**2 - im(x)**2, 2*re(x)*im(x))
|
| 190 |
+
|
| 191 |
+
# issue 6428:
|
| 192 |
+
r = Symbol('r', real=True)
|
| 193 |
+
i = Symbol('i', imaginary=True)
|
| 194 |
+
assert (i*r*x).as_real_imag() == (I*i*r*im(x), -I*i*r*re(x))
|
| 195 |
+
assert (i*r*x*(y + 2)).as_real_imag() == (
|
| 196 |
+
I*i*r*(re(y) + 2)*im(x) + I*i*r*re(x)*im(y),
|
| 197 |
+
-I*i*r*(re(y) + 2)*re(x) + I*i*r*im(x)*im(y))
|
| 198 |
+
|
| 199 |
+
# issue 7106:
|
| 200 |
+
assert ((1 + I)/(1 - I)).as_real_imag() == (0, 1)
|
| 201 |
+
assert ((1 + 2*I)*(1 + 3*I)).as_real_imag() == (-5, 5)
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def test_pow_issue_1724():
|
| 205 |
+
e = ((S.NegativeOne)**(S.One/3))
|
| 206 |
+
assert e.conjugate().n() == e.n().conjugate()
|
| 207 |
+
e = S('-2/3 - (-29/54 + sqrt(93)/18)**(1/3) - 1/(9*(-29/54 + sqrt(93)/18)**(1/3))')
|
| 208 |
+
assert e.conjugate().n() == e.n().conjugate()
|
| 209 |
+
e = 2**I
|
| 210 |
+
assert e.conjugate().n() == e.n().conjugate()
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def test_issue_5429():
|
| 214 |
+
assert sqrt(I).conjugate() != sqrt(I)
|
| 215 |
+
|
| 216 |
+
def test_issue_4124():
|
| 217 |
+
from sympy.core.numbers import oo
|
| 218 |
+
assert expand_complex(I*oo) == oo*I
|
| 219 |
+
|
| 220 |
+
def test_issue_11518():
|
| 221 |
+
x = Symbol("x", real=True)
|
| 222 |
+
y = Symbol("y", real=True)
|
| 223 |
+
r = sqrt(x**2 + y**2)
|
| 224 |
+
assert conjugate(r) == r
|
| 225 |
+
s = abs(x + I * y)
|
| 226 |
+
assert conjugate(s) == r
|
.venv/lib/python3.11/site-packages/sympy/core/tests/test_constructor_postprocessor.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.basic import Basic
|
| 2 |
+
from sympy.core.mul import Mul
|
| 3 |
+
from sympy.core.symbol import (Symbol, symbols)
|
| 4 |
+
|
| 5 |
+
from sympy.testing.pytest import XFAIL
|
| 6 |
+
|
| 7 |
+
class SymbolInMulOnce(Symbol):
|
| 8 |
+
# Test class for a symbol that can only appear once in a `Mul` expression.
|
| 9 |
+
pass
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
Basic._constructor_postprocessor_mapping[SymbolInMulOnce] = {
|
| 13 |
+
"Mul": [lambda x: x],
|
| 14 |
+
"Pow": [lambda x: x.base if isinstance(x.base, SymbolInMulOnce) else x],
|
| 15 |
+
"Add": [lambda x: x],
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _postprocess_SymbolRemovesOtherSymbols(expr):
|
| 20 |
+
args = tuple(i for i in expr.args if not isinstance(i, Symbol) or isinstance(i, SymbolRemovesOtherSymbols))
|
| 21 |
+
if args == expr.args:
|
| 22 |
+
return expr
|
| 23 |
+
return Mul.fromiter(args)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class SymbolRemovesOtherSymbols(Symbol):
|
| 27 |
+
# Test class for a symbol that removes other symbols in `Mul`.
|
| 28 |
+
pass
|
| 29 |
+
|
| 30 |
+
Basic._constructor_postprocessor_mapping[SymbolRemovesOtherSymbols] = {
|
| 31 |
+
"Mul": [_postprocess_SymbolRemovesOtherSymbols],
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
class SubclassSymbolInMulOnce(SymbolInMulOnce):
|
| 35 |
+
pass
|
| 36 |
+
|
| 37 |
+
class SubclassSymbolRemovesOtherSymbols(SymbolRemovesOtherSymbols):
|
| 38 |
+
pass
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def test_constructor_postprocessors1():
|
| 42 |
+
x = SymbolInMulOnce("x")
|
| 43 |
+
y = SymbolInMulOnce("y")
|
| 44 |
+
assert isinstance(3*x, Mul)
|
| 45 |
+
assert (3*x).args == (3, x)
|
| 46 |
+
assert x*x == x
|
| 47 |
+
assert 3*x*x == 3*x
|
| 48 |
+
assert 2*x*x + x == 3*x
|
| 49 |
+
assert x**3*y*y == x*y
|
| 50 |
+
assert x**5 + y*x**3 == x + x*y
|
| 51 |
+
|
| 52 |
+
w = SymbolRemovesOtherSymbols("w")
|
| 53 |
+
assert x*w == w
|
| 54 |
+
assert (3*w).args == (3, w)
|
| 55 |
+
assert set((w + x).args) == {x, w}
|
| 56 |
+
|
| 57 |
+
def test_constructor_postprocessors2():
|
| 58 |
+
x = SubclassSymbolInMulOnce("x")
|
| 59 |
+
y = SubclassSymbolInMulOnce("y")
|
| 60 |
+
assert isinstance(3*x, Mul)
|
| 61 |
+
assert (3*x).args == (3, x)
|
| 62 |
+
assert x*x == x
|
| 63 |
+
assert 3*x*x == 3*x
|
| 64 |
+
assert 2*x*x + x == 3*x
|
| 65 |
+
assert x**3*y*y == x*y
|
| 66 |
+
assert x**5 + y*x**3 == x + x*y
|
| 67 |
+
|
| 68 |
+
w = SubclassSymbolRemovesOtherSymbols("w")
|
| 69 |
+
assert x*w == w
|
| 70 |
+
assert (3*w).args == (3, w)
|
| 71 |
+
assert set((w + x).args) == {x, w}
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
@XFAIL
|
| 75 |
+
def test_subexpression_postprocessors():
|
| 76 |
+
# The postprocessors used to work with subexpressions, but the
|
| 77 |
+
# functionality was removed. See #15948.
|
| 78 |
+
a = symbols("a")
|
| 79 |
+
x = SymbolInMulOnce("x")
|
| 80 |
+
w = SymbolRemovesOtherSymbols("w")
|
| 81 |
+
assert 3*a*w**2 == 3*w**2
|
| 82 |
+
assert 3*a*x**3*w**2 == 3*w**2
|
| 83 |
+
|
| 84 |
+
x = SubclassSymbolInMulOnce("x")
|
| 85 |
+
w = SubclassSymbolRemovesOtherSymbols("w")
|
| 86 |
+
assert 3*a*w**2 == 3*w**2
|
| 87 |
+
assert 3*a*x**3*w**2 == 3*w**2
|
.venv/lib/python3.11/site-packages/sympy/core/tests/test_containers.py
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections import defaultdict
|
| 2 |
+
|
| 3 |
+
from sympy.core.basic import Basic
|
| 4 |
+
from sympy.core.containers import (Dict, Tuple)
|
| 5 |
+
from sympy.core.numbers import Integer
|
| 6 |
+
from sympy.core.kind import NumberKind
|
| 7 |
+
from sympy.matrices.kind import MatrixKind
|
| 8 |
+
from sympy.core.singleton import S
|
| 9 |
+
from sympy.core.symbol import symbols
|
| 10 |
+
from sympy.core.sympify import sympify
|
| 11 |
+
from sympy.matrices.dense import Matrix
|
| 12 |
+
from sympy.sets.sets import FiniteSet
|
| 13 |
+
from sympy.core.containers import tuple_wrapper, TupleKind
|
| 14 |
+
from sympy.core.expr import unchanged
|
| 15 |
+
from sympy.core.function import Function, Lambda
|
| 16 |
+
from sympy.core.relational import Eq
|
| 17 |
+
from sympy.testing.pytest import raises
|
| 18 |
+
from sympy.utilities.iterables import is_sequence, iterable
|
| 19 |
+
|
| 20 |
+
from sympy.abc import x, y
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def test_Tuple():
|
| 24 |
+
t = (1, 2, 3, 4)
|
| 25 |
+
st = Tuple(*t)
|
| 26 |
+
assert set(sympify(t)) == set(st)
|
| 27 |
+
assert len(t) == len(st)
|
| 28 |
+
assert set(sympify(t[:2])) == set(st[:2])
|
| 29 |
+
assert isinstance(st[:], Tuple)
|
| 30 |
+
assert st == Tuple(1, 2, 3, 4)
|
| 31 |
+
assert st.func(*st.args) == st
|
| 32 |
+
p, q, r, s = symbols('p q r s')
|
| 33 |
+
t2 = (p, q, r, s)
|
| 34 |
+
st2 = Tuple(*t2)
|
| 35 |
+
assert st2.atoms() == set(t2)
|
| 36 |
+
assert st == st2.subs({p: 1, q: 2, r: 3, s: 4})
|
| 37 |
+
# issue 5505
|
| 38 |
+
assert all(isinstance(arg, Basic) for arg in st.args)
|
| 39 |
+
assert Tuple(p, 1).subs(p, 0) == Tuple(0, 1)
|
| 40 |
+
assert Tuple(p, Tuple(p, 1)).subs(p, 0) == Tuple(0, Tuple(0, 1))
|
| 41 |
+
|
| 42 |
+
assert Tuple(t2) == Tuple(Tuple(*t2))
|
| 43 |
+
assert Tuple.fromiter(t2) == Tuple(*t2)
|
| 44 |
+
assert Tuple.fromiter(x for x in range(4)) == Tuple(0, 1, 2, 3)
|
| 45 |
+
assert st2.fromiter(st2.args) == st2
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def test_Tuple_contains():
|
| 49 |
+
t1, t2 = Tuple(1), Tuple(2)
|
| 50 |
+
assert t1 in Tuple(1, 2, 3, t1, Tuple(t2))
|
| 51 |
+
assert t2 not in Tuple(1, 2, 3, t1, Tuple(t2))
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def test_Tuple_concatenation():
|
| 55 |
+
assert Tuple(1, 2) + Tuple(3, 4) == Tuple(1, 2, 3, 4)
|
| 56 |
+
assert (1, 2) + Tuple(3, 4) == Tuple(1, 2, 3, 4)
|
| 57 |
+
assert Tuple(1, 2) + (3, 4) == Tuple(1, 2, 3, 4)
|
| 58 |
+
raises(TypeError, lambda: Tuple(1, 2) + 3)
|
| 59 |
+
raises(TypeError, lambda: 1 + Tuple(2, 3))
|
| 60 |
+
|
| 61 |
+
#the Tuple case in __radd__ is only reached when a subclass is involved
|
| 62 |
+
class Tuple2(Tuple):
|
| 63 |
+
def __radd__(self, other):
|
| 64 |
+
return Tuple.__radd__(self, other + other)
|
| 65 |
+
assert Tuple(1, 2) + Tuple2(3, 4) == Tuple(1, 2, 1, 2, 3, 4)
|
| 66 |
+
assert Tuple2(1, 2) + Tuple(3, 4) == Tuple(1, 2, 3, 4)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def test_Tuple_equality():
|
| 70 |
+
assert not isinstance(Tuple(1, 2), tuple)
|
| 71 |
+
assert (Tuple(1, 2) == (1, 2)) is True
|
| 72 |
+
assert (Tuple(1, 2) != (1, 2)) is False
|
| 73 |
+
assert (Tuple(1, 2) == (1, 3)) is False
|
| 74 |
+
assert (Tuple(1, 2) != (1, 3)) is True
|
| 75 |
+
assert (Tuple(1, 2) == Tuple(1, 2)) is True
|
| 76 |
+
assert (Tuple(1, 2) != Tuple(1, 2)) is False
|
| 77 |
+
assert (Tuple(1, 2) == Tuple(1, 3)) is False
|
| 78 |
+
assert (Tuple(1, 2) != Tuple(1, 3)) is True
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def test_Tuple_Eq():
|
| 82 |
+
assert Eq(Tuple(), Tuple()) is S.true
|
| 83 |
+
assert Eq(Tuple(1), 1) is S.false
|
| 84 |
+
assert Eq(Tuple(1, 2), Tuple(1)) is S.false
|
| 85 |
+
assert Eq(Tuple(1), Tuple(1)) is S.true
|
| 86 |
+
assert Eq(Tuple(1, 2), Tuple(1, 3)) is S.false
|
| 87 |
+
assert Eq(Tuple(1, 2), Tuple(1, 2)) is S.true
|
| 88 |
+
assert unchanged(Eq, Tuple(1, x), Tuple(1, 2))
|
| 89 |
+
assert Eq(Tuple(1, x), Tuple(1, 2)).subs(x, 2) is S.true
|
| 90 |
+
assert unchanged(Eq, Tuple(1, 2), x)
|
| 91 |
+
f = Function('f')
|
| 92 |
+
assert unchanged(Eq, Tuple(1), f(x))
|
| 93 |
+
assert Eq(Tuple(1), f(x)).subs(x, 1).subs(f, Lambda(y, (y,))) is S.true
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def test_Tuple_comparision():
|
| 97 |
+
assert (Tuple(1, 3) >= Tuple(-10, 30)) is S.true
|
| 98 |
+
assert (Tuple(1, 3) <= Tuple(-10, 30)) is S.false
|
| 99 |
+
assert (Tuple(1, 3) >= Tuple(1, 3)) is S.true
|
| 100 |
+
assert (Tuple(1, 3) <= Tuple(1, 3)) is S.true
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def test_Tuple_tuple_count():
|
| 104 |
+
assert Tuple(0, 1, 2, 3).tuple_count(4) == 0
|
| 105 |
+
assert Tuple(0, 4, 1, 2, 3).tuple_count(4) == 1
|
| 106 |
+
assert Tuple(0, 4, 1, 4, 2, 3).tuple_count(4) == 2
|
| 107 |
+
assert Tuple(0, 4, 1, 4, 2, 4, 3).tuple_count(4) == 3
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def test_Tuple_index():
|
| 111 |
+
assert Tuple(4, 0, 1, 2, 3).index(4) == 0
|
| 112 |
+
assert Tuple(0, 4, 1, 2, 3).index(4) == 1
|
| 113 |
+
assert Tuple(0, 1, 4, 2, 3).index(4) == 2
|
| 114 |
+
assert Tuple(0, 1, 2, 4, 3).index(4) == 3
|
| 115 |
+
assert Tuple(0, 1, 2, 3, 4).index(4) == 4
|
| 116 |
+
|
| 117 |
+
raises(ValueError, lambda: Tuple(0, 1, 2, 3).index(4))
|
| 118 |
+
raises(ValueError, lambda: Tuple(4, 0, 1, 2, 3).index(4, 1))
|
| 119 |
+
raises(ValueError, lambda: Tuple(0, 1, 2, 3, 4).index(4, 1, 4))
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def test_Tuple_mul():
|
| 123 |
+
assert Tuple(1, 2, 3)*2 == Tuple(1, 2, 3, 1, 2, 3)
|
| 124 |
+
assert 2*Tuple(1, 2, 3) == Tuple(1, 2, 3, 1, 2, 3)
|
| 125 |
+
assert Tuple(1, 2, 3)*Integer(2) == Tuple(1, 2, 3, 1, 2, 3)
|
| 126 |
+
assert Integer(2)*Tuple(1, 2, 3) == Tuple(1, 2, 3, 1, 2, 3)
|
| 127 |
+
|
| 128 |
+
raises(TypeError, lambda: Tuple(1, 2, 3)*S.Half)
|
| 129 |
+
raises(TypeError, lambda: S.Half*Tuple(1, 2, 3))
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def test_tuple_wrapper():
|
| 133 |
+
|
| 134 |
+
@tuple_wrapper
|
| 135 |
+
def wrap_tuples_and_return(*t):
|
| 136 |
+
return t
|
| 137 |
+
|
| 138 |
+
p = symbols('p')
|
| 139 |
+
assert wrap_tuples_and_return(p, 1) == (p, 1)
|
| 140 |
+
assert wrap_tuples_and_return((p, 1)) == (Tuple(p, 1),)
|
| 141 |
+
assert wrap_tuples_and_return(1, (p, 2), 3) == (1, Tuple(p, 2), 3)
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def test_iterable_is_sequence():
|
| 145 |
+
ordered = [[], (), Tuple(), Matrix([[]])]
|
| 146 |
+
unordered = [set()]
|
| 147 |
+
not_sympy_iterable = [{}, '', '']
|
| 148 |
+
assert all(is_sequence(i) for i in ordered)
|
| 149 |
+
assert all(not is_sequence(i) for i in unordered)
|
| 150 |
+
assert all(iterable(i) for i in ordered + unordered)
|
| 151 |
+
assert all(not iterable(i) for i in not_sympy_iterable)
|
| 152 |
+
assert all(iterable(i, exclude=None) for i in not_sympy_iterable)
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def test_TupleKind():
|
| 156 |
+
kind = TupleKind(NumberKind, MatrixKind(NumberKind))
|
| 157 |
+
assert Tuple(1, Matrix([1, 2])).kind is kind
|
| 158 |
+
assert Tuple(1, 2).kind is TupleKind(NumberKind, NumberKind)
|
| 159 |
+
assert Tuple(1, 2).kind.element_kind == (NumberKind, NumberKind)
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
def test_Dict():
|
| 163 |
+
x, y, z = symbols('x y z')
|
| 164 |
+
d = Dict({x: 1, y: 2, z: 3})
|
| 165 |
+
assert d[x] == 1
|
| 166 |
+
assert d[y] == 2
|
| 167 |
+
raises(KeyError, lambda: d[2])
|
| 168 |
+
raises(KeyError, lambda: d['2'])
|
| 169 |
+
assert len(d) == 3
|
| 170 |
+
assert set(d.keys()) == {x, y, z}
|
| 171 |
+
assert set(d.values()) == {S.One, S(2), S(3)}
|
| 172 |
+
assert d.get(5, 'default') == 'default'
|
| 173 |
+
assert d.get('5', 'default') == 'default'
|
| 174 |
+
assert x in d and z in d and 5 not in d and '5' not in d
|
| 175 |
+
assert d.has(x) and d.has(1) # SymPy Basic .has method
|
| 176 |
+
|
| 177 |
+
# Test input types
|
| 178 |
+
# input - a Python dict
|
| 179 |
+
# input - items as args - SymPy style
|
| 180 |
+
assert (Dict({x: 1, y: 2, z: 3}) ==
|
| 181 |
+
Dict((x, 1), (y, 2), (z, 3)))
|
| 182 |
+
|
| 183 |
+
raises(TypeError, lambda: Dict(((x, 1), (y, 2), (z, 3))))
|
| 184 |
+
with raises(NotImplementedError):
|
| 185 |
+
d[5] = 6 # assert immutability
|
| 186 |
+
|
| 187 |
+
assert set(
|
| 188 |
+
d.items()) == {Tuple(x, S.One), Tuple(y, S(2)), Tuple(z, S(3))}
|
| 189 |
+
assert set(d) == {x, y, z}
|
| 190 |
+
assert str(d) == '{x: 1, y: 2, z: 3}'
|
| 191 |
+
assert d.__repr__() == '{x: 1, y: 2, z: 3}'
|
| 192 |
+
|
| 193 |
+
# Test creating a Dict from a Dict.
|
| 194 |
+
d = Dict({x: 1, y: 2, z: 3})
|
| 195 |
+
assert d == Dict(d)
|
| 196 |
+
|
| 197 |
+
# Test for supporting defaultdict
|
| 198 |
+
d = defaultdict(int)
|
| 199 |
+
assert d[x] == 0
|
| 200 |
+
assert d[y] == 0
|
| 201 |
+
assert d[z] == 0
|
| 202 |
+
assert Dict(d)
|
| 203 |
+
d = Dict(d)
|
| 204 |
+
assert len(d) == 3
|
| 205 |
+
assert set(d.keys()) == {x, y, z}
|
| 206 |
+
assert set(d.values()) == {S.Zero, S.Zero, S.Zero}
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
def test_issue_5788():
|
| 210 |
+
args = [(1, 2), (2, 1)]
|
| 211 |
+
for o in [Dict, Tuple, FiniteSet]:
|
| 212 |
+
# __eq__ and arg handling
|
| 213 |
+
if o != Tuple:
|
| 214 |
+
assert o(*args) == o(*reversed(args))
|
| 215 |
+
pair = [o(*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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.concrete.summations import Sum
|
| 2 |
+
from sympy.core.basic import Basic
|
| 3 |
+
from sympy.core.function import (Derivative, Function, count_ops)
|
| 4 |
+
from sympy.core.numbers import (I, Rational, pi)
|
| 5 |
+
from sympy.core.relational import (Eq, Rel)
|
| 6 |
+
from sympy.core.singleton import S
|
| 7 |
+
from sympy.core.symbol import (Symbol, symbols)
|
| 8 |
+
from sympy.functions.elementary.exponential import exp
|
| 9 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
| 10 |
+
from sympy.integrals.integrals import Integral
|
| 11 |
+
from sympy.logic.boolalg import (And, Equivalent, ITE, Implies, Nand,
|
| 12 |
+
Nor, Not, Or, Xor)
|
| 13 |
+
from sympy.matrices.expressions.matexpr import MatrixSymbol
|
| 14 |
+
from sympy.core.containers import Tuple
|
| 15 |
+
|
| 16 |
+
x, y, z = symbols('x,y,z')
|
| 17 |
+
a, b, c = symbols('a,b,c')
|
| 18 |
+
|
| 19 |
+
def test_count_ops_non_visual():
|
| 20 |
+
def count(val):
|
| 21 |
+
return count_ops(val, visual=False)
|
| 22 |
+
assert count(x) == 0
|
| 23 |
+
assert count(x) is not S.Zero
|
| 24 |
+
assert count(x + y) == 1
|
| 25 |
+
assert count(x + y) is not S.One
|
| 26 |
+
assert count(x + y*x + 2*y) == 4
|
| 27 |
+
assert count({x + y: x}) == 1
|
| 28 |
+
assert count({x + y: S(2) + x}) is not S.One
|
| 29 |
+
assert count(x < y) == 1
|
| 30 |
+
assert count(Or(x,y)) == 1
|
| 31 |
+
assert count(And(x,y)) == 1
|
| 32 |
+
assert count(Not(x)) == 1
|
| 33 |
+
assert count(Nor(x,y)) == 2
|
| 34 |
+
assert count(Nand(x,y)) == 2
|
| 35 |
+
assert count(Xor(x,y)) == 1
|
| 36 |
+
assert count(Implies(x,y)) == 1
|
| 37 |
+
assert count(Equivalent(x,y)) == 1
|
| 38 |
+
assert count(ITE(x,y,z)) == 1
|
| 39 |
+
assert count(ITE(True,x,y)) == 0
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def test_count_ops_visual():
|
| 43 |
+
ADD, MUL, POW, SIN, COS, EXP, AND, D, G, M = symbols(
|
| 44 |
+
'Add Mul Pow sin cos exp And Derivative Integral Sum'.upper())
|
| 45 |
+
DIV, SUB, NEG = symbols('DIV SUB NEG')
|
| 46 |
+
LT, LE, GT, GE, EQ, NE = symbols('LT LE GT GE EQ NE')
|
| 47 |
+
NOT, OR, AND, XOR, IMPLIES, EQUIVALENT, _ITE, BASIC, TUPLE = symbols(
|
| 48 |
+
'Not Or And Xor Implies Equivalent ITE Basic Tuple'.upper())
|
| 49 |
+
|
| 50 |
+
def count(val):
|
| 51 |
+
return count_ops(val, visual=True)
|
| 52 |
+
|
| 53 |
+
assert count(7) is S.Zero
|
| 54 |
+
assert count(S(7)) is S.Zero
|
| 55 |
+
assert count(-1) == NEG
|
| 56 |
+
assert count(-2) == NEG
|
| 57 |
+
assert count(S(2)/3) == DIV
|
| 58 |
+
assert count(Rational(2, 3)) == DIV
|
| 59 |
+
assert count(pi/3) == DIV
|
| 60 |
+
assert count(-pi/3) == DIV + NEG
|
| 61 |
+
assert count(I - 1) == SUB
|
| 62 |
+
assert count(1 - I) == SUB
|
| 63 |
+
assert count(1 - 2*I) == SUB + MUL
|
| 64 |
+
|
| 65 |
+
assert count(x) is S.Zero
|
| 66 |
+
assert count(-x) == NEG
|
| 67 |
+
assert count(-2*x/3) == NEG + DIV + MUL
|
| 68 |
+
assert count(Rational(-2, 3)*x) == NEG + DIV + MUL
|
| 69 |
+
assert count(1/x) == DIV
|
| 70 |
+
assert count(1/(x*y)) == DIV + MUL
|
| 71 |
+
assert count(-1/x) == NEG + DIV
|
| 72 |
+
assert count(-2/x) == NEG + DIV
|
| 73 |
+
assert count(x/y) == DIV
|
| 74 |
+
assert count(-x/y) == NEG + DIV
|
| 75 |
+
|
| 76 |
+
assert count(x**2) == POW
|
| 77 |
+
assert count(-x**2) == POW + NEG
|
| 78 |
+
assert count(-2*x**2) == POW + MUL + NEG
|
| 79 |
+
|
| 80 |
+
assert count(x + pi/3) == ADD + DIV
|
| 81 |
+
assert count(x + S.One/3) == ADD + DIV
|
| 82 |
+
assert count(x + Rational(1, 3)) == ADD + DIV
|
| 83 |
+
assert count(x + y) == ADD
|
| 84 |
+
assert count(x - y) == SUB
|
| 85 |
+
assert count(y - x) == SUB
|
| 86 |
+
assert count(-1/(x - y)) == DIV + NEG + SUB
|
| 87 |
+
assert count(-1/(y - x)) == DIV + NEG + SUB
|
| 88 |
+
assert count(1 + x**y) == ADD + POW
|
| 89 |
+
assert count(1 + x + y) == 2*ADD
|
| 90 |
+
assert count(1 + x + y + z) == 3*ADD
|
| 91 |
+
assert count(1 + x**y + 2*x*y + y**2) == 3*ADD + 2*POW + 2*MUL
|
| 92 |
+
assert count(2*z + y + x + 1) == 3*ADD + MUL
|
| 93 |
+
assert count(2*z + y**17 + x + 1) == 3*ADD + MUL + POW
|
| 94 |
+
assert count(2*z + y**17 + x + sin(x)) == 3*ADD + POW + MUL + SIN
|
| 95 |
+
assert count(2*z + y**17 + x + sin(x**2)) == 3*ADD + MUL + 2*POW + SIN
|
| 96 |
+
assert count(2*z + y**17 + x + sin(
|
| 97 |
+
x**2) + exp(cos(x))) == 4*ADD + MUL + 2*POW + EXP + COS + SIN
|
| 98 |
+
|
| 99 |
+
assert count(Derivative(x, x)) == D
|
| 100 |
+
assert count(Integral(x, x) + 2*x/(1 + x)) == G + DIV + MUL + 2*ADD
|
| 101 |
+
assert count(Sum(x, (x, 1, x + 1)) + 2*x/(1 + x)) == M + DIV + MUL + 3*ADD
|
| 102 |
+
assert count(Basic()) is S.Zero
|
| 103 |
+
|
| 104 |
+
assert count({x + 1: sin(x)}) == ADD + SIN
|
| 105 |
+
assert count([x + 1, sin(x) + y, None]) == ADD + SIN + ADD
|
| 106 |
+
assert count({x + 1: sin(x), y: cos(x) + 1}) == SIN + COS + 2*ADD
|
| 107 |
+
assert count({}) is S.Zero
|
| 108 |
+
assert count([x + 1, sin(x)*y, None]) == SIN + ADD + MUL
|
| 109 |
+
assert count([]) is S.Zero
|
| 110 |
+
|
| 111 |
+
assert count(Basic()) == 0
|
| 112 |
+
assert count(Basic(Basic(),Basic(x,x+y))) == ADD + 2*BASIC
|
| 113 |
+
assert count(Basic(x, x + y)) == ADD + BASIC
|
| 114 |
+
assert [count(Rel(x, y, op)) for op in '< <= > >= == <> !='.split()
|
| 115 |
+
] == [LT, LE, GT, GE, EQ, NE, NE]
|
| 116 |
+
assert count(Or(x, y)) == OR
|
| 117 |
+
assert count(And(x, y)) == AND
|
| 118 |
+
assert count(Or(x, Or(y, And(z, a)))) == AND + OR
|
| 119 |
+
assert count(Nor(x, y)) == NOT + OR
|
| 120 |
+
assert count(Nand(x, y)) == NOT + AND
|
| 121 |
+
assert count(Xor(x, y)) == XOR
|
| 122 |
+
assert count(Implies(x, y)) == IMPLIES
|
| 123 |
+
assert count(Equivalent(x, y)) == EQUIVALENT
|
| 124 |
+
assert count(ITE(x, y, z)) == _ITE
|
| 125 |
+
assert count([Or(x, y), And(x, y), Basic(x + y)]
|
| 126 |
+
) == ADD + AND + BASIC + OR
|
| 127 |
+
|
| 128 |
+
assert count(Basic(Tuple(x))) == BASIC + TUPLE
|
| 129 |
+
#It checks that TUPLE is counted as an operation.
|
| 130 |
+
|
| 131 |
+
assert count(Eq(x + y, S(2))) == ADD + EQ
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def test_issue_9324():
|
| 135 |
+
def count(val):
|
| 136 |
+
return count_ops(val, visual=False)
|
| 137 |
+
|
| 138 |
+
M = MatrixSymbol('M', 10, 10)
|
| 139 |
+
assert count(M[0, 0]) == 0
|
| 140 |
+
assert count(2 * M[0, 0] + M[5, 7]) == 2
|
| 141 |
+
P = MatrixSymbol('P', 3, 3)
|
| 142 |
+
Q = MatrixSymbol('Q', 3, 3)
|
| 143 |
+
assert count(P + Q) == 1
|
| 144 |
+
m = Symbol('m', integer=True)
|
| 145 |
+
n = Symbol('n', integer=True)
|
| 146 |
+
M = MatrixSymbol('M', m + n, m * m)
|
| 147 |
+
assert count(M[0, 1]) == 2
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def test_issue_21532():
|
| 151 |
+
f = Function('f')
|
| 152 |
+
g = Function('g')
|
| 153 |
+
FUNC_F, FUNC_G = symbols('FUNC_F, FUNC_G')
|
| 154 |
+
assert f(x).count_ops(visual=True) == FUNC_F
|
| 155 |
+
assert g(x).count_ops(visual=True) == FUNC_G
|
.venv/lib/python3.11/site-packages/sympy/core/tests/test_diff.py
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.concrete.summations import Sum
|
| 2 |
+
from sympy.core.expr import Expr
|
| 3 |
+
from sympy.core.function import (Derivative, Function, diff, Subs)
|
| 4 |
+
from sympy.core.numbers import (I, Rational, pi)
|
| 5 |
+
from sympy.core.relational import Eq
|
| 6 |
+
from sympy.core.singleton import S
|
| 7 |
+
from sympy.core.symbol import Symbol
|
| 8 |
+
from sympy.functions.combinatorial.factorials import factorial
|
| 9 |
+
from sympy.functions.elementary.complexes import (im, re)
|
| 10 |
+
from sympy.functions.elementary.exponential import (exp, log)
|
| 11 |
+
from sympy.functions.elementary.miscellaneous import Max
|
| 12 |
+
from sympy.functions.elementary.piecewise import Piecewise
|
| 13 |
+
from sympy.functions.elementary.trigonometric import (cos, cot, sin, tan)
|
| 14 |
+
from sympy.tensor.array.ndim_array import NDimArray
|
| 15 |
+
from sympy.testing.pytest import raises
|
| 16 |
+
from sympy.abc import a, b, c, x, y, z
|
| 17 |
+
|
| 18 |
+
def test_diff():
|
| 19 |
+
assert Rational(1, 3).diff(x) is S.Zero
|
| 20 |
+
assert I.diff(x) is S.Zero
|
| 21 |
+
assert pi.diff(x) is S.Zero
|
| 22 |
+
assert x.diff(x, 0) == x
|
| 23 |
+
assert (x**2).diff(x, 2, x) == 0
|
| 24 |
+
assert (x**2).diff((x, 2), x) == 0
|
| 25 |
+
assert (x**2).diff((x, 1), x) == 2
|
| 26 |
+
assert (x**2).diff((x, 1), (x, 1)) == 2
|
| 27 |
+
assert (x**2).diff((x, 2)) == 2
|
| 28 |
+
assert (x**2).diff(x, y, 0) == 2*x
|
| 29 |
+
assert (x**2).diff(x, (y, 0)) == 2*x
|
| 30 |
+
assert (x**2).diff(x, y) == 0
|
| 31 |
+
raises(ValueError, lambda: x.diff(1, x))
|
| 32 |
+
|
| 33 |
+
p = Rational(5)
|
| 34 |
+
e = a*b + b**p
|
| 35 |
+
assert e.diff(a) == b
|
| 36 |
+
assert e.diff(b) == a + 5*b**4
|
| 37 |
+
assert e.diff(b).diff(a) == Rational(1)
|
| 38 |
+
e = a*(b + c)
|
| 39 |
+
assert e.diff(a) == b + c
|
| 40 |
+
assert e.diff(b) == a
|
| 41 |
+
assert e.diff(b).diff(a) == Rational(1)
|
| 42 |
+
e = c**p
|
| 43 |
+
assert e.diff(c, 6) == Rational(0)
|
| 44 |
+
assert e.diff(c, 5) == Rational(120)
|
| 45 |
+
e = c**Rational(2)
|
| 46 |
+
assert e.diff(c) == 2*c
|
| 47 |
+
e = a*b*c
|
| 48 |
+
assert e.diff(c) == a*b
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def test_diff2():
|
| 52 |
+
n3 = Rational(3)
|
| 53 |
+
n2 = Rational(2)
|
| 54 |
+
n6 = Rational(6)
|
| 55 |
+
|
| 56 |
+
e = n3*(-n2 + x**n2)*cos(x) + x*(-n6 + x**n2)*sin(x)
|
| 57 |
+
assert e == 3*(-2 + x**2)*cos(x) + x*(-6 + x**2)*sin(x)
|
| 58 |
+
assert e.diff(x).expand() == x**3*cos(x)
|
| 59 |
+
|
| 60 |
+
e = (x + 1)**3
|
| 61 |
+
assert e.diff(x) == 3*(x + 1)**2
|
| 62 |
+
e = x*(x + 1)**3
|
| 63 |
+
assert e.diff(x) == (x + 1)**3 + 3*x*(x + 1)**2
|
| 64 |
+
e = 2*exp(x*x)*x
|
| 65 |
+
assert e.diff(x) == 2*exp(x**2) + 4*x**2*exp(x**2)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def test_diff3():
|
| 69 |
+
p = Rational(5)
|
| 70 |
+
e = a*b + sin(b**p)
|
| 71 |
+
assert e == a*b + sin(b**5)
|
| 72 |
+
assert e.diff(a) == b
|
| 73 |
+
assert e.diff(b) == a + 5*b**4*cos(b**5)
|
| 74 |
+
e = tan(c)
|
| 75 |
+
assert e == tan(c)
|
| 76 |
+
assert e.diff(c) in [cos(c)**(-2), 1 + sin(c)**2/cos(c)**2, 1 + tan(c)**2]
|
| 77 |
+
e = c*log(c) - c
|
| 78 |
+
assert e == -c + c*log(c)
|
| 79 |
+
assert e.diff(c) == log(c)
|
| 80 |
+
e = log(sin(c))
|
| 81 |
+
assert e == log(sin(c))
|
| 82 |
+
assert e.diff(c) in [sin(c)**(-1)*cos(c), cot(c)]
|
| 83 |
+
e = (Rational(2)**a/log(Rational(2)))
|
| 84 |
+
assert e == 2**a*log(Rational(2))**(-1)
|
| 85 |
+
assert e.diff(a) == 2**a
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def test_diff_no_eval_derivative():
|
| 89 |
+
class My(Expr):
|
| 90 |
+
def __new__(cls, x):
|
| 91 |
+
return Expr.__new__(cls, x)
|
| 92 |
+
|
| 93 |
+
# My doesn't have its own _eval_derivative method
|
| 94 |
+
assert My(x).diff(x).func is Derivative
|
| 95 |
+
assert My(x).diff(x, 3).func is Derivative
|
| 96 |
+
assert re(x).diff(x, 2) == Derivative(re(x), (x, 2)) # issue 15518
|
| 97 |
+
assert diff(NDimArray([re(x), im(x)]), (x, 2)) == NDimArray(
|
| 98 |
+
[Derivative(re(x), (x, 2)), Derivative(im(x), (x, 2))])
|
| 99 |
+
# it doesn't have y so it shouldn't need a method for this case
|
| 100 |
+
assert My(x).diff(y) == 0
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def test_speed():
|
| 104 |
+
# this should return in 0.0s. If it takes forever, it's wrong.
|
| 105 |
+
assert x.diff(x, 10**8) == 0
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def test_deriv_noncommutative():
|
| 109 |
+
A = Symbol("A", commutative=False)
|
| 110 |
+
f = Function("f")
|
| 111 |
+
assert A*f(x)*A == f(x)*A**2
|
| 112 |
+
assert A*f(x).diff(x)*A == f(x).diff(x) * A**2
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def test_diff_nth_derivative():
|
| 116 |
+
f = Function("f")
|
| 117 |
+
n = Symbol("n", integer=True)
|
| 118 |
+
|
| 119 |
+
expr = diff(sin(x), (x, n))
|
| 120 |
+
expr2 = diff(f(x), (x, 2))
|
| 121 |
+
expr3 = diff(f(x), (x, n))
|
| 122 |
+
|
| 123 |
+
assert expr.subs(sin(x), cos(-x)) == Derivative(cos(-x), (x, n))
|
| 124 |
+
assert expr.subs(n, 1).doit() == cos(x)
|
| 125 |
+
assert expr.subs(n, 2).doit() == -sin(x)
|
| 126 |
+
|
| 127 |
+
assert expr2.subs(Derivative(f(x), x), y) == Derivative(y, x)
|
| 128 |
+
# Currently not supported (cannot determine if `n > 1`):
|
| 129 |
+
#assert expr3.subs(Derivative(f(x), x), y) == Derivative(y, (x, n-1))
|
| 130 |
+
assert expr3 == Derivative(f(x), (x, n))
|
| 131 |
+
|
| 132 |
+
assert diff(x, (x, n)) == Piecewise((x, Eq(n, 0)), (1, Eq(n, 1)), (0, True))
|
| 133 |
+
assert diff(2*x, (x, n)).dummy_eq(
|
| 134 |
+
Sum(Piecewise((2*x*factorial(n)/(factorial(y)*factorial(-y + n)),
|
| 135 |
+
Eq(y, 0) & Eq(Max(0, -y + n), 0)),
|
| 136 |
+
(2*factorial(n)/(factorial(y)*factorial(-y + n)), Eq(y, 0) & Eq(Max(0,
|
| 137 |
+
-y + n), 1)), (0, True)), (y, 0, n)))
|
| 138 |
+
# TODO: assert diff(x**2, (x, n)) == x**(2-n)*ff(2, n)
|
| 139 |
+
exprm = x*sin(x)
|
| 140 |
+
mul_diff = diff(exprm, (x, n))
|
| 141 |
+
assert isinstance(mul_diff, Sum)
|
| 142 |
+
for i in range(5):
|
| 143 |
+
assert mul_diff.subs(n, i).doit() == exprm.diff((x, i)).expand()
|
| 144 |
+
|
| 145 |
+
exprm2 = 2*y*x*sin(x)*cos(x)*log(x)*exp(x)
|
| 146 |
+
dex = exprm2.diff((x, n))
|
| 147 |
+
assert isinstance(dex, Sum)
|
| 148 |
+
for i in range(7):
|
| 149 |
+
assert dex.subs(n, i).doit().expand() == \
|
| 150 |
+
exprm2.diff((x, i)).expand()
|
| 151 |
+
|
| 152 |
+
assert (cos(x)*sin(y)).diff([[x, y, z]]) == NDimArray([
|
| 153 |
+
-sin(x)*sin(y), cos(x)*cos(y), 0])
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def test_issue_16160():
|
| 157 |
+
assert Derivative(x**3, (x, x)).subs(x, 2) == Subs(
|
| 158 |
+
Derivative(x**3, (x, 2)), x, 2)
|
| 159 |
+
assert Derivative(1 + x**3, (x, x)).subs(x, 0
|
| 160 |
+
) == Derivative(1 + y**3, (y, 0)).subs(y, 0)
|
.venv/lib/python3.11/site-packages/sympy/core/tests/test_equal.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.numbers import Rational
|
| 2 |
+
from sympy.core.symbol import (Dummy, Symbol)
|
| 3 |
+
from sympy.functions.elementary.exponential import exp
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def test_equal():
|
| 7 |
+
b = Symbol("b")
|
| 8 |
+
a = Symbol("a")
|
| 9 |
+
e1 = a + b
|
| 10 |
+
e2 = 2*a*b
|
| 11 |
+
e3 = a**3*b**2
|
| 12 |
+
e4 = a*b + b*a
|
| 13 |
+
assert not e1 == e2
|
| 14 |
+
assert not e1 == e2
|
| 15 |
+
assert e1 != e2
|
| 16 |
+
assert e2 == e4
|
| 17 |
+
assert e2 != e3
|
| 18 |
+
assert not e2 == e3
|
| 19 |
+
|
| 20 |
+
x = Symbol("x")
|
| 21 |
+
e1 = exp(x + 1/x)
|
| 22 |
+
y = Symbol("x")
|
| 23 |
+
e2 = exp(y + 1/y)
|
| 24 |
+
assert e1 == e2
|
| 25 |
+
assert not e1 != e2
|
| 26 |
+
y = Symbol("y")
|
| 27 |
+
e2 = exp(y + 1/y)
|
| 28 |
+
assert not e1 == e2
|
| 29 |
+
assert e1 != e2
|
| 30 |
+
|
| 31 |
+
e5 = Rational(3) + 2*x - x - x
|
| 32 |
+
assert e5 == 3
|
| 33 |
+
assert 3 == e5
|
| 34 |
+
assert e5 != 4
|
| 35 |
+
assert 4 != e5
|
| 36 |
+
assert e5 != 3 + x
|
| 37 |
+
assert 3 + x != e5
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def test_expevalbug():
|
| 41 |
+
x = Symbol("x")
|
| 42 |
+
e1 = exp(1*x)
|
| 43 |
+
e3 = exp(x)
|
| 44 |
+
assert e1 == e3
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def test_cmp_bug1():
|
| 48 |
+
class T:
|
| 49 |
+
pass
|
| 50 |
+
|
| 51 |
+
t = T()
|
| 52 |
+
x = Symbol("x")
|
| 53 |
+
|
| 54 |
+
assert not (x == t)
|
| 55 |
+
assert (x != t)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def test_cmp_bug2():
|
| 59 |
+
class T:
|
| 60 |
+
pass
|
| 61 |
+
|
| 62 |
+
t = T()
|
| 63 |
+
|
| 64 |
+
assert not (Symbol == t)
|
| 65 |
+
assert (Symbol != t)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def test_cmp_issue_4357():
|
| 69 |
+
""" Check that Basic subclasses can be compared with sympifiable objects.
|
| 70 |
+
|
| 71 |
+
https://github.com/sympy/sympy/issues/4357
|
| 72 |
+
"""
|
| 73 |
+
assert not (Symbol == 1)
|
| 74 |
+
assert (Symbol != 1)
|
| 75 |
+
assert not (Symbol == 'x')
|
| 76 |
+
assert (Symbol != 'x')
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def test_dummy_eq():
|
| 80 |
+
x = Symbol('x')
|
| 81 |
+
y = Symbol('y')
|
| 82 |
+
|
| 83 |
+
u = Dummy('u')
|
| 84 |
+
|
| 85 |
+
assert (u**2 + 1).dummy_eq(x**2 + 1) is True
|
| 86 |
+
assert ((u**2 + 1) == (x**2 + 1)) is False
|
| 87 |
+
|
| 88 |
+
assert (u**2 + y).dummy_eq(x**2 + y, x) is True
|
| 89 |
+
assert (u**2 + y).dummy_eq(x**2 + y, y) is False
|
.venv/lib/python3.11/site-packages/sympy/core/tests/test_eval.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.function import Function
|
| 2 |
+
from sympy.core.numbers import (I, Rational)
|
| 3 |
+
from sympy.core.singleton import S
|
| 4 |
+
from sympy.core.symbol import Symbol
|
| 5 |
+
from sympy.functions.elementary.exponential import exp
|
| 6 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 7 |
+
from sympy.functions.elementary.trigonometric import (cos, tan)
|
| 8 |
+
from sympy.testing.pytest import XFAIL
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def test_add_eval():
|
| 12 |
+
a = Symbol("a")
|
| 13 |
+
b = Symbol("b")
|
| 14 |
+
c = Rational(1)
|
| 15 |
+
p = Rational(5)
|
| 16 |
+
assert a*b + c + p == a*b + 6
|
| 17 |
+
assert c + a + p == a + 6
|
| 18 |
+
assert c + a - p == a + (-4)
|
| 19 |
+
assert a + a == 2*a
|
| 20 |
+
assert a + p + a == 2*a + 5
|
| 21 |
+
assert c + p == Rational(6)
|
| 22 |
+
assert b + a - b == a
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_addmul_eval():
|
| 26 |
+
a = Symbol("a")
|
| 27 |
+
b = Symbol("b")
|
| 28 |
+
c = Rational(1)
|
| 29 |
+
p = Rational(5)
|
| 30 |
+
assert c + a + b*c + a - p == 2*a + b + (-4)
|
| 31 |
+
assert a*2 + p + a == a*2 + 5 + a
|
| 32 |
+
assert a*2 + p + a == 3*a + 5
|
| 33 |
+
assert a*2 + a == 3*a
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def test_pow_eval():
|
| 37 |
+
# XXX Pow does not fully support conversion of negative numbers
|
| 38 |
+
# to their complex equivalent
|
| 39 |
+
|
| 40 |
+
assert sqrt(-1) == I
|
| 41 |
+
|
| 42 |
+
assert sqrt(-4) == 2*I
|
| 43 |
+
assert sqrt( 4) == 2
|
| 44 |
+
assert (8)**Rational(1, 3) == 2
|
| 45 |
+
assert (-8)**Rational(1, 3) == 2*((-1)**Rational(1, 3))
|
| 46 |
+
|
| 47 |
+
assert sqrt(-2) == I*sqrt(2)
|
| 48 |
+
assert (-1)**Rational(1, 3) != I
|
| 49 |
+
assert (-10)**Rational(1, 3) != I*((10)**Rational(1, 3))
|
| 50 |
+
assert (-2)**Rational(1, 4) != (2)**Rational(1, 4)
|
| 51 |
+
|
| 52 |
+
assert 64**Rational(1, 3) == 4
|
| 53 |
+
assert 64**Rational(2, 3) == 16
|
| 54 |
+
assert 24/sqrt(64) == 3
|
| 55 |
+
assert (-27)**Rational(1, 3) == 3*(-1)**Rational(1, 3)
|
| 56 |
+
|
| 57 |
+
assert (cos(2) / tan(2))**2 == (cos(2) / tan(2))**2
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
@XFAIL
|
| 61 |
+
def test_pow_eval_X1():
|
| 62 |
+
assert (-1)**Rational(1, 3) == S.Half + S.Half*I*sqrt(3)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def test_mulpow_eval():
|
| 66 |
+
x = Symbol('x')
|
| 67 |
+
assert sqrt(50)/(sqrt(2)*x) == 5/x
|
| 68 |
+
assert sqrt(27)/sqrt(3) == 3
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def test_evalpow_bug():
|
| 72 |
+
x = Symbol("x")
|
| 73 |
+
assert 1/(1/x) == x
|
| 74 |
+
assert 1/(-1/x) == -x
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def test_symbol_expand():
|
| 78 |
+
x = Symbol('x')
|
| 79 |
+
y = Symbol('y')
|
| 80 |
+
|
| 81 |
+
f = x**4*y**4
|
| 82 |
+
assert f == x**4*y**4
|
| 83 |
+
assert f == f.expand()
|
| 84 |
+
|
| 85 |
+
g = (x*y)**4
|
| 86 |
+
assert g == f
|
| 87 |
+
assert g.expand() == f
|
| 88 |
+
assert g.expand() == g.expand().expand()
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def test_function():
|
| 92 |
+
f, l = map(Function, 'fl')
|
| 93 |
+
x = Symbol('x')
|
| 94 |
+
assert exp(l(x))*l(x)/exp(l(x)) == l(x)
|
| 95 |
+
assert exp(f(x))*f(x)/exp(f(x)) == f(x)
|
.venv/lib/python3.11/site-packages/sympy/core/tests/test_evalf.py
ADDED
|
@@ -0,0 +1,734 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
|
| 3 |
+
from sympy.concrete.products import (Product, product)
|
| 4 |
+
from sympy.concrete.summations import Sum
|
| 5 |
+
from sympy.core.add import Add
|
| 6 |
+
from sympy.core.evalf import N
|
| 7 |
+
from sympy.core.function import (Function, nfloat)
|
| 8 |
+
from sympy.core.mul import Mul
|
| 9 |
+
from sympy.core import (GoldenRatio)
|
| 10 |
+
from sympy.core.numbers import (AlgebraicNumber, E, Float, I, Rational,
|
| 11 |
+
oo, zoo, nan, pi)
|
| 12 |
+
from sympy.core.power import Pow
|
| 13 |
+
from sympy.core.relational import Eq
|
| 14 |
+
from sympy.core.singleton import S
|
| 15 |
+
from sympy.core.symbol import Symbol
|
| 16 |
+
from sympy.core.sympify import sympify
|
| 17 |
+
from sympy.functions.combinatorial.factorials import factorial
|
| 18 |
+
from sympy.functions.combinatorial.numbers import fibonacci
|
| 19 |
+
from sympy.functions.elementary.complexes import (Abs, re, im)
|
| 20 |
+
from sympy.functions.elementary.exponential import (exp, log)
|
| 21 |
+
from sympy.functions.elementary.hyperbolic import (acosh, cosh)
|
| 22 |
+
from sympy.functions.elementary.integers import (ceiling, floor)
|
| 23 |
+
from sympy.functions.elementary.miscellaneous import (Max, sqrt)
|
| 24 |
+
from sympy.functions.elementary.trigonometric import (acos, atan, cos, sin, tan)
|
| 25 |
+
from sympy.integrals.integrals import (Integral, integrate)
|
| 26 |
+
from sympy.polys.polytools import factor
|
| 27 |
+
from sympy.polys.rootoftools import CRootOf
|
| 28 |
+
from sympy.polys.specialpolys import cyclotomic_poly
|
| 29 |
+
from sympy.printing import srepr
|
| 30 |
+
from sympy.printing.str import sstr
|
| 31 |
+
from sympy.simplify.simplify import simplify
|
| 32 |
+
from sympy.core.numbers import comp
|
| 33 |
+
from sympy.core.evalf import (complex_accuracy, PrecisionExhausted,
|
| 34 |
+
scaled_zero, get_integer_part, as_mpmath, evalf, _evalf_with_bounded_error)
|
| 35 |
+
from mpmath import inf, ninf, make_mpc
|
| 36 |
+
from mpmath.libmp.libmpf import from_float, fzero
|
| 37 |
+
from sympy.core.expr import unchanged
|
| 38 |
+
from sympy.testing.pytest import raises, XFAIL
|
| 39 |
+
from sympy.abc import n, x, y
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def NS(e, n=15, **options):
|
| 43 |
+
return sstr(sympify(e).evalf(n, **options), full_prec=True)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def test_evalf_helpers():
|
| 47 |
+
from mpmath.libmp import finf
|
| 48 |
+
assert complex_accuracy((from_float(2.0), None, 35, None)) == 35
|
| 49 |
+
assert complex_accuracy((from_float(2.0), from_float(10.0), 35, 100)) == 37
|
| 50 |
+
assert complex_accuracy(
|
| 51 |
+
(from_float(2.0), from_float(1000.0), 35, 100)) == 43
|
| 52 |
+
assert complex_accuracy((from_float(2.0), from_float(10.0), 100, 35)) == 35
|
| 53 |
+
assert complex_accuracy(
|
| 54 |
+
(from_float(2.0), from_float(1000.0), 100, 35)) == 35
|
| 55 |
+
assert complex_accuracy(finf) == math.inf
|
| 56 |
+
assert complex_accuracy(zoo) == math.inf
|
| 57 |
+
raises(ValueError, lambda: get_integer_part(zoo, 1, {}))
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def test_evalf_basic():
|
| 61 |
+
assert NS('pi', 15) == '3.14159265358979'
|
| 62 |
+
assert NS('2/3', 10) == '0.6666666667'
|
| 63 |
+
assert NS('355/113-pi', 6) == '2.66764e-7'
|
| 64 |
+
assert NS('16*atan(1/5)-4*atan(1/239)', 15) == '3.14159265358979'
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def test_cancellation():
|
| 68 |
+
assert NS(Add(pi, Rational(1, 10**1000), -pi, evaluate=False), 15,
|
| 69 |
+
maxn=1200) == '1.00000000000000e-1000'
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def test_evalf_powers():
|
| 73 |
+
assert NS('pi**(10**20)', 10) == '1.339148777e+49714987269413385435'
|
| 74 |
+
assert NS(pi**(10**100), 10) == ('4.946362032e+4971498726941338543512682882'
|
| 75 |
+
'9089887365167832438044244613405349992494711208'
|
| 76 |
+
'95526746555473864642912223')
|
| 77 |
+
assert NS('2**(1/10**50)', 15) == '1.00000000000000'
|
| 78 |
+
assert NS('2**(1/10**50)-1', 15) == '6.93147180559945e-51'
|
| 79 |
+
|
| 80 |
+
# Evaluation of Rump's ill-conditioned polynomial
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def test_evalf_rump():
|
| 84 |
+
a = 1335*y**6/4 + x**2*(11*x**2*y**2 - y**6 - 121*y**4 - 2) + 11*y**8/2 + x/(2*y)
|
| 85 |
+
assert NS(a, 15, subs={x: 77617, y: 33096}) == '-0.827396059946821'
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def test_evalf_complex():
|
| 89 |
+
assert NS('2*sqrt(pi)*I', 10) == '3.544907702*I'
|
| 90 |
+
assert NS('3+3*I', 15) == '3.00000000000000 + 3.00000000000000*I'
|
| 91 |
+
assert NS('E+pi*I', 15) == '2.71828182845905 + 3.14159265358979*I'
|
| 92 |
+
assert NS('pi * (3+4*I)', 15) == '9.42477796076938 + 12.5663706143592*I'
|
| 93 |
+
assert NS('I*(2+I)', 15) == '-1.00000000000000 + 2.00000000000000*I'
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
@XFAIL
|
| 97 |
+
def test_evalf_complex_bug():
|
| 98 |
+
assert NS('(pi+E*I)*(E+pi*I)', 15) in ('0.e-15 + 17.25866050002*I',
|
| 99 |
+
'0.e-17 + 17.25866050002*I', '-0.e-17 + 17.25866050002*I')
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def test_evalf_complex_powers():
|
| 103 |
+
assert NS('(E+pi*I)**100000000000000000') == \
|
| 104 |
+
'-3.58896782867793e+61850354284995199 + 4.58581754997159e+61850354284995199*I'
|
| 105 |
+
# XXX: rewrite if a+a*I simplification introduced in SymPy
|
| 106 |
+
#assert NS('(pi + pi*I)**2') in ('0.e-15 + 19.7392088021787*I', '0.e-16 + 19.7392088021787*I')
|
| 107 |
+
assert NS('(pi + pi*I)**2', chop=True) == '19.7392088021787*I'
|
| 108 |
+
assert NS(
|
| 109 |
+
'(pi + 1/10**8 + pi*I)**2') == '6.2831853e-8 + 19.7392088650106*I'
|
| 110 |
+
assert NS('(pi + 1/10**12 + pi*I)**2') == '6.283e-12 + 19.7392088021850*I'
|
| 111 |
+
assert NS('(pi + pi*I)**4', chop=True) == '-389.636364136010'
|
| 112 |
+
assert NS(
|
| 113 |
+
'(pi + 1/10**8 + pi*I)**4') == '-389.636366616512 + 2.4805021e-6*I'
|
| 114 |
+
assert NS('(pi + 1/10**12 + pi*I)**4') == '-389.636364136258 + 2.481e-10*I'
|
| 115 |
+
assert NS(
|
| 116 |
+
'(10000*pi + 10000*pi*I)**4', chop=True) == '-3.89636364136010e+18'
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
@XFAIL
|
| 120 |
+
def test_evalf_complex_powers_bug():
|
| 121 |
+
assert NS('(pi + pi*I)**4') == '-389.63636413601 + 0.e-14*I'
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def test_evalf_exponentiation():
|
| 125 |
+
assert NS(sqrt(-pi)) == '1.77245385090552*I'
|
| 126 |
+
assert NS(Pow(pi*I, Rational(
|
| 127 |
+
1, 2), evaluate=False)) == '1.25331413731550 + 1.25331413731550*I'
|
| 128 |
+
assert NS(pi**I) == '0.413292116101594 + 0.910598499212615*I'
|
| 129 |
+
assert NS(pi**(E + I/3)) == '20.8438653991931 + 8.36343473930031*I'
|
| 130 |
+
assert NS((pi + I/3)**(E + I/3)) == '17.2442906093590 + 13.6839376767037*I'
|
| 131 |
+
assert NS(exp(pi)) == '23.1406926327793'
|
| 132 |
+
assert NS(exp(pi + E*I)) == '-21.0981542849657 + 9.50576358282422*I'
|
| 133 |
+
assert NS(pi**pi) == '36.4621596072079'
|
| 134 |
+
assert NS((-pi)**pi) == '-32.9138577418939 - 15.6897116534332*I'
|
| 135 |
+
assert NS((-pi)**(-pi)) == '-0.0247567717232697 + 0.0118013091280262*I'
|
| 136 |
+
|
| 137 |
+
# An example from Smith, "Multiple Precision Complex Arithmetic and Functions"
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def test_evalf_complex_cancellation():
|
| 141 |
+
A = Rational('63287/100000')
|
| 142 |
+
B = Rational('52498/100000')
|
| 143 |
+
C = Rational('69301/100000')
|
| 144 |
+
D = Rational('83542/100000')
|
| 145 |
+
F = Rational('2231321613/2500000000')
|
| 146 |
+
# XXX: the number of returned mantissa digits in the real part could
|
| 147 |
+
# change with the implementation. What matters is that the returned digits are
|
| 148 |
+
# correct; those that are showing now are correct.
|
| 149 |
+
# >>> ((A+B*I)*(C+D*I)).expand()
|
| 150 |
+
# 64471/10000000000 + 2231321613*I/2500000000
|
| 151 |
+
# >>> 2231321613*4
|
| 152 |
+
# 8925286452L
|
| 153 |
+
assert NS((A + B*I)*(C + D*I), 6) == '6.44710e-6 + 0.892529*I'
|
| 154 |
+
assert NS((A + B*I)*(C + D*I), 10) == '6.447100000e-6 + 0.8925286452*I'
|
| 155 |
+
assert NS((A + B*I)*(
|
| 156 |
+
C + D*I) - F*I, 5) in ('6.4471e-6 + 0.e-14*I', '6.4471e-6 - 0.e-14*I')
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def test_evalf_logs():
|
| 160 |
+
assert NS("log(3+pi*I)", 15) == '1.46877619736226 + 0.808448792630022*I'
|
| 161 |
+
assert NS("log(pi*I)", 15) == '1.14472988584940 + 1.57079632679490*I'
|
| 162 |
+
assert NS('log(-1 + 0.00001)', 2) == '-1.0e-5 + 3.1*I'
|
| 163 |
+
assert NS('log(100, 10, evaluate=False)', 15) == '2.00000000000000'
|
| 164 |
+
assert NS('-2*I*log(-(-1)**(S(1)/9))', 15) == '-5.58505360638185'
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
def test_evalf_trig():
|
| 168 |
+
assert NS('sin(1)', 15) == '0.841470984807897'
|
| 169 |
+
assert NS('cos(1)', 15) == '0.540302305868140'
|
| 170 |
+
assert NS('sin(10**-6)', 15) == '9.99999999999833e-7'
|
| 171 |
+
assert NS('cos(10**-6)', 15) == '0.999999999999500'
|
| 172 |
+
assert NS('sin(E*10**100)', 15) == '0.409160531722613'
|
| 173 |
+
# Some input near roots
|
| 174 |
+
assert NS(sin(exp(pi*sqrt(163))*pi), 15) == '-2.35596641936785e-12'
|
| 175 |
+
assert NS(sin(pi*10**100 + Rational(7, 10**5), evaluate=False), 15, maxn=120) == \
|
| 176 |
+
'6.99999999428333e-5'
|
| 177 |
+
assert NS(sin(Rational(7, 10**5), evaluate=False), 15) == \
|
| 178 |
+
'6.99999999428333e-5'
|
| 179 |
+
|
| 180 |
+
# Check detection of various false identities
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
def test_evalf_near_integers():
|
| 184 |
+
# Binet's formula
|
| 185 |
+
f = lambda n: ((1 + sqrt(5))**n)/(2**n * sqrt(5))
|
| 186 |
+
assert NS(f(5000) - fibonacci(5000), 10, maxn=1500) == '5.156009964e-1046'
|
| 187 |
+
# Some near-integer identities from
|
| 188 |
+
# http://mathworld.wolfram.com/AlmostInteger.html
|
| 189 |
+
assert NS('sin(2017*2**(1/5))', 15) == '-1.00000000000000'
|
| 190 |
+
assert NS('sin(2017*2**(1/5))', 20) == '-0.99999999999999997857'
|
| 191 |
+
assert NS('1+sin(2017*2**(1/5))', 15) == '2.14322287389390e-17'
|
| 192 |
+
assert NS('45 - 613*E/37 + 35/991', 15) == '6.03764498766326e-11'
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def test_evalf_ramanujan():
|
| 196 |
+
assert NS(exp(pi*sqrt(163)) - 640320**3 - 744, 10) == '-7.499274028e-13'
|
| 197 |
+
# A related identity
|
| 198 |
+
A = 262537412640768744*exp(-pi*sqrt(163))
|
| 199 |
+
B = 196884*exp(-2*pi*sqrt(163))
|
| 200 |
+
C = 103378831900730205293632*exp(-3*pi*sqrt(163))
|
| 201 |
+
assert NS(1 - A - B + C, 10) == '1.613679005e-59'
|
| 202 |
+
|
| 203 |
+
# Input that for various reasons have failed at some point
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
def test_evalf_bugs():
|
| 207 |
+
assert NS(sin(1) + exp(-10**10), 10) == NS(sin(1), 10)
|
| 208 |
+
assert NS(exp(10**10) + sin(1), 10) == NS(exp(10**10), 10)
|
| 209 |
+
assert NS('expand_log(log(1+1/10**50))', 20) == '1.0000000000000000000e-50'
|
| 210 |
+
assert NS('log(10**100,10)', 10) == '100.0000000'
|
| 211 |
+
assert NS('log(2)', 10) == '0.6931471806'
|
| 212 |
+
assert NS(
|
| 213 |
+
'(sin(x)-x)/x**3', 15, subs={x: '1/10**50'}) == '-0.166666666666667'
|
| 214 |
+
assert NS(sin(1) + Rational(
|
| 215 |
+
1, 10**100)*I, 15) == '0.841470984807897 + 1.00000000000000e-100*I'
|
| 216 |
+
assert x.evalf() == x
|
| 217 |
+
assert NS((1 + I)**2*I, 6) == '-2.00000'
|
| 218 |
+
d = {n: (
|
| 219 |
+
-1)**Rational(6, 7), y: (-1)**Rational(4, 7), x: (-1)**Rational(2, 7)}
|
| 220 |
+
assert NS((x*(1 + y*(1 + n))).subs(d).evalf(), 6) == '0.346011 + 0.433884*I'
|
| 221 |
+
assert NS(((-I - sqrt(2)*I)**2).evalf()) == '-5.82842712474619'
|
| 222 |
+
assert NS((1 + I)**2*I, 15) == '-2.00000000000000'
|
| 223 |
+
# issue 4758 (1/2):
|
| 224 |
+
assert NS(pi.evalf(69) - pi) == '-4.43863937855894e-71'
|
| 225 |
+
# issue 4758 (2/2): With the bug present, this still only fails if the
|
| 226 |
+
# terms are in the order given here. This is not generally the case,
|
| 227 |
+
# because the order depends on the hashes of the terms.
|
| 228 |
+
assert NS(20 - 5008329267844*n**25 - 477638700*n**37 - 19*n,
|
| 229 |
+
subs={n: .01}) == '19.8100000000000'
|
| 230 |
+
assert NS(((x - 1)*(1 - x)**1000).n()
|
| 231 |
+
) == '(1.00000000000000 - x)**1000*(x - 1.00000000000000)'
|
| 232 |
+
assert NS((-x).n()) == '-x'
|
| 233 |
+
assert NS((-2*x).n()) == '-2.00000000000000*x'
|
| 234 |
+
assert NS((-2*x*y).n()) == '-2.00000000000000*x*y'
|
| 235 |
+
assert cos(x).n(subs={x: 1+I}) == cos(x).subs(x, 1+I).n()
|
| 236 |
+
# issue 6660. Also NaN != mpmath.nan
|
| 237 |
+
# In this order:
|
| 238 |
+
# 0*nan, 0/nan, 0*inf, 0/inf
|
| 239 |
+
# 0+nan, 0-nan, 0+inf, 0-inf
|
| 240 |
+
# >>> n = Some Number
|
| 241 |
+
# n*nan, n/nan, n*inf, n/inf
|
| 242 |
+
# n+nan, n-nan, n+inf, n-inf
|
| 243 |
+
assert (0*E**(oo)).n() is S.NaN
|
| 244 |
+
assert (0/E**(oo)).n() is S.Zero
|
| 245 |
+
|
| 246 |
+
assert (0+E**(oo)).n() is S.Infinity
|
| 247 |
+
assert (0-E**(oo)).n() is S.NegativeInfinity
|
| 248 |
+
|
| 249 |
+
assert (5*E**(oo)).n() is S.Infinity
|
| 250 |
+
assert (5/E**(oo)).n() is S.Zero
|
| 251 |
+
|
| 252 |
+
assert (5+E**(oo)).n() is S.Infinity
|
| 253 |
+
assert (5-E**(oo)).n() is S.NegativeInfinity
|
| 254 |
+
|
| 255 |
+
#issue 7416
|
| 256 |
+
assert as_mpmath(0.0, 10, {'chop': True}) == 0
|
| 257 |
+
|
| 258 |
+
#issue 5412
|
| 259 |
+
assert ((oo*I).n() == S.Infinity*I)
|
| 260 |
+
assert ((oo+oo*I).n() == S.Infinity + S.Infinity*I)
|
| 261 |
+
|
| 262 |
+
#issue 11518
|
| 263 |
+
assert NS(2*x**2.5, 5) == '2.0000*x**2.5000'
|
| 264 |
+
|
| 265 |
+
#issue 13076
|
| 266 |
+
assert NS(Mul(Max(0, y), x, evaluate=False).evalf()) == 'x*Max(0, y)'
|
| 267 |
+
|
| 268 |
+
#issue 18516
|
| 269 |
+
assert NS(log(S(3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376)/36360291795869936842385267079543319118023385026001623040346035832580600191583895484198508262979388783308179702534403855752855931517013066142992430916562025780021771247847643450125342836565813209972590371590152578728008385990139795377610001).evalf(15, chop=True)) == '-oo'
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
def test_evalf_integer_parts():
|
| 273 |
+
a = floor(log(8)/log(2) - exp(-1000), evaluate=False)
|
| 274 |
+
b = floor(log(8)/log(2), evaluate=False)
|
| 275 |
+
assert a.evalf() == 3.0
|
| 276 |
+
assert b.evalf() == 3.0
|
| 277 |
+
# equals, as a fallback, can still fail but it might succeed as here
|
| 278 |
+
assert ceiling(10*(sin(1)**2 + cos(1)**2)) == 10
|
| 279 |
+
|
| 280 |
+
assert int(floor(factorial(50)/E, evaluate=False).evalf(70)) == \
|
| 281 |
+
int(11188719610782480504630258070757734324011354208865721592720336800)
|
| 282 |
+
assert int(ceiling(factorial(50)/E, evaluate=False).evalf(70)) == \
|
| 283 |
+
int(11188719610782480504630258070757734324011354208865721592720336801)
|
| 284 |
+
assert int(floor(GoldenRatio**999 / sqrt(5) + S.Half)
|
| 285 |
+
.evalf(1000)) == fibonacci(999)
|
| 286 |
+
assert int(floor(GoldenRatio**1000 / sqrt(5) + S.Half)
|
| 287 |
+
.evalf(1000)) == fibonacci(1000)
|
| 288 |
+
|
| 289 |
+
assert ceiling(x).evalf(subs={x: 3}) == 3.0
|
| 290 |
+
assert ceiling(x).evalf(subs={x: 3*I}) == 3.0*I
|
| 291 |
+
assert ceiling(x).evalf(subs={x: 2 + 3*I}) == 2.0 + 3.0*I
|
| 292 |
+
assert ceiling(x).evalf(subs={x: 3.}) == 3.0
|
| 293 |
+
assert ceiling(x).evalf(subs={x: 3.*I}) == 3.0*I
|
| 294 |
+
assert ceiling(x).evalf(subs={x: 2. + 3*I}) == 2.0 + 3.0*I
|
| 295 |
+
|
| 296 |
+
assert float((floor(1.5, evaluate=False)+1/9).evalf()) == 1 + 1/9
|
| 297 |
+
assert float((floor(0.5, evaluate=False)+20).evalf()) == 20
|
| 298 |
+
|
| 299 |
+
# issue 19991
|
| 300 |
+
n = 1169809367327212570704813632106852886389036911
|
| 301 |
+
r = 744723773141314414542111064094745678855643068
|
| 302 |
+
|
| 303 |
+
assert floor(n / (pi / 2)) == r
|
| 304 |
+
assert floor(80782 * sqrt(2)) == 114242
|
| 305 |
+
|
| 306 |
+
# issue 20076
|
| 307 |
+
assert 260515 - floor(260515/pi + 1/2) * pi == atan(tan(260515))
|
| 308 |
+
|
| 309 |
+
assert floor(x).evalf(subs={x: sqrt(2)}) == 1.0
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
def test_evalf_trig_zero_detection():
|
| 313 |
+
a = sin(160*pi, evaluate=False)
|
| 314 |
+
t = a.evalf(maxn=100)
|
| 315 |
+
assert abs(t) < 1e-100
|
| 316 |
+
assert t._prec < 2
|
| 317 |
+
assert a.evalf(chop=True) == 0
|
| 318 |
+
raises(PrecisionExhausted, lambda: a.evalf(strict=True))
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
def test_evalf_sum():
|
| 322 |
+
assert Sum(n,(n,1,2)).evalf() == 3.
|
| 323 |
+
assert Sum(n,(n,1,2)).doit().evalf() == 3.
|
| 324 |
+
# the next test should return instantly
|
| 325 |
+
assert Sum(1/n,(n,1,2)).evalf() == 1.5
|
| 326 |
+
|
| 327 |
+
# issue 8219
|
| 328 |
+
assert Sum(E/factorial(n), (n, 0, oo)).evalf() == (E*E).evalf()
|
| 329 |
+
# issue 8254
|
| 330 |
+
assert Sum(2**n*n/factorial(n), (n, 0, oo)).evalf() == (2*E*E).evalf()
|
| 331 |
+
# issue 8411
|
| 332 |
+
s = Sum(1/x**2, (x, 100, oo))
|
| 333 |
+
assert s.n() == s.doit().n()
|
| 334 |
+
|
| 335 |
+
|
| 336 |
+
def test_evalf_divergent_series():
|
| 337 |
+
raises(ValueError, lambda: Sum(1/n, (n, 1, oo)).evalf())
|
| 338 |
+
raises(ValueError, lambda: Sum(n/(n**2 + 1), (n, 1, oo)).evalf())
|
| 339 |
+
raises(ValueError, lambda: Sum((-1)**n, (n, 1, oo)).evalf())
|
| 340 |
+
raises(ValueError, lambda: Sum((-1)**n, (n, 1, oo)).evalf())
|
| 341 |
+
raises(ValueError, lambda: Sum(n**2, (n, 1, oo)).evalf())
|
| 342 |
+
raises(ValueError, lambda: Sum(2**n, (n, 1, oo)).evalf())
|
| 343 |
+
raises(ValueError, lambda: Sum((-2)**n, (n, 1, oo)).evalf())
|
| 344 |
+
raises(ValueError, lambda: Sum((2*n + 3)/(3*n**2 + 4), (n, 0, oo)).evalf())
|
| 345 |
+
raises(ValueError, lambda: Sum((0.5*n**3)/(n**4 + 1), (n, 0, oo)).evalf())
|
| 346 |
+
|
| 347 |
+
|
| 348 |
+
def test_evalf_product():
|
| 349 |
+
assert Product(n, (n, 1, 10)).evalf() == 3628800.
|
| 350 |
+
assert comp(Product(1 - S.Half**2/n**2, (n, 1, oo)).n(5), 0.63662)
|
| 351 |
+
assert Product(n, (n, -1, 3)).evalf() == 0
|
| 352 |
+
|
| 353 |
+
|
| 354 |
+
def test_evalf_py_methods():
|
| 355 |
+
assert abs(float(pi + 1) - 4.1415926535897932) < 1e-10
|
| 356 |
+
assert abs(complex(pi + 1) - 4.1415926535897932) < 1e-10
|
| 357 |
+
assert abs(
|
| 358 |
+
complex(pi + E*I) - (3.1415926535897931 + 2.7182818284590451j)) < 1e-10
|
| 359 |
+
raises(TypeError, lambda: float(pi + x))
|
| 360 |
+
|
| 361 |
+
|
| 362 |
+
def test_evalf_power_subs_bugs():
|
| 363 |
+
assert (x**2).evalf(subs={x: 0}) == 0
|
| 364 |
+
assert sqrt(x).evalf(subs={x: 0}) == 0
|
| 365 |
+
assert (x**Rational(2, 3)).evalf(subs={x: 0}) == 0
|
| 366 |
+
assert (x**x).evalf(subs={x: 0}) == 1.0
|
| 367 |
+
assert (3**x).evalf(subs={x: 0}) == 1.0
|
| 368 |
+
assert exp(x).evalf(subs={x: 0}) == 1.0
|
| 369 |
+
assert ((2 + I)**x).evalf(subs={x: 0}) == 1.0
|
| 370 |
+
assert (0**x).evalf(subs={x: 0}) == 1.0
|
| 371 |
+
|
| 372 |
+
|
| 373 |
+
def test_evalf_arguments():
|
| 374 |
+
raises(TypeError, lambda: pi.evalf(method="garbage"))
|
| 375 |
+
|
| 376 |
+
|
| 377 |
+
def test_implemented_function_evalf():
|
| 378 |
+
from sympy.utilities.lambdify import implemented_function
|
| 379 |
+
f = Function('f')
|
| 380 |
+
f = implemented_function(f, lambda x: x + 1)
|
| 381 |
+
assert str(f(x)) == "f(x)"
|
| 382 |
+
assert str(f(2)) == "f(2)"
|
| 383 |
+
assert f(2).evalf() == 3.0
|
| 384 |
+
assert f(x).evalf() == f(x)
|
| 385 |
+
f = implemented_function(Function('sin'), lambda x: x + 1)
|
| 386 |
+
assert f(2).evalf() != sin(2)
|
| 387 |
+
del f._imp_ # XXX: due to caching _imp_ would influence all other tests
|
| 388 |
+
|
| 389 |
+
|
| 390 |
+
def test_evaluate_false():
|
| 391 |
+
for no in [0, False]:
|
| 392 |
+
assert Add(3, 2, evaluate=no).is_Add
|
| 393 |
+
assert Mul(3, 2, evaluate=no).is_Mul
|
| 394 |
+
assert Pow(3, 2, evaluate=no).is_Pow
|
| 395 |
+
assert Pow(y, 2, evaluate=True) - Pow(y, 2, evaluate=True) == 0
|
| 396 |
+
|
| 397 |
+
|
| 398 |
+
def test_evalf_relational():
|
| 399 |
+
assert Eq(x/5, y/10).evalf() == Eq(0.2*x, 0.1*y)
|
| 400 |
+
# if this first assertion fails it should be replaced with
|
| 401 |
+
# one that doesn't
|
| 402 |
+
assert unchanged(Eq, (3 - I)**2/2 + I, 0)
|
| 403 |
+
assert Eq((3 - I)**2/2 + I, 0).n() is S.false
|
| 404 |
+
assert nfloat(Eq((3 - I)**2 + I, 0)) == S.false
|
| 405 |
+
|
| 406 |
+
|
| 407 |
+
def test_issue_5486():
|
| 408 |
+
assert not cos(sqrt(0.5 + I)).n().is_Function
|
| 409 |
+
|
| 410 |
+
|
| 411 |
+
def test_issue_5486_bug():
|
| 412 |
+
from sympy.core.expr import Expr
|
| 413 |
+
from sympy.core.numbers import I
|
| 414 |
+
assert abs(Expr._from_mpmath(I._to_mpmath(15), 15) - I) < 1.0e-15
|
| 415 |
+
|
| 416 |
+
|
| 417 |
+
def test_bugs():
|
| 418 |
+
from sympy.functions.elementary.complexes import (polar_lift, re)
|
| 419 |
+
|
| 420 |
+
assert abs(re((1 + I)**2)) < 1e-15
|
| 421 |
+
|
| 422 |
+
# anything that evalf's to 0 will do in place of polar_lift
|
| 423 |
+
assert abs(polar_lift(0)).n() == 0
|
| 424 |
+
|
| 425 |
+
|
| 426 |
+
def test_subs():
|
| 427 |
+
assert NS('besseli(-x, y) - besseli(x, y)', subs={x: 3.5, y: 20.0}) == \
|
| 428 |
+
'-4.92535585957223e-10'
|
| 429 |
+
assert NS('Piecewise((x, x>0)) + Piecewise((1-x, x>0))', subs={x: 0.1}) == \
|
| 430 |
+
'1.00000000000000'
|
| 431 |
+
raises(TypeError, lambda: x.evalf(subs=(x, 1)))
|
| 432 |
+
|
| 433 |
+
|
| 434 |
+
def test_issue_4956_5204():
|
| 435 |
+
# issue 4956
|
| 436 |
+
v = S('''(-27*12**(1/3)*sqrt(31)*I +
|
| 437 |
+
27*2**(2/3)*3**(1/3)*sqrt(31)*I)/(-2511*2**(2/3)*3**(1/3) +
|
| 438 |
+
(29*18**(1/3) + 9*2**(1/3)*3**(2/3)*sqrt(31)*I +
|
| 439 |
+
87*2**(1/3)*3**(1/6)*I)**2)''')
|
| 440 |
+
assert NS(v, 1) == '0.e-118 - 0.e-118*I'
|
| 441 |
+
|
| 442 |
+
# issue 5204
|
| 443 |
+
v = S('''-(357587765856 + 18873261792*249**(1/2) + 56619785376*I*83**(1/2) +
|
| 444 |
+
108755765856*I*3**(1/2) + 41281887168*6**(1/3)*(1422 +
|
| 445 |
+
54*249**(1/2))**(1/3) - 1239810624*6**(1/3)*249**(1/2)*(1422 +
|
| 446 |
+
54*249**(1/2))**(1/3) - 3110400000*I*6**(1/3)*83**(1/2)*(1422 +
|
| 447 |
+
54*249**(1/2))**(1/3) + 13478400000*I*3**(1/2)*6**(1/3)*(1422 +
|
| 448 |
+
54*249**(1/2))**(1/3) + 1274950152*6**(2/3)*(1422 +
|
| 449 |
+
54*249**(1/2))**(2/3) + 32347944*6**(2/3)*249**(1/2)*(1422 +
|
| 450 |
+
54*249**(1/2))**(2/3) - 1758790152*I*3**(1/2)*6**(2/3)*(1422 +
|
| 451 |
+
54*249**(1/2))**(2/3) - 304403832*I*6**(2/3)*83**(1/2)*(1422 +
|
| 452 |
+
4*249**(1/2))**(2/3))/(175732658352 + (1106028 + 25596*249**(1/2) +
|
| 453 |
+
76788*I*83**(1/2))**2)''')
|
| 454 |
+
assert NS(v, 5) == '0.077284 + 1.1104*I'
|
| 455 |
+
assert NS(v, 1) == '0.08 + 1.*I'
|
| 456 |
+
|
| 457 |
+
|
| 458 |
+
def test_old_docstring():
|
| 459 |
+
a = (E + pi*I)*(E - pi*I)
|
| 460 |
+
assert NS(a) == '17.2586605000200'
|
| 461 |
+
assert a.n() == 17.25866050002001
|
| 462 |
+
|
| 463 |
+
|
| 464 |
+
def test_issue_4806():
|
| 465 |
+
assert integrate(atan(x)**2, (x, -1, 1)).evalf().round(1) == Float(0.5, 1)
|
| 466 |
+
assert atan(0, evaluate=False).n() == 0
|
| 467 |
+
|
| 468 |
+
|
| 469 |
+
def test_evalf_mul():
|
| 470 |
+
# SymPy should not try to expand this; it should be handled term-wise
|
| 471 |
+
# in evalf through mpmath
|
| 472 |
+
assert NS(product(1 + sqrt(n)*I, (n, 1, 500)), 1) == '5.e+567 + 2.e+568*I'
|
| 473 |
+
|
| 474 |
+
|
| 475 |
+
def test_scaled_zero():
|
| 476 |
+
a, b = (([0], 1, 100, 1), -1)
|
| 477 |
+
assert scaled_zero(100) == (a, b)
|
| 478 |
+
assert scaled_zero(a) == (0, 1, 100, 1)
|
| 479 |
+
a, b = (([1], 1, 100, 1), -1)
|
| 480 |
+
assert scaled_zero(100, -1) == (a, b)
|
| 481 |
+
assert scaled_zero(a) == (1, 1, 100, 1)
|
| 482 |
+
raises(ValueError, lambda: scaled_zero(scaled_zero(100)))
|
| 483 |
+
raises(ValueError, lambda: scaled_zero(100, 2))
|
| 484 |
+
raises(ValueError, lambda: scaled_zero(100, 0))
|
| 485 |
+
raises(ValueError, lambda: scaled_zero((1, 5, 1, 3)))
|
| 486 |
+
|
| 487 |
+
|
| 488 |
+
def test_chop_value():
|
| 489 |
+
for i in range(-27, 28):
|
| 490 |
+
assert (Pow(10, i)*2).n(chop=10**i) and not (Pow(10, i)).n(chop=10**i)
|
| 491 |
+
|
| 492 |
+
|
| 493 |
+
def test_infinities():
|
| 494 |
+
assert oo.evalf(chop=True) == inf
|
| 495 |
+
assert (-oo).evalf(chop=True) == ninf
|
| 496 |
+
|
| 497 |
+
|
| 498 |
+
def test_to_mpmath():
|
| 499 |
+
assert sqrt(3)._to_mpmath(20)._mpf_ == (0, int(908093), -19, 20)
|
| 500 |
+
assert S(3.2)._to_mpmath(20)._mpf_ == (0, int(838861), -18, 20)
|
| 501 |
+
|
| 502 |
+
|
| 503 |
+
def test_issue_6632_evalf():
|
| 504 |
+
add = (-100000*sqrt(2500000001) + 5000000001)
|
| 505 |
+
assert add.n() == 9.999999998e-11
|
| 506 |
+
assert (add*add).n() == 9.999999996e-21
|
| 507 |
+
|
| 508 |
+
|
| 509 |
+
def test_issue_4945():
|
| 510 |
+
from sympy.abc import H
|
| 511 |
+
assert (H/0).evalf(subs={H:1}) == zoo
|
| 512 |
+
|
| 513 |
+
|
| 514 |
+
def test_evalf_integral():
|
| 515 |
+
# test that workprec has to increase in order to get a result other than 0
|
| 516 |
+
eps = Rational(1, 1000000)
|
| 517 |
+
assert Integral(sin(x), (x, -pi, pi + eps)).n(2)._prec == 10
|
| 518 |
+
|
| 519 |
+
|
| 520 |
+
def test_issue_8821_highprec_from_str():
|
| 521 |
+
s = str(pi.evalf(128))
|
| 522 |
+
p = N(s)
|
| 523 |
+
assert Abs(sin(p)) < 1e-15
|
| 524 |
+
p = N(s, 64)
|
| 525 |
+
assert Abs(sin(p)) < 1e-64
|
| 526 |
+
|
| 527 |
+
|
| 528 |
+
def test_issue_8853():
|
| 529 |
+
p = Symbol('x', even=True, positive=True)
|
| 530 |
+
assert floor(-p - S.Half).is_even == False
|
| 531 |
+
assert floor(-p + S.Half).is_even == True
|
| 532 |
+
assert ceiling(p - S.Half).is_even == True
|
| 533 |
+
assert ceiling(p + S.Half).is_even == False
|
| 534 |
+
|
| 535 |
+
assert get_integer_part(S.Half, -1, {}, True) == (0, 0)
|
| 536 |
+
assert get_integer_part(S.Half, 1, {}, True) == (1, 0)
|
| 537 |
+
assert get_integer_part(Rational(-1, 2), -1, {}, True) == (-1, 0)
|
| 538 |
+
assert get_integer_part(Rational(-1, 2), 1, {}, True) == (0, 0)
|
| 539 |
+
|
| 540 |
+
|
| 541 |
+
def test_issue_17681():
|
| 542 |
+
class identity_func(Function):
|
| 543 |
+
|
| 544 |
+
def _eval_evalf(self, *args, **kwargs):
|
| 545 |
+
return self.args[0].evalf(*args, **kwargs)
|
| 546 |
+
|
| 547 |
+
assert floor(identity_func(S(0))) == 0
|
| 548 |
+
assert get_integer_part(S(0), 1, {}, True) == (0, 0)
|
| 549 |
+
|
| 550 |
+
|
| 551 |
+
def test_issue_9326():
|
| 552 |
+
from sympy.core.symbol import Dummy
|
| 553 |
+
d1 = Dummy('d')
|
| 554 |
+
d2 = Dummy('d')
|
| 555 |
+
e = d1 + d2
|
| 556 |
+
assert e.evalf(subs = {d1: 1, d2: 2}) == 3.0
|
| 557 |
+
|
| 558 |
+
|
| 559 |
+
def test_issue_10323():
|
| 560 |
+
assert ceiling(sqrt(2**30 + 1)) == 2**15 + 1
|
| 561 |
+
|
| 562 |
+
|
| 563 |
+
def test_AssocOp_Function():
|
| 564 |
+
# the first arg of Min is not comparable in the imaginary part
|
| 565 |
+
raises(ValueError, lambda: S('''
|
| 566 |
+
Min(-sqrt(3)*cos(pi/18)/6 + re(1/((-1/2 - sqrt(3)*I/2)*(1/6 +
|
| 567 |
+
sqrt(3)*I/18)**(1/3)))/3 + sin(pi/18)/2 + 2 + I*(-cos(pi/18)/2 -
|
| 568 |
+
sqrt(3)*sin(pi/18)/6 + im(1/((-1/2 - sqrt(3)*I/2)*(1/6 +
|
| 569 |
+
sqrt(3)*I/18)**(1/3)))/3), re(1/((-1/2 + sqrt(3)*I/2)*(1/6 +
|
| 570 |
+
sqrt(3)*I/18)**(1/3)))/3 - sqrt(3)*cos(pi/18)/6 - sin(pi/18)/2 + 2 +
|
| 571 |
+
I*(im(1/((-1/2 + sqrt(3)*I/2)*(1/6 + sqrt(3)*I/18)**(1/3)))/3 -
|
| 572 |
+
sqrt(3)*sin(pi/18)/6 + cos(pi/18)/2))'''))
|
| 573 |
+
# if that is changed so a non-comparable number remains as
|
| 574 |
+
# an arg, then the Min/Max instantiation needs to be changed
|
| 575 |
+
# to watch out for non-comparable args when making simplifications
|
| 576 |
+
# and the following test should be added instead (with e being
|
| 577 |
+
# the sympified expression above):
|
| 578 |
+
# raises(ValueError, lambda: e._eval_evalf(2))
|
| 579 |
+
|
| 580 |
+
|
| 581 |
+
def test_issue_10395():
|
| 582 |
+
eq = x*Max(0, y)
|
| 583 |
+
assert nfloat(eq) == eq
|
| 584 |
+
eq = x*Max(y, -1.1)
|
| 585 |
+
assert nfloat(eq) == eq
|
| 586 |
+
assert Max(y, 4).n() == Max(4.0, y)
|
| 587 |
+
|
| 588 |
+
|
| 589 |
+
def test_issue_13098():
|
| 590 |
+
assert floor(log(S('9.'+'9'*20), 10)) == 0
|
| 591 |
+
assert ceiling(log(S('9.'+'9'*20), 10)) == 1
|
| 592 |
+
assert floor(log(20 - S('9.'+'9'*20), 10)) == 1
|
| 593 |
+
assert ceiling(log(20 - S('9.'+'9'*20), 10)) == 2
|
| 594 |
+
|
| 595 |
+
|
| 596 |
+
def test_issue_14601():
|
| 597 |
+
e = 5*x*y/2 - y*(35*(x**3)/2 - 15*x/2)
|
| 598 |
+
subst = {x:0.0, y:0.0}
|
| 599 |
+
e2 = e.evalf(subs=subst)
|
| 600 |
+
assert float(e2) == 0.0
|
| 601 |
+
assert float((x + x*(x**2 + x)).evalf(subs={x: 0.0})) == 0.0
|
| 602 |
+
|
| 603 |
+
|
| 604 |
+
def test_issue_11151():
|
| 605 |
+
z = S.Zero
|
| 606 |
+
e = Sum(z, (x, 1, 2))
|
| 607 |
+
assert e != z # it shouldn't evaluate
|
| 608 |
+
# when it does evaluate, this is what it should give
|
| 609 |
+
assert evalf(e, 15, {}) == \
|
| 610 |
+
evalf(z, 15, {}) == (None, None, 15, None)
|
| 611 |
+
# so this shouldn't fail
|
| 612 |
+
assert (e/2).n() == 0
|
| 613 |
+
# this was where the issue appeared
|
| 614 |
+
expr0 = Sum(x**2 + x, (x, 1, 2))
|
| 615 |
+
expr1 = Sum(0, (x, 1, 2))
|
| 616 |
+
expr2 = expr1/expr0
|
| 617 |
+
assert simplify(factor(expr2) - expr2) == 0
|
| 618 |
+
|
| 619 |
+
|
| 620 |
+
def test_issue_13425():
|
| 621 |
+
assert N('2**.5', 30) == N('sqrt(2)', 30)
|
| 622 |
+
assert N('x - x', 30) == 0
|
| 623 |
+
assert abs((N('pi*.1', 22)*10 - pi).n()) < 1e-22
|
| 624 |
+
|
| 625 |
+
|
| 626 |
+
def test_issue_17421():
|
| 627 |
+
assert N(acos(-I + acosh(cosh(cosh(1) + I)))) == 1.0*I
|
| 628 |
+
|
| 629 |
+
|
| 630 |
+
def test_issue_20291():
|
| 631 |
+
from sympy.sets import EmptySet, Reals
|
| 632 |
+
from sympy.sets.sets import (Complement, FiniteSet, Intersection)
|
| 633 |
+
a = Symbol('a')
|
| 634 |
+
b = Symbol('b')
|
| 635 |
+
A = FiniteSet(a, b)
|
| 636 |
+
assert A.evalf(subs={a: 1, b: 2}) == FiniteSet(1.0, 2.0)
|
| 637 |
+
B = FiniteSet(a-b, 1)
|
| 638 |
+
assert B.evalf(subs={a: 1, b: 2}) == FiniteSet(-1.0, 1.0)
|
| 639 |
+
|
| 640 |
+
sol = Complement(Intersection(FiniteSet(-b/2 - sqrt(b**2-4*pi)/2), Reals), FiniteSet(0))
|
| 641 |
+
assert sol.evalf(subs={b: 1}) == EmptySet
|
| 642 |
+
|
| 643 |
+
|
| 644 |
+
def test_evalf_with_zoo():
|
| 645 |
+
assert (1/x).evalf(subs={x: 0}) == zoo # issue 8242
|
| 646 |
+
assert (-1/x).evalf(subs={x: 0}) == zoo # PR 16150
|
| 647 |
+
assert (0 ** x).evalf(subs={x: -1}) == zoo # PR 16150
|
| 648 |
+
assert (0 ** x).evalf(subs={x: -1 + I}) == nan
|
| 649 |
+
assert Mul(2, Pow(0, -1, evaluate=False), evaluate=False).evalf() == zoo # issue 21147
|
| 650 |
+
assert Mul(x, 1/x, evaluate=False).evalf(subs={x: 0}) == Mul(x, 1/x, evaluate=False).subs(x, 0) == nan
|
| 651 |
+
assert Mul(1/x, 1/x, evaluate=False).evalf(subs={x: 0}) == zoo
|
| 652 |
+
assert Mul(1/x, Abs(1/x), evaluate=False).evalf(subs={x: 0}) == zoo
|
| 653 |
+
assert Abs(zoo, evaluate=False).evalf() == oo
|
| 654 |
+
assert re(zoo, evaluate=False).evalf() == nan
|
| 655 |
+
assert im(zoo, evaluate=False).evalf() == nan
|
| 656 |
+
assert Add(zoo, zoo, evaluate=False).evalf() == nan
|
| 657 |
+
assert Add(oo, zoo, evaluate=False).evalf() == nan
|
| 658 |
+
assert Pow(zoo, -1, evaluate=False).evalf() == 0
|
| 659 |
+
assert Pow(zoo, Rational(-1, 3), evaluate=False).evalf() == 0
|
| 660 |
+
assert Pow(zoo, Rational(1, 3), evaluate=False).evalf() == zoo
|
| 661 |
+
assert Pow(zoo, S.Half, evaluate=False).evalf() == zoo
|
| 662 |
+
assert Pow(zoo, 2, evaluate=False).evalf() == zoo
|
| 663 |
+
assert Pow(0, zoo, evaluate=False).evalf() == nan
|
| 664 |
+
assert log(zoo, evaluate=False).evalf() == zoo
|
| 665 |
+
assert zoo.evalf(chop=True) == zoo
|
| 666 |
+
assert x.evalf(subs={x: zoo}) == zoo
|
| 667 |
+
|
| 668 |
+
|
| 669 |
+
def test_evalf_with_bounded_error():
|
| 670 |
+
cases = [
|
| 671 |
+
# zero
|
| 672 |
+
(Rational(0), None, 1),
|
| 673 |
+
# zero im part
|
| 674 |
+
(pi, None, 10),
|
| 675 |
+
# zero real part
|
| 676 |
+
(pi*I, None, 10),
|
| 677 |
+
# re and im nonzero
|
| 678 |
+
(2-3*I, None, 5),
|
| 679 |
+
# similar tests again, but using eps instead of m
|
| 680 |
+
(Rational(0), Rational(1, 2), None),
|
| 681 |
+
(pi, Rational(1, 1000), None),
|
| 682 |
+
(pi * I, Rational(1, 1000), None),
|
| 683 |
+
(2 - 3 * I, Rational(1, 1000), None),
|
| 684 |
+
# very large eps
|
| 685 |
+
(2 - 3 * I, Rational(1000), None),
|
| 686 |
+
# case where x already small, hence some cancellation in p = m + n - 1
|
| 687 |
+
(Rational(1234, 10**8), Rational(1, 10**12), None),
|
| 688 |
+
]
|
| 689 |
+
for x0, eps, m in cases:
|
| 690 |
+
a, b, _, _ = evalf(x0, 53, {})
|
| 691 |
+
c, d, _, _ = _evalf_with_bounded_error(x0, eps, m)
|
| 692 |
+
if eps is None:
|
| 693 |
+
eps = 2**(-m)
|
| 694 |
+
z = make_mpc((a or fzero, b or fzero))
|
| 695 |
+
w = make_mpc((c or fzero, d or fzero))
|
| 696 |
+
assert abs(w - z) < eps
|
| 697 |
+
|
| 698 |
+
# eps must be positive
|
| 699 |
+
raises(ValueError, lambda: _evalf_with_bounded_error(pi, Rational(0)))
|
| 700 |
+
raises(ValueError, lambda: _evalf_with_bounded_error(pi, -pi))
|
| 701 |
+
raises(ValueError, lambda: _evalf_with_bounded_error(pi, I))
|
| 702 |
+
|
| 703 |
+
|
| 704 |
+
def test_issue_22849():
|
| 705 |
+
a = -8 + 3 * sqrt(3)
|
| 706 |
+
x = AlgebraicNumber(a)
|
| 707 |
+
assert evalf(a, 1, {}) == evalf(x, 1, {})
|
| 708 |
+
|
| 709 |
+
|
| 710 |
+
def test_evalf_real_alg_num():
|
| 711 |
+
# This test demonstrates why the entry for `AlgebraicNumber` in
|
| 712 |
+
# `sympy.core.evalf._create_evalf_table()` has to use `x.to_root()`,
|
| 713 |
+
# instead of `x.as_expr()`. If the latter is used, then `z` will be
|
| 714 |
+
# a complex number with `0.e-20` for imaginary part, even though `a5`
|
| 715 |
+
# is a real number.
|
| 716 |
+
zeta = Symbol('zeta')
|
| 717 |
+
a5 = AlgebraicNumber(CRootOf(cyclotomic_poly(5), -1), [-1, -1, 0, 0], alias=zeta)
|
| 718 |
+
z = a5.evalf()
|
| 719 |
+
assert isinstance(z, Float)
|
| 720 |
+
assert not hasattr(z, '_mpc_')
|
| 721 |
+
assert hasattr(z, '_mpf_')
|
| 722 |
+
|
| 723 |
+
|
| 724 |
+
def test_issue_20733():
|
| 725 |
+
expr = 1/((x - 9)*(x - 8)*(x - 7)*(x - 4)**2*(x - 3)**3*(x - 2))
|
| 726 |
+
assert str(expr.evalf(1, subs={x:1})) == '-4.e-5'
|
| 727 |
+
assert str(expr.evalf(2, subs={x:1})) == '-4.1e-5'
|
| 728 |
+
assert str(expr.evalf(11, subs={x:1})) == '-4.1335978836e-5'
|
| 729 |
+
assert str(expr.evalf(20, subs={x:1})) == '-0.000041335978835978835979'
|
| 730 |
+
|
| 731 |
+
expr = Mul(*((x - i) for i in range(2, 1000)))
|
| 732 |
+
assert srepr(expr.evalf(2, subs={x: 1})) == "Float('4.0271e+2561', precision=10)"
|
| 733 |
+
assert srepr(expr.evalf(10, subs={x: 1})) == "Float('4.02790050126e+2561', precision=37)"
|
| 734 |
+
assert srepr(expr.evalf(53, subs={x: 1})) == "Float('4.0279005012722099453824067459760158730668154575647110393e+2561', precision=179)"
|