sets.py 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749
  1. from typing import Any, Callable
  2. from functools import reduce
  3. from collections import defaultdict
  4. import inspect
  5. from sympy.core.kind import Kind, UndefinedKind, NumberKind
  6. from sympy.core.basic import Basic
  7. from sympy.core.containers import Tuple, TupleKind
  8. from sympy.core.decorators import sympify_method_args, sympify_return
  9. from sympy.core.evalf import EvalfMixin
  10. from sympy.core.expr import Expr
  11. from sympy.core.function import Lambda
  12. from sympy.core.logic import (FuzzyBool, fuzzy_bool, fuzzy_or, fuzzy_and,
  13. fuzzy_not)
  14. from sympy.core.numbers import Float, Integer
  15. from sympy.core.operations import LatticeOp
  16. from sympy.core.parameters import global_parameters
  17. from sympy.core.relational import Eq, Ne, is_lt
  18. from sympy.core.singleton import Singleton, S
  19. from sympy.core.sorting import ordered
  20. from sympy.core.symbol import symbols, Symbol, Dummy, uniquely_named_symbol
  21. from sympy.core.sympify import _sympify, sympify, _sympy_converter
  22. from sympy.functions.elementary.exponential import exp, log
  23. from sympy.functions.elementary.miscellaneous import Max, Min
  24. from sympy.logic.boolalg import And, Or, Not, Xor, true, false
  25. from sympy.utilities.decorator import deprecated
  26. from sympy.utilities.exceptions import sympy_deprecation_warning
  27. from sympy.utilities.iterables import (iproduct, sift, roundrobin, iterable,
  28. subsets)
  29. from sympy.utilities.misc import func_name, filldedent
  30. from mpmath import mpi, mpf
  31. from mpmath.libmp.libmpf import prec_to_dps
  32. tfn = defaultdict(lambda: None, {
  33. True: S.true,
  34. S.true: S.true,
  35. False: S.false,
  36. S.false: S.false})
  37. @sympify_method_args
  38. class Set(Basic, EvalfMixin):
  39. """
  40. The base class for any kind of set.
  41. Explanation
  42. ===========
  43. This is not meant to be used directly as a container of items. It does not
  44. behave like the builtin ``set``; see :class:`FiniteSet` for that.
  45. Real intervals are represented by the :class:`Interval` class and unions of
  46. sets by the :class:`Union` class. The empty set is represented by the
  47. :class:`EmptySet` class and available as a singleton as ``S.EmptySet``.
  48. """
  49. __slots__ = ()
  50. is_number = False
  51. is_iterable = False
  52. is_interval = False
  53. is_FiniteSet = False
  54. is_Interval = False
  55. is_ProductSet = False
  56. is_Union = False
  57. is_Intersection: FuzzyBool = None
  58. is_UniversalSet: FuzzyBool = None
  59. is_Complement: FuzzyBool = None
  60. is_ComplexRegion = False
  61. is_empty: FuzzyBool = None
  62. is_finite_set: FuzzyBool = None
  63. @property # type: ignore
  64. @deprecated(
  65. """
  66. The is_EmptySet attribute of Set objects is deprecated.
  67. Use 's is S.EmptySet" or 's.is_empty' instead.
  68. """,
  69. deprecated_since_version="1.5",
  70. active_deprecations_target="deprecated-is-emptyset",
  71. )
  72. def is_EmptySet(self):
  73. return None
  74. @staticmethod
  75. def _infimum_key(expr):
  76. """
  77. Return infimum (if possible) else S.Infinity.
  78. """
  79. try:
  80. infimum = expr.inf
  81. assert infimum.is_comparable
  82. infimum = infimum.evalf() # issue #18505
  83. except (NotImplementedError,
  84. AttributeError, AssertionError, ValueError):
  85. infimum = S.Infinity
  86. return infimum
  87. def union(self, other):
  88. """
  89. Returns the union of ``self`` and ``other``.
  90. Examples
  91. ========
  92. As a shortcut it is possible to use the ``+`` operator:
  93. >>> from sympy import Interval, FiniteSet
  94. >>> Interval(0, 1).union(Interval(2, 3))
  95. Union(Interval(0, 1), Interval(2, 3))
  96. >>> Interval(0, 1) + Interval(2, 3)
  97. Union(Interval(0, 1), Interval(2, 3))
  98. >>> Interval(1, 2, True, True) + FiniteSet(2, 3)
  99. Union({3}, Interval.Lopen(1, 2))
  100. Similarly it is possible to use the ``-`` operator for set differences:
  101. >>> Interval(0, 2) - Interval(0, 1)
  102. Interval.Lopen(1, 2)
  103. >>> Interval(1, 3) - FiniteSet(2)
  104. Union(Interval.Ropen(1, 2), Interval.Lopen(2, 3))
  105. """
  106. return Union(self, other)
  107. def intersect(self, other):
  108. """
  109. Returns the intersection of 'self' and 'other'.
  110. Examples
  111. ========
  112. >>> from sympy import Interval
  113. >>> Interval(1, 3).intersect(Interval(1, 2))
  114. Interval(1, 2)
  115. >>> from sympy import imageset, Lambda, symbols, S
  116. >>> n, m = symbols('n m')
  117. >>> a = imageset(Lambda(n, 2*n), S.Integers)
  118. >>> a.intersect(imageset(Lambda(m, 2*m + 1), S.Integers))
  119. EmptySet
  120. """
  121. return Intersection(self, other)
  122. def intersection(self, other):
  123. """
  124. Alias for :meth:`intersect()`
  125. """
  126. return self.intersect(other)
  127. def is_disjoint(self, other):
  128. """
  129. Returns True if ``self`` and ``other`` are disjoint.
  130. Examples
  131. ========
  132. >>> from sympy import Interval
  133. >>> Interval(0, 2).is_disjoint(Interval(1, 2))
  134. False
  135. >>> Interval(0, 2).is_disjoint(Interval(3, 4))
  136. True
  137. References
  138. ==========
  139. .. [1] https://en.wikipedia.org/wiki/Disjoint_sets
  140. """
  141. return self.intersect(other) == S.EmptySet
  142. def isdisjoint(self, other):
  143. """
  144. Alias for :meth:`is_disjoint()`
  145. """
  146. return self.is_disjoint(other)
  147. def complement(self, universe):
  148. r"""
  149. The complement of 'self' w.r.t the given universe.
  150. Examples
  151. ========
  152. >>> from sympy import Interval, S
  153. >>> Interval(0, 1).complement(S.Reals)
  154. Union(Interval.open(-oo, 0), Interval.open(1, oo))
  155. >>> Interval(0, 1).complement(S.UniversalSet)
  156. Complement(UniversalSet, Interval(0, 1))
  157. """
  158. return Complement(universe, self)
  159. def _complement(self, other):
  160. # this behaves as other - self
  161. if isinstance(self, ProductSet) and isinstance(other, ProductSet):
  162. # If self and other are disjoint then other - self == self
  163. if len(self.sets) != len(other.sets):
  164. return other
  165. # There can be other ways to represent this but this gives:
  166. # (A x B) - (C x D) = ((A - C) x B) U (A x (B - D))
  167. overlaps = []
  168. pairs = list(zip(self.sets, other.sets))
  169. for n in range(len(pairs)):
  170. sets = (o if i != n else o-s for i, (s, o) in enumerate(pairs))
  171. overlaps.append(ProductSet(*sets))
  172. return Union(*overlaps)
  173. elif isinstance(other, Interval):
  174. if isinstance(self, (Interval, FiniteSet)):
  175. return Intersection(other, self.complement(S.Reals))
  176. elif isinstance(other, Union):
  177. return Union(*(o - self for o in other.args))
  178. elif isinstance(other, Complement):
  179. return Complement(other.args[0], Union(other.args[1], self), evaluate=False)
  180. elif other is S.EmptySet:
  181. return S.EmptySet
  182. elif isinstance(other, FiniteSet):
  183. sifted = sift(other, lambda x: fuzzy_bool(self.contains(x)))
  184. # ignore those that are contained in self
  185. return Union(FiniteSet(*(sifted[False])),
  186. Complement(FiniteSet(*(sifted[None])), self, evaluate=False)
  187. if sifted[None] else S.EmptySet)
  188. def symmetric_difference(self, other):
  189. """
  190. Returns symmetric difference of ``self`` and ``other``.
  191. Examples
  192. ========
  193. >>> from sympy import Interval, S
  194. >>> Interval(1, 3).symmetric_difference(S.Reals)
  195. Union(Interval.open(-oo, 1), Interval.open(3, oo))
  196. >>> Interval(1, 10).symmetric_difference(S.Reals)
  197. Union(Interval.open(-oo, 1), Interval.open(10, oo))
  198. >>> from sympy import S, EmptySet
  199. >>> S.Reals.symmetric_difference(EmptySet)
  200. Reals
  201. References
  202. ==========
  203. .. [1] https://en.wikipedia.org/wiki/Symmetric_difference
  204. """
  205. return SymmetricDifference(self, other)
  206. def _symmetric_difference(self, other):
  207. return Union(Complement(self, other), Complement(other, self))
  208. @property
  209. def inf(self):
  210. """
  211. The infimum of ``self``.
  212. Examples
  213. ========
  214. >>> from sympy import Interval, Union
  215. >>> Interval(0, 1).inf
  216. 0
  217. >>> Union(Interval(0, 1), Interval(2, 3)).inf
  218. 0
  219. """
  220. return self._inf
  221. @property
  222. def _inf(self):
  223. raise NotImplementedError("(%s)._inf" % self)
  224. @property
  225. def sup(self):
  226. """
  227. The supremum of ``self``.
  228. Examples
  229. ========
  230. >>> from sympy import Interval, Union
  231. >>> Interval(0, 1).sup
  232. 1
  233. >>> Union(Interval(0, 1), Interval(2, 3)).sup
  234. 3
  235. """
  236. return self._sup
  237. @property
  238. def _sup(self):
  239. raise NotImplementedError("(%s)._sup" % self)
  240. def contains(self, other):
  241. """
  242. Returns a SymPy value indicating whether ``other`` is contained
  243. in ``self``: ``true`` if it is, ``false`` if it is not, else
  244. an unevaluated ``Contains`` expression (or, as in the case of
  245. ConditionSet and a union of FiniteSet/Intervals, an expression
  246. indicating the conditions for containment).
  247. Examples
  248. ========
  249. >>> from sympy import Interval, S
  250. >>> from sympy.abc import x
  251. >>> Interval(0, 1).contains(0.5)
  252. True
  253. As a shortcut it is possible to use the ``in`` operator, but that
  254. will raise an error unless an affirmative true or false is not
  255. obtained.
  256. >>> Interval(0, 1).contains(x)
  257. (0 <= x) & (x <= 1)
  258. >>> x in Interval(0, 1)
  259. Traceback (most recent call last):
  260. ...
  261. TypeError: did not evaluate to a bool: None
  262. The result of 'in' is a bool, not a SymPy value
  263. >>> 1 in Interval(0, 2)
  264. True
  265. >>> _ is S.true
  266. False
  267. """
  268. from .contains import Contains
  269. other = sympify(other, strict=True)
  270. c = self._contains(other)
  271. if isinstance(c, Contains):
  272. return c
  273. if c is None:
  274. return Contains(other, self, evaluate=False)
  275. b = tfn[c]
  276. if b is None:
  277. return c
  278. return b
  279. def _contains(self, other):
  280. raise NotImplementedError(filldedent('''
  281. (%s)._contains(%s) is not defined. This method, when
  282. defined, will receive a sympified object. The method
  283. should return True, False, None or something that
  284. expresses what must be true for the containment of that
  285. object in self to be evaluated. If None is returned
  286. then a generic Contains object will be returned
  287. by the ``contains`` method.''' % (self, other)))
  288. def is_subset(self, other):
  289. """
  290. Returns True if ``self`` is a subset of ``other``.
  291. Examples
  292. ========
  293. >>> from sympy import Interval
  294. >>> Interval(0, 0.5).is_subset(Interval(0, 1))
  295. True
  296. >>> Interval(0, 1).is_subset(Interval(0, 1, left_open=True))
  297. False
  298. """
  299. if not isinstance(other, Set):
  300. raise ValueError("Unknown argument '%s'" % other)
  301. # Handle the trivial cases
  302. if self == other:
  303. return True
  304. is_empty = self.is_empty
  305. if is_empty is True:
  306. return True
  307. elif fuzzy_not(is_empty) and other.is_empty:
  308. return False
  309. if self.is_finite_set is False and other.is_finite_set:
  310. return False
  311. # Dispatch on subclass rules
  312. ret = self._eval_is_subset(other)
  313. if ret is not None:
  314. return ret
  315. ret = other._eval_is_superset(self)
  316. if ret is not None:
  317. return ret
  318. # Use pairwise rules from multiple dispatch
  319. from sympy.sets.handlers.issubset import is_subset_sets
  320. ret = is_subset_sets(self, other)
  321. if ret is not None:
  322. return ret
  323. # Fall back on computing the intersection
  324. # XXX: We shouldn't do this. A query like this should be handled
  325. # without evaluating new Set objects. It should be the other way round
  326. # so that the intersect method uses is_subset for evaluation.
  327. if self.intersect(other) == self:
  328. return True
  329. def _eval_is_subset(self, other):
  330. '''Returns a fuzzy bool for whether self is a subset of other.'''
  331. return None
  332. def _eval_is_superset(self, other):
  333. '''Returns a fuzzy bool for whether self is a subset of other.'''
  334. return None
  335. # This should be deprecated:
  336. def issubset(self, other):
  337. """
  338. Alias for :meth:`is_subset()`
  339. """
  340. return self.is_subset(other)
  341. def is_proper_subset(self, other):
  342. """
  343. Returns True if ``self`` is a proper subset of ``other``.
  344. Examples
  345. ========
  346. >>> from sympy import Interval
  347. >>> Interval(0, 0.5).is_proper_subset(Interval(0, 1))
  348. True
  349. >>> Interval(0, 1).is_proper_subset(Interval(0, 1))
  350. False
  351. """
  352. if isinstance(other, Set):
  353. return self != other and self.is_subset(other)
  354. else:
  355. raise ValueError("Unknown argument '%s'" % other)
  356. def is_superset(self, other):
  357. """
  358. Returns True if ``self`` is a superset of ``other``.
  359. Examples
  360. ========
  361. >>> from sympy import Interval
  362. >>> Interval(0, 0.5).is_superset(Interval(0, 1))
  363. False
  364. >>> Interval(0, 1).is_superset(Interval(0, 1, left_open=True))
  365. True
  366. """
  367. if isinstance(other, Set):
  368. return other.is_subset(self)
  369. else:
  370. raise ValueError("Unknown argument '%s'" % other)
  371. # This should be deprecated:
  372. def issuperset(self, other):
  373. """
  374. Alias for :meth:`is_superset()`
  375. """
  376. return self.is_superset(other)
  377. def is_proper_superset(self, other):
  378. """
  379. Returns True if ``self`` is a proper superset of ``other``.
  380. Examples
  381. ========
  382. >>> from sympy import Interval
  383. >>> Interval(0, 1).is_proper_superset(Interval(0, 0.5))
  384. True
  385. >>> Interval(0, 1).is_proper_superset(Interval(0, 1))
  386. False
  387. """
  388. if isinstance(other, Set):
  389. return self != other and self.is_superset(other)
  390. else:
  391. raise ValueError("Unknown argument '%s'" % other)
  392. def _eval_powerset(self):
  393. from .powerset import PowerSet
  394. return PowerSet(self)
  395. def powerset(self):
  396. """
  397. Find the Power set of ``self``.
  398. Examples
  399. ========
  400. >>> from sympy import EmptySet, FiniteSet, Interval
  401. A power set of an empty set:
  402. >>> A = EmptySet
  403. >>> A.powerset()
  404. {EmptySet}
  405. A power set of a finite set:
  406. >>> A = FiniteSet(1, 2)
  407. >>> a, b, c = FiniteSet(1), FiniteSet(2), FiniteSet(1, 2)
  408. >>> A.powerset() == FiniteSet(a, b, c, EmptySet)
  409. True
  410. A power set of an interval:
  411. >>> Interval(1, 2).powerset()
  412. PowerSet(Interval(1, 2))
  413. References
  414. ==========
  415. .. [1] https://en.wikipedia.org/wiki/Power_set
  416. """
  417. return self._eval_powerset()
  418. @property
  419. def measure(self):
  420. """
  421. The (Lebesgue) measure of ``self``.
  422. Examples
  423. ========
  424. >>> from sympy import Interval, Union
  425. >>> Interval(0, 1).measure
  426. 1
  427. >>> Union(Interval(0, 1), Interval(2, 3)).measure
  428. 2
  429. """
  430. return self._measure
  431. @property
  432. def kind(self):
  433. """
  434. The kind of a Set
  435. Explanation
  436. ===========
  437. Any :class:`Set` will have kind :class:`SetKind` which is
  438. parametrised by the kind of the elements of the set. For example
  439. most sets are sets of numbers and will have kind
  440. ``SetKind(NumberKind)``. If elements of sets are different in kind than
  441. their kind will ``SetKind(UndefinedKind)``. See
  442. :class:`sympy.core.kind.Kind` for an explanation of the kind system.
  443. Examples
  444. ========
  445. >>> from sympy import Interval, Matrix, FiniteSet, EmptySet, ProductSet, PowerSet
  446. >>> FiniteSet(Matrix([1, 2])).kind
  447. SetKind(MatrixKind(NumberKind))
  448. >>> Interval(1, 2).kind
  449. SetKind(NumberKind)
  450. >>> EmptySet.kind
  451. SetKind()
  452. A :class:`sympy.sets.powerset.PowerSet` is a set of sets:
  453. >>> PowerSet({1, 2, 3}).kind
  454. SetKind(SetKind(NumberKind))
  455. A :class:`ProductSet` represents the set of tuples of elements of
  456. other sets. Its kind is :class:`sympy.core.containers.TupleKind`
  457. parametrised by the kinds of the elements of those sets:
  458. >>> p = ProductSet(FiniteSet(1, 2), FiniteSet(3, 4))
  459. >>> list(p)
  460. [(1, 3), (2, 3), (1, 4), (2, 4)]
  461. >>> p.kind
  462. SetKind(TupleKind(NumberKind, NumberKind))
  463. When all elements of the set do not have same kind, the kind
  464. will be returned as ``SetKind(UndefinedKind)``:
  465. >>> FiniteSet(0, Matrix([1, 2])).kind
  466. SetKind(UndefinedKind)
  467. The kind of the elements of a set are given by the ``element_kind``
  468. attribute of ``SetKind``:
  469. >>> Interval(1, 2).kind.element_kind
  470. NumberKind
  471. See Also
  472. ========
  473. NumberKind
  474. sympy.core.kind.UndefinedKind
  475. sympy.core.containers.TupleKind
  476. MatrixKind
  477. sympy.matrices.expressions.sets.MatrixSet
  478. sympy.sets.conditionset.ConditionSet
  479. Rationals
  480. Naturals
  481. Integers
  482. sympy.sets.fancysets.ImageSet
  483. sympy.sets.fancysets.Range
  484. sympy.sets.fancysets.ComplexRegion
  485. sympy.sets.powerset.PowerSet
  486. sympy.sets.sets.ProductSet
  487. sympy.sets.sets.Interval
  488. sympy.sets.sets.Union
  489. sympy.sets.sets.Intersection
  490. sympy.sets.sets.Complement
  491. sympy.sets.sets.EmptySet
  492. sympy.sets.sets.UniversalSet
  493. sympy.sets.sets.FiniteSet
  494. sympy.sets.sets.SymmetricDifference
  495. sympy.sets.sets.DisjointUnion
  496. """
  497. return self._kind()
  498. @property
  499. def boundary(self):
  500. """
  501. The boundary or frontier of a set.
  502. Explanation
  503. ===========
  504. A point x is on the boundary of a set S if
  505. 1. x is in the closure of S.
  506. I.e. Every neighborhood of x contains a point in S.
  507. 2. x is not in the interior of S.
  508. I.e. There does not exist an open set centered on x contained
  509. entirely within S.
  510. There are the points on the outer rim of S. If S is open then these
  511. points need not actually be contained within S.
  512. For example, the boundary of an interval is its start and end points.
  513. This is true regardless of whether or not the interval is open.
  514. Examples
  515. ========
  516. >>> from sympy import Interval
  517. >>> Interval(0, 1).boundary
  518. {0, 1}
  519. >>> Interval(0, 1, True, False).boundary
  520. {0, 1}
  521. """
  522. return self._boundary
  523. @property
  524. def is_open(self):
  525. """
  526. Property method to check whether a set is open.
  527. Explanation
  528. ===========
  529. A set is open if and only if it has an empty intersection with its
  530. boundary. In particular, a subset A of the reals is open if and only
  531. if each one of its points is contained in an open interval that is a
  532. subset of A.
  533. Examples
  534. ========
  535. >>> from sympy import S
  536. >>> S.Reals.is_open
  537. True
  538. >>> S.Rationals.is_open
  539. False
  540. """
  541. return Intersection(self, self.boundary).is_empty
  542. @property
  543. def is_closed(self):
  544. """
  545. A property method to check whether a set is closed.
  546. Explanation
  547. ===========
  548. A set is closed if its complement is an open set. The closedness of a
  549. subset of the reals is determined with respect to R and its standard
  550. topology.
  551. Examples
  552. ========
  553. >>> from sympy import Interval
  554. >>> Interval(0, 1).is_closed
  555. True
  556. """
  557. return self.boundary.is_subset(self)
  558. @property
  559. def closure(self):
  560. """
  561. Property method which returns the closure of a set.
  562. The closure is defined as the union of the set itself and its
  563. boundary.
  564. Examples
  565. ========
  566. >>> from sympy import S, Interval
  567. >>> S.Reals.closure
  568. Reals
  569. >>> Interval(0, 1).closure
  570. Interval(0, 1)
  571. """
  572. return self + self.boundary
  573. @property
  574. def interior(self):
  575. """
  576. Property method which returns the interior of a set.
  577. The interior of a set S consists all points of S that do not
  578. belong to the boundary of S.
  579. Examples
  580. ========
  581. >>> from sympy import Interval
  582. >>> Interval(0, 1).interior
  583. Interval.open(0, 1)
  584. >>> Interval(0, 1).boundary.interior
  585. EmptySet
  586. """
  587. return self - self.boundary
  588. @property
  589. def _boundary(self):
  590. raise NotImplementedError()
  591. @property
  592. def _measure(self):
  593. raise NotImplementedError("(%s)._measure" % self)
  594. def _kind(self):
  595. return SetKind(UndefinedKind)
  596. def _eval_evalf(self, prec):
  597. dps = prec_to_dps(prec)
  598. return self.func(*[arg.evalf(n=dps) for arg in self.args])
  599. @sympify_return([('other', 'Set')], NotImplemented)
  600. def __add__(self, other):
  601. return self.union(other)
  602. @sympify_return([('other', 'Set')], NotImplemented)
  603. def __or__(self, other):
  604. return self.union(other)
  605. @sympify_return([('other', 'Set')], NotImplemented)
  606. def __and__(self, other):
  607. return self.intersect(other)
  608. @sympify_return([('other', 'Set')], NotImplemented)
  609. def __mul__(self, other):
  610. return ProductSet(self, other)
  611. @sympify_return([('other', 'Set')], NotImplemented)
  612. def __xor__(self, other):
  613. return SymmetricDifference(self, other)
  614. @sympify_return([('exp', Expr)], NotImplemented)
  615. def __pow__(self, exp):
  616. if not (exp.is_Integer and exp >= 0):
  617. raise ValueError("%s: Exponent must be a positive Integer" % exp)
  618. return ProductSet(*[self]*exp)
  619. @sympify_return([('other', 'Set')], NotImplemented)
  620. def __sub__(self, other):
  621. return Complement(self, other)
  622. def __contains__(self, other):
  623. other = _sympify(other)
  624. c = self._contains(other)
  625. b = tfn[c]
  626. if b is None:
  627. # x in y must evaluate to T or F; to entertain a None
  628. # result with Set use y.contains(x)
  629. raise TypeError('did not evaluate to a bool: %r' % c)
  630. return b
  631. class ProductSet(Set):
  632. """
  633. Represents a Cartesian Product of Sets.
  634. Explanation
  635. ===========
  636. Returns a Cartesian product given several sets as either an iterable
  637. or individual arguments.
  638. Can use ``*`` operator on any sets for convenient shorthand.
  639. Examples
  640. ========
  641. >>> from sympy import Interval, FiniteSet, ProductSet
  642. >>> I = Interval(0, 5); S = FiniteSet(1, 2, 3)
  643. >>> ProductSet(I, S)
  644. ProductSet(Interval(0, 5), {1, 2, 3})
  645. >>> (2, 2) in ProductSet(I, S)
  646. True
  647. >>> Interval(0, 1) * Interval(0, 1) # The unit square
  648. ProductSet(Interval(0, 1), Interval(0, 1))
  649. >>> coin = FiniteSet('H', 'T')
  650. >>> set(coin**2)
  651. {(H, H), (H, T), (T, H), (T, T)}
  652. The Cartesian product is not commutative or associative e.g.:
  653. >>> I*S == S*I
  654. False
  655. >>> (I*I)*I == I*(I*I)
  656. False
  657. Notes
  658. =====
  659. - Passes most operations down to the argument sets
  660. References
  661. ==========
  662. .. [1] https://en.wikipedia.org/wiki/Cartesian_product
  663. """
  664. is_ProductSet = True
  665. def __new__(cls, *sets, **assumptions):
  666. if len(sets) == 1 and iterable(sets[0]) and not isinstance(sets[0], (Set, set)):
  667. sympy_deprecation_warning(
  668. """
  669. ProductSet(iterable) is deprecated. Use ProductSet(*iterable) instead.
  670. """,
  671. deprecated_since_version="1.5",
  672. active_deprecations_target="deprecated-productset-iterable",
  673. )
  674. sets = tuple(sets[0])
  675. sets = [sympify(s) for s in sets]
  676. if not all(isinstance(s, Set) for s in sets):
  677. raise TypeError("Arguments to ProductSet should be of type Set")
  678. # Nullary product of sets is *not* the empty set
  679. if len(sets) == 0:
  680. return FiniteSet(())
  681. if S.EmptySet in sets:
  682. return S.EmptySet
  683. return Basic.__new__(cls, *sets, **assumptions)
  684. @property
  685. def sets(self):
  686. return self.args
  687. def flatten(self):
  688. def _flatten(sets):
  689. for s in sets:
  690. if s.is_ProductSet:
  691. yield from _flatten(s.sets)
  692. else:
  693. yield s
  694. return ProductSet(*_flatten(self.sets))
  695. def _contains(self, element):
  696. """
  697. ``in`` operator for ProductSets.
  698. Examples
  699. ========
  700. >>> from sympy import Interval
  701. >>> (2, 3) in Interval(0, 5) * Interval(0, 5)
  702. True
  703. >>> (10, 10) in Interval(0, 5) * Interval(0, 5)
  704. False
  705. Passes operation on to constituent sets
  706. """
  707. if element.is_Symbol:
  708. return None
  709. if not isinstance(element, Tuple) or len(element) != len(self.sets):
  710. return False
  711. return fuzzy_and(s._contains(e) for s, e in zip(self.sets, element))
  712. def as_relational(self, *symbols):
  713. symbols = [_sympify(s) for s in symbols]
  714. if len(symbols) != len(self.sets) or not all(
  715. i.is_Symbol for i in symbols):
  716. raise ValueError(
  717. 'number of symbols must match the number of sets')
  718. return And(*[s.as_relational(i) for s, i in zip(self.sets, symbols)])
  719. @property
  720. def _boundary(self):
  721. return Union(*(ProductSet(*(b + b.boundary if i != j else b.boundary
  722. for j, b in enumerate(self.sets)))
  723. for i, a in enumerate(self.sets)))
  724. @property
  725. def is_iterable(self):
  726. """
  727. A property method which tests whether a set is iterable or not.
  728. Returns True if set is iterable, otherwise returns False.
  729. Examples
  730. ========
  731. >>> from sympy import FiniteSet, Interval
  732. >>> I = Interval(0, 1)
  733. >>> A = FiniteSet(1, 2, 3, 4, 5)
  734. >>> I.is_iterable
  735. False
  736. >>> A.is_iterable
  737. True
  738. """
  739. return all(set.is_iterable for set in self.sets)
  740. def __iter__(self):
  741. """
  742. A method which implements is_iterable property method.
  743. If self.is_iterable returns True (both constituent sets are iterable),
  744. then return the Cartesian Product. Otherwise, raise TypeError.
  745. """
  746. return iproduct(*self.sets)
  747. @property
  748. def is_empty(self):
  749. return fuzzy_or(s.is_empty for s in self.sets)
  750. @property
  751. def is_finite_set(self):
  752. all_finite = fuzzy_and(s.is_finite_set for s in self.sets)
  753. return fuzzy_or([self.is_empty, all_finite])
  754. @property
  755. def _measure(self):
  756. measure = 1
  757. for s in self.sets:
  758. measure *= s.measure
  759. return measure
  760. def _kind(self):
  761. return SetKind(TupleKind(*(i.kind.element_kind for i in self.args)))
  762. def __len__(self):
  763. return reduce(lambda a, b: a*b, (len(s) for s in self.args))
  764. def __bool__(self):
  765. return all(self.sets)
  766. class Interval(Set):
  767. """
  768. Represents a real interval as a Set.
  769. Usage:
  770. Returns an interval with end points ``start`` and ``end``.
  771. For ``left_open=True`` (default ``left_open`` is ``False``) the interval
  772. will be open on the left. Similarly, for ``right_open=True`` the interval
  773. will be open on the right.
  774. Examples
  775. ========
  776. >>> from sympy import Symbol, Interval
  777. >>> Interval(0, 1)
  778. Interval(0, 1)
  779. >>> Interval.Ropen(0, 1)
  780. Interval.Ropen(0, 1)
  781. >>> Interval.Ropen(0, 1)
  782. Interval.Ropen(0, 1)
  783. >>> Interval.Lopen(0, 1)
  784. Interval.Lopen(0, 1)
  785. >>> Interval.open(0, 1)
  786. Interval.open(0, 1)
  787. >>> a = Symbol('a', real=True)
  788. >>> Interval(0, a)
  789. Interval(0, a)
  790. Notes
  791. =====
  792. - Only real end points are supported
  793. - ``Interval(a, b)`` with $a > b$ will return the empty set
  794. - Use the ``evalf()`` method to turn an Interval into an mpmath
  795. ``mpi`` interval instance
  796. References
  797. ==========
  798. .. [1] https://en.wikipedia.org/wiki/Interval_%28mathematics%29
  799. """
  800. is_Interval = True
  801. def __new__(cls, start, end, left_open=False, right_open=False):
  802. start = _sympify(start)
  803. end = _sympify(end)
  804. left_open = _sympify(left_open)
  805. right_open = _sympify(right_open)
  806. if not all(isinstance(a, (type(true), type(false)))
  807. for a in [left_open, right_open]):
  808. raise NotImplementedError(
  809. "left_open and right_open can have only true/false values, "
  810. "got %s and %s" % (left_open, right_open))
  811. # Only allow real intervals
  812. if fuzzy_not(fuzzy_and(i.is_extended_real for i in (start, end, end-start))):
  813. raise ValueError("Non-real intervals are not supported")
  814. # evaluate if possible
  815. if is_lt(end, start):
  816. return S.EmptySet
  817. elif (end - start).is_negative:
  818. return S.EmptySet
  819. if end == start and (left_open or right_open):
  820. return S.EmptySet
  821. if end == start and not (left_open or right_open):
  822. if start is S.Infinity or start is S.NegativeInfinity:
  823. return S.EmptySet
  824. return FiniteSet(end)
  825. # Make sure infinite interval end points are open.
  826. if start is S.NegativeInfinity:
  827. left_open = true
  828. if end is S.Infinity:
  829. right_open = true
  830. if start == S.Infinity or end == S.NegativeInfinity:
  831. return S.EmptySet
  832. return Basic.__new__(cls, start, end, left_open, right_open)
  833. @property
  834. def start(self):
  835. """
  836. The left end point of the interval.
  837. This property takes the same value as the ``inf`` property.
  838. Examples
  839. ========
  840. >>> from sympy import Interval
  841. >>> Interval(0, 1).start
  842. 0
  843. """
  844. return self._args[0]
  845. @property
  846. def end(self):
  847. """
  848. The right end point of the interval.
  849. This property takes the same value as the ``sup`` property.
  850. Examples
  851. ========
  852. >>> from sympy import Interval
  853. >>> Interval(0, 1).end
  854. 1
  855. """
  856. return self._args[1]
  857. @property
  858. def left_open(self):
  859. """
  860. True if interval is left-open.
  861. Examples
  862. ========
  863. >>> from sympy import Interval
  864. >>> Interval(0, 1, left_open=True).left_open
  865. True
  866. >>> Interval(0, 1, left_open=False).left_open
  867. False
  868. """
  869. return self._args[2]
  870. @property
  871. def right_open(self):
  872. """
  873. True if interval is right-open.
  874. Examples
  875. ========
  876. >>> from sympy import Interval
  877. >>> Interval(0, 1, right_open=True).right_open
  878. True
  879. >>> Interval(0, 1, right_open=False).right_open
  880. False
  881. """
  882. return self._args[3]
  883. @classmethod
  884. def open(cls, a, b):
  885. """Return an interval including neither boundary."""
  886. return cls(a, b, True, True)
  887. @classmethod
  888. def Lopen(cls, a, b):
  889. """Return an interval not including the left boundary."""
  890. return cls(a, b, True, False)
  891. @classmethod
  892. def Ropen(cls, a, b):
  893. """Return an interval not including the right boundary."""
  894. return cls(a, b, False, True)
  895. @property
  896. def _inf(self):
  897. return self.start
  898. @property
  899. def _sup(self):
  900. return self.end
  901. @property
  902. def left(self):
  903. return self.start
  904. @property
  905. def right(self):
  906. return self.end
  907. @property
  908. def is_empty(self):
  909. if self.left_open or self.right_open:
  910. cond = self.start >= self.end # One/both bounds open
  911. else:
  912. cond = self.start > self.end # Both bounds closed
  913. return fuzzy_bool(cond)
  914. @property
  915. def is_finite_set(self):
  916. return self.measure.is_zero
  917. def _complement(self, other):
  918. if other == S.Reals:
  919. a = Interval(S.NegativeInfinity, self.start,
  920. True, not self.left_open)
  921. b = Interval(self.end, S.Infinity, not self.right_open, True)
  922. return Union(a, b)
  923. if isinstance(other, FiniteSet):
  924. nums = [m for m in other.args if m.is_number]
  925. if nums == []:
  926. return None
  927. return Set._complement(self, other)
  928. @property
  929. def _boundary(self):
  930. finite_points = [p for p in (self.start, self.end)
  931. if abs(p) != S.Infinity]
  932. return FiniteSet(*finite_points)
  933. def _contains(self, other):
  934. if (not isinstance(other, Expr) or other is S.NaN
  935. or other.is_real is False or other.has(S.ComplexInfinity)):
  936. # if an expression has zoo it will be zoo or nan
  937. # and neither of those is real
  938. return false
  939. if self.start is S.NegativeInfinity and self.end is S.Infinity:
  940. if other.is_real is not None:
  941. return other.is_real
  942. d = Dummy()
  943. return self.as_relational(d).subs(d, other)
  944. def as_relational(self, x):
  945. """Rewrite an interval in terms of inequalities and logic operators."""
  946. x = sympify(x)
  947. if self.right_open:
  948. right = x < self.end
  949. else:
  950. right = x <= self.end
  951. if self.left_open:
  952. left = self.start < x
  953. else:
  954. left = self.start <= x
  955. return And(left, right)
  956. @property
  957. def _measure(self):
  958. return self.end - self.start
  959. def _kind(self):
  960. return SetKind(NumberKind)
  961. def to_mpi(self, prec=53):
  962. return mpi(mpf(self.start._eval_evalf(prec)),
  963. mpf(self.end._eval_evalf(prec)))
  964. def _eval_evalf(self, prec):
  965. return Interval(self.left._evalf(prec), self.right._evalf(prec),
  966. left_open=self.left_open, right_open=self.right_open)
  967. def _is_comparable(self, other):
  968. is_comparable = self.start.is_comparable
  969. is_comparable &= self.end.is_comparable
  970. is_comparable &= other.start.is_comparable
  971. is_comparable &= other.end.is_comparable
  972. return is_comparable
  973. @property
  974. def is_left_unbounded(self):
  975. """Return ``True`` if the left endpoint is negative infinity. """
  976. return self.left is S.NegativeInfinity or self.left == Float("-inf")
  977. @property
  978. def is_right_unbounded(self):
  979. """Return ``True`` if the right endpoint is positive infinity. """
  980. return self.right is S.Infinity or self.right == Float("+inf")
  981. def _eval_Eq(self, other):
  982. if not isinstance(other, Interval):
  983. if isinstance(other, FiniteSet):
  984. return false
  985. elif isinstance(other, Set):
  986. return None
  987. return false
  988. class Union(Set, LatticeOp):
  989. """
  990. Represents a union of sets as a :class:`Set`.
  991. Examples
  992. ========
  993. >>> from sympy import Union, Interval
  994. >>> Union(Interval(1, 2), Interval(3, 4))
  995. Union(Interval(1, 2), Interval(3, 4))
  996. The Union constructor will always try to merge overlapping intervals,
  997. if possible. For example:
  998. >>> Union(Interval(1, 2), Interval(2, 3))
  999. Interval(1, 3)
  1000. See Also
  1001. ========
  1002. Intersection
  1003. References
  1004. ==========
  1005. .. [1] https://en.wikipedia.org/wiki/Union_%28set_theory%29
  1006. """
  1007. is_Union = True
  1008. @property
  1009. def identity(self):
  1010. return S.EmptySet
  1011. @property
  1012. def zero(self):
  1013. return S.UniversalSet
  1014. def __new__(cls, *args, **kwargs):
  1015. evaluate = kwargs.get('evaluate', global_parameters.evaluate)
  1016. # flatten inputs to merge intersections and iterables
  1017. args = _sympify(args)
  1018. # Reduce sets using known rules
  1019. if evaluate:
  1020. args = list(cls._new_args_filter(args))
  1021. return simplify_union(args)
  1022. args = list(ordered(args, Set._infimum_key))
  1023. obj = Basic.__new__(cls, *args)
  1024. obj._argset = frozenset(args)
  1025. return obj
  1026. @property
  1027. def args(self):
  1028. return self._args
  1029. def _complement(self, universe):
  1030. # DeMorgan's Law
  1031. return Intersection(s.complement(universe) for s in self.args)
  1032. @property
  1033. def _inf(self):
  1034. # We use Min so that sup is meaningful in combination with symbolic
  1035. # interval end points.
  1036. return Min(*[set.inf for set in self.args])
  1037. @property
  1038. def _sup(self):
  1039. # We use Max so that sup is meaningful in combination with symbolic
  1040. # end points.
  1041. return Max(*[set.sup for set in self.args])
  1042. @property
  1043. def is_empty(self):
  1044. return fuzzy_and(set.is_empty for set in self.args)
  1045. @property
  1046. def is_finite_set(self):
  1047. return fuzzy_and(set.is_finite_set for set in self.args)
  1048. @property
  1049. def _measure(self):
  1050. # Measure of a union is the sum of the measures of the sets minus
  1051. # the sum of their pairwise intersections plus the sum of their
  1052. # triple-wise intersections minus ... etc...
  1053. # Sets is a collection of intersections and a set of elementary
  1054. # sets which made up those intersections (called "sos" for set of sets)
  1055. # An example element might of this list might be:
  1056. # ( {A,B,C}, A.intersect(B).intersect(C) )
  1057. # Start with just elementary sets ( ({A}, A), ({B}, B), ... )
  1058. # Then get and subtract ( ({A,B}, (A int B), ... ) while non-zero
  1059. sets = [(FiniteSet(s), s) for s in self.args]
  1060. measure = 0
  1061. parity = 1
  1062. while sets:
  1063. # Add up the measure of these sets and add or subtract it to total
  1064. measure += parity * sum(inter.measure for sos, inter in sets)
  1065. # For each intersection in sets, compute the intersection with every
  1066. # other set not already part of the intersection.
  1067. sets = ((sos + FiniteSet(newset), newset.intersect(intersection))
  1068. for sos, intersection in sets for newset in self.args
  1069. if newset not in sos)
  1070. # Clear out sets with no measure
  1071. sets = [(sos, inter) for sos, inter in sets if inter.measure != 0]
  1072. # Clear out duplicates
  1073. sos_list = []
  1074. sets_list = []
  1075. for _set in sets:
  1076. if _set[0] in sos_list:
  1077. continue
  1078. else:
  1079. sos_list.append(_set[0])
  1080. sets_list.append(_set)
  1081. sets = sets_list
  1082. # Flip Parity - next time subtract/add if we added/subtracted here
  1083. parity *= -1
  1084. return measure
  1085. def _kind(self):
  1086. kinds = tuple(arg.kind for arg in self.args if arg is not S.EmptySet)
  1087. if not kinds:
  1088. return SetKind()
  1089. elif all(i == kinds[0] for i in kinds):
  1090. return kinds[0]
  1091. else:
  1092. return SetKind(UndefinedKind)
  1093. @property
  1094. def _boundary(self):
  1095. def boundary_of_set(i):
  1096. """ The boundary of set i minus interior of all other sets """
  1097. b = self.args[i].boundary
  1098. for j, a in enumerate(self.args):
  1099. if j != i:
  1100. b = b - a.interior
  1101. return b
  1102. return Union(*map(boundary_of_set, range(len(self.args))))
  1103. def _contains(self, other):
  1104. return Or(*[s.contains(other) for s in self.args])
  1105. def is_subset(self, other):
  1106. return fuzzy_and(s.is_subset(other) for s in self.args)
  1107. def as_relational(self, symbol):
  1108. """Rewrite a Union in terms of equalities and logic operators. """
  1109. if (len(self.args) == 2 and
  1110. all(isinstance(i, Interval) for i in self.args)):
  1111. # optimization to give 3 args as (x > 1) & (x < 5) & Ne(x, 3)
  1112. # instead of as 4, ((1 <= x) & (x < 3)) | ((x <= 5) & (3 < x))
  1113. # XXX: This should be ideally be improved to handle any number of
  1114. # intervals and also not to assume that the intervals are in any
  1115. # particular sorted order.
  1116. a, b = self.args
  1117. if a.sup == b.inf and a.right_open and b.left_open:
  1118. mincond = symbol > a.inf if a.left_open else symbol >= a.inf
  1119. maxcond = symbol < b.sup if b.right_open else symbol <= b.sup
  1120. necond = Ne(symbol, a.sup)
  1121. return And(necond, mincond, maxcond)
  1122. return Or(*[i.as_relational(symbol) for i in self.args])
  1123. @property
  1124. def is_iterable(self):
  1125. return all(arg.is_iterable for arg in self.args)
  1126. def __iter__(self):
  1127. return roundrobin(*(iter(arg) for arg in self.args))
  1128. class Intersection(Set, LatticeOp):
  1129. """
  1130. Represents an intersection of sets as a :class:`Set`.
  1131. Examples
  1132. ========
  1133. >>> from sympy import Intersection, Interval
  1134. >>> Intersection(Interval(1, 3), Interval(2, 4))
  1135. Interval(2, 3)
  1136. We often use the .intersect method
  1137. >>> Interval(1,3).intersect(Interval(2,4))
  1138. Interval(2, 3)
  1139. See Also
  1140. ========
  1141. Union
  1142. References
  1143. ==========
  1144. .. [1] https://en.wikipedia.org/wiki/Intersection_%28set_theory%29
  1145. """
  1146. is_Intersection = True
  1147. @property
  1148. def identity(self):
  1149. return S.UniversalSet
  1150. @property
  1151. def zero(self):
  1152. return S.EmptySet
  1153. def __new__(cls, *args, **kwargs):
  1154. evaluate = kwargs.get('evaluate', global_parameters.evaluate)
  1155. # flatten inputs to merge intersections and iterables
  1156. args = list(ordered(set(_sympify(args))))
  1157. # Reduce sets using known rules
  1158. if evaluate:
  1159. args = list(cls._new_args_filter(args))
  1160. return simplify_intersection(args)
  1161. args = list(ordered(args, Set._infimum_key))
  1162. obj = Basic.__new__(cls, *args)
  1163. obj._argset = frozenset(args)
  1164. return obj
  1165. @property
  1166. def args(self):
  1167. return self._args
  1168. @property
  1169. def is_iterable(self):
  1170. return any(arg.is_iterable for arg in self.args)
  1171. @property
  1172. def is_finite_set(self):
  1173. if fuzzy_or(arg.is_finite_set for arg in self.args):
  1174. return True
  1175. def _kind(self):
  1176. kinds = tuple(arg.kind for arg in self.args if arg is not S.UniversalSet)
  1177. if not kinds:
  1178. return SetKind(UndefinedKind)
  1179. elif all(i == kinds[0] for i in kinds):
  1180. return kinds[0]
  1181. else:
  1182. return SetKind()
  1183. @property
  1184. def _inf(self):
  1185. raise NotImplementedError()
  1186. @property
  1187. def _sup(self):
  1188. raise NotImplementedError()
  1189. def _contains(self, other):
  1190. return And(*[set.contains(other) for set in self.args])
  1191. def __iter__(self):
  1192. sets_sift = sift(self.args, lambda x: x.is_iterable)
  1193. completed = False
  1194. candidates = sets_sift[True] + sets_sift[None]
  1195. finite_candidates, others = [], []
  1196. for candidate in candidates:
  1197. length = None
  1198. try:
  1199. length = len(candidate)
  1200. except TypeError:
  1201. others.append(candidate)
  1202. if length is not None:
  1203. finite_candidates.append(candidate)
  1204. finite_candidates.sort(key=len)
  1205. for s in finite_candidates + others:
  1206. other_sets = set(self.args) - {s}
  1207. other = Intersection(*other_sets, evaluate=False)
  1208. completed = True
  1209. for x in s:
  1210. try:
  1211. if x in other:
  1212. yield x
  1213. except TypeError:
  1214. completed = False
  1215. if completed:
  1216. return
  1217. if not completed:
  1218. if not candidates:
  1219. raise TypeError("None of the constituent sets are iterable")
  1220. raise TypeError(
  1221. "The computation had not completed because of the "
  1222. "undecidable set membership is found in every candidates.")
  1223. @staticmethod
  1224. def _handle_finite_sets(args):
  1225. '''Simplify intersection of one or more FiniteSets and other sets'''
  1226. # First separate the FiniteSets from the others
  1227. fs_args, others = sift(args, lambda x: x.is_FiniteSet, binary=True)
  1228. # Let the caller handle intersection of non-FiniteSets
  1229. if not fs_args:
  1230. return
  1231. # Convert to Python sets and build the set of all elements
  1232. fs_sets = [set(fs) for fs in fs_args]
  1233. all_elements = reduce(lambda a, b: a | b, fs_sets, set())
  1234. # Extract elements that are definitely in or definitely not in the
  1235. # intersection. Here we check contains for all of args.
  1236. definite = set()
  1237. for e in all_elements:
  1238. inall = fuzzy_and(s.contains(e) for s in args)
  1239. if inall is True:
  1240. definite.add(e)
  1241. if inall is not None:
  1242. for s in fs_sets:
  1243. s.discard(e)
  1244. # At this point all elements in all of fs_sets are possibly in the
  1245. # intersection. In some cases this is because they are definitely in
  1246. # the intersection of the finite sets but it's not clear if they are
  1247. # members of others. We might have {m, n}, {m}, and Reals where we
  1248. # don't know if m or n is real. We want to remove n here but it is
  1249. # possibly in because it might be equal to m. So what we do now is
  1250. # extract the elements that are definitely in the remaining finite
  1251. # sets iteratively until we end up with {n}, {}. At that point if we
  1252. # get any empty set all remaining elements are discarded.
  1253. fs_elements = reduce(lambda a, b: a | b, fs_sets, set())
  1254. # Need fuzzy containment testing
  1255. fs_symsets = [FiniteSet(*s) for s in fs_sets]
  1256. while fs_elements:
  1257. for e in fs_elements:
  1258. infs = fuzzy_and(s.contains(e) for s in fs_symsets)
  1259. if infs is True:
  1260. definite.add(e)
  1261. if infs is not None:
  1262. for n, s in enumerate(fs_sets):
  1263. # Update Python set and FiniteSet
  1264. if e in s:
  1265. s.remove(e)
  1266. fs_symsets[n] = FiniteSet(*s)
  1267. fs_elements.remove(e)
  1268. break
  1269. # If we completed the for loop without removing anything we are
  1270. # done so quit the outer while loop
  1271. else:
  1272. break
  1273. # If any of the sets of remainder elements is empty then we discard
  1274. # all of them for the intersection.
  1275. if not all(fs_sets):
  1276. fs_sets = [set()]
  1277. # Here we fold back the definitely included elements into each fs.
  1278. # Since they are definitely included they must have been members of
  1279. # each FiniteSet to begin with. We could instead fold these in with a
  1280. # Union at the end to get e.g. {3}|({x}&{y}) rather than {3,x}&{3,y}.
  1281. if definite:
  1282. fs_sets = [fs | definite for fs in fs_sets]
  1283. if fs_sets == [set()]:
  1284. return S.EmptySet
  1285. sets = [FiniteSet(*s) for s in fs_sets]
  1286. # Any set in others is redundant if it contains all the elements that
  1287. # are in the finite sets so we don't need it in the Intersection
  1288. all_elements = reduce(lambda a, b: a | b, fs_sets, set())
  1289. is_redundant = lambda o: all(fuzzy_bool(o.contains(e)) for e in all_elements)
  1290. others = [o for o in others if not is_redundant(o)]
  1291. if others:
  1292. rest = Intersection(*others)
  1293. # XXX: Maybe this shortcut should be at the beginning. For large
  1294. # FiniteSets it could much more efficient to process the other
  1295. # sets first...
  1296. if rest is S.EmptySet:
  1297. return S.EmptySet
  1298. # Flatten the Intersection
  1299. if rest.is_Intersection:
  1300. sets.extend(rest.args)
  1301. else:
  1302. sets.append(rest)
  1303. if len(sets) == 1:
  1304. return sets[0]
  1305. else:
  1306. return Intersection(*sets, evaluate=False)
  1307. def as_relational(self, symbol):
  1308. """Rewrite an Intersection in terms of equalities and logic operators"""
  1309. return And(*[set.as_relational(symbol) for set in self.args])
  1310. class Complement(Set):
  1311. r"""Represents the set difference or relative complement of a set with
  1312. another set.
  1313. $$A - B = \{x \in A \mid x \notin B\}$$
  1314. Examples
  1315. ========
  1316. >>> from sympy import Complement, FiniteSet
  1317. >>> Complement(FiniteSet(0, 1, 2), FiniteSet(1))
  1318. {0, 2}
  1319. See Also
  1320. =========
  1321. Intersection, Union
  1322. References
  1323. ==========
  1324. .. [1] https://mathworld.wolfram.com/ComplementSet.html
  1325. """
  1326. is_Complement = True
  1327. def __new__(cls, a, b, evaluate=True):
  1328. a, b = map(_sympify, (a, b))
  1329. if evaluate:
  1330. return Complement.reduce(a, b)
  1331. return Basic.__new__(cls, a, b)
  1332. @staticmethod
  1333. def reduce(A, B):
  1334. """
  1335. Simplify a :class:`Complement`.
  1336. """
  1337. if B == S.UniversalSet or A.is_subset(B):
  1338. return S.EmptySet
  1339. if isinstance(B, Union):
  1340. return Intersection(*(s.complement(A) for s in B.args))
  1341. result = B._complement(A)
  1342. if result is not None:
  1343. return result
  1344. else:
  1345. return Complement(A, B, evaluate=False)
  1346. def _contains(self, other):
  1347. A = self.args[0]
  1348. B = self.args[1]
  1349. return And(A.contains(other), Not(B.contains(other)))
  1350. def as_relational(self, symbol):
  1351. """Rewrite a complement in terms of equalities and logic
  1352. operators"""
  1353. A, B = self.args
  1354. A_rel = A.as_relational(symbol)
  1355. B_rel = Not(B.as_relational(symbol))
  1356. return And(A_rel, B_rel)
  1357. def _kind(self):
  1358. return self.args[0].kind
  1359. @property
  1360. def is_iterable(self):
  1361. if self.args[0].is_iterable:
  1362. return True
  1363. @property
  1364. def is_finite_set(self):
  1365. A, B = self.args
  1366. a_finite = A.is_finite_set
  1367. if a_finite is True:
  1368. return True
  1369. elif a_finite is False and B.is_finite_set:
  1370. return False
  1371. def __iter__(self):
  1372. A, B = self.args
  1373. for a in A:
  1374. if a not in B:
  1375. yield a
  1376. else:
  1377. continue
  1378. class EmptySet(Set, metaclass=Singleton):
  1379. """
  1380. Represents the empty set. The empty set is available as a singleton
  1381. as ``S.EmptySet``.
  1382. Examples
  1383. ========
  1384. >>> from sympy import S, Interval
  1385. >>> S.EmptySet
  1386. EmptySet
  1387. >>> Interval(1, 2).intersect(S.EmptySet)
  1388. EmptySet
  1389. See Also
  1390. ========
  1391. UniversalSet
  1392. References
  1393. ==========
  1394. .. [1] https://en.wikipedia.org/wiki/Empty_set
  1395. """
  1396. is_empty = True
  1397. is_finite_set = True
  1398. is_FiniteSet = True
  1399. @property # type: ignore
  1400. @deprecated(
  1401. """
  1402. The is_EmptySet attribute of Set objects is deprecated.
  1403. Use 's is S.EmptySet" or 's.is_empty' instead.
  1404. """,
  1405. deprecated_since_version="1.5",
  1406. active_deprecations_target="deprecated-is-emptyset",
  1407. )
  1408. def is_EmptySet(self):
  1409. return True
  1410. @property
  1411. def _measure(self):
  1412. return 0
  1413. def _contains(self, other):
  1414. return false
  1415. def as_relational(self, symbol):
  1416. return false
  1417. def __len__(self):
  1418. return 0
  1419. def __iter__(self):
  1420. return iter([])
  1421. def _eval_powerset(self):
  1422. return FiniteSet(self)
  1423. @property
  1424. def _boundary(self):
  1425. return self
  1426. def _complement(self, other):
  1427. return other
  1428. def _kind(self):
  1429. return SetKind()
  1430. def _symmetric_difference(self, other):
  1431. return other
  1432. class UniversalSet(Set, metaclass=Singleton):
  1433. """
  1434. Represents the set of all things.
  1435. The universal set is available as a singleton as ``S.UniversalSet``.
  1436. Examples
  1437. ========
  1438. >>> from sympy import S, Interval
  1439. >>> S.UniversalSet
  1440. UniversalSet
  1441. >>> Interval(1, 2).intersect(S.UniversalSet)
  1442. Interval(1, 2)
  1443. See Also
  1444. ========
  1445. EmptySet
  1446. References
  1447. ==========
  1448. .. [1] https://en.wikipedia.org/wiki/Universal_set
  1449. """
  1450. is_UniversalSet = True
  1451. is_empty = False
  1452. is_finite_set = False
  1453. def _complement(self, other):
  1454. return S.EmptySet
  1455. def _symmetric_difference(self, other):
  1456. return other
  1457. @property
  1458. def _measure(self):
  1459. return S.Infinity
  1460. def _kind(self):
  1461. return SetKind(UndefinedKind)
  1462. def _contains(self, other):
  1463. return true
  1464. def as_relational(self, symbol):
  1465. return true
  1466. @property
  1467. def _boundary(self):
  1468. return S.EmptySet
  1469. class FiniteSet(Set):
  1470. """
  1471. Represents a finite set of Sympy expressions.
  1472. Examples
  1473. ========
  1474. >>> from sympy import FiniteSet, Symbol, Interval, Naturals0
  1475. >>> FiniteSet(1, 2, 3, 4)
  1476. {1, 2, 3, 4}
  1477. >>> 3 in FiniteSet(1, 2, 3, 4)
  1478. True
  1479. >>> FiniteSet(1, (1, 2), Symbol('x'))
  1480. {1, x, (1, 2)}
  1481. >>> FiniteSet(Interval(1, 2), Naturals0, {1, 2})
  1482. FiniteSet({1, 2}, Interval(1, 2), Naturals0)
  1483. >>> members = [1, 2, 3, 4]
  1484. >>> f = FiniteSet(*members)
  1485. >>> f
  1486. {1, 2, 3, 4}
  1487. >>> f - FiniteSet(2)
  1488. {1, 3, 4}
  1489. >>> f + FiniteSet(2, 5)
  1490. {1, 2, 3, 4, 5}
  1491. References
  1492. ==========
  1493. .. [1] https://en.wikipedia.org/wiki/Finite_set
  1494. """
  1495. is_FiniteSet = True
  1496. is_iterable = True
  1497. is_empty = False
  1498. is_finite_set = True
  1499. def __new__(cls, *args, **kwargs):
  1500. evaluate = kwargs.get('evaluate', global_parameters.evaluate)
  1501. if evaluate:
  1502. args = list(map(sympify, args))
  1503. if len(args) == 0:
  1504. return S.EmptySet
  1505. else:
  1506. args = list(map(sympify, args))
  1507. # keep the form of the first canonical arg
  1508. dargs = {}
  1509. for i in reversed(list(ordered(args))):
  1510. if i.is_Symbol:
  1511. dargs[i] = i
  1512. else:
  1513. try:
  1514. dargs[i.as_dummy()] = i
  1515. except TypeError:
  1516. # e.g. i = class without args like `Interval`
  1517. dargs[i] = i
  1518. _args_set = set(dargs.values())
  1519. args = list(ordered(_args_set, Set._infimum_key))
  1520. obj = Basic.__new__(cls, *args)
  1521. obj._args_set = _args_set
  1522. return obj
  1523. def __iter__(self):
  1524. return iter(self.args)
  1525. def _complement(self, other):
  1526. if isinstance(other, Interval):
  1527. # Splitting in sub-intervals is only done for S.Reals;
  1528. # other cases that need splitting will first pass through
  1529. # Set._complement().
  1530. nums, syms = [], []
  1531. for m in self.args:
  1532. if m.is_number and m.is_real:
  1533. nums.append(m)
  1534. elif m.is_real == False:
  1535. pass # drop non-reals
  1536. else:
  1537. syms.append(m) # various symbolic expressions
  1538. if other == S.Reals and nums != []:
  1539. nums.sort()
  1540. intervals = [] # Build up a list of intervals between the elements
  1541. intervals += [Interval(S.NegativeInfinity, nums[0], True, True)]
  1542. for a, b in zip(nums[:-1], nums[1:]):
  1543. intervals.append(Interval(a, b, True, True)) # both open
  1544. intervals.append(Interval(nums[-1], S.Infinity, True, True))
  1545. if syms != []:
  1546. return Complement(Union(*intervals, evaluate=False),
  1547. FiniteSet(*syms), evaluate=False)
  1548. else:
  1549. return Union(*intervals, evaluate=False)
  1550. elif nums == []: # no splitting necessary or possible:
  1551. if syms:
  1552. return Complement(other, FiniteSet(*syms), evaluate=False)
  1553. else:
  1554. return other
  1555. elif isinstance(other, FiniteSet):
  1556. unk = []
  1557. for i in self:
  1558. c = sympify(other.contains(i))
  1559. if c is not S.true and c is not S.false:
  1560. unk.append(i)
  1561. unk = FiniteSet(*unk)
  1562. if unk == self:
  1563. return
  1564. not_true = []
  1565. for i in other:
  1566. c = sympify(self.contains(i))
  1567. if c is not S.true:
  1568. not_true.append(i)
  1569. return Complement(FiniteSet(*not_true), unk)
  1570. return Set._complement(self, other)
  1571. def _contains(self, other):
  1572. """
  1573. Tests whether an element, other, is in the set.
  1574. Explanation
  1575. ===========
  1576. The actual test is for mathematical equality (as opposed to
  1577. syntactical equality). In the worst case all elements of the
  1578. set must be checked.
  1579. Examples
  1580. ========
  1581. >>> from sympy import FiniteSet
  1582. >>> 1 in FiniteSet(1, 2)
  1583. True
  1584. >>> 5 in FiniteSet(1, 2)
  1585. False
  1586. """
  1587. if other in self._args_set:
  1588. return True
  1589. else:
  1590. # evaluate=True is needed to override evaluate=False context;
  1591. # we need Eq to do the evaluation
  1592. return fuzzy_or(fuzzy_bool(Eq(e, other, evaluate=True))
  1593. for e in self.args)
  1594. def _eval_is_subset(self, other):
  1595. return fuzzy_and(other._contains(e) for e in self.args)
  1596. @property
  1597. def _boundary(self):
  1598. return self
  1599. @property
  1600. def _inf(self):
  1601. return Min(*self)
  1602. @property
  1603. def _sup(self):
  1604. return Max(*self)
  1605. @property
  1606. def measure(self):
  1607. return 0
  1608. def _kind(self):
  1609. if not self.args:
  1610. return SetKind()
  1611. elif all(i.kind == self.args[0].kind for i in self.args):
  1612. return SetKind(self.args[0].kind)
  1613. else:
  1614. return SetKind(UndefinedKind)
  1615. def __len__(self):
  1616. return len(self.args)
  1617. def as_relational(self, symbol):
  1618. """Rewrite a FiniteSet in terms of equalities and logic operators. """
  1619. return Or(*[Eq(symbol, elem) for elem in self])
  1620. def compare(self, other):
  1621. return (hash(self) - hash(other))
  1622. def _eval_evalf(self, prec):
  1623. dps = prec_to_dps(prec)
  1624. return FiniteSet(*[elem.evalf(n=dps) for elem in self])
  1625. def _eval_simplify(self, **kwargs):
  1626. from sympy.simplify import simplify
  1627. return FiniteSet(*[simplify(elem, **kwargs) for elem in self])
  1628. @property
  1629. def _sorted_args(self):
  1630. return self.args
  1631. def _eval_powerset(self):
  1632. return self.func(*[self.func(*s) for s in subsets(self.args)])
  1633. def _eval_rewrite_as_PowerSet(self, *args, **kwargs):
  1634. """Rewriting method for a finite set to a power set."""
  1635. from .powerset import PowerSet
  1636. is2pow = lambda n: bool(n and not n & (n - 1))
  1637. if not is2pow(len(self)):
  1638. return None
  1639. fs_test = lambda arg: isinstance(arg, Set) and arg.is_FiniteSet
  1640. if not all(fs_test(arg) for arg in args):
  1641. return None
  1642. biggest = max(args, key=len)
  1643. for arg in subsets(biggest.args):
  1644. arg_set = FiniteSet(*arg)
  1645. if arg_set not in args:
  1646. return None
  1647. return PowerSet(biggest)
  1648. def __ge__(self, other):
  1649. if not isinstance(other, Set):
  1650. raise TypeError("Invalid comparison of set with %s" % func_name(other))
  1651. return other.is_subset(self)
  1652. def __gt__(self, other):
  1653. if not isinstance(other, Set):
  1654. raise TypeError("Invalid comparison of set with %s" % func_name(other))
  1655. return self.is_proper_superset(other)
  1656. def __le__(self, other):
  1657. if not isinstance(other, Set):
  1658. raise TypeError("Invalid comparison of set with %s" % func_name(other))
  1659. return self.is_subset(other)
  1660. def __lt__(self, other):
  1661. if not isinstance(other, Set):
  1662. raise TypeError("Invalid comparison of set with %s" % func_name(other))
  1663. return self.is_proper_subset(other)
  1664. def __eq__(self, other):
  1665. if isinstance(other, (set, frozenset)):
  1666. return self._args_set == other
  1667. return super().__eq__(other)
  1668. __hash__ : Callable[[Basic], Any] = Basic.__hash__
  1669. _sympy_converter[set] = lambda x: FiniteSet(*x)
  1670. _sympy_converter[frozenset] = lambda x: FiniteSet(*x)
  1671. class SymmetricDifference(Set):
  1672. """Represents the set of elements which are in either of the
  1673. sets and not in their intersection.
  1674. Examples
  1675. ========
  1676. >>> from sympy import SymmetricDifference, FiniteSet
  1677. >>> SymmetricDifference(FiniteSet(1, 2, 3), FiniteSet(3, 4, 5))
  1678. {1, 2, 4, 5}
  1679. See Also
  1680. ========
  1681. Complement, Union
  1682. References
  1683. ==========
  1684. .. [1] https://en.wikipedia.org/wiki/Symmetric_difference
  1685. """
  1686. is_SymmetricDifference = True
  1687. def __new__(cls, a, b, evaluate=True):
  1688. if evaluate:
  1689. return SymmetricDifference.reduce(a, b)
  1690. return Basic.__new__(cls, a, b)
  1691. @staticmethod
  1692. def reduce(A, B):
  1693. result = B._symmetric_difference(A)
  1694. if result is not None:
  1695. return result
  1696. else:
  1697. return SymmetricDifference(A, B, evaluate=False)
  1698. def as_relational(self, symbol):
  1699. """Rewrite a symmetric_difference in terms of equalities and
  1700. logic operators"""
  1701. A, B = self.args
  1702. A_rel = A.as_relational(symbol)
  1703. B_rel = B.as_relational(symbol)
  1704. return Xor(A_rel, B_rel)
  1705. @property
  1706. def is_iterable(self):
  1707. if all(arg.is_iterable for arg in self.args):
  1708. return True
  1709. def __iter__(self):
  1710. args = self.args
  1711. union = roundrobin(*(iter(arg) for arg in args))
  1712. for item in union:
  1713. count = 0
  1714. for s in args:
  1715. if item in s:
  1716. count += 1
  1717. if count % 2 == 1:
  1718. yield item
  1719. class DisjointUnion(Set):
  1720. """ Represents the disjoint union (also known as the external disjoint union)
  1721. of a finite number of sets.
  1722. Examples
  1723. ========
  1724. >>> from sympy import DisjointUnion, FiniteSet, Interval, Union, Symbol
  1725. >>> A = FiniteSet(1, 2, 3)
  1726. >>> B = Interval(0, 5)
  1727. >>> DisjointUnion(A, B)
  1728. DisjointUnion({1, 2, 3}, Interval(0, 5))
  1729. >>> DisjointUnion(A, B).rewrite(Union)
  1730. Union(ProductSet({1, 2, 3}, {0}), ProductSet(Interval(0, 5), {1}))
  1731. >>> C = FiniteSet(Symbol('x'), Symbol('y'), Symbol('z'))
  1732. >>> DisjointUnion(C, C)
  1733. DisjointUnion({x, y, z}, {x, y, z})
  1734. >>> DisjointUnion(C, C).rewrite(Union)
  1735. ProductSet({x, y, z}, {0, 1})
  1736. References
  1737. ==========
  1738. https://en.wikipedia.org/wiki/Disjoint_union
  1739. """
  1740. def __new__(cls, *sets):
  1741. dj_collection = []
  1742. for set_i in sets:
  1743. if isinstance(set_i, Set):
  1744. dj_collection.append(set_i)
  1745. else:
  1746. raise TypeError("Invalid input: '%s', input args \
  1747. to DisjointUnion must be Sets" % set_i)
  1748. obj = Basic.__new__(cls, *dj_collection)
  1749. return obj
  1750. @property
  1751. def sets(self):
  1752. return self.args
  1753. @property
  1754. def is_empty(self):
  1755. return fuzzy_and(s.is_empty for s in self.sets)
  1756. @property
  1757. def is_finite_set(self):
  1758. all_finite = fuzzy_and(s.is_finite_set for s in self.sets)
  1759. return fuzzy_or([self.is_empty, all_finite])
  1760. @property
  1761. def is_iterable(self):
  1762. if self.is_empty:
  1763. return False
  1764. iter_flag = True
  1765. for set_i in self.sets:
  1766. if not set_i.is_empty:
  1767. iter_flag = iter_flag and set_i.is_iterable
  1768. return iter_flag
  1769. def _eval_rewrite_as_Union(self, *sets):
  1770. """
  1771. Rewrites the disjoint union as the union of (``set`` x {``i``})
  1772. where ``set`` is the element in ``sets`` at index = ``i``
  1773. """
  1774. dj_union = S.EmptySet
  1775. index = 0
  1776. for set_i in sets:
  1777. if isinstance(set_i, Set):
  1778. cross = ProductSet(set_i, FiniteSet(index))
  1779. dj_union = Union(dj_union, cross)
  1780. index = index + 1
  1781. return dj_union
  1782. def _contains(self, element):
  1783. """
  1784. ``in`` operator for DisjointUnion
  1785. Examples
  1786. ========
  1787. >>> from sympy import Interval, DisjointUnion
  1788. >>> D = DisjointUnion(Interval(0, 1), Interval(0, 2))
  1789. >>> (0.5, 0) in D
  1790. True
  1791. >>> (0.5, 1) in D
  1792. True
  1793. >>> (1.5, 0) in D
  1794. False
  1795. >>> (1.5, 1) in D
  1796. True
  1797. Passes operation on to constituent sets
  1798. """
  1799. if not isinstance(element, Tuple) or len(element) != 2:
  1800. return False
  1801. if not element[1].is_Integer:
  1802. return False
  1803. if element[1] >= len(self.sets) or element[1] < 0:
  1804. return False
  1805. return element[0] in self.sets[element[1]]
  1806. def _kind(self):
  1807. if not self.args:
  1808. return SetKind()
  1809. elif all(i.kind == self.args[0].kind for i in self.args):
  1810. return self.args[0].kind
  1811. else:
  1812. return SetKind(UndefinedKind)
  1813. def __iter__(self):
  1814. if self.is_iterable:
  1815. iters = []
  1816. for i, s in enumerate(self.sets):
  1817. iters.append(iproduct(s, {Integer(i)}))
  1818. return iter(roundrobin(*iters))
  1819. else:
  1820. raise ValueError("'%s' is not iterable." % self)
  1821. def __len__(self):
  1822. """
  1823. Returns the length of the disjoint union, i.e., the number of elements in the set.
  1824. Examples
  1825. ========
  1826. >>> from sympy import FiniteSet, DisjointUnion, EmptySet
  1827. >>> D1 = DisjointUnion(FiniteSet(1, 2, 3, 4), EmptySet, FiniteSet(3, 4, 5))
  1828. >>> len(D1)
  1829. 7
  1830. >>> D2 = DisjointUnion(FiniteSet(3, 5, 7), EmptySet, FiniteSet(3, 5, 7))
  1831. >>> len(D2)
  1832. 6
  1833. >>> D3 = DisjointUnion(EmptySet, EmptySet)
  1834. >>> len(D3)
  1835. 0
  1836. Adds up the lengths of the constituent sets.
  1837. """
  1838. if self.is_finite_set:
  1839. size = 0
  1840. for set in self.sets:
  1841. size += len(set)
  1842. return size
  1843. else:
  1844. raise ValueError("'%s' is not a finite set." % self)
  1845. def imageset(*args):
  1846. r"""
  1847. Return an image of the set under transformation ``f``.
  1848. Explanation
  1849. ===========
  1850. If this function cannot compute the image, it returns an
  1851. unevaluated ImageSet object.
  1852. .. math::
  1853. \{ f(x) \mid x \in \mathrm{self} \}
  1854. Examples
  1855. ========
  1856. >>> from sympy import S, Interval, imageset, sin, Lambda
  1857. >>> from sympy.abc import x
  1858. >>> imageset(x, 2*x, Interval(0, 2))
  1859. Interval(0, 4)
  1860. >>> imageset(lambda x: 2*x, Interval(0, 2))
  1861. Interval(0, 4)
  1862. >>> imageset(Lambda(x, sin(x)), Interval(-2, 1))
  1863. ImageSet(Lambda(x, sin(x)), Interval(-2, 1))
  1864. >>> imageset(sin, Interval(-2, 1))
  1865. ImageSet(Lambda(x, sin(x)), Interval(-2, 1))
  1866. >>> imageset(lambda y: x + y, Interval(-2, 1))
  1867. ImageSet(Lambda(y, x + y), Interval(-2, 1))
  1868. Expressions applied to the set of Integers are simplified
  1869. to show as few negatives as possible and linear expressions
  1870. are converted to a canonical form. If this is not desirable
  1871. then the unevaluated ImageSet should be used.
  1872. >>> imageset(x, -2*x + 5, S.Integers)
  1873. ImageSet(Lambda(x, 2*x + 1), Integers)
  1874. See Also
  1875. ========
  1876. sympy.sets.fancysets.ImageSet
  1877. """
  1878. from .fancysets import ImageSet
  1879. from .setexpr import set_function
  1880. if len(args) < 2:
  1881. raise ValueError('imageset expects at least 2 args, got: %s' % len(args))
  1882. if isinstance(args[0], (Symbol, tuple)) and len(args) > 2:
  1883. f = Lambda(args[0], args[1])
  1884. set_list = args[2:]
  1885. else:
  1886. f = args[0]
  1887. set_list = args[1:]
  1888. if isinstance(f, Lambda):
  1889. pass
  1890. elif callable(f):
  1891. nargs = getattr(f, 'nargs', {})
  1892. if nargs:
  1893. if len(nargs) != 1:
  1894. raise NotImplementedError(filldedent('''
  1895. This function can take more than 1 arg
  1896. but the potentially complicated set input
  1897. has not been analyzed at this point to
  1898. know its dimensions. TODO
  1899. '''))
  1900. N = nargs.args[0]
  1901. if N == 1:
  1902. s = 'x'
  1903. else:
  1904. s = [Symbol('x%i' % i) for i in range(1, N + 1)]
  1905. else:
  1906. s = inspect.signature(f).parameters
  1907. dexpr = _sympify(f(*[Dummy() for i in s]))
  1908. var = tuple(uniquely_named_symbol(
  1909. Symbol(i), dexpr) for i in s)
  1910. f = Lambda(var, f(*var))
  1911. else:
  1912. raise TypeError(filldedent('''
  1913. expecting lambda, Lambda, or FunctionClass,
  1914. not \'%s\'.''' % func_name(f)))
  1915. if any(not isinstance(s, Set) for s in set_list):
  1916. name = [func_name(s) for s in set_list]
  1917. raise ValueError(
  1918. 'arguments after mapping should be sets, not %s' % name)
  1919. if len(set_list) == 1:
  1920. set = set_list[0]
  1921. try:
  1922. # TypeError if arg count != set dimensions
  1923. r = set_function(f, set)
  1924. if r is None:
  1925. raise TypeError
  1926. if not r:
  1927. return r
  1928. except TypeError:
  1929. r = ImageSet(f, set)
  1930. if isinstance(r, ImageSet):
  1931. f, set = r.args
  1932. if f.variables[0] == f.expr:
  1933. return set
  1934. if isinstance(set, ImageSet):
  1935. # XXX: Maybe this should just be:
  1936. # f2 = set.lambda
  1937. # fun = Lambda(f2.signature, f(*f2.expr))
  1938. # return imageset(fun, *set.base_sets)
  1939. if len(set.lamda.variables) == 1 and len(f.variables) == 1:
  1940. x = set.lamda.variables[0]
  1941. y = f.variables[0]
  1942. return imageset(
  1943. Lambda(x, f.expr.subs(y, set.lamda.expr)), *set.base_sets)
  1944. if r is not None:
  1945. return r
  1946. return ImageSet(f, *set_list)
  1947. def is_function_invertible_in_set(func, setv):
  1948. """
  1949. Checks whether function ``func`` is invertible when the domain is
  1950. restricted to set ``setv``.
  1951. """
  1952. # Functions known to always be invertible:
  1953. if func in (exp, log):
  1954. return True
  1955. u = Dummy("u")
  1956. fdiff = func(u).diff(u)
  1957. # monotonous functions:
  1958. # TODO: check subsets (`func` in `setv`)
  1959. if (fdiff > 0) == True or (fdiff < 0) == True:
  1960. return True
  1961. # TODO: support more
  1962. return None
  1963. def simplify_union(args):
  1964. """
  1965. Simplify a :class:`Union` using known rules.
  1966. Explanation
  1967. ===========
  1968. We first start with global rules like 'Merge all FiniteSets'
  1969. Then we iterate through all pairs and ask the constituent sets if they
  1970. can simplify themselves with any other constituent. This process depends
  1971. on ``union_sets(a, b)`` functions.
  1972. """
  1973. from sympy.sets.handlers.union import union_sets
  1974. # ===== Global Rules =====
  1975. if not args:
  1976. return S.EmptySet
  1977. for arg in args:
  1978. if not isinstance(arg, Set):
  1979. raise TypeError("Input args to Union must be Sets")
  1980. # Merge all finite sets
  1981. finite_sets = [x for x in args if x.is_FiniteSet]
  1982. if len(finite_sets) > 1:
  1983. a = (x for set in finite_sets for x in set)
  1984. finite_set = FiniteSet(*a)
  1985. args = [finite_set] + [x for x in args if not x.is_FiniteSet]
  1986. # ===== Pair-wise Rules =====
  1987. # Here we depend on rules built into the constituent sets
  1988. args = set(args)
  1989. new_args = True
  1990. while new_args:
  1991. for s in args:
  1992. new_args = False
  1993. for t in args - {s}:
  1994. new_set = union_sets(s, t)
  1995. # This returns None if s does not know how to intersect
  1996. # with t. Returns the newly intersected set otherwise
  1997. if new_set is not None:
  1998. if not isinstance(new_set, set):
  1999. new_set = {new_set}
  2000. new_args = (args - {s, t}).union(new_set)
  2001. break
  2002. if new_args:
  2003. args = new_args
  2004. break
  2005. if len(args) == 1:
  2006. return args.pop()
  2007. else:
  2008. return Union(*args, evaluate=False)
  2009. def simplify_intersection(args):
  2010. """
  2011. Simplify an intersection using known rules.
  2012. Explanation
  2013. ===========
  2014. We first start with global rules like
  2015. 'if any empty sets return empty set' and 'distribute any unions'
  2016. Then we iterate through all pairs and ask the constituent sets if they
  2017. can simplify themselves with any other constituent
  2018. """
  2019. # ===== Global Rules =====
  2020. if not args:
  2021. return S.UniversalSet
  2022. for arg in args:
  2023. if not isinstance(arg, Set):
  2024. raise TypeError("Input args to Union must be Sets")
  2025. # If any EmptySets return EmptySet
  2026. if S.EmptySet in args:
  2027. return S.EmptySet
  2028. # Handle Finite sets
  2029. rv = Intersection._handle_finite_sets(args)
  2030. if rv is not None:
  2031. return rv
  2032. # If any of the sets are unions, return a Union of Intersections
  2033. for s in args:
  2034. if s.is_Union:
  2035. other_sets = set(args) - {s}
  2036. if len(other_sets) > 0:
  2037. other = Intersection(*other_sets)
  2038. return Union(*(Intersection(arg, other) for arg in s.args))
  2039. else:
  2040. return Union(*s.args)
  2041. for s in args:
  2042. if s.is_Complement:
  2043. args.remove(s)
  2044. other_sets = args + [s.args[0]]
  2045. return Complement(Intersection(*other_sets), s.args[1])
  2046. from sympy.sets.handlers.intersection import intersection_sets
  2047. # At this stage we are guaranteed not to have any
  2048. # EmptySets, FiniteSets, or Unions in the intersection
  2049. # ===== Pair-wise Rules =====
  2050. # Here we depend on rules built into the constituent sets
  2051. args = set(args)
  2052. new_args = True
  2053. while new_args:
  2054. for s in args:
  2055. new_args = False
  2056. for t in args - {s}:
  2057. new_set = intersection_sets(s, t)
  2058. # This returns None if s does not know how to intersect
  2059. # with t. Returns the newly intersected set otherwise
  2060. if new_set is not None:
  2061. new_args = (args - {s, t}).union({new_set})
  2062. break
  2063. if new_args:
  2064. args = new_args
  2065. break
  2066. if len(args) == 1:
  2067. return args.pop()
  2068. else:
  2069. return Intersection(*args, evaluate=False)
  2070. def _handle_finite_sets(op, x, y, commutative):
  2071. # Handle finite sets:
  2072. fs_args, other = sift([x, y], lambda x: isinstance(x, FiniteSet), binary=True)
  2073. if len(fs_args) == 2:
  2074. return FiniteSet(*[op(i, j) for i in fs_args[0] for j in fs_args[1]])
  2075. elif len(fs_args) == 1:
  2076. sets = [_apply_operation(op, other[0], i, commutative) for i in fs_args[0]]
  2077. return Union(*sets)
  2078. else:
  2079. return None
  2080. def _apply_operation(op, x, y, commutative):
  2081. from .fancysets import ImageSet
  2082. d = Dummy('d')
  2083. out = _handle_finite_sets(op, x, y, commutative)
  2084. if out is None:
  2085. out = op(x, y)
  2086. if out is None and commutative:
  2087. out = op(y, x)
  2088. if out is None:
  2089. _x, _y = symbols("x y")
  2090. if isinstance(x, Set) and not isinstance(y, Set):
  2091. out = ImageSet(Lambda(d, op(d, y)), x).doit()
  2092. elif not isinstance(x, Set) and isinstance(y, Set):
  2093. out = ImageSet(Lambda(d, op(x, d)), y).doit()
  2094. else:
  2095. out = ImageSet(Lambda((_x, _y), op(_x, _y)), x, y)
  2096. return out
  2097. def set_add(x, y):
  2098. from sympy.sets.handlers.add import _set_add
  2099. return _apply_operation(_set_add, x, y, commutative=True)
  2100. def set_sub(x, y):
  2101. from sympy.sets.handlers.add import _set_sub
  2102. return _apply_operation(_set_sub, x, y, commutative=False)
  2103. def set_mul(x, y):
  2104. from sympy.sets.handlers.mul import _set_mul
  2105. return _apply_operation(_set_mul, x, y, commutative=True)
  2106. def set_div(x, y):
  2107. from sympy.sets.handlers.mul import _set_div
  2108. return _apply_operation(_set_div, x, y, commutative=False)
  2109. def set_pow(x, y):
  2110. from sympy.sets.handlers.power import _set_pow
  2111. return _apply_operation(_set_pow, x, y, commutative=False)
  2112. def set_function(f, x):
  2113. from sympy.sets.handlers.functions import _set_function
  2114. return _set_function(f, x)
  2115. class SetKind(Kind):
  2116. """
  2117. SetKind is kind for all Sets
  2118. Every instance of Set will have kind ``SetKind`` parametrised by the kind
  2119. of the elements of the ``Set``. The kind of the elements might be
  2120. ``NumberKind``, or ``TupleKind`` or something else. When not all elements
  2121. have the same kind then the kind of the elements will be given as
  2122. ``UndefinedKind``.
  2123. Parameters
  2124. ==========
  2125. element_kind: Kind (optional)
  2126. The kind of the elements of the set. In a well defined set all elements
  2127. will have the same kind. Otherwise the kind should
  2128. :class:`sympy.core.kind.UndefinedKind`. The ``element_kind`` argument is optional but
  2129. should only be omitted in the case of ``EmptySet`` whose kind is simply
  2130. ``SetKind()``
  2131. Examples
  2132. ========
  2133. >>> from sympy import Interval
  2134. >>> Interval(1, 2).kind
  2135. SetKind(NumberKind)
  2136. >>> Interval(1,2).kind.element_kind
  2137. NumberKind
  2138. See Also
  2139. ========
  2140. sympy.core.kind.NumberKind
  2141. sympy.matrices.common.MatrixKind
  2142. sympy.core.containers.TupleKind
  2143. """
  2144. def __new__(cls, element_kind=None):
  2145. obj = super().__new__(cls, element_kind)
  2146. obj.element_kind = element_kind
  2147. return obj
  2148. def __repr__(self):
  2149. if not self.element_kind:
  2150. return "SetKind()"
  2151. else:
  2152. return "SetKind(%s)" % self.element_kind