powerset.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from sympy.core.decorators import _sympifyit
  2. from sympy.core.parameters import global_parameters
  3. from sympy.core.logic import fuzzy_bool
  4. from sympy.core.singleton import S
  5. from sympy.core.sympify import _sympify
  6. from .sets import Set, FiniteSet
  7. class PowerSet(Set):
  8. r"""A symbolic object representing a power set.
  9. Parameters
  10. ==========
  11. arg : Set
  12. The set to take power of.
  13. evaluate : bool
  14. The flag to control evaluation.
  15. If the evaluation is disabled for finite sets, it can take
  16. advantage of using subset test as a membership test.
  17. Notes
  18. =====
  19. Power set `\mathcal{P}(S)` is defined as a set containing all the
  20. subsets of `S`.
  21. If the set `S` is a finite set, its power set would have
  22. `2^{\left| S \right|}` elements, where `\left| S \right|` denotes
  23. the cardinality of `S`.
  24. Examples
  25. ========
  26. >>> from sympy import PowerSet, S, FiniteSet
  27. A power set of a finite set:
  28. >>> PowerSet(FiniteSet(1, 2, 3))
  29. PowerSet({1, 2, 3})
  30. A power set of an empty set:
  31. >>> PowerSet(S.EmptySet)
  32. PowerSet(EmptySet)
  33. >>> PowerSet(PowerSet(S.EmptySet))
  34. PowerSet(PowerSet(EmptySet))
  35. A power set of an infinite set:
  36. >>> PowerSet(S.Reals)
  37. PowerSet(Reals)
  38. Evaluating the power set of a finite set to its explicit form:
  39. >>> PowerSet(FiniteSet(1, 2, 3)).rewrite(FiniteSet)
  40. FiniteSet(EmptySet, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3})
  41. References
  42. ==========
  43. .. [1] https://en.wikipedia.org/wiki/Power_set
  44. .. [2] https://en.wikipedia.org/wiki/Axiom_of_power_set
  45. """
  46. def __new__(cls, arg, evaluate=None):
  47. if evaluate is None:
  48. evaluate=global_parameters.evaluate
  49. arg = _sympify(arg)
  50. if not isinstance(arg, Set):
  51. raise ValueError('{} must be a set.'.format(arg))
  52. return super().__new__(cls, arg)
  53. @property
  54. def arg(self):
  55. return self.args[0]
  56. def _eval_rewrite_as_FiniteSet(self, *args, **kwargs):
  57. arg = self.arg
  58. if arg.is_FiniteSet:
  59. return arg.powerset()
  60. return None
  61. @_sympifyit('other', NotImplemented)
  62. def _contains(self, other):
  63. if not isinstance(other, Set):
  64. return None
  65. return fuzzy_bool(self.arg.is_superset(other))
  66. def _eval_is_subset(self, other):
  67. if isinstance(other, PowerSet):
  68. return self.arg.is_subset(other.arg)
  69. def __len__(self):
  70. return 2 ** len(self.arg)
  71. def __iter__(self):
  72. found = [S.EmptySet]
  73. yield S.EmptySet
  74. for x in self.arg:
  75. temp = []
  76. x = FiniteSet(x)
  77. for y in found:
  78. new = x + y
  79. yield new
  80. temp.append(new)
  81. found.extend(temp)