K iMddlmZmZddlmZmZddlmZddlm Z m Z ddl m Z m Z ddlmZddlmZddlmZdd lmZGd d eZGd d eZy))Sdiff)DefinedFunctionArgumentIndexError) fuzzy_not)EqNe)imsign) Piecewise)PolynomialError)roots) filldedentc\eZdZdZdZd dZeejfdZ dZ dZ dZ dZ y ) DiracDeltaa: The DiracDelta function and its derivatives. Explanation =========== DiracDelta is not an ordinary function. It can be rigorously defined either as a distribution or as a measure. DiracDelta only makes sense in definite integrals, and in particular, integrals of the form ``Integral(f(x)*DiracDelta(x - x0), (x, a, b))``, where it equals ``f(x0)`` if ``a <= x0 <= b`` and ``0`` otherwise. Formally, DiracDelta acts in some ways like a function that is ``0`` everywhere except at ``0``, but in many ways it also does not. It can often be useful to treat DiracDelta in formal ways, building up and manipulating expressions with delta functions (which may eventually be integrated), but care must be taken to not treat it as a real function. SymPy's ``oo`` is similar. It only truly makes sense formally in certain contexts (such as integration limits), but SymPy allows its use everywhere, and it tries to be consistent with operations on it (like ``1/oo``), but it is easy to get into trouble and get wrong results if ``oo`` is treated too much like a number. Similarly, if DiracDelta is treated too much like a function, it is easy to get wrong or nonsensical results. DiracDelta function has the following properties: 1) $\frac{d}{d x} \theta(x) = \delta(x)$ 2) $\int_{-\infty}^\infty \delta(x - a)f(x)\, dx = f(a)$ and $\int_{a- \epsilon}^{a+\epsilon} \delta(x - a)f(x)\, dx = f(a)$ 3) $\delta(x) = 0$ for all $x \neq 0$ 4) $\delta(g(x)) = \sum_i \frac{\delta(x - x_i)}{\|g'(x_i)\|}$ where $x_i$ are the roots of $g$ 5) $\delta(-x) = \delta(x)$ Derivatives of ``k``-th order of DiracDelta have the following properties: 6) $\delta(x, k) = 0$ for all $x \neq 0$ 7) $\delta(-x, k) = -\delta(x, k)$ for odd $k$ 8) $\delta(-x, k) = \delta(x, k)$ for even $k$ Examples ======== >>> from sympy import DiracDelta, diff, pi >>> from sympy.abc import x, y >>> DiracDelta(x) DiracDelta(x) >>> DiracDelta(1) 0 >>> DiracDelta(-1) 0 >>> DiracDelta(pi) 0 >>> DiracDelta(x - 4).subs(x, 4) DiracDelta(0) >>> diff(DiracDelta(x)) DiracDelta(x, 1) >>> diff(DiracDelta(x - 1), x, 2) DiracDelta(x - 1, 2) >>> diff(DiracDelta(x**2 - 1), x, 2) 2*(2*x**2*DiracDelta(x**2 - 1, 2) + DiracDelta(x**2 - 1, 1)) >>> DiracDelta(3*x).is_simple(x) True >>> DiracDelta(x**2).is_simple(x) False >>> DiracDelta((x**2 - 1)*y).expand(diracdelta=True, wrt=x) DiracDelta(x - 1)/(2*Abs(y)) + DiracDelta(x + 1)/(2*Abs(y)) See Also ======== Heaviside sympy.simplify.simplify.simplify, is_simple sympy.functions.special.tensor_functions.KroneckerDelta References ========== .. [1] https://mathworld.wolfram.com/DeltaFunction.html Tc|dk(rKd}t|jdkDr|jd}|j|jd|dzSt||)a0 Returns the first derivative of a DiracDelta Function. Explanation =========== The difference between ``diff()`` and ``fdiff()`` is: ``diff()`` is the user-level function and ``fdiff()`` is an object method. ``fdiff()`` is a convenience method available in the ``Function`` class. It returns the derivative of the function without considering the chain rule. ``diff(function, x)`` calls ``Function._eval_derivative`` which in turn calls ``fdiff()`` internally to compute the derivative of the function. Examples ======== >>> from sympy import DiracDelta, diff >>> from sympy.abc import x >>> DiracDelta(x).fdiff() DiracDelta(x, 1) >>> DiracDelta(x, 1).fdiff() DiracDelta(x, 2) >>> DiracDelta(x**2 - 1).fdiff() DiracDelta(x**2 - 1, 1) >>> diff(DiracDelta(x, 1)).fdiff() DiracDelta(x, 3) Parameters ========== argindex : integer degree of derivative r)lenargsfuncr)selfargindexks m/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/functions/special/delta_functions.pyfdiffzDiracDelta.fdiffgsXN q=A499~!IIaL99TYYq\1q51 1$T84 4c h|jr |jrtd|d|tjurtjS|j rtj Stt|jr6ttdtt|dt|d|j\}}|rN|dtjur9|jr || | S|jr|r || |S|| Sy |jr ||dSy ) a Returns a simplified form or a value of DiracDelta depending on the argument passed by the DiracDelta object. Explanation =========== The ``eval()`` method is automatically called when the ``DiracDelta`` class is about to be instantiated and it returns either some simplified instance or the unevaluated instance depending on the argument passed. In other words, ``eval()`` method is not needed to be called explicitly, it is being called and evaluated once the object is called. Examples ======== >>> from sympy import DiracDelta, S >>> from sympy.abc import x >>> DiracDelta(x) DiracDelta(x) >>> DiracDelta(-x, 1) -DiracDelta(x, 1) >>> DiracDelta(1) 0 >>> DiracDelta(5, 1) 0 >>> DiracDelta(0) DiracDelta(0) >>> DiracDelta(-1) 0 >>> DiracDelta(S.NaN) nan >>> DiracDelta(x - 100).subs(x, 5) 0 >>> DiracDelta(x - 100).subs(x, 100) DiracDelta(0) Parameters ========== k : integer order of derivative arg : argument passed to DiracDelta zUError: the second argument of DiracDelta must be a non-negative integer, z given instead.zV Function defined only for Real Values. Complex part: found in  .rF)evaluateN) is_Integer is_negative ValueErrorrNaN is_nonzeroZerorr is_zerorreprargs_cnc NegativeOneis_oddis_even)clsargrcncs revalzDiracDelta.evalsr||q}}:;>? ? !%%<55L >>66M RW__ %ZRW tCy)*+, , 2 1&xxSD! }$'(sC4|7c3$i7 YYsU+ +rc |jdd}|?|j}t|dk(r|j}nt t d|j dj|r*t|j dkDr|j ddk7r|S t|j d|}d}d}tt|j d|}|jD]F\}} |jdur0| dk(r+||j||z |j||z z }Dd}n|r|S |S#t$rY|SwxYw)aA Compute a simplified representation of the function using property number 4. Pass ``wrt`` as a hint to expand the expression with respect to a particular variable. Explanation =========== ``wrt`` is: - a variable with respect to which a DiracDelta expression will get expanded. Examples ======== >>> from sympy import DiracDelta >>> from sympy.abc import x, y >>> DiracDelta(x*y).expand(diracdelta=True, wrt=x) DiracDelta(x)/Abs(y) >>> DiracDelta(x*y).expand(diracdelta=True, wrt=y) DiracDelta(y)/Abs(x) >>> DiracDelta(x**2 + x - 2).expand(diracdelta=True, wrt=x) DiracDelta(x - 1)/3 + DiracDelta(x + 2)/3 See Also ======== is_simple, Diracdelta wrtNrz When there is more than 1 free symbol or variable in the expression, the 'wrt' keyword is required as a hint to expand when using the DiracDelta hint.rTF)get free_symbolsrpop TypeErrorrrhasrabsritemsis_realrsubsr ) rhintsr3freeargrootsresultvaliddargrms r_eval_expand_diracdeltaz"DiracDelta._eval_expand_diracdeltasXDiit$ ;$$D4yA~hhj , !!"" yy|$TYY!); ! PQ@QK TYYq\3/HFEtDIIaL#./D ( 199E)a1fdiia031BBBF "E       sBD>> E  E cj|jdj|}|r|jdk(Sy)aj Tells whether the argument(args[0]) of DiracDelta is a linear expression in *x*. Examples ======== >>> from sympy import DiracDelta, cos >>> from sympy.abc import x, y >>> DiracDelta(x*y).is_simple(x) True >>> DiracDelta(x*y).is_simple(y) True >>> DiracDelta(x**2 + x - 2).is_simple(x) False >>> DiracDelta(cos(x)).is_simple(x) False Parameters ========== x : can be a symbol See Also ======== sympy.simplify.simplify.simplify, DiracDelta rrF)ras_polydegree)rxps r is_simplezDiracDelta.is_simple*s3B IIaL  # 88:? "rcht|dk(r$ttdt|ddfdSy)aN Represents DiracDelta in a piecewise form. Examples ======== >>> from sympy import DiracDelta, Piecewise, Symbol >>> x = Symbol('x') >>> DiracDelta(x).rewrite(Piecewise) Piecewise((DiracDelta(0), Eq(x, 0)), (0, True)) >>> DiracDelta(x - 5).rewrite(Piecewise) Piecewise((DiracDelta(0), Eq(x, 5)), (0, True)) >>> DiracDelta(x**2 - 5).rewrite(Piecewise) Piecewise((DiracDelta(0), Eq(x**2, 5)), (0, True)) >>> DiracDelta(x - 5, 4).rewrite(Piecewise) DiracDelta(x - 5, 4) rr)rTN)rr rr)rrkwargss r_eval_rewrite_as_Piecewisez%DiracDelta._eval_rewrite_as_PiecewisePs4. t9>jmRQ^rIs r$_eval_rewrite_as_SingularityFunctionz/DiracDelta._eval_rewrite_as_SingularityFunctionjs (U :a= &q!R0 0 :a# #&q!R0 0   t9>A4yA~*1eDGQ.?.BBGG&q%Q*;A*>a1 M MJ(:;< 0 \end{cases}$ 3) $\frac{d}{d x} \max(x, 0) = \theta(x)$ Heaviside(x) is printed as $\theta(x)$ with the SymPy LaTeX printer. The value at 0 is set differently in different fields. SymPy uses 1/2, which is a convention from electronics and signal processing, and is consistent with solving improper integrals by Fourier transform and convolution. To specify a different value of Heaviside at ``x=0``, a second argument can be given. Using ``Heaviside(x, nan)`` gives an expression that will evaluate to nan for x=0. .. versionchanged:: 1.9 ``Heaviside(0)`` now returns 1/2 (before: undefined) Examples ======== >>> from sympy import Heaviside, nan >>> from sympy.abc import x >>> Heaviside(9) 1 >>> Heaviside(-9) 0 >>> Heaviside(0) 1/2 >>> Heaviside(0, nan) nan >>> (Heaviside(x) + 1).replace(Heaviside(x), Heaviside(x, 1)) Heaviside(x, 1) + 1 See Also ======== DiracDelta References ========== .. [1] https://mathworld.wolfram.com/HeavisideStepFunction.html .. [2] https://dlmf.nist.gov/1.16#iv TcT|dk(rt|jdSt||)a Returns the first derivative of a Heaviside Function. Examples ======== >>> from sympy import Heaviside, diff >>> from sympy.abc import x >>> Heaviside(x).fdiff() DiracDelta(x) >>> Heaviside(x**2 - 1).fdiff() DiracDelta(x**2 - 1) >>> diff(Heaviside(x)).fdiff() DiracDelta(x, 1) Parameters ========== argindex : integer order of derivative rr)rrr)rrs rrzHeaviside.fdiffs+4 q=diil+ +$T84 4rc t|tr(t|jdk(rtj }t |||||fi|S)Nr) isinstancerarrrHalfsuper__new__)r-r.H0options __class__s rrgzHeaviside.__new__s@ b) $RWW):BS#&sC?w??rcR|j}|dtjur|dd}|S)zArgs without default S.HalfrN)rrre)rrs rpargszHeaviside.pargss-yy 7aff 8D rcj|jrtjS|jrtjS|j r|S|tj urtj Stt|j r-tdtt|dt|dy)a~ Returns a simplified form or a value of Heaviside depending on the argument passed by the Heaviside object. Explanation =========== The ``eval()`` method is automatically called when the ``Heaviside`` class is about to be instantiated and it returns either some simplified instance or the unevaluated instance depending on the argument passed. In other words, ``eval()`` method is not needed to be called explicitly, it is being called and evaluated once the object is called. Examples ======== >>> from sympy import Heaviside, S >>> from sympy.abc import x >>> Heaviside(x) Heaviside(x) >>> Heaviside(19) 1 >>> Heaviside(0) 1/2 >>> Heaviside(0, 1) 1 >>> Heaviside(-5) 0 >>> Heaviside(S.NaN) nan >>> Heaviside(x - 100).subs(x, 5) 0 >>> Heaviside(x - 100).subs(x, 105) 1 Parameters ========== arg : argument passed by Heaviside object H0 : value of Heaviside(0) z5Function defined only for Real Values. Complex part: rrN) is_extended_negativerr&is_extended_positiveOner'r$rr r#r()r-r.rhs rr1zHeaviside.evalsj  # #66M  % %55L [[I AEE\55L r#w 'imnpqtnuivx|~AyBCE E(rc |dk(rtd|dkfdS|dk(rtd|dkfdStd|dkf|t|dfdS)am Represents Heaviside in a Piecewise form. Examples ======== >>> from sympy import Heaviside, Piecewise, Symbol, nan >>> x = Symbol('x') >>> Heaviside(x).rewrite(Piecewise) Piecewise((0, x < 0), (1/2, Eq(x, 0)), (1, True)) >>> Heaviside(x,nan).rewrite(Piecewise) Piecewise((0, x < 0), (nan, Eq(x, 0)), (1, True)) >>> Heaviside(x - 5).rewrite(Piecewise) Piecewise((0, x < 5), (1/2, Eq(x, 5)), (1, True)) >>> Heaviside(x**2 - 1).rewrite(Piecewise) Piecewise((0, x**2 < 1), (1/2, Eq(x**2, 1)), (1, True)) r)rTr)r r)rr.rhrMs rrNz$Heaviside._eval_rewrite_as_Piecewise/s^. 7a]I6 6 7aq\95 5!S1WBsAJ'7CCrc |jrxtt|dzdz t|dft d|df}tt|dzdz t t d|t jf|df}|Sy)aQ Represents the Heaviside function in the form of sign function. Explanation =========== The value of Heaviside(0) must be 1/2 for rewriting as sign to be strictly equivalent. For easier usage, we also allow this rewriting when Heaviside(0) is undefined. Examples ======== >>> from sympy import Heaviside, Symbol, sign, nan >>> x = Symbol('x', real=True) >>> y = Symbol('y') >>> Heaviside(x).rewrite(sign) sign(x)/2 + 1/2 >>> Heaviside(x, 0).rewrite(sign) Piecewise((sign(x)/2 + 1/2, Ne(x, 0)), (0, True)) >>> Heaviside(x, nan).rewrite(sign) Piecewise((sign(x)/2 + 1/2, Ne(x, 0)), (nan, True)) >>> Heaviside(x - 2).rewrite(sign) sign(x - 2)/2 + 1/2 >>> Heaviside(x**2 - 2*x + 1).rewrite(sign) sign(x**2 - 2*x + 1)/2 + 1/2 >>> Heaviside(y).rewrite(sign) Heaviside(y) >>> Heaviside(y**2 - 2*y + 1).rewrite(sign) Heaviside(y**2 - 2*y + 1) See Also ======== sign rr)rhTN)is_extended_realr r r rarrre)rr.rhrMpw1pw2s r_eval_rewrite_as_signzHeaviside._eval_rewrite_as_signLsZ   s)a-"BsAJ/1$d+-Cs)a-"Byr':AFF$CDd CJ rc ddlm}ddlm}|t dk(r |dddS|j }t |dk(r$|j}|||||ddSttd)zi Returns the Heaviside expression written in the form of Singularity Functions. rrPrRrzu rewrite(SingularityFunction) does not support arguments with more that one variable.) rVrQrWrSrar5rr6r7r)rrrhrMrQrSr>rIs rrXz.Heaviside._eval_rewrite_as_SingularityFunctions (U 9Q< &q!Q/ /   t9>A&q%a.*;Q? ?J(BCD DrrY)N)rZr[r\r]r;rrrergpropertyrlr^r1rNrwrX __classcell__)rjs@rrarasz5nG5>VV@ &&=E=E~D:-.FF4l=>FFDrraN) sympy.corerrsympy.core.functionrrsympy.core.logicrsympy.core.relationalrr $sympy.functions.elementary.complexesr r $sympy.functions.elementary.piecewiser sympy.polys.polyerrorsr sympy.polys.polyrootsrsympy.utilities.miscrrrar_rrrs?C&(9:2'+p<p