comparison.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from sympy.core.relational import Eq, is_eq
  2. from sympy.core.basic import Basic
  3. from sympy.core.logic import fuzzy_and, fuzzy_bool
  4. from sympy.logic.boolalg import And
  5. from sympy.multipledispatch import dispatch
  6. from sympy.sets.sets import tfn, ProductSet, Interval, FiniteSet, Set
  7. @dispatch(Interval, FiniteSet) # type:ignore
  8. def _eval_is_eq(lhs, rhs): # noqa: F811
  9. return False
  10. @dispatch(FiniteSet, Interval) # type:ignore
  11. def _eval_is_eq(lhs, rhs): # noqa: F811
  12. return False
  13. @dispatch(Interval, Interval) # type:ignore
  14. def _eval_is_eq(lhs, rhs): # noqa: F811
  15. return And(Eq(lhs.left, rhs.left),
  16. Eq(lhs.right, rhs.right),
  17. lhs.left_open == rhs.left_open,
  18. lhs.right_open == rhs.right_open)
  19. @dispatch(FiniteSet, FiniteSet) # type:ignore
  20. def _eval_is_eq(lhs, rhs): # noqa: F811
  21. def all_in_both():
  22. s_set = set(lhs.args)
  23. o_set = set(rhs.args)
  24. yield fuzzy_and(lhs._contains(e) for e in o_set - s_set)
  25. yield fuzzy_and(rhs._contains(e) for e in s_set - o_set)
  26. return tfn[fuzzy_and(all_in_both())]
  27. @dispatch(ProductSet, ProductSet) # type:ignore
  28. def _eval_is_eq(lhs, rhs): # noqa: F811
  29. if len(lhs.sets) != len(rhs.sets):
  30. return False
  31. eqs = (is_eq(x, y) for x, y in zip(lhs.sets, rhs.sets))
  32. return tfn[fuzzy_and(map(fuzzy_bool, eqs))]
  33. @dispatch(Set, Basic) # type:ignore
  34. def _eval_is_eq(lhs, rhs): # noqa: F811
  35. return False
  36. @dispatch(Set, Set) # type:ignore
  37. def _eval_is_eq(lhs, rhs): # noqa: F811
  38. return tfn[fuzzy_and(a.is_subset(b) for a, b in [(lhs, rhs), (rhs, lhs)])]