m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1336 lines
48 KiB

6 months ago
  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.simplify.simplify import simplify
  11. from sympy.logic.boolalg import (
  12. And, Boolean, Equivalent, ITE, Implies, Nand, Nor, Not, Or,
  13. POSform, SOPform, Xor, Xnor, conjuncts, disjuncts,
  14. distribute_or_over_and, distribute_and_over_or,
  15. eliminate_implications, is_nnf, is_cnf, is_dnf, simplify_logic,
  16. to_nnf, to_cnf, to_dnf, to_int_repr, bool_map, true, false,
  17. BooleanAtom, is_literal, term_to_integer,
  18. truth_table, as_Boolean, to_anf, is_anf, distribute_xor_over_and,
  19. anf_coeffs, ANFform, bool_minterm, bool_maxterm, bool_monomial,
  20. _check_pair, _convert_to_varsSOP, _convert_to_varsPOS, Exclusive,
  21. gateinputcount)
  22. from sympy.assumptions.cnf import CNF
  23. from sympy.testing.pytest import raises, XFAIL, slow
  24. from itertools import combinations, permutations, product
  25. A, B, C, D = symbols('A:D')
  26. a, b, c, d, e, w, x, y, z = symbols('a:e w:z')
  27. def test_overloading():
  28. """Test that |, & are overloaded as expected"""
  29. assert A & B == And(A, B)
  30. assert A | B == Or(A, B)
  31. assert (A & B) | C == Or(And(A, B), C)
  32. assert A >> B == Implies(A, B)
  33. assert A << B == Implies(B, A)
  34. assert ~A == Not(A)
  35. assert A ^ B == Xor(A, B)
  36. def test_And():
  37. assert And() is true
  38. assert And(A) == A
  39. assert And(True) is true
  40. assert And(False) is false
  41. assert And(True, True) is true
  42. assert And(True, False) is false
  43. assert And(False, False) is false
  44. assert And(True, A) == A
  45. assert And(False, A) is false
  46. assert And(True, True, True) is true
  47. assert And(True, True, A) == A
  48. assert And(True, False, A) is false
  49. assert And(1, A) == A
  50. raises(TypeError, lambda: And(2, A))
  51. raises(TypeError, lambda: And(A < 2, A))
  52. assert And(A < 1, A >= 1) is false
  53. e = A > 1
  54. assert And(e, e.canonical) == e.canonical
  55. g, l, ge, le = A > B, B < A, A >= B, B <= A
  56. assert And(g, l, ge, le) == And(ge, g)
  57. assert {And(*i) for i in permutations((l,g,le,ge))} == {And(ge, g)}
  58. assert And(And(Eq(a, 0), Eq(b, 0)), And(Ne(a, 0), Eq(c, 0))) is false
  59. def test_Or():
  60. assert Or() is false
  61. assert Or(A) == A
  62. assert Or(True) is true
  63. assert Or(False) is false
  64. assert Or(True, True) is true
  65. assert Or(True, False) is true
  66. assert Or(False, False) is false
  67. assert Or(True, A) is true
  68. assert Or(False, A) == A
  69. assert Or(True, False, False) is true
  70. assert Or(True, False, A) is true
  71. assert Or(False, False, A) == A
  72. assert Or(1, A) is true
  73. raises(TypeError, lambda: Or(2, A))
  74. raises(TypeError, lambda: Or(A < 2, A))
  75. assert Or(A < 1, A >= 1) is true
  76. e = A > 1
  77. assert Or(e, e.canonical) == e
  78. g, l, ge, le = A > B, B < A, A >= B, B <= A
  79. assert Or(g, l, ge, le) == Or(g, ge)
  80. def test_Xor():
  81. assert Xor() is false
  82. assert Xor(A) == A
  83. assert Xor(A, A) is false
  84. assert Xor(True, A, A) is true
  85. assert Xor(A, A, A, A, A) == A
  86. assert Xor(True, False, False, A, B) == ~Xor(A, B)
  87. assert Xor(True) is true
  88. assert Xor(False) is false
  89. assert Xor(True, True) is false
  90. assert Xor(True, False) is true
  91. assert Xor(False, False) is false
  92. assert Xor(True, A) == ~A
  93. assert Xor(False, A) == A
  94. assert Xor(True, False, False) is true
  95. assert Xor(True, False, A) == ~A
  96. assert Xor(False, False, A) == A
  97. assert isinstance(Xor(A, B), Xor)
  98. assert Xor(A, B, Xor(C, D)) == Xor(A, B, C, D)
  99. assert Xor(A, B, Xor(B, C)) == Xor(A, C)
  100. assert Xor(A < 1, A >= 1, B) == Xor(0, 1, B) == Xor(1, 0, B)
  101. e = A > 1
  102. assert Xor(e, e.canonical) == Xor(0, 0) == Xor(1, 1)
  103. def test_rewrite_as_And():
  104. expr = x ^ y
  105. assert expr.rewrite(And) == (x | y) & (~x | ~y)
  106. def test_rewrite_as_Or():
  107. expr = x ^ y
  108. assert expr.rewrite(Or) == (x & ~y) | (y & ~x)
  109. def test_rewrite_as_Nand():
  110. expr = (y & z) | (z & ~w)
  111. assert expr.rewrite(Nand) == ~(~(y & z) & ~(z & ~w))
  112. def test_rewrite_as_Nor():
  113. expr = z & (y | ~w)
  114. assert expr.rewrite(Nor) == ~(~z | ~(y | ~w))
  115. def test_Not():
  116. raises(TypeError, lambda: Not(True, False))
  117. assert Not(True) is false
  118. assert Not(False) is true
  119. assert Not(0) is true
  120. assert Not(1) is false
  121. assert Not(2) is false
  122. def test_Nand():
  123. assert Nand() is false
  124. assert Nand(A) == ~A
  125. assert Nand(True) is false
  126. assert Nand(False) is true
  127. assert Nand(True, True) is false
  128. assert Nand(True, False) is true
  129. assert Nand(False, False) is true
  130. assert Nand(True, A) == ~A
  131. assert Nand(False, A) is true
  132. assert Nand(True, True, True) is false
  133. assert Nand(True, True, A) == ~A
  134. assert Nand(True, False, A) is true
  135. def test_Nor():
  136. assert Nor() is true
  137. assert Nor(A) == ~A
  138. assert Nor(True) is false
  139. assert Nor(False) is true
  140. assert Nor(True, True) is false
  141. assert Nor(True, False) is false
  142. assert Nor(False, False) is true
  143. assert Nor(True, A) is false
  144. assert Nor(False, A) == ~A
  145. assert Nor(True, True, True) is false
  146. assert Nor(True, True, A) is false
  147. assert Nor(True, False, A) is false
  148. def test_Xnor():
  149. assert Xnor() is true
  150. assert Xnor(A) == ~A
  151. assert Xnor(A, A) is true
  152. assert Xnor(True, A, A) is false
  153. assert Xnor(A, A, A, A, A) == ~A
  154. assert Xnor(True) is false
  155. assert Xnor(False) is true
  156. assert Xnor(True, True) is true
  157. assert Xnor(True, False) is false
  158. assert Xnor(False, False) is true
  159. assert Xnor(True, A) == A
  160. assert Xnor(False, A) == ~A
  161. assert Xnor(True, False, False) is false
  162. assert Xnor(True, False, A) == A
  163. assert Xnor(False, False, A) == ~A
  164. def test_Implies():
  165. raises(ValueError, lambda: Implies(A, B, C))
  166. assert Implies(True, True) is true
  167. assert Implies(True, False) is false
  168. assert Implies(False, True) is true
  169. assert Implies(False, False) is true
  170. assert Implies(0, A) is true
  171. assert Implies(1, 1) is true
  172. assert Implies(1, 0) is false
  173. assert A >> B == B << A
  174. assert (A < 1) >> (A >= 1) == (A >= 1)
  175. assert (A < 1) >> (S.One > A) is true
  176. assert A >> A is true
  177. def test_Equivalent():
  178. assert Equivalent(A, B) == Equivalent(B, A) == Equivalent(A, B, A)
  179. assert Equivalent() is true
  180. assert Equivalent(A, A) == Equivalent(A) is true
  181. assert Equivalent(True, True) == Equivalent(False, False) is true
  182. assert Equivalent(True, False) == Equivalent(False, True) is false
  183. assert Equivalent(A, True) == A
  184. assert Equivalent(A, False) == Not(A)
  185. assert Equivalent(A, B, True) == A & B
  186. assert Equivalent(A, B, False) == ~A & ~B
  187. assert Equivalent(1, A) == A
  188. assert Equivalent(0, A) == Not(A)
  189. assert Equivalent(A, Equivalent(B, C)) != Equivalent(Equivalent(A, B), C)
  190. assert Equivalent(A < 1, A >= 1) is false
  191. assert Equivalent(A < 1, A >= 1, 0) is false
  192. assert Equivalent(A < 1, A >= 1, 1) is false
  193. assert Equivalent(A < 1, S.One > A) == Equivalent(1, 1) == Equivalent(0, 0)
  194. assert Equivalent(Equality(A, B), Equality(B, A)) is true
  195. def test_Exclusive():
  196. assert Exclusive(False, False, False) is true
  197. assert Exclusive(True, False, False) is true
  198. assert Exclusive(True, True, False) is false
  199. assert Exclusive(True, True, True) is false
  200. def test_equals():
  201. assert Not(Or(A, B)).equals(And(Not(A), Not(B))) is True
  202. assert Equivalent(A, B).equals((A >> B) & (B >> A)) is True
  203. assert ((A | ~B) & (~A | B)).equals((~A & ~B) | (A & B)) is True
  204. assert (A >> B).equals(~A >> ~B) is False
  205. assert (A >> (B >> A)).equals(A >> (C >> A)) is False
  206. raises(NotImplementedError, lambda: (A & B).equals(A > B))
  207. def test_simplification_boolalg():
  208. """
  209. Test working of simplification methods.
  210. """
  211. set1 = [[0, 0, 1], [0, 1, 1], [1, 0, 0], [1, 1, 0]]
  212. set2 = [[0, 0, 0], [0, 1, 0], [1, 0, 1], [1, 1, 1]]
  213. assert SOPform([x, y, z], set1) == Or(And(Not(x), z), And(Not(z), x))
  214. assert Not(SOPform([x, y, z], set2)) == \
  215. Not(Or(And(Not(x), Not(z)), And(x, z)))
  216. assert POSform([x, y, z], set1 + set2) is true
  217. assert SOPform([x, y, z], set1 + set2) is true
  218. assert SOPform([Dummy(), Dummy(), Dummy()], set1 + set2) is true
  219. minterms = [[0, 0, 0, 1], [0, 0, 1, 1], [0, 1, 1, 1], [1, 0, 1, 1],
  220. [1, 1, 1, 1]]
  221. dontcares = [[0, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 1]]
  222. assert (
  223. SOPform([w, x, y, z], minterms, dontcares) ==
  224. Or(And(y, z), And(Not(w), Not(x))))
  225. assert POSform([w, x, y, z], minterms, dontcares) == And(Or(Not(w), y), z)
  226. minterms = [1, 3, 7, 11, 15]
  227. dontcares = [0, 2, 5]
  228. assert (
  229. SOPform([w, x, y, z], minterms, dontcares) ==
  230. Or(And(y, z), And(Not(w), Not(x))))
  231. assert POSform([w, x, y, z], minterms, dontcares) == And(Or(Not(w), y), z)
  232. minterms = [1, [0, 0, 1, 1], 7, [1, 0, 1, 1],
  233. [1, 1, 1, 1]]
  234. dontcares = [0, [0, 0, 1, 0], 5]
  235. assert (
  236. SOPform([w, x, y, z], minterms, dontcares) ==
  237. Or(And(y, z), And(Not(w), Not(x))))
  238. assert POSform([w, x, y, z], minterms, dontcares) == And(Or(Not(w), y), z)
  239. minterms = [1, {y: 1, z: 1}]
  240. dontcares = [0, [0, 0, 1, 0], 5]
  241. assert (
  242. SOPform([w, x, y, z], minterms, dontcares) ==
  243. Or(And(y, z), And(Not(w), Not(x))))
  244. assert POSform([w, x, y, z], minterms, dontcares) == And(Or(Not(w), y), z)
  245. minterms = [{y: 1, z: 1}, 1]
  246. dontcares = [[0, 0, 0, 0]]
  247. minterms = [[0, 0, 0]]
  248. raises(ValueError, lambda: SOPform([w, x, y, z], minterms))
  249. raises(ValueError, lambda: POSform([w, x, y, z], minterms))
  250. raises(TypeError, lambda: POSform([w, x, y, z], ["abcdefg"]))
  251. # test simplification
  252. ans = And(A, Or(B, C))
  253. assert simplify_logic(A & (B | C)) == ans
  254. assert simplify_logic((A & B) | (A & C)) == ans
  255. assert simplify_logic(Implies(A, B)) == Or(Not(A), B)
  256. assert simplify_logic(Equivalent(A, B)) == \
  257. Or(And(A, B), And(Not(A), Not(B)))
  258. assert simplify_logic(And(Equality(A, 2), C)) == And(Equality(A, 2), C)
  259. assert simplify_logic(And(Equality(A, 2), A)) is S.false
  260. assert simplify_logic(And(Equality(A, 2), A)) == And(Equality(A, 2), A)
  261. assert simplify_logic(And(Equality(A, B), C)) == And(Equality(A, B), C)
  262. assert simplify_logic(Or(And(Equality(A, 3), B), And(Equality(A, 3), C))) \
  263. == And(Equality(A, 3), Or(B, C))
  264. b = (~x & ~y & ~z) | (~x & ~y & z)
  265. e = And(A, b)
  266. assert simplify_logic(e) == A & ~x & ~y
  267. raises(ValueError, lambda: simplify_logic(A & (B | C), form='blabla'))
  268. assert simplify(Or(x <= y, And(x < y, z))) == (x <= y)
  269. assert simplify(Or(x <= y, And(y > x, z))) == (x <= y)
  270. assert simplify(Or(x >= y, And(y < x, z))) == (x >= y)
  271. # Check that expressions with nine variables or more are not simplified
  272. # (without the force-flag)
  273. a, b, c, d, e, f, g, h, j = symbols('a b c d e f g h j')
  274. expr = a & b & c & d & e & f & g & h & j | \
  275. a & b & c & d & e & f & g & h & ~j
  276. # This expression can be simplified to get rid of the j variables
  277. assert simplify_logic(expr) == expr
  278. # check input
  279. ans = SOPform([x, y], [[1, 0]])
  280. assert SOPform([x, y], [[1, 0]]) == ans
  281. assert POSform([x, y], [[1, 0]]) == ans
  282. raises(ValueError, lambda: SOPform([x], [[1]], [[1]]))
  283. assert SOPform([x], [[1]], [[0]]) is true
  284. assert SOPform([x], [[0]], [[1]]) is true
  285. assert SOPform([x], [], []) is false
  286. raises(ValueError, lambda: POSform([x], [[1]], [[1]]))
  287. assert POSform([x], [[1]], [[0]]) is true
  288. assert POSform([x], [[0]], [[1]]) is true
  289. assert POSform([x], [], []) is false
  290. # check working of simplify
  291. assert simplify((A & B) | (A & C)) == And(A, Or(B, C))
  292. assert simplify(And(x, Not(x))) == False
  293. assert simplify(Or(x, Not(x))) == True
  294. assert simplify(And(Eq(x, 0), Eq(x, y))) == And(Eq(x, 0), Eq(y, 0))
  295. assert And(Eq(x - 1, 0), Eq(x, y)).simplify() == And(Eq(x, 1), Eq(y, 1))
  296. assert And(Ne(x - 1, 0), Ne(x, y)).simplify() == And(Ne(x, 1), Ne(x, y))
  297. assert And(Eq(x - 1, 0), Ne(x, y)).simplify() == And(Eq(x, 1), Ne(y, 1))
  298. assert And(Eq(x - 1, 0), Eq(x, z + y), Eq(y + x, 0)).simplify(
  299. ) == And(Eq(x, 1), Eq(y, -1), Eq(z, 2))
  300. assert And(Eq(x - 1, 0), Eq(x + 2, 3)).simplify() == Eq(x, 1)
  301. assert And(Ne(x - 1, 0), Ne(x + 2, 3)).simplify() == Ne(x, 1)
  302. assert And(Eq(x - 1, 0), Eq(x + 2, 2)).simplify() == False
  303. assert And(Ne(x - 1, 0), Ne(x + 2, 2)).simplify(
  304. ) == And(Ne(x, 1), Ne(x, 0))
  305. def test_bool_map():
  306. """
  307. Test working of bool_map function.
  308. """
  309. minterms = [[0, 0, 0, 1], [0, 0, 1, 1], [0, 1, 1, 1], [1, 0, 1, 1],
  310. [1, 1, 1, 1]]
  311. assert bool_map(Not(Not(a)), a) == (a, {a: a})
  312. assert bool_map(SOPform([w, x, y, z], minterms),
  313. POSform([w, x, y, z], minterms)) == \
  314. (And(Or(Not(w), y), Or(Not(x), y), z), {x: x, w: w, z: z, y: y})
  315. assert bool_map(SOPform([x, z, y], [[1, 0, 1]]),
  316. SOPform([a, b, c], [[1, 0, 1]])) != False
  317. function1 = SOPform([x, z, y], [[1, 0, 1], [0, 0, 1]])
  318. function2 = SOPform([a, b, c], [[1, 0, 1], [1, 0, 0]])
  319. assert bool_map(function1, function2) == \
  320. (function1, {y: a, z: b})
  321. assert bool_map(Xor(x, y), ~Xor(x, y)) == False
  322. assert bool_map(And(x, y), Or(x, y)) is None
  323. assert bool_map(And(x, y), And(x, y, z)) is None
  324. # issue 16179
  325. assert bool_map(Xor(x, y, z), ~Xor(x, y, z)) == False
  326. assert bool_map(Xor(a, x, y, z), ~Xor(a, x, y, z)) == False
  327. def test_bool_symbol():
  328. """Test that mixing symbols with boolean values
  329. works as expected"""
  330. assert And(A, True) == A
  331. assert And(A, True, True) == A
  332. assert And(A, False) is false
  333. assert And(A, True, False) is false
  334. assert Or(A, True) is true
  335. assert Or(A, False) == A
  336. def test_is_boolean():
  337. assert isinstance(True, Boolean) is False
  338. assert isinstance(true, Boolean) is True
  339. assert 1 == True
  340. assert 1 != true
  341. assert (1 == true) is False
  342. assert 0 == False
  343. assert 0 != false
  344. assert (0 == false) is False
  345. assert true.is_Boolean is True
  346. assert (A & B).is_Boolean
  347. assert (A | B).is_Boolean
  348. assert (~A).is_Boolean
  349. assert (A ^ B).is_Boolean
  350. assert A.is_Boolean != isinstance(A, Boolean)
  351. assert isinstance(A, Boolean)
  352. def test_subs():
  353. assert (A & B).subs(A, True) == B
  354. assert (A & B).subs(A, False) is false
  355. assert (A & B).subs(B, True) == A
  356. assert (A & B).subs(B, False) is false
  357. assert (A & B).subs({A: True, B: True}) is true
  358. assert (A | B).subs(A, True) is true
  359. assert (A | B).subs(A, False) == B
  360. assert (A | B).subs(B, True) is true
  361. assert (A | B).subs(B, False) == A
  362. assert (A | B).subs({A: True, B: True}) is true
  363. """
  364. we test for axioms of boolean algebra
  365. see https://en.wikipedia.org/wiki/Boolean_algebra_(structure)
  366. """
  367. def test_commutative():
  368. """Test for commutativity of And and Or"""
  369. A, B = map(Boolean, symbols('A,B'))
  370. assert A & B == B & A
  371. assert A | B == B | A
  372. def test_and_associativity():
  373. """Test for associativity of And"""
  374. assert (A & B) & C == A & (B & C)
  375. def test_or_assicativity():
  376. assert ((A | B) | C) == (A | (B | C))
  377. def test_double_negation():
  378. a = Boolean()
  379. assert ~(~a) == a
  380. # test methods
  381. def test_eliminate_implications():
  382. assert eliminate_implications(Implies(A, B, evaluate=False)) == (~A) | B
  383. assert eliminate_implications(
  384. A >> (C >> Not(B))) == Or(Or(Not(B), Not(C)), Not(A))
  385. assert eliminate_implications(Equivalent(A, B, C, D)) == \
  386. (~A | B) & (~B | C) & (~C | D) & (~D | A)
  387. def test_conjuncts():
  388. assert conjuncts(A & B & C) == {A, B, C}
  389. assert conjuncts((A | B) & C) == {A | B, C}
  390. assert conjuncts(A) == {A}
  391. assert conjuncts(True) == {True}
  392. assert conjuncts(False) == {False}
  393. def test_disjuncts():
  394. assert disjuncts(A | B | C) == {A, B, C}
  395. assert disjuncts((A | B) & C) == {(A | B) & C}
  396. assert disjuncts(A) == {A}
  397. assert disjuncts(True) == {True}
  398. assert disjuncts(False) == {False}
  399. def test_distribute():
  400. assert distribute_and_over_or(Or(And(A, B), C)) == And(Or(A, C), Or(B, C))
  401. assert distribute_or_over_and(And(A, Or(B, C))) == Or(And(A, B), And(A, C))
  402. assert distribute_xor_over_and(And(A, Xor(B, C))) == Xor(And(A, B), And(A, C))
  403. def test_to_anf():
  404. x, y, z = symbols('x,y,z')
  405. assert to_anf(And(x, y)) == And(x, y)
  406. assert to_anf(Or(x, y)) == Xor(x, y, And(x, y))
  407. assert to_anf(Or(Implies(x, y), And(x, y), y)) == \
  408. Xor(x, True, x & y, remove_true=False)
  409. assert to_anf(Or(Nand(x, y), Nor(x, y), Xnor(x, y), Implies(x, y))) == True
  410. assert to_anf(Or(x, Not(y), Nor(x,z), And(x, y), Nand(y, z))) == \
  411. Xor(True, And(y, z), And(x, y, z), remove_true=False)
  412. assert to_anf(Xor(x, y)) == Xor(x, y)
  413. assert to_anf(Not(x)) == Xor(x, True, remove_true=False)
  414. assert to_anf(Nand(x, y)) == Xor(True, And(x, y), remove_true=False)
  415. assert to_anf(Nor(x, y)) == Xor(x, y, True, And(x, y), remove_true=False)
  416. assert to_anf(Implies(x, y)) == Xor(x, True, And(x, y), remove_true=False)
  417. assert to_anf(Equivalent(x, y)) == Xor(x, y, True, remove_true=False)
  418. assert to_anf(Nand(x | y, x >> y), deep=False) == \
  419. Xor(True, And(Or(x, y), Implies(x, y)), remove_true=False)
  420. assert to_anf(Nor(x ^ y, x & y), deep=False) == \
  421. Xor(True, Or(Xor(x, y), And(x, y)), remove_true=False)
  422. def test_to_nnf():
  423. assert to_nnf(true) is true
  424. assert to_nnf(false) is false
  425. assert to_nnf(A) == A
  426. assert to_nnf(A | ~A | B) is true
  427. assert to_nnf(A & ~A & B) is false
  428. assert to_nnf(A >> B) == ~A | B
  429. assert to_nnf(Equivalent(A, B, C)) == (~A | B) & (~B | C) & (~C | A)
  430. assert to_nnf(A ^ B ^ C) == \
  431. (A | B | C) & (~A | ~B | C) & (A | ~B | ~C) & (~A | B | ~C)
  432. assert to_nnf(ITE(A, B, C)) == (~A | B) & (A | C)
  433. assert to_nnf(Not(A | B | C)) == ~A & ~B & ~C
  434. assert to_nnf(Not(A & B & C)) == ~A | ~B | ~C
  435. assert to_nnf(Not(A >> B)) == A & ~B
  436. assert to_nnf(Not(Equivalent(A, B, C))) == And(Or(A, B, C), Or(~A, ~B, ~C))
  437. assert to_nnf(Not(A ^ B ^ C)) == \
  438. (~A | B | C) & (A | ~B | C) & (A | B | ~C) & (~A | ~B | ~C)
  439. assert to_nnf(Not(ITE(A, B, C))) == (~A | ~B) & (A | ~C)
  440. assert to_nnf((A >> B) ^ (B >> A)) == (A & ~B) | (~A & B)
  441. assert to_nnf((A >> B) ^ (B >> A), False) == \
  442. (~A | ~B | A | B) & ((A & ~B) | (~A & B))
  443. assert ITE(A, 1, 0).to_nnf() == A
  444. assert ITE(A, 0, 1).to_nnf() == ~A
  445. # although ITE can hold non-Boolean, it will complain if
  446. # an attempt is made to convert the ITE to Boolean nnf
  447. raises(TypeError, lambda: ITE(A < 1, [1], B).to_nnf())
  448. def test_to_cnf():
  449. assert to_cnf(~(B | C)) == And(Not(B), Not(C))
  450. assert to_cnf((A & B) | C) == And(Or(A, C), Or(B, C))
  451. assert to_cnf(A >> B) == (~A) | B
  452. assert to_cnf(A >> (B & C)) == (~A | B) & (~A | C)
  453. assert to_cnf(A & (B | C) | ~A & (B | C), True) == B | C
  454. assert to_cnf(A & B) == And(A, B)
  455. assert to_cnf(Equivalent(A, B)) == And(Or(A, Not(B)), Or(B, Not(A)))
  456. assert to_cnf(Equivalent(A, B & C)) == \
  457. (~A | B) & (~A | C) & (~B | ~C | A)
  458. assert to_cnf(Equivalent(A, B | C), True) == \
  459. And(Or(Not(B), A), Or(Not(C), A), Or(B, C, Not(A)))
  460. assert to_cnf(A + 1) == A + 1
  461. def test_issue_18904():
  462. x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15 = symbols('x1:16')
  463. eq = (( x1 & x2 & x3 & x4 & x5 & x6 & x7 & x8 & x9 ) |
  464. ( x1 & x2 & x3 & x4 & x5 & x6 & x7 & x10 & x9 ) |
  465. ( x1 & x11 & x3 & x12 & x5 & x13 & x14 & x15 & x9 ))
  466. assert is_cnf(to_cnf(eq))
  467. raises(ValueError, lambda: to_cnf(eq, simplify=True))
  468. for f, t in zip((And, Or), (to_cnf, to_dnf)):
  469. eq = f(x1, x2, x3, x4, x5, x6, x7, x8, x9)
  470. raises(ValueError, lambda: to_cnf(eq, simplify=True))
  471. assert t(eq, simplify=True, force=True) == eq
  472. def test_issue_9949():
  473. assert is_cnf(to_cnf((b > -5) | (a > 2) & (a < 4)))
  474. def test_to_CNF():
  475. assert CNF.CNF_to_cnf(CNF.to_CNF(~(B | C))) == to_cnf(~(B | C))
  476. assert CNF.CNF_to_cnf(CNF.to_CNF((A & B) | C)) == to_cnf((A & B) | C)
  477. assert CNF.CNF_to_cnf(CNF.to_CNF(A >> B)) == to_cnf(A >> B)
  478. assert CNF.CNF_to_cnf(CNF.to_CNF(A >> (B & C))) == to_cnf(A >> (B & C))
  479. assert CNF.CNF_to_cnf(CNF.to_CNF(A & (B | C) | ~A & (B | C))) == to_cnf(A & (B | C) | ~A & (B | C))
  480. assert CNF.CNF_to_cnf(CNF.to_CNF(A & B)) == to_cnf(A & B)
  481. def test_to_dnf():
  482. assert to_dnf(~(B | C)) == And(Not(B), Not(C))
  483. assert to_dnf(A & (B | C)) == Or(And(A, B), And(A, C))
  484. assert to_dnf(A >> B) == (~A) | B
  485. assert to_dnf(A >> (B & C)) == (~A) | (B & C)
  486. assert to_dnf(A | B) == A | B
  487. assert to_dnf(Equivalent(A, B), True) == \
  488. Or(And(A, B), And(Not(A), Not(B)))
  489. assert to_dnf(Equivalent(A, B & C), True) == \
  490. Or(And(A, B, C), And(Not(A), Not(B)), And(Not(A), Not(C)))
  491. assert to_dnf(A + 1) == A + 1
  492. def test_to_int_repr():
  493. x, y, z = map(Boolean, symbols('x,y,z'))
  494. def sorted_recursive(arg):
  495. try:
  496. return sorted(sorted_recursive(x) for x in arg)
  497. except TypeError: # arg is not a sequence
  498. return arg
  499. assert sorted_recursive(to_int_repr([x | y, z | x], [x, y, z])) == \
  500. sorted_recursive([[1, 2], [1, 3]])
  501. assert sorted_recursive(to_int_repr([x | y, z | ~x], [x, y, z])) == \
  502. sorted_recursive([[1, 2], [3, -1]])
  503. def test_is_anf():
  504. x, y = symbols('x,y')
  505. assert is_anf(true) is True
  506. assert is_anf(false) is True
  507. assert is_anf(x) is True
  508. assert is_anf(And(x, y)) is True
  509. assert is_anf(Xor(x, y, And(x, y))) is True
  510. assert is_anf(Xor(x, y, Or(x, y))) is False
  511. assert is_anf(Xor(Not(x), y)) is False
  512. def test_is_nnf():
  513. assert is_nnf(true) is True
  514. assert is_nnf(A) is True
  515. assert is_nnf(~A) is True
  516. assert is_nnf(A & B) is True
  517. assert is_nnf((A & B) | (~A & A) | (~B & B) | (~A & ~B), False) is True
  518. assert is_nnf((A | B) & (~A | ~B)) is True
  519. assert is_nnf(Not(Or(A, B))) is False
  520. assert is_nnf(A ^ B) is False
  521. assert is_nnf((A & B) | (~A & A) | (~B & B) | (~A & ~B), True) is False
  522. def test_is_cnf():
  523. assert is_cnf(x) is True
  524. assert is_cnf(x | y | z) is True
  525. assert is_cnf(x & y & z) is True
  526. assert is_cnf((x | y) & z) is True
  527. assert is_cnf((x & y) | z) is False
  528. assert is_cnf(~(x & y) | z) is False
  529. def test_is_dnf():
  530. assert is_dnf(x) is True
  531. assert is_dnf(x | y | z) is True
  532. assert is_dnf(x & y & z) is True
  533. assert is_dnf((x & y) | z) is True
  534. assert is_dnf((x | y) & z) is False
  535. assert is_dnf(~(x | y) & z) is False
  536. def test_ITE():
  537. A, B, C = symbols('A:C')
  538. assert ITE(True, False, True) is false
  539. assert ITE(True, True, False) is true
  540. assert ITE(False, True, False) is false
  541. assert ITE(False, False, True) is true
  542. assert isinstance(ITE(A, B, C), ITE)
  543. A = True
  544. assert ITE(A, B, C) == B
  545. A = False
  546. assert ITE(A, B, C) == C
  547. B = True
  548. assert ITE(And(A, B), B, C) == C
  549. assert ITE(Or(A, False), And(B, True), False) is false
  550. assert ITE(x, A, B) == Not(x)
  551. assert ITE(x, B, A) == x
  552. assert ITE(1, x, y) == x
  553. assert ITE(0, x, y) == y
  554. raises(TypeError, lambda: ITE(2, x, y))
  555. raises(TypeError, lambda: ITE(1, [], y))
  556. raises(TypeError, lambda: ITE(1, (), y))
  557. raises(TypeError, lambda: ITE(1, y, []))
  558. assert ITE(1, 1, 1) is S.true
  559. assert isinstance(ITE(1, 1, 1, evaluate=False), ITE)
  560. raises(TypeError, lambda: ITE(x > 1, y, x))
  561. assert ITE(Eq(x, True), y, x) == ITE(x, y, x)
  562. assert ITE(Eq(x, False), y, x) == ITE(~x, y, x)
  563. assert ITE(Ne(x, True), y, x) == ITE(~x, y, x)
  564. assert ITE(Ne(x, False), y, x) == ITE(x, y, x)
  565. assert ITE(Eq(S. true, x), y, x) == ITE(x, y, x)
  566. assert ITE(Eq(S.false, x), y, x) == ITE(~x, y, x)
  567. assert ITE(Ne(S.true, x), y, x) == ITE(~x, y, x)
  568. assert ITE(Ne(S.false, x), y, x) == ITE(x, y, x)
  569. # 0 and 1 in the context are not treated as True/False
  570. # so the equality must always be False since dissimilar
  571. # objects cannot be equal
  572. assert ITE(Eq(x, 0), y, x) == x
  573. assert ITE(Eq(x, 1), y, x) == x
  574. assert ITE(Ne(x, 0), y, x) == y
  575. assert ITE(Ne(x, 1), y, x) == y
  576. assert ITE(Eq(x, 0), y, z).subs(x, 0) == y
  577. assert ITE(Eq(x, 0), y, z).subs(x, 1) == z
  578. raises(ValueError, lambda: ITE(x > 1, y, x, z))
  579. def test_is_literal():
  580. assert is_literal(True) is True
  581. assert is_literal(False) is True
  582. assert is_literal(A) is True
  583. assert is_literal(~A) is True
  584. assert is_literal(Or(A, B)) is False
  585. assert is_literal(Q.zero(A)) is True
  586. assert is_literal(Not(Q.zero(A))) is True
  587. assert is_literal(Or(A, B)) is False
  588. assert is_literal(And(Q.zero(A), Q.zero(B))) is False
  589. assert is_literal(x < 3)
  590. assert not is_literal(x + y < 3)
  591. def test_operators():
  592. # Mostly test __and__, __rand__, and so on
  593. assert True & A == A & True == A
  594. assert False & A == A & False == False
  595. assert A & B == And(A, B)
  596. assert True | A == A | True == True
  597. assert False | A == A | False == A
  598. assert A | B == Or(A, B)
  599. assert ~A == Not(A)
  600. assert True >> A == A << True == A
  601. assert False >> A == A << False == True
  602. assert A >> True == True << A == True
  603. assert A >> False == False << A == ~A
  604. assert A >> B == B << A == Implies(A, B)
  605. assert True ^ A == A ^ True == ~A
  606. assert False ^ A == A ^ False == A
  607. assert A ^ B == Xor(A, B)
  608. def test_true_false():
  609. assert true is S.true
  610. assert false is S.false
  611. assert true is not True
  612. assert false is not False
  613. assert true
  614. assert not false
  615. assert true == True
  616. assert false == False
  617. assert not (true == False)
  618. assert not (false == True)
  619. assert not (true == false)
  620. assert hash(true) == hash(True)
  621. assert hash(false) == hash(False)
  622. assert len({true, True}) == len({false, False}) == 1
  623. assert isinstance(true, BooleanAtom)
  624. assert isinstance(false, BooleanAtom)
  625. # We don't want to subclass from bool, because bool subclasses from
  626. # int. But operators like &, |, ^, <<, >>, and ~ act differently on 0 and
  627. # 1 then we want them to on true and false. See the docstrings of the
  628. # various And, Or, etc. functions for examples.
  629. assert not isinstance(true, bool)
  630. assert not isinstance(false, bool)
  631. # Note: using 'is' comparison is important here. We want these to return
  632. # true and false, not True and False
  633. assert Not(true) is false
  634. assert Not(True) is false
  635. assert Not(false) is true
  636. assert Not(False) is true
  637. assert ~true is false
  638. assert ~false is true
  639. for T, F in product((True, true), (False, false)):
  640. assert And(T, F) is false
  641. assert And(F, T) is false
  642. assert And(F, F) is false
  643. assert And(T, T) is true
  644. assert And(T, x) == x
  645. assert And(F, x) is false
  646. if not (T is True and F is False):
  647. assert T & F is false
  648. assert F & T is false
  649. if F is not False:
  650. assert F & F is false
  651. if T is not True:
  652. assert T & T is true
  653. assert Or(T, F) is true
  654. assert Or(F, T) is true
  655. assert Or(F, F) is false
  656. assert Or(T, T) is true
  657. assert Or(T, x) is true
  658. assert Or(F, x) == x
  659. if not (T is True and F is False):
  660. assert T | F is true
  661. assert F | T is true
  662. if F is not False:
  663. assert F | F is false
  664. if T is not True:
  665. assert T | T is true
  666. assert Xor(T, F) is true
  667. assert Xor(F, T) is true
  668. assert Xor(F, F) is false
  669. assert Xor(T, T) is false
  670. assert Xor(T, x) == ~x
  671. assert Xor(F, x) == x
  672. if not (T is True and F is False):
  673. assert T ^ F is true
  674. assert F ^ T is true
  675. if F is not False:
  676. assert F ^ F is false
  677. if T is not True:
  678. assert T ^ T is false
  679. assert Nand(T, F) is true
  680. assert Nand(F, T) is true
  681. assert Nand(F, F) is true
  682. assert Nand(T, T) is false
  683. assert Nand(T, x) == ~x
  684. assert Nand(F, x) is true
  685. assert Nor(T, F) is false
  686. assert Nor(F, T) is false
  687. assert Nor(F, F) is true
  688. assert Nor(T, T) is false
  689. assert Nor(T, x) is false
  690. assert Nor(F, x) == ~x
  691. assert Implies(T, F) is false
  692. assert Implies(F, T) is true
  693. assert Implies(F, F) is true
  694. assert Implies(T, T) is true
  695. assert Implies(T, x) == x
  696. assert Implies(F, x) is true
  697. assert Implies(x, T) is true
  698. assert Implies(x, F) == ~x
  699. if not (T is True and F is False):
  700. assert T >> F is false
  701. assert F << T is false
  702. assert F >> T is true
  703. assert T << F is true
  704. if F is not False:
  705. assert F >> F is true
  706. assert F << F is true
  707. if T is not True:
  708. assert T >> T is true
  709. assert T << T is true
  710. assert Equivalent(T, F) is false
  711. assert Equivalent(F, T) is false
  712. assert Equivalent(F, F) is true
  713. assert Equivalent(T, T) is true
  714. assert Equivalent(T, x) == x
  715. assert Equivalent(F, x) == ~x
  716. assert Equivalent(x, T) == x
  717. assert Equivalent(x, F) == ~x
  718. assert ITE(T, T, T) is true
  719. assert ITE(T, T, F) is true
  720. assert ITE(T, F, T) is false
  721. assert ITE(T, F, F) is false
  722. assert ITE(F, T, T) is true
  723. assert ITE(F, T, F) is false
  724. assert ITE(F, F, T) is true
  725. assert ITE(F, F, F) is false
  726. assert all(i.simplify(1, 2) is i for i in (S.true, S.false))
  727. def test_bool_as_set():
  728. assert ITE(y <= 0, False, y >= 1).as_set() == Interval(1, oo)
  729. assert And(x <= 2, x >= -2).as_set() == Interval(-2, 2)
  730. assert Or(x >= 2, x <= -2).as_set() == Interval(-oo, -2) + Interval(2, oo)
  731. assert Not(x > 2).as_set() == Interval(-oo, 2)
  732. # issue 10240
  733. assert Not(And(x > 2, x < 3)).as_set() == \
  734. Union(Interval(-oo, 2), Interval(3, oo))
  735. assert true.as_set() == S.UniversalSet
  736. assert false.as_set() is S.EmptySet
  737. assert x.as_set() == S.UniversalSet
  738. assert And(Or(x < 1, x > 3), x < 2).as_set() == Interval.open(-oo, 1)
  739. assert And(x < 1, sin(x) < 3).as_set() == (x < 1).as_set()
  740. raises(NotImplementedError, lambda: (sin(x) < 1).as_set())
  741. # watch for object morph in as_set
  742. assert Eq(-1, cos(2*x)**2/sin(2*x)**2).as_set() is S.EmptySet
  743. @XFAIL
  744. def test_multivariate_bool_as_set():
  745. x, y = symbols('x,y')
  746. assert And(x >= 0, y >= 0).as_set() == Interval(0, oo)*Interval(0, oo)
  747. assert Or(x >= 0, y >= 0).as_set() == S.Reals*S.Reals - \
  748. Interval(-oo, 0, True, True)*Interval(-oo, 0, True, True)
  749. def test_all_or_nothing():
  750. x = symbols('x', extended_real=True)
  751. args = x >= -oo, x <= oo
  752. v = And(*args)
  753. if v.func is And:
  754. assert len(v.args) == len(args) - args.count(S.true)
  755. else:
  756. assert v == True
  757. v = Or(*args)
  758. if v.func is Or:
  759. assert len(v.args) == 2
  760. else:
  761. assert v == True
  762. def test_canonical_atoms():
  763. assert true.canonical == true
  764. assert false.canonical == false
  765. def test_negated_atoms():
  766. assert true.negated == false
  767. assert false.negated == true
  768. def test_issue_8777():
  769. assert And(x > 2, x < oo).as_set() == Interval(2, oo, left_open=True)
  770. assert And(x >= 1, x < oo).as_set() == Interval(1, oo)
  771. assert (x < oo).as_set() == Interval(-oo, oo)
  772. assert (x > -oo).as_set() == Interval(-oo, oo)
  773. def test_issue_8975():
  774. assert Or(And(-oo < x, x <= -2), And(2 <= x, x < oo)).as_set() == \
  775. Interval(-oo, -2) + Interval(2, oo)
  776. def test_term_to_integer():
  777. assert term_to_integer([1, 0, 1, 0, 0, 1, 0]) == 82
  778. assert term_to_integer('0010101000111001') == 10809
  779. def test_issue_21971():
  780. a, b, c, d = symbols('a b c d')
  781. f = a & b & c | a & c
  782. assert f.subs(a & c, d) == b & d | d
  783. assert f.subs(a & b & c, d) == a & c | d
  784. f = (a | b | c) & (a | c)
  785. assert f.subs(a | c, d) == (b | d) & d
  786. assert f.subs(a | b | c, d) == (a | c) & d
  787. f = (a ^ b ^ c) & (a ^ c)
  788. assert f.subs(a ^ c, d) == (b ^ d) & d
  789. assert f.subs(a ^ b ^ c, d) == (a ^ c) & d
  790. def test_truth_table():
  791. assert list(truth_table(And(x, y), [x, y], input=False)) == \
  792. [False, False, False, True]
  793. assert list(truth_table(x | y, [x, y], input=False)) == \
  794. [False, True, True, True]
  795. assert list(truth_table(x >> y, [x, y], input=False)) == \
  796. [True, True, False, True]
  797. assert list(truth_table(And(x, y), [x, y])) == \
  798. [([0, 0], False), ([0, 1], False), ([1, 0], False), ([1, 1], True)]
  799. def test_issue_8571():
  800. for t in (S.true, S.false):
  801. raises(TypeError, lambda: +t)
  802. raises(TypeError, lambda: -t)
  803. raises(TypeError, lambda: abs(t))
  804. # use int(bool(t)) to get 0 or 1
  805. raises(TypeError, lambda: int(t))
  806. for o in [S.Zero, S.One, x]:
  807. for _ in range(2):
  808. raises(TypeError, lambda: o + t)
  809. raises(TypeError, lambda: o - t)
  810. raises(TypeError, lambda: o % t)
  811. raises(TypeError, lambda: o*t)
  812. raises(TypeError, lambda: o/t)
  813. raises(TypeError, lambda: o**t)
  814. o, t = t, o # do again in reversed order
  815. def test_expand_relational():
  816. n = symbols('n', negative=True)
  817. p, q = symbols('p q', positive=True)
  818. r = ((n + q*(-n/q + 1))/(q*(-n/q + 1)) < 0)
  819. assert r is not S.false
  820. assert r.expand() is S.false
  821. assert (q > 0).expand() is S.true
  822. def test_issue_12717():
  823. assert S.true.is_Atom == True
  824. assert S.false.is_Atom == True
  825. def test_as_Boolean():
  826. nz = symbols('nz', nonzero=True)
  827. assert all(as_Boolean(i) is S.true for i in (True, S.true, 1, nz))
  828. z = symbols('z', zero=True)
  829. assert all(as_Boolean(i) is S.false for i in (False, S.false, 0, z))
  830. assert all(as_Boolean(i) == i for i in (x, x < 0))
  831. for i in (2, S(2), x + 1, []):
  832. raises(TypeError, lambda: as_Boolean(i))
  833. def test_binary_symbols():
  834. assert ITE(x < 1, y, z).binary_symbols == {y, z}
  835. for f in (Eq, Ne):
  836. assert f(x, 1).binary_symbols == set()
  837. assert f(x, True).binary_symbols == {x}
  838. assert f(x, False).binary_symbols == {x}
  839. assert S.true.binary_symbols == set()
  840. assert S.false.binary_symbols == set()
  841. assert x.binary_symbols == {x}
  842. assert And(x, Eq(y, False), Eq(z, 1)).binary_symbols == {x, y}
  843. assert Q.prime(x).binary_symbols == set()
  844. assert Q.lt(x, 1).binary_symbols == set()
  845. assert Q.is_true(x).binary_symbols == {x}
  846. assert Q.eq(x, True).binary_symbols == {x}
  847. assert Q.prime(x).binary_symbols == set()
  848. def test_BooleanFunction_diff():
  849. assert And(x, y).diff(x) == Piecewise((0, Eq(y, False)), (1, True))
  850. def test_issue_14700():
  851. A, B, C, D, E, F, G, H = symbols('A B C D E F G H')
  852. q = ((B & D & H & ~F) | (B & H & ~C & ~D) | (B & H & ~C & ~F) |
  853. (B & H & ~D & ~G) | (B & H & ~F & ~G) | (C & G & ~B & ~D) |
  854. (C & G & ~D & ~H) | (C & G & ~F & ~H) | (D & F & H & ~B) |
  855. (D & F & ~G & ~H) | (B & D & F & ~C & ~H) | (D & E & F & ~B & ~C) |
  856. (D & F & ~A & ~B & ~C) | (D & F & ~A & ~C & ~H) |
  857. (A & B & D & F & ~E & ~H))
  858. soldnf = ((B & D & H & ~F) | (D & F & H & ~B) | (B & H & ~C & ~D) |
  859. (B & H & ~D & ~G) | (C & G & ~B & ~D) | (C & G & ~D & ~H) |
  860. (C & G & ~F & ~H) | (D & F & ~G & ~H) | (D & E & F & ~C & ~H) |
  861. (D & F & ~A & ~C & ~H) | (A & B & D & F & ~E & ~H))
  862. solcnf = ((B | C | D) & (B | D | G) & (C | D | H) & (C | F | H) &
  863. (D | G | H) & (F | G | H) & (B | F | ~D | ~H) &
  864. (~B | ~D | ~F | ~H) & (D | ~B | ~C | ~G | ~H) &
  865. (A | H | ~C | ~D | ~F | ~G) & (H | ~C | ~D | ~E | ~F | ~G) &
  866. (B | E | H | ~A | ~D | ~F | ~G))
  867. assert simplify_logic(q, "dnf") == soldnf
  868. assert simplify_logic(q, "cnf") == solcnf
  869. minterms = [[0, 1, 0, 0], [0, 1, 0, 1], [0, 1, 1, 0], [0, 1, 1, 1],
  870. [0, 0, 1, 1], [1, 0, 1, 1]]
  871. dontcares = [[1, 0, 0, 0], [1, 0, 0, 1], [1, 1, 0, 0], [1, 1, 0, 1]]
  872. assert SOPform([w, x, y, z], minterms) == (x & ~w) | (y & z & ~x)
  873. # Should not be more complicated with don't cares
  874. assert SOPform([w, x, y, z], minterms, dontcares) == \
  875. (x & ~w) | (y & z & ~x)
  876. def test_relational_simplification():
  877. w, x, y, z = symbols('w x y z', real=True)
  878. d, e = symbols('d e', real=False)
  879. # Test all combinations or sign and order
  880. assert Or(x >= y, x < y).simplify() == S.true
  881. assert Or(x >= y, y > x).simplify() == S.true
  882. assert Or(x >= y, -x > -y).simplify() == S.true
  883. assert Or(x >= y, -y < -x).simplify() == S.true
  884. assert Or(-x <= -y, x < y).simplify() == S.true
  885. assert Or(-x <= -y, -x > -y).simplify() == S.true
  886. assert Or(-x <= -y, y > x).simplify() == S.true
  887. assert Or(-x <= -y, -y < -x).simplify() == S.true
  888. assert Or(y <= x, x < y).simplify() == S.true
  889. assert Or(y <= x, y > x).simplify() == S.true
  890. assert Or(y <= x, -x > -y).simplify() == S.true
  891. assert Or(y <= x, -y < -x).simplify() == S.true
  892. assert Or(-y >= -x, x < y).simplify() == S.true
  893. assert Or(-y >= -x, y > x).simplify() == S.true
  894. assert Or(-y >= -x, -x > -y).simplify() == S.true
  895. assert Or(-y >= -x, -y < -x).simplify() == S.true
  896. assert Or(x < y, x >= y).simplify() == S.true
  897. assert Or(y > x, x >= y).simplify() == S.true
  898. assert Or(-x > -y, x >= y).simplify() == S.true
  899. assert Or(-y < -x, x >= y).simplify() == S.true
  900. assert Or(x < y, -x <= -y).simplify() == S.true
  901. assert Or(-x > -y, -x <= -y).simplify() == S.true
  902. assert Or(y > x, -x <= -y).simplify() == S.true
  903. assert Or(-y < -x, -x <= -y).simplify() == S.true
  904. assert Or(x < y, y <= x).simplify() == S.true
  905. assert Or(y > x, y <= x).simplify() == S.true
  906. assert Or(-x > -y, y <= x).simplify() == S.true
  907. assert Or(-y < -x, y <= x).simplify() == S.true
  908. assert Or(x < y, -y >= -x).simplify() == S.true
  909. assert Or(y > x, -y >= -x).simplify() == S.true
  910. assert Or(-x > -y, -y >= -x).simplify() == S.true
  911. assert Or(-y < -x, -y >= -x).simplify() == S.true
  912. # Some other tests
  913. assert Or(x >= y, w < z, x <= y).simplify() == S.true
  914. assert And(x >= y, x < y).simplify() == S.false
  915. assert Or(x >= y, Eq(y, x)).simplify() == (x >= y)
  916. assert And(x >= y, Eq(y, x)).simplify() == Eq(x, y)
  917. assert And(Eq(x, y), x >= 1, 2 < y, y >= 5, z < y).simplify() == \
  918. (Eq(x, y) & (x >= 1) & (y >= 5) & (y > z))
  919. assert Or(Eq(x, y), x >= y, w < y, z < y).simplify() == \
  920. (x >= y) | (y > z) | (w < y)
  921. assert And(Eq(x, y), x >= y, w < y, y >= z, z < y).simplify() == \
  922. Eq(x, y) & (y > z) & (w < y)
  923. # assert And(Eq(x, y), x >= y, w < y, y >= z, z < y).simplify(relational_minmax=True) == \
  924. # And(Eq(x, y), y > Max(w, z))
  925. # assert Or(Eq(x, y), x >= 1, 2 < y, y >= 5, z < y).simplify(relational_minmax=True) == \
  926. # (Eq(x, y) | (x >= 1) | (y > Min(2, z)))
  927. assert And(Eq(x, y), x >= 1, 2 < y, y >= 5, z < y).simplify() == \
  928. (Eq(x, y) & (x >= 1) & (y >= 5) & (y > z))
  929. assert (Eq(x, y) & Eq(d, e) & (x >= y) & (d >= e)).simplify() == \
  930. (Eq(x, y) & Eq(d, e) & (d >= e))
  931. assert And(Eq(x, y), Eq(x, -y)).simplify() == And(Eq(x, 0), Eq(y, 0))
  932. assert Xor(x >= y, x <= y).simplify() == Ne(x, y)
  933. assert And(x > 1, x < -1, Eq(x, y)).simplify() == S.false
  934. # From #16690
  935. assert And(x >= y, Eq(y, 0)).simplify() == And(x >= 0, Eq(y, 0))
  936. def test_issue_8373():
  937. x = symbols('x', real=True)
  938. assert Or(x < 1, x > -1).simplify() == S.true
  939. assert Or(x < 1, x >= 1).simplify() == S.true
  940. assert And(x < 1, x >= 1).simplify() == S.false
  941. assert Or(x <= 1, x >= 1).simplify() == S.true
  942. def test_issue_7950():
  943. x = symbols('x', real=True)
  944. assert And(Eq(x, 1), Eq(x, 2)).simplify() == S.false
  945. @slow
  946. def test_relational_simplification_numerically():
  947. def test_simplification_numerically_function(original, simplified):
  948. symb = original.free_symbols
  949. n = len(symb)
  950. valuelist = list(set(list(combinations(list(range(-(n-1), n))*n, n))))
  951. for values in valuelist:
  952. sublist = dict(zip(symb, values))
  953. originalvalue = original.subs(sublist)
  954. simplifiedvalue = simplified.subs(sublist)
  955. assert originalvalue == simplifiedvalue, "Original: {}\nand"\
  956. " simplified: {}\ndo not evaluate to the same value for {}"\
  957. "".format(original, simplified, sublist)
  958. w, x, y, z = symbols('w x y z', real=True)
  959. d, e = symbols('d e', real=False)
  960. expressions = (And(Eq(x, y), x >= y, w < y, y >= z, z < y),
  961. And(Eq(x, y), x >= 1, 2 < y, y >= 5, z < y),
  962. Or(Eq(x, y), x >= 1, 2 < y, y >= 5, z < y),
  963. And(x >= y, Eq(y, x)),
  964. Or(And(Eq(x, y), x >= y, w < y, Or(y >= z, z < y)),
  965. And(Eq(x, y), x >= 1, 2 < y, y >= -1, z < y)),
  966. (Eq(x, y) & Eq(d, e) & (x >= y) & (d >= e)),
  967. )
  968. for expression in expressions:
  969. test_simplification_numerically_function(expression,
  970. expression.simplify())
  971. def test_relational_simplification_patterns_numerically():
  972. from sympy.core import Wild
  973. from sympy.logic.boolalg import _simplify_patterns_and, \
  974. _simplify_patterns_or, _simplify_patterns_xor
  975. a = Wild('a')
  976. b = Wild('b')
  977. c = Wild('c')
  978. symb = [a, b, c]
  979. patternlists = [[And, _simplify_patterns_and()],
  980. [Or, _simplify_patterns_or()],
  981. [Xor, _simplify_patterns_xor()]]
  982. valuelist = list(set(list(combinations(list(range(-2, 3))*3, 3))))
  983. # Skip combinations of +/-2 and 0, except for all 0
  984. valuelist = [v for v in valuelist if any([w % 2 for w in v]) or not any(v)]
  985. for func, patternlist in patternlists:
  986. for pattern in patternlist:
  987. original = func(*pattern[0].args)
  988. simplified = pattern[1]
  989. for values in valuelist:
  990. sublist = dict(zip(symb, values))
  991. originalvalue = original.xreplace(sublist)
  992. simplifiedvalue = simplified.xreplace(sublist)
  993. assert originalvalue == simplifiedvalue, "Original: {}\nand"\
  994. " simplified: {}\ndo not evaluate to the same value for"\
  995. "{}".format(pattern[0], simplified, sublist)
  996. def test_issue_16803():
  997. n = symbols('n')
  998. # No simplification done, but should not raise an exception
  999. assert ((n > 3) | (n < 0) | ((n > 0) & (n < 3))).simplify() == \
  1000. (n > 3) | (n < 0) | ((n > 0) & (n < 3))
  1001. def test_issue_17530():
  1002. r = {x: oo, y: oo}
  1003. assert Or(x + y > 0, x - y < 0).subs(r)
  1004. assert not And(x + y < 0, x - y < 0).subs(r)
  1005. raises(TypeError, lambda: Or(x + y < 0, x - y < 0).subs(r))
  1006. raises(TypeError, lambda: And(x + y > 0, x - y < 0).subs(r))
  1007. raises(TypeError, lambda: And(x + y > 0, x - y < 0).subs(r))
  1008. def test_anf_coeffs():
  1009. assert anf_coeffs([1, 0]) == [1, 1]
  1010. assert anf_coeffs([0, 0, 0, 1]) == [0, 0, 0, 1]
  1011. assert anf_coeffs([0, 1, 1, 1]) == [0, 1, 1, 1]
  1012. assert anf_coeffs([1, 1, 1, 0]) == [1, 0, 0, 1]
  1013. assert anf_coeffs([1, 0, 0, 0]) == [1, 1, 1, 1]
  1014. assert anf_coeffs([1, 0, 0, 1]) == [1, 1, 1, 0]
  1015. assert anf_coeffs([1, 1, 0, 1]) == [1, 0, 1, 1]
  1016. def test_ANFform():
  1017. x, y = symbols('x,y')
  1018. assert ANFform([x], [1, 1]) == True
  1019. assert ANFform([x], [0, 0]) == False
  1020. assert ANFform([x], [1, 0]) == Xor(x, True, remove_true=False)
  1021. assert ANFform([x, y], [1, 1, 1, 0]) == \
  1022. Xor(True, And(x, y), remove_true=False)
  1023. def test_bool_minterm():
  1024. x, y = symbols('x,y')
  1025. assert bool_minterm(3, [x, y]) == And(x, y)
  1026. assert bool_minterm([1, 0], [x, y]) == And(Not(y), x)
  1027. def test_bool_maxterm():
  1028. x, y = symbols('x,y')
  1029. assert bool_maxterm(2, [x, y]) == Or(Not(x), y)
  1030. assert bool_maxterm([0, 1], [x, y]) == Or(Not(y), x)
  1031. def test_bool_monomial():
  1032. x, y = symbols('x,y')
  1033. assert bool_monomial(1, [x, y]) == y
  1034. assert bool_monomial([1, 1], [x, y]) == And(x, y)
  1035. def test_check_pair():
  1036. assert _check_pair([0, 1, 0], [0, 1, 1]) == 2
  1037. assert _check_pair([0, 1, 0], [1, 1, 1]) == -1
  1038. def test_issue_19114():
  1039. expr = (B & C) | (A & ~C) | (~A & ~B)
  1040. # Expression is minimal, but there are multiple minimal forms possible
  1041. res1 = (A & B) | (C & ~A) | (~B & ~C)
  1042. result = to_dnf(expr, simplify=True)
  1043. assert result in (expr, res1)
  1044. def test_issue_20870():
  1045. result = SOPform([a, b, c, d], [1, 2, 3, 4, 5, 6, 8, 9, 11, 12, 14, 15])
  1046. expected = ((d & ~b) | (a & b & c) | (a & ~c & ~d) |
  1047. (b & ~a & ~c) | (c & ~a & ~d))
  1048. assert result == expected
  1049. def test_convert_to_varsSOP():
  1050. assert _convert_to_varsSOP([0, 1, 0], [x, y, z]) == And(Not(x), y, Not(z))
  1051. assert _convert_to_varsSOP([3, 1, 0], [x, y, z]) == And(y, Not(z))
  1052. def test_convert_to_varsPOS():
  1053. assert _convert_to_varsPOS([0, 1, 0], [x, y, z]) == Or(x, Not(y), z)
  1054. assert _convert_to_varsPOS([3, 1, 0], [x, y, z]) == Or(Not(y), z)
  1055. def test_gateinputcount():
  1056. a, b, c, d, e = symbols('a:e')
  1057. assert gateinputcount(And(a, b)) == 2
  1058. assert gateinputcount(a | b & c & d ^ (e | a)) == 9
  1059. assert gateinputcount(And(a, True)) == 0
  1060. raises(TypeError, lambda: gateinputcount(a*b))
  1061. def test_refine():
  1062. # relational
  1063. assert not refine(x < 0, ~(x < 0))
  1064. assert refine(x < 0, (x < 0))
  1065. assert refine(x < 0, (0 > x)) is S.true
  1066. assert refine(x < 0, (y < 0)) == (x < 0)
  1067. assert not refine(x <= 0, ~(x <= 0))
  1068. assert refine(x <= 0, (x <= 0))
  1069. assert refine(x <= 0, (0 >= x)) is S.true
  1070. assert refine(x <= 0, (y <= 0)) == (x <= 0)
  1071. assert not refine(x > 0, ~(x > 0))
  1072. assert refine(x > 0, (x > 0))
  1073. assert refine(x > 0, (0 < x)) is S.true
  1074. assert refine(x > 0, (y > 0)) == (x > 0)
  1075. assert not refine(x >= 0, ~(x >= 0))
  1076. assert refine(x >= 0, (x >= 0))
  1077. assert refine(x >= 0, (0 <= x)) is S.true
  1078. assert refine(x >= 0, (y >= 0)) == (x >= 0)
  1079. assert not refine(Eq(x, 0), ~(Eq(x, 0)))
  1080. assert refine(Eq(x, 0), (Eq(x, 0)))
  1081. assert refine(Eq(x, 0), (Eq(0, x))) is S.true
  1082. assert refine(Eq(x, 0), (Eq(y, 0))) == Eq(x, 0)
  1083. assert not refine(Ne(x, 0), ~(Ne(x, 0)))
  1084. assert refine(Ne(x, 0), (Ne(0, x))) is S.true
  1085. assert refine(Ne(x, 0), (Ne(x, 0)))
  1086. assert refine(Ne(x, 0), (Ne(y, 0))) == (Ne(x, 0))
  1087. # boolean functions
  1088. assert refine(And(x > 0, y > 0), (x > 0)) == (y > 0)
  1089. assert refine(And(x > 0, y > 0), (x > 0) & (y > 0)) is S.true
  1090. # predicates
  1091. assert refine(Q.positive(x), Q.positive(x)) is S.true
  1092. assert refine(Q.positive(x), Q.negative(x)) is S.false
  1093. assert refine(Q.positive(x), Q.real(x)) == Q.positive(x)
  1094. def test_relational_threeterm_simplification_patterns_numerically():
  1095. from sympy.core import Wild
  1096. from sympy.logic.boolalg import _simplify_patterns_and3
  1097. a = Wild('a')
  1098. b = Wild('b')
  1099. c = Wild('c')
  1100. symb = [a, b, c]
  1101. patternlists = [[And, _simplify_patterns_and3()]]
  1102. valuelist = list(set(list(combinations(list(range(-2, 3))*3, 3))))
  1103. # Skip combinations of +/-2 and 0, except for all 0
  1104. valuelist = [v for v in valuelist if any([w % 2 for w in v]) or not any(v)]
  1105. for func, patternlist in patternlists:
  1106. for pattern in patternlist:
  1107. original = func(*pattern[0].args)
  1108. simplified = pattern[1]
  1109. for values in valuelist:
  1110. sublist = dict(zip(symb, values))
  1111. originalvalue = original.xreplace(sublist)
  1112. simplifiedvalue = simplified.xreplace(sublist)
  1113. assert originalvalue == simplifiedvalue, "Original: {}\nand"\
  1114. " simplified: {}\ndo not evaluate to the same value for"\
  1115. "{}".format(pattern[0], simplified, sublist)