ZTWHHH commited on
Commit
fa82258
·
verified ·
1 Parent(s): 3db1b31

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +2 -0
  2. wemm/lib/python3.10/site-packages/sympy/benchmarks/__pycache__/bench_discrete_log.cpython-310.pyc +0 -0
  3. wemm/lib/python3.10/site-packages/sympy/benchmarks/bench_discrete_log.py +83 -0
  4. wemm/lib/python3.10/site-packages/sympy/benchmarks/bench_meijerint.py +261 -0
  5. wemm/lib/python3.10/site-packages/sympy/benchmarks/bench_symbench.py +134 -0
  6. wemm/lib/python3.10/site-packages/sympy/crypto/__pycache__/__init__.cpython-310.pyc +0 -0
  7. wemm/lib/python3.10/site-packages/sympy/crypto/__pycache__/crypto.cpython-310.pyc +0 -0
  8. wemm/lib/python3.10/site-packages/sympy/logic/__pycache__/__init__.cpython-310.pyc +0 -0
  9. wemm/lib/python3.10/site-packages/sympy/logic/__pycache__/inference.cpython-310.pyc +0 -0
  10. wemm/lib/python3.10/site-packages/sympy/logic/algorithms/__pycache__/__init__.cpython-310.pyc +0 -0
  11. wemm/lib/python3.10/site-packages/sympy/logic/algorithms/__pycache__/dpll.cpython-310.pyc +0 -0
  12. wemm/lib/python3.10/site-packages/sympy/logic/algorithms/__pycache__/dpll2.cpython-310.pyc +0 -0
  13. wemm/lib/python3.10/site-packages/sympy/logic/algorithms/__pycache__/lra_theory.cpython-310.pyc +0 -0
  14. wemm/lib/python3.10/site-packages/sympy/logic/algorithms/__pycache__/minisat22_wrapper.cpython-310.pyc +0 -0
  15. wemm/lib/python3.10/site-packages/sympy/logic/algorithms/__pycache__/pycosat_wrapper.cpython-310.pyc +0 -0
  16. wemm/lib/python3.10/site-packages/sympy/logic/algorithms/__pycache__/z3_wrapper.cpython-310.pyc +0 -0
  17. wemm/lib/python3.10/site-packages/sympy/logic/algorithms/dpll.py +308 -0
  18. wemm/lib/python3.10/site-packages/sympy/logic/tests/__init__.py +0 -0
  19. wemm/lib/python3.10/site-packages/sympy/logic/tests/__pycache__/__init__.cpython-310.pyc +0 -0
  20. wemm/lib/python3.10/site-packages/sympy/logic/tests/__pycache__/test_boolalg.cpython-310.pyc +0 -0
  21. wemm/lib/python3.10/site-packages/sympy/logic/tests/__pycache__/test_dimacs.cpython-310.pyc +0 -0
  22. wemm/lib/python3.10/site-packages/sympy/logic/tests/__pycache__/test_inference.cpython-310.pyc +0 -0
  23. wemm/lib/python3.10/site-packages/sympy/logic/tests/__pycache__/test_lra_theory.cpython-310.pyc +0 -0
  24. wemm/lib/python3.10/site-packages/sympy/logic/tests/test_boolalg.py +1352 -0
  25. wemm/lib/python3.10/site-packages/sympy/logic/tests/test_dimacs.py +234 -0
  26. wemm/lib/python3.10/site-packages/sympy/logic/tests/test_inference.py +381 -0
  27. wemm/lib/python3.10/site-packages/sympy/logic/tests/test_lra_theory.py +440 -0
  28. wemm/lib/python3.10/site-packages/sympy/logic/utilities/__init__.py +3 -0
  29. wemm/lib/python3.10/site-packages/sympy/logic/utilities/__pycache__/__init__.cpython-310.pyc +0 -0
  30. wemm/lib/python3.10/site-packages/sympy/logic/utilities/__pycache__/dimacs.cpython-310.pyc +0 -0
  31. wemm/lib/python3.10/site-packages/sympy/logic/utilities/dimacs.py +70 -0
  32. wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/__pycache__/interval_arithmetic.cpython-310.pyc +0 -0
  33. wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/__pycache__/interval_membership.cpython-310.pyc +0 -0
  34. wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/__pycache__/lib_interval.cpython-310.pyc +0 -0
  35. wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/lib_interval.py +452 -0
  36. wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__init__.py +0 -0
  37. wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__pycache__/__init__.cpython-310.pyc +0 -0
  38. wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__pycache__/test_interval_functions.cpython-310.pyc +0 -0
  39. wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__pycache__/test_intervalmath.cpython-310.pyc +0 -0
  40. wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/test_interval_membership.py +150 -0
  41. wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/test_intervalmath.py +213 -0
  42. wemm/lib/python3.10/site-packages/sympy/plotting/tests/test_region_and.png +3 -0
  43. wemm/lib/python3.10/site-packages/sympy/plotting/tests/test_region_not.png +3 -0
  44. wemm/lib/python3.10/site-packages/sympy/plotting/tests/test_region_or.png +3 -0
  45. wemm/lib/python3.10/site-packages/sympy/plotting/tests/test_region_xor.png +3 -0
  46. wemm/lib/python3.10/site-packages/sympy/printing/__pycache__/latex.cpython-310.pyc +3 -0
  47. wemm/lib/python3.10/site-packages/sympy/printing/pretty/tests/__pycache__/test_pretty.cpython-310.pyc +3 -0
  48. wemm/lib/python3.10/site-packages/sympy/series/__init__.py +23 -0
  49. wemm/lib/python3.10/site-packages/sympy/series/__pycache__/approximants.cpython-310.pyc +0 -0
  50. wemm/lib/python3.10/site-packages/sympy/series/__pycache__/aseries.cpython-310.pyc +0 -0
.gitattributes CHANGED
@@ -204,3 +204,5 @@ wemm/lib/python3.10/site-packages/sympy/polys/matrices/__pycache__/domainmatrix.
204
  wemm/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_polytools.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
205
  parrot/lib/python3.10/site-packages/numpy/_core/_multiarray_umath.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
206
  wemm/lib/python3.10/site-packages/sympy/polys/__pycache__/polytools.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
 
 
 
204
  wemm/lib/python3.10/site-packages/sympy/polys/tests/__pycache__/test_polytools.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
205
  parrot/lib/python3.10/site-packages/numpy/_core/_multiarray_umath.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
206
  wemm/lib/python3.10/site-packages/sympy/polys/__pycache__/polytools.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
