basic.py 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355
  1. """Base class for all the objects in SymPy"""
  2. from __future__ import annotations
  3. from collections import Counter
  4. from collections.abc import Mapping, Iterable
  5. from itertools import zip_longest
  6. from functools import cmp_to_key
  7. from typing import TYPE_CHECKING, overload
  8. from .assumptions import _prepare_class_assumptions
  9. from .cache import cacheit
  10. from .sympify import _sympify, sympify, SympifyError, _external_converter
  11. from .sorting import ordered
  12. from .kind import Kind, UndefinedKind
  13. from ._print_helpers import Printable
  14. from sympy.utilities.decorator import deprecated
  15. from sympy.utilities.exceptions import sympy_deprecation_warning
  16. from sympy.utilities.iterables import iterable, numbered_symbols
  17. from sympy.utilities.misc import filldedent, func_name
  18. if TYPE_CHECKING:
  19. from typing import ClassVar, TypeVar, Any
  20. from typing_extensions import Self
  21. from .assumptions import StdFactKB
  22. from .symbol import Symbol
  23. Tbasic = TypeVar("Tbasic", bound='Basic')
  24. def as_Basic(expr):
  25. """Return expr as a Basic instance using strict sympify
  26. or raise a TypeError; this is just a wrapper to _sympify,
  27. raising a TypeError instead of a SympifyError."""
  28. try:
  29. return _sympify(expr)
  30. except SympifyError:
  31. raise TypeError(
  32. 'Argument must be a Basic object, not `%s`' % func_name(
  33. expr))
  34. # Key for sorting commutative args in canonical order
  35. # by name. This is used for canonical ordering of the
  36. # args for Add and Mul *if* the names of both classes
  37. # being compared appear here. Some things in this list
  38. # are not spelled the same as their name so they do not,
  39. # in effect, appear here. See Basic.compare.
  40. ordering_of_classes = [
  41. # singleton numbers
  42. 'Zero', 'One', 'Half', 'Infinity', 'NaN', 'NegativeOne', 'NegativeInfinity',
  43. # numbers
  44. 'Integer', 'Rational', 'Float',
  45. # singleton symbols
  46. 'Exp1', 'Pi', 'ImaginaryUnit',
  47. # symbols
  48. 'Symbol', 'Wild',
  49. # arithmetic operations
  50. 'Pow', 'Mul', 'Add',
  51. # function values
  52. 'Derivative', 'Integral',
  53. # defined singleton functions
  54. 'Abs', 'Sign', 'Sqrt',
  55. 'Floor', 'Ceiling',
  56. 'Re', 'Im', 'Arg',
  57. 'Conjugate',
  58. 'Exp', 'Log',
  59. 'Sin', 'Cos', 'Tan', 'Cot', 'ASin', 'ACos', 'ATan', 'ACot',
  60. 'Sinh', 'Cosh', 'Tanh', 'Coth', 'ASinh', 'ACosh', 'ATanh', 'ACoth',
  61. 'RisingFactorial', 'FallingFactorial',
  62. 'factorial', 'binomial',
  63. 'Gamma', 'LowerGamma', 'UpperGamma', 'PolyGamma',
  64. 'Erf',
  65. # special polynomials
  66. 'Chebyshev', 'Chebyshev2',
  67. # undefined functions
  68. 'Function', 'WildFunction',
  69. # anonymous functions
  70. 'Lambda',
  71. # Landau O symbol
  72. 'Order',
  73. # relational operations
  74. 'Equality', 'Unequality', 'StrictGreaterThan', 'StrictLessThan',
  75. 'GreaterThan', 'LessThan',
  76. ]
  77. def _cmp_name(x: type, y: type) -> int:
  78. """return -1, 0, 1 if the name of x is before that of y.
  79. A string comparison is done if either name does not appear
  80. in `ordering_of_classes`. This is the helper for
  81. ``Basic.compare``
  82. Examples
  83. ========
  84. >>> from sympy import cos, tan, sin
  85. >>> from sympy.core import basic
  86. >>> save = basic.ordering_of_classes
  87. >>> basic.ordering_of_classes = ()
  88. >>> basic._cmp_name(cos, tan)
  89. -1
  90. >>> basic.ordering_of_classes = ["tan", "sin", "cos"]
  91. >>> basic._cmp_name(cos, tan)
  92. 1
  93. >>> basic._cmp_name(sin, cos)
  94. -1
  95. >>> basic.ordering_of_classes = save
  96. """
  97. n1 = x.__name__
  98. n2 = y.__name__
  99. if n1 == n2:
  100. return 0
  101. # If the other object is not a Basic subclass, then we are not equal to it.
  102. if not issubclass(y, Basic):
  103. return -1
  104. UNKNOWN = len(ordering_of_classes) + 1
  105. try:
  106. i1 = ordering_of_classes.index(n1)
  107. except ValueError:
  108. i1 = UNKNOWN
  109. try:
  110. i2 = ordering_of_classes.index(n2)
  111. except ValueError:
  112. i2 = UNKNOWN
  113. if i1 == UNKNOWN and i2 == UNKNOWN:
  114. return (n1 > n2) - (n1 < n2)
  115. return (i1 > i2) - (i1 < i2)
  116. @cacheit
  117. def _get_postprocessors(clsname, arg_type):
  118. # Since only Add, Mul, Pow can be clsname, this cache
  119. # is not quadratic.
  120. postprocessors = set()
  121. mappings = _get_postprocessors_for_type(arg_type)
  122. for mapping in mappings:
  123. f = mapping.get(clsname, None)
  124. if f is not None:
  125. postprocessors.update(f)
  126. return postprocessors
  127. @cacheit
  128. def _get_postprocessors_for_type(arg_type):
  129. return tuple(
  130. Basic._constructor_postprocessor_mapping[cls]
  131. for cls in arg_type.mro()
  132. if cls in Basic._constructor_postprocessor_mapping
  133. )
  134. class Basic(Printable):
  135. """
  136. Base class for all SymPy objects.
  137. Notes and conventions
  138. =====================
  139. 1) Always use ``.args``, when accessing parameters of some instance:
  140. >>> from sympy import cot
  141. >>> from sympy.abc import x, y
  142. >>> cot(x).args
  143. (x,)
  144. >>> cot(x).args[0]
  145. x
  146. >>> (x*y).args
  147. (x, y)
  148. >>> (x*y).args[1]
  149. y
  150. 2) Never use internal methods or variables (the ones prefixed with ``_``):
  151. >>> cot(x)._args # do not use this, use cot(x).args instead
  152. (x,)
  153. 3) By "SymPy object" we mean something that can be returned by
  154. ``sympify``. But not all objects one encounters using SymPy are
  155. subclasses of Basic. For example, mutable objects are not:
  156. >>> from sympy import Basic, Matrix, sympify
  157. >>> A = Matrix([[1, 2], [3, 4]]).as_mutable()
  158. >>> isinstance(A, Basic)
  159. False
  160. >>> B = sympify(A)
  161. >>> isinstance(B, Basic)
  162. True
  163. """
  164. __slots__ = ('_mhash', # hash value
  165. '_args', # arguments
  166. '_assumptions'
  167. )
  168. _args: tuple[Basic, ...]
  169. _mhash: int | None
  170. @property
  171. def __sympy__(self):
  172. return True
  173. def __init_subclass__(cls):
  174. # Initialize the default_assumptions FactKB and also any assumptions
  175. # property methods. This method will only be called for subclasses of
  176. # Basic but not for Basic itself so we call
  177. # _prepare_class_assumptions(Basic) below the class definition.
  178. super().__init_subclass__()
  179. _prepare_class_assumptions(cls)
  180. # To be overridden with True in the appropriate subclasses
  181. is_number = False
  182. is_Atom = False
  183. is_Symbol = False
  184. is_symbol = False
  185. is_Indexed = False
  186. is_Dummy = False
  187. is_Wild = False
  188. is_Function = False
  189. is_Add = False
  190. is_Mul = False
  191. is_Pow = False
  192. is_Number = False
  193. is_Float = False
  194. is_Rational = False
  195. is_Integer = False
  196. is_NumberSymbol = False
  197. is_Order = False
  198. is_Derivative = False
  199. is_Piecewise = False
  200. is_Poly = False
  201. is_AlgebraicNumber = False
  202. is_Relational = False
  203. is_Equality = False
  204. is_Boolean = False
  205. is_Not = False
  206. is_Matrix = False
  207. is_Vector = False
  208. is_Point = False
  209. is_MatAdd = False
  210. is_MatMul = False
  211. default_assumptions: ClassVar[StdFactKB]
  212. is_composite: bool | None
  213. is_noninteger: bool | None
  214. is_extended_positive: bool | None
  215. is_negative: bool | None
  216. is_complex: bool | None
  217. is_extended_nonpositive: bool | None
  218. is_integer: bool | None
  219. is_positive: bool | None
  220. is_rational: bool | None
  221. is_extended_nonnegative: bool | None
  222. is_infinite: bool | None
  223. is_antihermitian: bool | None
  224. is_extended_negative: bool | None
  225. is_extended_real: bool | None
  226. is_finite: bool | None
  227. is_polar: bool | None
  228. is_imaginary: bool | None
  229. is_transcendental: bool | None
  230. is_extended_nonzero: bool | None
  231. is_nonzero: bool | None
  232. is_odd: bool | None
  233. is_algebraic: bool | None
  234. is_prime: bool | None
  235. is_commutative: bool | None
  236. is_nonnegative: bool | None
  237. is_nonpositive: bool | None
  238. is_hermitian: bool | None
  239. is_irrational: bool | None
  240. is_real: bool | None
  241. is_zero: bool | None
  242. is_even: bool | None
  243. kind: Kind = UndefinedKind
  244. def __new__(cls, *args):
  245. obj = object.__new__(cls)
  246. obj._assumptions = cls.default_assumptions
  247. obj._mhash = None # will be set by __hash__ method.
  248. obj._args = args # all items in args must be Basic objects
  249. return obj
  250. def copy(self):
  251. return self.func(*self.args)
  252. def __getnewargs__(self):
  253. return self.args
  254. def __getstate__(self):
  255. return None
  256. def __setstate__(self, state):
  257. for name, value in state.items():
  258. setattr(self, name, value)
  259. def __reduce_ex__(self, protocol):
  260. if protocol < 2:
  261. msg = "Only pickle protocol 2 or higher is supported by SymPy"
  262. raise NotImplementedError(msg)
  263. return super().__reduce_ex__(protocol)
  264. def __hash__(self) -> int:
  265. # hash cannot be cached using cache_it because infinite recurrence
  266. # occurs as hash is needed for setting cache dictionary keys
  267. h = self._mhash
  268. if h is None:
  269. h = hash((type(self).__name__,) + self._hashable_content())
  270. self._mhash = h
  271. return h
  272. def _hashable_content(self):
  273. """Return a tuple of information about self that can be used to
  274. compute the hash. If a class defines additional attributes,
  275. like ``name`` in Symbol, then this method should be updated
  276. accordingly to return such relevant attributes.
  277. Defining more than _hashable_content is necessary if __eq__ has
  278. been defined by a class. See note about this in Basic.__eq__."""
  279. return self._args
  280. @property
  281. def assumptions0(self):
  282. """
  283. Return object `type` assumptions.
  284. For example:
  285. Symbol('x', real=True)
  286. Symbol('x', integer=True)
  287. are different objects. In other words, besides Python type (Symbol in
  288. this case), the initial assumptions are also forming their typeinfo.
  289. Examples
  290. ========
  291. >>> from sympy import Symbol
  292. >>> from sympy.abc import x
  293. >>> x.assumptions0
  294. {'commutative': True}
  295. >>> x = Symbol("x", positive=True)
  296. >>> x.assumptions0
  297. {'commutative': True, 'complex': True, 'extended_negative': False,
  298. 'extended_nonnegative': True, 'extended_nonpositive': False,
  299. 'extended_nonzero': True, 'extended_positive': True, 'extended_real':
  300. True, 'finite': True, 'hermitian': True, 'imaginary': False,
  301. 'infinite': False, 'negative': False, 'nonnegative': True,
  302. 'nonpositive': False, 'nonzero': True, 'positive': True, 'real':
  303. True, 'zero': False}
  304. """
  305. return {}
  306. def compare(self, other):
  307. """
  308. Return -1, 0, 1 if the object is less than, equal,
  309. or greater than other in a canonical sense.
  310. Non-Basic are always greater than Basic.
  311. If both names of the classes being compared appear
  312. in the `ordering_of_classes` then the ordering will
  313. depend on the appearance of the names there.
  314. If either does not appear in that list, then the
  315. comparison is based on the class name.
  316. If the names are the same then a comparison is made
  317. on the length of the hashable content.
  318. Items of the equal-lengthed contents are then
  319. successively compared using the same rules. If there
  320. is never a difference then 0 is returned.
  321. Examples
  322. ========
  323. >>> from sympy.abc import x, y
  324. >>> x.compare(y)
  325. -1
  326. >>> x.compare(x)
  327. 0
  328. >>> y.compare(x)
  329. 1
  330. """
  331. # all redefinitions of __cmp__ method should start with the
  332. # following lines:
  333. if self is other:
  334. return 0
  335. n1 = self.__class__
  336. n2 = other.__class__
  337. c = _cmp_name(n1, n2)
  338. if c:
  339. return c
  340. #
  341. st = self._hashable_content()
  342. ot = other._hashable_content()
  343. len_st = len(st)
  344. len_ot = len(ot)
  345. c = (len_st > len_ot) - (len_st < len_ot)
  346. if c:
  347. return c
  348. for l, r in zip(st, ot):
  349. if isinstance(l, Basic):
  350. c = l.compare(r)
  351. elif isinstance(l, frozenset):
  352. l = Basic(*l) if isinstance(l, frozenset) else l
  353. r = Basic(*r) if isinstance(r, frozenset) else r
  354. c = l.compare(r)
  355. else:
  356. c = (l > r) - (l < r)
  357. if c:
  358. return c
  359. return 0
  360. @classmethod
  361. def fromiter(cls, args, **assumptions):
  362. """
  363. Create a new object from an iterable.
  364. This is a convenience function that allows one to create objects from
  365. any iterable, without having to convert to a list or tuple first.
  366. Examples
  367. ========
  368. >>> from sympy import Tuple
  369. >>> Tuple.fromiter(i for i in range(5))
  370. (0, 1, 2, 3, 4)
  371. """
  372. return cls(*tuple(args), **assumptions)
  373. @classmethod
  374. def class_key(cls) -> tuple[int, int, str]:
  375. """Nice order of classes."""
  376. return 5, 0, cls.__name__
  377. @cacheit
  378. def sort_key(self, order=None):
  379. """
  380. Return a sort key.
  381. Examples
  382. ========
  383. >>> from sympy import S, I
  384. >>> sorted([S(1)/2, I, -I], key=lambda x: x.sort_key())
  385. [1/2, -I, I]
  386. >>> S("[x, 1/x, 1/x**2, x**2, x**(1/2), x**(1/4), x**(3/2)]")
  387. [x, 1/x, x**(-2), x**2, sqrt(x), x**(1/4), x**(3/2)]
  388. >>> sorted(_, key=lambda x: x.sort_key())
  389. [x**(-2), 1/x, x**(1/4), sqrt(x), x, x**(3/2), x**2]
  390. """
  391. # XXX: remove this when issue 5169 is fixed
  392. def inner_key(arg):
  393. if isinstance(arg, Basic):
  394. return arg.sort_key(order)
  395. else:
  396. return arg
  397. args = self._sorted_args
  398. args = len(args), tuple([inner_key(arg) for arg in args])
  399. return self.class_key(), args, S.One.sort_key(), S.One
  400. def _do_eq_sympify(self, other):
  401. """Returns a boolean indicating whether a == b when either a
  402. or b is not a Basic. This is only done for types that were either
  403. added to `converter` by a 3rd party or when the object has `_sympy_`
  404. defined. This essentially reuses the code in `_sympify` that is
  405. specific for this use case. Non-user defined types that are meant
  406. to work with SymPy should be handled directly in the __eq__ methods
  407. of the `Basic` classes it could equate to and not be converted. Note
  408. that after conversion, `==` is used again since it is not
  409. necessarily clear whether `self` or `other`'s __eq__ method needs
  410. to be used."""
  411. for superclass in type(other).__mro__:
  412. conv = _external_converter.get(superclass)
  413. if conv is not None:
  414. return self == conv(other)
  415. if hasattr(other, '_sympy_'):
  416. return self == other._sympy_()
  417. return NotImplemented
  418. def __eq__(self, other):
  419. """Return a boolean indicating whether a == b on the basis of
  420. their symbolic trees.
  421. This is the same as a.compare(b) == 0 but faster.
  422. Notes
  423. =====
  424. If a class that overrides __eq__() needs to retain the
  425. implementation of __hash__() from a parent class, the
  426. interpreter must be told this explicitly by setting
  427. __hash__ : Callable[[object], int] = <ParentClass>.__hash__.
  428. Otherwise the inheritance of __hash__() will be blocked,
  429. just as if __hash__ had been explicitly set to None.
  430. References
  431. ==========
  432. from https://docs.python.org/dev/reference/datamodel.html#object.__hash__
  433. """
  434. if self is other:
  435. return True
  436. if not isinstance(other, Basic):
  437. return self._do_eq_sympify(other)
  438. # check for pure number expr
  439. if not (self.is_Number and other.is_Number) and (
  440. type(self) != type(other)):
  441. return False
  442. a, b = self._hashable_content(), other._hashable_content()
  443. if a != b:
  444. return False
  445. # check number *in* an expression
  446. for a, b in zip(a, b):
  447. if not isinstance(a, Basic):
  448. continue
  449. if a.is_Number and type(a) != type(b):
  450. return False
  451. return True
  452. def __ne__(self, other):
  453. """``a != b`` -> Compare two symbolic trees and see whether they are different
  454. this is the same as:
  455. ``a.compare(b) != 0``
  456. but faster
  457. """
  458. return not self == other
  459. def dummy_eq(self, other, symbol=None):
  460. """
  461. Compare two expressions and handle dummy symbols.
  462. Examples
  463. ========
  464. >>> from sympy import Dummy
  465. >>> from sympy.abc import x, y
  466. >>> u = Dummy('u')
  467. >>> (u**2 + 1).dummy_eq(x**2 + 1)
  468. True
  469. >>> (u**2 + 1) == (x**2 + 1)
  470. False
  471. >>> (u**2 + y).dummy_eq(x**2 + y, x)
  472. True
  473. >>> (u**2 + y).dummy_eq(x**2 + y, y)
  474. False
  475. """
  476. s = self.as_dummy()
  477. o = _sympify(other)
  478. o = o.as_dummy()
  479. dummy_symbols = [i for i in s.free_symbols if i.is_Dummy]
  480. if len(dummy_symbols) == 1:
  481. dummy = dummy_symbols.pop()
  482. else:
  483. return s == o
  484. if symbol is None:
  485. symbols = o.free_symbols
  486. if len(symbols) == 1:
  487. symbol = symbols.pop()
  488. else:
  489. return s == o
  490. tmp = dummy.__class__()
  491. return s.xreplace({dummy: tmp}) == o.xreplace({symbol: tmp})
  492. @overload
  493. def atoms(self) -> set[Basic]: ...
  494. @overload
  495. def atoms(self, *types: Tbasic | type[Tbasic]) -> set[Tbasic]: ...
  496. def atoms(self, *types: Tbasic | type[Tbasic]) -> set[Basic] | set[Tbasic]:
  497. """Returns the atoms that form the current object.
  498. By default, only objects that are truly atomic and cannot
  499. be divided into smaller pieces are returned: symbols, numbers,
  500. and number symbols like I and pi. It is possible to request
  501. atoms of any type, however, as demonstrated below.
  502. Examples
  503. ========
  504. >>> from sympy import I, pi, sin
  505. >>> from sympy.abc import x, y
  506. >>> (1 + x + 2*sin(y + I*pi)).atoms()
  507. {1, 2, I, pi, x, y}
  508. If one or more types are given, the results will contain only
  509. those types of atoms.
  510. >>> from sympy import Number, NumberSymbol, Symbol
  511. >>> (1 + x + 2*sin(y + I*pi)).atoms(Symbol)
  512. {x, y}
  513. >>> (1 + x + 2*sin(y + I*pi)).atoms(Number)
  514. {1, 2}
  515. >>> (1 + x + 2*sin(y + I*pi)).atoms(Number, NumberSymbol)
  516. {1, 2, pi}
  517. >>> (1 + x + 2*sin(y + I*pi)).atoms(Number, NumberSymbol, I)
  518. {1, 2, I, pi}
  519. Note that I (imaginary unit) and zoo (complex infinity) are special
  520. types of number symbols and are not part of the NumberSymbol class.
  521. The type can be given implicitly, too:
  522. >>> (1 + x + 2*sin(y + I*pi)).atoms(x) # x is a Symbol
  523. {x, y}
  524. Be careful to check your assumptions when using the implicit option
  525. since ``S(1).is_Integer = True`` but ``type(S(1))`` is ``One``, a special type
  526. of SymPy atom, while ``type(S(2))`` is type ``Integer`` and will find all
  527. integers in an expression:
  528. >>> from sympy import S
  529. >>> (1 + x + 2*sin(y + I*pi)).atoms(S(1))
  530. {1}
  531. >>> (1 + x + 2*sin(y + I*pi)).atoms(S(2))
  532. {1, 2}
  533. Finally, arguments to atoms() can select more than atomic atoms: any
  534. SymPy type (loaded in core/__init__.py) can be listed as an argument
  535. and those types of "atoms" as found in scanning the arguments of the
  536. expression recursively:
  537. >>> from sympy import Function, Mul
  538. >>> from sympy.core.function import AppliedUndef
  539. >>> f = Function('f')
  540. >>> (1 + f(x) + 2*sin(y + I*pi)).atoms(Function)
  541. {f(x), sin(y + I*pi)}
  542. >>> (1 + f(x) + 2*sin(y + I*pi)).atoms(AppliedUndef)
  543. {f(x)}
  544. >>> (1 + x + 2*sin(y + I*pi)).atoms(Mul)
  545. {I*pi, 2*sin(y + I*pi)}
  546. """
  547. nodes = _preorder_traversal(self)
  548. if types:
  549. types2 = tuple([t if isinstance(t, type) else type(t) for t in types])
  550. return {node for node in nodes if isinstance(node, types2)}
  551. else:
  552. return {node for node in nodes if not node.args}
  553. @property
  554. def free_symbols(self) -> set[Basic]:
  555. """Return from the atoms of self those which are free symbols.
  556. Not all free symbols are ``Symbol`` (see examples)
  557. For most expressions, all symbols are free symbols. For some classes
  558. this is not true. e.g. Integrals use Symbols for the dummy variables
  559. which are bound variables, so Integral has a method to return all
  560. symbols except those. Derivative keeps track of symbols with respect
  561. to which it will perform a derivative; those are
  562. bound variables, too, so it has its own free_symbols method.
  563. Any other method that uses bound variables should implement a
  564. free_symbols method.
  565. Examples
  566. ========
  567. >>> from sympy import Derivative, Integral, IndexedBase
  568. >>> from sympy.abc import x, y, n
  569. >>> (x + 1).free_symbols
  570. {x}
  571. >>> Integral(x, y).free_symbols
  572. {x, y}
  573. Not all free symbols are actually symbols:
  574. >>> IndexedBase('F')[0].free_symbols
  575. {F, F[0]}
  576. The symbols of differentiation are not included unless they
  577. appear in the expression being differentiated.
  578. >>> Derivative(x + y, y).free_symbols
  579. {x, y}
  580. >>> Derivative(x, y).free_symbols
  581. {x}
  582. >>> Derivative(x, (y, n)).free_symbols
  583. {n, x}
  584. If you want to know if a symbol is in the variables of the
  585. Derivative you can do so as follows:
  586. >>> Derivative(x, y).has_free(y)
  587. True
  588. """
  589. empty: set[Basic] = set()
  590. return empty.union(*(a.free_symbols for a in self.args))
  591. @property
  592. def expr_free_symbols(self):
  593. sympy_deprecation_warning("""
  594. The expr_free_symbols property is deprecated. Use free_symbols to get
  595. the free symbols of an expression.
  596. """,
  597. deprecated_since_version="1.9",
  598. active_deprecations_target="deprecated-expr-free-symbols")
  599. return set()
  600. def as_dummy(self) -> "Self":
  601. """Return the expression with any objects having structurally
  602. bound symbols replaced with unique, canonical symbols within
  603. the object in which they appear and having only the default
  604. assumption for commutativity being True. When applied to a
  605. symbol a new symbol having only the same commutativity will be
  606. returned.
  607. Examples
  608. ========
  609. >>> from sympy import Integral, Symbol
  610. >>> from sympy.abc import x
  611. >>> r = Symbol('r', real=True)
  612. >>> Integral(r, (r, x)).as_dummy()
  613. Integral(_0, (_0, x))
  614. >>> _.variables[0].is_real is None
  615. True
  616. >>> r.as_dummy()
  617. _r
  618. Notes
  619. =====
  620. Any object that has structurally bound variables should have
  621. a property, ``bound_symbols`` that returns those symbols
  622. appearing in the object.
  623. """
  624. from .symbol import Dummy, Symbol
  625. def can(x):
  626. # mask free that shadow bound
  627. free = x.free_symbols
  628. bound = set(x.bound_symbols)
  629. d = {i: Dummy() for i in bound & free}
  630. x = x.subs(d)
  631. # replace bound with canonical names
  632. x = x.xreplace(x.canonical_variables)
  633. # return after undoing masking
  634. return x.xreplace({v: k for k, v in d.items()})
  635. if not self.has(Symbol):
  636. return self
  637. return self.replace(
  638. lambda x: hasattr(x, 'bound_symbols'),
  639. can,
  640. simultaneous=False) # type:ignore
  641. @property
  642. def canonical_variables(self) -> dict[Basic, Symbol]:
  643. """Return a dictionary mapping any variable defined in
  644. ``self.bound_symbols`` to Symbols that do not clash
  645. with any free symbols in the expression.
  646. Examples
  647. ========
  648. >>> from sympy import Lambda
  649. >>> from sympy.abc import x
  650. >>> Lambda(x, 2*x).canonical_variables
  651. {x: _0}
  652. """
  653. bound: list[Basic] | None = getattr(self, 'bound_symbols', None)
  654. if bound is None:
  655. return {}
  656. dums = numbered_symbols('_')
  657. reps = {}
  658. # watch out for free symbol that are not in bound symbols;
  659. # those that are in bound symbols are about to get changed
  660. # XXX: free_symbols only returns particular kinds of expressions that
  661. # generally have a .name attribute. There is not a proper class/type
  662. # that represents this.
  663. names = {i.name for i in self.free_symbols - set(bound)} # type: ignore
  664. for b in bound:
  665. d = next(dums)
  666. if b.is_Symbol:
  667. while d.name in names:
  668. d = next(dums)
  669. reps[b] = d
  670. return reps
  671. def rcall(self, *args):
  672. """Apply on the argument recursively through the expression tree.
  673. This method is used to simulate a common abuse of notation for
  674. operators. For instance, in SymPy the following will not work:
  675. ``(x+Lambda(y, 2*y))(z) == x+2*z``,
  676. however, you can use:
  677. >>> from sympy import Lambda
  678. >>> from sympy.abc import x, y, z
  679. >>> (x + Lambda(y, 2*y)).rcall(z)
  680. x + 2*z
  681. """
  682. if callable(self):
  683. return self(*args)
  684. elif self.args:
  685. newargs = [sub.rcall(*args) for sub in self.args]
  686. return self.func(*newargs)
  687. else:
  688. return self
  689. def is_hypergeometric(self, k):
  690. from sympy.simplify.simplify import hypersimp
  691. from sympy.functions.elementary.piecewise import Piecewise
  692. if self.has(Piecewise):
  693. return None
  694. return hypersimp(self, k) is not None
  695. @property
  696. def is_comparable(self):
  697. """Return True if self can be computed to a real number
  698. (or already is a real number) with precision, else False.
  699. Examples
  700. ========
  701. >>> from sympy import exp_polar, pi, I
  702. >>> (I*exp_polar(I*pi/2)).is_comparable
  703. True
  704. >>> (I*exp_polar(I*pi*2)).is_comparable
  705. False
  706. A False result does not mean that `self` cannot be rewritten
  707. into a form that would be comparable. For example, the
  708. difference computed below is zero but without simplification
  709. it does not evaluate to a zero with precision:
  710. >>> e = 2**pi*(1 + 2**pi)
  711. >>> dif = e - e.expand()
  712. >>> dif.is_comparable
  713. False
  714. >>> dif.n(2)._prec
  715. 1
  716. """
  717. return self._eval_is_comparable()
  718. def _eval_is_comparable(self) -> bool:
  719. # Expr.is_comparable overrides this
  720. return False
  721. @property
  722. def func(self):
  723. """
  724. The top-level function in an expression.
  725. The following should hold for all objects::
  726. >> x == x.func(*x.args)
  727. Examples
  728. ========
  729. >>> from sympy.abc import x
  730. >>> a = 2*x
  731. >>> a.func
  732. <class 'sympy.core.mul.Mul'>
  733. >>> a.args
  734. (2, x)
  735. >>> a.func(*a.args)
  736. 2*x
  737. >>> a == a.func(*a.args)
  738. True
  739. """
  740. return self.__class__
  741. @property
  742. def args(self) -> tuple[Basic, ...]:
  743. """Returns a tuple of arguments of 'self'.
  744. Examples
  745. ========
  746. >>> from sympy import cot
  747. >>> from sympy.abc import x, y
  748. >>> cot(x).args
  749. (x,)
  750. >>> cot(x).args[0]
  751. x
  752. >>> (x*y).args
  753. (x, y)
  754. >>> (x*y).args[1]
  755. y
  756. Notes
  757. =====
  758. Never use self._args, always use self.args.
  759. Only use _args in __new__ when creating a new function.
  760. Do not override .args() from Basic (so that it is easy to
  761. change the interface in the future if needed).
  762. """
  763. return self._args
  764. @property
  765. def _sorted_args(self):
  766. """
  767. The same as ``args``. Derived classes which do not fix an
  768. order on their arguments should override this method to
  769. produce the sorted representation.
  770. """
  771. return self.args
  772. def as_content_primitive(self, radical=False, clear=True):
  773. """A stub to allow Basic args (like Tuple) to be skipped when computing
  774. the content and primitive components of an expression.
  775. See Also
  776. ========
  777. sympy.core.expr.Expr.as_content_primitive
  778. """
  779. return S.One, self
  780. @overload
  781. def subs(self, arg1: Mapping[Basic | complex, Basic | complex], arg2: None=None, **kwargs: Any) -> Basic: ...
  782. @overload
  783. def subs(self, arg1: Iterable[tuple[Basic | complex, Basic | complex]], arg2: None=None, **kwargs: Any) -> Basic: ...
  784. @overload
  785. def subs(self, arg1: Basic | complex, arg2: Basic | complex, **kwargs: Any) -> Basic: ...
  786. def subs(self, arg1: Mapping[Basic | complex, Basic | complex]
  787. | Iterable[tuple[Basic | complex, Basic | complex]] | Basic | complex,
  788. arg2: Basic | complex | None = None, **kwargs: Any) -> Basic:
  789. """
  790. Substitutes old for new in an expression after sympifying args.
  791. `args` is either:
  792. - two arguments, e.g. foo.subs(old, new)
  793. - one iterable argument, e.g. foo.subs(iterable). The iterable may be
  794. o an iterable container with (old, new) pairs. In this case the
  795. replacements are processed in the order given with successive
  796. patterns possibly affecting replacements already made.
  797. o a dict or set whose key/value items correspond to old/new pairs.
  798. In this case the old/new pairs will be sorted by op count and in
  799. case of a tie, by number of args and the default_sort_key. The
  800. resulting sorted list is then processed as an iterable container
  801. (see previous).
  802. If the keyword ``simultaneous`` is True, the subexpressions will not be
  803. evaluated until all the substitutions have been made.
  804. Examples
  805. ========
  806. >>> from sympy import pi, exp, limit, oo
  807. >>> from sympy.abc import x, y
  808. >>> (1 + x*y).subs(x, pi)
  809. pi*y + 1
  810. >>> (1 + x*y).subs({x:pi, y:2})
  811. 1 + 2*pi
  812. >>> (1 + x*y).subs([(x, pi), (y, 2)])
  813. 1 + 2*pi
  814. >>> reps = [(y, x**2), (x, 2)]
  815. >>> (x + y).subs(reps)
  816. 6
  817. >>> (x + y).subs(reversed(reps))
  818. x**2 + 2
  819. >>> (x**2 + x**4).subs(x**2, y)
  820. y**2 + y
  821. To replace only the x**2 but not the x**4, use xreplace:
  822. >>> (x**2 + x**4).xreplace({x**2: y})
  823. x**4 + y
  824. To delay evaluation until all substitutions have been made,
  825. set the keyword ``simultaneous`` to True:
  826. >>> (x/y).subs([(x, 0), (y, 0)])
  827. 0
  828. >>> (x/y).subs([(x, 0), (y, 0)], simultaneous=True)
  829. nan
  830. This has the added feature of not allowing subsequent substitutions
  831. to affect those already made:
  832. >>> ((x + y)/y).subs({x + y: y, y: x + y})
  833. 1
  834. >>> ((x + y)/y).subs({x + y: y, y: x + y}, simultaneous=True)
  835. y/(x + y)
  836. In order to obtain a canonical result, unordered iterables are
  837. sorted by count_op length, number of arguments and by the
  838. default_sort_key to break any ties. All other iterables are left
  839. unsorted.
  840. >>> from sympy import sqrt, sin, cos
  841. >>> from sympy.abc import a, b, c, d, e
  842. >>> A = (sqrt(sin(2*x)), a)
  843. >>> B = (sin(2*x), b)
  844. >>> C = (cos(2*x), c)
  845. >>> D = (x, d)
  846. >>> E = (exp(x), e)
  847. >>> expr = sqrt(sin(2*x))*sin(exp(x)*x)*cos(2*x) + sin(2*x)
  848. >>> expr.subs(dict([A, B, C, D, E]))
  849. a*c*sin(d*e) + b
  850. The resulting expression represents a literal replacement of the
  851. old arguments with the new arguments. This may not reflect the
  852. limiting behavior of the expression:
  853. >>> (x**3 - 3*x).subs({x: oo})
  854. nan
  855. >>> limit(x**3 - 3*x, x, oo)
  856. oo
  857. If the substitution will be followed by numerical
  858. evaluation, it is better to pass the substitution to
  859. evalf as
  860. >>> (1/x).evalf(subs={x: 3.0}, n=21)
  861. 0.333333333333333333333
  862. rather than
  863. >>> (1/x).subs({x: 3.0}).evalf(21)
  864. 0.333333333333333314830
  865. as the former will ensure that the desired level of precision is
  866. obtained.
  867. See Also
  868. ========
  869. replace: replacement capable of doing wildcard-like matching,
  870. parsing of match, and conditional replacements
  871. xreplace: exact node replacement in expr tree; also capable of
  872. using matching rules
  873. sympy.core.evalf.EvalfMixin.evalf: calculates the given formula to a desired level of precision
  874. """
  875. from .containers import Dict
  876. from .symbol import Dummy, Symbol
  877. from .numbers import _illegal
  878. items: Iterable[tuple[Basic | complex, Basic | complex]]
  879. unordered = False
  880. if arg2 is None:
  881. if isinstance(arg1, set):
  882. items = arg1
  883. unordered = True
  884. elif isinstance(arg1, (Dict, Mapping)):
  885. unordered = True
  886. items = arg1.items() # type: ignore
  887. elif not iterable(arg1):
  888. raise ValueError(filldedent("""
  889. When a single argument is passed to subs
  890. it should be a dictionary of old: new pairs or an iterable
  891. of (old, new) tuples."""))
  892. else:
  893. items = arg1 # type: ignore
  894. else:
  895. items = [(arg1, arg2)] # type: ignore
  896. def sympify_old(old) -> Basic:
  897. if isinstance(old, str):
  898. # Use Symbol rather than parse_expr for old
  899. return Symbol(old)
  900. elif isinstance(old, type):
  901. # Allow a type e.g. Function('f') or sin
  902. return sympify(old, strict=False)
  903. else:
  904. return sympify(old, strict=True)
  905. def sympify_new(new) -> Basic:
  906. if isinstance(new, (str, type)):
  907. # Allow a type or parse a string input
  908. return sympify(new, strict=False)
  909. else:
  910. return sympify(new, strict=True)
  911. sequence = [(sympify_old(s1), sympify_new(s2)) for s1, s2 in items]
  912. # skip if there is no change
  913. sequence = [(s1, s2) for s1, s2 in sequence if not _aresame(s1, s2)]
  914. simultaneous = kwargs.pop('simultaneous', False)
  915. if unordered:
  916. from .sorting import _nodes, default_sort_key
  917. sequence_dict = dict(sequence)
  918. # order so more complex items are first and items
  919. # of identical complexity are ordered so
  920. # f(x) < f(y) < x < y
  921. # \___ 2 __/ \_1_/ <- number of nodes
  922. #
  923. # For more complex ordering use an unordered sequence.
  924. k = list(ordered(sequence_dict, default=False, keys=(
  925. lambda x: -_nodes(x),
  926. default_sort_key,
  927. )))
  928. sequence = [(k, sequence_dict[k]) for k in k]
  929. # do infinities first
  930. if not simultaneous:
  931. redo = [i for i, seq in enumerate(sequence) if seq[1] in _illegal]
  932. for i in reversed(redo):
  933. sequence.insert(0, sequence.pop(i))
  934. if simultaneous: # XXX should this be the default for dict subs?
  935. reps = {}
  936. rv = self
  937. kwargs['hack2'] = True
  938. m = Dummy('subs_m')
  939. for old, new in sequence:
  940. com = new.is_commutative
  941. if com is None:
  942. com = True
  943. d = Dummy('subs_d', commutative=com)
  944. # using d*m so Subs will be used on dummy variables
  945. # in things like Derivative(f(x, y), x) in which x
  946. # is both free and bound
  947. rv = rv._subs(old, d*m, **kwargs)
  948. if not isinstance(rv, Basic):
  949. break
  950. reps[d] = new
  951. reps[m] = S.One # get rid of m
  952. return rv.xreplace(reps)
  953. else:
  954. rv = self
  955. for old, new in sequence:
  956. rv = rv._subs(old, new, **kwargs)
  957. if not isinstance(rv, Basic):
  958. break
  959. return rv
  960. @cacheit
  961. def _subs(self, old, new, **hints):
  962. """Substitutes an expression old -> new.
  963. If self is not equal to old then _eval_subs is called.
  964. If _eval_subs does not want to make any special replacement
  965. then a None is received which indicates that the fallback
  966. should be applied wherein a search for replacements is made
  967. amongst the arguments of self.
  968. >>> from sympy import Add
  969. >>> from sympy.abc import x, y, z
  970. Examples
  971. ========
  972. Add's _eval_subs knows how to target x + y in the following
  973. so it makes the change:
  974. >>> (x + y + z).subs(x + y, 1)
  975. z + 1
  976. Add's _eval_subs does not need to know how to find x + y in
  977. the following:
  978. >>> Add._eval_subs(z*(x + y) + 3, x + y, 1) is None
  979. True
  980. The returned None will cause the fallback routine to traverse the args and
  981. pass the z*(x + y) arg to Mul where the change will take place and the
  982. substitution will succeed:
  983. >>> (z*(x + y) + 3).subs(x + y, 1)
  984. z + 3
  985. ** Developers Notes **
  986. An _eval_subs routine for a class should be written if:
  987. 1) any arguments are not instances of Basic (e.g. bool, tuple);
  988. 2) some arguments should not be targeted (as in integration
  989. variables);
  990. 3) if there is something other than a literal replacement
  991. that should be attempted (as in Piecewise where the condition
  992. may be updated without doing a replacement).
  993. If it is overridden, here are some special cases that might arise:
  994. 1) If it turns out that no special change was made and all
  995. the original sub-arguments should be checked for
  996. replacements then None should be returned.
  997. 2) If it is necessary to do substitutions on a portion of
  998. the expression then _subs should be called. _subs will
  999. handle the case of any sub-expression being equal to old
  1000. (which usually would not be the case) while its fallback
  1001. will handle the recursion into the sub-arguments. For
  1002. example, after Add's _eval_subs removes some matching terms
  1003. it must process the remaining terms so it calls _subs
  1004. on each of the un-matched terms and then adds them
  1005. onto the terms previously obtained.
  1006. 3) If the initial expression should remain unchanged then
  1007. the original expression should be returned. (Whenever an
  1008. expression is returned, modified or not, no further
  1009. substitution of old -> new is attempted.) Sum's _eval_subs
  1010. routine uses this strategy when a substitution is attempted
  1011. on any of its summation variables.
  1012. """
  1013. def fallback(self, old, new):
  1014. """
  1015. Try to replace old with new in any of self's arguments.
  1016. """
  1017. hit = False
  1018. args = list(self.args)
  1019. for i, arg in enumerate(args):
  1020. if not hasattr(arg, '_eval_subs'):
  1021. continue
  1022. arg = arg._subs(old, new, **hints)
  1023. if not _aresame(arg, args[i]):
  1024. hit = True
  1025. args[i] = arg
  1026. if hit:
  1027. rv = self.func(*args)
  1028. hack2 = hints.get('hack2', False)
  1029. if hack2 and self.is_Mul and not rv.is_Mul: # 2-arg hack
  1030. coeff = S.One
  1031. nonnumber = []
  1032. for i in args:
  1033. if i.is_Number:
  1034. coeff *= i
  1035. else:
  1036. nonnumber.append(i)
  1037. nonnumber = self.func(*nonnumber)
  1038. if coeff is S.One:
  1039. return nonnumber
  1040. else:
  1041. return self.func(coeff, nonnumber, evaluate=False)
  1042. return rv
  1043. return self
  1044. if _aresame(self, old):
  1045. return new
  1046. rv = self._eval_subs(old, new)
  1047. if rv is None:
  1048. rv = fallback(self, old, new)
  1049. return rv
  1050. def _eval_subs(self, old, new) -> Basic | None:
  1051. """Override this stub if you want to do anything more than
  1052. attempt a replacement of old with new in the arguments of self.
  1053. See also
  1054. ========
  1055. _subs
  1056. """
  1057. return None
  1058. def xreplace(self, rule):
  1059. """
  1060. Replace occurrences of objects within the expression.
  1061. Parameters
  1062. ==========
  1063. rule : dict-like
  1064. Expresses a replacement rule
  1065. Returns
  1066. =======
  1067. xreplace : the result of the replacement
  1068. Examples
  1069. ========
  1070. >>> from sympy import symbols, pi, exp
  1071. >>> x, y, z = symbols('x y z')
  1072. >>> (1 + x*y).xreplace({x: pi})
  1073. pi*y + 1
  1074. >>> (1 + x*y).xreplace({x: pi, y: 2})
  1075. 1 + 2*pi
  1076. Replacements occur only if an entire node in the expression tree is
  1077. matched:
  1078. >>> (x*y + z).xreplace({x*y: pi})
  1079. z + pi
  1080. >>> (x*y*z).xreplace({x*y: pi})
  1081. x*y*z
  1082. >>> (2*x).xreplace({2*x: y, x: z})
  1083. y
  1084. >>> (2*2*x).xreplace({2*x: y, x: z})
  1085. 4*z
  1086. >>> (x + y + 2).xreplace({x + y: 2})
  1087. x + y + 2
  1088. >>> (x + 2 + exp(x + 2)).xreplace({x + 2: y})
  1089. x + exp(y) + 2
  1090. xreplace does not differentiate between free and bound symbols. In the
  1091. following, subs(x, y) would not change x since it is a bound symbol,
  1092. but xreplace does:
  1093. >>> from sympy import Integral
  1094. >>> Integral(x, (x, 1, 2*x)).xreplace({x: y})
  1095. Integral(y, (y, 1, 2*y))
  1096. Trying to replace x with an expression raises an error:
  1097. >>> Integral(x, (x, 1, 2*x)).xreplace({x: 2*y}) # doctest: +SKIP
  1098. ValueError: Invalid limits given: ((2*y, 1, 4*y),)
  1099. See Also
  1100. ========
  1101. replace: replacement capable of doing wildcard-like matching,
  1102. parsing of match, and conditional replacements
  1103. subs: substitution of subexpressions as defined by the objects
  1104. themselves.
  1105. """
  1106. value, _ = self._xreplace(rule)
  1107. return value
  1108. def _xreplace(self, rule):
  1109. """
  1110. Helper for xreplace. Tracks whether a replacement actually occurred.
  1111. """
  1112. if self in rule:
  1113. return rule[self], True
  1114. elif rule:
  1115. args = []
  1116. changed = False
  1117. for a in self.args:
  1118. _xreplace = getattr(a, '_xreplace', None)
  1119. if _xreplace is not None:
  1120. a_xr = _xreplace(rule)
  1121. args.append(a_xr[0])
  1122. changed |= a_xr[1]
  1123. else:
  1124. args.append(a)
  1125. args = tuple(args)
  1126. if changed:
  1127. return self.func(*args), True
  1128. return self, False
  1129. @cacheit
  1130. def has(self, *patterns):
  1131. """
  1132. Test whether any subexpression matches any of the patterns.
  1133. Examples
  1134. ========
  1135. >>> from sympy import sin
  1136. >>> from sympy.abc import x, y, z
  1137. >>> (x**2 + sin(x*y)).has(z)
  1138. False
  1139. >>> (x**2 + sin(x*y)).has(x, y, z)
  1140. True
  1141. >>> x.has(x)
  1142. True
  1143. Note ``has`` is a structural algorithm with no knowledge of
  1144. mathematics. Consider the following half-open interval:
  1145. >>> from sympy import Interval
  1146. >>> i = Interval.Lopen(0, 5); i
  1147. Interval.Lopen(0, 5)
  1148. >>> i.args
  1149. (0, 5, True, False)
  1150. >>> i.has(4) # there is no "4" in the arguments
  1151. False
  1152. >>> i.has(0) # there *is* a "0" in the arguments
  1153. True
  1154. Instead, use ``contains`` to determine whether a number is in the
  1155. interval or not:
  1156. >>> i.contains(4)
  1157. True
  1158. >>> i.contains(0)
  1159. False
  1160. Note that ``expr.has(*patterns)`` is exactly equivalent to
  1161. ``any(expr.has(p) for p in patterns)``. In particular, ``False`` is
  1162. returned when the list of patterns is empty.
  1163. >>> x.has()
  1164. False
  1165. """
  1166. return self._has(iterargs, *patterns)
  1167. def has_xfree(self, s: set[Basic]):
  1168. """Return True if self has any of the patterns in s as a
  1169. free argument, else False. This is like `Basic.has_free`
  1170. but this will only report exact argument matches.
  1171. Examples
  1172. ========
  1173. >>> from sympy import Function
  1174. >>> from sympy.abc import x, y
  1175. >>> f = Function('f')
  1176. >>> f(x).has_xfree({f})
  1177. False
  1178. >>> f(x).has_xfree({f(x)})
  1179. True
  1180. >>> f(x + 1).has_xfree({x})
  1181. True
  1182. >>> f(x + 1).has_xfree({x + 1})
  1183. True
  1184. >>> f(x + y + 1).has_xfree({x + 1})
  1185. False
  1186. """
  1187. # protect O(1) containment check by requiring:
  1188. if type(s) is not set:
  1189. raise TypeError('expecting set argument')
  1190. return any(a in s for a in iterfreeargs(self))
  1191. @cacheit
  1192. def has_free(self, *patterns):
  1193. """Return True if self has object(s) ``x`` as a free expression
  1194. else False.
  1195. Examples
  1196. ========
  1197. >>> from sympy import Integral, Function
  1198. >>> from sympy.abc import x, y
  1199. >>> f = Function('f')
  1200. >>> g = Function('g')
  1201. >>> expr = Integral(f(x), (f(x), 1, g(y)))
  1202. >>> expr.free_symbols
  1203. {y}
  1204. >>> expr.has_free(g(y))
  1205. True
  1206. >>> expr.has_free(*(x, f(x)))
  1207. False
  1208. This works for subexpressions and types, too:
  1209. >>> expr.has_free(g)
  1210. True
  1211. >>> (x + y + 1).has_free(y + 1)
  1212. True
  1213. """
  1214. if not patterns:
  1215. return False
  1216. p0 = patterns[0]
  1217. if len(patterns) == 1 and iterable(p0) and not isinstance(p0, Basic):
  1218. # Basic can contain iterables (though not non-Basic, ideally)
  1219. # but don't encourage mixed passing patterns
  1220. raise TypeError(filldedent('''
  1221. Expecting 1 or more Basic args, not a single
  1222. non-Basic iterable. Don't forget to unpack
  1223. iterables: `eq.has_free(*patterns)`'''))
  1224. # try quick test first
  1225. s = set(patterns)
  1226. rv = self.has_xfree(s)
  1227. if rv:
  1228. return rv
  1229. # now try matching through slower _has
  1230. return self._has(iterfreeargs, *patterns)
  1231. def _has(self, iterargs, *patterns):
  1232. # separate out types and unhashable objects
  1233. type_set = set() # only types
  1234. p_set = set() # hashable non-types
  1235. for p in patterns:
  1236. if isinstance(p, type) and issubclass(p, Basic):
  1237. type_set.add(p)
  1238. continue
  1239. if not isinstance(p, Basic):
  1240. try:
  1241. p = _sympify(p)
  1242. except SympifyError:
  1243. continue # Basic won't have this in it
  1244. p_set.add(p) # fails if object defines __eq__ but
  1245. # doesn't define __hash__
  1246. types = tuple(type_set) #
  1247. for i in iterargs(self): #
  1248. if i in p_set: # <--- here, too
  1249. return True
  1250. if isinstance(i, types):
  1251. return True
  1252. # use matcher if defined, e.g. operations defines
  1253. # matcher that checks for exact subset containment,
  1254. # (x + y + 1).has(x + 1) -> True
  1255. for i in p_set - type_set: # types don't have matchers
  1256. if not hasattr(i, '_has_matcher'):
  1257. continue
  1258. match = i._has_matcher()
  1259. if any(match(arg) for arg in iterargs(self)):
  1260. return True
  1261. # no success
  1262. return False
  1263. def replace(self, query, value, map=False, simultaneous=True, exact=None) -> Basic:
  1264. """
  1265. Replace matching subexpressions of ``self`` with ``value``.
  1266. If ``map = True`` then also return the mapping {old: new} where ``old``
  1267. was a sub-expression found with query and ``new`` is the replacement
  1268. value for it. If the expression itself does not match the query, then
  1269. the returned value will be ``self.xreplace(map)`` otherwise it should
  1270. be ``self.subs(ordered(map.items()))``.
  1271. Traverses an expression tree and performs replacement of matching
  1272. subexpressions from the bottom to the top of the tree. The default
  1273. approach is to do the replacement in a simultaneous fashion so
  1274. changes made are targeted only once. If this is not desired or causes
  1275. problems, ``simultaneous`` can be set to False.
  1276. In addition, if an expression containing more than one Wild symbol
  1277. is being used to match subexpressions and the ``exact`` flag is None
  1278. it will be set to True so the match will only succeed if all non-zero
  1279. values are received for each Wild that appears in the match pattern.
  1280. Setting this to False accepts a match of 0; while setting it True
  1281. accepts all matches that have a 0 in them. See example below for
  1282. cautions.
  1283. The list of possible combinations of queries and replacement values
  1284. is listed below:
  1285. Examples
  1286. ========
  1287. Initial setup
  1288. >>> from sympy import log, sin, cos, tan, Wild, Mul, Add
  1289. >>> from sympy.abc import x, y
  1290. >>> f = log(sin(x)) + tan(sin(x**2))
  1291. 1.1. type -> type
  1292. obj.replace(type, newtype)
  1293. When object of type ``type`` is found, replace it with the
  1294. result of passing its argument(s) to ``newtype``.
  1295. >>> f.replace(sin, cos)
  1296. log(cos(x)) + tan(cos(x**2))
  1297. >>> sin(x).replace(sin, cos, map=True)
  1298. (cos(x), {sin(x): cos(x)})
  1299. >>> (x*y).replace(Mul, Add)
  1300. x + y
  1301. 1.2. type -> func
  1302. obj.replace(type, func)
  1303. When object of type ``type`` is found, apply ``func`` to its
  1304. argument(s). ``func`` must be written to handle the number
  1305. of arguments of ``type``.
  1306. >>> f.replace(sin, lambda arg: sin(2*arg))
  1307. log(sin(2*x)) + tan(sin(2*x**2))
  1308. >>> (x*y).replace(Mul, lambda *args: sin(2*Mul(*args)))
  1309. sin(2*x*y)
  1310. 2.1. pattern -> expr
  1311. obj.replace(pattern(wild), expr(wild))
  1312. Replace subexpressions matching ``pattern`` with the expression
  1313. written in terms of the Wild symbols in ``pattern``.
  1314. >>> a, b = map(Wild, 'ab')
  1315. >>> f.replace(sin(a), tan(a))
  1316. log(tan(x)) + tan(tan(x**2))
  1317. >>> f.replace(sin(a), tan(a/2))
  1318. log(tan(x/2)) + tan(tan(x**2/2))
  1319. >>> f.replace(sin(a), a)
  1320. log(x) + tan(x**2)
  1321. >>> (x*y).replace(a*x, a)
  1322. y
  1323. Matching is exact by default when more than one Wild symbol
  1324. is used: matching fails unless the match gives non-zero
  1325. values for all Wild symbols:
  1326. >>> (2*x + y).replace(a*x + b, b - a)
  1327. y - 2
  1328. >>> (2*x).replace(a*x + b, b - a)
  1329. 2*x
  1330. When set to False, the results may be non-intuitive:
  1331. >>> (2*x).replace(a*x + b, b - a, exact=False)
  1332. 2/x
  1333. 2.2. pattern -> func
  1334. obj.replace(pattern(wild), lambda wild: expr(wild))
  1335. All behavior is the same as in 2.1 but now a function in terms of
  1336. pattern variables is used rather than an expression:
  1337. >>> f.replace(sin(a), lambda a: sin(2*a))
  1338. log(sin(2*x)) + tan(sin(2*x**2))
  1339. 3.1. func -> func
  1340. obj.replace(filter, func)
  1341. Replace subexpression ``e`` with ``func(e)`` if ``filter(e)``
  1342. is True.
  1343. >>> g = 2*sin(x**3)
  1344. >>> g.replace(lambda expr: expr.is_Number, lambda expr: expr**2)
  1345. 4*sin(x**9)
  1346. The expression itself is also targeted by the query but is done in
  1347. such a fashion that changes are not made twice.
  1348. >>> e = x*(x*y + 1)
  1349. >>> e.replace(lambda x: x.is_Mul, lambda x: 2*x)
  1350. 2*x*(2*x*y + 1)
  1351. When matching a single symbol, `exact` will default to True, but
  1352. this may or may not be the behavior that is desired:
  1353. Here, we want `exact=False`:
  1354. >>> from sympy import Function
  1355. >>> f = Function('f')
  1356. >>> e = f(1) + f(0)
  1357. >>> q = f(a), lambda a: f(a + 1)
  1358. >>> e.replace(*q, exact=False)
  1359. f(1) + f(2)
  1360. >>> e.replace(*q, exact=True)
  1361. f(0) + f(2)
  1362. But here, the nature of matching makes selecting
  1363. the right setting tricky:
  1364. >>> e = x**(1 + y)
  1365. >>> (x**(1 + y)).replace(x**(1 + a), lambda a: x**-a, exact=False)
  1366. x
  1367. >>> (x**(1 + y)).replace(x**(1 + a), lambda a: x**-a, exact=True)
  1368. x**(-x - y + 1)
  1369. >>> (x**y).replace(x**(1 + a), lambda a: x**-a, exact=False)
  1370. x
  1371. >>> (x**y).replace(x**(1 + a), lambda a: x**-a, exact=True)
  1372. x**(1 - y)
  1373. It is probably better to use a different form of the query
  1374. that describes the target expression more precisely:
  1375. >>> (1 + x**(1 + y)).replace(
  1376. ... lambda x: x.is_Pow and x.exp.is_Add and x.exp.args[0] == 1,
  1377. ... lambda x: x.base**(1 - (x.exp - 1)))
  1378. ...
  1379. x**(1 - y) + 1
  1380. See Also
  1381. ========
  1382. subs: substitution of subexpressions as defined by the objects
  1383. themselves.
  1384. xreplace: exact node replacement in expr tree; also capable of
  1385. using matching rules
  1386. """
  1387. try:
  1388. query = _sympify(query)
  1389. except SympifyError:
  1390. pass
  1391. try:
  1392. value = _sympify(value)
  1393. except SympifyError:
  1394. pass
  1395. if isinstance(query, type):
  1396. _query = lambda expr: isinstance(expr, query)
  1397. if isinstance(value, type):
  1398. _value = lambda expr, result: value(*expr.args)
  1399. elif callable(value):
  1400. _value = lambda expr, result: value(*expr.args)
  1401. else:
  1402. raise TypeError(
  1403. "given a type, replace() expects another "
  1404. "type or a callable")
  1405. elif isinstance(query, Basic):
  1406. _query = lambda expr: expr.match(query)
  1407. if exact is None:
  1408. from .symbol import Wild
  1409. exact = (len(query.atoms(Wild)) > 1)
  1410. if isinstance(value, Basic):
  1411. if exact:
  1412. _value = lambda expr, result: (value.subs(result)
  1413. if all(result.values()) else expr)
  1414. else:
  1415. _value = lambda expr, result: value.subs(result)
  1416. elif callable(value):
  1417. # match dictionary keys get the trailing underscore stripped
  1418. # from them and are then passed as keywords to the callable;
  1419. # if ``exact`` is True, only accept match if there are no null
  1420. # values amongst those matched.
  1421. if exact:
  1422. _value = lambda expr, result: (value(**
  1423. {str(k)[:-1]: v for k, v in result.items()})
  1424. if all(val for val in result.values()) else expr)
  1425. else:
  1426. _value = lambda expr, result: value(**
  1427. {str(k)[:-1]: v for k, v in result.items()})
  1428. else:
  1429. raise TypeError(
  1430. "given an expression, replace() expects "
  1431. "another expression or a callable")
  1432. elif callable(query):
  1433. _query = query
  1434. if callable(value):
  1435. _value = lambda expr, result: value(expr)
  1436. else:
  1437. raise TypeError(
  1438. "given a callable, replace() expects "
  1439. "another callable")
  1440. else:
  1441. raise TypeError(
  1442. "first argument to replace() must be a "
  1443. "type, an expression or a callable")
  1444. def walk(rv, F):
  1445. """Apply ``F`` to args and then to result.
  1446. """
  1447. args = getattr(rv, 'args', None)
  1448. if args is not None:
  1449. if args:
  1450. newargs = tuple([walk(a, F) for a in args])
  1451. if args != newargs:
  1452. rv = rv.func(*newargs)
  1453. if simultaneous:
  1454. # if rv is something that was already
  1455. # matched (that was changed) then skip
  1456. # applying F again
  1457. for i, e in enumerate(args):
  1458. if rv == e and e != newargs[i]:
  1459. return rv
  1460. rv = F(rv)
  1461. return rv
  1462. mapping = {} # changes that took place
  1463. def rec_replace(expr):
  1464. result = _query(expr)
  1465. if result or result == {}:
  1466. v = _value(expr, result)
  1467. if v is not None and v != expr:
  1468. if map:
  1469. mapping[expr] = v
  1470. expr = v
  1471. return expr
  1472. rv = walk(self, rec_replace)
  1473. return (rv, mapping) if map else rv # type: ignore
  1474. def find(self, query, group=False):
  1475. """Find all subexpressions matching a query."""
  1476. query = _make_find_query(query)
  1477. results = list(filter(query, _preorder_traversal(self)))
  1478. if not group:
  1479. return set(results)
  1480. return dict(Counter(results))
  1481. def count(self, query):
  1482. """Count the number of matching subexpressions."""
  1483. query = _make_find_query(query)
  1484. return sum(bool(query(sub)) for sub in _preorder_traversal(self))
  1485. def matches(self, expr, repl_dict=None, old=False):
  1486. """
  1487. Helper method for match() that looks for a match between Wild symbols
  1488. in self and expressions in expr.
  1489. Examples
  1490. ========
  1491. >>> from sympy import symbols, Wild, Basic
  1492. >>> a, b, c = symbols('a b c')
  1493. >>> x = Wild('x')
  1494. >>> Basic(a + x, x).matches(Basic(a + b, c)) is None
  1495. True
  1496. >>> Basic(a + x, x).matches(Basic(a + b + c, b + c))
  1497. {x_: b + c}
  1498. """
  1499. expr = sympify(expr)
  1500. if not isinstance(expr, self.__class__):
  1501. return None
  1502. if repl_dict is None:
  1503. repl_dict = {}
  1504. else:
  1505. repl_dict = repl_dict.copy()
  1506. if self == expr:
  1507. return repl_dict
  1508. if len(self.args) != len(expr.args):
  1509. return None
  1510. d = repl_dict # already a copy
  1511. for arg, other_arg in zip(self.args, expr.args):
  1512. if arg == other_arg:
  1513. continue
  1514. if arg.is_Relational:
  1515. try:
  1516. d = arg.xreplace(d).matches(other_arg, d, old=old)
  1517. except TypeError: # Should be InvalidComparisonError when introduced
  1518. d = None
  1519. else:
  1520. d = arg.xreplace(d).matches(other_arg, d, old=old)
  1521. if d is None:
  1522. return None
  1523. return d
  1524. def match(self, pattern, old=False):
  1525. """
  1526. Pattern matching.
  1527. Wild symbols match all.
  1528. Return ``None`` when expression (self) does not match with pattern.
  1529. Otherwise return a dictionary such that::
  1530. pattern.xreplace(self.match(pattern)) == self
  1531. Examples
  1532. ========
  1533. >>> from sympy import Wild, Sum
  1534. >>> from sympy.abc import x, y
  1535. >>> p = Wild("p")
  1536. >>> q = Wild("q")
  1537. >>> r = Wild("r")
  1538. >>> e = (x+y)**(x+y)
  1539. >>> e.match(p**p)
  1540. {p_: x + y}
  1541. >>> e.match(p**q)
  1542. {p_: x + y, q_: x + y}
  1543. >>> e = (2*x)**2
  1544. >>> e.match(p*q**r)
  1545. {p_: 4, q_: x, r_: 2}
  1546. >>> (p*q**r).xreplace(e.match(p*q**r))
  1547. 4*x**2
  1548. Since match is purely structural expressions that are equivalent up to
  1549. bound symbols will not match:
  1550. >>> print(Sum(x, (x, 1, 2)).match(Sum(y, (y, 1, p))))
  1551. None
  1552. An expression with bound symbols can be matched if the pattern uses
  1553. a distinct ``Wild`` for each bound symbol:
  1554. >>> Sum(x, (x, 1, 2)).match(Sum(q, (q, 1, p)))
  1555. {p_: 2, q_: x}
  1556. The ``old`` flag will give the old-style pattern matching where
  1557. expressions and patterns are essentially solved to give the match. Both
  1558. of the following give None unless ``old=True``:
  1559. >>> (x - 2).match(p - x, old=True)
  1560. {p_: 2*x - 2}
  1561. >>> (2/x).match(p*x, old=True)
  1562. {p_: 2/x**2}
  1563. See Also
  1564. ========
  1565. matches: pattern.matches(expr) is the same as expr.match(pattern)
  1566. xreplace: exact structural replacement
  1567. replace: structural replacement with pattern matching
  1568. Wild: symbolic placeholders for expressions in pattern matching
  1569. """
  1570. pattern = sympify(pattern)
  1571. return pattern.matches(self, old=old)
  1572. def count_ops(self, visual=False):
  1573. """Wrapper for count_ops that returns the operation count."""
  1574. from .function import count_ops
  1575. return count_ops(self, visual)
  1576. def doit(self, **hints):
  1577. """Evaluate objects that are not evaluated by default like limits,
  1578. integrals, sums and products. All objects of this kind will be
  1579. evaluated recursively, unless some species were excluded via 'hints'
  1580. or unless the 'deep' hint was set to 'False'.
  1581. >>> from sympy import Integral
  1582. >>> from sympy.abc import x
  1583. >>> 2*Integral(x, x)
  1584. 2*Integral(x, x)
  1585. >>> (2*Integral(x, x)).doit()
  1586. x**2
  1587. >>> (2*Integral(x, x)).doit(deep=False)
  1588. 2*Integral(x, x)
  1589. """
  1590. if hints.get('deep', True):
  1591. terms = [term.doit(**hints) if isinstance(term, Basic) else term
  1592. for term in self.args]
  1593. return self.func(*terms)
  1594. else:
  1595. return self
  1596. def simplify(self, **kwargs) -> Basic:
  1597. """See the simplify function in sympy.simplify"""
  1598. from sympy.simplify.simplify import simplify
  1599. return simplify(self, **kwargs)
  1600. def refine(self, assumption=True):
  1601. """See the refine function in sympy.assumptions"""
  1602. from sympy.assumptions.refine import refine
  1603. return refine(self, assumption)
  1604. def _eval_derivative_n_times(self, s, n):
  1605. # This is the default evaluator for derivatives (as called by `diff`
  1606. # and `Derivative`), it will attempt a loop to derive the expression
  1607. # `n` times by calling the corresponding `_eval_derivative` method,
  1608. # while leaving the derivative unevaluated if `n` is symbolic. This
  1609. # method should be overridden if the object has a closed form for its
  1610. # symbolic n-th derivative.
  1611. from .numbers import Integer
  1612. if isinstance(n, (int, Integer)):
  1613. obj = self
  1614. for i in range(n):
  1615. prev = obj
  1616. obj = obj._eval_derivative(s)
  1617. if obj is None:
  1618. return None
  1619. elif obj == prev:
  1620. break
  1621. return obj
  1622. else:
  1623. return None
  1624. def rewrite(self, *args, deep=True, **hints):
  1625. """
  1626. Rewrite *self* using a defined rule.
  1627. Rewriting transforms an expression to another, which is mathematically
  1628. equivalent but structurally different. For example you can rewrite
  1629. trigonometric functions as complex exponentials or combinatorial
  1630. functions as gamma function.
  1631. This method takes a *pattern* and a *rule* as positional arguments.
  1632. *pattern* is optional parameter which defines the types of expressions
  1633. that will be transformed. If it is not passed, all possible expressions
  1634. will be rewritten. *rule* defines how the expression will be rewritten.
  1635. Parameters
  1636. ==========
  1637. args : Expr
  1638. A *rule*, or *pattern* and *rule*.
  1639. - *pattern* is a type or an iterable of types.
  1640. - *rule* can be any object.
  1641. deep : bool, optional
  1642. If ``True``, subexpressions are recursively transformed. Default is
  1643. ``True``.
  1644. Examples
  1645. ========
  1646. If *pattern* is unspecified, all possible expressions are transformed.
  1647. >>> from sympy import cos, sin, exp, I
  1648. >>> from sympy.abc import x
  1649. >>> expr = cos(x) + I*sin(x)
  1650. >>> expr.rewrite(exp)
  1651. exp(I*x)
  1652. Pattern can be a type or an iterable of types.
  1653. >>> expr.rewrite(sin, exp)
  1654. exp(I*x)/2 + cos(x) - exp(-I*x)/2
  1655. >>> expr.rewrite([cos,], exp)
  1656. exp(I*x)/2 + I*sin(x) + exp(-I*x)/2
  1657. >>> expr.rewrite([cos, sin], exp)
  1658. exp(I*x)
  1659. Rewriting behavior can be implemented by defining ``_eval_rewrite()``
  1660. method.
  1661. >>> from sympy import Expr, sqrt, pi
  1662. >>> class MySin(Expr):
  1663. ... def _eval_rewrite(self, rule, args, **hints):
  1664. ... x, = args
  1665. ... if rule == cos:
  1666. ... return cos(pi/2 - x, evaluate=False)
  1667. ... if rule == sqrt:
  1668. ... return sqrt(1 - cos(x)**2)
  1669. >>> MySin(MySin(x)).rewrite(cos)
  1670. cos(-cos(-x + pi/2) + pi/2)
  1671. >>> MySin(x).rewrite(sqrt)
  1672. sqrt(1 - cos(x)**2)
  1673. Defining ``_eval_rewrite_as_[...]()`` method is supported for backwards
  1674. compatibility reason. This may be removed in the future and using it is
  1675. discouraged.
  1676. >>> class MySin(Expr):
  1677. ... def _eval_rewrite_as_cos(self, *args, **hints):
  1678. ... x, = args
  1679. ... return cos(pi/2 - x, evaluate=False)
  1680. >>> MySin(x).rewrite(cos)
  1681. cos(-x + pi/2)
  1682. """
  1683. if not args:
  1684. return self
  1685. hints.update(deep=deep)
  1686. pattern = args[:-1]
  1687. rule = args[-1]
  1688. # Special case: map `abs` to `Abs`
  1689. if rule is abs:
  1690. from sympy.functions.elementary.complexes import Abs
  1691. rule = Abs
  1692. # support old design by _eval_rewrite_as_[...] method
  1693. if isinstance(rule, str):
  1694. method = "_eval_rewrite_as_%s" % rule
  1695. elif hasattr(rule, "__name__"):
  1696. # rule is class or function
  1697. clsname = rule.__name__
  1698. method = "_eval_rewrite_as_%s" % clsname
  1699. else:
  1700. # rule is instance
  1701. clsname = rule.__class__.__name__
  1702. method = "_eval_rewrite_as_%s" % clsname
  1703. if pattern:
  1704. if iterable(pattern[0]):
  1705. pattern = pattern[0]
  1706. pattern = tuple(p for p in pattern if self.has(p))
  1707. if not pattern:
  1708. return self
  1709. # hereafter, empty pattern is interpreted as all pattern.
  1710. return self._rewrite(pattern, rule, method, **hints)
  1711. def _rewrite(self, pattern, rule, method, **hints):
  1712. deep = hints.pop('deep', True)
  1713. if deep:
  1714. args = [a._rewrite(pattern, rule, method, **hints)
  1715. for a in self.args]
  1716. else:
  1717. args = self.args
  1718. if not pattern or any(isinstance(self, p) for p in pattern):
  1719. meth = getattr(self, method, None)
  1720. if meth is not None:
  1721. rewritten = meth(*args, **hints)
  1722. else:
  1723. rewritten = self._eval_rewrite(rule, args, **hints)
  1724. if rewritten is not None:
  1725. return rewritten
  1726. if not args:
  1727. return self
  1728. return self.func(*args)
  1729. def _eval_rewrite(self, rule, args, **hints):
  1730. return None
  1731. _constructor_postprocessor_mapping = {} # type: ignore
  1732. @classmethod
  1733. def _exec_constructor_postprocessors(cls, obj):
  1734. # WARNING: This API is experimental.
  1735. # This is an experimental API that introduces constructor
  1736. # postprosessors for SymPy Core elements. If an argument of a SymPy
  1737. # expression has a `_constructor_postprocessor_mapping` attribute, it will
  1738. # be interpreted as a dictionary containing lists of postprocessing
  1739. # functions for matching expression node names.
  1740. clsname = obj.__class__.__name__
  1741. postprocessors = {f for i in obj.args
  1742. for f in _get_postprocessors(clsname, type(i))}
  1743. for f in postprocessors:
  1744. obj = f(obj)
  1745. return obj
  1746. def _sage_(self):
  1747. """
  1748. Convert *self* to a symbolic expression of SageMath.
  1749. This version of the method is merely a placeholder.
  1750. """
  1751. old_method = self._sage_
  1752. from sage.interfaces.sympy import sympy_init # type: ignore
  1753. sympy_init() # may monkey-patch _sage_ method into self's class or superclasses
  1754. if old_method == self._sage_:
  1755. raise NotImplementedError('conversion to SageMath is not implemented')
  1756. else:
  1757. # call the freshly monkey-patched method
  1758. return self._sage_()
  1759. def could_extract_minus_sign(self) -> bool:
  1760. return False # see Expr.could_extract_minus_sign
  1761. def is_same(a, b, approx=None):
  1762. """Return True if a and b are structurally the same, else False.
  1763. If `approx` is supplied, it will be used to test whether two
  1764. numbers are the same or not. By default, only numbers of the
  1765. same type will compare equal, so S.Half != Float(0.5).
  1766. Examples
  1767. ========
  1768. In SymPy (unlike Python) two numbers do not compare the same if they are
  1769. not of the same type:
  1770. >>> from sympy import S
  1771. >>> 2.0 == S(2)
  1772. False
  1773. >>> 0.5 == S.Half
  1774. False
  1775. By supplying a function with which to compare two numbers, such
  1776. differences can be ignored. e.g. `equal_valued` will return True
  1777. for decimal numbers having a denominator that is a power of 2,
  1778. regardless of precision.
  1779. >>> from sympy import Float
  1780. >>> from sympy.core.numbers import equal_valued
  1781. >>> (S.Half/4).is_same(Float(0.125, 1), equal_valued)
  1782. True
  1783. >>> Float(1, 2).is_same(Float(1, 10), equal_valued)
  1784. True
  1785. But decimals without a power of 2 denominator will compare
  1786. as not being the same.
  1787. >>> Float(0.1, 9).is_same(Float(0.1, 10), equal_valued)
  1788. False
  1789. But arbitrary differences can be ignored by supplying a function
  1790. to test the equivalence of two numbers:
  1791. >>> import math
  1792. >>> Float(0.1, 9).is_same(Float(0.1, 10), math.isclose)
  1793. True
  1794. Other objects might compare the same even though types are not the
  1795. same. This routine will only return True if two expressions are
  1796. identical in terms of class types.
  1797. >>> from sympy import eye, Basic
  1798. >>> eye(1) == S(eye(1)) # mutable vs immutable
  1799. True
  1800. >>> Basic.is_same(eye(1), S(eye(1)))
  1801. False
  1802. """
  1803. from .numbers import Number
  1804. from .traversal import postorder_traversal as pot
  1805. for t in zip_longest(pot(a), pot(b)):
  1806. if None in t:
  1807. return False
  1808. a, b = t
  1809. if isinstance(a, Number):
  1810. if not isinstance(b, Number):
  1811. return False
  1812. if approx:
  1813. return approx(a, b)
  1814. if not (a == b and a.__class__ == b.__class__):
  1815. return False
  1816. return True
  1817. _aresame = Basic.is_same # for sake of others importing this
  1818. # key used by Mul and Add to make canonical args
  1819. _args_sortkey = cmp_to_key(Basic.compare)
  1820. # For all Basic subclasses _prepare_class_assumptions is called by
  1821. # Basic.__init_subclass__ but that method is not called for Basic itself so we
  1822. # call the function here instead.
  1823. _prepare_class_assumptions(Basic)
  1824. class Atom(Basic):
  1825. """
  1826. A parent class for atomic things. An atom is an expression with no subexpressions.
  1827. Examples
  1828. ========
  1829. Symbol, Number, Rational, Integer, ...
  1830. But not: Add, Mul, Pow, ...
  1831. """
  1832. is_Atom = True
  1833. __slots__ = ()
  1834. def matches(self, expr, repl_dict=None, old=False):
  1835. if self == expr:
  1836. if repl_dict is None:
  1837. return {}
  1838. return repl_dict.copy()
  1839. def xreplace(self, rule, hack2=False):
  1840. return rule.get(self, self)
  1841. def doit(self, **hints):
  1842. return self
  1843. @classmethod
  1844. def class_key(cls):
  1845. return 2, 0, cls.__name__
  1846. @cacheit
  1847. def sort_key(self, order=None):
  1848. return self.class_key(), (1, (str(self),)), S.One.sort_key(), S.One
  1849. def _eval_simplify(self, **kwargs):
  1850. return self
  1851. @property
  1852. def _sorted_args(self):
  1853. # this is here as a safeguard against accidentally using _sorted_args
  1854. # on Atoms -- they cannot be rebuilt as atom.func(*atom._sorted_args)
  1855. # since there are no args. So the calling routine should be checking
  1856. # to see that this property is not called for Atoms.
  1857. raise AttributeError('Atoms have no args. It might be necessary'
  1858. ' to make a check for Atoms in the calling code.')
  1859. def _atomic(e, recursive=False):
  1860. """Return atom-like quantities as far as substitution is
  1861. concerned: Derivatives, Functions and Symbols. Do not
  1862. return any 'atoms' that are inside such quantities unless
  1863. they also appear outside, too, unless `recursive` is True.
  1864. Examples
  1865. ========
  1866. >>> from sympy import Derivative, Function, cos
  1867. >>> from sympy.abc import x, y
  1868. >>> from sympy.core.basic import _atomic
  1869. >>> f = Function('f')
  1870. >>> _atomic(x + y)
  1871. {x, y}
  1872. >>> _atomic(x + f(y))
  1873. {x, f(y)}
  1874. >>> _atomic(Derivative(f(x), x) + cos(x) + y)
  1875. {y, cos(x), Derivative(f(x), x)}
  1876. """
  1877. pot = _preorder_traversal(e)
  1878. seen = set()
  1879. if isinstance(e, Basic):
  1880. free = getattr(e, "free_symbols", None)
  1881. if free is None:
  1882. return {e}
  1883. else:
  1884. return set()
  1885. from .symbol import Symbol
  1886. from .function import Derivative, Function
  1887. atoms = set()
  1888. for p in pot:
  1889. if p in seen:
  1890. pot.skip()
  1891. continue
  1892. seen.add(p)
  1893. if isinstance(p, Symbol) and p in free:
  1894. atoms.add(p)
  1895. elif isinstance(p, (Derivative, Function)):
  1896. if not recursive:
  1897. pot.skip()
  1898. atoms.add(p)
  1899. return atoms
  1900. def _make_find_query(query):
  1901. """Convert the argument of Basic.find() into a callable"""
  1902. try:
  1903. query = _sympify(query)
  1904. except SympifyError:
  1905. pass
  1906. if isinstance(query, type):
  1907. return lambda expr: isinstance(expr, query)
  1908. elif isinstance(query, Basic):
  1909. return lambda expr: expr.match(query) is not None
  1910. return query
  1911. # Delayed to avoid cyclic import
  1912. from .singleton import S
  1913. from .traversal import (preorder_traversal as _preorder_traversal,
  1914. iterargs, iterfreeargs)
  1915. preorder_traversal = deprecated(
  1916. """
  1917. Using preorder_traversal from the sympy.core.basic submodule is
  1918. deprecated.
  1919. Instead, use preorder_traversal from the top-level sympy namespace, like
  1920. sympy.preorder_traversal
  1921. """,
  1922. deprecated_since_version="1.10",
  1923. active_deprecations_target="deprecated-traversal-functions-moved",
  1924. )(_preorder_traversal)