function.py 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423
  1. """
  2. There are three types of functions implemented in SymPy:
  3. 1) defined functions (in the sense that they can be evaluated) like
  4. exp or sin; they have a name and a body:
  5. f = exp
  6. 2) undefined function which have a name but no body. Undefined
  7. functions can be defined using a Function class as follows:
  8. f = Function('f')
  9. (the result will be a Function instance)
  10. 3) anonymous function (or lambda function) which have a body (defined
  11. with dummy variables) but have no name:
  12. f = Lambda(x, exp(x)*x)
  13. f = Lambda((x, y), exp(x)*y)
  14. The fourth type of functions are composites, like (sin + cos)(x); these work in
  15. SymPy core, but are not yet part of SymPy.
  16. Examples
  17. ========
  18. >>> import sympy
  19. >>> f = sympy.Function("f")
  20. >>> from sympy.abc import x
  21. >>> f(x)
  22. f(x)
  23. >>> print(sympy.srepr(f(x).func))
  24. Function('f')
  25. >>> f(x).args
  26. (x,)
  27. """
  28. from __future__ import annotations
  29. from typing import Any
  30. from collections.abc import Iterable
  31. import copyreg
  32. from .add import Add
  33. from .basic import Basic, _atomic
  34. from .cache import cacheit
  35. from .containers import Tuple, Dict
  36. from .decorators import _sympifyit
  37. from .evalf import pure_complex
  38. from .expr import Expr, AtomicExpr
  39. from .logic import fuzzy_and, fuzzy_or, fuzzy_not, FuzzyBool
  40. from .mul import Mul
  41. from .numbers import Rational, Float, Integer
  42. from .operations import LatticeOp
  43. from .parameters import global_parameters
  44. from .rules import Transform
  45. from .singleton import S
  46. from .sympify import sympify, _sympify
  47. from .sorting import default_sort_key, ordered
  48. from sympy.utilities.exceptions import (sympy_deprecation_warning,
  49. SymPyDeprecationWarning, ignore_warnings)
  50. from sympy.utilities.iterables import (has_dups, sift, iterable,
  51. is_sequence, uniq, topological_sort)
  52. from sympy.utilities.lambdify import MPMATH_TRANSLATIONS
  53. from sympy.utilities.misc import as_int, filldedent, func_name
  54. import mpmath
  55. from mpmath.libmp.libmpf import prec_to_dps
  56. import inspect
  57. from collections import Counter
  58. def _coeff_isneg(a):
  59. """Return True if the leading Number is negative.
  60. Examples
  61. ========
  62. >>> from sympy.core.function import _coeff_isneg
  63. >>> from sympy import S, Symbol, oo, pi
  64. >>> _coeff_isneg(-3*pi)
  65. True
  66. >>> _coeff_isneg(S(3))
  67. False
  68. >>> _coeff_isneg(-oo)
  69. True
  70. >>> _coeff_isneg(Symbol('n', negative=True)) # coeff is 1
  71. False
  72. For matrix expressions:
  73. >>> from sympy import MatrixSymbol, sqrt
  74. >>> A = MatrixSymbol("A", 3, 3)
  75. >>> _coeff_isneg(-sqrt(2)*A)
  76. True
  77. >>> _coeff_isneg(sqrt(2)*A)
  78. False
  79. """
  80. if a.is_MatMul:
  81. a = a.args[0]
  82. if a.is_Mul:
  83. a = a.args[0]
  84. return a.is_Number and a.is_extended_negative
  85. class PoleError(Exception):
  86. pass
  87. class ArgumentIndexError(ValueError):
  88. def __str__(self):
  89. return ("Invalid operation with argument number %s for Function %s" %
  90. (self.args[1], self.args[0]))
  91. class BadSignatureError(TypeError):
  92. '''Raised when a Lambda is created with an invalid signature'''
  93. pass
  94. class BadArgumentsError(TypeError):
  95. '''Raised when a Lambda is called with an incorrect number of arguments'''
  96. pass
  97. # Python 3 version that does not raise a Deprecation warning
  98. def arity(cls):
  99. """Return the arity of the function if it is known, else None.
  100. Explanation
  101. ===========
  102. When default values are specified for some arguments, they are
  103. optional and the arity is reported as a tuple of possible values.
  104. Examples
  105. ========
  106. >>> from sympy import arity, log
  107. >>> arity(lambda x: x)
  108. 1
  109. >>> arity(log)
  110. (1, 2)
  111. >>> arity(lambda *x: sum(x)) is None
  112. True
  113. """
  114. eval_ = getattr(cls, 'eval', cls)
  115. parameters = inspect.signature(eval_).parameters.items()
  116. if [p for _, p in parameters if p.kind == p.VAR_POSITIONAL]:
  117. return
  118. p_or_k = [p for _, p in parameters if p.kind == p.POSITIONAL_OR_KEYWORD]
  119. # how many have no default and how many have a default value
  120. no, yes = map(len, sift(p_or_k,
  121. lambda p:p.default == p.empty, binary=True))
  122. return no if not yes else tuple(range(no, no + yes + 1))
  123. class FunctionClass(type):
  124. """
  125. Base class for function classes. FunctionClass is a subclass of type.
  126. Use Function('<function name>' [ , signature ]) to create
  127. undefined function classes.
  128. """
  129. _new = type.__new__
  130. def __init__(cls, *args, **kwargs):
  131. # honor kwarg value or class-defined value before using
  132. # the number of arguments in the eval function (if present)
  133. nargs = kwargs.pop('nargs', cls.__dict__.get('nargs', arity(cls)))
  134. if nargs is None and 'nargs' not in cls.__dict__:
  135. for supcls in cls.__mro__:
  136. if hasattr(supcls, '_nargs'):
  137. nargs = supcls._nargs
  138. break
  139. else:
  140. continue
  141. # Canonicalize nargs here; change to set in nargs.
  142. if is_sequence(nargs):
  143. if not nargs:
  144. raise ValueError(filldedent('''
  145. Incorrectly specified nargs as %s:
  146. if there are no arguments, it should be
  147. `nargs = 0`;
  148. if there are any number of arguments,
  149. it should be
  150. `nargs = None`''' % str(nargs)))
  151. nargs = tuple(ordered(set(nargs)))
  152. elif nargs is not None:
  153. nargs = (as_int(nargs),)
  154. cls._nargs = nargs
  155. # When __init__ is called from UndefinedFunction it is called with
  156. # just one arg but when it is called from subclassing Function it is
  157. # called with the usual (name, bases, namespace) type() signature.
  158. if len(args) == 3:
  159. namespace = args[2]
  160. if 'eval' in namespace and not isinstance(namespace['eval'], classmethod):
  161. raise TypeError("eval on Function subclasses should be a class method (defined with @classmethod)")
  162. @property
  163. def __signature__(self):
  164. """
  165. Allow Python 3's inspect.signature to give a useful signature for
  166. Function subclasses.
  167. """
  168. # Python 3 only, but backports (like the one in IPython) still might
  169. # call this.
  170. try:
  171. from inspect import signature
  172. except ImportError:
  173. return None
  174. # TODO: Look at nargs
  175. return signature(self.eval)
  176. @property
  177. def free_symbols(self):
  178. return set()
  179. @property
  180. def xreplace(self):
  181. # Function needs args so we define a property that returns
  182. # a function that takes args...and then use that function
  183. # to return the right value
  184. return lambda rule, **_: rule.get(self, self)
  185. @property
  186. def nargs(self):
  187. """Return a set of the allowed number of arguments for the function.
  188. Examples
  189. ========
  190. >>> from sympy import Function
  191. >>> f = Function('f')
  192. If the function can take any number of arguments, the set of whole
  193. numbers is returned:
  194. >>> Function('f').nargs
  195. Naturals0
  196. If the function was initialized to accept one or more arguments, a
  197. corresponding set will be returned:
  198. >>> Function('f', nargs=1).nargs
  199. {1}
  200. >>> Function('f', nargs=(2, 1)).nargs
  201. {1, 2}
  202. The undefined function, after application, also has the nargs
  203. attribute; the actual number of arguments is always available by
  204. checking the ``args`` attribute:
  205. >>> f = Function('f')
  206. >>> f(1).nargs
  207. Naturals0
  208. >>> len(f(1).args)
  209. 1
  210. """
  211. from sympy.sets.sets import FiniteSet
  212. # XXX it would be nice to handle this in __init__ but there are import
  213. # problems with trying to import FiniteSet there
  214. return FiniteSet(*self._nargs) if self._nargs else S.Naturals0
  215. def _valid_nargs(self, n : int) -> bool:
  216. """ Return True if the specified integer is a valid number of arguments
  217. The number of arguments n is guaranteed to be an integer and positive
  218. """
  219. if self._nargs:
  220. return n in self._nargs
  221. nargs = self.nargs
  222. return nargs is S.Naturals0 or n in nargs
  223. def __repr__(cls):
  224. return cls.__name__
  225. class Application(Basic, metaclass=FunctionClass):
  226. """
  227. Base class for applied functions.
  228. Explanation
  229. ===========
  230. Instances of Application represent the result of applying an application of
  231. any type to any object.
  232. """
  233. is_Function = True
  234. @cacheit
  235. def __new__(cls, *args, **options):
  236. from sympy.sets.fancysets import Naturals0
  237. from sympy.sets.sets import FiniteSet
  238. args = list(map(sympify, args))
  239. evaluate = options.pop('evaluate', global_parameters.evaluate)
  240. # WildFunction (and anything else like it) may have nargs defined
  241. # and we throw that value away here
  242. options.pop('nargs', None)
  243. if options:
  244. raise ValueError("Unknown options: %s" % options)
  245. if evaluate:
  246. evaluated = cls.eval(*args)
  247. if evaluated is not None:
  248. return evaluated
  249. obj = super().__new__(cls, *args, **options)
  250. # make nargs uniform here
  251. sentinel = object()
  252. objnargs = getattr(obj, "nargs", sentinel)
  253. if objnargs is not sentinel:
  254. # things passing through here:
  255. # - functions subclassed from Function (e.g. myfunc(1).nargs)
  256. # - functions like cos(1).nargs
  257. # - AppliedUndef with given nargs like Function('f', nargs=1)(1).nargs
  258. # Canonicalize nargs here
  259. if is_sequence(objnargs):
  260. nargs = tuple(ordered(set(objnargs)))
  261. elif objnargs is not None:
  262. nargs = (as_int(objnargs),)
  263. else:
  264. nargs = None
  265. else:
  266. # things passing through here:
  267. # - WildFunction('f').nargs
  268. # - AppliedUndef with no nargs like Function('f')(1).nargs
  269. nargs = obj._nargs # note the underscore here
  270. # convert to FiniteSet
  271. obj.nargs = FiniteSet(*nargs) if nargs else Naturals0()
  272. return obj
  273. @classmethod
  274. def eval(cls, *args):
  275. """
  276. Returns a canonical form of cls applied to arguments args.
  277. Explanation
  278. ===========
  279. The ``eval()`` method is called when the class ``cls`` is about to be
  280. instantiated and it should return either some simplified instance
  281. (possible of some other class), or if the class ``cls`` should be
  282. unmodified, return None.
  283. Examples of ``eval()`` for the function "sign"
  284. .. code-block:: python
  285. @classmethod
  286. def eval(cls, arg):
  287. if arg is S.NaN:
  288. return S.NaN
  289. if arg.is_zero: return S.Zero
  290. if arg.is_positive: return S.One
  291. if arg.is_negative: return S.NegativeOne
  292. if isinstance(arg, Mul):
  293. coeff, terms = arg.as_coeff_Mul(rational=True)
  294. if coeff is not S.One:
  295. return cls(coeff) * cls(terms)
  296. """
  297. return
  298. @property
  299. def func(self):
  300. return self.__class__
  301. def _eval_subs(self, old, new):
  302. if (old.is_Function and new.is_Function and
  303. callable(old) and callable(new) and
  304. old == self.func and len(self.args) in new.nargs):
  305. return new(*[i._subs(old, new) for i in self.args])
  306. class Function(Application, Expr):
  307. r"""
  308. Base class for applied mathematical functions.
  309. It also serves as a constructor for undefined function classes.
  310. See the :ref:`custom-functions` guide for details on how to subclass
  311. ``Function`` and what methods can be defined.
  312. Examples
  313. ========
  314. **Undefined Functions**
  315. To create an undefined function, pass a string of the function name to
  316. ``Function``.
  317. >>> from sympy import Function, Symbol
  318. >>> x = Symbol('x')
  319. >>> f = Function('f')
  320. >>> g = Function('g')(x)
  321. >>> f
  322. f
  323. >>> f(x)
  324. f(x)
  325. >>> g
  326. g(x)
  327. >>> f(x).diff(x)
  328. Derivative(f(x), x)
  329. >>> g.diff(x)
  330. Derivative(g(x), x)
  331. Assumptions can be passed to ``Function`` the same as with a
  332. :class:`~.Symbol`. Alternatively, you can use a ``Symbol`` with
  333. assumptions for the function name and the function will inherit the name
  334. and assumptions associated with the ``Symbol``:
  335. >>> f_real = Function('f', real=True)
  336. >>> f_real(x).is_real
  337. True
  338. >>> f_real_inherit = Function(Symbol('f', real=True))
  339. >>> f_real_inherit(x).is_real
  340. True
  341. Note that assumptions on a function are unrelated to the assumptions on
  342. the variables it is called on. If you want to add a relationship, subclass
  343. ``Function`` and define custom assumptions handler methods. See the
  344. :ref:`custom-functions-assumptions` section of the :ref:`custom-functions`
  345. guide for more details.
  346. **Custom Function Subclasses**
  347. The :ref:`custom-functions` guide has several
  348. :ref:`custom-functions-complete-examples` of how to subclass ``Function``
  349. to create a custom function.
  350. """
  351. @property
  352. def _diff_wrt(self):
  353. return False
  354. @cacheit
  355. def __new__(cls, *args, **options) -> type[AppliedUndef]: # type: ignore
  356. # Handle calls like Function('f')
  357. if cls is Function:
  358. return UndefinedFunction(*args, **options) # type: ignore
  359. else:
  360. return cls._new_(*args, **options) # type: ignore
  361. @classmethod
  362. def _new_(cls, *args, **options) -> Expr:
  363. n = len(args)
  364. if not cls._valid_nargs(n):
  365. # XXX: exception message must be in exactly this format to
  366. # make it work with NumPy's functions like vectorize(). See,
  367. # for example, https://github.com/numpy/numpy/issues/1697.
  368. # The ideal solution would be just to attach metadata to
  369. # the exception and change NumPy to take advantage of this.
  370. temp = ('%(name)s takes %(qual)s %(args)s '
  371. 'argument%(plural)s (%(given)s given)')
  372. raise TypeError(temp % {
  373. 'name': cls,
  374. 'qual': 'exactly' if len(cls.nargs) == 1 else 'at least',
  375. 'args': min(cls.nargs),
  376. 'plural': 's'*(min(cls.nargs) != 1),
  377. 'given': n})
  378. evaluate = options.get('evaluate', global_parameters.evaluate)
  379. result = super().__new__(cls, *args, **options)
  380. if evaluate and isinstance(result, cls) and result.args:
  381. _should_evalf = [cls._should_evalf(a) for a in result.args]
  382. pr2 = min(_should_evalf)
  383. if pr2 > 0:
  384. pr = max(_should_evalf)
  385. result = result.evalf(prec_to_dps(pr))
  386. return _sympify(result)
  387. @classmethod
  388. def _should_evalf(cls, arg):
  389. """
  390. Decide if the function should automatically evalf().
  391. Explanation
  392. ===========
  393. By default (in this implementation), this happens if (and only if) the
  394. ARG is a floating point number (including complex numbers).
  395. This function is used by __new__.
  396. Returns the precision to evalf to, or -1 if it should not evalf.
  397. """
  398. if arg.is_Float:
  399. return arg._prec
  400. if not arg.is_Add:
  401. return -1
  402. m = pure_complex(arg)
  403. if m is None:
  404. return -1
  405. # the elements of m are of type Number, so have a _prec
  406. return max(m[0]._prec, m[1]._prec)
  407. @classmethod
  408. def class_key(cls):
  409. from sympy.sets.fancysets import Naturals0
  410. funcs = {
  411. 'exp': 10,
  412. 'log': 11,
  413. 'sin': 20,
  414. 'cos': 21,
  415. 'tan': 22,
  416. 'cot': 23,
  417. 'sinh': 30,
  418. 'cosh': 31,
  419. 'tanh': 32,
  420. 'coth': 33,
  421. 'conjugate': 40,
  422. 're': 41,
  423. 'im': 42,
  424. 'arg': 43,
  425. }
  426. name = cls.__name__
  427. try:
  428. i = funcs[name]
  429. except KeyError:
  430. i = 0 if isinstance(cls.nargs, Naturals0) else 10000
  431. return 4, i, name
  432. def _eval_evalf(self, prec):
  433. def _get_mpmath_func(fname):
  434. """Lookup mpmath function based on name"""
  435. if isinstance(self, AppliedUndef):
  436. # Shouldn't lookup in mpmath but might have ._imp_
  437. return None
  438. if not hasattr(mpmath, fname):
  439. fname = MPMATH_TRANSLATIONS.get(fname, None)
  440. if fname is None:
  441. return None
  442. return getattr(mpmath, fname)
  443. _eval_mpmath = getattr(self, '_eval_mpmath', None)
  444. if _eval_mpmath is None:
  445. func = _get_mpmath_func(self.func.__name__)
  446. args = self.args
  447. else:
  448. func, args = _eval_mpmath()
  449. # Fall-back evaluation
  450. if func is None:
  451. imp = getattr(self, '_imp_', None)
  452. if imp is None:
  453. return None
  454. try:
  455. return Float(imp(*[i.evalf(prec) for i in self.args]), prec)
  456. except (TypeError, ValueError):
  457. return None
  458. # Convert all args to mpf or mpc
  459. # Convert the arguments to *higher* precision than requested for the
  460. # final result.
  461. # XXX + 5 is a guess, it is similar to what is used in evalf.py. Should
  462. # we be more intelligent about it?
  463. try:
  464. args = [arg._to_mpmath(prec + 5) for arg in args]
  465. def bad(m):
  466. from mpmath import mpf, mpc
  467. # the precision of an mpf value is the last element
  468. # if that is 1 (and m[1] is not 1 which would indicate a
  469. # power of 2), then the eval failed; so check that none of
  470. # the arguments failed to compute to a finite precision.
  471. # Note: An mpc value has two parts, the re and imag tuple;
  472. # check each of those parts, too. Anything else is allowed to
  473. # pass
  474. if isinstance(m, mpf):
  475. m = m._mpf_
  476. return m[1] !=1 and m[-1] == 1
  477. elif isinstance(m, mpc):
  478. m, n = m._mpc_
  479. return m[1] !=1 and m[-1] == 1 and \
  480. n[1] !=1 and n[-1] == 1
  481. else:
  482. return False
  483. if any(bad(a) for a in args):
  484. raise ValueError # one or more args failed to compute with significance
  485. except ValueError:
  486. return
  487. with mpmath.workprec(prec):
  488. v = func(*args)
  489. return Expr._from_mpmath(v, prec)
  490. def _eval_derivative(self, s):
  491. # f(x).diff(s) -> x.diff(s) * f.fdiff(1)(s)
  492. i = 0
  493. l = []
  494. for a in self.args:
  495. i += 1
  496. da = a.diff(s)
  497. if da.is_zero:
  498. continue
  499. try:
  500. df = self.fdiff(i)
  501. except ArgumentIndexError:
  502. df = Function.fdiff(self, i)
  503. l.append(df * da)
  504. return Add(*l)
  505. def _eval_is_commutative(self):
  506. return fuzzy_and(a.is_commutative for a in self.args)
  507. def _eval_is_meromorphic(self, x, a):
  508. if not self.args:
  509. return True
  510. if any(arg.has(x) for arg in self.args[1:]):
  511. return False
  512. arg = self.args[0]
  513. if not arg._eval_is_meromorphic(x, a):
  514. return None
  515. return fuzzy_not(type(self).is_singular(arg.subs(x, a)))
  516. _singularities: FuzzyBool | tuple[Expr, ...] = None
  517. @classmethod
  518. def is_singular(cls, a):
  519. """
  520. Tests whether the argument is an essential singularity
  521. or a branch point, or the functions is non-holomorphic.
  522. """
  523. ss = cls._singularities
  524. if ss in (True, None, False):
  525. return ss
  526. return fuzzy_or(a.is_infinite if s is S.ComplexInfinity
  527. else (a - s).is_zero for s in ss)
  528. def _eval_aseries(self, n, args0, x, logx):
  529. """
  530. Compute an asymptotic expansion around args0, in terms of self.args.
  531. This function is only used internally by _eval_nseries and should not
  532. be called directly; derived classes can overwrite this to implement
  533. asymptotic expansions.
  534. """
  535. raise PoleError(filldedent('''
  536. Asymptotic expansion of %s around %s is
  537. not implemented.''' % (type(self), args0)))
  538. def _eval_nseries(self, x, n, logx, cdir=0):
  539. """
  540. This function does compute series for multivariate functions,
  541. but the expansion is always in terms of *one* variable.
  542. Examples
  543. ========
  544. >>> from sympy import atan2
  545. >>> from sympy.abc import x, y
  546. >>> atan2(x, y).series(x, n=2)
  547. atan2(0, y) + x/y + O(x**2)
  548. >>> atan2(x, y).series(y, n=2)
  549. -y/x + atan2(x, 0) + O(y**2)
  550. This function also computes asymptotic expansions, if necessary
  551. and possible:
  552. >>> from sympy import loggamma
  553. >>> loggamma(1/x)._eval_nseries(x,0,None)
  554. -1/x - log(x)/x + log(x)/2 + O(1)
  555. """
  556. from .symbol import uniquely_named_symbol
  557. from sympy.series.order import Order
  558. from sympy.sets.sets import FiniteSet
  559. args = self.args
  560. args0 = [t.limit(x, 0) for t in args]
  561. if any(t.is_finite is False for t in args0):
  562. from .numbers import oo, zoo, nan
  563. a = [t.as_leading_term(x, logx=logx) for t in args]
  564. a0 = [t.limit(x, 0) for t in a]
  565. if any(t.has(oo, -oo, zoo, nan) for t in a0):
  566. return self._eval_aseries(n, args0, x, logx)
  567. # Careful: the argument goes to oo, but only logarithmically so. We
  568. # are supposed to do a power series expansion "around the
  569. # logarithmic term". e.g.
  570. # f(1+x+log(x))
  571. # -> f(1+logx) + x*f'(1+logx) + O(x**2)
  572. # where 'logx' is given in the argument
  573. a = [t._eval_nseries(x, n, logx) for t in args]
  574. z = [r - r0 for (r, r0) in zip(a, a0)]
  575. p = [Dummy() for _ in z]
  576. q = []
  577. v = None
  578. for ai, zi, pi in zip(a0, z, p):
  579. if zi.has(x):
  580. if v is not None:
  581. raise NotImplementedError
  582. q.append(ai + pi)
  583. v = pi
  584. else:
  585. q.append(ai)
  586. e1 = self.func(*q)
  587. if v is None:
  588. return e1
  589. s = e1._eval_nseries(v, n, logx)
  590. o = s.getO()
  591. s = s.removeO()
  592. s = s.subs(v, zi).expand() + Order(o.expr.subs(v, zi), x)
  593. return s
  594. if (self.func.nargs is S.Naturals0
  595. or (self.func.nargs == FiniteSet(1) and args0[0])
  596. or any(c > 1 for c in self.func.nargs)):
  597. e = self
  598. e1 = e.expand()
  599. if e == e1:
  600. #for example when e = sin(x+1) or e = sin(cos(x))
  601. #let's try the general algorithm
  602. if len(e.args) == 1:
  603. # issue 14411
  604. e = e.func(e.args[0].cancel())
  605. term = e.subs(x, S.Zero)
  606. if term.is_finite is False or term is S.NaN:
  607. raise PoleError("Cannot expand %s around 0" % (self))
  608. series = term
  609. fact = S.One
  610. _x = uniquely_named_symbol('xi', self)
  611. e = e.subs(x, _x)
  612. for i in range(1, n):
  613. fact *= Rational(i)
  614. e = e.diff(_x)
  615. subs = e.subs(_x, S.Zero)
  616. if subs is S.NaN:
  617. # try to evaluate a limit if we have to
  618. subs = e.limit(_x, S.Zero)
  619. if subs.is_finite is False:
  620. raise PoleError("Cannot expand %s around 0" % (self))
  621. term = subs*(x**i)/fact
  622. term = term.expand()
  623. series += term
  624. return series + Order(x**n, x)
  625. return e1.nseries(x, n=n, logx=logx)
  626. arg = self.args[0]
  627. l = []
  628. g = None
  629. # try to predict a number of terms needed
  630. nterms = n + 2
  631. cf = Order(arg.as_leading_term(x), x).getn()
  632. if cf != 0:
  633. nterms = (n/cf).ceiling()
  634. for i in range(nterms):
  635. g = self.taylor_term(i, arg, g)
  636. g = g.nseries(x, n=n, logx=logx)
  637. l.append(g)
  638. return Add(*l) + Order(x**n, x)
  639. def fdiff(self, argindex=1):
  640. """
  641. Returns the first derivative of the function.
  642. """
  643. if not (1 <= argindex <= len(self.args)):
  644. raise ArgumentIndexError(self, argindex)
  645. ix = argindex - 1
  646. A = self.args[ix]
  647. if A._diff_wrt:
  648. if len(self.args) == 1 or not A.is_Symbol:
  649. return _derivative_dispatch(self, A)
  650. for i, v in enumerate(self.args):
  651. if i != ix and A in v.free_symbols:
  652. # it can't be in any other argument's free symbols
  653. # issue 8510
  654. break
  655. else:
  656. return _derivative_dispatch(self, A)
  657. # See issue 4624 and issue 4719, 5600 and 8510
  658. D = Dummy('xi_%i' % argindex, dummy_index=hash(A))
  659. args = self.args[:ix] + (D,) + self.args[ix + 1:]
  660. return Subs(Derivative(self.func(*args), D), D, A)
  661. def _eval_as_leading_term(self, x, logx, cdir):
  662. """Stub that should be overridden by new Functions to return
  663. the first non-zero term in a series if ever an x-dependent
  664. argument whose leading term vanishes as x -> 0 might be encountered.
  665. See, for example, cos._eval_as_leading_term.
  666. """
  667. from sympy.series.order import Order
  668. args = [a.as_leading_term(x, logx=logx) for a in self.args]
  669. o = Order(1, x)
  670. if any(x in a.free_symbols and o.contains(a) for a in args):
  671. # Whereas x and any finite number are contained in O(1, x),
  672. # expressions like 1/x are not. If any arg simplified to a
  673. # vanishing expression as x -> 0 (like x or x**2, but not
  674. # 3, 1/x, etc...) then the _eval_as_leading_term is needed
  675. # to supply the first non-zero term of the series,
  676. #
  677. # e.g. expression leading term
  678. # ---------- ------------
  679. # cos(1/x) cos(1/x)
  680. # cos(cos(x)) cos(1)
  681. # cos(x) 1 <- _eval_as_leading_term needed
  682. # sin(x) x <- _eval_as_leading_term needed
  683. #
  684. raise NotImplementedError(
  685. '%s has no _eval_as_leading_term routine' % self.func)
  686. else:
  687. return self
  688. class DefinedFunction(Function):
  689. """Base class for defined functions like ``sin``, ``cos``, ..."""
  690. @cacheit
  691. def __new__(cls, *args, **options) -> Expr: # type: ignore
  692. return cls._new_(*args, **options)
  693. class AppliedUndef(Function):
  694. """
  695. Base class for expressions resulting from the application of an undefined
  696. function.
  697. """
  698. is_number = False
  699. name: str
  700. def __new__(cls, *args, **options) -> Expr: # type: ignore
  701. args = tuple(map(sympify, args))
  702. u = [a.name for a in args if isinstance(a, UndefinedFunction)]
  703. if u:
  704. raise TypeError('Invalid argument: expecting an expression, not UndefinedFunction%s: %s' % (
  705. 's'*(len(u) > 1), ', '.join(u)))
  706. obj: Expr = super().__new__(cls, *args, **options) # type: ignore
  707. return obj
  708. def _eval_as_leading_term(self, x, logx, cdir):
  709. return self
  710. @property
  711. def _diff_wrt(self):
  712. """
  713. Allow derivatives wrt to undefined functions.
  714. Examples
  715. ========
  716. >>> from sympy import Function, Symbol
  717. >>> f = Function('f')
  718. >>> x = Symbol('x')
  719. >>> f(x)._diff_wrt
  720. True
  721. >>> f(x).diff(x)
  722. Derivative(f(x), x)
  723. """
  724. return True
  725. class UndefSageHelper:
  726. """
  727. Helper to facilitate Sage conversion.
  728. """
  729. def __get__(self, ins, typ):
  730. import sage.all as sage
  731. if ins is None:
  732. return lambda: sage.function(typ.__name__)
  733. else:
  734. args = [arg._sage_() for arg in ins.args]
  735. return lambda : sage.function(ins.__class__.__name__)(*args)
  736. _undef_sage_helper = UndefSageHelper()
  737. class UndefinedFunction(FunctionClass):
  738. """
  739. The (meta)class of undefined functions.
  740. """
  741. name: str
  742. _sage_: UndefSageHelper
  743. def __new__(mcl, name, bases=(AppliedUndef,), __dict__=None, **kwargs) -> type[AppliedUndef]:
  744. from .symbol import _filter_assumptions
  745. # Allow Function('f', real=True)
  746. # and/or Function(Symbol('f', real=True))
  747. assumptions, kwargs = _filter_assumptions(kwargs)
  748. if isinstance(name, Symbol):
  749. assumptions = name._merge(assumptions)
  750. name = name.name
  751. elif not isinstance(name, str):
  752. raise TypeError('expecting string or Symbol for name')
  753. else:
  754. commutative = assumptions.get('commutative', None)
  755. assumptions = Symbol(name, **assumptions).assumptions0
  756. if commutative is None:
  757. assumptions.pop('commutative')
  758. __dict__ = __dict__ or {}
  759. # put the `is_*` for into __dict__
  760. __dict__.update({'is_%s' % k: v for k, v in assumptions.items()})
  761. # You can add other attributes, although they do have to be hashable
  762. # (but seriously, if you want to add anything other than assumptions,
  763. # just subclass Function)
  764. __dict__.update(kwargs)
  765. # add back the sanitized assumptions without the is_ prefix
  766. kwargs.update(assumptions)
  767. # Save these for __eq__
  768. __dict__.update({'_kwargs': kwargs})
  769. # do this for pickling
  770. __dict__['__module__'] = None
  771. obj = super().__new__(mcl, name, bases, __dict__) # type: ignore
  772. obj.name = name
  773. obj._sage_ = _undef_sage_helper
  774. return obj # type: ignore
  775. def __instancecheck__(cls, instance):
  776. return cls in type(instance).__mro__
  777. _kwargs: dict[str, bool | None] = {}
  778. def __hash__(self):
  779. return hash((self.class_key(), frozenset(self._kwargs.items())))
  780. def __eq__(self, other):
  781. return (isinstance(other, self.__class__) and
  782. self.class_key() == other.class_key() and
  783. self._kwargs == other._kwargs)
  784. def __ne__(self, other):
  785. return not self == other
  786. @property
  787. def _diff_wrt(self):
  788. return False
  789. # Using copyreg is the only way to make a dynamically generated instance of a
  790. # metaclass picklable without using a custom pickler. It is not possible to
  791. # define e.g. __reduce__ on the metaclass because obj.__reduce__ will retrieve
  792. # the __reduce__ method for reducing instances of the type rather than for the
  793. # type itself.
  794. def _reduce_undef(f):
  795. return (_rebuild_undef, (f.name, f._kwargs))
  796. def _rebuild_undef(name, kwargs):
  797. return Function(name, **kwargs)
  798. copyreg.pickle(UndefinedFunction, _reduce_undef)
  799. # XXX: The type: ignore on WildFunction is because mypy complains:
  800. #
  801. # sympy/core/function.py:939: error: Cannot determine type of 'sort_key' in
  802. # base class 'Expr'
  803. #
  804. # Somehow this is because of the @cacheit decorator but it is not clear how to
  805. # fix it.
  806. class WildFunction(Function, AtomicExpr): # type: ignore
  807. """
  808. A WildFunction function matches any function (with its arguments).
  809. Examples
  810. ========
  811. >>> from sympy import WildFunction, Function, cos
  812. >>> from sympy.abc import x, y
  813. >>> F = WildFunction('F')
  814. >>> f = Function('f')
  815. >>> F.nargs
  816. Naturals0
  817. >>> x.match(F)
  818. >>> F.match(F)
  819. {F_: F_}
  820. >>> f(x).match(F)
  821. {F_: f(x)}
  822. >>> cos(x).match(F)
  823. {F_: cos(x)}
  824. >>> f(x, y).match(F)
  825. {F_: f(x, y)}
  826. To match functions with a given number of arguments, set ``nargs`` to the
  827. desired value at instantiation:
  828. >>> F = WildFunction('F', nargs=2)
  829. >>> F.nargs
  830. {2}
  831. >>> f(x).match(F)
  832. >>> f(x, y).match(F)
  833. {F_: f(x, y)}
  834. To match functions with a range of arguments, set ``nargs`` to a tuple
  835. containing the desired number of arguments, e.g. if ``nargs = (1, 2)``
  836. then functions with 1 or 2 arguments will be matched.
  837. >>> F = WildFunction('F', nargs=(1, 2))
  838. >>> F.nargs
  839. {1, 2}
  840. >>> f(x).match(F)
  841. {F_: f(x)}
  842. >>> f(x, y).match(F)
  843. {F_: f(x, y)}
  844. >>> f(x, y, 1).match(F)
  845. """
  846. # XXX: What is this class attribute used for?
  847. include: set[Any] = set()
  848. def __init__(cls, name, **assumptions):
  849. from sympy.sets.sets import Set, FiniteSet
  850. cls.name = name
  851. nargs = assumptions.pop('nargs', S.Naturals0)
  852. if not isinstance(nargs, Set):
  853. # Canonicalize nargs here. See also FunctionClass.
  854. if is_sequence(nargs):
  855. nargs = tuple(ordered(set(nargs)))
  856. elif nargs is not None:
  857. nargs = (as_int(nargs),)
  858. nargs = FiniteSet(*nargs)
  859. cls.nargs = nargs
  860. def matches(self, expr, repl_dict=None, old=False):
  861. if not isinstance(expr, (AppliedUndef, Function)):
  862. return None
  863. if len(expr.args) not in self.nargs:
  864. return None
  865. if repl_dict is None:
  866. repl_dict = {}
  867. else:
  868. repl_dict = repl_dict.copy()
  869. repl_dict[self] = expr
  870. return repl_dict
  871. class Derivative(Expr):
  872. """
  873. Carries out differentiation of the given expression with respect to symbols.
  874. Examples
  875. ========
  876. >>> from sympy import Derivative, Function, symbols, Subs
  877. >>> from sympy.abc import x, y
  878. >>> f, g = symbols('f g', cls=Function)
  879. >>> Derivative(x**2, x, evaluate=True)
  880. 2*x
  881. Denesting of derivatives retains the ordering of variables:
  882. >>> Derivative(Derivative(f(x, y), y), x)
  883. Derivative(f(x, y), y, x)
  884. Contiguously identical symbols are merged into a tuple giving
  885. the symbol and the count:
  886. >>> Derivative(f(x), x, x, y, x)
  887. Derivative(f(x), (x, 2), y, x)
  888. If the derivative cannot be performed, and evaluate is True, the
  889. order of the variables of differentiation will be made canonical:
  890. >>> Derivative(f(x, y), y, x, evaluate=True)
  891. Derivative(f(x, y), x, y)
  892. Derivatives with respect to undefined functions can be calculated:
  893. >>> Derivative(f(x)**2, f(x), evaluate=True)
  894. 2*f(x)
  895. Such derivatives will show up when the chain rule is used to
  896. evaluate a derivative:
  897. >>> f(g(x)).diff(x)
  898. Derivative(f(g(x)), g(x))*Derivative(g(x), x)
  899. Substitution is used to represent derivatives of functions with
  900. arguments that are not symbols or functions:
  901. >>> f(2*x + 3).diff(x) == 2*Subs(f(y).diff(y), y, 2*x + 3)
  902. True
  903. Notes
  904. =====
  905. Simplification of high-order derivatives:
  906. Because there can be a significant amount of simplification that can be
  907. done when multiple differentiations are performed, results will be
  908. automatically simplified in a fairly conservative fashion unless the
  909. keyword ``simplify`` is set to False.
  910. >>> from sympy import sqrt, diff, Function, symbols
  911. >>> from sympy.abc import x, y, z
  912. >>> f, g = symbols('f,g', cls=Function)
  913. >>> e = sqrt((x + 1)**2 + x)
  914. >>> diff(e, (x, 5), simplify=False).count_ops()
  915. 136
  916. >>> diff(e, (x, 5)).count_ops()
  917. 30
  918. Ordering of variables:
  919. If evaluate is set to True and the expression cannot be evaluated, the
  920. list of differentiation symbols will be sorted, that is, the expression is
  921. assumed to have continuous derivatives up to the order asked.
  922. Derivative wrt non-Symbols:
  923. For the most part, one may not differentiate wrt non-symbols.
  924. For example, we do not allow differentiation wrt `x*y` because
  925. there are multiple ways of structurally defining where x*y appears
  926. in an expression: a very strict definition would make
  927. (x*y*z).diff(x*y) == 0. Derivatives wrt defined functions (like
  928. cos(x)) are not allowed, either:
  929. >>> (x*y*z).diff(x*y)
  930. Traceback (most recent call last):
  931. ...
  932. ValueError: Can't calculate derivative wrt x*y.
  933. To make it easier to work with variational calculus, however,
  934. derivatives wrt AppliedUndef and Derivatives are allowed.
  935. For example, in the Euler-Lagrange method one may write
  936. F(t, u, v) where u = f(t) and v = f'(t). These variables can be
  937. written explicitly as functions of time::
  938. >>> from sympy.abc import t
  939. >>> F = Function('F')
  940. >>> U = f(t)
  941. >>> V = U.diff(t)
  942. The derivative wrt f(t) can be obtained directly:
  943. >>> direct = F(t, U, V).diff(U)
  944. When differentiation wrt a non-Symbol is attempted, the non-Symbol
  945. is temporarily converted to a Symbol while the differentiation
  946. is performed and the same answer is obtained:
  947. >>> indirect = F(t, U, V).subs(U, x).diff(x).subs(x, U)
  948. >>> assert direct == indirect
  949. The implication of this non-symbol replacement is that all
  950. functions are treated as independent of other functions and the
  951. symbols are independent of the functions that contain them::
  952. >>> x.diff(f(x))
  953. 0
  954. >>> g(x).diff(f(x))
  955. 0
  956. It also means that derivatives are assumed to depend only
  957. on the variables of differentiation, not on anything contained
  958. within the expression being differentiated::
  959. >>> F = f(x)
  960. >>> Fx = F.diff(x)
  961. >>> Fx.diff(F) # derivative depends on x, not F
  962. 0
  963. >>> Fxx = Fx.diff(x)
  964. >>> Fxx.diff(Fx) # derivative depends on x, not Fx
  965. 0
  966. The last example can be made explicit by showing the replacement
  967. of Fx in Fxx with y:
  968. >>> Fxx.subs(Fx, y)
  969. Derivative(y, x)
  970. Since that in itself will evaluate to zero, differentiating
  971. wrt Fx will also be zero:
  972. >>> _.doit()
  973. 0
  974. Replacing undefined functions with concrete expressions
  975. One must be careful to replace undefined functions with expressions
  976. that contain variables consistent with the function definition and
  977. the variables of differentiation or else insconsistent result will
  978. be obtained. Consider the following example:
  979. >>> eq = f(x)*g(y)
  980. >>> eq.subs(f(x), x*y).diff(x, y).doit()
  981. y*Derivative(g(y), y) + g(y)
  982. >>> eq.diff(x, y).subs(f(x), x*y).doit()
  983. y*Derivative(g(y), y)
  984. The results differ because `f(x)` was replaced with an expression
  985. that involved both variables of differentiation. In the abstract
  986. case, differentiation of `f(x)` by `y` is 0; in the concrete case,
  987. the presence of `y` made that derivative nonvanishing and produced
  988. the extra `g(y)` term.
  989. Defining differentiation for an object
  990. An object must define ._eval_derivative(symbol) method that returns
  991. the differentiation result. This function only needs to consider the
  992. non-trivial case where expr contains symbol and it should call the diff()
  993. method internally (not _eval_derivative); Derivative should be the only
  994. one to call _eval_derivative.
  995. Any class can allow derivatives to be taken with respect to
  996. itself (while indicating its scalar nature). See the
  997. docstring of Expr._diff_wrt.
  998. See Also
  999. ========
  1000. _sort_variable_count
  1001. """
  1002. is_Derivative = True
  1003. @property
  1004. def _diff_wrt(self):
  1005. """An expression may be differentiated wrt a Derivative if
  1006. it is in elementary form.
  1007. Examples
  1008. ========
  1009. >>> from sympy import Function, Derivative, cos
  1010. >>> from sympy.abc import x
  1011. >>> f = Function('f')
  1012. >>> Derivative(f(x), x)._diff_wrt
  1013. True
  1014. >>> Derivative(cos(x), x)._diff_wrt
  1015. False
  1016. >>> Derivative(x + 1, x)._diff_wrt
  1017. False
  1018. A Derivative might be an unevaluated form of what will not be
  1019. a valid variable of differentiation if evaluated. For example,
  1020. >>> Derivative(f(f(x)), x).doit()
  1021. Derivative(f(x), x)*Derivative(f(f(x)), f(x))
  1022. Such an expression will present the same ambiguities as arise
  1023. when dealing with any other product, like ``2*x``, so ``_diff_wrt``
  1024. is False:
  1025. >>> Derivative(f(f(x)), x)._diff_wrt
  1026. False
  1027. """
  1028. return self.expr._diff_wrt and isinstance(self.doit(), Derivative)
  1029. def __new__(cls, expr, *variables, **kwargs):
  1030. expr = sympify(expr)
  1031. if not isinstance(expr, Basic):
  1032. raise TypeError(f"Cannot represent derivative of {type(expr)}")
  1033. symbols_or_none = getattr(expr, "free_symbols", None)
  1034. has_symbol_set = isinstance(symbols_or_none, set)
  1035. if not has_symbol_set:
  1036. raise ValueError(filldedent('''
  1037. Since there are no variables in the expression %s,
  1038. it cannot be differentiated.''' % expr))
  1039. # determine value for variables if it wasn't given
  1040. if not variables:
  1041. variables = expr.free_symbols
  1042. if len(variables) != 1:
  1043. if expr.is_number:
  1044. return S.Zero
  1045. if len(variables) == 0:
  1046. raise ValueError(filldedent('''
  1047. Since there are no variables in the expression,
  1048. the variable(s) of differentiation must be supplied
  1049. to differentiate %s''' % expr))
  1050. else:
  1051. raise ValueError(filldedent('''
  1052. Since there is more than one variable in the
  1053. expression, the variable(s) of differentiation
  1054. must be supplied to differentiate %s''' % expr))
  1055. # Split the list of variables into a list of the variables we are diff
  1056. # wrt, where each element of the list has the form (s, count) where
  1057. # s is the entity to diff wrt and count is the order of the
  1058. # derivative.
  1059. variable_count = []
  1060. array_likes = (tuple, list, Tuple)
  1061. from sympy.tensor.array import Array, NDimArray
  1062. for i, v in enumerate(variables):
  1063. if isinstance(v, UndefinedFunction):
  1064. raise TypeError(
  1065. "cannot differentiate wrt "
  1066. "UndefinedFunction: %s" % v)
  1067. if isinstance(v, array_likes):
  1068. if len(v) == 0:
  1069. # Ignore empty tuples: Derivative(expr, ... , (), ... )
  1070. continue
  1071. if isinstance(v[0], array_likes):
  1072. # Derive by array: Derivative(expr, ... , [[x, y, z]], ... )
  1073. if len(v) == 1:
  1074. v = Array(v[0])
  1075. count = 1
  1076. else:
  1077. v, count = v
  1078. v = Array(v)
  1079. else:
  1080. v, count = v
  1081. if count == 0:
  1082. continue
  1083. variable_count.append(Tuple(v, count))
  1084. continue
  1085. v = sympify(v)
  1086. if isinstance(v, Integer):
  1087. if i == 0:
  1088. raise ValueError("First variable cannot be a number: %i" % v)
  1089. count = v
  1090. prev, prevcount = variable_count[-1]
  1091. if prevcount != 1:
  1092. raise TypeError("tuple {} followed by number {}".format((prev, prevcount), v))
  1093. if count == 0:
  1094. variable_count.pop()
  1095. else:
  1096. variable_count[-1] = Tuple(prev, count)
  1097. else:
  1098. count = 1
  1099. variable_count.append(Tuple(v, count))
  1100. # light evaluation of contiguous, identical
  1101. # items: (x, 1), (x, 1) -> (x, 2)
  1102. merged = []
  1103. for t in variable_count:
  1104. v, c = t
  1105. if c.is_negative:
  1106. raise ValueError(
  1107. 'order of differentiation must be nonnegative')
  1108. if merged and merged[-1][0] == v:
  1109. c += merged[-1][1]
  1110. if not c:
  1111. merged.pop()
  1112. else:
  1113. merged[-1] = Tuple(v, c)
  1114. else:
  1115. merged.append(t)
  1116. variable_count = merged
  1117. # sanity check of variables of differentation; we waited
  1118. # until the counts were computed since some variables may
  1119. # have been removed because the count was 0
  1120. for v, c in variable_count:
  1121. # v must have _diff_wrt True
  1122. if not v._diff_wrt:
  1123. __ = '' # filler to make error message neater
  1124. raise ValueError(filldedent('''
  1125. Can't calculate derivative wrt %s.%s''' % (v,
  1126. __)))
  1127. # We make a special case for 0th derivative, because there is no
  1128. # good way to unambiguously print this.
  1129. if len(variable_count) == 0:
  1130. return expr
  1131. evaluate = kwargs.get('evaluate', False)
  1132. if evaluate:
  1133. if isinstance(expr, Derivative):
  1134. expr = expr.canonical
  1135. variable_count = [
  1136. (v.canonical if isinstance(v, Derivative) else v, c)
  1137. for v, c in variable_count]
  1138. # Look for a quick exit if there are symbols that don't appear in
  1139. # expression at all. Note, this cannot check non-symbols like
  1140. # Derivatives as those can be created by intermediate
  1141. # derivatives.
  1142. zero = False
  1143. free = expr.free_symbols
  1144. from sympy.matrices.expressions.matexpr import MatrixExpr
  1145. for v, c in variable_count:
  1146. vfree = v.free_symbols
  1147. if c.is_positive and vfree:
  1148. if isinstance(v, AppliedUndef):
  1149. # these match exactly since
  1150. # x.diff(f(x)) == g(x).diff(f(x)) == 0
  1151. # and are not created by differentiation
  1152. D = Dummy()
  1153. if not expr.xreplace({v: D}).has(D):
  1154. zero = True
  1155. break
  1156. elif isinstance(v, MatrixExpr):
  1157. zero = False
  1158. break
  1159. elif isinstance(v, Symbol) and v not in free:
  1160. zero = True
  1161. break
  1162. else:
  1163. if not free & vfree:
  1164. # e.g. v is IndexedBase or Matrix
  1165. zero = True
  1166. break
  1167. if zero:
  1168. return cls._get_zero_with_shape_like(expr)
  1169. # make the order of symbols canonical
  1170. #TODO: check if assumption of discontinuous derivatives exist
  1171. variable_count = cls._sort_variable_count(variable_count)
  1172. # denest
  1173. if isinstance(expr, Derivative):
  1174. variable_count = list(expr.variable_count) + variable_count
  1175. expr = expr.expr
  1176. return _derivative_dispatch(expr, *variable_count, **kwargs)
  1177. # we return here if evaluate is False or if there is no
  1178. # _eval_derivative method
  1179. if not evaluate or not hasattr(expr, '_eval_derivative'):
  1180. # return an unevaluated Derivative
  1181. if evaluate and variable_count == [(expr, 1)] and expr.is_scalar:
  1182. # special hack providing evaluation for classes
  1183. # that have defined is_scalar=True but have no
  1184. # _eval_derivative defined
  1185. return S.One
  1186. return Expr.__new__(cls, expr, *variable_count)
  1187. # evaluate the derivative by calling _eval_derivative method
  1188. # of expr for each variable
  1189. # -------------------------------------------------------------
  1190. nderivs = 0 # how many derivatives were performed
  1191. unhandled = []
  1192. from sympy.matrices.matrixbase import MatrixBase
  1193. for i, (v, count) in enumerate(variable_count):
  1194. old_expr = expr
  1195. old_v = None
  1196. is_symbol = v.is_symbol or isinstance(v,
  1197. (Iterable, Tuple, MatrixBase, NDimArray))
  1198. if not is_symbol:
  1199. old_v = v
  1200. v = Dummy('xi')
  1201. expr = expr.xreplace({old_v: v})
  1202. # Derivatives and UndefinedFunctions are independent
  1203. # of all others
  1204. clashing = not (isinstance(old_v, (Derivative, AppliedUndef)))
  1205. if v not in expr.free_symbols and not clashing:
  1206. return expr.diff(v) # expr's version of 0
  1207. if not old_v.is_scalar and not hasattr(
  1208. old_v, '_eval_derivative'):
  1209. # special hack providing evaluation for classes
  1210. # that have defined is_scalar=True but have no
  1211. # _eval_derivative defined
  1212. expr *= old_v.diff(old_v)
  1213. obj = cls._dispatch_eval_derivative_n_times(expr, v, count)
  1214. if obj is not None and obj.is_zero:
  1215. return obj
  1216. nderivs += count
  1217. if old_v is not None:
  1218. if obj is not None:
  1219. # remove the dummy that was used
  1220. obj = obj.subs(v, old_v)
  1221. # restore expr
  1222. expr = old_expr
  1223. if obj is None:
  1224. # we've already checked for quick-exit conditions
  1225. # that give 0 so the remaining variables
  1226. # are contained in the expression but the expression
  1227. # did not compute a derivative so we stop taking
  1228. # derivatives
  1229. unhandled = variable_count[i:]
  1230. break
  1231. expr = obj
  1232. # what we have so far can be made canonical
  1233. expr = expr.replace(
  1234. lambda x: isinstance(x, Derivative),
  1235. lambda x: x.canonical)
  1236. if unhandled:
  1237. if isinstance(expr, Derivative):
  1238. unhandled = list(expr.variable_count) + unhandled
  1239. expr = expr.expr
  1240. expr = Expr.__new__(cls, expr, *unhandled)
  1241. if (nderivs > 1) == True and kwargs.get('simplify', True):
  1242. from .exprtools import factor_terms
  1243. from sympy.simplify.simplify import signsimp
  1244. expr = factor_terms(signsimp(expr))
  1245. return expr
  1246. @property
  1247. def canonical(cls):
  1248. return cls.func(cls.expr,
  1249. *Derivative._sort_variable_count(cls.variable_count))
  1250. @classmethod
  1251. def _sort_variable_count(cls, vc):
  1252. """
  1253. Sort (variable, count) pairs into canonical order while
  1254. retaining order of variables that do not commute during
  1255. differentiation:
  1256. * symbols and functions commute with each other
  1257. * derivatives commute with each other
  1258. * a derivative does not commute with anything it contains
  1259. * any other object is not allowed to commute if it has
  1260. free symbols in common with another object
  1261. Examples
  1262. ========
  1263. >>> from sympy import Derivative, Function, symbols
  1264. >>> vsort = Derivative._sort_variable_count
  1265. >>> x, y, z = symbols('x y z')
  1266. >>> f, g, h = symbols('f g h', cls=Function)
  1267. Contiguous items are collapsed into one pair:
  1268. >>> vsort([(x, 1), (x, 1)])
  1269. [(x, 2)]
  1270. >>> vsort([(y, 1), (f(x), 1), (y, 1), (f(x), 1)])
  1271. [(y, 2), (f(x), 2)]
  1272. Ordering is canonical.
  1273. >>> def vsort0(*v):
  1274. ... # docstring helper to
  1275. ... # change vi -> (vi, 0), sort, and return vi vals
  1276. ... return [i[0] for i in vsort([(i, 0) for i in v])]
  1277. >>> vsort0(y, x)
  1278. [x, y]
  1279. >>> vsort0(g(y), g(x), f(y))
  1280. [f(y), g(x), g(y)]
  1281. Symbols are sorted as far to the left as possible but never
  1282. move to the left of a derivative having the same symbol in
  1283. its variables; the same applies to AppliedUndef which are
  1284. always sorted after Symbols:
  1285. >>> dfx = f(x).diff(x)
  1286. >>> assert vsort0(dfx, y) == [y, dfx]
  1287. >>> assert vsort0(dfx, x) == [dfx, x]
  1288. """
  1289. if not vc:
  1290. return []
  1291. vc = list(vc)
  1292. if len(vc) == 1:
  1293. return [Tuple(*vc[0])]
  1294. V = list(range(len(vc)))
  1295. E = []
  1296. v = lambda i: vc[i][0]
  1297. D = Dummy()
  1298. def _block(d, v, wrt=False):
  1299. # return True if v should not come before d else False
  1300. if d == v:
  1301. return wrt
  1302. if d.is_Symbol:
  1303. return False
  1304. if isinstance(d, Derivative):
  1305. # a derivative blocks if any of it's variables contain
  1306. # v; the wrt flag will return True for an exact match
  1307. # and will cause an AppliedUndef to block if v is in
  1308. # the arguments
  1309. if any(_block(k, v, wrt=True)
  1310. for k in d._wrt_variables):
  1311. return True
  1312. return False
  1313. if not wrt and isinstance(d, AppliedUndef):
  1314. return False
  1315. if v.is_Symbol:
  1316. return v in d.free_symbols
  1317. if isinstance(v, AppliedUndef):
  1318. return _block(d.xreplace({v: D}), D)
  1319. return d.free_symbols & v.free_symbols
  1320. for i in range(len(vc)):
  1321. for j in range(i):
  1322. if _block(v(j), v(i)):
  1323. E.append((j,i))
  1324. # this is the default ordering to use in case of ties
  1325. O = dict(zip(ordered(uniq([i for i, c in vc])), range(len(vc))))
  1326. ix = topological_sort((V, E), key=lambda i: O[v(i)])
  1327. # merge counts of contiguously identical items
  1328. merged = []
  1329. for v, c in [vc[i] for i in ix]:
  1330. if merged and merged[-1][0] == v:
  1331. merged[-1][1] += c
  1332. else:
  1333. merged.append([v, c])
  1334. return [Tuple(*i) for i in merged]
  1335. def _eval_is_commutative(self):
  1336. return self.expr.is_commutative
  1337. def _eval_derivative(self, v):
  1338. # If v (the variable of differentiation) is not in
  1339. # self.variables, we might be able to take the derivative.
  1340. if v not in self._wrt_variables:
  1341. dedv = self.expr.diff(v)
  1342. if isinstance(dedv, Derivative):
  1343. return dedv.func(dedv.expr, *(self.variable_count + dedv.variable_count))
  1344. # dedv (d(self.expr)/dv) could have simplified things such that the
  1345. # derivative wrt things in self.variables can now be done. Thus,
  1346. # we set evaluate=True to see if there are any other derivatives
  1347. # that can be done. The most common case is when dedv is a simple
  1348. # number so that the derivative wrt anything else will vanish.
  1349. return self.func(dedv, *self.variables, evaluate=True)
  1350. # In this case v was in self.variables so the derivative wrt v has
  1351. # already been attempted and was not computed, either because it
  1352. # couldn't be or evaluate=False originally.
  1353. variable_count = list(self.variable_count)
  1354. variable_count.append((v, 1))
  1355. return self.func(self.expr, *variable_count, evaluate=False)
  1356. def doit(self, **hints):
  1357. expr = self.expr
  1358. if hints.get('deep', True):
  1359. expr = expr.doit(**hints)
  1360. hints['evaluate'] = True
  1361. rv = self.func(expr, *self.variable_count, **hints)
  1362. if rv!= self and rv.has(Derivative):
  1363. rv = rv.doit(**hints)
  1364. return rv
  1365. @_sympifyit('z0', NotImplementedError)
  1366. def doit_numerically(self, z0):
  1367. """
  1368. Evaluate the derivative at z numerically.
  1369. When we can represent derivatives at a point, this should be folded
  1370. into the normal evalf. For now, we need a special method.
  1371. """
  1372. if len(self.free_symbols) != 1 or len(self.variables) != 1:
  1373. raise NotImplementedError('partials and higher order derivatives')
  1374. z = list(self.free_symbols)[0]
  1375. def eval(x):
  1376. f0 = self.expr.subs(z, Expr._from_mpmath(x, prec=mpmath.mp.prec))
  1377. f0 = f0.evalf(prec_to_dps(mpmath.mp.prec))
  1378. return f0._to_mpmath(mpmath.mp.prec)
  1379. return Expr._from_mpmath(mpmath.diff(eval,
  1380. z0._to_mpmath(mpmath.mp.prec)),
  1381. mpmath.mp.prec)
  1382. @property
  1383. def expr(self):
  1384. return self._args[0]
  1385. @property
  1386. def _wrt_variables(self):
  1387. # return the variables of differentiation without
  1388. # respect to the type of count (int or symbolic)
  1389. return [i[0] for i in self.variable_count]
  1390. @property
  1391. def variables(self):
  1392. # TODO: deprecate? YES, make this 'enumerated_variables' and
  1393. # name _wrt_variables as variables
  1394. # TODO: support for `d^n`?
  1395. rv = []
  1396. for v, count in self.variable_count:
  1397. if not count.is_Integer:
  1398. raise TypeError(filldedent('''
  1399. Cannot give expansion for symbolic count. If you just
  1400. want a list of all variables of differentiation, use
  1401. _wrt_variables.'''))
  1402. rv.extend([v]*count)
  1403. return tuple(rv)
  1404. @property
  1405. def variable_count(self):
  1406. return self._args[1:]
  1407. @property
  1408. def derivative_count(self):
  1409. return sum([count for _, count in self.variable_count], 0)
  1410. @property
  1411. def free_symbols(self):
  1412. ret = self.expr.free_symbols
  1413. # Add symbolic counts to free_symbols
  1414. for _, count in self.variable_count:
  1415. ret.update(count.free_symbols)
  1416. return ret
  1417. @property
  1418. def kind(self):
  1419. return self.args[0].kind
  1420. def _eval_subs(self, old, new):
  1421. # The substitution (old, new) cannot be done inside
  1422. # Derivative(expr, vars) for a variety of reasons
  1423. # as handled below.
  1424. if old in self._wrt_variables:
  1425. # first handle the counts
  1426. expr = self.func(self.expr, *[(v, c.subs(old, new))
  1427. for v, c in self.variable_count])
  1428. if expr != self:
  1429. return expr._eval_subs(old, new)
  1430. # quick exit case
  1431. if not getattr(new, '_diff_wrt', False):
  1432. # case (0): new is not a valid variable of
  1433. # differentiation
  1434. if isinstance(old, Symbol):
  1435. # don't introduce a new symbol if the old will do
  1436. return Subs(self, old, new)
  1437. else:
  1438. xi = Dummy('xi')
  1439. return Subs(self.xreplace({old: xi}), xi, new)
  1440. # If both are Derivatives with the same expr, check if old is
  1441. # equivalent to self or if old is a subderivative of self.
  1442. if old.is_Derivative and old.expr == self.expr:
  1443. if self.canonical == old.canonical:
  1444. return new
  1445. # collections.Counter doesn't have __le__
  1446. def _subset(a, b):
  1447. return all((a[i] <= b[i]) == True for i in a)
  1448. old_vars = Counter(dict(reversed(old.variable_count)))
  1449. self_vars = Counter(dict(reversed(self.variable_count)))
  1450. if _subset(old_vars, self_vars):
  1451. return _derivative_dispatch(new, *(self_vars - old_vars).items()).canonical
  1452. args = list(self.args)
  1453. newargs = [x._subs(old, new) for x in args]
  1454. if args[0] == old:
  1455. # complete replacement of self.expr
  1456. # we already checked that the new is valid so we know
  1457. # it won't be a problem should it appear in variables
  1458. return _derivative_dispatch(*newargs)
  1459. if newargs[0] != args[0]:
  1460. # case (1) can't change expr by introducing something that is in
  1461. # the _wrt_variables if it was already in the expr
  1462. # e.g.
  1463. # for Derivative(f(x, g(y)), y), x cannot be replaced with
  1464. # anything that has y in it; for f(g(x), g(y)).diff(g(y))
  1465. # g(x) cannot be replaced with anything that has g(y)
  1466. syms = {vi: Dummy() for vi in self._wrt_variables
  1467. if not vi.is_Symbol}
  1468. wrt = {syms.get(vi, vi) for vi in self._wrt_variables}
  1469. forbidden = args[0].xreplace(syms).free_symbols & wrt
  1470. nfree = new.xreplace(syms).free_symbols
  1471. ofree = old.xreplace(syms).free_symbols
  1472. if (nfree - ofree) & forbidden:
  1473. return Subs(self, old, new)
  1474. viter = ((i, j) for ((i, _), (j, _)) in zip(newargs[1:], args[1:]))
  1475. if any(i != j for i, j in viter): # a wrt-variable change
  1476. # case (2) can't change vars by introducing a variable
  1477. # that is contained in expr, e.g.
  1478. # for Derivative(f(z, g(h(x), y)), y), y cannot be changed to
  1479. # x, h(x), or g(h(x), y)
  1480. for a in _atomic(self.expr, recursive=True):
  1481. for i in range(1, len(newargs)):
  1482. vi, _ = newargs[i]
  1483. if a == vi and vi != args[i][0]:
  1484. return Subs(self, old, new)
  1485. # more arg-wise checks
  1486. vc = newargs[1:]
  1487. oldv = self._wrt_variables
  1488. newe = self.expr
  1489. subs = []
  1490. for i, (vi, ci) in enumerate(vc):
  1491. if not vi._diff_wrt:
  1492. # case (3) invalid differentiation expression so
  1493. # create a replacement dummy
  1494. xi = Dummy('xi_%i' % i)
  1495. # replace the old valid variable with the dummy
  1496. # in the expression
  1497. newe = newe.xreplace({oldv[i]: xi})
  1498. # and replace the bad variable with the dummy
  1499. vc[i] = (xi, ci)
  1500. # and record the dummy with the new (invalid)
  1501. # differentiation expression
  1502. subs.append((xi, vi))
  1503. if subs:
  1504. # handle any residual substitution in the expression
  1505. newe = newe._subs(old, new)
  1506. # return the Subs-wrapped derivative
  1507. return Subs(Derivative(newe, *vc), *zip(*subs))
  1508. # everything was ok
  1509. return _derivative_dispatch(*newargs)
  1510. def _eval_lseries(self, x, logx, cdir=0):
  1511. dx = self.variables
  1512. for term in self.expr.lseries(x, logx=logx, cdir=cdir):
  1513. yield self.func(term, *dx)
  1514. def _eval_nseries(self, x, n, logx, cdir=0):
  1515. arg = self.expr.nseries(x, n=n, logx=logx)
  1516. o = arg.getO()
  1517. dx = self.variables
  1518. rv = [self.func(a, *dx) for a in Add.make_args(arg.removeO())]
  1519. if o:
  1520. rv.append(o/x)
  1521. return Add(*rv)
  1522. def _eval_as_leading_term(self, x, logx, cdir):
  1523. series_gen = self.expr.lseries(x)
  1524. d = S.Zero
  1525. for leading_term in series_gen:
  1526. d = diff(leading_term, *self.variables)
  1527. if d != 0:
  1528. break
  1529. return d
  1530. def as_finite_difference(self, points=1, x0=None, wrt=None):
  1531. """ Expresses a Derivative instance as a finite difference.
  1532. Parameters
  1533. ==========
  1534. points : sequence or coefficient, optional
  1535. If sequence: discrete values (length >= order+1) of the
  1536. independent variable used for generating the finite
  1537. difference weights.
  1538. If it is a coefficient, it will be used as the step-size
  1539. for generating an equidistant sequence of length order+1
  1540. centered around ``x0``. Default: 1 (step-size 1)
  1541. x0 : number or Symbol, optional
  1542. the value of the independent variable (``wrt``) at which the
  1543. derivative is to be approximated. Default: same as ``wrt``.
  1544. wrt : Symbol, optional
  1545. "with respect to" the variable for which the (partial)
  1546. derivative is to be approximated for. If not provided it
  1547. is required that the derivative is ordinary. Default: ``None``.
  1548. Examples
  1549. ========
  1550. >>> from sympy import symbols, Function, exp, sqrt, Symbol
  1551. >>> x, h = symbols('x h')
  1552. >>> f = Function('f')
  1553. >>> f(x).diff(x).as_finite_difference()
  1554. -f(x - 1/2) + f(x + 1/2)
  1555. The default step size and number of points are 1 and
  1556. ``order + 1`` respectively. We can change the step size by
  1557. passing a symbol as a parameter:
  1558. >>> f(x).diff(x).as_finite_difference(h)
  1559. -f(-h/2 + x)/h + f(h/2 + x)/h
  1560. We can also specify the discretized values to be used in a
  1561. sequence:
  1562. >>> f(x).diff(x).as_finite_difference([x, x+h, x+2*h])
  1563. -3*f(x)/(2*h) + 2*f(h + x)/h - f(2*h + x)/(2*h)
  1564. The algorithm is not restricted to use equidistant spacing, nor
  1565. do we need to make the approximation around ``x0``, but we can get
  1566. an expression estimating the derivative at an offset:
  1567. >>> e, sq2 = exp(1), sqrt(2)
  1568. >>> xl = [x-h, x+h, x+e*h]
  1569. >>> f(x).diff(x, 1).as_finite_difference(xl, x+h*sq2) # doctest: +ELLIPSIS
  1570. 2*h*((h + sqrt(2)*h)/(2*h) - (-sqrt(2)*h + h)/(2*h))*f(E*h + x)/...
  1571. To approximate ``Derivative`` around ``x0`` using a non-equidistant
  1572. spacing step, the algorithm supports assignment of undefined
  1573. functions to ``points``:
  1574. >>> dx = Function('dx')
  1575. >>> f(x).diff(x).as_finite_difference(points=dx(x), x0=x-h)
  1576. -f(-h + x - dx(-h + x)/2)/dx(-h + x) + f(-h + x + dx(-h + x)/2)/dx(-h + x)
  1577. Partial derivatives are also supported:
  1578. >>> y = Symbol('y')
  1579. >>> d2fdxdy=f(x,y).diff(x,y)
  1580. >>> d2fdxdy.as_finite_difference(wrt=x)
  1581. -Derivative(f(x - 1/2, y), y) + Derivative(f(x + 1/2, y), y)
  1582. We can apply ``as_finite_difference`` to ``Derivative`` instances in
  1583. compound expressions using ``replace``:
  1584. >>> (1 + 42**f(x).diff(x)).replace(lambda arg: arg.is_Derivative,
  1585. ... lambda arg: arg.as_finite_difference())
  1586. 42**(-f(x - 1/2) + f(x + 1/2)) + 1
  1587. See also
  1588. ========
  1589. sympy.calculus.finite_diff.apply_finite_diff
  1590. sympy.calculus.finite_diff.differentiate_finite
  1591. sympy.calculus.finite_diff.finite_diff_weights
  1592. """
  1593. from sympy.calculus.finite_diff import _as_finite_diff
  1594. return _as_finite_diff(self, points, x0, wrt)
  1595. @classmethod
  1596. def _get_zero_with_shape_like(cls, expr):
  1597. return S.Zero
  1598. @classmethod
  1599. def _dispatch_eval_derivative_n_times(cls, expr, v, count):
  1600. # Evaluate the derivative `n` times. If
  1601. # `_eval_derivative_n_times` is not overridden by the current
  1602. # object, the default in `Basic` will call a loop over
  1603. # `_eval_derivative`:
  1604. return expr._eval_derivative_n_times(v, count)
  1605. def _derivative_dispatch(expr, *variables, **kwargs):
  1606. from sympy.matrices.matrixbase import MatrixBase
  1607. from sympy.matrices.expressions.matexpr import MatrixExpr
  1608. from sympy.tensor.array import NDimArray
  1609. array_types = (MatrixBase, MatrixExpr, NDimArray, list, tuple, Tuple)
  1610. if isinstance(expr, array_types) or any(isinstance(i[0], array_types) if isinstance(i, (tuple, list, Tuple)) else isinstance(i, array_types) for i in variables):
  1611. from sympy.tensor.array.array_derivatives import ArrayDerivative
  1612. return ArrayDerivative(expr, *variables, **kwargs)
  1613. return Derivative(expr, *variables, **kwargs)
  1614. class Lambda(Expr):
  1615. """
  1616. Lambda(x, expr) represents a lambda function similar to Python's
  1617. 'lambda x: expr'. A function of several variables is written as
  1618. Lambda((x, y, ...), expr).
  1619. Examples
  1620. ========
  1621. A simple example:
  1622. >>> from sympy import Lambda
  1623. >>> from sympy.abc import x
  1624. >>> f = Lambda(x, x**2)
  1625. >>> f(4)
  1626. 16
  1627. For multivariate functions, use:
  1628. >>> from sympy.abc import y, z, t
  1629. >>> f2 = Lambda((x, y, z, t), x + y**z + t**z)
  1630. >>> f2(1, 2, 3, 4)
  1631. 73
  1632. It is also possible to unpack tuple arguments:
  1633. >>> f = Lambda(((x, y), z), x + y + z)
  1634. >>> f((1, 2), 3)
  1635. 6
  1636. A handy shortcut for lots of arguments:
  1637. >>> p = x, y, z
  1638. >>> f = Lambda(p, x + y*z)
  1639. >>> f(*p)
  1640. x + y*z
  1641. """
  1642. is_Function = True
  1643. def __new__(cls, signature, expr) -> Lambda:
  1644. if iterable(signature) and not isinstance(signature, (tuple, Tuple)):
  1645. sympy_deprecation_warning(
  1646. """
  1647. Using a non-tuple iterable as the first argument to Lambda
  1648. is deprecated. Use Lambda(tuple(args), expr) instead.
  1649. """,
  1650. deprecated_since_version="1.5",
  1651. active_deprecations_target="deprecated-non-tuple-lambda",
  1652. )
  1653. signature = tuple(signature)
  1654. _sig = signature if iterable(signature) else (signature,)
  1655. sig: Tuple = sympify(_sig) # type: ignore
  1656. cls._check_signature(sig)
  1657. if len(sig) == 1 and sig[0] == expr:
  1658. return S.IdentityFunction
  1659. return Expr.__new__(cls, sig, sympify(expr))
  1660. @classmethod
  1661. def _check_signature(cls, sig):
  1662. syms = set()
  1663. def rcheck(args):
  1664. for a in args:
  1665. if a.is_symbol:
  1666. if a in syms:
  1667. raise BadSignatureError("Duplicate symbol %s" % a)
  1668. syms.add(a)
  1669. elif isinstance(a, Tuple):
  1670. rcheck(a)
  1671. else:
  1672. raise BadSignatureError("Lambda signature should be only tuples"
  1673. " and symbols, not %s" % a)
  1674. if not isinstance(sig, Tuple):
  1675. raise BadSignatureError("Lambda signature should be a tuple not %s" % sig)
  1676. # Recurse through the signature:
  1677. rcheck(sig)
  1678. @property
  1679. def signature(self):
  1680. """The expected form of the arguments to be unpacked into variables"""
  1681. return self._args[0]
  1682. @property
  1683. def expr(self):
  1684. """The return value of the function"""
  1685. return self._args[1]
  1686. @property
  1687. def variables(self):
  1688. """The variables used in the internal representation of the function"""
  1689. def _variables(args):
  1690. if isinstance(args, Tuple):
  1691. for arg in args:
  1692. yield from _variables(arg)
  1693. else:
  1694. yield args
  1695. return tuple(_variables(self.signature))
  1696. @property
  1697. def nargs(self):
  1698. from sympy.sets.sets import FiniteSet
  1699. return FiniteSet(len(self.signature))
  1700. bound_symbols = variables
  1701. @property
  1702. def free_symbols(self):
  1703. return self.expr.free_symbols - set(self.variables)
  1704. def __call__(self, *args):
  1705. n = len(args)
  1706. if n not in self.nargs: # Lambda only ever has 1 value in nargs
  1707. # XXX: exception message must be in exactly this format to
  1708. # make it work with NumPy's functions like vectorize(). See,
  1709. # for example, https://github.com/numpy/numpy/issues/1697.
  1710. # The ideal solution would be just to attach metadata to
  1711. # the exception and change NumPy to take advantage of this.
  1712. ## XXX does this apply to Lambda? If not, remove this comment.
  1713. temp = ('%(name)s takes exactly %(args)s '
  1714. 'argument%(plural)s (%(given)s given)')
  1715. raise BadArgumentsError(temp % {
  1716. 'name': self,
  1717. 'args': list(self.nargs)[0],
  1718. 'plural': 's'*(list(self.nargs)[0] != 1),
  1719. 'given': n})
  1720. d = self._match_signature(self.signature, args)
  1721. return self.expr.xreplace(d)
  1722. def _match_signature(self, sig, args):
  1723. symargmap = {}
  1724. def rmatch(pars, args):
  1725. for par, arg in zip(pars, args):
  1726. if par.is_symbol:
  1727. symargmap[par] = arg
  1728. elif isinstance(par, Tuple):
  1729. if not isinstance(arg, (tuple, Tuple)) or len(args) != len(pars):
  1730. raise BadArgumentsError("Can't match %s and %s" % (args, pars))
  1731. rmatch(par, arg)
  1732. rmatch(sig, args)
  1733. return symargmap
  1734. @property
  1735. def is_identity(self):
  1736. """Return ``True`` if this ``Lambda`` is an identity function. """
  1737. return self.signature == self.expr
  1738. def _eval_evalf(self, prec):
  1739. return self.func(self.args[0], self.args[1].evalf(n=prec_to_dps(prec)))
  1740. class Subs(Expr):
  1741. """
  1742. Represents unevaluated substitutions of an expression.
  1743. ``Subs(expr, x, x0)`` represents the expression resulting
  1744. from substituting x with x0 in expr.
  1745. Parameters
  1746. ==========
  1747. expr : Expr
  1748. An expression.
  1749. x : tuple, variable
  1750. A variable or list of distinct variables.
  1751. x0 : tuple or list of tuples
  1752. A point or list of evaluation points
  1753. corresponding to those variables.
  1754. Examples
  1755. ========
  1756. >>> from sympy import Subs, Function, sin, cos
  1757. >>> from sympy.abc import x, y, z
  1758. >>> f = Function('f')
  1759. Subs are created when a particular substitution cannot be made. The
  1760. x in the derivative cannot be replaced with 0 because 0 is not a
  1761. valid variables of differentiation:
  1762. >>> f(x).diff(x).subs(x, 0)
  1763. Subs(Derivative(f(x), x), x, 0)
  1764. Once f is known, the derivative and evaluation at 0 can be done:
  1765. >>> _.subs(f, sin).doit() == sin(x).diff(x).subs(x, 0) == cos(0)
  1766. True
  1767. Subs can also be created directly with one or more variables:
  1768. >>> Subs(f(x)*sin(y) + z, (x, y), (0, 1))
  1769. Subs(z + f(x)*sin(y), (x, y), (0, 1))
  1770. >>> _.doit()
  1771. z + f(0)*sin(1)
  1772. Notes
  1773. =====
  1774. ``Subs`` objects are generally useful to represent unevaluated derivatives
  1775. calculated at a point.
  1776. The variables may be expressions, but they are subjected to the limitations
  1777. of subs(), so it is usually a good practice to use only symbols for
  1778. variables, since in that case there can be no ambiguity.
  1779. There's no automatic expansion - use the method .doit() to effect all
  1780. possible substitutions of the object and also of objects inside the
  1781. expression.
  1782. When evaluating derivatives at a point that is not a symbol, a Subs object
  1783. is returned. One is also able to calculate derivatives of Subs objects - in
  1784. this case the expression is always expanded (for the unevaluated form, use
  1785. Derivative()).
  1786. In order to allow expressions to combine before doit is done, a
  1787. representation of the Subs expression is used internally to make
  1788. expressions that are superficially different compare the same:
  1789. >>> a, b = Subs(x, x, 0), Subs(y, y, 0)
  1790. >>> a + b
  1791. 2*Subs(x, x, 0)
  1792. This can lead to unexpected consequences when using methods
  1793. like `has` that are cached:
  1794. >>> s = Subs(x, x, 0)
  1795. >>> s.has(x), s.has(y)
  1796. (True, False)
  1797. >>> ss = s.subs(x, y)
  1798. >>> ss.has(x), ss.has(y)
  1799. (True, False)
  1800. >>> s, ss
  1801. (Subs(x, x, 0), Subs(y, y, 0))
  1802. """
  1803. def __new__(cls, expr, variables, point, **assumptions):
  1804. if not is_sequence(variables, Tuple):
  1805. variables = [variables]
  1806. variables = Tuple(*variables)
  1807. if has_dups(variables):
  1808. repeated = [str(v) for v, i in Counter(variables).items() if i > 1]
  1809. __ = ', '.join(repeated)
  1810. raise ValueError(filldedent('''
  1811. The following expressions appear more than once: %s
  1812. ''' % __))
  1813. point = Tuple(*(point if is_sequence(point, Tuple) else [point]))
  1814. if len(point) != len(variables):
  1815. raise ValueError('Number of point values must be the same as '
  1816. 'the number of variables.')
  1817. if not point:
  1818. return sympify(expr)
  1819. # denest
  1820. if isinstance(expr, Subs):
  1821. variables = expr.variables + variables
  1822. point = expr.point + point
  1823. expr = expr.expr
  1824. else:
  1825. expr = sympify(expr)
  1826. # use symbols with names equal to the point value (with prepended _)
  1827. # to give a variable-independent expression
  1828. pre = "_"
  1829. pts = sorted(set(point), key=default_sort_key)
  1830. from sympy.printing.str import StrPrinter
  1831. class CustomStrPrinter(StrPrinter):
  1832. def _print_Dummy(self, expr):
  1833. return str(expr) + str(expr.dummy_index)
  1834. def mystr(expr, **settings):
  1835. p = CustomStrPrinter(settings)
  1836. return p.doprint(expr)
  1837. while 1:
  1838. s_pts = {p: Symbol(pre + mystr(p)) for p in pts}
  1839. reps = [(v, s_pts[p])
  1840. for v, p in zip(variables, point)]
  1841. # if any underscore-prepended symbol is already a free symbol
  1842. # and is a variable with a different point value, then there
  1843. # is a clash, e.g. _0 clashes in Subs(_0 + _1, (_0, _1), (1, 0))
  1844. # because the new symbol that would be created is _1 but _1
  1845. # is already mapped to 0 so __0 and __1 are used for the new
  1846. # symbols
  1847. if any(r in expr.free_symbols and
  1848. r in variables and
  1849. Symbol(pre + mystr(point[variables.index(r)])) != r
  1850. for _, r in reps):
  1851. pre += "_"
  1852. continue
  1853. break
  1854. obj = Expr.__new__(cls, expr, Tuple(*variables), point)
  1855. obj._expr = expr.xreplace(dict(reps))
  1856. return obj
  1857. def _eval_is_commutative(self):
  1858. return self.expr.is_commutative
  1859. def doit(self, **hints):
  1860. e, v, p = self.args
  1861. # remove self mappings
  1862. for i, (vi, pi) in enumerate(zip(v, p)):
  1863. if vi == pi:
  1864. v = v[:i] + v[i + 1:]
  1865. p = p[:i] + p[i + 1:]
  1866. if not v:
  1867. return self.expr
  1868. if isinstance(e, Derivative):
  1869. # apply functions first, e.g. f -> cos
  1870. undone = []
  1871. for i, vi in enumerate(v):
  1872. if isinstance(vi, FunctionClass):
  1873. e = e.subs(vi, p[i])
  1874. else:
  1875. undone.append((vi, p[i]))
  1876. if not isinstance(e, Derivative):
  1877. e = e.doit()
  1878. if isinstance(e, Derivative):
  1879. # do Subs that aren't related to differentiation
  1880. undone2 = []
  1881. D = Dummy()
  1882. arg = e.args[0]
  1883. for vi, pi in undone:
  1884. if D not in e.xreplace({vi: D}).free_symbols:
  1885. if arg.has(vi):
  1886. e = e.subs(vi, pi)
  1887. else:
  1888. undone2.append((vi, pi))
  1889. undone = undone2
  1890. # differentiate wrt variables that are present
  1891. wrt = []
  1892. D = Dummy()
  1893. expr = e.expr
  1894. free = expr.free_symbols
  1895. for vi, ci in e.variable_count:
  1896. if isinstance(vi, Symbol) and vi in free:
  1897. expr = expr.diff((vi, ci))
  1898. elif D in expr.subs(vi, D).free_symbols:
  1899. expr = expr.diff((vi, ci))
  1900. else:
  1901. wrt.append((vi, ci))
  1902. # inject remaining subs
  1903. rv = expr.subs(undone)
  1904. # do remaining differentiation *in order given*
  1905. for vc in wrt:
  1906. rv = rv.diff(vc)
  1907. else:
  1908. # inject remaining subs
  1909. rv = e.subs(undone)
  1910. else:
  1911. rv = e.doit(**hints).subs(list(zip(v, p)))
  1912. if hints.get('deep', True) and rv != self:
  1913. rv = rv.doit(**hints)
  1914. return rv
  1915. def evalf(self, prec=None, **options):
  1916. return self.doit().evalf(prec, **options)
  1917. n = evalf # type:ignore
  1918. @property
  1919. def variables(self):
  1920. """The variables to be evaluated"""
  1921. return self._args[1]
  1922. bound_symbols = variables
  1923. @property
  1924. def expr(self):
  1925. """The expression on which the substitution operates"""
  1926. return self._args[0]
  1927. @property
  1928. def point(self):
  1929. """The values for which the variables are to be substituted"""
  1930. return self._args[2]
  1931. @property
  1932. def free_symbols(self):
  1933. return (self.expr.free_symbols - set(self.variables) |
  1934. set(self.point.free_symbols))
  1935. @property
  1936. def expr_free_symbols(self):
  1937. sympy_deprecation_warning("""
  1938. The expr_free_symbols property is deprecated. Use free_symbols to get
  1939. the free symbols of an expression.
  1940. """,
  1941. deprecated_since_version="1.9",
  1942. active_deprecations_target="deprecated-expr-free-symbols")
  1943. # Don't show the warning twice from the recursive call
  1944. with ignore_warnings(SymPyDeprecationWarning):
  1945. return (self.expr.expr_free_symbols - set(self.variables) |
  1946. set(self.point.expr_free_symbols))
  1947. def __eq__(self, other):
  1948. if not isinstance(other, Subs):
  1949. return False
  1950. return self._hashable_content() == other._hashable_content()
  1951. def __ne__(self, other):
  1952. return not(self == other)
  1953. def __hash__(self):
  1954. return super().__hash__()
  1955. def _hashable_content(self):
  1956. return (self._expr.xreplace(self.canonical_variables),
  1957. ) + tuple(ordered([(v, p) for v, p in
  1958. zip(self.variables, self.point) if not self.expr.has(v)]))
  1959. def _eval_subs(self, old, new):
  1960. # Subs doit will do the variables in order; the semantics
  1961. # of subs for Subs is have the following invariant for
  1962. # Subs object foo:
  1963. # foo.doit().subs(reps) == foo.subs(reps).doit()
  1964. pt = list(self.point)
  1965. if old in self.variables:
  1966. if _atomic(new) == {new} and not any(
  1967. i.has(new) for i in self.args):
  1968. # the substitution is neutral
  1969. return self.xreplace({old: new})
  1970. # any occurrence of old before this point will get
  1971. # handled by replacements from here on
  1972. i = self.variables.index(old)
  1973. for j in range(i, len(self.variables)):
  1974. pt[j] = pt[j]._subs(old, new)
  1975. return self.func(self.expr, self.variables, pt)
  1976. v = [i._subs(old, new) for i in self.variables]
  1977. if v != list(self.variables):
  1978. return self.func(self.expr, self.variables + (old,), pt + [new])
  1979. expr = self.expr._subs(old, new)
  1980. pt = [i._subs(old, new) for i in self.point]
  1981. return self.func(expr, v, pt)
  1982. def _eval_derivative(self, s):
  1983. # Apply the chain rule of the derivative on the substitution variables:
  1984. f = self.expr
  1985. vp = V, P = self.variables, self.point
  1986. val = Add.fromiter(p.diff(s)*Subs(f.diff(v), *vp).doit()
  1987. for v, p in zip(V, P))
  1988. # these are all the free symbols in the expr
  1989. efree = f.free_symbols
  1990. # some symbols like IndexedBase include themselves and args
  1991. # as free symbols
  1992. compound = {i for i in efree if len(i.free_symbols) > 1}
  1993. # hide them and see what independent free symbols remain
  1994. dums = {Dummy() for i in compound}
  1995. masked = f.xreplace(dict(zip(compound, dums)))
  1996. ifree = masked.free_symbols - dums
  1997. # include the compound symbols
  1998. free = ifree | compound
  1999. # remove the variables already handled
  2000. free -= set(V)
  2001. # add back any free symbols of remaining compound symbols
  2002. free |= {i for j in free & compound for i in j.free_symbols}
  2003. # if symbols of s are in free then there is more to do
  2004. if free & s.free_symbols:
  2005. val += Subs(f.diff(s), self.variables, self.point).doit()
  2006. return val
  2007. def _eval_nseries(self, x, n, logx, cdir=0):
  2008. if x in self.point:
  2009. # x is the variable being substituted into
  2010. apos = self.point.index(x)
  2011. other = self.variables[apos]
  2012. else:
  2013. other = x
  2014. arg = self.expr.nseries(other, n=n, logx=logx)
  2015. o = arg.getO()
  2016. terms = Add.make_args(arg.removeO())
  2017. rv = Add(*[self.func(a, *self.args[1:]) for a in terms])
  2018. if o:
  2019. rv += o.subs(other, x)
  2020. return rv
  2021. def _eval_as_leading_term(self, x, logx, cdir):
  2022. if x in self.point:
  2023. ipos = self.point.index(x)
  2024. xvar = self.variables[ipos]
  2025. return self.expr.as_leading_term(xvar)
  2026. if x in self.variables:
  2027. # if `x` is a dummy variable, it means it won't exist after the
  2028. # substitution has been performed:
  2029. return self
  2030. # The variable is independent of the substitution:
  2031. return self.expr.as_leading_term(x)
  2032. def diff(f, *symbols, **kwargs):
  2033. """
  2034. Differentiate f with respect to symbols.
  2035. Explanation
  2036. ===========
  2037. This is just a wrapper to unify .diff() and the Derivative class; its
  2038. interface is similar to that of integrate(). You can use the same
  2039. shortcuts for multiple variables as with Derivative. For example,
  2040. diff(f(x), x, x, x) and diff(f(x), x, 3) both return the third derivative
  2041. of f(x).
  2042. You can pass evaluate=False to get an unevaluated Derivative class. Note
  2043. that if there are 0 symbols (such as diff(f(x), x, 0), then the result will
  2044. be the function (the zeroth derivative), even if evaluate=False.
  2045. Examples
  2046. ========
  2047. >>> from sympy import sin, cos, Function, diff
  2048. >>> from sympy.abc import x, y
  2049. >>> f = Function('f')
  2050. >>> diff(sin(x), x)
  2051. cos(x)
  2052. >>> diff(f(x), x, x, x)
  2053. Derivative(f(x), (x, 3))
  2054. >>> diff(f(x), x, 3)
  2055. Derivative(f(x), (x, 3))
  2056. >>> diff(sin(x)*cos(y), x, 2, y, 2)
  2057. sin(x)*cos(y)
  2058. >>> type(diff(sin(x), x))
  2059. cos
  2060. >>> type(diff(sin(x), x, evaluate=False))
  2061. <class 'sympy.core.function.Derivative'>
  2062. >>> type(diff(sin(x), x, 0))
  2063. sin
  2064. >>> type(diff(sin(x), x, 0, evaluate=False))
  2065. sin
  2066. >>> diff(sin(x))
  2067. cos(x)
  2068. >>> diff(sin(x*y))
  2069. Traceback (most recent call last):
  2070. ...
  2071. ValueError: specify differentiation variables to differentiate sin(x*y)
  2072. Note that ``diff(sin(x))`` syntax is meant only for convenience
  2073. in interactive sessions and should be avoided in library code.
  2074. References
  2075. ==========
  2076. .. [1] https://reference.wolfram.com/legacy/v5_2/Built-inFunctions/AlgebraicComputation/Calculus/D.html
  2077. See Also
  2078. ========
  2079. Derivative
  2080. idiff: computes the derivative implicitly
  2081. """
  2082. if hasattr(f, 'diff'):
  2083. return f.diff(*symbols, **kwargs)
  2084. kwargs.setdefault('evaluate', True)
  2085. return _derivative_dispatch(f, *symbols, **kwargs)
  2086. def expand(e, deep=True, modulus=None, power_base=True, power_exp=True,
  2087. mul=True, log=True, multinomial=True, basic=True, **hints):
  2088. r"""
  2089. Expand an expression using methods given as hints.
  2090. Explanation
  2091. ===========
  2092. Hints evaluated unless explicitly set to False are: ``basic``, ``log``,
  2093. ``multinomial``, ``mul``, ``power_base``, and ``power_exp`` The following
  2094. hints are supported but not applied unless set to True: ``complex``,
  2095. ``func``, and ``trig``. In addition, the following meta-hints are
  2096. supported by some or all of the other hints: ``frac``, ``numer``,
  2097. ``denom``, ``modulus``, and ``force``. ``deep`` is supported by all
  2098. hints. Additionally, subclasses of Expr may define their own hints or
  2099. meta-hints.
  2100. The ``basic`` hint is used for any special rewriting of an object that
  2101. should be done automatically (along with the other hints like ``mul``)
  2102. when expand is called. This is a catch-all hint to handle any sort of
  2103. expansion that may not be described by the existing hint names. To use
  2104. this hint an object should override the ``_eval_expand_basic`` method.
  2105. Objects may also define their own expand methods, which are not run by
  2106. default. See the API section below.
  2107. If ``deep`` is set to ``True`` (the default), things like arguments of
  2108. functions are recursively expanded. Use ``deep=False`` to only expand on
  2109. the top level.
  2110. If the ``force`` hint is used, assumptions about variables will be ignored
  2111. in making the expansion.
  2112. Hints
  2113. =====
  2114. These hints are run by default
  2115. mul
  2116. ---
  2117. Distributes multiplication over addition:
  2118. >>> from sympy import cos, exp, sin
  2119. >>> from sympy.abc import x, y, z
  2120. >>> (y*(x + z)).expand(mul=True)
  2121. x*y + y*z
  2122. multinomial
  2123. -----------
  2124. Expand (x + y + ...)**n where n is a positive integer.
  2125. >>> ((x + y + z)**2).expand(multinomial=True)
  2126. x**2 + 2*x*y + 2*x*z + y**2 + 2*y*z + z**2
  2127. power_exp
  2128. ---------
  2129. Expand addition in exponents into multiplied bases.
  2130. >>> exp(x + y).expand(power_exp=True)
  2131. exp(x)*exp(y)
  2132. >>> (2**(x + y)).expand(power_exp=True)
  2133. 2**x*2**y
  2134. power_base
  2135. ----------
  2136. Split powers of multiplied bases.
  2137. This only happens by default if assumptions allow, or if the
  2138. ``force`` meta-hint is used:
  2139. >>> ((x*y)**z).expand(power_base=True)
  2140. (x*y)**z
  2141. >>> ((x*y)**z).expand(power_base=True, force=True)
  2142. x**z*y**z
  2143. >>> ((2*y)**z).expand(power_base=True)
  2144. 2**z*y**z
  2145. Note that in some cases where this expansion always holds, SymPy performs
  2146. it automatically:
  2147. >>> (x*y)**2
  2148. x**2*y**2
  2149. log
  2150. ---
  2151. Pull out power of an argument as a coefficient and split logs products
  2152. into sums of logs.
  2153. Note that these only work if the arguments of the log function have the
  2154. proper assumptions--the arguments must be positive and the exponents must
  2155. be real--or else the ``force`` hint must be True:
  2156. >>> from sympy import log, symbols
  2157. >>> log(x**2*y).expand(log=True)
  2158. log(x**2*y)
  2159. >>> log(x**2*y).expand(log=True, force=True)
  2160. 2*log(x) + log(y)
  2161. >>> x, y = symbols('x,y', positive=True)
  2162. >>> log(x**2*y).expand(log=True)
  2163. 2*log(x) + log(y)
  2164. basic
  2165. -----
  2166. This hint is intended primarily as a way for custom subclasses to enable
  2167. expansion by default.
  2168. These hints are not run by default:
  2169. complex
  2170. -------
  2171. Split an expression into real and imaginary parts.
  2172. >>> x, y = symbols('x,y')
  2173. >>> (x + y).expand(complex=True)
  2174. re(x) + re(y) + I*im(x) + I*im(y)
  2175. >>> cos(x).expand(complex=True)
  2176. -I*sin(re(x))*sinh(im(x)) + cos(re(x))*cosh(im(x))
  2177. Note that this is just a wrapper around ``as_real_imag()``. Most objects
  2178. that wish to redefine ``_eval_expand_complex()`` should consider
  2179. redefining ``as_real_imag()`` instead.
  2180. func
  2181. ----
  2182. Expand other functions.
  2183. >>> from sympy import gamma
  2184. >>> gamma(x + 1).expand(func=True)
  2185. x*gamma(x)
  2186. trig
  2187. ----
  2188. Do trigonometric expansions.
  2189. >>> cos(x + y).expand(trig=True)
  2190. -sin(x)*sin(y) + cos(x)*cos(y)
  2191. >>> sin(2*x).expand(trig=True)
  2192. 2*sin(x)*cos(x)
  2193. Note that the forms of ``sin(n*x)`` and ``cos(n*x)`` in terms of ``sin(x)``
  2194. and ``cos(x)`` are not unique, due to the identity `\sin^2(x) + \cos^2(x)
  2195. = 1`. The current implementation uses the form obtained from Chebyshev
  2196. polynomials, but this may change. See `this MathWorld article
  2197. <https://mathworld.wolfram.com/Multiple-AngleFormulas.html>`_ for more
  2198. information.
  2199. Notes
  2200. =====
  2201. - You can shut off unwanted methods::
  2202. >>> (exp(x + y)*(x + y)).expand()
  2203. x*exp(x)*exp(y) + y*exp(x)*exp(y)
  2204. >>> (exp(x + y)*(x + y)).expand(power_exp=False)
  2205. x*exp(x + y) + y*exp(x + y)
  2206. >>> (exp(x + y)*(x + y)).expand(mul=False)
  2207. (x + y)*exp(x)*exp(y)
  2208. - Use deep=False to only expand on the top level::
  2209. >>> exp(x + exp(x + y)).expand()
  2210. exp(x)*exp(exp(x)*exp(y))
  2211. >>> exp(x + exp(x + y)).expand(deep=False)
  2212. exp(x)*exp(exp(x + y))
  2213. - Hints are applied in an arbitrary, but consistent order (in the current
  2214. implementation, they are applied in alphabetical order, except
  2215. multinomial comes before mul, but this may change). Because of this,
  2216. some hints may prevent expansion by other hints if they are applied
  2217. first. For example, ``mul`` may distribute multiplications and prevent
  2218. ``log`` and ``power_base`` from expanding them. Also, if ``mul`` is
  2219. applied before ``multinomial`, the expression might not be fully
  2220. distributed. The solution is to use the various ``expand_hint`` helper
  2221. functions or to use ``hint=False`` to this function to finely control
  2222. which hints are applied. Here are some examples::
  2223. >>> from sympy import expand, expand_mul, expand_power_base
  2224. >>> x, y, z = symbols('x,y,z', positive=True)
  2225. >>> expand(log(x*(y + z)))
  2226. log(x) + log(y + z)
  2227. Here, we see that ``log`` was applied before ``mul``. To get the mul
  2228. expanded form, either of the following will work::
  2229. >>> expand_mul(log(x*(y + z)))
  2230. log(x*y + x*z)
  2231. >>> expand(log(x*(y + z)), log=False)
  2232. log(x*y + x*z)
  2233. A similar thing can happen with the ``power_base`` hint::
  2234. >>> expand((x*(y + z))**x)
  2235. (x*y + x*z)**x
  2236. To get the ``power_base`` expanded form, either of the following will
  2237. work::
  2238. >>> expand((x*(y + z))**x, mul=False)
  2239. x**x*(y + z)**x
  2240. >>> expand_power_base((x*(y + z))**x)
  2241. x**x*(y + z)**x
  2242. >>> expand((x + y)*y/x)
  2243. y + y**2/x
  2244. The parts of a rational expression can be targeted::
  2245. >>> expand((x + y)*y/x/(x + 1), frac=True)
  2246. (x*y + y**2)/(x**2 + x)
  2247. >>> expand((x + y)*y/x/(x + 1), numer=True)
  2248. (x*y + y**2)/(x*(x + 1))
  2249. >>> expand((x + y)*y/x/(x + 1), denom=True)
  2250. y*(x + y)/(x**2 + x)
  2251. - The ``modulus`` meta-hint can be used to reduce the coefficients of an
  2252. expression post-expansion::
  2253. >>> expand((3*x + 1)**2)
  2254. 9*x**2 + 6*x + 1
  2255. >>> expand((3*x + 1)**2, modulus=5)
  2256. 4*x**2 + x + 1
  2257. - Either ``expand()`` the function or ``.expand()`` the method can be
  2258. used. Both are equivalent::
  2259. >>> expand((x + 1)**2)
  2260. x**2 + 2*x + 1
  2261. >>> ((x + 1)**2).expand()
  2262. x**2 + 2*x + 1
  2263. API
  2264. ===
  2265. Objects can define their own expand hints by defining
  2266. ``_eval_expand_hint()``. The function should take the form::
  2267. def _eval_expand_hint(self, **hints):
  2268. # Only apply the method to the top-level expression
  2269. ...
  2270. See also the example below. Objects should define ``_eval_expand_hint()``
  2271. methods only if ``hint`` applies to that specific object. The generic
  2272. ``_eval_expand_hint()`` method defined in Expr will handle the no-op case.
  2273. Each hint should be responsible for expanding that hint only.
  2274. Furthermore, the expansion should be applied to the top-level expression
  2275. only. ``expand()`` takes care of the recursion that happens when
  2276. ``deep=True``.
  2277. You should only call ``_eval_expand_hint()`` methods directly if you are
  2278. 100% sure that the object has the method, as otherwise you are liable to
  2279. get unexpected ``AttributeError``s. Note, again, that you do not need to
  2280. recursively apply the hint to args of your object: this is handled
  2281. automatically by ``expand()``. ``_eval_expand_hint()`` should
  2282. generally not be used at all outside of an ``_eval_expand_hint()`` method.
  2283. If you want to apply a specific expansion from within another method, use
  2284. the public ``expand()`` function, method, or ``expand_hint()`` functions.
  2285. In order for expand to work, objects must be rebuildable by their args,
  2286. i.e., ``obj.func(*obj.args) == obj`` must hold.
  2287. Expand methods are passed ``**hints`` so that expand hints may use
  2288. 'metahints'--hints that control how different expand methods are applied.
  2289. For example, the ``force=True`` hint described above that causes
  2290. ``expand(log=True)`` to ignore assumptions is such a metahint. The
  2291. ``deep`` meta-hint is handled exclusively by ``expand()`` and is not
  2292. passed to ``_eval_expand_hint()`` methods.
  2293. Note that expansion hints should generally be methods that perform some
  2294. kind of 'expansion'. For hints that simply rewrite an expression, use the
  2295. .rewrite() API.
  2296. Examples
  2297. ========
  2298. >>> from sympy import Expr, sympify
  2299. >>> class MyClass(Expr):
  2300. ... def __new__(cls, *args):
  2301. ... args = sympify(args)
  2302. ... return Expr.__new__(cls, *args)
  2303. ...
  2304. ... def _eval_expand_double(self, *, force=False, **hints):
  2305. ... '''
  2306. ... Doubles the args of MyClass.
  2307. ...
  2308. ... If there more than four args, doubling is not performed,
  2309. ... unless force=True is also used (False by default).
  2310. ... '''
  2311. ... if not force and len(self.args) > 4:
  2312. ... return self
  2313. ... return self.func(*(self.args + self.args))
  2314. ...
  2315. >>> a = MyClass(1, 2, MyClass(3, 4))
  2316. >>> a
  2317. MyClass(1, 2, MyClass(3, 4))
  2318. >>> a.expand(double=True)
  2319. MyClass(1, 2, MyClass(3, 4, 3, 4), 1, 2, MyClass(3, 4, 3, 4))
  2320. >>> a.expand(double=True, deep=False)
  2321. MyClass(1, 2, MyClass(3, 4), 1, 2, MyClass(3, 4))
  2322. >>> b = MyClass(1, 2, 3, 4, 5)
  2323. >>> b.expand(double=True)
  2324. MyClass(1, 2, 3, 4, 5)
  2325. >>> b.expand(double=True, force=True)
  2326. MyClass(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
  2327. See Also
  2328. ========
  2329. expand_log, expand_mul, expand_multinomial, expand_complex, expand_trig,
  2330. expand_power_base, expand_power_exp, expand_func, sympy.simplify.hyperexpand.hyperexpand
  2331. """
  2332. # don't modify this; modify the Expr.expand method
  2333. hints['power_base'] = power_base
  2334. hints['power_exp'] = power_exp
  2335. hints['mul'] = mul
  2336. hints['log'] = log
  2337. hints['multinomial'] = multinomial
  2338. hints['basic'] = basic
  2339. return sympify(e).expand(deep=deep, modulus=modulus, **hints)
  2340. # This is a special application of two hints
  2341. def _mexpand(expr, recursive=False):
  2342. # expand multinomials and then expand products; this may not always
  2343. # be sufficient to give a fully expanded expression (see
  2344. # test_issue_8247_8354 in test_arit)
  2345. if expr is None:
  2346. return
  2347. was = None
  2348. while was != expr:
  2349. was, expr = expr, expand_mul(expand_multinomial(expr))
  2350. if not recursive:
  2351. break
  2352. return expr
  2353. # These are simple wrappers around single hints.
  2354. def expand_mul(expr, deep=True):
  2355. """
  2356. Wrapper around expand that only uses the mul hint. See the expand
  2357. docstring for more information.
  2358. Examples
  2359. ========
  2360. >>> from sympy import symbols, expand_mul, exp, log
  2361. >>> x, y = symbols('x,y', positive=True)
  2362. >>> expand_mul(exp(x+y)*(x+y)*log(x*y**2))
  2363. x*exp(x + y)*log(x*y**2) + y*exp(x + y)*log(x*y**2)
  2364. """
  2365. return sympify(expr).expand(deep=deep, mul=True, power_exp=False,
  2366. power_base=False, basic=False, multinomial=False, log=False)
  2367. def expand_multinomial(expr, deep=True):
  2368. """
  2369. Wrapper around expand that only uses the multinomial hint. See the expand
  2370. docstring for more information.
  2371. Examples
  2372. ========
  2373. >>> from sympy import symbols, expand_multinomial, exp
  2374. >>> x, y = symbols('x y', positive=True)
  2375. >>> expand_multinomial((x + exp(x + 1))**2)
  2376. x**2 + 2*x*exp(x + 1) + exp(2*x + 2)
  2377. """
  2378. return sympify(expr).expand(deep=deep, mul=False, power_exp=False,
  2379. power_base=False, basic=False, multinomial=True, log=False)
  2380. def expand_log(expr, deep=True, force=False, factor=False):
  2381. """
  2382. Wrapper around expand that only uses the log hint. See the expand
  2383. docstring for more information.
  2384. Examples
  2385. ========
  2386. >>> from sympy import symbols, expand_log, exp, log
  2387. >>> x, y = symbols('x,y', positive=True)
  2388. >>> expand_log(exp(x+y)*(x+y)*log(x*y**2))
  2389. (x + y)*(log(x) + 2*log(y))*exp(x + y)
  2390. """
  2391. from sympy.functions.elementary.exponential import log
  2392. from sympy.simplify.radsimp import fraction
  2393. if factor is False:
  2394. def _handleMul(x):
  2395. # look for the simple case of expanded log(b**a)/log(b) -> a in args
  2396. n, d = fraction(x)
  2397. n = [i for i in n.atoms(log) if i.args[0].is_Integer]
  2398. d = [i for i in d.atoms(log) if i.args[0].is_Integer]
  2399. if len(n) == 1 and len(d) == 1:
  2400. n = n[0]
  2401. d = d[0]
  2402. from sympy import multiplicity
  2403. m = multiplicity(d.args[0], n.args[0])
  2404. if m:
  2405. r = m + log(n.args[0]//d.args[0]**m)/d
  2406. x = x.subs(n, d*r)
  2407. x1 = expand_mul(expand_log(x, deep=deep, force=force, factor=True))
  2408. if x1.count(log) <= x.count(log):
  2409. return x1
  2410. return x
  2411. expr = expr.replace(
  2412. lambda x: x.is_Mul and all(any(isinstance(i, log) and i.args[0].is_Rational
  2413. for i in Mul.make_args(j)) for j in x.as_numer_denom()),
  2414. _handleMul)
  2415. return sympify(expr).expand(deep=deep, log=True, mul=False,
  2416. power_exp=False, power_base=False, multinomial=False,
  2417. basic=False, force=force, factor=factor)
  2418. def expand_func(expr, deep=True):
  2419. """
  2420. Wrapper around expand that only uses the func hint. See the expand
  2421. docstring for more information.
  2422. Examples
  2423. ========
  2424. >>> from sympy import expand_func, gamma
  2425. >>> from sympy.abc import x
  2426. >>> expand_func(gamma(x + 2))
  2427. x*(x + 1)*gamma(x)
  2428. """
  2429. return sympify(expr).expand(deep=deep, func=True, basic=False,
  2430. log=False, mul=False, power_exp=False, power_base=False, multinomial=False)
  2431. def expand_trig(expr, deep=True):
  2432. """
  2433. Wrapper around expand that only uses the trig hint. See the expand
  2434. docstring for more information.
  2435. Examples
  2436. ========
  2437. >>> from sympy import expand_trig, sin
  2438. >>> from sympy.abc import x, y
  2439. >>> expand_trig(sin(x+y)*(x+y))
  2440. (x + y)*(sin(x)*cos(y) + sin(y)*cos(x))
  2441. """
  2442. return sympify(expr).expand(deep=deep, trig=True, basic=False,
  2443. log=False, mul=False, power_exp=False, power_base=False, multinomial=False)
  2444. def expand_complex(expr, deep=True):
  2445. """
  2446. Wrapper around expand that only uses the complex hint. See the expand
  2447. docstring for more information.
  2448. Examples
  2449. ========
  2450. >>> from sympy import expand_complex, exp, sqrt, I
  2451. >>> from sympy.abc import z
  2452. >>> expand_complex(exp(z))
  2453. I*exp(re(z))*sin(im(z)) + exp(re(z))*cos(im(z))
  2454. >>> expand_complex(sqrt(I))
  2455. sqrt(2)/2 + sqrt(2)*I/2
  2456. See Also
  2457. ========
  2458. sympy.core.expr.Expr.as_real_imag
  2459. """
  2460. return sympify(expr).expand(deep=deep, complex=True, basic=False,
  2461. log=False, mul=False, power_exp=False, power_base=False, multinomial=False)
  2462. def expand_power_base(expr, deep=True, force=False):
  2463. """
  2464. Wrapper around expand that only uses the power_base hint.
  2465. A wrapper to expand(power_base=True) which separates a power with a base
  2466. that is a Mul into a product of powers, without performing any other
  2467. expansions, provided that assumptions about the power's base and exponent
  2468. allow.
  2469. deep=False (default is True) will only apply to the top-level expression.
  2470. force=True (default is False) will cause the expansion to ignore
  2471. assumptions about the base and exponent. When False, the expansion will
  2472. only happen if the base is non-negative or the exponent is an integer.
  2473. >>> from sympy.abc import x, y, z
  2474. >>> from sympy import expand_power_base, sin, cos, exp, Symbol
  2475. >>> (x*y)**2
  2476. x**2*y**2
  2477. >>> (2*x)**y
  2478. (2*x)**y
  2479. >>> expand_power_base(_)
  2480. 2**y*x**y
  2481. >>> expand_power_base((x*y)**z)
  2482. (x*y)**z
  2483. >>> expand_power_base((x*y)**z, force=True)
  2484. x**z*y**z
  2485. >>> expand_power_base(sin((x*y)**z), deep=False)
  2486. sin((x*y)**z)
  2487. >>> expand_power_base(sin((x*y)**z), force=True)
  2488. sin(x**z*y**z)
  2489. >>> expand_power_base((2*sin(x))**y + (2*cos(x))**y)
  2490. 2**y*sin(x)**y + 2**y*cos(x)**y
  2491. >>> expand_power_base((2*exp(y))**x)
  2492. 2**x*exp(y)**x
  2493. >>> expand_power_base((2*cos(x))**y)
  2494. 2**y*cos(x)**y
  2495. Notice that sums are left untouched. If this is not the desired behavior,
  2496. apply full ``expand()`` to the expression:
  2497. >>> expand_power_base(((x+y)*z)**2)
  2498. z**2*(x + y)**2
  2499. >>> (((x+y)*z)**2).expand()
  2500. x**2*z**2 + 2*x*y*z**2 + y**2*z**2
  2501. >>> expand_power_base((2*y)**(1+z))
  2502. 2**(z + 1)*y**(z + 1)
  2503. >>> ((2*y)**(1+z)).expand()
  2504. 2*2**z*y**(z + 1)
  2505. The power that is unexpanded can be expanded safely when
  2506. ``y != 0``, otherwise different values might be obtained for the expression:
  2507. >>> prev = _
  2508. If we indicate that ``y`` is positive but then replace it with
  2509. a value of 0 after expansion, the expression becomes 0:
  2510. >>> p = Symbol('p', positive=True)
  2511. >>> prev.subs(y, p).expand().subs(p, 0)
  2512. 0
  2513. But if ``z = -1`` the expression would not be zero:
  2514. >>> prev.subs(y, 0).subs(z, -1)
  2515. 1
  2516. See Also
  2517. ========
  2518. expand
  2519. """
  2520. return sympify(expr).expand(deep=deep, log=False, mul=False,
  2521. power_exp=False, power_base=True, multinomial=False,
  2522. basic=False, force=force)
  2523. def expand_power_exp(expr, deep=True):
  2524. """
  2525. Wrapper around expand that only uses the power_exp hint.
  2526. See the expand docstring for more information.
  2527. Examples
  2528. ========
  2529. >>> from sympy import expand_power_exp, Symbol
  2530. >>> from sympy.abc import x, y
  2531. >>> expand_power_exp(3**(y + 2))
  2532. 9*3**y
  2533. >>> expand_power_exp(x**(y + 2))
  2534. x**(y + 2)
  2535. If ``x = 0`` the value of the expression depends on the
  2536. value of ``y``; if the expression were expanded the result
  2537. would be 0. So expansion is only done if ``x != 0``:
  2538. >>> expand_power_exp(Symbol('x', zero=False)**(y + 2))
  2539. x**2*x**y
  2540. """
  2541. return sympify(expr).expand(deep=deep, complex=False, basic=False,
  2542. log=False, mul=False, power_exp=True, power_base=False, multinomial=False)
  2543. def count_ops(expr, visual=False):
  2544. """
  2545. Return a representation (integer or expression) of the operations in expr.
  2546. Parameters
  2547. ==========
  2548. expr : Expr
  2549. If expr is an iterable, the sum of the op counts of the
  2550. items will be returned.
  2551. visual : bool, optional
  2552. If ``False`` (default) then the sum of the coefficients of the
  2553. visual expression will be returned.
  2554. If ``True`` then the number of each type of operation is shown
  2555. with the core class types (or their virtual equivalent) multiplied by the
  2556. number of times they occur.
  2557. Examples
  2558. ========
  2559. >>> from sympy.abc import a, b, x, y
  2560. >>> from sympy import sin, count_ops
  2561. Although there is not a SUB object, minus signs are interpreted as
  2562. either negations or subtractions:
  2563. >>> (x - y).count_ops(visual=True)
  2564. SUB
  2565. >>> (-x).count_ops(visual=True)
  2566. NEG
  2567. Here, there are two Adds and a Pow:
  2568. >>> (1 + a + b**2).count_ops(visual=True)
  2569. 2*ADD + POW
  2570. In the following, an Add, Mul, Pow and two functions:
  2571. >>> (sin(x)*x + sin(x)**2).count_ops(visual=True)
  2572. ADD + MUL + POW + 2*SIN
  2573. for a total of 5:
  2574. >>> (sin(x)*x + sin(x)**2).count_ops(visual=False)
  2575. 5
  2576. Note that "what you type" is not always what you get. The expression
  2577. 1/x/y is translated by sympy into 1/(x*y) so it gives a DIV and MUL rather
  2578. than two DIVs:
  2579. >>> (1/x/y).count_ops(visual=True)
  2580. DIV + MUL
  2581. The visual option can be used to demonstrate the difference in
  2582. operations for expressions in different forms. Here, the Horner
  2583. representation is compared with the expanded form of a polynomial:
  2584. >>> eq=x*(1 + x*(2 + x*(3 + x)))
  2585. >>> count_ops(eq.expand(), visual=True) - count_ops(eq, visual=True)
  2586. -MUL + 3*POW
  2587. The count_ops function also handles iterables:
  2588. >>> count_ops([x, sin(x), None, True, x + 2], visual=False)
  2589. 2
  2590. >>> count_ops([x, sin(x), None, True, x + 2], visual=True)
  2591. ADD + SIN
  2592. >>> count_ops({x: sin(x), x + 2: y + 1}, visual=True)
  2593. 2*ADD + SIN
  2594. """
  2595. from .relational import Relational
  2596. from sympy.concrete.summations import Sum
  2597. from sympy.integrals.integrals import Integral
  2598. from sympy.logic.boolalg import BooleanFunction
  2599. from sympy.simplify.radsimp import fraction
  2600. expr = sympify(expr)
  2601. if isinstance(expr, Expr) and not expr.is_Relational:
  2602. ops = []
  2603. args = [expr]
  2604. NEG = Symbol('NEG')
  2605. DIV = Symbol('DIV')
  2606. SUB = Symbol('SUB')
  2607. ADD = Symbol('ADD')
  2608. EXP = Symbol('EXP')
  2609. while args:
  2610. a = args.pop()
  2611. # if the following fails because the object is
  2612. # not Basic type, then the object should be fixed
  2613. # since it is the intention that all args of Basic
  2614. # should themselves be Basic
  2615. if a.is_Rational:
  2616. #-1/3 = NEG + DIV
  2617. if a is not S.One:
  2618. if a.p < 0:
  2619. ops.append(NEG)
  2620. if a.q != 1:
  2621. ops.append(DIV)
  2622. continue
  2623. elif a.is_Mul or a.is_MatMul:
  2624. if _coeff_isneg(a):
  2625. ops.append(NEG)
  2626. if a.args[0] is S.NegativeOne:
  2627. a = a.as_two_terms()[1]
  2628. else:
  2629. a = -a
  2630. n, d = fraction(a)
  2631. if n.is_Integer:
  2632. ops.append(DIV)
  2633. if n < 0:
  2634. ops.append(NEG)
  2635. args.append(d)
  2636. continue # won't be -Mul but could be Add
  2637. elif d is not S.One:
  2638. if not d.is_Integer:
  2639. args.append(d)
  2640. ops.append(DIV)
  2641. args.append(n)
  2642. continue # could be -Mul
  2643. elif a.is_Add or a.is_MatAdd:
  2644. aargs = list(a.args)
  2645. negs = 0
  2646. for i, ai in enumerate(aargs):
  2647. if _coeff_isneg(ai):
  2648. negs += 1
  2649. args.append(-ai)
  2650. if i > 0:
  2651. ops.append(SUB)
  2652. else:
  2653. args.append(ai)
  2654. if i > 0:
  2655. ops.append(ADD)
  2656. if negs == len(aargs): # -x - y = NEG + SUB
  2657. ops.append(NEG)
  2658. elif _coeff_isneg(aargs[0]): # -x + y = SUB, but already recorded ADD
  2659. ops.append(SUB - ADD)
  2660. continue
  2661. if a.is_Pow and a.exp is S.NegativeOne:
  2662. ops.append(DIV)
  2663. args.append(a.base) # won't be -Mul but could be Add
  2664. continue
  2665. if a == S.Exp1:
  2666. ops.append(EXP)
  2667. continue
  2668. if a.is_Pow and a.base == S.Exp1:
  2669. ops.append(EXP)
  2670. args.append(a.exp)
  2671. continue
  2672. if a.is_Mul or isinstance(a, LatticeOp):
  2673. o = Symbol(a.func.__name__.upper())
  2674. # count the args
  2675. ops.append(o*(len(a.args) - 1))
  2676. elif a.args and (
  2677. a.is_Pow or a.is_Function or isinstance(a, (Derivative, Integral, Sum))):
  2678. # if it's not in the list above we don't
  2679. # consider a.func something to count, e.g.
  2680. # Tuple, MatrixSymbol, etc...
  2681. if isinstance(a.func, UndefinedFunction):
  2682. o = Symbol("FUNC_" + a.func.__name__.upper())
  2683. else:
  2684. o = Symbol(a.func.__name__.upper())
  2685. ops.append(o)
  2686. if not a.is_Symbol:
  2687. args.extend(a.args)
  2688. elif isinstance(expr, Dict):
  2689. ops = [count_ops(k, visual=visual) +
  2690. count_ops(v, visual=visual) for k, v in expr.items()]
  2691. elif iterable(expr):
  2692. ops = [count_ops(i, visual=visual) for i in expr]
  2693. elif isinstance(expr, (Relational, BooleanFunction)):
  2694. ops = []
  2695. for arg in expr.args:
  2696. ops.append(count_ops(arg, visual=True))
  2697. o = Symbol(func_name(expr, short=True).upper())
  2698. ops.append(o)
  2699. elif not isinstance(expr, Basic):
  2700. ops = []
  2701. else: # it's Basic not isinstance(expr, Expr):
  2702. if not isinstance(expr, Basic):
  2703. raise TypeError("Invalid type of expr")
  2704. else:
  2705. ops = []
  2706. args = [expr]
  2707. while args:
  2708. a = args.pop()
  2709. if a.args:
  2710. o = Symbol(type(a).__name__.upper())
  2711. if a.is_Boolean:
  2712. ops.append(o*(len(a.args)-1))
  2713. else:
  2714. ops.append(o)
  2715. args.extend(a.args)
  2716. if not ops:
  2717. if visual:
  2718. return S.Zero
  2719. return 0
  2720. ops = Add(*ops)
  2721. if visual:
  2722. return ops
  2723. if ops.is_Number:
  2724. return int(ops)
  2725. return sum(int((a.args or [1])[0]) for a in Add.make_args(ops))
  2726. def nfloat(expr, n=15, exponent=False, dkeys=False):
  2727. """Make all Rationals in expr Floats except those in exponents
  2728. (unless the exponents flag is set to True) and those in undefined
  2729. functions. When processing dictionaries, do not modify the keys
  2730. unless ``dkeys=True``.
  2731. Examples
  2732. ========
  2733. >>> from sympy import nfloat, cos, pi, sqrt
  2734. >>> from sympy.abc import x, y
  2735. >>> nfloat(x**4 + x/2 + cos(pi/3) + 1 + sqrt(y))
  2736. x**4 + 0.5*x + sqrt(y) + 1.5
  2737. >>> nfloat(x**4 + sqrt(y), exponent=True)
  2738. x**4.0 + y**0.5
  2739. Container types are not modified:
  2740. >>> type(nfloat((1, 2))) is tuple
  2741. True
  2742. """
  2743. from sympy.matrices.matrixbase import MatrixBase
  2744. kw = {"n": n, "exponent": exponent, "dkeys": dkeys}
  2745. if isinstance(expr, MatrixBase):
  2746. return expr.applyfunc(lambda e: nfloat(e, **kw))
  2747. # handling of iterable containers
  2748. if iterable(expr, exclude=str):
  2749. if isinstance(expr, (dict, Dict)):
  2750. if dkeys:
  2751. args = [tuple((nfloat(i, **kw) for i in a))
  2752. for a in expr.items()]
  2753. else:
  2754. args = [(k, nfloat(v, **kw)) for k, v in expr.items()]
  2755. if isinstance(expr, dict):
  2756. return type(expr)(args)
  2757. else:
  2758. return expr.func(*args)
  2759. elif isinstance(expr, Basic):
  2760. return expr.func(*[nfloat(a, **kw) for a in expr.args])
  2761. return type(expr)([nfloat(a, **kw) for a in expr])
  2762. rv = sympify(expr)
  2763. if rv.is_Number:
  2764. return Float(rv, n)
  2765. elif rv.is_number:
  2766. # evalf doesn't always set the precision
  2767. rv = rv.n(n)
  2768. if rv.is_Number:
  2769. rv = Float(rv.n(n), n)
  2770. else:
  2771. pass # pure_complex(rv) is likely True
  2772. return rv
  2773. elif rv.is_Atom:
  2774. return rv
  2775. elif rv.is_Relational:
  2776. args_nfloat = (nfloat(arg, **kw) for arg in rv.args)
  2777. return rv.func(*args_nfloat)
  2778. # watch out for RootOf instances that don't like to have
  2779. # their exponents replaced with Dummies and also sometimes have
  2780. # problems with evaluating at low precision (issue 6393)
  2781. from sympy.polys.rootoftools import RootOf
  2782. rv = rv.xreplace({ro: ro.n(n) for ro in rv.atoms(RootOf)})
  2783. from .power import Pow
  2784. if not exponent:
  2785. reps = [(p, Pow(p.base, Dummy())) for p in rv.atoms(Pow)]
  2786. rv = rv.xreplace(dict(reps))
  2787. rv = rv.n(n)
  2788. if not exponent:
  2789. rv = rv.xreplace({d.exp: p.exp for p, d in reps})
  2790. else:
  2791. # Pow._eval_evalf special cases Integer exponents so if
  2792. # exponent is suppose to be handled we have to do so here
  2793. rv = rv.xreplace(Transform(
  2794. lambda x: Pow(x.base, Float(x.exp, n)),
  2795. lambda x: x.is_Pow and x.exp.is_Integer))
  2796. return rv.xreplace(Transform(
  2797. lambda x: x.func(*nfloat(x.args, n, exponent)),
  2798. lambda x: isinstance(x, Function) and not isinstance(x, AppliedUndef)))
  2799. from .symbol import Dummy, Symbol