207
+ wemm/lib/python3.10/site-packages/sympy/printing/__pycache__/latex.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
208
+ wemm/lib/python3.10/site-packages/sympy/printing/pretty/tests/__pycache__/test_pretty.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
wemm/lib/python3.10/site-packages/sympy/benchmarks/__pycache__/bench_discrete_log.cpython-310.pyc ADDED
Binary file (1.99 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/benchmarks/bench_discrete_log.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ from time import time
3
+ from sympy.ntheory.residue_ntheory import (discrete_log,
4
+ _discrete_log_trial_mul, _discrete_log_shanks_steps,
5
+ _discrete_log_pollard_rho, _discrete_log_pohlig_hellman)
6
+
7
+
8
+ # Cyclic group (Z/pZ)* with p prime, order p - 1 and generator g
9
+ data_set_1 = [
10
+ # p, p - 1, g
11
+ [191, 190, 19],
12
+ [46639, 46638, 6],
13
+ [14789363, 14789362, 2],
14
+ [4254225211, 4254225210, 2],
15
+ [432751500361, 432751500360, 7],
16
+ [158505390797053, 158505390797052, 2],
17
+ [6575202655312007, 6575202655312006, 5],
18
+ [8430573471995353769, 8430573471995353768, 3],
19
+ [3938471339744997827267, 3938471339744997827266, 2],
20
+ [875260951364705563393093, 875260951364705563393092, 5],
21
+ ]
22
+
23
+
24
+ # Cyclic sub-groups of (Z/nZ)* with prime order p and generator g
25
+ # (n, p are primes and n = 2 * p + 1)
26
+ data_set_2 = [
27
+ # n, p, g
28
+ [227, 113, 3],
29
+ [2447, 1223, 2],
30
+ [24527, 12263, 2],
31
+ [245639, 122819, 2],
32
+ [2456747, 1228373, 3],
33
+ [24567899, 12283949, 3],
34
+ [245679023, 122839511, 2],
35
+ [2456791307, 1228395653, 3],
36
+ [24567913439, 12283956719, 2],
37
+ [245679135407, 122839567703, 2],
38
+ [2456791354763, 1228395677381, 3],
39
+ [24567913550903, 12283956775451, 2],
40
+ [245679135509519, 122839567754759, 2],
41
+ ]
42
+
43
+
44
+ # Cyclic sub-groups of (Z/nZ)* with smooth order o and generator g
45
+ data_set_3 = [
46
+ # n, o, g
47
+ [2**118, 2**116, 3],
48
+ ]
49
+
50
+
51
+ def bench_discrete_log(data_set, algo=None):
52
+ if algo is None:
53
+ f = discrete_log
54
+ elif algo == 'trial':
55
+ f = _discrete_log_trial_mul
56
+ elif algo == 'shanks':
57
+ f = _discrete_log_shanks_steps
58
+ elif algo == 'rho':
59
+ f = _discrete_log_pollard_rho
60
+ elif algo == 'ph':
61
+ f = _discrete_log_pohlig_hellman
62
+ else:
63
+ raise ValueError("Argument 'algo' should be one"
64
+ " of ('trial', 'shanks', 'rho' or 'ph')")
65
+
66
+ for i, data in enumerate(data_set):
67
+ for j, (n, p, g) in enumerate(data):
68
+ t = time()
69
+ l = f(n, pow(g, p - 1, n), g, p)
70
+ t = time() - t
71
+ print('[%02d-%03d] %15.10f' % (i, j, t))
72
+ assert l == p - 1
73
+
74
+
75
+ if __name__ == '__main__':
76
+ algo = sys.argv[1] \
77
+ if len(sys.argv) > 1 else None
78
+ data_set = [
79
+ data_set_1,
80
+ data_set_2,
81
+ data_set_3,
82
+ ]
83
+ bench_discrete_log(data_set, algo)
wemm/lib/python3.10/site-packages/sympy/benchmarks/bench_meijerint.py ADDED
@@ -0,0 +1,261 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # conceal the implicit import from the code quality tester
2
+ from sympy.core.numbers import (oo, pi)
3
+ from sympy.core.symbol import (Symbol, symbols)
4
+ from sympy.functions.elementary.exponential import exp
5
+ from sympy.functions.elementary.miscellaneous import sqrt
6
+ from sympy.functions.special.bessel import besseli
7
+ from sympy.functions.special.gamma_functions import gamma
8
+ from sympy.integrals.integrals import integrate
9
+ from sympy.integrals.transforms import (mellin_transform,
10
+ inverse_fourier_transform, inverse_mellin_transform,
11
+ laplace_transform, inverse_laplace_transform, fourier_transform)
12
+
13
+ LT = laplace_transform
14
+ FT = fourier_transform
15
+ MT = mellin_transform
16
+ IFT = inverse_fourier_transform
17
+ ILT = inverse_laplace_transform
18
+ IMT = inverse_mellin_transform
19
+
20
+ from sympy.abc import x, y
21
+ nu, beta, rho = symbols('nu beta rho')
22
+
23
+ apos, bpos, cpos, dpos, posk, p = symbols('a b c d k p', positive=True)
24
+ k = Symbol('k', real=True)
25
+ negk = Symbol('k', negative=True)
26
+
27
+ mu1, mu2 = symbols('mu1 mu2', real=True, nonzero=True, finite=True)
28
+ sigma1, sigma2 = symbols('sigma1 sigma2', real=True, nonzero=True,
29
+ finite=True, positive=True)
30
+ rate = Symbol('lambda', positive=True)
31
+
32
+
33
+ def normal(x, mu, sigma):
34
+ return 1/sqrt(2*pi*sigma**2)*exp(-(x - mu)**2/2/sigma**2)
35
+
36
+
37
+ def exponential(x, rate):
38
+ return rate*exp(-rate*x)
39
+ alpha, beta = symbols('alpha beta', positive=True)
40
+ betadist = x**(alpha - 1)*(1 + x)**(-alpha - beta)*gamma(alpha + beta) \
41
+ /gamma(alpha)/gamma(beta)
42
+ kint = Symbol('k', integer=True, positive=True)
43
+ chi = 2**(1 - kint/2)*x**(kint - 1)*exp(-x**2/2)/gamma(kint/2)
44
+ chisquared = 2**(-k/2)/gamma(k/2)*x**(k/2 - 1)*exp(-x/2)
45
+ dagum = apos*p/x*(x/bpos)**(apos*p)/(1 + x**apos/bpos**apos)**(p + 1)
46
+ d1, d2 = symbols('d1 d2', positive=True)
47
+ f = sqrt(((d1*x)**d1 * d2**d2)/(d1*x + d2)**(d1 + d2))/x \
48
+ /gamma(d1/2)/gamma(d2/2)*gamma((d1 + d2)/2)
49
+ nupos, sigmapos = symbols('nu sigma', positive=True)
50
+ rice = x/sigmapos**2*exp(-(x**2 + nupos**2)/2/sigmapos**2)*besseli(0, x*
51
+ nupos/sigmapos**2)
52
+ mu = Symbol('mu', real=True)
53
+ laplace = exp(-abs(x - mu)/bpos)/2/bpos
54
+
55
+ u = Symbol('u', polar=True)
56
+ tpos = Symbol('t', positive=True)
57
+
58
+
59
+ def E(expr):
60
+ integrate(expr*exponential(x, rate)*normal(y, mu1, sigma1),
61
+ (x, 0, oo), (y, -oo, oo), meijerg=True)
62
+ integrate(expr*exponential(x, rate)*normal(y, mu1, sigma1),
63
+ (y, -oo, oo), (x, 0, oo), meijerg=True)
64
+
65
+ bench = [
66
+ 'MT(x**nu*Heaviside(x - 1), x, s)',
67
+ 'MT(x**nu*Heaviside(1 - x), x, s)',
68
+ 'MT((1-x)**(beta - 1)*Heaviside(1-x), x, s)',
69
+ 'MT((x-1)**(beta - 1)*Heaviside(x-1), x, s)',
70
+ 'MT((1+x)**(-rho), x, s)',
71
+ 'MT(abs(1-x)**(-rho), x, s)',
72
+ 'MT((1-x)**(beta-1)*Heaviside(1-x) + a*(x-1)**(beta-1)*Heaviside(x-1), x, s)',
73
+ 'MT((x**a-b**a)/(x-b), x, s)',
74
+ 'MT((x**a-bpos**a)/(x-bpos), x, s)',
75
+ 'MT(exp(-x), x, s)',
76
+ 'MT(exp(-1/x), x, s)',
77
+ 'MT(log(x)**4*Heaviside(1-x), x, s)',
78
+ 'MT(log(x)**3*Heaviside(x-1), x, s)',
79
+ 'MT(log(x + 1), x, s)',
80
+ 'MT(log(1/x + 1), x, s)',
81
+ 'MT(log(abs(1 - x)), x, s)',
82
+ 'MT(log(abs(1 - 1/x)), x, s)',
83
+ 'MT(log(x)/(x+1), x, s)',
84
+ 'MT(log(x)**2/(x+1), x, s)',
85
+ 'MT(log(x)/(x+1)**2, x, s)',
86
+ 'MT(erf(sqrt(x)), x, s)',
87
+
88
+ 'MT(besselj(a, 2*sqrt(x)), x, s)',
89
+ 'MT(sin(sqrt(x))*besselj(a, sqrt(x)), x, s)',
90
+ 'MT(cos(sqrt(x))*besselj(a, sqrt(x)), x, s)',
91
+ 'MT(besselj(a, sqrt(x))**2, x, s)',
92
+ 'MT(besselj(a, sqrt(x))*besselj(-a, sqrt(x)), x, s)',
93
+ 'MT(besselj(a - 1, sqrt(x))*besselj(a, sqrt(x)), x, s)',
94
+ 'MT(besselj(a, sqrt(x))*besselj(b, sqrt(x)), x, s)',
95
+ 'MT(besselj(a, sqrt(x))**2 + besselj(-a, sqrt(x))**2, x, s)',
96
+ 'MT(bessely(a, 2*sqrt(x)), x, s)',
97
+ 'MT(sin(sqrt(x))*bessely(a, sqrt(x)), x, s)',
98
+ 'MT(cos(sqrt(x))*bessely(a, sqrt(x)), x, s)',
99
+ 'MT(besselj(a, sqrt(x))*bessely(a, sqrt(x)), x, s)',
100
+ 'MT(besselj(a, sqrt(x))*bessely(b, sqrt(x)), x, s)',
101
+ 'MT(bessely(a, sqrt(x))**2, x, s)',
102
+
103
+ 'MT(besselk(a, 2*sqrt(x)), x, s)',
104
+ 'MT(besselj(a, 2*sqrt(2*sqrt(x)))*besselk(a, 2*sqrt(2*sqrt(x))), x, s)',
105
+ 'MT(besseli(a, sqrt(x))*besselk(a, sqrt(x)), x, s)',
106
+ 'MT(besseli(b, sqrt(x))*besselk(a, sqrt(x)), x, s)',
107
+ 'MT(exp(-x/2)*besselk(a, x/2), x, s)',
108
+
109
+ # later: ILT, IMT
110
+
111
+ 'LT((t-apos)**bpos*exp(-cpos*(t-apos))*Heaviside(t-apos), t, s)',
112
+ 'LT(t**apos, t, s)',
113
+ 'LT(Heaviside(t), t, s)',
114
+ 'LT(Heaviside(t - apos), t, s)',
115
+ 'LT(1 - exp(-apos*t), t, s)',
116
+ 'LT((exp(2*t)-1)*exp(-bpos - t)*Heaviside(t)/2, t, s, noconds=True)',
117
+ 'LT(exp(t), t, s)',
118
+ 'LT(exp(2*t), t, s)',
119
+ 'LT(exp(apos*t), t, s)',
120
+ 'LT(log(t/apos), t, s)',
121
+ 'LT(erf(t), t, s)',
122
+ 'LT(sin(apos*t), t, s)',
123
+ 'LT(cos(apos*t), t, s)',
124
+ 'LT(exp(-apos*t)*sin(bpos*t), t, s)',
125
+ 'LT(exp(-apos*t)*cos(bpos*t), t, s)',
126
+ 'LT(besselj(0, t), t, s, noconds=True)',
127
+ 'LT(besselj(1, t), t, s, noconds=True)',
128
+
129
+ 'FT(Heaviside(1 - abs(2*apos*x)), x, k)',
130
+ 'FT(Heaviside(1-abs(apos*x))*(1-abs(apos*x)), x, k)',
131
+ 'FT(exp(-apos*x)*Heaviside(x), x, k)',
132
+ 'IFT(1/(apos + 2*pi*I*x), x, posk, noconds=False)',
133
+ 'IFT(1/(apos + 2*pi*I*x), x, -posk, noconds=False)',
134
+ 'IFT(1/(apos + 2*pi*I*x), x, negk)',
135
+ 'FT(x*exp(-apos*x)*Heaviside(x), x, k)',
136
+ 'FT(exp(-apos*x)*sin(bpos*x)*Heaviside(x), x, k)',
137
+ 'FT(exp(-apos*x**2), x, k)',
138
+ 'IFT(sqrt(pi/apos)*exp(-(pi*k)**2/apos), k, x)',
139
+ 'FT(exp(-apos*abs(x)), x, k)',
140
+
141
+ 'integrate(normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True)',
142
+ 'integrate(x*normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True)',
143
+ 'integrate(x**2*normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True)',
144
+ 'integrate(x**3*normal(x, mu1, sigma1), (x, -oo, oo), meijerg=True)',
145
+ 'integrate(normal(x, mu1, sigma1)*normal(y, mu2, sigma2),'
146
+ ' (x, -oo, oo), (y, -oo, oo), meijerg=True)',
147
+ 'integrate(x*normal(x, mu1, sigma1)*normal(y, mu2, sigma2),'
148
+ ' (x, -oo, oo), (y, -oo, oo), meijerg=True)',
149
+ 'integrate(y*normal(x, mu1, sigma1)*normal(y, mu2, sigma2),'
150
+ ' (x, -oo, oo), (y, -oo, oo), meijerg=True)',
151
+ 'integrate(x*y*normal(x, mu1, sigma1)*normal(y, mu2, sigma2),'
152
+ ' (x, -oo, oo), (y, -oo, oo), meijerg=True)',
153
+ 'integrate((x+y+1)*normal(x, mu1, sigma1)*normal(y, mu2, sigma2),'
154
+ ' (x, -oo, oo), (y, -oo, oo), meijerg=True)',
155
+ 'integrate((x+y-1)*normal(x, mu1, sigma1)*normal(y, mu2, sigma2),'
156
+ ' (x, -oo, oo), (y, -oo, oo), meijerg=True)',
157
+ 'integrate(x**2*normal(x, mu1, sigma1)*normal(y, mu2, sigma2),'
158
+ ' (x, -oo, oo), (y, -oo, oo), meijerg=True)',
159
+ 'integrate(y**2*normal(x, mu1, sigma1)*normal(y, mu2, sigma2),'
160
+ ' (x, -oo, oo), (y, -oo, oo), meijerg=True)',
161
+ 'integrate(exponential(x, rate), (x, 0, oo), meijerg=True)',
162
+ 'integrate(x*exponential(x, rate), (x, 0, oo), meijerg=True)',
163
+ 'integrate(x**2*exponential(x, rate), (x, 0, oo), meijerg=True)',
164
+ 'E(1)',
165
+ 'E(x*y)',
166
+ 'E(x*y**2)',
167
+ 'E((x+y+1)**2)',
168
+ 'E(x+y+1)',
169
+ 'E((x+y-1)**2)',
170
+ 'integrate(betadist, (x, 0, oo), meijerg=True)',
171
+ 'integrate(x*betadist, (x, 0, oo), meijerg=True)',
172
+ 'integrate(x**2*betadist, (x, 0, oo), meijerg=True)',
173
+ 'integrate(chi, (x, 0, oo), meijerg=True)',
174
+ 'integrate(x*chi, (x, 0, oo), meijerg=True)',
175
+ 'integrate(x**2*chi, (x, 0, oo), meijerg=True)',
176
+ 'integrate(chisquared, (x, 0, oo), meijerg=True)',
177
+ 'integrate(x*chisquared, (x, 0, oo), meijerg=True)',
178
+ 'integrate(x**2*chisquared, (x, 0, oo), meijerg=True)',
179
+ 'integrate(((x-k)/sqrt(2*k))**3*chisquared, (x, 0, oo), meijerg=True)',
180
+ 'integrate(dagum, (x, 0, oo), meijerg=True)',
181
+ 'integrate(x*dagum, (x, 0, oo), meijerg=True)',
182
+ 'integrate(x**2*dagum, (x, 0, oo), meijerg=True)',
183
+ 'integrate(f, (x, 0, oo), meijerg=True)',
184
+ 'integrate(x*f, (x, 0, oo), meijerg=True)',
185
+ 'integrate(x**2*f, (x, 0, oo), meijerg=True)',
186
+ 'integrate(rice, (x, 0, oo), meijerg=True)',
187
+ 'integrate(laplace, (x, -oo, oo), meijerg=True)',
188
+ 'integrate(x*laplace, (x, -oo, oo), meijerg=True)',
189
+ 'integrate(x**2*laplace, (x, -oo, oo), meijerg=True)',
190
+ 'integrate(log(x) * x**(k-1) * exp(-x) / gamma(k), (x, 0, oo))',
191
+
192
+ 'integrate(sin(z*x)*(x**2-1)**(-(y+S(1)/2)), (x, 1, oo), meijerg=True)',
193
+ 'integrate(besselj(0,x)*besselj(1,x)*exp(-x**2), (x, 0, oo), meijerg=True)',
194
+ 'integrate(besselj(0,x)*besselj(1,x)*besselk(0,x), (x, 0, oo), meijerg=True)',
195
+ 'integrate(besselj(0,x)*besselj(1,x)*exp(-x**2), (x, 0, oo), meijerg=True)',
196
+ 'integrate(besselj(a,x)*besselj(b,x)/x, (x,0,oo), meijerg=True)',
197
+
198
+ 'hyperexpand(meijerg((-s - a/2 + 1, -s + a/2 + 1), (-a/2 - S(1)/2, -s + a/2 + S(3)/2), (a/2, -a/2), (-a/2 - S(1)/2, -s + a/2 + S(3)/2), 1))',
199
+ "gammasimp(S('2**(2*s)*(-pi*gamma(-a + 1)*gamma(a + 1)*gamma(-a - s + 1)*gamma(-a + s - 1/2)*gamma(a - s + 3/2)*gamma(a + s + 1)/(a*(a + s)) - gamma(-a - 1/2)*gamma(-a + 1)*gamma(a + 1)*gamma(a + 3/2)*gamma(-s + 3/2)*gamma(s - 1/2)*gamma(-a + s + 1)*gamma(a - s + 1)/(a*(-a + s)))*gamma(-2*s + 1)*gamma(s + 1)/(pi*s*gamma(-a - 1/2)*gamma(a + 3/2)*gamma(-s + 1)*gamma(-s + 3/2)*gamma(s - 1/2)*gamma(-a - s + 1)*gamma(-a + s - 1/2)*gamma(a - s + 1)*gamma(a - s + 3/2))'))",
200
+
201
+ 'mellin_transform(E1(x), x, s)',
202
+ 'inverse_mellin_transform(gamma(s)/s, s, x, (0, oo))',
203
+ 'mellin_transform(expint(a, x), x, s)',
204
+ 'mellin_transform(Si(x), x, s)',
205
+ 'inverse_mellin_transform(-2**s*sqrt(pi)*gamma((s + 1)/2)/(2*s*gamma(-s/2 + 1)), s, x, (-1, 0))',
206
+ 'mellin_transform(Ci(sqrt(x)), x, s)',
207
+ 'inverse_mellin_transform(-4**s*sqrt(pi)*gamma(s)/(2*s*gamma(-s + S(1)/2)),s, u, (0, 1))',
208
+ 'laplace_transform(Ci(x), x, s)',
209
+ 'laplace_transform(expint(a, x), x, s)',
210
+ 'laplace_transform(expint(1, x), x, s)',
211
+ 'laplace_transform(expint(2, x), x, s)',
212
+ 'inverse_laplace_transform(-log(1 + s**2)/2/s, s, u)',
213
+ 'inverse_laplace_transform(log(s + 1)/s, s, x)',
214
+ 'inverse_laplace_transform((s - log(s + 1))/s**2, s, x)',
215
+ 'laplace_transform(Chi(x), x, s)',
216
+ 'laplace_transform(Shi(x), x, s)',
217
+
218
+ 'integrate(exp(-z*x)/x, (x, 1, oo), meijerg=True, conds="none")',
219
+ 'integrate(exp(-z*x)/x**2, (x, 1, oo), meijerg=True, conds="none")',
220
+ 'integrate(exp(-z*x)/x**3, (x, 1, oo), meijerg=True,conds="none")',
221
+ 'integrate(-cos(x)/x, (x, tpos, oo), meijerg=True)',
222
+ 'integrate(-sin(x)/x, (x, tpos, oo), meijerg=True)',
223
+ 'integrate(sin(x)/x, (x, 0, z), meijerg=True)',
224
+ 'integrate(sinh(x)/x, (x, 0, z), meijerg=True)',
225
+ 'integrate(exp(-x)/x, x, meijerg=True)',
226
+ 'integrate(exp(-x)/x**2, x, meijerg=True)',
227
+ 'integrate(cos(u)/u, u, meijerg=True)',
228
+ 'integrate(cosh(u)/u, u, meijerg=True)',
229
+ 'integrate(expint(1, x), x, meijerg=True)',
230
+ 'integrate(expint(2, x), x, meijerg=True)',
231
+ 'integrate(Si(x), x, meijerg=True)',
232
+ 'integrate(Ci(u), u, meijerg=True)',
233
+ 'integrate(Shi(x), x, meijerg=True)',
234
+ 'integrate(Chi(u), u, meijerg=True)',
235
+ 'integrate(Si(x)*exp(-x), (x, 0, oo), meijerg=True)',
236
+ 'integrate(expint(1, x)*sin(x), (x, 0, oo), meijerg=True)'
237
+ ]
238
+
239
+ from time import time
240
+ from sympy.core.cache import clear_cache
241
+ import sys
242
+
243
+ timings = []
244
+
245
+ if __name__ == '__main__':
246
+ for n, string in enumerate(bench):
247
+ clear_cache()
248
+ _t = time()
249
+ exec(string)
250
+ _t = time() - _t
251
+ timings += [(_t, string)]
252
+ sys.stdout.write('.')
253
+ sys.stdout.flush()
254
+ if n % (len(bench) // 10) == 0:
255
+ sys.stdout.write('%s' % (10*n // len(bench)))
256
+ print()
257
+
258
+ timings.sort(key=lambda x: -x[0])
259
+
260
+ for ti, string in timings:
261
+ print('%.2fs %s' % (ti, string))
wemm/lib/python3.10/site-packages/sympy/benchmarks/bench_symbench.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ from sympy.core.random import random
3
+ from sympy.core.numbers import (I, Integer, pi)
4
+ from sympy.core.symbol import Symbol
5
+ from sympy.core.sympify import sympify
6
+ from sympy.functions.elementary.miscellaneous import sqrt
7
+ from sympy.functions.elementary.trigonometric import sin
8
+ from sympy.polys.polytools import factor
9
+ from sympy.simplify.simplify import simplify
10
+ from sympy.abc import x, y, z
11
+ from timeit import default_timer as clock
12
+
13
+
14
+ def bench_R1():
15
+ "real(f(f(f(f(f(f(f(f(f(f(i/2)))))))))))"
16
+ def f(z):
17
+ return sqrt(Integer(1)/3)*z**2 + I/3
18
+ f(f(f(f(f(f(f(f(f(f(I/2)))))))))).as_real_imag()[0]
19
+
20
+
21
+ def bench_R2():
22
+ "Hermite polynomial hermite(15, y)"
23
+ def hermite(n, y):
24
+ if n == 1:
25
+ return 2*y
26
+ if n == 0:
27
+ return 1
28
+ return (2*y*hermite(n - 1, y) - 2*(n - 1)*hermite(n - 2, y)).expand()
29
+
30
+ hermite(15, y)
31
+
32
+
33
+ def bench_R3():
34
+ "a = [bool(f==f) for _ in range(10)]"
35
+ f = x + y + z
36
+ [bool(f == f) for _ in range(10)]
37
+
38
+
39
+ def bench_R4():
40
+ # we don't have Tuples
41
+ pass
42
+
43
+
44
+ def bench_R5():
45
+ "blowup(L, 8); L=uniq(L)"
46
+ def blowup(L, n):
47
+ for i in range(n):
48
+ L.append( (L[i] + L[i + 1]) * L[i + 2] )
49
+
50
+ def uniq(x):
51
+ v = set(x)
52
+ return v
53
+ L = [x, y, z]
54
+ blowup(L, 8)
55
+ L = uniq(L)
56
+
57
+
58
+ def bench_R6():
59
+ "sum(simplify((x+sin(i))/x+(x-sin(i))/x) for i in range(100))"
60
+ sum(simplify((x + sin(i))/x + (x - sin(i))/x) for i in range(100))
61
+
62
+
63
+ def bench_R7():
64
+ "[f.subs(x, random()) for _ in range(10**4)]"
65
+ f = x**24 + 34*x**12 + 45*x**3 + 9*x**18 + 34*x**10 + 32*x**21
66
+ [f.subs(x, random()) for _ in range(10**4)]
67
+
68
+
69
+ def bench_R8():
70
+ "right(x^2,0,5,10^4)"
71
+ def right(f, a, b, n):
72
+ a = sympify(a)
73
+ b = sympify(b)
74
+ n = sympify(n)
75
+ x = f.atoms(Symbol).pop()
76
+ Deltax = (b - a)/n
77
+ c = a
78
+ est = 0
79
+ for i in range(n):
80
+ c += Deltax
81
+ est += f.subs(x, c)
82
+ return est*Deltax
83
+
84
+ right(x**2, 0, 5, 10**4)
85
+
86
+
87
+ def _bench_R9():
88
+ "factor(x^20 - pi^5*y^20)"
89
+ factor(x**20 - pi**5*y**20)
90
+
91
+
92
+ def bench_R10():
93
+ "v = [-pi,-pi+1/10..,pi]"
94
+ def srange(min, max, step):
95
+ v = [min]
96
+ while (max - v[-1]).evalf() > 0:
97
+ v.append(v[-1] + step)
98
+ return v[:-1]
99
+ srange(-pi, pi, sympify(1)/10)
100
+
101
+
102
+ def bench_R11():
103
+ "a = [random() + random()*I for w in [0..1000]]"
104
+ [random() + random()*I for w in range(1000)]
105
+
106
+
107
+ def bench_S1():
108
+ "e=(x+y+z+1)**7;f=e*(e+1);f.expand()"
109
+ e = (x + y + z + 1)**7
110
+ f = e*(e + 1)
111
+ f.expand()
112
+
113
+
114
+ if __name__ == '__main__':
115
+ benchmarks = [
116
+ bench_R1,
117
+ bench_R2,
118
+ bench_R3,
119
+ bench_R5,
120
+ bench_R6,
121
+ bench_R7,
122
+ bench_R8,
123
+ #_bench_R9,
124
+ bench_R10,
125
+ bench_R11,
126
+ #bench_S1,
127
+ ]
128
+
129
+ report = []
130
+ for b in benchmarks:
131
+ t = clock()
132
+ b()
133
+ t = clock() - t
134
+ print("%s%65s: %f" % (b.__name__, b.__doc__, t))
wemm/lib/python3.10/site-packages/sympy/crypto/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (1.63 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/crypto/__pycache__/crypto.cpython-310.pyc ADDED
Binary file (94.1 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/logic/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (672 Bytes). View file
 
wemm/lib/python3.10/site-packages/sympy/logic/__pycache__/inference.cpython-310.pyc ADDED
Binary file (9.15 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/logic/algorithms/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (172 Bytes). View file
 
wemm/lib/python3.10/site-packages/sympy/logic/algorithms/__pycache__/dpll.cpython-310.pyc ADDED
Binary file (8.01 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/logic/algorithms/__pycache__/dpll2.cpython-310.pyc ADDED
Binary file (17.9 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/logic/algorithms/__pycache__/lra_theory.cpython-310.pyc ADDED
Binary file (29 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/logic/algorithms/__pycache__/minisat22_wrapper.cpython-310.pyc ADDED
Binary file (1.93 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/logic/algorithms/__pycache__/pycosat_wrapper.cpython-310.pyc ADDED
Binary file (1.4 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/logic/algorithms/__pycache__/z3_wrapper.cpython-310.pyc ADDED
Binary file (4.2 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/logic/algorithms/dpll.py ADDED
@@ -0,0 +1,308 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Implementation of DPLL algorithm
2
+
3
+ Further improvements: eliminate calls to pl_true, implement branching rules,
4
+ efficient unit propagation.
5
+
6
+ References:
7
+ - https://en.wikipedia.org/wiki/DPLL_algorithm
8
+ - https://www.researchgate.net/publication/242384772_Implementations_of_the_DPLL_Algorithm
9
+ """
10
+
11
+ from sympy.core.sorting import default_sort_key
12
+ from sympy.logic.boolalg import Or, Not, conjuncts, disjuncts, to_cnf, \
13
+ to_int_repr, _find_predicates
14
+ from sympy.assumptions.cnf import CNF
15
+ from sympy.logic.inference import pl_true, literal_symbol
16
+
17
+
18
+ def dpll_satisfiable(expr):
19
+ """
20
+ Check satisfiability of a propositional sentence.
21
+ It returns a model rather than True when it succeeds
22
+
23
+ >>> from sympy.abc import A, B
24
+ >>> from sympy.logic.algorithms.dpll import dpll_satisfiable
25
+ >>> dpll_satisfiable(A & ~B)
26
+ {A: True, B: False}
27
+ >>> dpll_satisfiable(A & ~A)
28
+ False
29
+
30
+ """
31
+ if not isinstance(expr, CNF):
32
+ clauses = conjuncts(to_cnf(expr))
33
+ else:
34
+ clauses = expr.clauses
35
+ if False in clauses:
36
+ return False
37
+ symbols = sorted(_find_predicates(expr), key=default_sort_key)
38
+ symbols_int_repr = set(range(1, len(symbols) + 1))
39
+ clauses_int_repr = to_int_repr(clauses, symbols)
40
+ result = dpll_int_repr(clauses_int_repr, symbols_int_repr, {})
41
+ if not result:
42
+ return result
43
+ output = {}
44
+ for key in result:
45
+ output.update({symbols[key - 1]: result[key]})
46
+ return output
47
+
48
+
49
+ def dpll(clauses, symbols, model):
50
+ """
51
+ Compute satisfiability in a partial model.
52
+ Clauses is an array of conjuncts.
53
+
54
+ >>> from sympy.abc import A, B, D
55
+ >>> from sympy.logic.algorithms.dpll import dpll
56
+ >>> dpll([A, B, D], [A, B], {D: False})
57
+ False
58
+
59
+ """
60
+ # compute DP kernel
61
+ P, value = find_unit_clause(clauses, model)
62
+ while P:
63
+ model.update({P: value})
64
+ symbols.remove(P)
65
+ if not value:
66
+ P = ~P
67
+ clauses = unit_propagate(clauses, P)
68
+ P, value = find_unit_clause(clauses, model)
69
+ P, value = find_pure_symbol(symbols, clauses)
70
+ while P:
71
+ model.update({P: value})
72
+ symbols.remove(P)
73
+ if not value:
74
+ P = ~P
75
+ clauses = unit_propagate(clauses, P)
76
+ P, value = find_pure_symbol(symbols, clauses)
77
+ # end DP kernel
78
+ unknown_clauses = []
79
+ for c in clauses:
80
+ val = pl_true(c, model)
81
+ if val is False:
82
+ return False
83
+ if val is not True:
84
+ unknown_clauses.append(c)
85
+ if not unknown_clauses:
86
+ return model
87
+ if not clauses:
88
+ return model
89
+ P = symbols.pop()
90
+ model_copy = model.copy()
91
+ model.update({P: True})
92
+ model_copy.update({P: False})
93
+ symbols_copy = symbols[:]
94
+ return (dpll(unit_propagate(unknown_clauses, P), symbols, model) or
95
+ dpll(unit_propagate(unknown_clauses, Not(P)), symbols_copy, model_copy))
96
+
97
+
98
+ def dpll_int_repr(clauses, symbols, model):
99
+ """
100
+ Compute satisfiability in a partial model.
101
+ Arguments are expected to be in integer representation
102
+
103
+ >>> from sympy.logic.algorithms.dpll import dpll_int_repr
104
+ >>> dpll_int_repr([{1}, {2}, {3}], {1, 2}, {3: False})
105
+ False
106
+
107
+ """
108
+ # compute DP kernel
109
+ P, value = find_unit_clause_int_repr(clauses, model)
110
+ while P:
111
+ model.update({P: value})
112
+ symbols.remove(P)
113
+ if not value:
114
+ P = -P
115
+ clauses = unit_propagate_int_repr(clauses, P)
116
+ P, value = find_unit_clause_int_repr(clauses, model)
117
+ P, value = find_pure_symbol_int_repr(symbols, clauses)
118
+ while P:
119
+ model.update({P: value})
120
+ symbols.remove(P)
121
+ if not value:
122
+ P = -P
123
+ clauses = unit_propagate_int_repr(clauses, P)
124
+ P, value = find_pure_symbol_int_repr(symbols, clauses)
125
+ # end DP kernel
126
+ unknown_clauses = []
127
+ for c in clauses:
128
+ val = pl_true_int_repr(c, model)
129
+ if val is False:
130
+ return False
131
+ if val is not True:
132
+ unknown_clauses.append(c)
133
+ if not unknown_clauses:
134
+ return model
135
+ P = symbols.pop()
136
+ model_copy = model.copy()
137
+ model.update({P: True})
138
+ model_copy.update({P: False})
139
+ symbols_copy = symbols.copy()
140
+ return (dpll_int_repr(unit_propagate_int_repr(unknown_clauses, P), symbols, model) or
141
+ dpll_int_repr(unit_propagate_int_repr(unknown_clauses, -P), symbols_copy, model_copy))
142
+
143
+ ### helper methods for DPLL
144
+
145
+
146
+ def pl_true_int_repr(clause, model={}):
147
+ """
148
+ Lightweight version of pl_true.
149
+ Argument clause represents the set of args of an Or clause. This is used
150
+ inside dpll_int_repr, it is not meant to be used directly.
151
+
152
+ >>> from sympy.logic.algorithms.dpll import pl_true_int_repr
153
+ >>> pl_true_int_repr({1, 2}, {1: False})
154
+ >>> pl_true_int_repr({1, 2}, {1: False, 2: False})
155
+ False
156
+
157
+ """
158
+ result = False
159
+ for lit in clause:
160
+ if lit < 0:
161
+ p = model.get(-lit)
162
+ if p is not None:
163
+ p = not p
164
+ else:
165
+ p = model.get(lit)
166
+ if p is True:
167
+ return True
168
+ elif p is None:
169
+ result = None
170
+ return result
171
+
172
+
173
+ def unit_propagate(clauses, symbol):
174
+ """
175
+ Returns an equivalent set of clauses
176
+ If a set of clauses contains the unit clause l, the other clauses are
177
+ simplified by the application of the two following rules:
178
+
179
+ 1. every clause containing l is removed
180
+ 2. in every clause that contains ~l this literal is deleted
181
+
182
+ Arguments are expected to be in CNF.
183
+
184
+ >>> from sympy.abc import A, B, D
185
+ >>> from sympy.logic.algorithms.dpll import unit_propagate
186
+ >>> unit_propagate([A | B, D | ~B, B], B)
187
+ [D, B]
188
+
189
+ """
190
+ output = []
191
+ for c in clauses:
192
+ if c.func != Or:
193
+ output.append(c)
194
+ continue
195
+ for arg in c.args:
196
+ if arg == ~symbol:
197
+ output.append(Or(*[x for x in c.args if x != ~symbol]))
198
+ break
199
+ if arg == symbol:
200
+ break
201
+ else:
202
+ output.append(c)
203
+ return output
204
+
205
+
206
+ def unit_propagate_int_repr(clauses, s):
207
+ """
208
+ Same as unit_propagate, but arguments are expected to be in integer
209
+ representation
210
+
211
+ >>> from sympy.logic.algorithms.dpll import unit_propagate_int_repr
212
+ >>> unit_propagate_int_repr([{1, 2}, {3, -2}, {2}], 2)
213
+ [{3}]
214
+
215
+ """
216
+ negated = {-s}
217
+ return [clause - negated for clause in clauses if s not in clause]
218
+
219
+
220
+ def find_pure_symbol(symbols, unknown_clauses):
221
+ """
222
+ Find a symbol and its value if it appears only as a positive literal
223
+ (or only as a negative) in clauses.
224
+
225
+ >>> from sympy.abc import A, B, D
226
+ >>> from sympy.logic.algorithms.dpll import find_pure_symbol
227
+ >>> find_pure_symbol([A, B, D], [A|~B,~B|~D,D|A])
228
+ (A, True)
229
+
230
+ """
231
+ for sym in symbols:
232
+ found_pos, found_neg = False, False
233
+ for c in unknown_clauses:
234
+ if not found_pos and sym in disjuncts(c):
235
+ found_pos = True
236
+ if not found_neg and Not(sym) in disjuncts(c):
237
+ found_neg = True
238
+ if found_pos != found_neg:
239
+ return sym, found_pos
240
+ return None, None
241
+
242
+
243
+ def find_pure_symbol_int_repr(symbols, unknown_clauses):
244
+ """
245
+ Same as find_pure_symbol, but arguments are expected
246
+ to be in integer representation
247
+
248
+ >>> from sympy.logic.algorithms.dpll import find_pure_symbol_int_repr
249
+ >>> find_pure_symbol_int_repr({1,2,3},
250
+ ... [{1, -2}, {-2, -3}, {3, 1}])
251
+ (1, True)
252
+
253
+ """
254
+ all_symbols = set().union(*unknown_clauses)
255
+ found_pos = all_symbols.intersection(symbols)
256
+ found_neg = all_symbols.intersection([-s for s in symbols])
257
+ for p in found_pos:
258
+ if -p not in found_neg:
259
+ return p, True
260
+ for p in found_neg:
261
+ if -p not in found_pos:
262
+ return -p, False
263
+ return None, None
264
+
265
+
266
+ def find_unit_clause(clauses, model):
267
+ """
268
+ A unit clause has only 1 variable that is not bound in the model.
269
+
270
+ >>> from sympy.abc import A, B, D
271
+ >>> from sympy.logic.algorithms.dpll import find_unit_clause
272
+ >>> find_unit_clause([A | B | D, B | ~D, A | ~B], {A:True})
273
+ (B, False)
274
+
275
+ """
276
+ for clause in clauses:
277
+ num_not_in_model = 0
278
+ for literal in disjuncts(clause):
279
+ sym = literal_symbol(literal)
280
+ if sym not in model:
281
+ num_not_in_model += 1
282
+ P, value = sym, not isinstance(literal, Not)
283
+ if num_not_in_model == 1:
284
+ return P, value
285
+ return None, None
286
+
287
+
288
+ def find_unit_clause_int_repr(clauses, model):
289
+ """
290
+ Same as find_unit_clause, but arguments are expected to be in
291
+ integer representation.
292
+
293
+ >>> from sympy.logic.algorithms.dpll import find_unit_clause_int_repr
294
+ >>> find_unit_clause_int_repr([{1, 2, 3},
295
+ ... {2, -3}, {1, -2}], {1: True})
296
+ (2, False)
297
+
298
+ """
299
+ bound = set(model) | {-sym for sym in model}
300
+ for clause in clauses:
301
+ unbound = clause - bound
302
+ if len(unbound) == 1:
303
+ p = unbound.pop()
304
+ if p < 0:
305
+ return -p, False
306
+ else:
307
+ return p, True
308
+ return None, None
wemm/lib/python3.10/site-packages/sympy/logic/tests/__init__.py ADDED
File without changes
wemm/lib/python3.10/site-packages/sympy/logic/tests/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (167 Bytes). View file
 
wemm/lib/python3.10/site-packages/sympy/logic/tests/__pycache__/test_boolalg.cpython-310.pyc ADDED
Binary file (49 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/logic/tests/__pycache__/test_dimacs.cpython-310.pyc ADDED
Binary file (4.4 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/logic/tests/__pycache__/test_inference.cpython-310.pyc ADDED
Binary file (14.3 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/logic/tests/__pycache__/test_lra_theory.cpython-310.pyc ADDED
Binary file (14.9 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/logic/tests/test_boolalg.py ADDED
@@ -0,0 +1,1352 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.assumptions.ask import Q
2
+ from sympy.assumptions.refine import refine
3
+ from sympy.core.numbers import oo
4
+ from sympy.core.relational import Equality, Eq, Ne
5
+ from sympy.core.singleton import S
6
+ from sympy.core.symbol import (Dummy, symbols)
7
+ from sympy.functions import Piecewise
8
+ from sympy.functions.elementary.trigonometric import cos, sin
9
+ from sympy.sets.sets import Interval, Union
10
+ from sympy.sets.contains import Contains
11
+ from sympy.simplify.simplify import simplify
12
+ from sympy.logic.boolalg import (
13
+ And, Boolean, Equivalent, ITE, Implies, Nand, Nor, Not, Or,
14
+ POSform, SOPform, Xor, Xnor, conjuncts, disjuncts,
15
+ distribute_or_over_and, distribute_and_over_or,
16
+ eliminate_implications, is_nnf, is_cnf, is_dnf, simplify_logic,
17
+ to_nnf, to_cnf, to_dnf, to_int_repr, bool_map, true, false,
18
+ BooleanAtom, is_literal, term_to_integer,
19
+ truth_table, as_Boolean, to_anf, is_anf, distribute_xor_over_and,
20
+ anf_coeffs, ANFform, bool_minterm, bool_maxterm, bool_monomial,
21
+ _check_pair, _convert_to_varsSOP, _convert_to_varsPOS, Exclusive,
22
+ gateinputcount)
23
+ from sympy.assumptions.cnf import CNF
24
+
25
+ from sympy.testing.pytest import raises, XFAIL, slow
26
+
27
+ from itertools import combinations, permutations, product
28
+
29
+ A, B, C, D = symbols('A:D')
30
+ a, b, c, d, e, w, x, y, z = symbols('a:e w:z')
31
+
32
+
33
+ def test_overloading():
34
+ """Test that |, & are overloaded as expected"""
35
+
36
+ assert A & B == And(A, B)
37
+ assert A | B == Or(A, B)
38
+ assert (A & B) | C == Or(And(A, B), C)
39
+ assert A >> B == Implies(A, B)
40
+ assert A << B == Implies(B, A)
41
+ assert ~A == Not(A)
42
+ assert A ^ B == Xor(A, B)
43
+
44
+
45
+ def test_And():
46
+ assert And() is true
47
+ assert And(A) == A
48
+ assert And(True) is true
49
+ assert And(False) is false
50
+ assert And(True, True) is true
51
+ assert And(True, False) is false
52
+ assert And(False, False) is false
53
+ assert And(True, A) == A
54
+ assert And(False, A) is false
55
+ assert And(True, True, True) is true
56
+ assert And(True, True, A) == A
57
+ assert And(True, False, A) is false
58
+ assert And(1, A) == A
59
+ raises(TypeError, lambda: And(2, A))
60
+ assert And(A < 1, A >= 1) is false
61
+ e = A > 1
62
+ assert And(e, e.canonical) == e.canonical
63
+ g, l, ge, le = A > B, B < A, A >= B, B <= A
64
+ assert And(g, l, ge, le) == And(ge, g)
65
+ assert {And(*i) for i in permutations((l,g,le,ge))} == {And(ge, g)}
66
+ assert And(And(Eq(a, 0), Eq(b, 0)), And(Ne(a, 0), Eq(c, 0))) is false
67
+
68
+
69
+ def test_Or():
70
+ assert Or() is false
71
+ assert Or(A) == A
72
+ assert Or(True) is true
73
+ assert Or(False) is false
74
+ assert Or(True, True) is true
75
+ assert Or(True, False) is true
76
+ assert Or(False, False) is false
77
+ assert Or(True, A) is true
78
+ assert Or(False, A) == A
79
+ assert Or(True, False, False) is true
80
+ assert Or(True, False, A) is true
81
+ assert Or(False, False, A) == A
82
+ assert Or(1, A) is true
83
+ raises(TypeError, lambda: Or(2, A))
84
+ assert Or(A < 1, A >= 1) is true
85
+ e = A > 1
86
+ assert Or(e, e.canonical) == e
87
+ g, l, ge, le = A > B, B < A, A >= B, B <= A
88
+ assert Or(g, l, ge, le) == Or(g, ge)
89
+
90
+
91
+ def test_Xor():
92
+ assert Xor() is false
93
+ assert Xor(A) == A
94
+ assert Xor(A, A) is false
95
+ assert Xor(True, A, A) is true
96
+ assert Xor(A, A, A, A, A) == A
97
+ assert Xor(True, False, False, A, B) == ~Xor(A, B)
98
+ assert Xor(True) is true
99
+ assert Xor(False) is false
100
+ assert Xor(True, True) is false
101
+ assert Xor(True, False) is true
102
+ assert Xor(False, False) is false
103
+ assert Xor(True, A) == ~A
104
+ assert Xor(False, A) == A
105
+ assert Xor(True, False, False) is true
106
+ assert Xor(True, False, A) == ~A
107
+ assert Xor(False, False, A) == A
108
+ assert isinstance(Xor(A, B), Xor)
109
+ assert Xor(A, B, Xor(C, D)) == Xor(A, B, C, D)
110
+ assert Xor(A, B, Xor(B, C)) == Xor(A, C)
111
+ assert Xor(A < 1, A >= 1, B) == Xor(0, 1, B) == Xor(1, 0, B)
112
+ e = A > 1
113
+ assert Xor(e, e.canonical) == Xor(0, 0) == Xor(1, 1)
114
+
115
+
116
+ def test_rewrite_as_And():
117
+ expr = x ^ y
118
+ assert expr.rewrite(And) == (x | y) & (~x | ~y)
119
+
120
+
121
+ def test_rewrite_as_Or():
122
+ expr = x ^ y
123
+ assert expr.rewrite(Or) == (x & ~y) | (y & ~x)
124
+
125
+
126
+ def test_rewrite_as_Nand():
127
+ expr = (y & z) | (z & ~w)
128
+ assert expr.rewrite(Nand) == ~(~(y & z) & ~(z & ~w))
129
+
130
+
131
+ def test_rewrite_as_Nor():
132
+ expr = z & (y | ~w)
133
+ assert expr.rewrite(Nor) == ~(~z | ~(y | ~w))
134
+
135
+
136
+ def test_Not():
137
+ raises(TypeError, lambda: Not(True, False))
138
+ assert Not(True) is false
139
+ assert Not(False) is true
140
+ assert Not(0) is true
141
+ assert Not(1) is false
142
+ assert Not(2) is false
143
+
144
+
145
+ def test_Nand():
146
+ assert Nand() is false
147
+ assert Nand(A) == ~A
148
+ assert Nand(True) is false
149
+ assert Nand(False) is true
150
+ assert Nand(True, True) is false
151
+ assert Nand(True, False) is true
152
+ assert Nand(False, False) is true
153
+ assert Nand(True, A) == ~A
154
+ assert Nand(False, A) is true
155
+ assert Nand(True, True, True) is false
156
+ assert Nand(True, True, A) == ~A
157
+ assert Nand(True, False, A) is true
158
+
159
+
160
+ def test_Nor():
161
+ assert Nor() is true
162
+ assert Nor(A) == ~A
163
+ assert Nor(True) is false
164
+ assert Nor(False) is true
165
+ assert Nor(True, True) is false
166
+ assert Nor(True, False) is false
167
+ assert Nor(False, False) is true
168
+ assert Nor(True, A) is false
169
+ assert Nor(False, A) == ~A
170
+ assert Nor(True, True, True) is false
171
+ assert Nor(True, True, A) is false
172
+ assert Nor(True, False, A) is false
173
+
174
+
175
+ def test_Xnor():
176
+ assert Xnor() is true
177
+ assert Xnor(A) == ~A
178
+ assert Xnor(A, A) is true
179
+ assert Xnor(True, A, A) is false
180
+ assert Xnor(A, A, A, A, A) == ~A
181
+ assert Xnor(True) is false
182
+ assert Xnor(False) is true
183
+ assert Xnor(True, True) is true
184
+ assert Xnor(True, False) is false
185
+ assert Xnor(False, False) is true
186
+ assert Xnor(True, A) == A
187
+ assert Xnor(False, A) == ~A
188
+ assert Xnor(True, False, False) is false
189
+ assert Xnor(True, False, A) == A
190
+ assert Xnor(False, False, A) == ~A
191
+
192
+
193
+ def test_Implies():
194
+ raises(ValueError, lambda: Implies(A, B, C))
195
+ assert Implies(True, True) is true
196
+ assert Implies(True, False) is false
197
+ assert Implies(False, True) is true
198
+ assert Implies(False, False) is true
199
+ assert Implies(0, A) is true
200
+ assert Implies(1, 1) is true
201
+ assert Implies(1, 0) is false
202
+ assert A >> B == B << A
203
+ assert (A < 1) >> (A >= 1) == (A >= 1)
204
+ assert (A < 1) >> (S.One > A) is true
205
+ assert A >> A is true
206
+
207
+
208
+ def test_Equivalent():
209
+ assert Equivalent(A, B) == Equivalent(B, A) == Equivalent(A, B, A)
210
+ assert Equivalent() is true
211
+ assert Equivalent(A, A) == Equivalent(A) is true
212
+ assert Equivalent(True, True) == Equivalent(False, False) is true
213
+ assert Equivalent(True, False) == Equivalent(False, True) is false
214
+ assert Equivalent(A, True) == A
215
+ assert Equivalent(A, False) == Not(A)
216
+ assert Equivalent(A, B, True) == A & B
217
+ assert Equivalent(A, B, False) == ~A & ~B
218
+ assert Equivalent(1, A) == A
219
+ assert Equivalent(0, A) == Not(A)
220
+ assert Equivalent(A, Equivalent(B, C)) != Equivalent(Equivalent(A, B), C)
221
+ assert Equivalent(A < 1, A >= 1) is false
222
+ assert Equivalent(A < 1, A >= 1, 0) is false
223
+ assert Equivalent(A < 1, A >= 1, 1) is false
224
+ assert Equivalent(A < 1, S.One > A) == Equivalent(1, 1) == Equivalent(0, 0)
225
+ assert Equivalent(Equality(A, B), Equality(B, A)) is true
226
+
227
+
228
+ def test_Exclusive():
229
+ assert Exclusive(False, False, False) is true
230
+ assert Exclusive(True, False, False) is true
231
+ assert Exclusive(True, True, False) is false
232
+ assert Exclusive(True, True, True) is false
233
+
234
+
235
+ def test_equals():
236
+ assert Not(Or(A, B)).equals(And(Not(A), Not(B))) is True
237
+ assert Equivalent(A, B).equals((A >> B) & (B >> A)) is True
238
+ assert ((A | ~B) & (~A | B)).equals((~A & ~B) | (A & B)) is True
239
+ assert (A >> B).equals(~A >> ~B) is False
240
+ assert (A >> (B >> A)).equals(A >> (C >> A)) is False
241
+ raises(NotImplementedError, lambda: (A & B).equals(A > B))
242
+
243
+
244
+ def test_simplification_boolalg():
245
+ """
246
+ Test working of simplification methods.
247
+ """
248
+ set1 = [[0, 0, 1], [0, 1, 1], [1, 0, 0], [1, 1, 0]]
249
+ set2 = [[0, 0, 0], [0, 1, 0], [1, 0, 1], [1, 1, 1]]
250
+ assert SOPform([x, y, z], set1) == Or(And(Not(x), z), And(Not(z), x))
251
+ assert Not(SOPform([x, y, z], set2)) == \
252
+ Not(Or(And(Not(x), Not(z)), And(x, z)))
253
+ assert POSform([x, y, z], set1 + set2) is true
254
+ assert SOPform([x, y, z], set1 + set2) is true
255
+ assert SOPform([Dummy(), Dummy(), Dummy()], set1 + set2) is true
256
+
257
+ minterms = [[0, 0, 0, 1], [0, 0, 1, 1], [0, 1, 1, 1], [1, 0, 1, 1],
258
+ [1, 1, 1, 1]]
259
+ dontcares = [[0, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 1]]
260
+ assert (
261
+ SOPform([w, x, y, z], minterms, dontcares) ==
262
+ Or(And(y, z), And(Not(w), Not(x))))
263
+ assert POSform([w, x, y, z], minterms, dontcares) == And(Or(Not(w), y), z)
264
+
265
+ minterms = [1, 3, 7, 11, 15]
266
+ dontcares = [0, 2, 5]
267
+ assert (
268
+ SOPform([w, x, y, z], minterms, dontcares) ==
269
+ Or(And(y, z), And(Not(w), Not(x))))
270
+ assert POSform([w, x, y, z], minterms, dontcares) == And(Or(Not(w), y), z)
271
+
272
+ minterms = [1, [0, 0, 1, 1], 7, [1, 0, 1, 1],
273
+ [1, 1, 1, 1]]
274
+ dontcares = [0, [0, 0, 1, 0], 5]
275
+ assert (
276
+ SOPform([w, x, y, z], minterms, dontcares) ==
277
+ Or(And(y, z), And(Not(w), Not(x))))
278
+ assert POSform([w, x, y, z], minterms, dontcares) == And(Or(Not(w), y), z)
279
+
280
+ minterms = [1, {y: 1, z: 1}]
281
+ dontcares = [0, [0, 0, 1, 0], 5]
282
+ assert (
283
+ SOPform([w, x, y, z], minterms, dontcares) ==
284
+ Or(And(y, z), And(Not(w), Not(x))))
285
+ assert POSform([w, x, y, z], minterms, dontcares) == And(Or(Not(w), y), z)
286
+
287
+
288
+ minterms = [{y: 1, z: 1}, 1]
289
+ dontcares = [[0, 0, 0, 0]]
290
+
291
+ minterms = [[0, 0, 0]]
292
+ raises(ValueError, lambda: SOPform([w, x, y, z], minterms))
293
+ raises(ValueError, lambda: POSform([w, x, y, z], minterms))
294
+
295
+ raises(TypeError, lambda: POSform([w, x, y, z], ["abcdefg"]))
296
+
297
+ # test simplification
298
+ ans = And(A, Or(B, C))
299
+ assert simplify_logic(A & (B | C)) == ans
300
+ assert simplify_logic((A & B) | (A & C)) == ans
301
+ assert simplify_logic(Implies(A, B)) == Or(Not(A), B)
302
+ assert simplify_logic(Equivalent(A, B)) == \
303
+ Or(And(A, B), And(Not(A), Not(B)))
304
+ assert simplify_logic(And(Equality(A, 2), C)) == And(Equality(A, 2), C)
305
+ assert simplify_logic(And(Equality(A, 2), A)) == And(Equality(A, 2), A)
306
+ assert simplify_logic(And(Equality(A, B), C)) == And(Equality(A, B), C)
307
+ assert simplify_logic(Or(And(Equality(A, 3), B), And(Equality(A, 3), C))) \
308
+ == And(Equality(A, 3), Or(B, C))
309
+ b = (~x & ~y & ~z) | (~x & ~y & z)
310
+ e = And(A, b)
311
+ assert simplify_logic(e) == A & ~x & ~y
312
+ raises(ValueError, lambda: simplify_logic(A & (B | C), form='blabla'))
313
+ assert simplify(Or(x <= y, And(x < y, z))) == (x <= y)
314
+ assert simplify(Or(x <= y, And(y > x, z))) == (x <= y)
315
+ assert simplify(Or(x >= y, And(y < x, z))) == (x >= y)
316
+
317
+ # Check that expressions with nine variables or more are not simplified
318
+ # (without the force-flag)
319
+ a, b, c, d, e, f, g, h, j = symbols('a b c d e f g h j')
320
+ expr = a & b & c & d & e & f & g & h & j | \
321
+ a & b & c & d & e & f & g & h & ~j
322
+ # This expression can be simplified to get rid of the j variables
323
+ assert simplify_logic(expr) == expr
324
+
325
+ # Test dontcare
326
+ assert simplify_logic((a & b) | c | d, dontcare=(a & b)) == c | d
327
+
328
+ # check input
329
+ ans = SOPform([x, y], [[1, 0]])
330
+ assert SOPform([x, y], [[1, 0]]) == ans
331
+ assert POSform([x, y], [[1, 0]]) == ans
332
+
333
+ raises(ValueError, lambda: SOPform([x], [[1]], [[1]]))
334
+ assert SOPform([x], [[1]], [[0]]) is true
335
+ assert SOPform([x], [[0]], [[1]]) is true
336
+ assert SOPform([x], [], []) is false
337
+
338
+ raises(ValueError, lambda: POSform([x], [[1]], [[1]]))
339
+ assert POSform([x], [[1]], [[0]]) is true
340
+ assert POSform([x], [[0]], [[1]]) is true
341
+ assert POSform([x], [], []) is false
342
+
343
+ # check working of simplify
344
+ assert simplify((A & B) | (A & C)) == And(A, Or(B, C))
345
+ assert simplify(And(x, Not(x))) == False
346
+ assert simplify(Or(x, Not(x))) == True
347
+ assert simplify(And(Eq(x, 0), Eq(x, y))) == And(Eq(x, 0), Eq(y, 0))
348
+ assert And(Eq(x - 1, 0), Eq(x, y)).simplify() == And(Eq(x, 1), Eq(y, 1))
349
+ assert And(Ne(x - 1, 0), Ne(x, y)).simplify() == And(Ne(x, 1), Ne(x, y))
350
+ assert And(Eq(x - 1, 0), Ne(x, y)).simplify() == And(Eq(x, 1), Ne(y, 1))
351
+ assert And(Eq(x - 1, 0), Eq(x, z + y), Eq(y + x, 0)).simplify(
352
+ ) == And(Eq(x, 1), Eq(y, -1), Eq(z, 2))
353
+ assert And(Eq(x - 1, 0), Eq(x + 2, 3)).simplify() == Eq(x, 1)
354
+ assert And(Ne(x - 1, 0), Ne(x + 2, 3)).simplify() == Ne(x, 1)
355
+ assert And(Eq(x - 1, 0), Eq(x + 2, 2)).simplify() == False
356
+ assert And(Ne(x - 1, 0), Ne(x + 2, 2)).simplify(
357
+ ) == And(Ne(x, 1), Ne(x, 0))
358
+ assert simplify(Xor(x, ~x)) == True
359
+
360
+
361
+ def test_bool_map():
362
+ """
363
+ Test working of bool_map function.
364
+ """
365
+
366
+ minterms = [[0, 0, 0, 1], [0, 0, 1, 1], [0, 1, 1, 1], [1, 0, 1, 1],
367
+ [1, 1, 1, 1]]
368
+ assert bool_map(Not(Not(a)), a) == (a, {a: a})
369
+ assert bool_map(SOPform([w, x, y, z], minterms),
370
+ POSform([w, x, y, z], minterms)) == \
371
+ (And(Or(Not(w), y), Or(Not(x), y), z), {x: x, w: w, z: z, y: y})
372
+ assert bool_map(SOPform([x, z, y], [[1, 0, 1]]),
373
+ SOPform([a, b, c], [[1, 0, 1]])) != False
374
+ function1 = SOPform([x, z, y], [[1, 0, 1], [0, 0, 1]])
375
+ function2 = SOPform([a, b, c], [[1, 0, 1], [1, 0, 0]])
376
+ assert bool_map(function1, function2) == \
377
+ (function1, {y: a, z: b})
378
+ assert bool_map(Xor(x, y), ~Xor(x, y)) == False
379
+ assert bool_map(And(x, y), Or(x, y)) is None
380
+ assert bool_map(And(x, y), And(x, y, z)) is None
381
+ # issue 16179
382
+ assert bool_map(Xor(x, y, z), ~Xor(x, y, z)) == False
383
+ assert bool_map(Xor(a, x, y, z), ~Xor(a, x, y, z)) == False
384
+
385
+
386
+ def test_bool_symbol():
387
+ """Test that mixing symbols with boolean values
388
+ works as expected"""
389
+
390
+ assert And(A, True) == A
391
+ assert And(A, True, True) == A
392
+ assert And(A, False) is false
393
+ assert And(A, True, False) is false
394
+ assert Or(A, True) is true
395
+ assert Or(A, False) == A
396
+
397
+
398
+ def test_is_boolean():
399
+ assert isinstance(True, Boolean) is False
400
+ assert isinstance(true, Boolean) is True
401
+ assert 1 == True
402
+ assert 1 != true
403
+ assert (1 == true) is False
404
+ assert 0 == False
405
+ assert 0 != false
406
+ assert (0 == false) is False
407
+ assert true.is_Boolean is True
408
+ assert (A & B).is_Boolean
409
+ assert (A | B).is_Boolean
410
+ assert (~A).is_Boolean
411
+ assert (A ^ B).is_Boolean
412
+ assert A.is_Boolean != isinstance(A, Boolean)
413
+ assert isinstance(A, Boolean)
414
+
415
+
416
+ def test_subs():
417
+ assert (A & B).subs(A, True) == B
418
+ assert (A & B).subs(A, False) is false
419
+ assert (A & B).subs(B, True) == A
420
+ assert (A & B).subs(B, False) is false
421
+ assert (A & B).subs({A: True, B: True}) is true
422
+ assert (A | B).subs(A, True) is true
423
+ assert (A | B).subs(A, False) == B
424
+ assert (A | B).subs(B, True) is true
425
+ assert (A | B).subs(B, False) == A
426
+ assert (A | B).subs({A: True, B: True}) is true
427
+
428
+
429
+ """
430
+ we test for axioms of boolean algebra
431
+ see https://en.wikipedia.org/wiki/Boolean_algebra_(structure)
432
+ """
433
+
434
+
435
+ def test_commutative():
436
+ """Test for commutativity of And and Or"""
437
+ A, B = map(Boolean, symbols('A,B'))
438
+
439
+ assert A & B == B & A
440
+ assert A | B == B | A
441
+
442
+
443
+ def test_and_associativity():
444
+ """Test for associativity of And"""
445
+
446
+ assert (A & B) & C == A & (B & C)
447
+
448
+
449
+ def test_or_assicativity():
450
+ assert ((A | B) | C) == (A | (B | C))
451
+
452
+
453
+ def test_double_negation():
454
+ a = Boolean()
455
+ assert ~(~a) == a
456
+
457
+
458
+ # test methods
459
+
460
+ def test_eliminate_implications():
461
+ assert eliminate_implications(Implies(A, B, evaluate=False)) == (~A) | B
462
+ assert eliminate_implications(
463
+ A >> (C >> Not(B))) == Or(Or(Not(B), Not(C)), Not(A))
464
+ assert eliminate_implications(Equivalent(A, B, C, D)) == \
465
+ (~A | B) & (~B | C) & (~C | D) & (~D | A)
466
+
467
+
468
+ def test_conjuncts():
469
+ assert conjuncts(A & B & C) == {A, B, C}
470
+ assert conjuncts((A | B) & C) == {A | B, C}
471
+ assert conjuncts(A) == {A}
472
+ assert conjuncts(True) == {True}
473
+ assert conjuncts(False) == {False}
474
+
475
+
476
+ def test_disjuncts():
477
+ assert disjuncts(A | B | C) == {A, B, C}
478
+ assert disjuncts((A | B) & C) == {(A | B) & C}
479
+ assert disjuncts(A) == {A}
480
+ assert disjuncts(True) == {True}
481
+ assert disjuncts(False) == {False}
482
+
483
+
484
+ def test_distribute():
485
+ assert distribute_and_over_or(Or(And(A, B), C)) == And(Or(A, C), Or(B, C))
486
+ assert distribute_or_over_and(And(A, Or(B, C))) == Or(And(A, B), And(A, C))
487
+ assert distribute_xor_over_and(And(A, Xor(B, C))) == Xor(And(A, B), And(A, C))
488
+
489
+
490
+ def test_to_anf():
491
+ x, y, z = symbols('x,y,z')
492
+ assert to_anf(And(x, y)) == And(x, y)
493
+ assert to_anf(Or(x, y)) == Xor(x, y, And(x, y))
494
+ assert to_anf(Or(Implies(x, y), And(x, y), y)) == \
495
+ Xor(x, True, x & y, remove_true=False)
496
+ assert to_anf(Or(Nand(x, y), Nor(x, y), Xnor(x, y), Implies(x, y))) == True
497
+ assert to_anf(Or(x, Not(y), Nor(x,z), And(x, y), Nand(y, z))) == \
498
+ Xor(True, And(y, z), And(x, y, z), remove_true=False)
499
+ assert to_anf(Xor(x, y)) == Xor(x, y)
500
+ assert to_anf(Not(x)) == Xor(x, True, remove_true=False)
501
+ assert to_anf(Nand(x, y)) == Xor(True, And(x, y), remove_true=False)
502
+ assert to_anf(Nor(x, y)) == Xor(x, y, True, And(x, y), remove_true=False)
503
+ assert to_anf(Implies(x, y)) == Xor(x, True, And(x, y), remove_true=False)
504
+ assert to_anf(Equivalent(x, y)) == Xor(x, y, True, remove_true=False)
505
+ assert to_anf(Nand(x | y, x >> y), deep=False) == \
506
+ Xor(True, And(Or(x, y), Implies(x, y)), remove_true=False)
507
+ assert to_anf(Nor(x ^ y, x & y), deep=False) == \
508
+ Xor(True, Or(Xor(x, y), And(x, y)), remove_true=False)
509
+ # issue 25218
510
+ assert to_anf(x ^ ~(x ^ y ^ ~y)) == False
511
+
512
+
513
+ def test_to_nnf():
514
+ assert to_nnf(true) is true
515
+ assert to_nnf(false) is false
516
+ assert to_nnf(A) == A
517
+ assert to_nnf(A | ~A | B) is true
518
+ assert to_nnf(A & ~A & B) is false
519
+ assert to_nnf(A >> B) == ~A | B
520
+ assert to_nnf(Equivalent(A, B, C)) == (~A | B) & (~B | C) & (~C | A)
521
+ assert to_nnf(A ^ B ^ C) == \
522
+ (A | B | C) & (~A | ~B | C) & (A | ~B | ~C) & (~A | B | ~C)
523
+ assert to_nnf(ITE(A, B, C)) == (~A | B) & (A | C)
524
+ assert to_nnf(Not(A | B | C)) == ~A & ~B & ~C
525
+ assert to_nnf(Not(A & B & C)) == ~A | ~B | ~C
526
+ assert to_nnf(Not(A >> B)) == A & ~B
527
+ assert to_nnf(Not(Equivalent(A, B, C))) == And(Or(A, B, C), Or(~A, ~B, ~C))
528
+ assert to_nnf(Not(A ^ B ^ C)) == \
529
+ (~A | B | C) & (A | ~B | C) & (A | B | ~C) & (~A | ~B | ~C)
530
+ assert to_nnf(Not(ITE(A, B, C))) == (~A | ~B) & (A | ~C)
531
+ assert to_nnf((A >> B) ^ (B >> A)) == (A & ~B) | (~A & B)
532
+ assert to_nnf((A >> B) ^ (B >> A), False) == \
533
+ (~A | ~B | A | B) & ((A & ~B) | (~A & B))
534
+ assert ITE(A, 1, 0).to_nnf() == A
535
+ assert ITE(A, 0, 1).to_nnf() == ~A
536
+ # although ITE can hold non-Boolean, it will complain if
537
+ # an attempt is made to convert the ITE to Boolean nnf
538
+ raises(TypeError, lambda: ITE(A < 1, [1], B).to_nnf())
539
+
540
+
541
+ def test_to_cnf():
542
+ assert to_cnf(~(B | C)) == And(Not(B), Not(C))
543
+ assert to_cnf((A & B) | C) == And(Or(A, C), Or(B, C))
544
+ assert to_cnf(A >> B) == (~A) | B
545
+ assert to_cnf(A >> (B & C)) == (~A | B) & (~A | C)
546
+ assert to_cnf(A & (B | C) | ~A & (B | C), True) == B | C
547
+ assert to_cnf(A & B) == And(A, B)
548
+
549
+ assert to_cnf(Equivalent(A, B)) == And(Or(A, Not(B)), Or(B, Not(A)))
550
+ assert to_cnf(Equivalent(A, B & C)) == \
551
+ (~A | B) & (~A | C) & (~B | ~C | A)
552
+ assert to_cnf(Equivalent(A, B | C), True) == \
553
+ And(Or(Not(B), A), Or(Not(C), A), Or(B, C, Not(A)))
554
+ assert to_cnf(A + 1) == A + 1
555
+
556
+
557
+ def test_issue_18904():
558
+ x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15 = symbols('x1:16')
559
+ eq = (( x1 & x2 & x3 & x4 & x5 & x6 & x7 & x8 & x9 ) |
560
+ ( x1 & x2 & x3 & x4 & x5 & x6 & x7 & x10 & x9 ) |
561
+ ( x1 & x11 & x3 & x12 & x5 & x13 & x14 & x15 & x9 ))
562
+ assert is_cnf(to_cnf(eq))
563
+ raises(ValueError, lambda: to_cnf(eq, simplify=True))
564
+ for f, t in zip((And, Or), (to_cnf, to_dnf)):
565
+ eq = f(x1, x2, x3, x4, x5, x6, x7, x8, x9)
566
+ raises(ValueError, lambda: to_cnf(eq, simplify=True))
567
+ assert t(eq, simplify=True, force=True) == eq
568
+
569
+
570
+ def test_issue_9949():
571
+ assert is_cnf(to_cnf((b > -5) | (a > 2) & (a < 4)))
572
+
573
+
574
+ def test_to_CNF():
575
+ assert CNF.CNF_to_cnf(CNF.to_CNF(~(B | C))) == to_cnf(~(B | C))
576
+ assert CNF.CNF_to_cnf(CNF.to_CNF((A & B) | C)) == to_cnf((A & B) | C)
577
+ assert CNF.CNF_to_cnf(CNF.to_CNF(A >> B)) == to_cnf(A >> B)
578
+ assert CNF.CNF_to_cnf(CNF.to_CNF(A >> (B & C))) == to_cnf(A >> (B & C))
579
+ assert CNF.CNF_to_cnf(CNF.to_CNF(A & (B | C) | ~A & (B | C))) == to_cnf(A & (B | C) | ~A & (B | C))
580
+ assert CNF.CNF_to_cnf(CNF.to_CNF(A & B)) == to_cnf(A & B)
581
+
582
+
583
+ def test_to_dnf():
584
+ assert to_dnf(~(B | C)) == And(Not(B), Not(C))
585
+ assert to_dnf(A & (B | C)) == Or(And(A, B), And(A, C))
586
+ assert to_dnf(A >> B) == (~A) | B
587
+ assert to_dnf(A >> (B & C)) == (~A) | (B & C)
588
+ assert to_dnf(A | B) == A | B
589
+
590
+ assert to_dnf(Equivalent(A, B), True) == \
591
+ Or(And(A, B), And(Not(A), Not(B)))
592
+ assert to_dnf(Equivalent(A, B & C), True) == \
593
+ Or(And(A, B, C), And(Not(A), Not(B)), And(Not(A), Not(C)))
594
+ assert to_dnf(A + 1) == A + 1
595
+
596
+
597
+ def test_to_int_repr():
598
+ x, y, z = map(Boolean, symbols('x,y,z'))
599
+
600
+ def sorted_recursive(arg):
601
+ try:
602
+ return sorted(sorted_recursive(x) for x in arg)
603
+ except TypeError: # arg is not a sequence
604
+ return arg
605
+
606
+ assert sorted_recursive(to_int_repr([x | y, z | x], [x, y, z])) == \
607
+ sorted_recursive([[1, 2], [1, 3]])
608
+ assert sorted_recursive(to_int_repr([x | y, z | ~x], [x, y, z])) == \
609
+ sorted_recursive([[1, 2], [3, -1]])
610
+
611
+
612
+ def test_is_anf():
613
+ x, y = symbols('x,y')
614
+ assert is_anf(true) is True
615
+ assert is_anf(false) is True
616
+ assert is_anf(x) is True
617
+ assert is_anf(And(x, y)) is True
618
+ assert is_anf(Xor(x, y, And(x, y))) is True
619
+ assert is_anf(Xor(x, y, Or(x, y))) is False
620
+ assert is_anf(Xor(Not(x), y)) is False
621
+
622
+
623
+ def test_is_nnf():
624
+ assert is_nnf(true) is True
625
+ assert is_nnf(A) is True
626
+ assert is_nnf(~A) is True
627
+ assert is_nnf(A & B) is True
628
+ assert is_nnf((A & B) | (~A & A) | (~B & B) | (~A & ~B), False) is True
629
+ assert is_nnf((A | B) & (~A | ~B)) is True
630
+ assert is_nnf(Not(Or(A, B))) is False
631
+ assert is_nnf(A ^ B) is False
632
+ assert is_nnf((A & B) | (~A & A) | (~B & B) | (~A & ~B), True) is False
633
+
634
+
635
+ def test_is_cnf():
636
+ assert is_cnf(x) is True
637
+ assert is_cnf(x | y | z) is True
638
+ assert is_cnf(x & y & z) is True
639
+ assert is_cnf((x | y) & z) is True
640
+ assert is_cnf((x & y) | z) is False
641
+ assert is_cnf(~(x & y) | z) is False
642
+
643
+
644
+ def test_is_dnf():
645
+ assert is_dnf(x) is True
646
+ assert is_dnf(x | y | z) is True
647
+ assert is_dnf(x & y & z) is True
648
+ assert is_dnf((x & y) | z) is True
649
+ assert is_dnf((x | y) & z) is False
650
+ assert is_dnf(~(x | y) & z) is False
651
+
652
+
653
+ def test_ITE():
654
+ A, B, C = symbols('A:C')
655
+ assert ITE(True, False, True) is false
656
+ assert ITE(True, True, False) is true
657
+ assert ITE(False, True, False) is false
658
+ assert ITE(False, False, True) is true
659
+ assert isinstance(ITE(A, B, C), ITE)
660
+
661
+ A = True
662
+ assert ITE(A, B, C) == B
663
+ A = False
664
+ assert ITE(A, B, C) == C
665
+ B = True
666
+ assert ITE(And(A, B), B, C) == C
667
+ assert ITE(Or(A, False), And(B, True), False) is false
668
+ assert ITE(x, A, B) == Not(x)
669
+ assert ITE(x, B, A) == x
670
+ assert ITE(1, x, y) == x
671
+ assert ITE(0, x, y) == y
672
+ raises(TypeError, lambda: ITE(2, x, y))
673
+ raises(TypeError, lambda: ITE(1, [], y))
674
+ raises(TypeError, lambda: ITE(1, (), y))
675
+ raises(TypeError, lambda: ITE(1, y, []))
676
+ assert ITE(1, 1, 1) is S.true
677
+ assert isinstance(ITE(1, 1, 1, evaluate=False), ITE)
678
+
679
+ assert ITE(Eq(x, True), y, x) == ITE(x, y, x)
680
+ assert ITE(Eq(x, False), y, x) == ITE(~x, y, x)
681
+ assert ITE(Ne(x, True), y, x) == ITE(~x, y, x)
682
+ assert ITE(Ne(x, False), y, x) == ITE(x, y, x)
683
+ assert ITE(Eq(S. true, x), y, x) == ITE(x, y, x)
684
+ assert ITE(Eq(S.false, x), y, x) == ITE(~x, y, x)
685
+ assert ITE(Ne(S.true, x), y, x) == ITE(~x, y, x)
686
+ assert ITE(Ne(S.false, x), y, x) == ITE(x, y, x)
687
+ # 0 and 1 in the context are not treated as True/False
688
+ # so the equality must always be False since dissimilar
689
+ # objects cannot be equal
690
+ assert ITE(Eq(x, 0), y, x) == x
691
+ assert ITE(Eq(x, 1), y, x) == x
692
+ assert ITE(Ne(x, 0), y, x) == y
693
+ assert ITE(Ne(x, 1), y, x) == y
694
+ assert ITE(Eq(x, 0), y, z).subs(x, 0) == y
695
+ assert ITE(Eq(x, 0), y, z).subs(x, 1) == z
696
+ raises(ValueError, lambda: ITE(x > 1, y, x, z))
697
+
698
+
699
+ def test_is_literal():
700
+ assert is_literal(True) is True
701
+ assert is_literal(False) is True
702
+ assert is_literal(A) is True
703
+ assert is_literal(~A) is True
704
+ assert is_literal(Or(A, B)) is False
705
+ assert is_literal(Q.zero(A)) is True
706
+ assert is_literal(Not(Q.zero(A))) is True
707
+ assert is_literal(Or(A, B)) is False
708
+ assert is_literal(And(Q.zero(A), Q.zero(B))) is False
709
+ assert is_literal(x < 3)
710
+ assert not is_literal(x + y < 3)
711
+
712
+
713
+ def test_operators():
714
+ # Mostly test __and__, __rand__, and so on
715
+ assert True & A == A & True == A
716
+ assert False & A == A & False == False
717
+ assert A & B == And(A, B)
718
+ assert True | A == A | True == True
719
+ assert False | A == A | False == A
720
+ assert A | B == Or(A, B)
721
+ assert ~A == Not(A)
722
+ assert True >> A == A << True == A
723
+ assert False >> A == A << False == True
724
+ assert A >> True == True << A == True
725
+ assert A >> False == False << A == ~A
726
+ assert A >> B == B << A == Implies(A, B)
727
+ assert True ^ A == A ^ True == ~A
728
+ assert False ^ A == A ^ False == A
729
+ assert A ^ B == Xor(A, B)
730
+
731
+
732
+ def test_true_false():
733
+ assert true is S.true
734
+ assert false is S.false
735
+ assert true is not True
736
+ assert false is not False
737
+ assert true
738
+ assert not false
739
+ assert true == True
740
+ assert false == False
741
+ assert not (true == False)
742
+ assert not (false == True)
743
+ assert not (true == false)
744
+
745
+ assert hash(true) == hash(True)
746
+ assert hash(false) == hash(False)
747
+ assert len({true, True}) == len({false, False}) == 1
748
+
749
+ assert isinstance(true, BooleanAtom)
750
+ assert isinstance(false, BooleanAtom)
751
+ # We don't want to subclass from bool, because bool subclasses from
752
+ # int. But operators like &, |, ^, <<, >>, and ~ act differently on 0 and
753
+ # 1 then we want them to on true and false. See the docstrings of the
754
+ # various And, Or, etc. functions for examples.
755
+ assert not isinstance(true, bool)
756
+ assert not isinstance(false, bool)
757
+
758
+ # Note: using 'is' comparison is important here. We want these to return
759
+ # true and false, not True and False
760
+
761
+ assert Not(true) is false
762
+ assert Not(True) is false
763
+ assert Not(false) is true
764
+ assert Not(False) is true
765
+ assert ~true is false
766
+ assert ~false is true
767
+
768
+ for T, F in product((True, true), (False, false)):
769
+ assert And(T, F) is false
770
+ assert And(F, T) is false
771
+ assert And(F, F) is false
772
+ assert And(T, T) is true
773
+ assert And(T, x) == x
774
+ assert And(F, x) is false
775
+ if not (T is True and F is False):
776
+ assert T & F is false
777
+ assert F & T is false
778
+ if F is not False:
779
+ assert F & F is false
780
+ if T is not True:
781
+ assert T & T is true
782
+
783
+ assert Or(T, F) is true
784
+ assert Or(F, T) is true
785
+ assert Or(F, F) is false
786
+ assert Or(T, T) is true
787
+ assert Or(T, x) is true
788
+ assert Or(F, x) == x
789
+ if not (T is True and F is False):
790
+ assert T | F is true
791
+ assert F | T is true
792
+ if F is not False:
793
+ assert F | F is false
794
+ if T is not True:
795
+ assert T | T is true
796
+
797
+ assert Xor(T, F) is true
798
+ assert Xor(F, T) is true
799
+ assert Xor(F, F) is false
800
+ assert Xor(T, T) is false
801
+ assert Xor(T, x) == ~x
802
+ assert Xor(F, x) == x
803
+ if not (T is True and F is False):
804
+ assert T ^ F is true
805
+ assert F ^ T is true
806
+ if F is not False:
807
+ assert F ^ F is false
808
+ if T is not True:
809
+ assert T ^ T is false
810
+
811
+ assert Nand(T, F) is true
812
+ assert Nand(F, T) is true
813
+ assert Nand(F, F) is true
814
+ assert Nand(T, T) is false
815
+ assert Nand(T, x) == ~x
816
+ assert Nand(F, x) is true
817
+
818
+ assert Nor(T, F) is false
819
+ assert Nor(F, T) is false
820
+ assert Nor(F, F) is true
821
+ assert Nor(T, T) is false
822
+ assert Nor(T, x) is false
823
+ assert Nor(F, x) == ~x
824
+
825
+ assert Implies(T, F) is false
826
+ assert Implies(F, T) is true
827
+ assert Implies(F, F) is true
828
+ assert Implies(T, T) is true
829
+ assert Implies(T, x) == x
830
+ assert Implies(F, x) is true
831
+ assert Implies(x, T) is true
832
+ assert Implies(x, F) == ~x
833
+ if not (T is True and F is False):
834
+ assert T >> F is false
835
+ assert F << T is false
836
+ assert F >> T is true
837
+ assert T << F is true
838
+ if F is not False:
839
+ assert F >> F is true
840
+ assert F << F is true
841
+ if T is not True:
842
+ assert T >> T is true
843
+ assert T << T is true
844
+
845
+ assert Equivalent(T, F) is false
846
+ assert Equivalent(F, T) is false
847
+ assert Equivalent(F, F) is true
848
+ assert Equivalent(T, T) is true
849
+ assert Equivalent(T, x) == x
850
+ assert Equivalent(F, x) == ~x
851
+ assert Equivalent(x, T) == x
852
+ assert Equivalent(x, F) == ~x
853
+
854
+ assert ITE(T, T, T) is true
855
+ assert ITE(T, T, F) is true
856
+ assert ITE(T, F, T) is false
857
+ assert ITE(T, F, F) is false
858
+ assert ITE(F, T, T) is true
859
+ assert ITE(F, T, F) is false
860
+ assert ITE(F, F, T) is true
861
+ assert ITE(F, F, F) is false
862
+
863
+ assert all(i.simplify(1, 2) is i for i in (S.true, S.false))
864
+
865
+
866
+ def test_bool_as_set():
867
+ assert ITE(y <= 0, False, y >= 1).as_set() == Interval(1, oo)
868
+ assert And(x <= 2, x >= -2).as_set() == Interval(-2, 2)
869
+ assert Or(x >= 2, x <= -2).as_set() == Interval(-oo, -2) + Interval(2, oo)
870
+ assert Not(x > 2).as_set() == Interval(-oo, 2)
871
+ # issue 10240
872
+ assert Not(And(x > 2, x < 3)).as_set() == \
873
+ Union(Interval(-oo, 2), Interval(3, oo))
874
+ assert true.as_set() == S.UniversalSet
875
+ assert false.as_set() is S.EmptySet
876
+ assert x.as_set() == S.UniversalSet
877
+ assert And(Or(x < 1, x > 3), x < 2).as_set() == Interval.open(-oo, 1)
878
+ assert And(x < 1, sin(x) < 3).as_set() == (x < 1).as_set()
879
+ raises(NotImplementedError, lambda: (sin(x) < 1).as_set())
880
+ # watch for object morph in as_set
881
+ assert Eq(-1, cos(2*x)**2/sin(2*x)**2).as_set() is S.EmptySet
882
+
883
+
884
+ @XFAIL
885
+ def test_multivariate_bool_as_set():
886
+ x, y = symbols('x,y')
887
+
888
+ assert And(x >= 0, y >= 0).as_set() == Interval(0, oo)*Interval(0, oo)
889
+ assert Or(x >= 0, y >= 0).as_set() == S.Reals*S.Reals - \
890
+ Interval(-oo, 0, True, True)*Interval(-oo, 0, True, True)
891
+
892
+
893
+ def test_all_or_nothing():
894
+ x = symbols('x', extended_real=True)
895
+ args = x >= -oo, x <= oo
896
+ v = And(*args)
897
+ if v.func is And:
898
+ assert len(v.args) == len(args) - args.count(S.true)
899
+ else:
900
+ assert v == True
901
+ v = Or(*args)
902
+ if v.func is Or:
903
+ assert len(v.args) == 2
904
+ else:
905
+ assert v == True
906
+
907
+
908
+ def test_canonical_atoms():
909
+ assert true.canonical == true
910
+ assert false.canonical == false
911
+
912
+
913
+ def test_negated_atoms():
914
+ assert true.negated == false
915
+ assert false.negated == true
916
+
917
+
918
+ def test_issue_8777():
919
+ assert And(x > 2, x < oo).as_set() == Interval(2, oo, left_open=True)
920
+ assert And(x >= 1, x < oo).as_set() == Interval(1, oo)
921
+ assert (x < oo).as_set() == Interval(-oo, oo)
922
+ assert (x > -oo).as_set() == Interval(-oo, oo)
923
+
924
+
925
+ def test_issue_8975():
926
+ assert Or(And(-oo < x, x <= -2), And(2 <= x, x < oo)).as_set() == \
927
+ Interval(-oo, -2) + Interval(2, oo)
928
+
929
+
930
+ def test_term_to_integer():
931
+ assert term_to_integer([1, 0, 1, 0, 0, 1, 0]) == 82
932
+ assert term_to_integer('0010101000111001') == 10809
933
+
934
+
935
+ def test_issue_21971():
936
+ a, b, c, d = symbols('a b c d')
937
+ f = a & b & c | a & c
938
+ assert f.subs(a & c, d) == b & d | d
939
+ assert f.subs(a & b & c, d) == a & c | d
940
+
941
+ f = (a | b | c) & (a | c)
942
+ assert f.subs(a | c, d) == (b | d) & d
943
+ assert f.subs(a | b | c, d) == (a | c) & d
944
+
945
+ f = (a ^ b ^ c) & (a ^ c)
946
+ assert f.subs(a ^ c, d) == (b ^ d) & d
947
+ assert f.subs(a ^ b ^ c, d) == (a ^ c) & d
948
+
949
+
950
+ def test_truth_table():
951
+ assert list(truth_table(And(x, y), [x, y], input=False)) == \
952
+ [False, False, False, True]
953
+ assert list(truth_table(x | y, [x, y], input=False)) == \
954
+ [False, True, True, True]
955
+ assert list(truth_table(x >> y, [x, y], input=False)) == \
956
+ [True, True, False, True]
957
+ assert list(truth_table(And(x, y), [x, y])) == \
958
+ [([0, 0], False), ([0, 1], False), ([1, 0], False), ([1, 1], True)]
959
+
960
+
961
+ def test_issue_8571():
962
+ for t in (S.true, S.false):
963
+ raises(TypeError, lambda: +t)
964
+ raises(TypeError, lambda: -t)
965
+ raises(TypeError, lambda: abs(t))
966
+ # use int(bool(t)) to get 0 or 1
967
+ raises(TypeError, lambda: int(t))
968
+
969
+ for o in [S.Zero, S.One, x]:
970
+ for _ in range(2):
971
+ raises(TypeError, lambda: o + t)
972
+ raises(TypeError, lambda: o - t)
973
+ raises(TypeError, lambda: o % t)
974
+ raises(TypeError, lambda: o*t)
975
+ raises(TypeError, lambda: o/t)
976
+ raises(TypeError, lambda: o**t)
977
+ o, t = t, o # do again in reversed order
978
+
979
+
980
+ def test_expand_relational():
981
+ n = symbols('n', negative=True)
982
+ p, q = symbols('p q', positive=True)
983
+ r = ((n + q*(-n/q + 1))/(q*(-n/q + 1)) < 0)
984
+ assert r is not S.false
985
+ assert r.expand() is S.false
986
+ assert (q > 0).expand() is S.true
987
+
988
+
989
+ def test_issue_12717():
990
+ assert S.true.is_Atom == True
991
+ assert S.false.is_Atom == True
992
+
993
+
994
+ def test_as_Boolean():
995
+ nz = symbols('nz', nonzero=True)
996
+ assert all(as_Boolean(i) is S.true for i in (True, S.true, 1, nz))
997
+ z = symbols('z', zero=True)
998
+ assert all(as_Boolean(i) is S.false for i in (False, S.false, 0, z))
999
+ assert all(as_Boolean(i) == i for i in (x, x < 0))
1000
+ for i in (2, S(2), x + 1, []):
1001
+ raises(TypeError, lambda: as_Boolean(i))
1002
+
1003
+
1004
+ def test_binary_symbols():
1005
+ assert ITE(x < 1, y, z).binary_symbols == {y, z}
1006
+ for f in (Eq, Ne):
1007
+ assert f(x, 1).binary_symbols == set()
1008
+ assert f(x, True).binary_symbols == {x}
1009
+ assert f(x, False).binary_symbols == {x}
1010
+ assert S.true.binary_symbols == set()
1011
+ assert S.false.binary_symbols == set()
1012
+ assert x.binary_symbols == {x}
1013
+ assert And(x, Eq(y, False), Eq(z, 1)).binary_symbols == {x, y}
1014
+ assert Q.prime(x).binary_symbols == set()
1015
+ assert Q.lt(x, 1).binary_symbols == set()
1016
+ assert Q.is_true(x).binary_symbols == {x}
1017
+ assert Q.eq(x, True).binary_symbols == {x}
1018
+ assert Q.prime(x).binary_symbols == set()
1019
+
1020
+
1021
+ def test_BooleanFunction_diff():
1022
+ assert And(x, y).diff(x) == Piecewise((0, Eq(y, False)), (1, True))
1023
+
1024
+
1025
+ def test_issue_14700():
1026
+ A, B, C, D, E, F, G, H = symbols('A B C D E F G H')
1027
+ q = ((B & D & H & ~F) | (B & H & ~C & ~D) | (B & H & ~C & ~F) |
1028
+ (B & H & ~D & ~G) | (B & H & ~F & ~G) | (C & G & ~B & ~D) |
1029
+ (C & G & ~D & ~H) | (C & G & ~F & ~H) | (D & F & H & ~B) |
1030
+ (D & F & ~G & ~H) | (B & D & F & ~C & ~H) | (D & E & F & ~B & ~C) |
1031
+ (D & F & ~A & ~B & ~C) | (D & F & ~A & ~C & ~H) |
1032
+ (A & B & D & F & ~E & ~H))
1033
+ soldnf = ((B & D & H & ~F) | (D & F & H & ~B) | (B & H & ~C & ~D) |
1034
+ (B & H & ~D & ~G) | (C & G & ~B & ~D) | (C & G & ~D & ~H) |
1035
+ (C & G & ~F & ~H) | (D & F & ~G & ~H) | (D & E & F & ~C & ~H) |
1036
+ (D & F & ~A & ~C & ~H) | (A & B & D & F & ~E & ~H))
1037
+ solcnf = ((B | C | D) & (B | D | G) & (C | D | H) & (C | F | H) &
1038
+ (D | G | H) & (F | G | H) & (B | F | ~D | ~H) &
1039
+ (~B | ~D | ~F | ~H) & (D | ~B | ~C | ~G | ~H) &
1040
+ (A | H | ~C | ~D | ~F | ~G) & (H | ~C | ~D | ~E | ~F | ~G) &
1041
+ (B | E | H | ~A | ~D | ~F | ~G))
1042
+ assert simplify_logic(q, "dnf") == soldnf
1043
+ assert simplify_logic(q, "cnf") == solcnf
1044
+
1045
+ minterms = [[0, 1, 0, 0], [0, 1, 0, 1], [0, 1, 1, 0], [0, 1, 1, 1],
1046
+ [0, 0, 1, 1], [1, 0, 1, 1]]
1047
+ dontcares = [[1, 0, 0, 0], [1, 0, 0, 1], [1, 1, 0, 0], [1, 1, 0, 1]]
1048
+ assert SOPform([w, x, y, z], minterms) == (x & ~w) | (y & z & ~x)
1049
+ # Should not be more complicated with don't cares
1050
+ assert SOPform([w, x, y, z], minterms, dontcares) == \
1051
+ (x & ~w) | (y & z & ~x)
1052
+
1053
+
1054
+ def test_issue_25115():
1055
+ cond = Contains(x, S.Integers)
1056
+ # Previously this raised an exception:
1057
+ assert simplify_logic(cond) == cond
1058
+
1059
+
1060
+ def test_relational_simplification():
1061
+ w, x, y, z = symbols('w x y z', real=True)
1062
+ d, e = symbols('d e', real=False)
1063
+ # Test all combinations or sign and order
1064
+ assert Or(x >= y, x < y).simplify() == S.true
1065
+ assert Or(x >= y, y > x).simplify() == S.true
1066
+ assert Or(x >= y, -x > -y).simplify() == S.true
1067
+ assert Or(x >= y, -y < -x).simplify() == S.true
1068
+ assert Or(-x <= -y, x < y).simplify() == S.true
1069
+ assert Or(-x <= -y, -x > -y).simplify() == S.true
1070
+ assert Or(-x <= -y, y > x).simplify() == S.true
1071
+ assert Or(-x <= -y, -y < -x).simplify() == S.true
1072
+ assert Or(y <= x, x < y).simplify() == S.true
1073
+ assert Or(y <= x, y > x).simplify() == S.true
1074
+ assert Or(y <= x, -x > -y).simplify() == S.true
1075
+ assert Or(y <= x, -y < -x).simplify() == S.true
1076
+ assert Or(-y >= -x, x < y).simplify() == S.true
1077
+ assert Or(-y >= -x, y > x).simplify() == S.true
1078
+ assert Or(-y >= -x, -x > -y).simplify() == S.true
1079
+ assert Or(-y >= -x, -y < -x).simplify() == S.true
1080
+
1081
+ assert Or(x < y, x >= y).simplify() == S.true
1082
+ assert Or(y > x, x >= y).simplify() == S.true
1083
+ assert Or(-x > -y, x >= y).simplify() == S.true
1084
+ assert Or(-y < -x, x >= y).simplify() == S.true
1085
+ assert Or(x < y, -x <= -y).simplify() == S.true
1086
+ assert Or(-x > -y, -x <= -y).simplify() == S.true
1087
+ assert Or(y > x, -x <= -y).simplify() == S.true
1088
+ assert Or(-y < -x, -x <= -y).simplify() == S.true
1089
+ assert Or(x < y, y <= x).simplify() == S.true
1090
+ assert Or(y > x, y <= x).simplify() == S.true
1091
+ assert Or(-x > -y, y <= x).simplify() == S.true
1092
+ assert Or(-y < -x, y <= x).simplify() == S.true
1093
+ assert Or(x < y, -y >= -x).simplify() == S.true
1094
+ assert Or(y > x, -y >= -x).simplify() == S.true
1095
+ assert Or(-x > -y, -y >= -x).simplify() == S.true
1096
+ assert Or(-y < -x, -y >= -x).simplify() == S.true
1097
+
1098
+ # Some other tests
1099
+ assert Or(x >= y, w < z, x <= y).simplify() == S.true
1100
+ assert And(x >= y, x < y).simplify() == S.false
1101
+ assert Or(x >= y, Eq(y, x)).simplify() == (x >= y)
1102
+ assert And(x >= y, Eq(y, x)).simplify() == Eq(x, y)
1103
+ assert And(Eq(x, y), x >= 1, 2 < y, y >= 5, z < y).simplify() == \
1104
+ (Eq(x, y) & (x >= 1) & (y >= 5) & (y > z))
1105
+ assert Or(Eq(x, y), x >= y, w < y, z < y).simplify() == \
1106
+ (x >= y) | (y > z) | (w < y)
1107
+ assert And(Eq(x, y), x >= y, w < y, y >= z, z < y).simplify() == \
1108
+ Eq(x, y) & (y > z) & (w < y)
1109
+ # assert And(Eq(x, y), x >= y, w < y, y >= z, z < y).simplify(relational_minmax=True) == \
1110
+ # And(Eq(x, y), y > Max(w, z))
1111
+ # assert Or(Eq(x, y), x >= 1, 2 < y, y >= 5, z < y).simplify(relational_minmax=True) == \
1112
+ # (Eq(x, y) | (x >= 1) | (y > Min(2, z)))
1113
+ assert And(Eq(x, y), x >= 1, 2 < y, y >= 5, z < y).simplify() == \
1114
+ (Eq(x, y) & (x >= 1) & (y >= 5) & (y > z))
1115
+ assert (Eq(x, y) & Eq(d, e) & (x >= y) & (d >= e)).simplify() == \
1116
+ (Eq(x, y) & Eq(d, e) & (d >= e))
1117
+ assert And(Eq(x, y), Eq(x, -y)).simplify() == And(Eq(x, 0), Eq(y, 0))
1118
+ assert Xor(x >= y, x <= y).simplify() == Ne(x, y)
1119
+ assert And(x > 1, x < -1, Eq(x, y)).simplify() == S.false
1120
+ # From #16690
1121
+ assert And(x >= y, Eq(y, 0)).simplify() == And(x >= 0, Eq(y, 0))
1122
+ assert Or(Ne(x, 1), Ne(x, 2)).simplify() == S.true
1123
+ assert And(Eq(x, 1), Ne(2, x)).simplify() == Eq(x, 1)
1124
+ assert Or(Eq(x, 1), Ne(2, x)).simplify() == Ne(x, 2)
1125
+
1126
+ def test_issue_8373():
1127
+ x = symbols('x', real=True)
1128
+ assert Or(x < 1, x > -1).simplify() == S.true
1129
+ assert Or(x < 1, x >= 1).simplify() == S.true
1130
+ assert And(x < 1, x >= 1).simplify() == S.false
1131
+ assert Or(x <= 1, x >= 1).simplify() == S.true
1132
+
1133
+
1134
+ def test_issue_7950():
1135
+ x = symbols('x', real=True)
1136
+ assert And(Eq(x, 1), Eq(x, 2)).simplify() == S.false
1137
+
1138
+
1139
+ @slow
1140
+ def test_relational_simplification_numerically():
1141
+ def test_simplification_numerically_function(original, simplified):
1142
+ symb = original.free_symbols
1143
+ n = len(symb)
1144
+ valuelist = list(set(combinations(list(range(-(n-1), n))*n, n)))
1145
+ for values in valuelist:
1146
+ sublist = dict(zip(symb, values))
1147
+ originalvalue = original.subs(sublist)
1148
+ simplifiedvalue = simplified.subs(sublist)
1149
+ assert originalvalue == simplifiedvalue, "Original: {}\nand"\
1150
+ " simplified: {}\ndo not evaluate to the same value for {}"\
1151
+ "".format(original, simplified, sublist)
1152
+
1153
+ w, x, y, z = symbols('w x y z', real=True)
1154
+ d, e = symbols('d e', real=False)
1155
+
1156
+ expressions = (And(Eq(x, y), x >= y, w < y, y >= z, z < y),
1157
+ And(Eq(x, y), x >= 1, 2 < y, y >= 5, z < y),
1158
+ Or(Eq(x, y), x >= 1, 2 < y, y >= 5, z < y),
1159
+ And(x >= y, Eq(y, x)),
1160
+ Or(And(Eq(x, y), x >= y, w < y, Or(y >= z, z < y)),
1161
+ And(Eq(x, y), x >= 1, 2 < y, y >= -1, z < y)),
1162
+ (Eq(x, y) & Eq(d, e) & (x >= y) & (d >= e)),
1163
+ )
1164
+
1165
+ for expression in expressions:
1166
+ test_simplification_numerically_function(expression,
1167
+ expression.simplify())
1168
+
1169
+
1170
+ def test_relational_simplification_patterns_numerically():
1171
+ from sympy.core import Wild
1172
+ from sympy.logic.boolalg import _simplify_patterns_and, \
1173
+ _simplify_patterns_or, _simplify_patterns_xor
1174
+ a = Wild('a')
1175
+ b = Wild('b')
1176
+ c = Wild('c')
1177
+ symb = [a, b, c]
1178
+ patternlists = [[And, _simplify_patterns_and()],
1179
+ [Or, _simplify_patterns_or()],
1180
+ [Xor, _simplify_patterns_xor()]]
1181
+ valuelist = list(set(combinations(list(range(-2, 3))*3, 3)))
1182
+ # Skip combinations of +/-2 and 0, except for all 0
1183
+ valuelist = [v for v in valuelist if any(w % 2 for w in v) or not any(v)]
1184
+ for func, patternlist in patternlists:
1185
+ for pattern in patternlist:
1186
+ original = func(*pattern[0].args)
1187
+ simplified = pattern[1]
1188
+ for values in valuelist:
1189
+ sublist = dict(zip(symb, values))
1190
+ originalvalue = original.xreplace(sublist)
1191
+ simplifiedvalue = simplified.xreplace(sublist)
1192
+ assert originalvalue == simplifiedvalue, "Original: {}\nand"\
1193
+ " simplified: {}\ndo not evaluate to the same value for"\
1194
+ "{}".format(pattern[0], simplified, sublist)
1195
+
1196
+
1197
+ def test_issue_16803():
1198
+ n = symbols('n')
1199
+ # No simplification done, but should not raise an exception
1200
+ assert ((n > 3) | (n < 0) | ((n > 0) & (n < 3))).simplify() == \
1201
+ (n > 3) | (n < 0) | ((n > 0) & (n < 3))
1202
+
1203
+
1204
+ def test_issue_17530():
1205
+ r = {x: oo, y: oo}
1206
+ assert Or(x + y > 0, x - y < 0).subs(r)
1207
+ assert not And(x + y < 0, x - y < 0).subs(r)
1208
+ raises(TypeError, lambda: Or(x + y < 0, x - y < 0).subs(r))
1209
+ raises(TypeError, lambda: And(x + y > 0, x - y < 0).subs(r))
1210
+ raises(TypeError, lambda: And(x + y > 0, x - y < 0).subs(r))
1211
+
1212
+
1213
+ def test_anf_coeffs():
1214
+ assert anf_coeffs([1, 0]) == [1, 1]
1215
+ assert anf_coeffs([0, 0, 0, 1]) == [0, 0, 0, 1]
1216
+ assert anf_coeffs([0, 1, 1, 1]) == [0, 1, 1, 1]
1217
+ assert anf_coeffs([1, 1, 1, 0]) == [1, 0, 0, 1]
1218
+ assert anf_coeffs([1, 0, 0, 0]) == [1, 1, 1, 1]
1219
+ assert anf_coeffs([1, 0, 0, 1]) == [1, 1, 1, 0]
1220
+ assert anf_coeffs([1, 1, 0, 1]) == [1, 0, 1, 1]
1221
+
1222
+
1223
+ def test_ANFform():
1224
+ x, y = symbols('x,y')
1225
+ assert ANFform([x], [1, 1]) == True
1226
+ assert ANFform([x], [0, 0]) == False
1227
+ assert ANFform([x], [1, 0]) == Xor(x, True, remove_true=False)
1228
+ assert ANFform([x, y], [1, 1, 1, 0]) == \
1229
+ Xor(True, And(x, y), remove_true=False)
1230
+
1231
+
1232
+ def test_bool_minterm():
1233
+ x, y = symbols('x,y')
1234
+ assert bool_minterm(3, [x, y]) == And(x, y)
1235
+ assert bool_minterm([1, 0], [x, y]) == And(Not(y), x)
1236
+
1237
+
1238
+ def test_bool_maxterm():
1239
+ x, y = symbols('x,y')
1240
+ assert bool_maxterm(2, [x, y]) == Or(Not(x), y)
1241
+ assert bool_maxterm([0, 1], [x, y]) == Or(Not(y), x)
1242
+
1243
+
1244
+ def test_bool_monomial():
1245
+ x, y = symbols('x,y')
1246
+ assert bool_monomial(1, [x, y]) == y
1247
+ assert bool_monomial([1, 1], [x, y]) == And(x, y)
1248
+
1249
+
1250
+ def test_check_pair():
1251
+ assert _check_pair([0, 1, 0], [0, 1, 1]) == 2
1252
+ assert _check_pair([0, 1, 0], [1, 1, 1]) == -1
1253
+
1254
+
1255
+ def test_issue_19114():
1256
+ expr = (B & C) | (A & ~C) | (~A & ~B)
1257
+ # Expression is minimal, but there are multiple minimal forms possible
1258
+ res1 = (A & B) | (C & ~A) | (~B & ~C)
1259
+ result = to_dnf(expr, simplify=True)
1260
+ assert result in (expr, res1)
1261
+
1262
+
1263
+ def test_issue_20870():
1264
+ result = SOPform([a, b, c, d], [1, 2, 3, 4, 5, 6, 8, 9, 11, 12, 14, 15])
1265
+ expected = ((d & ~b) | (a & b & c) | (a & ~c & ~d) |
1266
+ (b & ~a & ~c) | (c & ~a & ~d))
1267
+ assert result == expected
1268
+
1269
+
1270
+ def test_convert_to_varsSOP():
1271
+ assert _convert_to_varsSOP([0, 1, 0], [x, y, z]) == And(Not(x), y, Not(z))
1272
+ assert _convert_to_varsSOP([3, 1, 0], [x, y, z]) == And(y, Not(z))
1273
+
1274
+
1275
+ def test_convert_to_varsPOS():
1276
+ assert _convert_to_varsPOS([0, 1, 0], [x, y, z]) == Or(x, Not(y), z)
1277
+ assert _convert_to_varsPOS([3, 1, 0], [x, y, z]) == Or(Not(y), z)
1278
+
1279
+
1280
+ def test_gateinputcount():
1281
+ a, b, c, d, e = symbols('a:e')
1282
+ assert gateinputcount(And(a, b)) == 2
1283
+ assert gateinputcount(a | b & c & d ^ (e | a)) == 9
1284
+ assert gateinputcount(And(a, True)) == 0
1285
+ raises(TypeError, lambda: gateinputcount(a*b))
1286
+
1287
+
1288
+ def test_refine():
1289
+ # relational
1290
+ assert not refine(x < 0, ~(x < 0))
1291
+ assert refine(x < 0, (x < 0))
1292
+ assert refine(x < 0, (0 > x)) is S.true
1293
+ assert refine(x < 0, (y < 0)) == (x < 0)
1294
+ assert not refine(x <= 0, ~(x <= 0))
1295
+ assert refine(x <= 0, (x <= 0))
1296
+ assert refine(x <= 0, (0 >= x)) is S.true
1297
+ assert refine(x <= 0, (y <= 0)) == (x <= 0)
1298
+ assert not refine(x > 0, ~(x > 0))
1299
+ assert refine(x > 0, (x > 0))
1300
+ assert refine(x > 0, (0 < x)) is S.true
1301
+ assert refine(x > 0, (y > 0)) == (x > 0)
1302
+ assert not refine(x >= 0, ~(x >= 0))
1303
+ assert refine(x >= 0, (x >= 0))
1304
+ assert refine(x >= 0, (0 <= x)) is S.true
1305
+ assert refine(x >= 0, (y >= 0)) == (x >= 0)
1306
+ assert not refine(Eq(x, 0), ~(Eq(x, 0)))
1307
+ assert refine(Eq(x, 0), (Eq(x, 0)))
1308
+ assert refine(Eq(x, 0), (Eq(0, x))) is S.true
1309
+ assert refine(Eq(x, 0), (Eq(y, 0))) == Eq(x, 0)
1310
+ assert not refine(Ne(x, 0), ~(Ne(x, 0)))
1311
+ assert refine(Ne(x, 0), (Ne(0, x))) is S.true
1312
+ assert refine(Ne(x, 0), (Ne(x, 0)))
1313
+ assert refine(Ne(x, 0), (Ne(y, 0))) == (Ne(x, 0))
1314
+
1315
+ # boolean functions
1316
+ assert refine(And(x > 0, y > 0), (x > 0)) == (y > 0)
1317
+ assert refine(And(x > 0, y > 0), (x > 0) & (y > 0)) is S.true
1318
+
1319
+ # predicates
1320
+ assert refine(Q.positive(x), Q.positive(x)) is S.true
1321
+ assert refine(Q.positive(x), Q.negative(x)) is S.false
1322
+ assert refine(Q.positive(x), Q.real(x)) == Q.positive(x)
1323
+
1324
+
1325
+ def test_relational_threeterm_simplification_patterns_numerically():
1326
+ from sympy.core import Wild
1327
+ from sympy.logic.boolalg import _simplify_patterns_and3
1328
+ a = Wild('a')
1329
+ b = Wild('b')
1330
+ c = Wild('c')
1331
+ symb = [a, b, c]
1332
+ patternlists = [[And, _simplify_patterns_and3()]]
1333
+ valuelist = list(set(combinations(list(range(-2, 3))*3, 3)))
1334
+ # Skip combinations of +/-2 and 0, except for all 0
1335
+ valuelist = [v for v in valuelist if any(w % 2 for w in v) or not any(v)]
1336
+ for func, patternlist in patternlists:
1337
+ for pattern in patternlist:
1338
+ original = func(*pattern[0].args)
1339
+ simplified = pattern[1]
1340
+ for values in valuelist:
1341
+ sublist = dict(zip(symb, values))
1342
+ originalvalue = original.xreplace(sublist)
1343
+ simplifiedvalue = simplified.xreplace(sublist)
1344
+ assert originalvalue == simplifiedvalue, "Original: {}\nand"\
1345
+ " simplified: {}\ndo not evaluate to the same value for"\
1346
+ "{}".format(pattern[0], simplified, sublist)
1347
+
1348
+
1349
+ def test_issue_25451():
1350
+ x = Or(And(a, c), Eq(a, b))
1351
+ assert isinstance(x, Or)
1352
+ assert set(x.args) == {And(a, c), Eq(a, b)}
wemm/lib/python3.10/site-packages/sympy/logic/tests/test_dimacs.py ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Various tests on satisfiability using dimacs cnf file syntax
2
+ You can find lots of cnf files in
3
+ ftp://dimacs.rutgers.edu/pub/challenge/satisfiability/benchmarks/cnf/
4
+ """
5
+
6
+ from sympy.logic.utilities.dimacs import load
7
+ from sympy.logic.algorithms.dpll import dpll_satisfiable
8
+
9
+
10
+ def test_f1():
11
+ assert bool(dpll_satisfiable(load(f1)))
12
+
13
+
14
+ def test_f2():
15
+ assert bool(dpll_satisfiable(load(f2)))
16
+
17
+
18
+ def test_f3():
19
+ assert bool(dpll_satisfiable(load(f3)))
20
+
21
+
22
+ def test_f4():
23
+ assert not bool(dpll_satisfiable(load(f4)))
24
+
25
+
26
+ def test_f5():
27
+ assert bool(dpll_satisfiable(load(f5)))
28
+
29
+ f1 = """c simple example
30
+ c Resolution: SATISFIABLE
31
+ c
32
+ p cnf 3 2
33
+ 1 -3 0
34
+ 2 3 -1 0
35
+ """
36
+
37
+
38
+ f2 = """c an example from Quinn's text, 16 variables and 18 clauses.
39
+ c Resolution: SATISFIABLE
40
+ c
41
+ p cnf 16 18
42
+ 1 2 0
43
+ -2 -4 0
44
+ 3 4 0
45
+ -4 -5 0
46
+ 5 -6 0
47
+ 6 -7 0
48
+ 6 7 0
49
+ 7 -16 0
50
+ 8 -9 0
51
+ -8 -14 0
52
+ 9 10 0
53
+ 9 -10 0
54
+ -10 -11 0
55
+ 10 12 0
56
+ 11 12 0
57
+ 13 14 0
58
+ 14 -15 0
59
+ 15 16 0
60
+ """
61
+
62
+ f3 = """c
63
+ p cnf 6 9
64
+ -1 0
65
+ -3 0
66
+ 2 -1 0
67
+ 2 -4 0
68
+ 5 -4 0
69
+ -1 -3 0
70
+ -4 -6 0
71
+ 1 3 -2 0
72
+ 4 6 -2 -5 0
73
+ """
74
+
75
+ f4 = """c
76
+ c file: hole6.cnf [http://people.sc.fsu.edu/~jburkardt/data/cnf/hole6.cnf]
77
+ c
78
+ c SOURCE: John Hooker (jh38+@andrew.cmu.edu)
79
+ c
80
+ c DESCRIPTION: Pigeon hole problem of placing n (for file 'holen.cnf') pigeons
81
+ c in n+1 holes without placing 2 pigeons in the same hole
82
+ c
83
+ c NOTE: Part of the collection at the Forschungsinstitut fuer
84
+ c anwendungsorientierte Wissensverarbeitung in Ulm Germany.
85
+ c
86
+ c NOTE: Not satisfiable
87
+ c
88
+ p cnf 42 133
89
+ -1 -7 0
90
+ -1 -13 0
91
+ -1 -19 0
92
+ -1 -25 0
93
+ -1 -31 0
94
+ -1 -37 0
95
+ -7 -13 0
96
+ -7 -19 0
97
+ -7 -25 0
98
+ -7 -31 0
99
+ -7 -37 0
100
+ -13 -19 0
101
+ -13 -25 0
102
+ -13 -31 0
103
+ -13 -37 0
104
+ -19 -25 0
105
+ -19 -31 0
106
+ -19 -37 0
107
+ -25 -31 0
108
+ -25 -37 0
109
+ -31 -37 0
110
+ -2 -8 0
111
+ -2 -14 0
112
+ -2 -20 0
113
+ -2 -26 0
114
+ -2 -32 0
115
+ -2 -38 0
116
+ -8 -14 0
117
+ -8 -20 0
118
+ -8 -26 0
119
+ -8 -32 0
120
+ -8 -38 0
121
+ -14 -20 0
122
+ -14 -26 0
123
+ -14 -32 0
124
+ -14 -38 0
125
+ -20 -26 0
126
+ -20 -32 0
127
+ -20 -38 0
128
+ -26 -32 0
129
+ -26 -38 0
130
+ -32 -38 0
131
+ -3 -9 0
132
+ -3 -15 0
133
+ -3 -21 0
134
+ -3 -27 0
135
+ -3 -33 0
136
+ -3 -39 0
137
+ -9 -15 0
138
+ -9 -21 0
139
+ -9 -27 0
140
+ -9 -33 0
141
+ -9 -39 0
142
+ -15 -21 0
143
+ -15 -27 0
144
+ -15 -33 0
145
+ -15 -39 0
146
+ -21 -27 0
147
+ -21 -33 0
148
+ -21 -39 0
149
+ -27 -33 0
150
+ -27 -39 0
151
+ -33 -39 0
152
+ -4 -10 0
153
+ -4 -16 0
154
+ -4 -22 0
155
+ -4 -28 0
156
+ -4 -34 0
157
+ -4 -40 0
158
+ -10 -16 0
159
+ -10 -22 0
160
+ -10 -28 0
161
+ -10 -34 0
162
+ -10 -40 0
163
+ -16 -22 0
164
+ -16 -28 0
165
+ -16 -34 0
166
+ -16 -40 0
167
+ -22 -28 0
168
+ -22 -34 0
169
+ -22 -40 0
170
+ -28 -34 0
171
+ -28 -40 0
172
+ -34 -40 0
173
+ -5 -11 0
174
+ -5 -17 0
175
+ -5 -23 0
176
+ -5 -29 0
177
+ -5 -35 0
178
+ -5 -41 0
179
+ -11 -17 0
180
+ -11 -23 0
181
+ -11 -29 0
182
+ -11 -35 0
183
+ -11 -41 0
184
+ -17 -23 0
185
+ -17 -29 0
186
+ -17 -35 0
187
+ -17 -41 0
188
+ -23 -29 0
189
+ -23 -35 0
190
+ -23 -41 0
191
+ -29 -35 0
192
+ -29 -41 0
193
+ -35 -41 0
194
+ -6 -12 0
195
+ -6 -18 0
196
+ -6 -24 0
197
+ -6 -30 0
198
+ -6 -36 0
199
+ -6 -42 0
200
+ -12 -18 0
201
+ -12 -24 0
202
+ -12 -30 0
203
+ -12 -36 0
204
+ -12 -42 0
205
+ -18 -24 0
206
+ -18 -30 0
207
+ -18 -36 0
208
+ -18 -42 0
209
+ -24 -30 0
210
+ -24 -36 0
211
+ -24 -42 0
212
+ -30 -36 0
213
+ -30 -42 0
214
+ -36 -42 0
215
+ 6 5 4 3 2 1 0
216
+ 12 11 10 9 8 7 0
217
+ 18 17 16 15 14 13 0
218
+ 24 23 22 21 20 19 0
219
+ 30 29 28 27 26 25 0
220
+ 36 35 34 33 32 31 0
221
+ 42 41 40 39 38 37 0
222
+ """
223
+
224
+ f5 = """c simple example requiring variable selection
225
+ c
226
+ c NOTE: Satisfiable
227
+ c
228
+ p cnf 5 5
229
+ 1 2 3 0
230
+ 1 -2 3 0
231
+ 4 5 -3 0
232
+ 1 -4 -3 0
233
+ -1 -5 0
234
+ """
wemm/lib/python3.10/site-packages/sympy/logic/tests/test_inference.py ADDED
@@ -0,0 +1,381 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """For more tests on satisfiability, see test_dimacs"""
2
+
3
+ from sympy.assumptions.ask import Q
4
+ from sympy.core.symbol import symbols
5
+ from sympy.core.relational import Unequality
6
+ from sympy.logic.boolalg import And, Or, Implies, Equivalent, true, false
7
+ from sympy.logic.inference import literal_symbol, \
8
+ pl_true, satisfiable, valid, entails, PropKB
9
+ from sympy.logic.algorithms.dpll import dpll, dpll_satisfiable, \
10
+ find_pure_symbol, find_unit_clause, unit_propagate, \
11
+ find_pure_symbol_int_repr, find_unit_clause_int_repr, \
12
+ unit_propagate_int_repr
13
+ from sympy.logic.algorithms.dpll2 import dpll_satisfiable as dpll2_satisfiable
14
+
15
+ from sympy.logic.algorithms.z3_wrapper import z3_satisfiable
16
+ from sympy.assumptions.cnf import CNF, EncodedCNF
17
+ from sympy.logic.tests.test_lra_theory import make_random_problem
18
+ from sympy.core.random import randint
19
+
20
+ from sympy.testing.pytest import raises, skip
21
+ from sympy.external import import_module
22
+
23
+
24
+ def test_literal():
25
+ A, B = symbols('A,B')
26
+ assert literal_symbol(True) is True
27
+ assert literal_symbol(False) is False
28
+ assert literal_symbol(A) is A
29
+ assert literal_symbol(~A) is A
30
+
31
+
32
+ def test_find_pure_symbol():
33
+ A, B, C = symbols('A,B,C')
34
+ assert find_pure_symbol([A], [A]) == (A, True)
35
+ assert find_pure_symbol([A, B], [~A | B, ~B | A]) == (None, None)
36
+ assert find_pure_symbol([A, B, C], [ A | ~B, ~B | ~C, C | A]) == (A, True)
37
+ assert find_pure_symbol([A, B, C], [~A | B, B | ~C, C | A]) == (B, True)
38
+ assert find_pure_symbol([A, B, C], [~A | ~B, ~B | ~C, C | A]) == (B, False)
39
+ assert find_pure_symbol(
40
+ [A, B, C], [~A | B, ~B | ~C, C | A]) == (None, None)
41
+
42
+
43
+ def test_find_pure_symbol_int_repr():
44
+ assert find_pure_symbol_int_repr([1], [{1}]) == (1, True)
45
+ assert find_pure_symbol_int_repr([1, 2],
46
+ [{-1, 2}, {-2, 1}]) == (None, None)
47
+ assert find_pure_symbol_int_repr([1, 2, 3],
48
+ [{1, -2}, {-2, -3}, {3, 1}]) == (1, True)
49
+ assert find_pure_symbol_int_repr([1, 2, 3],
50
+ [{-1, 2}, {2, -3}, {3, 1}]) == (2, True)
51
+ assert find_pure_symbol_int_repr([1, 2, 3],
52
+ [{-1, -2}, {-2, -3}, {3, 1}]) == (2, False)
53
+ assert find_pure_symbol_int_repr([1, 2, 3],
54
+ [{-1, 2}, {-2, -3}, {3, 1}]) == (None, None)
55
+
56
+
57
+ def test_unit_clause():
58
+ A, B, C = symbols('A,B,C')
59
+ assert find_unit_clause([A], {}) == (A, True)
60
+ assert find_unit_clause([A, ~A], {}) == (A, True) # Wrong ??
61
+ assert find_unit_clause([A | B], {A: True}) == (B, True)
62
+ assert find_unit_clause([A | B], {B: True}) == (A, True)
63
+ assert find_unit_clause(
64
+ [A | B | C, B | ~C, A | ~B], {A: True}) == (B, False)
65
+ assert find_unit_clause([A | B | C, B | ~C, A | B], {A: True}) == (B, True)
66
+ assert find_unit_clause([A | B | C, B | ~C, A ], {}) == (A, True)
67
+
68
+
69
+ def test_unit_clause_int_repr():
70
+ assert find_unit_clause_int_repr(map(set, [[1]]), {}) == (1, True)
71
+ assert find_unit_clause_int_repr(map(set, [[1], [-1]]), {}) == (1, True)
72
+ assert find_unit_clause_int_repr([{1, 2}], {1: True}) == (2, True)
73
+ assert find_unit_clause_int_repr([{1, 2}], {2: True}) == (1, True)
74
+ assert find_unit_clause_int_repr(map(set,
75
+ [[1, 2, 3], [2, -3], [1, -2]]), {1: True}) == (2, False)
76
+ assert find_unit_clause_int_repr(map(set,
77
+ [[1, 2, 3], [3, -3], [1, 2]]), {1: True}) == (2, True)
78
+
79
+ A, B, C = symbols('A,B,C')
80
+ assert find_unit_clause([A | B | C, B | ~C, A ], {}) == (A, True)
81
+
82
+
83
+ def test_unit_propagate():
84
+ A, B, C = symbols('A,B,C')
85
+ assert unit_propagate([A | B], A) == []
86
+ assert unit_propagate([A | B, ~A | C, ~C | B, A], A) == [C, ~C | B, A]
87
+
88
+
89
+ def test_unit_propagate_int_repr():
90
+ assert unit_propagate_int_repr([{1, 2}], 1) == []
91
+ assert unit_propagate_int_repr(map(set,
92
+ [[1, 2], [-1, 3], [-3, 2], [1]]), 1) == [{3}, {-3, 2}]
93
+
94
+
95
+ def test_dpll():
96
+ """This is also tested in test_dimacs"""
97
+ A, B, C = symbols('A,B,C')
98
+ assert dpll([A | B], [A, B], {A: True, B: True}) == {A: True, B: True}
99
+
100
+
101
+ def test_dpll_satisfiable():
102
+ A, B, C = symbols('A,B,C')
103
+ assert dpll_satisfiable( A & ~A ) is False
104
+ assert dpll_satisfiable( A & ~B ) == {A: True, B: False}
105
+ assert dpll_satisfiable(
106
+ A | B ) in ({A: True}, {B: True}, {A: True, B: True})
107
+ assert dpll_satisfiable(
108
+ (~A | B) & (~B | A) ) in ({A: True, B: True}, {A: False, B: False})
109
+ assert dpll_satisfiable( (A | B) & (~B | C) ) in ({A: True, B: False},
110
+ {A: True, C: True}, {B: True, C: True})
111
+ assert dpll_satisfiable( A & B & C ) == {A: True, B: True, C: True}
112
+ assert dpll_satisfiable( (A | B) & (A >> B) ) == {B: True}
113
+ assert dpll_satisfiable( Equivalent(A, B) & A ) == {A: True, B: True}
114
+ assert dpll_satisfiable( Equivalent(A, B) & ~A ) == {A: False, B: False}
115
+
116
+
117
+ def test_dpll2_satisfiable():
118
+ A, B, C = symbols('A,B,C')
119
+ assert dpll2_satisfiable( A & ~A ) is False
120
+ assert dpll2_satisfiable( A & ~B ) == {A: True, B: False}
121
+ assert dpll2_satisfiable(
122
+ A | B ) in ({A: True}, {B: True}, {A: True, B: True})
123
+ assert dpll2_satisfiable(
124
+ (~A | B) & (~B | A) ) in ({A: True, B: True}, {A: False, B: False})
125
+ assert dpll2_satisfiable( (A | B) & (~B | C) ) in ({A: True, B: False, C: True},
126
+ {A: True, B: True, C: True})
127
+ assert dpll2_satisfiable( A & B & C ) == {A: True, B: True, C: True}
128
+ assert dpll2_satisfiable( (A | B) & (A >> B) ) in ({B: True, A: False},
129
+ {B: True, A: True})
130
+ assert dpll2_satisfiable( Equivalent(A, B) & A ) == {A: True, B: True}
131
+ assert dpll2_satisfiable( Equivalent(A, B) & ~A ) == {A: False, B: False}
132
+
133
+
134
+ def test_minisat22_satisfiable():
135
+ A, B, C = symbols('A,B,C')
136
+ minisat22_satisfiable = lambda expr: satisfiable(expr, algorithm="minisat22")
137
+ assert minisat22_satisfiable( A & ~A ) is False
138
+ assert minisat22_satisfiable( A & ~B ) == {A: True, B: False}
139
+ assert minisat22_satisfiable(
140
+ A | B ) in ({A: True}, {B: False}, {A: False, B: True}, {A: True, B: True}, {A: True, B: False})
141
+ assert minisat22_satisfiable(
142
+ (~A | B) & (~B | A) ) in ({A: True, B: True}, {A: False, B: False})
143
+ assert minisat22_satisfiable( (A | B) & (~B | C) ) in ({A: True, B: False, C: True},
144
+ {A: True, B: True, C: True}, {A: False, B: True, C: True}, {A: True, B: False, C: False})
145
+ assert minisat22_satisfiable( A & B & C ) == {A: True, B: True, C: True}
146
+ assert minisat22_satisfiable( (A | B) & (A >> B) ) in ({B: True, A: False},
147
+ {B: True, A: True})
148
+ assert minisat22_satisfiable( Equivalent(A, B) & A ) == {A: True, B: True}
149
+ assert minisat22_satisfiable( Equivalent(A, B) & ~A ) == {A: False, B: False}
150
+
151
+ def test_minisat22_minimal_satisfiable():
152
+ A, B, C = symbols('A,B,C')
153
+ minisat22_satisfiable = lambda expr, minimal=True: satisfiable(expr, algorithm="minisat22", minimal=True)
154
+ assert minisat22_satisfiable( A & ~A ) is False
155
+ assert minisat22_satisfiable( A & ~B ) == {A: True, B: False}
156
+ assert minisat22_satisfiable(
157
+ A | B ) in ({A: True}, {B: False}, {A: False, B: True}, {A: True, B: True}, {A: True, B: False})
158
+ assert minisat22_satisfiable(
159
+ (~A | B) & (~B | A) ) in ({A: True, B: True}, {A: False, B: False})
160
+ assert minisat22_satisfiable( (A | B) & (~B | C) ) in ({A: True, B: False, C: True},
161
+ {A: True, B: True, C: True}, {A: False, B: True, C: True}, {A: True, B: False, C: False})
162
+ assert minisat22_satisfiable( A & B & C ) == {A: True, B: True, C: True}
163
+ assert minisat22_satisfiable( (A | B) & (A >> B) ) in ({B: True, A: False},
164
+ {B: True, A: True})
165
+ assert minisat22_satisfiable( Equivalent(A, B) & A ) == {A: True, B: True}
166
+ assert minisat22_satisfiable( Equivalent(A, B) & ~A ) == {A: False, B: False}
167
+ g = satisfiable((A | B | C),algorithm="minisat22",minimal=True,all_models=True)
168
+ sol = next(g)
169
+ first_solution = {key for key, value in sol.items() if value}
170
+ sol=next(g)
171
+ second_solution = {key for key, value in sol.items() if value}
172
+ sol=next(g)
173
+ third_solution = {key for key, value in sol.items() if value}
174
+ assert not first_solution <= second_solution
175
+ assert not second_solution <= third_solution
176
+ assert not first_solution <= third_solution
177
+
178
+ def test_satisfiable():
179
+ A, B, C = symbols('A,B,C')
180
+ assert satisfiable(A & (A >> B) & ~B) is False
181
+
182
+
183
+ def test_valid():
184
+ A, B, C = symbols('A,B,C')
185
+ assert valid(A >> (B >> A)) is True
186
+ assert valid((A >> (B >> C)) >> ((A >> B) >> (A >> C))) is True
187
+ assert valid((~B >> ~A) >> (A >> B)) is True
188
+ assert valid(A | B | C) is False
189
+ assert valid(A >> B) is False
190
+
191
+
192
+ def test_pl_true():
193
+ A, B, C = symbols('A,B,C')
194
+ assert pl_true(True) is True
195
+ assert pl_true( A & B, {A: True, B: True}) is True
196
+ assert pl_true( A | B, {A: True}) is True
197
+ assert pl_true( A | B, {B: True}) is True
198
+ assert pl_true( A | B, {A: None, B: True}) is True
199
+ assert pl_true( A >> B, {A: False}) is True
200
+ assert pl_true( A | B | ~C, {A: False, B: True, C: True}) is True
201
+ assert pl_true(Equivalent(A, B), {A: False, B: False}) is True
202
+
203
+ # test for false
204
+ assert pl_true(False) is False
205
+ assert pl_true( A & B, {A: False, B: False}) is False
206
+ assert pl_true( A & B, {A: False}) is False
207
+ assert pl_true( A & B, {B: False}) is False
208
+ assert pl_true( A | B, {A: False, B: False}) is False
209
+
210
+ #test for None
211
+ assert pl_true(B, {B: None}) is None
212
+ assert pl_true( A & B, {A: True, B: None}) is None
213
+ assert pl_true( A >> B, {A: True, B: None}) is None
214
+ assert pl_true(Equivalent(A, B), {A: None}) is None
215
+ assert pl_true(Equivalent(A, B), {A: True, B: None}) is None
216
+
217
+ # Test for deep
218
+ assert pl_true(A | B, {A: False}, deep=True) is None
219
+ assert pl_true(~A & ~B, {A: False}, deep=True) is None
220
+ assert pl_true(A | B, {A: False, B: False}, deep=True) is False
221
+ assert pl_true(A & B & (~A | ~B), {A: True}, deep=True) is False
222
+ assert pl_true((C >> A) >> (B >> A), {C: True}, deep=True) is True
223
+
224
+
225
+ def test_pl_true_wrong_input():
226
+ from sympy.core.numbers import pi
227
+ raises(ValueError, lambda: pl_true('John Cleese'))
228
+ raises(ValueError, lambda: pl_true(42 + pi + pi ** 2))
229
+ raises(ValueError, lambda: pl_true(42))
230
+
231
+
232
+ def test_entails():
233
+ A, B, C = symbols('A, B, C')
234
+ assert entails(A, [A >> B, ~B]) is False
235
+ assert entails(B, [Equivalent(A, B), A]) is True
236
+ assert entails((A >> B) >> (~A >> ~B)) is False
237
+ assert entails((A >> B) >> (~B >> ~A)) is True
238
+
239
+
240
+ def test_PropKB():
241
+ A, B, C = symbols('A,B,C')
242
+ kb = PropKB()
243
+ assert kb.ask(A >> B) is False
244
+ assert kb.ask(A >> (B >> A)) is True
245
+ kb.tell(A >> B)
246
+ kb.tell(B >> C)
247
+ assert kb.ask(A) is False
248
+ assert kb.ask(B) is False
249
+ assert kb.ask(C) is False
250
+ assert kb.ask(~A) is False
251
+ assert kb.ask(~B) is False
252
+ assert kb.ask(~C) is False
253
+ assert kb.ask(A >> C) is True
254
+ kb.tell(A)
255
+ assert kb.ask(A) is True
256
+ assert kb.ask(B) is True
257
+ assert kb.ask(C) is True
258
+ assert kb.ask(~C) is False
259
+ kb.retract(A)
260
+ assert kb.ask(C) is False
261
+
262
+
263
+ def test_propKB_tolerant():
264
+ """"tolerant to bad input"""
265
+ kb = PropKB()
266
+ A, B, C = symbols('A,B,C')
267
+ assert kb.ask(B) is False
268
+
269
+ def test_satisfiable_non_symbols():
270
+ x, y = symbols('x y')
271
+ assumptions = Q.zero(x*y)
272
+ facts = Implies(Q.zero(x*y), Q.zero(x) | Q.zero(y))
273
+ query = ~Q.zero(x) & ~Q.zero(y)
274
+ refutations = [
275
+ {Q.zero(x): True, Q.zero(x*y): True},
276
+ {Q.zero(y): True, Q.zero(x*y): True},
277
+ {Q.zero(x): True, Q.zero(y): True, Q.zero(x*y): True},
278
+ {Q.zero(x): True, Q.zero(y): False, Q.zero(x*y): True},
279
+ {Q.zero(x): False, Q.zero(y): True, Q.zero(x*y): True}]
280
+ assert not satisfiable(And(assumptions, facts, query), algorithm='dpll')
281
+ assert satisfiable(And(assumptions, facts, ~query), algorithm='dpll') in refutations
282
+ assert not satisfiable(And(assumptions, facts, query), algorithm='dpll2')
283
+ assert satisfiable(And(assumptions, facts, ~query), algorithm='dpll2') in refutations
284
+
285
+ def test_satisfiable_bool():
286
+ from sympy.core.singleton import S
287
+ assert satisfiable(true) == {true: true}
288
+ assert satisfiable(S.true) == {true: true}
289
+ assert satisfiable(false) is False
290
+ assert satisfiable(S.false) is False
291
+
292
+
293
+ def test_satisfiable_all_models():
294
+ from sympy.abc import A, B
295
+ assert next(satisfiable(False, all_models=True)) is False
296
+ assert list(satisfiable((A >> ~A) & A, all_models=True)) == [False]
297
+ assert list(satisfiable(True, all_models=True)) == [{true: true}]
298
+
299
+ models = [{A: True, B: False}, {A: False, B: True}]
300
+ result = satisfiable(A ^ B, all_models=True)
301
+ models.remove(next(result))
302
+ models.remove(next(result))
303
+ raises(StopIteration, lambda: next(result))
304
+ assert not models
305
+
306
+ assert list(satisfiable(Equivalent(A, B), all_models=True)) == \
307
+ [{A: False, B: False}, {A: True, B: True}]
308
+
309
+ models = [{A: False, B: False}, {A: False, B: True}, {A: True, B: True}]
310
+ for model in satisfiable(A >> B, all_models=True):
311
+ models.remove(model)
312
+ assert not models
313
+
314
+ # This is a santiy test to check that only the required number
315
+ # of solutions are generated. The expr below has 2**100 - 1 models
316
+ # which would time out the test if all are generated at once.
317
+ from sympy.utilities.iterables import numbered_symbols
318
+ from sympy.logic.boolalg import Or
319
+ sym = numbered_symbols()
320
+ X = [next(sym) for i in range(100)]
321
+ result = satisfiable(Or(*X), all_models=True)
322
+ for i in range(10):
323
+ assert next(result)
324
+
325
+
326
+ def test_z3():
327
+ z3 = import_module("z3")
328
+
329
+ if not z3:
330
+ skip("z3 not installed.")
331
+ A, B, C = symbols('A,B,C')
332
+ x, y, z = symbols('x,y,z')
333
+ assert z3_satisfiable((x >= 2) & (x < 1)) is False
334
+ assert z3_satisfiable( A & ~A ) is False
335
+
336
+ model = z3_satisfiable(A & (~A | B | C))
337
+ assert bool(model) is True
338
+ assert model[A] is True
339
+
340
+ # test nonlinear function
341
+ assert z3_satisfiable((x ** 2 >= 2) & (x < 1) & (x > -1)) is False
342
+
343
+
344
+ def test_z3_vs_lra_dpll2():
345
+ z3 = import_module("z3")
346
+ if z3 is None:
347
+ skip("z3 not installed.")
348
+
349
+ def boolean_formula_to_encoded_cnf(bf):
350
+ cnf = CNF.from_prop(bf)
351
+ enc = EncodedCNF()
352
+ enc.from_cnf(cnf)
353
+ return enc
354
+
355
+ def make_random_cnf(num_clauses=5, num_constraints=10, num_var=2):
356
+ assert num_clauses <= num_constraints
357
+ constraints = make_random_problem(num_variables=num_var, num_constraints=num_constraints, rational=False)
358
+ clauses = [[cons] for cons in constraints[:num_clauses]]
359
+ for cons in constraints[num_clauses:]:
360
+ if isinstance(cons, Unequality):
361
+ cons = ~cons
362
+ i = randint(0, num_clauses-1)
363
+ clauses[i].append(cons)
364
+
365
+ clauses = [Or(*clause) for clause in clauses]
366
+ cnf = And(*clauses)
367
+ return boolean_formula_to_encoded_cnf(cnf)
368
+
369
+ lra_dpll2_satisfiable = lambda x: dpll2_satisfiable(x, use_lra_theory=True)
370
+
371
+ for _ in range(50):
372
+ cnf = make_random_cnf(num_clauses=10, num_constraints=15, num_var=2)
373
+
374
+ try:
375
+ z3_sat = z3_satisfiable(cnf)
376
+ except z3.z3types.Z3Exception:
377
+ continue
378
+
379
+ lra_dpll2_sat = lra_dpll2_satisfiable(cnf) is not False
380
+
381
+ assert z3_sat == lra_dpll2_sat
wemm/lib/python3.10/site-packages/sympy/logic/tests/test_lra_theory.py ADDED
@@ -0,0 +1,440 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.numbers import Rational, I, oo
2
+ from sympy.core.relational import Eq
3
+ from sympy.core.symbol import symbols
4
+ from sympy.core.singleton import S
5
+ from sympy.matrices.dense import Matrix
6
+ from sympy.matrices.dense import randMatrix
7
+ from sympy.assumptions.ask import Q
8
+ from sympy.logic.boolalg import And
9
+ from sympy.abc import x, y, z
10
+ from sympy.assumptions.cnf import CNF, EncodedCNF
11
+ from sympy.functions.elementary.trigonometric import cos
12
+ from sympy.external import import_module
13
+
14
+ from sympy.logic.algorithms.lra_theory import LRASolver, UnhandledInput, LRARational, HANDLE_NEGATION
15
+ from sympy.core.random import random, choice, randint
16
+ from sympy.core.sympify import sympify
17
+ from sympy.ntheory.generate import randprime
18
+ from sympy.core.relational import StrictLessThan, StrictGreaterThan
19
+ import itertools
20
+
21
+ from sympy.testing.pytest import raises, XFAIL, skip
22
+
23
+ def make_random_problem(num_variables=2, num_constraints=2, sparsity=.1, rational=True,
24
+ disable_strict = False, disable_nonstrict=False, disable_equality=False):
25
+ def rand(sparsity=sparsity):
26
+ if random() < sparsity:
27
+ return sympify(0)
28
+ if rational:
29
+ int1, int2 = [randprime(0, 50) for _ in range(2)]
30
+ return Rational(int1, int2) * choice([-1, 1])
31
+ else:
32
+ return randint(1, 10) * choice([-1, 1])
33
+
34
+ variables = symbols('x1:%s' % (num_variables + 1))
35
+ constraints = []
36
+ for _ in range(num_constraints):
37
+ lhs, rhs = sum(rand() * x for x in variables), rand(sparsity=0) # sparsity=0 bc of bug with smtlib_code
38
+ options = []
39
+ if not disable_equality:
40
+ options += [Eq(lhs, rhs)]
41
+ if not disable_nonstrict:
42
+ options += [lhs <= rhs, lhs >= rhs]
43
+ if not disable_strict:
44
+ options += [lhs < rhs, lhs > rhs]
45
+
46
+ constraints.append(choice(options))
47
+
48
+ return constraints
49
+
50
+ def check_if_satisfiable_with_z3(constraints):
51
+ from sympy.external.importtools import import_module
52
+ from sympy.printing.smtlib import smtlib_code
53
+ from sympy.logic.boolalg import And
54
+ boolean_formula = And(*constraints)
55
+ z3 = import_module("z3")
56
+ if z3:
57
+ smtlib_string = smtlib_code(boolean_formula)
58
+ s = z3.Solver()
59
+ s.from_string(smtlib_string)
60
+ res = str(s.check())
61
+ if res == 'sat':
62
+ return True
63
+ elif res == 'unsat':
64
+ return False
65
+ else:
66
+ raise ValueError(f"z3 was not able to check the satisfiability of {boolean_formula}")
67
+
68
+ def find_rational_assignment(constr, assignment, iter=20):
69
+ eps = sympify(1)
70
+
71
+ for _ in range(iter):
72
+ assign = {key: val[0] + val[1]*eps for key, val in assignment.items()}
73
+ try:
74
+ for cons in constr:
75
+ assert cons.subs(assign) == True
76
+ return assign
77
+ except AssertionError:
78
+ eps = eps/2
79
+
80
+ return None
81
+
82
+ def boolean_formula_to_encoded_cnf(bf):
83
+ cnf = CNF.from_prop(bf)
84
+ enc = EncodedCNF()
85
+ enc.from_cnf(cnf)
86
+ return enc
87
+
88
+
89
+ def test_from_encoded_cnf():
90
+ s1, s2 = symbols("s1 s2")
91
+
92
+ # Test preprocessing
93
+ # Example is from section 3 of paper.
94
+ phi = (x >= 0) & ((x + y <= 2) | (x + 2 * y - z >= 6)) & (Eq(x + y, 2) | (x + 2 * y - z > 4))
95
+ enc = boolean_formula_to_encoded_cnf(phi)
96
+ lra, _ = LRASolver.from_encoded_cnf(enc, testing_mode=True)
97
+ assert lra.A.shape == (2, 5)
98
+ assert str(lra.slack) == '[_s1, _s2]'
99
+ assert str(lra.nonslack) == '[x, y, z]'
100
+ assert lra.A == Matrix([[ 1, 1, 0, -1, 0],
101
+ [-1, -2, 1, 0, -1]])
102
+ assert {(str(b.var), b.bound, b.upper, b.equality, b.strict) for b in lra.enc_to_boundary.values()} == {('_s1', 2, None, True, False),
103
+ ('_s1', 2, True, False, False),
104
+ ('_s2', -4, True, False, True),
105
+ ('_s2', -6, True, False, False),
106
+ ('x', 0, False, False, False)}
107
+
108
+
109
+ def test_problem():
110
+ from sympy.logic.algorithms.lra_theory import LRASolver
111
+ from sympy.assumptions.cnf import CNF, EncodedCNF
112
+ cons = [-2 * x - 2 * y >= 7, -9 * y >= 7, -6 * y >= 5]
113
+ cnf = CNF().from_prop(And(*cons))
114
+ enc = EncodedCNF()
115
+ enc.from_cnf(cnf)
116
+ lra, _ = LRASolver.from_encoded_cnf(enc)
117
+ lra.assert_lit(1)
118
+ lra.assert_lit(2)
119
+ lra.assert_lit(3)
120
+ is_sat, assignment = lra.check()
121
+ assert is_sat is True
122
+
123
+
124
+ def test_random_problems():
125
+ z3 = import_module("z3")
126
+ if z3 is None:
127
+ skip("z3 is not installed")
128
+
129
+ special_cases = []; x1, x2, x3 = symbols("x1 x2 x3")
130
+ special_cases.append([x1 - 3 * x2 <= -5, 6 * x1 + 4 * x2 <= 0, -7 * x1 + 3 * x2 <= 3])
131
+ special_cases.append([-3 * x1 >= 3, Eq(4 * x1, -1)])
132
+ special_cases.append([-4 * x1 < 4, 6 * x1 <= -6])
133
+ special_cases.append([-3 * x2 >= 7, 6 * x1 <= -5, -3 * x2 <= -4])
134
+ special_cases.append([x + y >= 2, x + y <= 1])
135
+ special_cases.append([x >= 0, x + y <= 2, x + 2 * y - z >= 6]) # from paper example
136
+ special_cases.append([-2 * x1 - 2 * x2 >= 7, -9 * x1 >= 7, -6 * x1 >= 5])
137
+ special_cases.append([2 * x1 > -3, -9 * x1 < -6, 9 * x1 <= 6])
138
+ special_cases.append([-2*x1 < -4, 9*x1 > -9])
139
+ special_cases.append([-6*x1 >= -1, -8*x1 + x2 >= 5, -8*x1 + 7*x2 < 4, x1 > 7])
140
+ special_cases.append([Eq(x1, 2), Eq(5*x1, -2), Eq(-7*x2, -6), Eq(9*x1 + 10*x2, 9)])
141
+ special_cases.append([Eq(3*x1, 6), Eq(x1 - 8*x2, -9), Eq(-7*x1 + 5*x2, 3), Eq(3*x2, 7)])
142
+ special_cases.append([-4*x1 < 4, 6*x1 <= -6])
143
+ special_cases.append([-3*x1 + 8*x2 >= -8, -10*x2 > 9, 8*x1 - 4*x2 < 8, 10*x1 - 9*x2 >= -9])
144
+ special_cases.append([x1 + 5*x2 >= -6, 9*x1 - 3*x2 >= -9, 6*x1 + 6*x2 < -10, -3*x1 + 3*x2 < -7])
145
+ special_cases.append([-9*x1 < 7, -5*x1 - 7*x2 < -1, 3*x1 + 7*x2 > 1, -6*x1 - 6*x2 > 9])
146
+ special_cases.append([9*x1 - 6*x2 >= -7, 9*x1 + 4*x2 < -8, -7*x2 <= 1, 10*x2 <= -7])
147
+
148
+ feasible_count = 0
149
+ for i in range(50):
150
+ if i % 8 == 0:
151
+ constraints = make_random_problem(num_variables=1, num_constraints=2, rational=False)
152
+ elif i % 8 == 1:
153
+ constraints = make_random_problem(num_variables=2, num_constraints=4, rational=False, disable_equality=True,
154
+ disable_nonstrict=True)
155
+ elif i % 8 == 2:
156
+ constraints = make_random_problem(num_variables=2, num_constraints=4, rational=False, disable_strict=True)
157
+ elif i % 8 == 3:
158
+ constraints = make_random_problem(num_variables=3, num_constraints=12, rational=False)
159
+ else:
160
+ constraints = make_random_problem(num_variables=3, num_constraints=6, rational=False)
161
+
162
+ if i < len(special_cases):
163
+ constraints = special_cases[i]
164
+
165
+ if False in constraints or True in constraints:
166
+ continue
167
+
168
+ phi = And(*constraints)
169
+ if phi == False:
170
+ continue
171
+ cnf = CNF.from_prop(phi); enc = EncodedCNF()
172
+ enc.from_cnf(cnf)
173
+ assert all(0 not in clause for clause in enc.data)
174
+
175
+ lra, _ = LRASolver.from_encoded_cnf(enc, testing_mode=True)
176
+ s_subs = lra.s_subs
177
+
178
+ lra.run_checks = True
179
+ s_subs_rev = {value: key for key, value in s_subs.items()}
180
+ lits = {lit for clause in enc.data for lit in clause}
181
+
182
+ bounds = [(lra.enc_to_boundary[l], l) for l in lits if l in lra.enc_to_boundary]
183
+ bounds = sorted(bounds, key=lambda x: (str(x[0].var), x[0].bound, str(x[0].upper))) # to remove nondeterminism
184
+
185
+ for b, l in bounds:
186
+ if lra.result and lra.result[0] == False:
187
+ break
188
+ lra.assert_lit(l)
189
+
190
+ feasible = lra.check()
191
+
192
+ if feasible[0] == True:
193
+ feasible_count += 1
194
+ assert check_if_satisfiable_with_z3(constraints) is True
195
+ cons_funcs = [cons.func for cons in constraints]
196
+ assignment = feasible[1]
197
+ assignment = {key.var : value for key, value in assignment.items()}
198
+ if not (StrictLessThan in cons_funcs or StrictGreaterThan in cons_funcs):
199
+ assignment = {key: value[0] for key, value in assignment.items()}
200
+ for cons in constraints:
201
+ assert cons.subs(assignment) == True
202
+
203
+ else:
204
+ rat_assignment = find_rational_assignment(constraints, assignment)
205
+ assert rat_assignment is not None
206
+ else:
207
+ assert check_if_satisfiable_with_z3(constraints) is False
208
+
209
+ conflict = feasible[1]
210
+ assert len(conflict) >= 2
211
+ conflict = {lra.enc_to_boundary[-l].get_inequality() for l in conflict}
212
+ conflict = {clause.subs(s_subs_rev) for clause in conflict}
213
+ assert check_if_satisfiable_with_z3(conflict) is False
214
+
215
+ # check that conflict clause is probably minimal
216
+ for subset in itertools.combinations(conflict, len(conflict)-1):
217
+ assert check_if_satisfiable_with_z3(subset) is True
218
+
219
+
220
+ @XFAIL
221
+ def test_pos_neg_zero():
222
+ bf = Q.positive(x) & Q.negative(x) & Q.zero(y)
223
+ enc = boolean_formula_to_encoded_cnf(bf)
224
+ lra, _ = LRASolver.from_encoded_cnf(enc, testing_mode=True)
225
+ for lit in enc.encoding.values():
226
+ if lra.assert_lit(lit) is not None:
227
+ break
228
+ assert len(lra.enc_to_boundary) == 3
229
+ assert lra.check()[0] == False
230
+
231
+ bf = Q.positive(x) & Q.lt(x, -1)
232
+ enc = boolean_formula_to_encoded_cnf(bf)
233
+ lra, _ = LRASolver.from_encoded_cnf(enc, testing_mode=True)
234
+ for lit in enc.encoding.values():
235
+ if lra.assert_lit(lit) is not None:
236
+ break
237
+ assert len(lra.enc_to_boundary) == 2
238
+ assert lra.check()[0] == False
239
+
240
+ bf = Q.positive(x) & Q.zero(x)
241
+ enc = boolean_formula_to_encoded_cnf(bf)
242
+ lra, _ = LRASolver.from_encoded_cnf(enc, testing_mode=True)
243
+ for lit in enc.encoding.values():
244
+ if lra.assert_lit(lit) is not None:
245
+ break
246
+ assert len(lra.enc_to_boundary) == 2
247
+ assert lra.check()[0] == False
248
+
249
+ bf = Q.positive(x) & Q.zero(y)
250
+ enc = boolean_formula_to_encoded_cnf(bf)
251
+ lra, _ = LRASolver.from_encoded_cnf(enc, testing_mode=True)
252
+ for lit in enc.encoding.values():
253
+ if lra.assert_lit(lit) is not None:
254
+ break
255
+ assert len(lra.enc_to_boundary) == 2
256
+ assert lra.check()[0] == True
257
+
258
+
259
+ @XFAIL
260
+ def test_pos_neg_infinite():
261
+ bf = Q.positive_infinite(x) & Q.lt(x, 10000000) & Q.positive_infinite(y)
262
+ enc = boolean_formula_to_encoded_cnf(bf)
263
+ lra, _ = LRASolver.from_encoded_cnf(enc, testing_mode=True)
264
+ for lit in enc.encoding.values():
265
+ if lra.assert_lit(lit) is not None:
266
+ break
267
+ assert len(lra.enc_to_boundary) == 3
268
+ assert lra.check()[0] == False
269
+
270
+ bf = Q.positive_infinite(x) & Q.gt(x, 10000000) & Q.positive_infinite(y)
271
+ enc = boolean_formula_to_encoded_cnf(bf)
272
+ lra, _ = LRASolver.from_encoded_cnf(enc, testing_mode=True)
273
+ for lit in enc.encoding.values():
274
+ if lra.assert_lit(lit) is not None:
275
+ break
276
+ assert len(lra.enc_to_boundary) == 3
277
+ assert lra.check()[0] == True
278
+
279
+ bf = Q.positive_infinite(x) & Q.negative_infinite(x)
280
+ enc = boolean_formula_to_encoded_cnf(bf)
281
+ lra, _ = LRASolver.from_encoded_cnf(enc, testing_mode=True)
282
+ for lit in enc.encoding.values():
283
+ if lra.assert_lit(lit) is not None:
284
+ break
285
+ assert len(lra.enc_to_boundary) == 2
286
+ assert lra.check()[0] == False
287
+
288
+
289
+ def test_binrel_evaluation():
290
+ bf = Q.gt(3, 2)
291
+ enc = boolean_formula_to_encoded_cnf(bf)
292
+ lra, conflicts = LRASolver.from_encoded_cnf(enc, testing_mode=True)
293
+ assert len(lra.enc_to_boundary) == 0
294
+ assert conflicts == [[1]]
295
+
296
+ bf = Q.lt(3, 2)
297
+ enc = boolean_formula_to_encoded_cnf(bf)
298
+ lra, conflicts = LRASolver.from_encoded_cnf(enc, testing_mode=True)
299
+ assert len(lra.enc_to_boundary) == 0
300
+ assert conflicts == [[-1]]
301
+
302
+
303
+ def test_negation():
304
+ assert HANDLE_NEGATION is True
305
+ bf = Q.gt(x, 1) & ~Q.gt(x, 0)
306
+ enc = boolean_formula_to_encoded_cnf(bf)
307
+ lra, _ = LRASolver.from_encoded_cnf(enc, testing_mode=True)
308
+ for clause in enc.data:
309
+ for lit in clause:
310
+ lra.assert_lit(lit)
311
+ assert len(lra.enc_to_boundary) == 2
312
+ assert lra.check()[0] == False
313
+ assert sorted(lra.check()[1]) in [[-1, 2], [-2, 1]]
314
+
315
+ bf = ~Q.gt(x, 1) & ~Q.lt(x, 0)
316
+ enc = boolean_formula_to_encoded_cnf(bf)
317
+ lra, _ = LRASolver.from_encoded_cnf(enc, testing_mode=True)
318
+ for clause in enc.data:
319
+ for lit in clause:
320
+ lra.assert_lit(lit)
321
+ assert len(lra.enc_to_boundary) == 2
322
+ assert lra.check()[0] == True
323
+
324
+ bf = ~Q.gt(x, 0) & ~Q.lt(x, 1)
325
+ enc = boolean_formula_to_encoded_cnf(bf)
326
+ lra, _ = LRASolver.from_encoded_cnf(enc, testing_mode=True)
327
+ for clause in enc.data:
328
+ for lit in clause:
329
+ lra.assert_lit(lit)
330
+ assert len(lra.enc_to_boundary) == 2
331
+ assert lra.check()[0] == False
332
+
333
+ bf = ~Q.gt(x, 0) & ~Q.le(x, 0)
334
+ enc = boolean_formula_to_encoded_cnf(bf)
335
+ lra, _ = LRASolver.from_encoded_cnf(enc, testing_mode=True)
336
+ for clause in enc.data:
337
+ for lit in clause:
338
+ lra.assert_lit(lit)
339
+ assert len(lra.enc_to_boundary) == 2
340
+ assert lra.check()[0] == False
341
+
342
+ bf = ~Q.le(x+y, 2) & ~Q.ge(x-y, 2) & ~Q.ge(y, 0)
343
+ enc = boolean_formula_to_encoded_cnf(bf)
344
+ lra, _ = LRASolver.from_encoded_cnf(enc, testing_mode=True)
345
+ for clause in enc.data:
346
+ for lit in clause:
347
+ lra.assert_lit(lit)
348
+ assert len(lra.enc_to_boundary) == 3
349
+ assert lra.check()[0] == False
350
+ assert len(lra.check()[1]) == 3
351
+ assert all(i > 0 for i in lra.check()[1])
352
+
353
+
354
+ def test_unhandled_input():
355
+ nan = S.NaN
356
+ bf = Q.gt(3, nan) & Q.gt(x, nan)
357
+ enc = boolean_formula_to_encoded_cnf(bf)
358
+ raises(ValueError, lambda: LRASolver.from_encoded_cnf(enc, testing_mode=True))
359
+
360
+ bf = Q.gt(3, I) & Q.gt(x, I)
361
+ enc = boolean_formula_to_encoded_cnf(bf)
362
+ raises(UnhandledInput, lambda: LRASolver.from_encoded_cnf(enc, testing_mode=True))
363
+
364
+ bf = Q.gt(3, float("inf")) & Q.gt(x, float("inf"))
365
+ enc = boolean_formula_to_encoded_cnf(bf)
366
+ raises(UnhandledInput, lambda: LRASolver.from_encoded_cnf(enc, testing_mode=True))
367
+
368
+ bf = Q.gt(3, oo) & Q.gt(x, oo)
369
+ enc = boolean_formula_to_encoded_cnf(bf)
370
+ raises(UnhandledInput, lambda: LRASolver.from_encoded_cnf(enc, testing_mode=True))
371
+
372
+ # test non-linearity
373
+ bf = Q.gt(x**2 + x, 2)
374
+ enc = boolean_formula_to_encoded_cnf(bf)
375
+ raises(UnhandledInput, lambda: LRASolver.from_encoded_cnf(enc, testing_mode=True))
376
+
377
+ bf = Q.gt(cos(x) + x, 2)
378
+ enc = boolean_formula_to_encoded_cnf(bf)
379
+ raises(UnhandledInput, lambda: LRASolver.from_encoded_cnf(enc, testing_mode=True))
380
+
381
+ @XFAIL
382
+ def test_infinite_strict_inequalities():
383
+ # Extensive testing of the interaction between strict inequalities
384
+ # and constraints containing infinity is needed because
385
+ # the paper's rule for strict inequalities don't work when
386
+ # infinite numbers are allowed. Using the paper's rules you
387
+ # can end up with situations where oo + delta > oo is considered
388
+ # True when oo + delta should be equal to oo.
389
+ # See https://math.stackexchange.com/questions/4757069/can-this-method-of-converting-strict-inequalities-to-equisatisfiable-nonstrict-i
390
+ bf = (-x - y >= -float("inf")) & (x > 0) & (y >= float("inf"))
391
+ enc = boolean_formula_to_encoded_cnf(bf)
392
+ lra, _ = LRASolver.from_encoded_cnf(enc, testing_mode=True)
393
+ for lit in sorted(enc.encoding.values()):
394
+ if lra.assert_lit(lit) is not None:
395
+ break
396
+ assert len(lra.enc_to_boundary) == 3
397
+ assert lra.check()[0] == True
398
+
399
+
400
+ def test_pivot():
401
+ for _ in range(10):
402
+ m = randMatrix(5)
403
+ rref = m.rref()
404
+ for _ in range(5):
405
+ i, j = randint(0, 4), randint(0, 4)
406
+ if m[i, j] != 0:
407
+ assert LRASolver._pivot(m, i, j).rref() == rref
408
+
409
+
410
+ def test_reset_bounds():
411
+ bf = Q.ge(x, 1) & Q.lt(x, 1)
412
+ enc = boolean_formula_to_encoded_cnf(bf)
413
+ lra, _ = LRASolver.from_encoded_cnf(enc, testing_mode=True)
414
+ for clause in enc.data:
415
+ for lit in clause:
416
+ lra.assert_lit(lit)
417
+ assert len(lra.enc_to_boundary) == 2
418
+ assert lra.check()[0] == False
419
+
420
+ lra.reset_bounds()
421
+ assert lra.check()[0] == True
422
+ for var in lra.all_var:
423
+ assert var.upper == LRARational(float("inf"), 0)
424
+ assert var.upper_from_eq == False
425
+ assert var.upper_from_neg == False
426
+ assert var.lower == LRARational(-float("inf"), 0)
427
+ assert var.lower_from_eq == False
428
+ assert var.lower_from_neg == False
429
+ assert var.assign == LRARational(0, 0)
430
+ assert var.var is not None
431
+ assert var.col_idx is not None
432
+
433
+
434
+ def test_empty_cnf():
435
+ cnf = CNF()
436
+ enc = EncodedCNF()
437
+ enc.from_cnf(cnf)
438
+ lra, conflict = LRASolver.from_encoded_cnf(enc)
439
+ assert len(conflict) == 0
440
+ assert lra.check() == (True, {})
wemm/lib/python3.10/site-packages/sympy/logic/utilities/__init__.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ from .dimacs import load_file
2
+
3
+ __all__ = ['load_file']
wemm/lib/python3.10/site-packages/sympy/logic/utilities/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (233 Bytes). View file
 
wemm/lib/python3.10/site-packages/sympy/logic/utilities/__pycache__/dimacs.cpython-310.pyc ADDED
Binary file (1.53 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/logic/utilities/dimacs.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """For reading in DIMACS file format
2
+
3
+ www.cs.ubc.ca/~hoos/SATLIB/Benchmarks/SAT/satformat.ps
4
+
5
+ """
6
+
7
+ from sympy.core import Symbol
8
+ from sympy.logic.boolalg import And, Or
9
+ import re
10
+
11
+
12
+ def load(s):
13
+ """Loads a boolean expression from a string.
14
+
15
+ Examples
16
+ ========
17
+
18
+ >>> from sympy.logic.utilities.dimacs import load
19
+ >>> load('1')
20
+ cnf_1
21
+ >>> load('1 2')
22
+ cnf_1 | cnf_2
23
+ >>> load('1 \\n 2')
24
+ cnf_1 & cnf_2
25
+ >>> load('1 2 \\n 3')
26
+ cnf_3 & (cnf_1 | cnf_2)
27
+ """
28
+ clauses = []
29
+
30
+ lines = s.split('\n')
31
+
32
+ pComment = re.compile(r'c.*')
33
+ pStats = re.compile(r'p\s*cnf\s*(\d*)\s*(\d*)')
34
+
35
+ while len(lines) > 0:
36
+ line = lines.pop(0)
37
+
38
+ # Only deal with lines that aren't comments
39
+ if not pComment.match(line):
40
+ m = pStats.match(line)
41
+
42
+ if not m:
43
+ nums = line.rstrip('\n').split(' ')
44
+ list = []
45
+ for lit in nums:
46
+ if lit != '':
47
+ if int(lit) == 0:
48
+ continue
49
+ num = abs(int(lit))
50
+ sign = True
51
+ if int(lit) < 0:
52
+ sign = False
53
+
54
+ if sign:
55
+ list.append(Symbol("cnf_%s" % num))
56
+ else:
57
+ list.append(~Symbol("cnf_%s" % num))
58
+
59
+ if len(list) > 0:
60
+ clauses.append(Or(*list))
61
+
62
+ return And(*clauses)
63
+
64
+
65
+ def load_file(location):
66
+ """Loads a boolean expression from a file."""
67
+ with open(location) as f:
68
+ s = f.read()
69
+
70
+ return load(s)
wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/__pycache__/interval_arithmetic.cpython-310.pyc ADDED
Binary file (9.24 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/__pycache__/interval_membership.cpython-310.pyc ADDED
Binary file (3.05 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/__pycache__/lib_interval.cpython-310.pyc ADDED
Binary file (9.58 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/lib_interval.py ADDED
@@ -0,0 +1,452 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ The module contains implemented functions for interval arithmetic."""
2
+ from functools import reduce
3
+
4
+ from sympy.plotting.intervalmath import interval
5
+ from sympy.external import import_module
6
+
7
+
8
+ def Abs(x):
9
+ if isinstance(x, (int, float)):
10
+ return interval(abs(x))
11
+ elif isinstance(x, interval):
12
+ if x.start < 0 and x.end > 0:
13
+ return interval(0, max(abs(x.start), abs(x.end)), is_valid=x.is_valid)
14
+ else:
15
+ return interval(abs(x.start), abs(x.end))
16
+ else:
17
+ raise NotImplementedError
18
+
19
+ #Monotonic
20
+
21
+
22
+ def exp(x):
23
+ """evaluates the exponential of an interval"""
24
+ np = import_module('numpy')
25
+ if isinstance(x, (int, float)):
26
+ return interval(np.exp(x), np.exp(x))
27
+ elif isinstance(x, interval):
28
+ return interval(np.exp(x.start), np.exp(x.end), is_valid=x.is_valid)
29
+ else:
30
+ raise NotImplementedError
31
+
32
+
33
+ #Monotonic
34
+ def log(x):
35
+ """evaluates the natural logarithm of an interval"""
36
+ np = import_module('numpy')
37
+ if isinstance(x, (int, float)):
38
+ if x <= 0:
39
+ return interval(-np.inf, np.inf, is_valid=False)
40
+ else:
41
+ return interval(np.log(x))
42
+ elif isinstance(x, interval):
43
+ if not x.is_valid:
44
+ return interval(-np.inf, np.inf, is_valid=x.is_valid)
45
+ elif x.end <= 0:
46
+ return interval(-np.inf, np.inf, is_valid=False)
47
+ elif x.start <= 0:
48
+ return interval(-np.inf, np.inf, is_valid=None)
49
+
50
+ return interval(np.log(x.start), np.log(x.end))
51
+ else:
52
+ raise NotImplementedError
53
+
54
+
55
+ #Monotonic
56
+ def log10(x):
57
+ """evaluates the logarithm to the base 10 of an interval"""
58
+ np = import_module('numpy')
59
+ if isinstance(x, (int, float)):
60
+ if x <= 0:
61
+ return interval(-np.inf, np.inf, is_valid=False)
62
+ else:
63
+ return interval(np.log10(x))
64
+ elif isinstance(x, interval):
65
+ if not x.is_valid:
66
+ return interval(-np.inf, np.inf, is_valid=x.is_valid)
67
+ elif x.end <= 0:
68
+ return interval(-np.inf, np.inf, is_valid=False)
69
+ elif x.start <= 0:
70
+ return interval(-np.inf, np.inf, is_valid=None)
71
+ return interval(np.log10(x.start), np.log10(x.end))
72
+ else:
73
+ raise NotImplementedError
74
+
75
+
76
+ #Monotonic
77
+ def atan(x):
78
+ """evaluates the tan inverse of an interval"""
79
+ np = import_module('numpy')
80
+ if isinstance(x, (int, float)):
81
+ return interval(np.arctan(x))
82
+ elif isinstance(x, interval):
83
+ start = np.arctan(x.start)
84
+ end = np.arctan(x.end)
85
+ return interval(start, end, is_valid=x.is_valid)
86
+ else:
87
+ raise NotImplementedError
88
+
89
+
90
+ #periodic
91
+ def sin(x):
92
+ """evaluates the sine of an interval"""
93
+ np = import_module('numpy')
94
+ if isinstance(x, (int, float)):
95
+ return interval(np.sin(x))
96
+ elif isinstance(x, interval):
97
+ if not x.is_valid:
98
+ return interval(-1, 1, is_valid=x.is_valid)
99
+ na, __ = divmod(x.start, np.pi / 2.0)
100
+ nb, __ = divmod(x.end, np.pi / 2.0)
101
+ start = min(np.sin(x.start), np.sin(x.end))
102
+ end = max(np.sin(x.start), np.sin(x.end))
103
+ if nb - na > 4:
104
+ return interval(-1, 1, is_valid=x.is_valid)
105
+ elif na == nb:
106
+ return interval(start, end, is_valid=x.is_valid)
107
+ else:
108
+ if (na - 1) // 4 != (nb - 1) // 4:
109
+ #sin has max
110
+ end = 1
111
+ if (na - 3) // 4 != (nb - 3) // 4:
112
+ #sin has min
113
+ start = -1
114
+ return interval(start, end)
115
+ else:
116
+ raise NotImplementedError
117
+
118
+
119
+ #periodic
120
+ def cos(x):
121
+ """Evaluates the cos of an interval"""
122
+ np = import_module('numpy')
123
+ if isinstance(x, (int, float)):
124
+ return interval(np.sin(x))
125
+ elif isinstance(x, interval):
126
+ if not (np.isfinite(x.start) and np.isfinite(x.end)):
127
+ return interval(-1, 1, is_valid=x.is_valid)
128
+ na, __ = divmod(x.start, np.pi / 2.0)
129
+ nb, __ = divmod(x.end, np.pi / 2.0)
130
+ start = min(np.cos(x.start), np.cos(x.end))
131
+ end = max(np.cos(x.start), np.cos(x.end))
132
+ if nb - na > 4:
133
+ #differ more than 2*pi
134
+ return interval(-1, 1, is_valid=x.is_valid)
135
+ elif na == nb:
136
+ #in the same quadarant
137
+ return interval(start, end, is_valid=x.is_valid)
138
+ else:
139
+ if (na) // 4 != (nb) // 4:
140
+ #cos has max
141
+ end = 1
142
+ if (na - 2) // 4 != (nb - 2) // 4:
143
+ #cos has min
144
+ start = -1
145
+ return interval(start, end, is_valid=x.is_valid)
146
+ else:
147
+ raise NotImplementedError
148
+
149
+
150
+ def tan(x):
151
+ """Evaluates the tan of an interval"""
152
+ return sin(x) / cos(x)
153
+
154
+
155
+ #Monotonic
156
+ def sqrt(x):
157
+ """Evaluates the square root of an interval"""
158
+ np = import_module('numpy')
159
+ if isinstance(x, (int, float)):
160
+ if x > 0:
161
+ return interval(np.sqrt(x))
162
+ else:
163
+ return interval(-np.inf, np.inf, is_valid=False)
164
+ elif isinstance(x, interval):
165
+ #Outside the domain
166
+ if x.end < 0:
167
+ return interval(-np.inf, np.inf, is_valid=False)
168
+ #Partially outside the domain
169
+ elif x.start < 0:
170
+ return interval(-np.inf, np.inf, is_valid=None)
171
+ else:
172
+ return interval(np.sqrt(x.start), np.sqrt(x.end),
173
+ is_valid=x.is_valid)
174
+ else:
175
+ raise NotImplementedError
176
+
177
+
178
+ def imin(*args):
179
+ """Evaluates the minimum of a list of intervals"""
180
+ np = import_module('numpy')
181
+ if not all(isinstance(arg, (int, float, interval)) for arg in args):
182
+ return NotImplementedError
183
+ else:
184
+ new_args = [a for a in args if isinstance(a, (int, float))
185
+ or a.is_valid]
186
+ if len(new_args) == 0:
187
+ if all(a.is_valid is False for a in args):
188
+ return interval(-np.inf, np.inf, is_valid=False)
189
+ else:
190
+ return interval(-np.inf, np.inf, is_valid=None)
191
+ start_array = [a if isinstance(a, (int, float)) else a.start
192
+ for a in new_args]
193
+
194
+ end_array = [a if isinstance(a, (int, float)) else a.end
195
+ for a in new_args]
196
+ return interval(min(start_array), min(end_array))
197
+
198
+
199
+ def imax(*args):
200
+ """Evaluates the maximum of a list of intervals"""
201
+ np = import_module('numpy')
202
+ if not all(isinstance(arg, (int, float, interval)) for arg in args):
203
+ return NotImplementedError
204
+ else:
205
+ new_args = [a for a in args if isinstance(a, (int, float))
206
+ or a.is_valid]
207
+ if len(new_args) == 0:
208
+ if all(a.is_valid is False for a in args):
209
+ return interval(-np.inf, np.inf, is_valid=False)
210
+ else:
211
+ return interval(-np.inf, np.inf, is_valid=None)
212
+ start_array = [a if isinstance(a, (int, float)) else a.start
213
+ for a in new_args]
214
+
215
+ end_array = [a if isinstance(a, (int, float)) else a.end
216
+ for a in new_args]
217
+
218
+ return interval(max(start_array), max(end_array))
219
+
220
+
221
+ #Monotonic
222
+ def sinh(x):
223
+ """Evaluates the hyperbolic sine of an interval"""
224
+ np = import_module('numpy')
225
+ if isinstance(x, (int, float)):
226
+ return interval(np.sinh(x), np.sinh(x))
227
+ elif isinstance(x, interval):
228
+ return interval(np.sinh(x.start), np.sinh(x.end), is_valid=x.is_valid)
229
+ else:
230
+ raise NotImplementedError
231
+
232
+
233
+ def cosh(x):
234
+ """Evaluates the hyperbolic cos of an interval"""
235
+ np = import_module('numpy')
236
+ if isinstance(x, (int, float)):
237
+ return interval(np.cosh(x), np.cosh(x))
238
+ elif isinstance(x, interval):
239
+ #both signs
240
+ if x.start < 0 and x.end > 0:
241
+ end = max(np.cosh(x.start), np.cosh(x.end))
242
+ return interval(1, end, is_valid=x.is_valid)
243
+ else:
244
+ #Monotonic
245
+ start = np.cosh(x.start)
246
+ end = np.cosh(x.end)
247
+ return interval(start, end, is_valid=x.is_valid)
248
+ else:
249
+ raise NotImplementedError
250
+
251
+
252
+ #Monotonic
253
+ def tanh(x):
254
+ """Evaluates the hyperbolic tan of an interval"""
255
+ np = import_module('numpy')
256
+ if isinstance(x, (int, float)):
257
+ return interval(np.tanh(x), np.tanh(x))
258
+ elif isinstance(x, interval):
259
+ return interval(np.tanh(x.start), np.tanh(x.end), is_valid=x.is_valid)
260
+ else:
261
+ raise NotImplementedError
262
+
263
+
264
+ def asin(x):
265
+ """Evaluates the inverse sine of an interval"""
266
+ np = import_module('numpy')
267
+ if isinstance(x, (int, float)):
268
+ #Outside the domain
269
+ if abs(x) > 1:
270
+ return interval(-np.inf, np.inf, is_valid=False)
271
+ else:
272
+ return interval(np.arcsin(x), np.arcsin(x))
273
+ elif isinstance(x, interval):
274
+ #Outside the domain
275
+ if x.is_valid is False or x.start > 1 or x.end < -1:
276
+ return interval(-np.inf, np.inf, is_valid=False)
277
+ #Partially outside the domain
278
+ elif x.start < -1 or x.end > 1:
279
+ return interval(-np.inf, np.inf, is_valid=None)
280
+ else:
281
+ start = np.arcsin(x.start)
282
+ end = np.arcsin(x.end)
283
+ return interval(start, end, is_valid=x.is_valid)
284
+
285
+
286
+ def acos(x):
287
+ """Evaluates the inverse cos of an interval"""
288
+ np = import_module('numpy')
289
+ if isinstance(x, (int, float)):
290
+ if abs(x) > 1:
291
+ #Outside the domain
292
+ return interval(-np.inf, np.inf, is_valid=False)
293
+ else:
294
+ return interval(np.arccos(x), np.arccos(x))
295
+ elif isinstance(x, interval):
296
+ #Outside the domain
297
+ if x.is_valid is False or x.start > 1 or x.end < -1:
298
+ return interval(-np.inf, np.inf, is_valid=False)
299
+ #Partially outside the domain
300
+ elif x.start < -1 or x.end > 1:
301
+ return interval(-np.inf, np.inf, is_valid=None)
302
+ else:
303
+ start = np.arccos(x.start)
304
+ end = np.arccos(x.end)
305
+ return interval(start, end, is_valid=x.is_valid)
306
+
307
+
308
+ def ceil(x):
309
+ """Evaluates the ceiling of an interval"""
310
+ np = import_module('numpy')
311
+ if isinstance(x, (int, float)):
312
+ return interval(np.ceil(x))
313
+ elif isinstance(x, interval):
314
+ if x.is_valid is False:
315
+ return interval(-np.inf, np.inf, is_valid=False)
316
+ else:
317
+ start = np.ceil(x.start)
318
+ end = np.ceil(x.end)
319
+ #Continuous over the interval
320
+ if start == end:
321
+ return interval(start, end, is_valid=x.is_valid)
322
+ else:
323
+ #Not continuous over the interval
324
+ return interval(start, end, is_valid=None)
325
+ else:
326
+ return NotImplementedError
327
+
328
+
329
+ def floor(x):
330
+ """Evaluates the floor of an interval"""
331
+ np = import_module('numpy')
332
+ if isinstance(x, (int, float)):
333
+ return interval(np.floor(x))
334
+ elif isinstance(x, interval):
335
+ if x.is_valid is False:
336
+ return interval(-np.inf, np.inf, is_valid=False)
337
+ else:
338
+ start = np.floor(x.start)
339
+ end = np.floor(x.end)
340
+ #continuous over the argument
341
+ if start == end:
342
+ return interval(start, end, is_valid=x.is_valid)
343
+ else:
344
+ #not continuous over the interval
345
+ return interval(start, end, is_valid=None)
346
+ else:
347
+ return NotImplementedError
348
+
349
+
350
+ def acosh(x):
351
+ """Evaluates the inverse hyperbolic cosine of an interval"""
352
+ np = import_module('numpy')
353
+ if isinstance(x, (int, float)):
354
+ #Outside the domain
355
+ if x < 1:
356
+ return interval(-np.inf, np.inf, is_valid=False)
357
+ else:
358
+ return interval(np.arccosh(x))
359
+ elif isinstance(x, interval):
360
+ #Outside the domain
361
+ if x.end < 1:
362
+ return interval(-np.inf, np.inf, is_valid=False)
363
+ #Partly outside the domain
364
+ elif x.start < 1:
365
+ return interval(-np.inf, np.inf, is_valid=None)
366
+ else:
367
+ start = np.arccosh(x.start)
368
+ end = np.arccosh(x.end)
369
+ return interval(start, end, is_valid=x.is_valid)
370
+ else:
371
+ return NotImplementedError
372
+
373
+
374
+ #Monotonic
375
+ def asinh(x):
376
+ """Evaluates the inverse hyperbolic sine of an interval"""
377
+ np = import_module('numpy')
378
+ if isinstance(x, (int, float)):
379
+ return interval(np.arcsinh(x))
380
+ elif isinstance(x, interval):
381
+ start = np.arcsinh(x.start)
382
+ end = np.arcsinh(x.end)
383
+ return interval(start, end, is_valid=x.is_valid)
384
+ else:
385
+ return NotImplementedError
386
+
387
+
388
+ def atanh(x):
389
+ """Evaluates the inverse hyperbolic tangent of an interval"""
390
+ np = import_module('numpy')
391
+ if isinstance(x, (int, float)):
392
+ #Outside the domain
393
+ if abs(x) >= 1:
394
+ return interval(-np.inf, np.inf, is_valid=False)
395
+ else:
396
+ return interval(np.arctanh(x))
397
+ elif isinstance(x, interval):
398
+ #outside the domain
399
+ if x.is_valid is False or x.start >= 1 or x.end <= -1:
400
+ return interval(-np.inf, np.inf, is_valid=False)
401
+ #partly outside the domain
402
+ elif x.start <= -1 or x.end >= 1:
403
+ return interval(-np.inf, np.inf, is_valid=None)
404
+ else:
405
+ start = np.arctanh(x.start)
406
+ end = np.arctanh(x.end)
407
+ return interval(start, end, is_valid=x.is_valid)
408
+ else:
409
+ return NotImplementedError
410
+
411
+
412
+ #Three valued logic for interval plotting.
413
+
414
+ def And(*args):
415
+ """Defines the three valued ``And`` behaviour for a 2-tuple of
416
+ three valued logic values"""
417
+ def reduce_and(cmp_intervala, cmp_intervalb):
418
+ if cmp_intervala[0] is False or cmp_intervalb[0] is False:
419
+ first = False
420
+ elif cmp_intervala[0] is None or cmp_intervalb[0] is None:
421
+ first = None
422
+ else:
423
+ first = True
424
+ if cmp_intervala[1] is False or cmp_intervalb[1] is False:
425
+ second = False
426
+ elif cmp_intervala[1] is None or cmp_intervalb[1] is None:
427
+ second = None
428
+ else:
429
+ second = True
430
+ return (first, second)
431
+ return reduce(reduce_and, args)
432
+
433
+
434
+ def Or(*args):
435
+ """Defines the three valued ``Or`` behaviour for a 2-tuple of
436
+ three valued logic values"""
437
+ def reduce_or(cmp_intervala, cmp_intervalb):
438
+ if cmp_intervala[0] is True or cmp_intervalb[0] is True:
439
+ first = True
440
+ elif cmp_intervala[0] is None or cmp_intervalb[0] is None:
441
+ first = None
442
+ else:
443
+ first = False
444
+
445
+ if cmp_intervala[1] is True or cmp_intervalb[1] is True:
446
+ second = True
447
+ elif cmp_intervala[1] is None or cmp_intervalb[1] is None:
448
+ second = None
449
+ else:
450
+ second = False
451
+ return (first, second)
452
+ return reduce(reduce_or, args)
wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__init__.py ADDED
File without changes
wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (183 Bytes). View file
 
wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__pycache__/test_interval_functions.cpython-310.pyc ADDED
Binary file (9.28 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/__pycache__/test_intervalmath.cpython-310.pyc ADDED
Binary file (6.35 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/test_interval_membership.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.core.symbol import Symbol
2
+ from sympy.plotting.intervalmath import interval
3
+ from sympy.plotting.intervalmath.interval_membership import intervalMembership
4
+ from sympy.plotting.experimental_lambdify import experimental_lambdify
5
+ from sympy.testing.pytest import raises
6
+
7
+
8
+ def test_creation():
9
+ assert intervalMembership(True, True)
10
+ raises(TypeError, lambda: intervalMembership(True))
11
+ raises(TypeError, lambda: intervalMembership(True, True, True))
12
+
13
+
14
+ def test_getitem():
15
+ a = intervalMembership(True, False)
16
+ assert a[0] is True
17
+ assert a[1] is False
18
+ raises(IndexError, lambda: a[2])
19
+
20
+
21
+ def test_str():
22
+ a = intervalMembership(True, False)
23
+ assert str(a) == 'intervalMembership(True, False)'
24
+ assert repr(a) == 'intervalMembership(True, False)'
25
+
26
+
27
+ def test_equivalence():
28
+ a = intervalMembership(True, True)
29
+ b = intervalMembership(True, False)
30
+ assert (a == b) is False
31
+ assert (a != b) is True
32
+
33
+ a = intervalMembership(True, False)
34
+ b = intervalMembership(True, False)
35
+ assert (a == b) is True
36
+ assert (a != b) is False
37
+
38
+
39
+ def test_not():
40
+ x = Symbol('x')
41
+
42
+ r1 = x > -1
43
+ r2 = x <= -1
44
+
45
+ i = interval
46
+
47
+ f1 = experimental_lambdify((x,), r1)
48
+ f2 = experimental_lambdify((x,), r2)
49
+
50
+ tt = i(-0.1, 0.1, is_valid=True)
51
+ tn = i(-0.1, 0.1, is_valid=None)
52
+ tf = i(-0.1, 0.1, is_valid=False)
53
+
54
+ assert f1(tt) == ~f2(tt)
55
+ assert f1(tn) == ~f2(tn)
56
+ assert f1(tf) == ~f2(tf)
57
+
58
+ nt = i(0.9, 1.1, is_valid=True)
59
+ nn = i(0.9, 1.1, is_valid=None)
60
+ nf = i(0.9, 1.1, is_valid=False)
61
+
62
+ assert f1(nt) == ~f2(nt)
63
+ assert f1(nn) == ~f2(nn)
64
+ assert f1(nf) == ~f2(nf)
65
+
66
+ ft = i(1.9, 2.1, is_valid=True)
67
+ fn = i(1.9, 2.1, is_valid=None)
68
+ ff = i(1.9, 2.1, is_valid=False)
69
+
70
+ assert f1(ft) == ~f2(ft)
71
+ assert f1(fn) == ~f2(fn)
72
+ assert f1(ff) == ~f2(ff)
73
+
74
+
75
+ def test_boolean():
76
+ # There can be 9*9 test cases in full mapping of the cartesian product.
77
+ # But we only consider 3*3 cases for simplicity.
78
+ s = [
79
+ intervalMembership(False, False),
80
+ intervalMembership(None, None),
81
+ intervalMembership(True, True)
82
+ ]
83
+
84
+ # Reduced tests for 'And'
85
+ a1 = [
86
+ intervalMembership(False, False),
87
+ intervalMembership(False, False),
88
+ intervalMembership(False, False),
89
+ intervalMembership(False, False),
90
+ intervalMembership(None, None),
91
+ intervalMembership(None, None),
92
+ intervalMembership(False, False),
93
+ intervalMembership(None, None),
94
+ intervalMembership(True, True)
95
+ ]
96
+ a1_iter = iter(a1)
97
+ for i in range(len(s)):
98
+ for j in range(len(s)):
99
+ assert s[i] & s[j] == next(a1_iter)
100
+
101
+ # Reduced tests for 'Or'
102
+ a1 = [
103
+ intervalMembership(False, False),
104
+ intervalMembership(None, False),
105
+ intervalMembership(True, False),
106
+ intervalMembership(None, False),
107
+ intervalMembership(None, None),
108
+ intervalMembership(True, None),
109
+ intervalMembership(True, False),
110
+ intervalMembership(True, None),
111
+ intervalMembership(True, True)
112
+ ]
113
+ a1_iter = iter(a1)
114
+ for i in range(len(s)):
115
+ for j in range(len(s)):
116
+ assert s[i] | s[j] == next(a1_iter)
117
+
118
+ # Reduced tests for 'Xor'
119
+ a1 = [
120
+ intervalMembership(False, False),
121
+ intervalMembership(None, False),
122
+ intervalMembership(True, False),
123
+ intervalMembership(None, False),
124
+ intervalMembership(None, None),
125
+ intervalMembership(None, None),
126
+ intervalMembership(True, False),
127
+ intervalMembership(None, None),
128
+ intervalMembership(False, True)
129
+ ]
130
+ a1_iter = iter(a1)
131
+ for i in range(len(s)):
132
+ for j in range(len(s)):
133
+ assert s[i] ^ s[j] == next(a1_iter)
134
+
135
+ # Reduced tests for 'Not'
136
+ a1 = [
137
+ intervalMembership(True, False),
138
+ intervalMembership(None, None),
139
+ intervalMembership(False, True)
140
+ ]
141
+ a1_iter = iter(a1)
142
+ for i in range(len(s)):
143
+ assert ~s[i] == next(a1_iter)
144
+
145
+
146
+ def test_boolean_errors():
147
+ a = intervalMembership(True, True)
148
+ raises(ValueError, lambda: a & 1)
149
+ raises(ValueError, lambda: a | 1)
150
+ raises(ValueError, lambda: a ^ 1)
wemm/lib/python3.10/site-packages/sympy/plotting/intervalmath/tests/test_intervalmath.py ADDED
@@ -0,0 +1,213 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.plotting.intervalmath import interval
2
+ from sympy.testing.pytest import raises
3
+
4
+
5
+ def test_interval():
6
+ assert (interval(1, 1) == interval(1, 1, is_valid=True)) == (True, True)
7
+ assert (interval(1, 1) == interval(1, 1, is_valid=False)) == (True, False)
8
+ assert (interval(1, 1) == interval(1, 1, is_valid=None)) == (True, None)
9
+ assert (interval(1, 1.5) == interval(1, 2)) == (None, True)
10
+ assert (interval(0, 1) == interval(2, 3)) == (False, True)
11
+ assert (interval(0, 1) == interval(1, 2)) == (None, True)
12
+ assert (interval(1, 2) != interval(1, 2)) == (False, True)
13
+ assert (interval(1, 3) != interval(2, 3)) == (None, True)
14
+ assert (interval(1, 3) != interval(-5, -3)) == (True, True)
15
+ assert (
16
+ interval(1, 3, is_valid=False) != interval(-5, -3)) == (True, False)
17
+ assert (interval(1, 3, is_valid=None) != interval(-5, 3)) == (None, None)
18
+ assert (interval(4, 4) != 4) == (False, True)
19
+ assert (interval(1, 1) == 1) == (True, True)
20
+ assert (interval(1, 3, is_valid=False) == interval(1, 3)) == (True, False)
21
+ assert (interval(1, 3, is_valid=None) == interval(1, 3)) == (True, None)
22
+ inter = interval(-5, 5)
23
+ assert (interval(inter) == interval(-5, 5)) == (True, True)
24
+ assert inter.width == 10
25
+ assert 0 in inter
26
+ assert -5 in inter
27
+ assert 5 in inter
28
+ assert interval(0, 3) in inter
29
+ assert interval(-6, 2) not in inter
30
+ assert -5.05 not in inter
31
+ assert 5.3 not in inter
32
+ interb = interval(-float('inf'), float('inf'))
33
+ assert 0 in inter
34
+ assert inter in interb
35
+ assert interval(0, float('inf')) in interb
36
+ assert interval(-float('inf'), 5) in interb
37
+ assert interval(-1e50, 1e50) in interb
38
+ assert (
39
+ -interval(-1, -2, is_valid=False) == interval(1, 2)) == (True, False)
40
+ raises(ValueError, lambda: interval(1, 2, 3))
41
+
42
+
43
+ def test_interval_add():
44
+ assert (interval(1, 2) + interval(2, 3) == interval(3, 5)) == (True, True)
45
+ assert (1 + interval(1, 2) == interval(2, 3)) == (True, True)
46
+ assert (interval(1, 2) + 1 == interval(2, 3)) == (True, True)
47
+ compare = (1 + interval(0, float('inf')) == interval(1, float('inf')))
48
+ assert compare == (True, True)
49
+ a = 1 + interval(2, 5, is_valid=False)
50
+ assert a.is_valid is False
51
+ a = 1 + interval(2, 5, is_valid=None)
52
+ assert a.is_valid is None
53
+ a = interval(2, 5, is_valid=False) + interval(3, 5, is_valid=None)
54
+ assert a.is_valid is False
55
+ a = interval(3, 5) + interval(-1, 1, is_valid=None)
56
+ assert a.is_valid is None
57
+ a = interval(2, 5, is_valid=False) + 1
58
+ assert a.is_valid is False
59
+
60
+
61
+ def test_interval_sub():
62
+ assert (interval(1, 2) - interval(1, 5) == interval(-4, 1)) == (True, True)
63
+ assert (interval(1, 2) - 1 == interval(0, 1)) == (True, True)
64
+ assert (1 - interval(1, 2) == interval(-1, 0)) == (True, True)
65
+ a = 1 - interval(1, 2, is_valid=False)
66
+ assert a.is_valid is False
67
+ a = interval(1, 4, is_valid=None) - 1
68
+ assert a.is_valid is None
69
+ a = interval(1, 3, is_valid=False) - interval(1, 3)
70
+ assert a.is_valid is False
71
+ a = interval(1, 3, is_valid=None) - interval(1, 3)
72
+ assert a.is_valid is None
73
+
74
+
75
+ def test_interval_inequality():
76
+ assert (interval(1, 2) < interval(3, 4)) == (True, True)
77
+ assert (interval(1, 2) < interval(2, 4)) == (None, True)
78
+ assert (interval(1, 2) < interval(-2, 0)) == (False, True)
79
+ assert (interval(1, 2) <= interval(2, 4)) == (True, True)
80
+ assert (interval(1, 2) <= interval(1.5, 6)) == (None, True)
81
+ assert (interval(2, 3) <= interval(1, 2)) == (None, True)
82
+ assert (interval(2, 3) <= interval(1, 1.5)) == (False, True)
83
+ assert (
84
+ interval(1, 2, is_valid=False) <= interval(-2, 0)) == (False, False)
85
+ assert (interval(1, 2, is_valid=None) <= interval(-2, 0)) == (False, None)
86
+ assert (interval(1, 2) <= 1.5) == (None, True)
87
+ assert (interval(1, 2) <= 3) == (True, True)
88
+ assert (interval(1, 2) <= 0) == (False, True)
89
+ assert (interval(5, 8) > interval(2, 3)) == (True, True)
90
+ assert (interval(2, 5) > interval(1, 3)) == (None, True)
91
+ assert (interval(2, 3) > interval(3.1, 5)) == (False, True)
92
+
93
+ assert (interval(-1, 1) == 0) == (None, True)
94
+ assert (interval(-1, 1) == 2) == (False, True)
95
+ assert (interval(-1, 1) != 0) == (None, True)
96
+ assert (interval(-1, 1) != 2) == (True, True)
97
+
98
+ assert (interval(3, 5) > 2) == (True, True)
99
+ assert (interval(3, 5) < 2) == (False, True)
100
+ assert (interval(1, 5) < 2) == (None, True)
101
+ assert (interval(1, 5) > 2) == (None, True)
102
+ assert (interval(0, 1) > 2) == (False, True)
103
+ assert (interval(1, 2) >= interval(0, 1)) == (True, True)
104
+ assert (interval(1, 2) >= interval(0, 1.5)) == (None, True)
105
+ assert (interval(1, 2) >= interval(3, 4)) == (False, True)
106
+ assert (interval(1, 2) >= 0) == (True, True)
107
+ assert (interval(1, 2) >= 1.2) == (None, True)
108
+ assert (interval(1, 2) >= 3) == (False, True)
109
+ assert (2 > interval(0, 1)) == (True, True)
110
+ a = interval(-1, 1, is_valid=False) < interval(2, 5, is_valid=None)
111
+ assert a == (True, False)
112
+ a = interval(-1, 1, is_valid=None) < interval(2, 5, is_valid=False)
113
+ assert a == (True, False)
114
+ a = interval(-1, 1, is_valid=None) < interval(2, 5, is_valid=None)
115
+ assert a == (True, None)
116
+ a = interval(-1, 1, is_valid=False) > interval(-5, -2, is_valid=None)
117
+ assert a == (True, False)
118
+ a = interval(-1, 1, is_valid=None) > interval(-5, -2, is_valid=False)
119
+ assert a == (True, False)
120
+ a = interval(-1, 1, is_valid=None) > interval(-5, -2, is_valid=None)
121
+ assert a == (True, None)
122
+
123
+
124
+ def test_interval_mul():
125
+ assert (
126
+ interval(1, 5) * interval(2, 10) == interval(2, 50)) == (True, True)
127
+ a = interval(-1, 1) * interval(2, 10) == interval(-10, 10)
128
+ assert a == (True, True)
129
+
130
+ a = interval(-1, 1) * interval(-5, 3) == interval(-5, 5)
131
+ assert a == (True, True)
132
+
133
+ assert (interval(1, 3) * 2 == interval(2, 6)) == (True, True)
134
+ assert (3 * interval(-1, 2) == interval(-3, 6)) == (True, True)
135
+
136
+ a = 3 * interval(1, 2, is_valid=False)
137
+ assert a.is_valid is False
138
+
139
+ a = 3 * interval(1, 2, is_valid=None)
140
+ assert a.is_valid is None
141
+
142
+ a = interval(1, 5, is_valid=False) * interval(1, 2, is_valid=None)
143
+ assert a.is_valid is False
144
+
145
+
146
+ def test_interval_div():
147
+ div = interval(1, 2, is_valid=False) / 3
148
+ assert div == interval(-float('inf'), float('inf'), is_valid=False)
149
+
150
+ div = interval(1, 2, is_valid=None) / 3
151
+ assert div == interval(-float('inf'), float('inf'), is_valid=None)
152
+
153
+ div = 3 / interval(1, 2, is_valid=None)
154
+ assert div == interval(-float('inf'), float('inf'), is_valid=None)
155
+ a = interval(1, 2) / 0
156
+ assert a.is_valid is False
157
+ a = interval(0.5, 1) / interval(-1, 0)
158
+ assert a.is_valid is None
159
+ a = interval(0, 1) / interval(0, 1)
160
+ assert a.is_valid is None
161
+
162
+ a = interval(-1, 1) / interval(-1, 1)
163
+ assert a.is_valid is None
164
+
165
+ a = interval(-1, 2) / interval(0.5, 1) == interval(-2.0, 4.0)
166
+ assert a == (True, True)
167
+ a = interval(0, 1) / interval(0.5, 1) == interval(0.0, 2.0)
168
+ assert a == (True, True)
169
+ a = interval(-1, 0) / interval(0.5, 1) == interval(-2.0, 0.0)
170
+ assert a == (True, True)
171
+ a = interval(-0.5, -0.25) / interval(0.5, 1) == interval(-1.0, -0.25)
172
+ assert a == (True, True)
173
+ a = interval(0.5, 1) / interval(0.5, 1) == interval(0.5, 2.0)
174
+ assert a == (True, True)
175
+ a = interval(0.5, 4) / interval(0.5, 1) == interval(0.5, 8.0)
176
+ assert a == (True, True)
177
+ a = interval(-1, -0.5) / interval(0.5, 1) == interval(-2.0, -0.5)
178
+ assert a == (True, True)
179
+ a = interval(-4, -0.5) / interval(0.5, 1) == interval(-8.0, -0.5)
180
+ assert a == (True, True)
181
+ a = interval(-1, 2) / interval(-2, -0.5) == interval(-4.0, 2.0)
182
+ assert a == (True, True)
183
+ a = interval(0, 1) / interval(-2, -0.5) == interval(-2.0, 0.0)
184
+ assert a == (True, True)
185
+ a = interval(-1, 0) / interval(-2, -0.5) == interval(0.0, 2.0)
186
+ assert a == (True, True)
187
+ a = interval(-0.5, -0.25) / interval(-2, -0.5) == interval(0.125, 1.0)
188
+ assert a == (True, True)
189
+ a = interval(0.5, 1) / interval(-2, -0.5) == interval(-2.0, -0.25)
190
+ assert a == (True, True)
191
+ a = interval(0.5, 4) / interval(-2, -0.5) == interval(-8.0, -0.25)
192
+ assert a == (True, True)
193
+ a = interval(-1, -0.5) / interval(-2, -0.5) == interval(0.25, 2.0)
194
+ assert a == (True, True)
195
+ a = interval(-4, -0.5) / interval(-2, -0.5) == interval(0.25, 8.0)
196
+ assert a == (True, True)
197
+ a = interval(-5, 5, is_valid=False) / 2
198
+ assert a.is_valid is False
199
+
200
+ def test_hashable():
201
+ '''
202
+ test that interval objects are hashable.
203
+ this is required in order to be able to put them into the cache, which
204
+ appears to be necessary for plotting in py3k. For details, see:
205
+
206
+ https://github.com/sympy/sympy/pull/2101
207
+ https://github.com/sympy/sympy/issues/6533
208
+ '''
209
+ hash(interval(1, 1))
210
+ hash(interval(1, 1, is_valid=True))
211
+ hash(interval(-4, -0.5))
212
+ hash(interval(-2, -0.5))
213
+ hash(interval(0.25, 8.0))
wemm/lib/python3.10/site-packages/sympy/plotting/tests/test_region_and.png ADDED

Git LFS Details

  • SHA256: 115d0b9b81ed40f93fe9e216b4f6384cf71093e3bbb64a5d648b8b9858c645a0
  • Pointer size: 129 Bytes
  • Size of remote file: 6.86 kB
wemm/lib/python3.10/site-packages/sympy/plotting/tests/test_region_not.png ADDED

Git LFS Details

  • SHA256: dceffdfe73d6d78f453142c4713e51a88dbe9361f79c710b6df2400edd9c3bc9
  • Pointer size: 129 Bytes
  • Size of remote file: 7.94 kB
wemm/lib/python3.10/site-packages/sympy/plotting/tests/test_region_or.png ADDED

Git LFS Details

  • SHA256: e41ba0d3dbf2a20f82bb79a4cbba5bb458dec396ccbdba5ed195d6b200ca7f2e
  • Pointer size: 129 Bytes
  • Size of remote file: 8.81 kB
wemm/lib/python3.10/site-packages/sympy/plotting/tests/test_region_xor.png ADDED

Git LFS Details

  • SHA256: 92e71558103d03df0ea5c47876277968b5d4ca8ab8cf43b80b73cce9d962052c
  • Pointer size: 130 Bytes
  • Size of remote file: 10 kB
wemm/lib/python3.10/site-packages/sympy/printing/__pycache__/latex.cpython-310.pyc ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5d8864784c255fc33d6f4c3b8d0ac7e3e6d22c7d5725148f75de0605a063e2da
3
+ size 119123
wemm/lib/python3.10/site-packages/sympy/printing/pretty/tests/__pycache__/test_pretty.cpython-310.pyc ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d44790819430c1ea2af80b5a3674166abeb191b8344fa9104ea4b8b114d7ddd9
3
+ size 154761
wemm/lib/python3.10/site-packages/sympy/series/__init__.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """A module that handles series: find a limit, order the series etc.
2
+ """
3
+ from .order import Order
4
+ from .limits import limit, Limit
5
+ from .gruntz import gruntz
6
+ from .series import series
7
+ from .approximants import approximants
8
+ from .residues import residue
9
+ from .sequences import SeqPer, SeqFormula, sequence, SeqAdd, SeqMul
10
+ from .fourier import fourier_series
11
+ from .formal import fps
12
+ from .limitseq import difference_delta, limit_seq
13
+
14
+ from sympy.core.singleton import S
15
+ EmptySequence = S.EmptySequence
16
+
17
+ O = Order
18
+
19
+ __all__ = ['Order', 'O', 'limit', 'Limit', 'gruntz', 'series', 'approximants',
20
+ 'residue', 'EmptySequence', 'SeqPer', 'SeqFormula', 'sequence',
21
+ 'SeqAdd', 'SeqMul', 'fourier_series', 'fps', 'difference_delta',
22
+ 'limit_seq'
23
+ ]
wemm/lib/python3.10/site-packages/sympy/series/__pycache__/approximants.cpython-310.pyc ADDED
Binary file (3.41 kB). View file
 
wemm/lib/python3.10/site-packages/sympy/series/__pycache__/aseries.cpython-310.pyc ADDED
Binary file (470 Bytes). View file