
    \hn                      S r SSKJr  SSKJr  SSKJr  SSKrSSKJ	r	  SSK
JrJr  SS	KJr  SS
KJrJr  SSKJr  SSKJr  SSKJrJr  SSKJrJrJrJr  SSKJr  SSK J!r!J"r"J#r#  SSK$J%r%  SSK&J'r'  SSK(J)r)  SSK*J+r+  SSK,J,r,J-r-  SSK.J/r/J0r0  SSK1J2r2J3r3J4r4  SSK5J6r6J7r7J8r8J9r9J:r:J;r;  SSK<J=r=  SSK>J?r?J@r@JArA  SSKBrBSSKCJDrD  SSKErESSKFJGrG  S rH " S S\I5      rJ " S  S!\K5      rL " S" S#\M5      rN " S$ S%\M5      rOS& rP " S' S(\Q5      rR " S) S*\\RS+9rS " S, S-\S\5      rT " S. S/\T5      rU " S0 S1\T5      rV " S2 S35      rW\W" 5       rX " S4 S5\R5      rYS6 rZS7 r[\R                  " \Y\Z5         " S8 S9\T\5      r] " S: S;\5      r^S< r_ " S= S>\5      r` " S? S@\5      raSA rb  SOSB jrcSPSC jrdSQSD jreSQSE jrfSRSF jrgSQSG jrhSQSH jriSQSI jrjSSSJ jrkSQSK jrlSPSL jrmSTSM jrnSSNKoJprpJqrq  g)Ua  
There are three types of functions implemented in SymPy:

    1) defined functions (in the sense that they can be evaluated) like
       exp or sin; they have a name and a body:
           f = exp
    2) undefined function which have a name but no body. Undefined
       functions can be defined using a Function class as follows:
           f = Function('f')
       (the result will be a Function instance)
    3) anonymous function (or lambda function) which have a body (defined
       with dummy variables) but have no name:
           f = Lambda(x, exp(x)*x)
           f = Lambda((x, y), exp(x)*y)
    The fourth type of functions are composites, like (sin + cos)(x); these work in
    SymPy core, but are not yet part of SymPy.

    Examples
    ========

    >>> import sympy
    >>> f = sympy.Function("f")
    >>> from sympy.abc import x
    >>> f(x)
    f(x)
    >>> print(sympy.srepr(f(x).func))
    Function('f')
    >>> f(x).args
    (x,)

    )annotations)Any)IterableN   )Add)Basic_atomic)cacheit)TupleDict)
_sympifyit)pure_complex)Expr
AtomicExpr)	fuzzy_andfuzzy_or	fuzzy_not	FuzzyBool)Mul)RationalFloatInteger)	LatticeOp)global_parameters)	Transform)S)sympify_sympify)default_sort_keyordered)sympy_deprecation_warningSymPyDeprecationWarningignore_warnings)has_dupssiftiterableis_sequenceuniqtopological_sort)MPMATH_TRANSLATIONS)as_int
filldedent	func_name)prec_to_dps)Counterc                    U R                   (       a  U R                  S   n U R                  (       a  U R                  S   n U R                  =(       a    U R                  $ )a  Return True if the leading Number is negative.

Examples
========

>>> from sympy.core.function import _coeff_isneg
>>> from sympy import S, Symbol, oo, pi
>>> _coeff_isneg(-3*pi)
True
>>> _coeff_isneg(S(3))
False
>>> _coeff_isneg(-oo)
True
>>> _coeff_isneg(Symbol('n', negative=True)) # coeff is 1
False

For matrix expressions:

>>> from sympy import MatrixSymbol, sqrt
>>> A = MatrixSymbol("A", 3, 3)
>>> _coeff_isneg(-sqrt(2)*A)
True
>>> _coeff_isneg(sqrt(2)*A)
False
r   )	is_MatMulargsis_Mul	is_Numberis_extended_negative)as    K/var/www/auris/envauris/lib/python3.13/site-packages/sympy/core/function.py_coeff_isnegr8   E   sA    6 	{{FF1IxxFF1I;;11111    c                      \ rS rSrSrg)	PoleErrorg    N)__name__
__module____qualname____firstlineno____static_attributes__r=   r9   r7   r;   r;   g   s    r9   r;   c                      \ rS rSrS rSrg)ArgumentIndexErrork   c                J    SU R                   S   < SU R                   S   < 3$ )Nz'Invalid operation with argument number r   z for Function r   )r2   selfs    r7   __str__ArgumentIndexError.__str__l   s!    		!diil, 	-r9   r=   N)r>   r?   r@   rA   rI   rB   r=   r9   r7   rD   rD   k   s    -r9   rD   c                      \ rS rSrSrSrg)BadSignatureErrorq   z9Raised when a Lambda is created with an invalid signaturer=   Nr>   r?   r@   rA   __doc__rB   r=   r9   r7   rL   rL   q   s    Cr9   rL   c                      \ rS rSrSrSrg)BadArgumentsErrorv   zDRaised when a Lambda is called with an incorrect number of argumentsr=   NrN   r=   r9   r7   rQ   rQ   v   s    Nr9   rQ   c           	        [        U SU 5      n[        R                  " U5      R                  R	                  5       nU VVs/ s H#  u  p4UR
                  UR                  :X  d  M!  UPM%     snn(       a  gU VVs/ s H#  u  p4UR
                  UR                  :X  d  M!  UPM%     nnn[        [        [        US SS95      u  pgU(       d  U$ [        [        XfU-   S-   5      5      $ s  snnf s  snnf )ae  Return the arity of the function if it is known, else None.

Explanation
===========

When default values are specified for some arguments, they are
optional and the arity is reported as a tuple of possible values.

Examples
========

>>> from sympy import arity, log
>>> arity(lambda x: x)
1
>>> arity(log)
(1, 2)
>>> arity(lambda *x: sum(x)) is None
True
evalNc                4    U R                   U R                  :H  $ N)defaultempty)ps    r7   <lambda>arity.<locals>.<lambda>   s    agg%r9   T)binaryr   )getattrinspect	signature
parametersitemskindVAR_POSITIONALPOSITIONAL_OR_KEYWORDmaplenr%   tuplerange)clseval_r`   _rY   p_or_knoyess           r7   arityro   |   s    ( C%E""5)44::<J ?jdaAFFa.>.>$>j??&LJDA!&&A4K4K*KaJFL#tF%d4 5GB2<eE"3hl$;<< @Ls    C&%C&: C,C,c                      \ rS rSrSr\R                  rS r\	S 5       r
\	S 5       r\	S 5       r\	S 5       rSS jrS	 rS
rg)FunctionClass   z
Base class for function classes. FunctionClass is a subclass of type.

Use Function('<function name>' [ , signature ]) to create
undefined function classes.
c           	     J   UR                  SU R                  R                  S[        U 5      5      5      nUcB  SU R                  ;  a2  U R                   H"  n[        US5      (       a  UR                  n  OM$     [        U5      (       aE  U(       d   [        [        S[        U5      -  5      5      e[        [        [        U5      5      5      nOUb  [        U5      4nX0l        [        U5      S:X  a0  US   nSU;   a$  [!        US   ["        5      (       d  [%        S5      eg g g )Nnargs_nargsa  
                    Incorrectly specified nargs as %s:
                    if there are no arguments, it should be
                    `nargs = 0`;
                    if there are any number of arguments,
                    it should be
                    `nargs = None`      rT   zPeval on Function subclasses should be a class method (defined with @classmethod))pop__dict__getro   __mro__hasattrru   r'   
ValueErrorr,   strrg   r    setr+   rf   
isinstanceclassmethod	TypeError)ri   r2   kwargsrt   supcls	namespaces         r7   __init__FunctionClass.__init__   s    

7CLL$4$4WeCj$IJ=WCLL8++68,,"MME & u  -& ),E
-3 "4 5 5 '#e*-.EE]$E

 t9>QI":i6G+U+U rss ,V" r9   c                T     SSK Jn  U" U R                  5      $ ! [         a     gf = f)zX
Allow Python 3's inspect.signature to give a useful signature for
Function subclasses.
r   )r_   N)r^   r_   ImportErrorrT   )rH   r_   s     r7   __signature__FunctionClass.__signature__   s/    	)
 ##	  		s    
''c                    [        5       $ rV   )r   rG   s    r7   free_symbolsFunctionClass.free_symbols   s	    ur9   c                   ^  U 4S j$ )Nc                (   > U R                  TT5      $ rV   )rz   )rulerk   rH   s     r7   rZ   (FunctionClass.xreplace.<locals>.<lambda>   s    $!5r9   r=   rG   s   `r7   xreplaceFunctionClass.xreplace   s    
 65r9   c                n    SSK Jn  U R                  (       a  U" U R                  6 $ [        R                  $ )a  Return a set of the allowed number of arguments for the function.

Examples
========

>>> from sympy import Function
>>> f = Function('f')

If the function can take any number of arguments, the set of whole
numbers is returned:

>>> Function('f').nargs
Naturals0

If the function was initialized to accept one or more arguments, a
corresponding set will be returned:

>>> Function('f', nargs=1).nargs
{1}
>>> Function('f', nargs=(2, 1)).nargs
{1, 2}

The undefined function, after application, also has the nargs
attribute; the actual number of arguments is always available by
checking the ``args`` attribute:

>>> f = Function('f')
>>> f(1).nargs
Naturals0
>>> len(f(1).args)
1
r   	FiniteSet)sympy.sets.setsr   ru   r   	Naturals0rH   r   s     r7   rt   FunctionClass.nargs   s(    D 	. +/++y$++&F1;;Fr9   c                    U R                   (       a  XR                   ;   $ U R                  nU[        R                  L =(       d    X;   $ )zReturn True if the specified integer is a valid number of arguments

The number of arguments n is guaranteed to be an integer and positive

)ru   rt   r   r   )rH   nrt   s      r7   _valid_nargsFunctionClass._valid_nargs	  s7     ;;##

#1qz1r9   c                    U R                   $ rV   )r>   ri   s    r7   __repr__FunctionClass.__repr__  s    ||r9   r=   N)r   intreturnbool)r>   r?   r@   rA   rO   type__new___newr   propertyr   r   r   rt   r   r   rB   r=   r9   r7   rq   rq      sv     <<D!tF $ $   6 6 $G $GL
2r9   rq   c                  `   ^  \ rS rSrSrSr\U 4S j5       r\S 5       r	\
S 5       rS rSrU =r$ )	Applicationi  z
Base class for applied functions.

Explanation
===========

Instances of Application represent the result of applying an application of
any type to any object.
Tc                L  > SSK Jn  SSKJn  [	        [        [        U5      5      nUR                  S[        R                  5      nUR                  SS 5        U(       a  [        SU-  5      eU(       a  U R                  " U6 nUb  U$ [        TU ]4  " U /UQ70 UD6n[        5       n[        USU5      n	XLaA  [!        U	5      (       a  [#        [%        ['        U	5      5      5      n
OU	b  [)        U	5      4n
OS n
OUR*                  n
U
(       a  U" U
6 Ul        U$ U" 5       Ul        U$ )Nr   r   r   evaluatert   zUnknown options: %s)sympy.sets.fancysetsr   r   r   listre   r   rx   r   r   r}   rT   superr   objectr]   r'   rg   r    r   r+   ru   rt   )ri   r2   optionsr   r   r   	evaluatedobjsentinelobjnargsrt   	__class__s              r7   r   Application.__new__&  s   2-C&';;z+<+E+EF 	GT"2W<==$I$  goc4D4G4 832# 8$$gc(m45%)+
 JJE).Iu%	
 5>K	
r9   c                    g)a  
Returns a canonical form of cls applied to arguments args.

Explanation
===========

The ``eval()`` method is called when the class ``cls`` is about to be
instantiated and it should return either some simplified instance
(possible of some other class), or if the class ``cls`` should be
unmodified, return None.

Examples of ``eval()`` for the function "sign"

.. code-block:: python

    @classmethod
    def eval(cls, arg):
        if arg is S.NaN:
            return S.NaN
        if arg.is_zero: return S.Zero
        if arg.is_positive: return S.One
        if arg.is_negative: return S.NegativeOne
        if isinstance(arg, Mul):
            coeff, terms = arg.as_coeff_Mul(rational=True)
            if coeff is not S.One:
                return cls(coeff) * cls(terms)

Nr=   )ri   r2   s     r7   rT   Application.evalS  s    < 	r9   c                    U R                   $ rV   r   rG   s    r7   funcApplication.funcs  s    ~~r9   c           	     \   UR                   (       a  UR                   (       a  [        U5      (       as  [        U5      (       ab  XR                  :X  aR  [        U R                  5      UR
                  ;   a.  U" U R                   Vs/ s H  o3R                  X5      PM     sn6 $ g g g g g g s  snf rV   )is_Functioncallabler   rf   r2   rt   _subs)rH   oldnewis       r7   
_eval_subsApplication._eval_subsw  s}    OOSMMhsmm99TYY399!<DII>Iq*I>?? "= ,M !0O ?s   B)r=   )r>   r?   r@   rA   rO   r   r
   r   r   rT   r   r   r   rB   __classcell__r   s   @r7   r   r     sV     K* *X  >  @ @r9   r   )	metaclassc                     ^  \ rS rSr% Sr\S 5       r\SS j5       r\	SU 4S jj5       r
\	S 5       r\	S 5       rS rS	 rS
 rS rSrS\S'   \	S 5       rS rSS jrSS jrS rSrU =r$ )Functioni~  a  
Base class for applied mathematical functions.

It also serves as a constructor for undefined function classes.

See the :ref:`custom-functions` guide for details on how to subclass
``Function`` and what methods can be defined.

Examples
========

**Undefined Functions**

To create an undefined function, pass a string of the function name to
``Function``.

>>> from sympy import Function, Symbol
>>> x = Symbol('x')
>>> f = Function('f')
>>> g = Function('g')(x)
>>> f
f
>>> f(x)
f(x)
>>> g
g(x)
>>> f(x).diff(x)
Derivative(f(x), x)
>>> g.diff(x)
Derivative(g(x), x)

Assumptions can be passed to ``Function`` the same as with a
:class:`~.Symbol`. Alternatively, you can use a ``Symbol`` with
assumptions for the function name and the function will inherit the name
and assumptions associated with the ``Symbol``:

>>> f_real = Function('f', real=True)
>>> f_real(x).is_real
True
>>> f_real_inherit = Function(Symbol('f', real=True))
>>> f_real_inherit(x).is_real
True

Note that assumptions on a function are unrelated to the assumptions on
the variables it is called on. If you want to add a relationship, subclass
``Function`` and define custom assumptions handler methods. See the
:ref:`custom-functions-assumptions` section of the :ref:`custom-functions`
guide for more details.

**Custom Function Subclasses**

The :ref:`custom-functions` guide has several
:ref:`custom-functions-complete-examples` of how to subclass ``Function``
to create a custom function.

c                    gNFr=   rG   s    r7   	_diff_wrtFunction._diff_wrt      r9   c                N    U [         L a  [        U0 UD6$ U R                  " U0 UD6$ rV   )r   UndefinedFunction_new_ri   r2   r   s      r7   r   Function.__new__  s/     (?$d6g6699d.g..r9   c           
       > [        U5      nU R                  U5      (       d]  Sn[        UU [        U R                  5      S:X  a  SOS[	        U R                  5      S[	        U R                  5      S:g  -  US.-  5      eUR                  S[        R                  5      n[        TU ]$  " U /UQ70 UD6nU(       a  [        X`5      (       aq  UR                  (       a`  UR                   Vs/ s H  opR                  U5      PM     nn[	        U5      n	U	S:  a%  [        U5      n
UR                  [        U
5      5      n[!        U5      $ s  snf )	NzE%(name)s takes %(qual)s %(args)s argument%(plural)s (%(given)s given)r   exactlyzat leasts)namequalr2   pluralgivenr   r   )rf   r   r   rt   minrz   r   r   r   r   r   r2   _should_evalfmaxevalfr.   r   )ri   r2   r   r   tempr   resultr6   r   pr2prr   s              r7   r   Function._new_  s   I"":DD%(^q%8	jCIIs399~23$    ;;z+<+E+EF7t7w7
6//FKK;A;;G;a..q1;MGm$CQw'k"o6 Hs   *Ec                    UR                   (       a  UR                  $ UR                  (       d  g[        U5      nUc  g[	        US   R                  US   R                  5      $ )a7  
Decide if the function should automatically evalf().

Explanation
===========

By default (in this implementation), this happens if (and only if) the
ARG is a floating point number (including complex numbers).
This function is used by __new__.

Returns the precision to evalf to, or -1 if it should not evalf.
r   r   )is_Float_precis_Addr   r   )ri   argms      r7   r   Function._should_evalf  sN     <<99zz91Q4::qtzz**r9   c                    SSK Jn  SSSSSSS	S
SSSSSSS.nU R                  n X#   nSXC4$ ! [         a"    [	        U R
                  U5      (       a  SOSn N/f = f)Nr   r   
                            !   (   )   *   +   )explogsincostancotsinhcoshtanhcoth	conjugatereimr   i'     )r   r   r>   KeyErrorr   rt   )ri   r   funcsr   r   s        r7   	class_keyFunction.class_key  s    2
  ||	AA !z  	A		9555A	As   - )AAc           
       ^ ^
 U 4S jn[        T SS 5      nUc)  U" T R                  R                  5      nT R                  nO	U" 5       u  pEUcJ  [        T SS 5      nUc  g  [	        U" T R                   Vs/ s H  owR                  U5      PM     sn6 U5      $  U Vs/ s H  oR                  US-   5      PM     nnS m
[        U
4S jU 5       5      (       a  [        e [        R                  " U5         U" U6 n	S S S 5        [        R                  " W	U5      $ s  snf ! [        [        4 a     g f = fs  snf ! [         a     g f = f! , (       d  f       NU= f)Nc                   > [        T[        5      (       a  g[        [        U 5      (       d  [        R
                  " U S5      n U c  g[        [        U 5      $ )z$Lookup mpmath function based on nameN)r   AppliedUndefr|   mpmathr*   rz   r]   )fnamerH   s    r7   _get_mpmath_func.Function._eval_evalf.<locals>._get_mpmath_func  sG    $--65))+//t<=65))r9   _eval_mpmath_imp_   c                    SSK JnJn  [        X5      (       a#  U R                  n U S   S:g  =(       a    U S   S:H  $ [        X5      (       aC  U R
                  u  pU S   S:g  =(       a&    U S   S:H  =(       a    US   S:g  =(       a    US   S:H  $ g)Nr   )mpfmpcr   r   F)r  r  r  r   _mpf__mpc_)r   r  r  r   s       r7   bad!Function._eval_evalf.<locals>.bad<  s    + a%%AQ4!82"
2''77DAQ4!8 0"
 0!q0%&rUaZ0 !r9   c              3  4   >#    U  H  nT" U5      v   M     g 7frV   r=   ).0r6   r   s     r7   	<genexpr>'Function._eval_evalf.<locals>.<genexpr>N  s     (4a3q664   )r]   r   r>   r2   r   r   r   r}   
_to_mpmathanyr  workprecr   _from_mpmath)rH   precr  r  r   r2   impr   r   vr   s   `         @r7   _eval_evalfFunction._eval_evalf  sH   
	* t^T:#DII$6$67D99D%JD <$.C{S$))"D)Q774=)"DEtLL	8<=NN4!8,D=!$ (4(((   )
 __T"dA #   D))K #Ez*  >*  		 #"sT    D  5DD  D; D6:%D; 6ED   D32D36D; ;
EE
Ec                &   Sn/ nU R                    HQ  nUS-  nUR                  U5      nUR                  (       a  M,   U R                  U5      nUR                  Xe-  5        MS     [        U6 $ ! [         a    [
        R                  X5      n N@f = f)Nr   r   )r2   diffis_zerofdiffrD   r   appendr   )rH   r   r   lr6   dadfs          r7   _eval_derivativeFunction._eval_derivativeX  s    AFABzz-ZZ] HHRW  Aw & -^^D,-s   A..BBc                :    [        S U R                   5       5      $ )Nc              3  8   #    U  H  oR                   v   M     g 7frV   )is_commutativer#  r6   s     r7   r$  0Function._eval_is_commutative.<locals>.<genexpr>i  s     =9a))9s   )r   r2   rG   s    r7   _eval_is_commutativeFunction._eval_is_commutativeh  s    =499===r9   c                ,  ^ U R                   (       d  g[        U4S jU R                   SS   5       5      (       a  gU R                   S   nUR                  TU5      (       d  g [        [	        U 5      R                  UR                  TU5      5      5      $ )NTc              3  D   >#    U  H  oR                  T5      v   M     g 7frV   has)r#  r   xs     r7   r$  0Function._eval_is_meromorphic.<locals>.<genexpr>n  s     3]cwwqzz]    r   Fr   )r2   r(  _eval_is_meromorphicr   r   is_singularsubs)rH   rE  r6   r   s    `  r7   rH  Function._eval_is_meromorphick  sr    yy3TYYqr]333iil''1--d//A?@@r9   NzFuzzyBool | tuple[Expr, ...]_singularitiesc                V   ^ U R                   nUS;   a  U$ [        U4S jU 5       5      $ )zp
Tests whether the argument is an essential singularity
or a branch point, or the functions is non-holomorphic.
)TNFc              3     >#    U  H3  nU[         R                  L a  TR                  OTU-
  R                  v   M5     g 7frV   )r   ComplexInfinityis_infiniter2  )r#  r   r6   s     r7   r$  'Function.is_singular.<locals>.<genexpr>  s9      :68 *+a.?.?)?!e__-68s   ;>)rL  r   )ri   r6   sss    ` r7   rI  Function.is_singulary  s8     $$I :68: : 	:r9   c           	     N    [        [        S[        U 5      < SU< S35      5      e)z
Compute an asymptotic expansion around args0, in terms of self.args.
This function is only used internally by _eval_nseries and should not
be called directly; derived classes can overwrite this to implement
asymptotic expansions.
z%
            Asymptotic expansion of z around z  is
            not implemented.)r;   r,   r   )rH   r   args0rE  logxs        r7   _eval_aseriesFunction._eval_aseries  s&     
#':u$6 7 8 	8r9   c                J
  ^&^'^( SSK Jn  SSKJn  SSKJn  U R                  nU V	s/ s H  oR                  US5      PM     n
n	[        S U
 5       5      (       Ga  SSK	J
m'Jm(Jm&  U V	s/ s H  oR                  XS9PM     nn	U V	s/ s H  oR                  US5      PM     nn	[        U&U'U(4S	 jU 5       5      (       a  U R                  X*X5      $ U V	s/ s H  oR                  XU5      PM     nn	[!        X5       VVs/ s H	  u  pX-
  PM     nnnU Vs/ s H  n[#        5       PM     nn/ nS
n[!        XU5       HO  u  nnnUR%                  U5      (       a!  Ub  [&        eUR)                  UU-   5        UnM>  UR)                  U5        MQ     U R*                  " U6 nUc  U$ UR                  UX#5      nUR-                  5       nUR/                  5       nUR1                  UW5      R3                  5       U" UR4                  R1                  UU5      U5      -   nU$ U R*                  R6                  [8        R:                  L dV  U R*                  R6                  U" S5      :X  a
  U
S   (       d,  [        S U R*                  R6                   5       5      (       Ga  U nUR3                  5       nUU:X  Ga  [=        UR                  5      S:X  a,  UR+                  UR                  S   R?                  5       5      nUR1                  U[8        R@                  5      nURB                  SL d  U[8        RD                  L a  [G        SU -  5      eUn[8        RH                  nU" SU 5      nUR1                  UU5      n[K        SU5       H  nU[M        U5      -  nURO                  U5      nUR1                  U[8        R@                  5      n U [8        RD                  L a   UR                  U[8        R@                  5      n U RB                  SL a  [G        SU -  5      eU UU-  -  U-  nUR3                  5       nUU-  nM     UU" X-  U5      -   $ URQ                  XUS9$ U R                  S   n!/ n"S
n#US-   n$U" U!R                  U5      U5      RS                  5       n%U%S:w  a  UU%-  RU                  5       n$[K        U$5       H7  nU RW                  UU!U#5      n#U#RQ                  XUS9n#U"R)                  U#5        M9     [Y        U"6 U" X-  U5      -   $ s  sn	f s  sn	f s  sn	f s  sn	f s  snnf s  snf )a  
This function does compute series for multivariate functions,
but the expansion is always in terms of *one* variable.

Examples
========

>>> from sympy import atan2
>>> from sympy.abc import x, y
>>> atan2(x, y).series(x, n=2)
atan2(0, y) + x/y + O(x**2)
>>> atan2(x, y).series(y, n=2)
-y/x + atan2(x, 0) + O(y**2)

This function also computes asymptotic expansions, if necessary
and possible:

>>> from sympy import loggamma
>>> loggamma(1/x)._eval_nseries(x,0,None)
-1/x - log(x)/x + log(x)/2 + O(1)

r   )uniquely_named_symbolr   Orderr   c              3  <   #    U  H  oR                   S L v   M     g7f)FN)	is_finite)r#  ts     r7   r$  )Function._eval_nseries.<locals>.<genexpr>  s     3U{{e#Us   )oozoonanrV  c              3  L   >#    U  H  oR                  TT* TT5      v   M     g 7frV   rC  )r#  r_  rc  ra  rb  s     r7   r$  r`    s#     8R55bS#s++Rs   !$Nc              3  *   #    U  H	  oS :  v   M     g7f)r   Nr=   )r#  cs     r7   r$  r`    s     6o1uos   FzCannot expand %s around 0xir   rV  rw   )-symbolrZ  sympy.series.orderr\  r   r   r2   limitr(  numbersra  rb  rc  as_leading_termrW  _eval_nserieszipDummyrD  NotImplementedErrorr4  r   getOremoveOrJ  expandexprrt   r   r   rf   cancelZeror^  NaNr;   Onerh   r   r1  nseriesgetnceilingtaylor_termr   ))rH   rE  r   rV  cdirrZ  r\  r   r2   r_  rU  r6   a0rr0zrk   rY   qr-  aizipie1r   oetermseriesfact_xr   rJ  r   r5  gntermscfrc  ra  rb  s)                                         @@@r7   ro  Function._eval_nseries  sQ   . 	2,-yy(,-1A-3U333--:>?$Q""1"0$A?)*+A''!Q-B+8R888))!A<< 7;;dt,dA;'*1z2zGQzA2"#$!Q!A$AA!"m
B66!99}11HHR"W%AHHRL , ABy	  A,AA		Aq"$$&qvv{{1b/A1)EEAHIIOOq{{*IIOOy|3a6diioo666ABBw qvv;!#qvvay//12Avva(>>U*daeem#$?4$HIIuu*46FF1bMq!AHQK'Dr
A66"aff-Dquu} wwr1662~~.'(Ct(LMMA;t+D;;=DdNF % adA..::a4:00iilQ3&&q)1-2247d^^%FvA  C+A		!t	,AHHQK  AwqtQ''_ . @+ <2$s#   T)TTTTT c                P   SUs=::  a  [        U R                  5      ::  d  O  [        X5      eUS-
  nU R                  U   nUR                  (       av  [        U R                  5      S:X  d  UR                  (       d  [        X5      $ [        U R                  5       H  u  pEXB:w  d  M  X5R                  ;   d  M    O   [        X5      $ [        SU-  [        U5      S9nU R                  SU U4-   U R                  US-   S -   n[        [        U R                  " U6 U5      Xc5      $ )z/
Returns the first derivative of the function.
r   xi_%i)dummy_indexN)rf   r2   rD   r   	is_Symbol_derivative_dispatch	enumerater   rq  hashSubs
Derivativer   )rH   argindexixAr   r-  Dr2   s           r7   r3  Function.fdiff  s     X/TYY/$T44\IIbM;;499~"!+++D44!$)),7qNN2 	 - ,D44 'H$$q':yy"~$tyya'99Jtyy$/3Q::r9   c                   ^^ SSK Jn  U R                   Vs/ s H  oUR                  TUS9PM     nnU" ST5      m[	        UU4S jU 5       5      (       a  [        SU R                  -  5      eU $ s  snf )zStub that should be overridden by new Functions to return
the first non-zero term in a series if ever an x-dependent
argument whose leading term vanishes as x -> 0 might be encountered.
See, for example, cos._eval_as_leading_term.
r   r[  rd  r   c              3  r   >#    U  H,  nTUR                   ;   =(       a    TR                  U5      v   M.     g 7frV   )r   contains)r#  r6   r  rE  s     r7   r$  1Function._eval_as_leading_term.<locals>.<genexpr>  s*     CdqANN"4qzz!}4ds   47z'%s has no _eval_as_leading_term routine)rk  r\  r2   rn  r(  rr  r   )rH   rE  rV  r  r\  r6   r2   r  s    `     @r7   _eval_as_leading_termFunction._eval_as_leading_term  st     	-9=CA!!!$!/C!QKCdCCC &9DIIEG G K' Ds   A0r=   r   ztype[AppliedUndef]r   r   r   )r   )r>   r?   r@   rA   rO   r   r   r
   r   r   r   r   r  r.  r8  r?  rH  rL  __annotations__rI  rW  ro  r3  r  rB   r   r   s   @r7   r   r   ~  s    7r   / /    : + +.  6@*D >
A 48N07
: 
:	8j(X;0 r9   r   c                  ,    \ rS rSrSr\SS j5       rSrg)DefinedFunctioni2  z;Base class for defined functions like ``sin``, ``cos``, ...c                &    U R                   " U0 UD6$ rV   )r   r   s      r7   r   DefinedFunction.__new__5  s    yy$*'**r9   r=   Nr  )r>   r?   r@   rA   rO   r
   r   rB   r=   r9   r7   r  r  2  s    E+ +r9   r  c                  V   ^  \ rS rSr% SrSrS\S'   S
U 4S jjrS r\	S 5       r
S	rU =r$ )r  i:  zU
Base class for expressions resulting from the application of an undefined
function.
Fr~   r   c                B  > [        [        [        U5      5      nU Vs/ s H&  n[        U[        5      (       d  M  UR
                  PM(     nnU(       a1  [        SS[        U5      S:  -  < SSR                  U5      < 35      e[        TU ](  " U /UQ70 UD6nU$ s  snf )Nz@Invalid argument: expecting an expression, not UndefinedFunctionr   r   z: , )rg   re   r   r   r   r   r   rf   joinr   r   )ri   r2   r   r6   ur   r   s         r7   r   AppliedUndef.__new__D  s    S$'(!FTZ3D%EVQVVTFSVaZ $))A,0 1 1GOC:$:':
 Gs
   BBc                    U $ rV   r=   )rH   rE  rV  r  s       r7   r  "AppliedUndef._eval_as_leading_termM  s    r9   c                    g)z
Allow derivatives wrt to undefined functions.

Examples
========

>>> from sympy import Function, Symbol
>>> f = Function('f')
>>> x = Symbol('x')
>>> f(x)._diff_wrt
True
>>> f(x).diff(x)
Derivative(f(x), x)
Tr=   rG   s    r7   r   AppliedUndef._diff_wrtP  s      r9   r=   r  )r>   r?   r@   rA   rO   	is_numberr  r   r  r   r   rB   r   r   s   @r7   r  r  :  s2    
 I
I  r9   r  c                      \ rS rSrSrS rSrg)UndefSageHelperic  z'
Helper to facilitate Sage conversion.
c                   ^^^^ SS K Jm  Tc  UU4S j$ TR                   Vs/ s H  o3R                  5       PM     snmUUU4S j$ s  snf )Nr   c                 :   > T R                  TR                  5      $ rV   )functionr>   )sagetyps   r7   rZ   )UndefSageHelper.__get__.<locals>.<lambda>j  s    4==6r9   c                 T   > TR                  TR                  R                  5      " T 6 $ rV   )r  r   r>   )r2   insr  s   r7   rZ   r  m  s    DMM#--*@*@A4Hr9   )sage.allallr2   _sage_)rH   r  r  r   r2   r  s    `` @@r7   __get__UndefSageHelper.__get__g  s8    ;66,/HH5HSJJLH5DHH 6s   Ar=   N)r>   r?   r@   rA   rO   r  rB   r=   r9   r7   r  r  c  s    Ir9   r  c                     ^  \ rS rSr% SrS\S'   S\S'   \4S4SU 4S jjjrS	 r0 r	S
\S'   S r
S rS r\S 5       rSrU =r$ )r   ir  z)
The (meta)class of undefined functions.
r~   r   r  r  Nc                  > SSK Jn  U" U5      u  pd[        U[        5      (       a  UR	                  U5      nUR
                  nO\[        U[        5      (       d  [        S5      eUR                  SS 5      n[        U40 UD6R                  nUc  UR                  S5        U=(       d    0 nUR                  UR                  5        VV	s0 s H  u  pSU-  U	_M     sn	n5        UR                  U5        UR                  U5        UR                  SU05        S US'   [        TU ]9  XX#5      n
Xl        [        U
l        U
$ s  sn	nf )Nr   )_filter_assumptionsz#expecting string or Symbol for namecommutativezis_%s_kwargsr?   )rj  r  r   Symbol_merger   r~   r   rz   assumptions0rx   updatera   r   r   _undef_sage_helperr  )mclr   basesry   r   r  assumptionsr  kr-  r   r   s              r7   r   UndefinedFunction.__new__y  s   / 2&9dF##++k2K99DD#&&ABB%//->K 55BBK".>rK4E4E4GH4GDA1a4GHI 	k"F+,!%goc9'

 Is   
E
c                2    U [        U5      R                  ;   $ rV   )r   r{   )ri   instances     r7   __instancecheck__#UndefinedFunction.__instancecheck__  s    d8n,,,,r9   zdict[str, bool | None]r  c                z    [        U R                  5       [        U R                  R	                  5       5      45      $ rV   )r  r  	frozensetr  ra   rG   s    r7   __hash__UndefinedFunction.__hash__  s+    T^^%y1C1C1E'FGHHr9   c                    [        XR                  5      =(       aA    U R                  5       UR                  5       :H  =(       a    U R                  UR                  :H  $ rV   )r   r   r  r  rH   others     r7   __eq__UndefinedFunction.__eq__  sC    5..1 *NN 11*LLEMM)	+r9   c                    X:X  + $ rV   r=   r  s     r7   __ne__UndefinedFunction.__ne__  s      r9   c                    gr   r=   rG   s    r7   r   UndefinedFunction._diff_wrt  r   r9   r=   r  )r>   r?   r@   rA   rO   r  r  r   r  r  r  r  r  r   r   rB   r   r   s   @r7   r   r   r  s]     I".4  B- ')G#(I+
!  r9   r   c                >    [         U R                  U R                  44$ rV   )_rebuild_undefr   r  )fs    r7   _reduce_undefr    s    QVVQYY/00r9   c                    [        U 40 UD6$ rV   )r   )r   r   s     r7   r  r    s    D#F##r9   c                  B    \ rS rSr% Sr\" 5       rS\S'   S rS	S jr	Sr
g)
WildFunctioni  a  
A WildFunction function matches any function (with its arguments).

Examples
========

>>> from sympy import WildFunction, Function, cos
>>> from sympy.abc import x, y
>>> F = WildFunction('F')
>>> f = Function('f')
>>> F.nargs
Naturals0
>>> x.match(F)
>>> F.match(F)
{F_: F_}
>>> f(x).match(F)
{F_: f(x)}
>>> cos(x).match(F)
{F_: cos(x)}
>>> f(x, y).match(F)
{F_: f(x, y)}

To match functions with a given number of arguments, set ``nargs`` to the
desired value at instantiation:

>>> F = WildFunction('F', nargs=2)
>>> F.nargs
{2}
>>> f(x).match(F)
>>> f(x, y).match(F)
{F_: f(x, y)}

To match functions with a range of arguments, set ``nargs`` to a tuple
containing the desired number of arguments, e.g. if ``nargs = (1, 2)``
then functions with 1 or 2 arguments will be matched.

>>> F = WildFunction('F', nargs=(1, 2))
>>> F.nargs
{1, 2}
>>> f(x).match(F)
{F_: f(x)}
>>> f(x, y).match(F)
{F_: f(x, y)}
>>> f(x, y, 1).match(F)

zset[Any]includec                   SSK JnJn  Xl        UR	                  S[
        R                  5      n[        XS5      (       dB  [        U5      (       a  [        [        [        U5      5      5      nOUb  [        U5      4nU" U6 nXPl        g )Nr   )Setr   rt   )r   r  r   r   rx   r   r   r   r'   rg   r    r   r+   rt   )ri   r   r  r  r   rt   s         r7   r   WildFunction.__init__  sh    25%%%5!!gc%j12"(u%E	r9   Nc                    [        U[        [        45      (       d  g [        UR                  5      U R
                  ;  a  g Uc  0 nOUR                  5       nXU '   U$ rV   )r   r  r   rf   r2   rt   copy)rH   rv  	repl_dictr   s       r7   matchesWildFunction.matches  sR    $x 899tyy>+I!(I$r9   r=   r   )r>   r?   r@   rA   rO   r   r  r  r   r  rB   r=   r9   r7   r  r    s     -` GXr9   r  c                  <   \ rS rSrSrSr\S 5       rS r\S 5       r	\
S 5       rS rS	 rS
 r\" S\5      S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       rS rSS jrSS jrS rSS jr\
S 5       r\
S 5       rSr g)r  i  a@  
Carries out differentiation of the given expression with respect to symbols.

Examples
========

>>> from sympy import Derivative, Function, symbols, Subs
>>> from sympy.abc import x, y
>>> f, g = symbols('f g', cls=Function)

>>> Derivative(x**2, x, evaluate=True)
2*x

Denesting of derivatives retains the ordering of variables:

    >>> Derivative(Derivative(f(x, y), y), x)
    Derivative(f(x, y), y, x)

Contiguously identical symbols are merged into a tuple giving
the symbol and the count:

    >>> Derivative(f(x), x, x, y, x)
    Derivative(f(x), (x, 2), y, x)

If the derivative cannot be performed, and evaluate is True, the
order of the variables of differentiation will be made canonical:

    >>> Derivative(f(x, y), y, x, evaluate=True)
    Derivative(f(x, y), x, y)

Derivatives with respect to undefined functions can be calculated:

    >>> Derivative(f(x)**2, f(x), evaluate=True)
    2*f(x)

Such derivatives will show up when the chain rule is used to
evaluate a derivative:

    >>> f(g(x)).diff(x)
    Derivative(f(g(x)), g(x))*Derivative(g(x), x)

Substitution is used to represent derivatives of functions with
arguments that are not symbols or functions:

    >>> f(2*x + 3).diff(x) == 2*Subs(f(y).diff(y), y, 2*x + 3)
    True

Notes
=====

Simplification of high-order derivatives:

Because there can be a significant amount of simplification that can be
done when multiple differentiations are performed, results will be
automatically simplified in a fairly conservative fashion unless the
keyword ``simplify`` is set to False.

    >>> from sympy import sqrt, diff, Function, symbols
    >>> from sympy.abc import x, y, z
    >>> f, g = symbols('f,g', cls=Function)

    >>> e = sqrt((x + 1)**2 + x)
    >>> diff(e, (x, 5), simplify=False).count_ops()
    136
    >>> diff(e, (x, 5)).count_ops()
    30

Ordering of variables:

If evaluate is set to True and the expression cannot be evaluated, the
list of differentiation symbols will be sorted, that is, the expression is
assumed to have continuous derivatives up to the order asked.

Derivative wrt non-Symbols:

For the most part, one may not differentiate wrt non-symbols.
For example, we do not allow differentiation wrt `x*y` because
there are multiple ways of structurally defining where x*y appears
in an expression: a very strict definition would make
(x*y*z).diff(x*y) == 0. Derivatives wrt defined functions (like
cos(x)) are not allowed, either:

    >>> (x*y*z).diff(x*y)
    Traceback (most recent call last):
    ...
    ValueError: Can't calculate derivative wrt x*y.

To make it easier to work with variational calculus, however,
derivatives wrt AppliedUndef and Derivatives are allowed.
For example, in the Euler-Lagrange method one may write
F(t, u, v) where u = f(t) and v = f'(t). These variables can be
written explicitly as functions of time::

    >>> from sympy.abc import t
    >>> F = Function('F')
    >>> U = f(t)
    >>> V = U.diff(t)

The derivative wrt f(t) can be obtained directly:

    >>> direct = F(t, U, V).diff(U)

When differentiation wrt a non-Symbol is attempted, the non-Symbol
is temporarily converted to a Symbol while the differentiation
is performed and the same answer is obtained:

    >>> indirect = F(t, U, V).subs(U, x).diff(x).subs(x, U)
    >>> assert direct == indirect

The implication of this non-symbol replacement is that all
functions are treated as independent of other functions and the
symbols are independent of the functions that contain them::

    >>> x.diff(f(x))
    0
    >>> g(x).diff(f(x))
    0

It also means that derivatives are assumed to depend only
on the variables of differentiation, not on anything contained
within the expression being differentiated::

    >>> F = f(x)
    >>> Fx = F.diff(x)
    >>> Fx.diff(F)  # derivative depends on x, not F
    0
    >>> Fxx = Fx.diff(x)
    >>> Fxx.diff(Fx)  # derivative depends on x, not Fx
    0

The last example can be made explicit by showing the replacement
of Fx in Fxx with y:

    >>> Fxx.subs(Fx, y)
    Derivative(y, x)

    Since that in itself will evaluate to zero, differentiating
    wrt Fx will also be zero:

    >>> _.doit()
    0

Replacing undefined functions with concrete expressions

One must be careful to replace undefined functions with expressions
that contain variables consistent with the function definition and
the variables of differentiation or else insconsistent result will
be obtained. Consider the following example:

>>> eq = f(x)*g(y)
>>> eq.subs(f(x), x*y).diff(x, y).doit()
y*Derivative(g(y), y) + g(y)
>>> eq.diff(x, y).subs(f(x), x*y).doit()
y*Derivative(g(y), y)

The results differ because `f(x)` was replaced with an expression
that involved both variables of differentiation. In the abstract
case, differentiation of `f(x)` by `y` is 0; in the concrete case,
the presence of `y` made that derivative nonvanishing and produced
the extra `g(y)` term.

Defining differentiation for an object

An object must define ._eval_derivative(symbol) method that returns
the differentiation result. This function only needs to consider the
non-trivial case where expr contains symbol and it should call the diff()
method internally (not _eval_derivative); Derivative should be the only
one to call _eval_derivative.

Any class can allow derivatives to be taken with respect to
itself (while indicating its scalar nature). See the
docstring of Expr._diff_wrt.

See Also
========
_sort_variable_count
Tc                x    U R                   R                  =(       a    [        U R                  5       [        5      $ )a  An expression may be differentiated wrt a Derivative if
it is in elementary form.

Examples
========

>>> from sympy import Function, Derivative, cos
>>> from sympy.abc import x
>>> f = Function('f')

>>> Derivative(f(x), x)._diff_wrt
True
>>> Derivative(cos(x), x)._diff_wrt
False
>>> Derivative(x + 1, x)._diff_wrt
False

A Derivative might be an unevaluated form of what will not be
a valid variable of differentiation if evaluated. For example,

>>> Derivative(f(f(x)), x).doit()
Derivative(f(x), x)*Derivative(f(f(x)), f(x))

Such an expression will present the same ambiguities as arise
when dealing with any other product, like ``2*x``, so ``_diff_wrt``
is False:

>>> Derivative(f(f(x)), x)._diff_wrt
False
)rv  r   r   doitr  rG   s    r7   r   Derivative._diff_wrt  s'    @ yy""Jz$))+z'JJr9   c           	        [        U5      n[        U[        5      (       d  [        S[	        U5       35      e[        USS 5      n[        U[        5      nU(       d  [        [        SU-  5      5      eU(       dy  UR                  n[        U5      S:w  a^  UR                  (       a  [        R                  $ [        U5      S:X  a  [        [        SU-  5      5      e[        [        SU-  5      5      e/ n[        [        [         4nSSKJnJn	  [)        U5       GHU  u  p[        U[*        5      (       a  [        S	U-  5      e[        X5      (       aw  [        U5      S:X  a  MJ  [        US   U5      (       a*  [        U5      S:X  a  U" US   5      nSnOUu  pU" U5      nOUu  pUS:X  a  M  UR-                  [!        X5      5        M  [        U5      n[        U[.        5      (       ai  U
S:X  a  [        S
U-  5      eUnUS   u  pUS:w  a  [        SR1                  X4U5      5      eUS:X  a  UR3                  5         GM(  [!        X5      US'   GM9  SnUR-                  [!        X5      5        GMX     / nU H}  nUu  nnUR4                  (       a  [        S5      eU(       aA  US   S   U:X  a5  UUS   S   -  nU(       d  UR3                  5         M[  [!        UU5      US'   Ml  UR-                  U5        M     UnU H5  u  nnUR6                  (       a  M  Sn[        [        SU< SU< 35      5      e   [        U5      S:X  a  U$ UR9                  SS5      nU(       GaZ  [        U[:        5      (       a  UR<                  nU VVs/ s H+  u  nn[        U[:        5      (       a  UR<                  OUU4PM-     nnnSnUR                  nSSKJ n  U H  u  nnUR                  nURB                  (       d  M%  U(       d  M.  [        U[D        5      (       a7  [G        5       nURI                  UU05      RK                  U5      (       d  Sn  OHMz  [        UU5      (       a  Sn  O1[        U[L        5      (       a
  UU;  a  Sn  OUU-  (       a  M  Sn  O   U(       a  U RO                  U5      $ U RQ                  U5      n[        U[:        5      (       a3  [        URR                  5      U-   nURT                  n[W        U/UQ70 UD6$ U(       a  [Y        US5      (       dG  U(       a)  XaS4/:X  a!  URZ                  (       a  [        R\                  $ [^        R`                  " X/UQ76 $ Sn/ nSSK1J2n  [)        U5       GH-  u  n
u  pUnS nURf                  =(       d    [        U[h        [         UU	45      nU(       d  Un[G        S5      nURI                  UU05      n[        U[:        [D        45      (       + nXR                  ;  a  U(       d  URk                  U5      s  $ URZ                  (       d%  [Y        US5      (       d  UURk                  U5      -  nU Rm                  XU5      n U b  U Rn                  (       a  U s  $ UU-  nUb  U b  U Rq                  UU5      n UnU c  XjS  n  OU nGM0     URs                  S S 5      nU(       aP  [        U[:        5      (       a$  [        URR                  5      U-   nURT                  n[^        R`                  " X/UQ76 nUS:  S:X  a1  UR9                  SS5      (       a  SSK:J;n!  SSK<J=n"  U!" U"" U5      5      nU$ s  snnf )NzCannot represent derivative of r   zp
                Since there are no variables in the expression %s,
                it cannot be differentiated.r   r   z
                        Since there are no variables in the expression,
                        the variable(s) of differentiation must be supplied
                        to differentiate %sz
                        Since there is more than one variable in the
                        expression, the variable(s) of differentiation
                        must be supplied to differentiate %s)Array	NDimArrayz.cannot differentiate wrt UndefinedFunction: %sz%First variable cannot be a number: %ir   ztuple {} followed by number {}z,order of differentiation must be nonnegative z4
                    Can't calculate derivative wrt .r   F
MatrixExprTr8  
MatrixBaserh  c                "    [        U [        5      $ rV   )r   r  rE  s    r7   rZ   $Derivative.__new__.<locals>.<lambda>  s    jJ/r9   c                    U R                   $ rV   )	canonicalr  s    r7   rZ   r	    s    akkr9   simplify)factor_terms)signsimp)>r   r   r   r   r   r]   r   r}   r,   r   rf   r  r   rx  rg   r   r   sympy.tensor.arrayr  r   r  r   r4  r   formatrx   is_negativer   rz   r  r  "sympy.matrices.expressions.matexprr  is_positiver  rq  r   rD  r  _get_zero_with_shape_like_sort_variable_countvariable_countrv  r  r|   	is_scalarrz  r   r   sympy.matrices.matrixbaser  	is_symbolr   r1  !_dispatch_eval_derivative_n_timesr2  rJ  replace	exprtoolsr  sympy.simplify.simplifyr  )#ri   rv  	variablesr   symbols_or_nonehas_symbol_setr  array_likesr  r   r   r-  countprev	prevcountmergedr_  rg  __r   zerofreer  vfreer  nderivs	unhandledr  old_exprold_vr  clashingr   r  r  s#                                      r7   r   Derivative.__new__  s   t}$&&=d4j\JKK!$=#OS9Z )026)7 8 9 9
 ))I9~">>66My>Q&$Z 1/ 2616 &7 8 8
 %Z 1@ CG1G &H I I dE*7i(DA!.//,./01 1 !))q6Q;adK001v{!!A$K !#$!!H HAA:%%eAo6
A!W%%6$%Lq%PQQ"0"4>#$D$K$KTL]_`$abbA:"&&().t);N2&%%eAo6O )V ADAq}} BD D&*Q-1,VBZ]"JJL!&q!F2Ja     
 #DAq;;; ?@- "  	 # ~!#K::j%0$
++~~ +,*DAq !+1j 9 9q!D*  , D$$DE&1===UU!!\22 "G#}}aV488;;#'D!  < $Az22$#Av..1D=##e||#'D!+ ', 44T:: !55nEN dJ''!$"5"56GN99D'H~HHH wt-?@@Nayk9dnn uu<<;N;;
 	8&~6MAzHE :z!5*i8(:I $K}}eQZ0 !+5:|2L MN---h99Q<'w103 03
 EJJu--D77GC3;;
uG ?((1e,C{ +2.	D] 7b ||/!# $
++ !4!45	A	yy<<6I6DaKD VZZ
D%A%A/8/D,s   2]c                v    U R                   " U R                  /[        R                  U R                  5      Q76 $ rV   )r   rv  r  r  r  r   s    r7   r  Derivative.canonical  s5    xx B,,S-?-?@B 	Br9   c                R  ^^	^
^^ T(       d  / $ [        T5      m[        T5      S:X  a  [        TS   6 /$ [        [        [        T5      5      5      n/ nU4S jm[	        5       m	SU	U4S jjm[        [        T5      5       HC  n[        U5       H1  nT" T" U5      T" U5      5      (       d  M  UR                  XT45        M3     ME     [        [        [        [        T VVs/ s H  u  pFUPM	     snn5      5      [        [        T5      5      5      5      m
[        X#4U
U4S jS9n/ nU Vs/ s H  nTU   PM
     sn H>  u  mnU(       a  US   S   T:X  a  US   S==   U-  ss'   M+  UR                  TU/5        M@     U Vs/ s H  n[        U6 PM     sn$ s  snnf s  snf s  snf )	a  
Sort (variable, count) pairs into canonical order while
retaining order of variables that do not commute during
differentiation:

* symbols and functions commute with each other
* derivatives commute with each other
* a derivative does not commute with anything it contains
* any other object is not allowed to commute if it has
  free symbols in common with another object

Examples
========

>>> from sympy import Derivative, Function, symbols
>>> vsort = Derivative._sort_variable_count
>>> x, y, z = symbols('x y z')
>>> f, g, h = symbols('f g h', cls=Function)

Contiguous items are collapsed into one pair:

>>> vsort([(x, 1), (x, 1)])
[(x, 2)]
>>> vsort([(y, 1), (f(x), 1), (y, 1), (f(x), 1)])
[(y, 2), (f(x), 2)]

Ordering is canonical.

>>> def vsort0(*v):
...     # docstring helper to
...     # change vi -> (vi, 0), sort, and return vi vals
...     return [i[0] for i in vsort([(i, 0) for i in v])]

>>> vsort0(y, x)
[x, y]
>>> vsort0(g(y), g(x), f(y))
[f(y), g(x), g(y)]

Symbols are sorted as far to the left as possible but never
move to the left of a derivative having the same symbol in
its variables; the same applies to AppliedUndef which are
always sorted after Symbols:

>>> dfx = f(x).diff(x)
>>> assert vsort0(dfx, y) == [y, dfx]
>>> assert vsort0(dfx, x) == [dfx, x]
r   r   c                   > TU    S   $ Nr   r=   )r   vcs    r7   rZ   1Derivative._sort_variable_count.<locals>.<lambda>   s    beAhr9   c                  >^ U T:X  a  U$ U R                   (       a  g[        U [        5      (       a'  [        UU4S jU R                   5       5      (       a  ggU(       d  [        U [
        5      (       a  gTR                   (       a  TU R                  ;   $ [        T[
        5      (       a  T" U R                  TT05      T5      $ U R                  TR                  -  $ )NFc              3  4   >#    U  H  nT" UTS S9v   M     g7f)T)wrtNr=   )r#  r  _blockr-  s     r7   r$  BDerivative._sort_variable_count.<locals>._block.<locals>.<genexpr>-  s!      3!1A a-!1r&  T)r  r   r  r(  _wrt_variablesr  r   r   )dr-  r9  r  r:  s    ` r7   r:  /Derivative._sort_variable_count.<locals>._block"  s    Av
{{!Z((
  3!"!1!13 3 3:a66{{ANN**!\**ajj!Q0!44>>ANN22r9   c                   > TT" U 5         $ rV   r=   )r   Or-  s    r7   rZ   r6  >  s    AadGr9   keyr   F)r   rf   r   rh   rq  r4  dictrp  r    r(   r)   )ri   r5  VEr   jrg  r  r%  r  r@  r:  r-  s    `       @@@@r7   r  Derivative._sort_variable_count  sl   b I"Xr7a<2a5M?"s2w G	3 	3, s2wA1X!A$!%%HHaUO   
 WT"41"456c"gGHqf*;<$&'BqRUB'DAq&*Q-1,r
1"q!f%	 (
 $**6aq	6** #5 (
 +s   !F,FF$c                .    U R                   R                  $ rV   rv  r<  rG   s    r7   r?  Derivative._eval_is_commutativeH      yy'''r9   c                   XR                   ;  a  U R                  R                  U5      n[        U[        5      (       a4  UR
                  " UR                  /U R                  UR                  -   Q76 $ U R
                  " U/U R                  Q7SS06$ [        U R                  5      nUR                  US45        U R
                  " U R                  /UQ7SS06$ )Nr   Tr   F)
r<  rv  r1  r   r  r   r  r  r   r4  )rH   r-  dedvr  s       r7   r8  Derivative._eval_derivativeK  s     '''99>>!$D$
++yyYd.A.ADDWDW.WYY 99TBDNNBTBB d112q!f%yyD^DeDDr9   c                   U R                   nUR                  SS5      (       a  UR                  " S0 UD6nSUS'   U R                  " U/U R                  Q70 UD6nX0:w  a,  UR                  [        5      (       a  UR                  " S0 UD6nU$ )NdeepTr   r=   )rv  rz   r  r   r  rD  r  )rH   hintsrv  rvs       r7   r  Derivative.doit_  s~    yy99VT""99%u%D jYYt;d11;U;9
++''"E"B	r9   z0c                  ^ ^ [        T R                  5      S:w  d  [        T R                  5      S:w  a  [        S5      e[	        T R                  5      S   mU U4S jn[
        R                  " [        R                  " UUR                  [        R                  R                  5      5      [        R                  R                  5      $ )z
Evaluate the derivative at z numerically.

When we can represent derivatives at a point, this should be folded
into the normal evalf. For now, we need a special method.
r   z%partials and higher order derivativesr   c                H  > TR                   R                  T[        R                  " U [        R
                  R                  S95      nUR                  [        [        R
                  R                  5      5      nUR                  [        R
                  R                  5      $ )N)r+  )
rv  rJ  r   r*  r  mpr+  r   r.   r'  )rE  f0rH   r  s     r7   rT   )Derivative.doit_numerically.<locals>.evalu  s^    4#4#4QVYY^^#LMB+fiinn56B==00r9   )rf   r   r  rr  r   r   r*  r  r1  r'  rX  r+  )rH   rU  rT   r  s   `  @r7   doit_numericallyDerivative.doit_numericallyi  s     t  !Q&#dnn*=*B%&MNN""#A&	1   T-/]]699>>-J"L!'1 	1r9   c                     U R                   S   $ r4  _argsrG   s    r7   rv  Derivative.expr}  s    zz!}r9   c                J    U R                    Vs/ s H  oS   PM	     sn$ s  snf r4  )r  )rH   r   s     r7   r<  Derivative._wrt_variables  s&     #1121!1222s    c                    / nU R                    H?  u  p#UR                  (       d  [        [        S5      5      eUR	                  U/U-  5        MA     [        U5      $ )Nz
                Cannot give expansion for symbolic count. If you just
                want a list of all variables of differentiation, use
                _wrt_variables.)r  
is_Integerr   r,   extendrg   )rH   rS  r-  r"  s       r7   r  Derivative.variables  s]    
 ++HA##
 ,# !$ % % IIqc%i  , Ryr9   c                     U R                   SS  $ )Nr   r^  rG   s    r7   r  Derivative.variable_count  s    zz!"~r9   c                d    [        U R                   VVs/ s H  u  pUPM	     snnS5      $ s  snnf r4  )sumr  )rH   rk   r"  s      r7   derivative_countDerivative.derivative_count  s+    $*=*=>*=haE*=>BB>s   ,
c                    U R                   R                  nU R                   H   u  p#UR                  UR                  5        M"     U$ rV   )rv  r   r  r  )rH   retrk   r"  s       r7   r   Derivative.free_symbols  s:    ii$$++HAJJu))* ,
r9   c                4    U R                   S   R                  $ r4  )r2   rb   rG   s    r7   rb   Derivative.kind  s    yy|   r9   c                L   XR                   ;   a  U R                  " U R                  /U R                   VVs/ s H  u  p4X4R	                  X5      4PM     snnQ76 nXP:w  a  UR                  X5      $ [        USS5      (       dH  [        U[        5      (       a  [        XU5      $ [        S5      n[        U R                  X05      Xb5      $ UR                  (       a  UR                  U R                  :X  a  U R                  UR                  :X  a  U$ S n[        [        [!        UR                  5      5      5      n[        [        [!        U R                  5      5      5      n	U" X5      (       a&  [#        U/X-
  R%                  5       Q76 R                  $ ['        U R(                  5      n
U
 Vs/ s H  oR+                  X5      PM     nnU
S   U:X  a  [#        U6 $ US   U
S   :w  a  U R                    Vs0 s H!  nUR,                  (       a  M  U[        5       _M#     nnU R                    Vs1 s H  oR/                  X5      iM     nnU
S   R                  U5      R0                  U-  nUR                  U5      R0                  nUR                  U5      R0                  nUU-
  U-  (       a  [        XU5      $ S [3        USS  U
SS  5       5       n[5        S U 5       5      (       Ga  [7        U R                  S	S
9 HJ  n[9        S[;        U5      5       H.  nUU   u  nnUU:X  d  M  XU   S   :w  d  M   [        XU5      s  s  $    ML     USS  nU R                   nU R                  n/ n[=        U5       HY  u  nu  nnUR>                  (       a  M  [        SU-  5      nUR                  UU   U05      nUU4UU'   URA                  Xm45        M[     U(       a-  UR+                  X5      n[        [C        U/UQ76 /[3        U6 Q76 $ [#        U6 $ s  snnf s  snf s  snf s  snf )Nr   Frh  c                2   ^ ^ [        U U4S jT  5       5      $ )Nc              3  @   >#    U  H  nTU   TU   :*  S :H  v   M     g7f)TNr=   )r#  r   r6   bs     r7   r$  9Derivative._eval_subs.<locals>._subset.<locals>.<genexpr>  s"     =1aAaDAaDLT11s   )r  )r6   ru  s   ``r7   _subset&Derivative._eval_subs.<locals>._subset  s    =1===r9   r   c              3  6   #    U  H  u  u  pu  p2X4v   M     g 7frV   r=   )r#  r   rk   rG  s       r7   r$  (Derivative._eval_subs.<locals>.<genexpr>  s     K0J,VaVa!0Js   r   c              3  .   #    U  H  u  pX:g  v   M     g 7frV   r=   )r#  r   rG  s      r7   r$  rz    s     (%$!qv%s   T)	recursiver  )"r<  r   rv  r  rJ  r   r]   r   r  r  rq  r   is_Derivativer  r/   rD  reversedr  ra   r   r2   r   r  rz   r   rp  r(  r	   rh   rf   r  r   r4  r  )rH   r   r   r-  rg  rv  rh  rw  old_vars	self_varsr2   rE  newargsvisymsr9  	forbiddennfreeofreeviterr6   r   rk   r5  oldvnewerJ  cis                               r7   r   Derivative._eval_subs  s    %%%99TYY 2 //*1/DA ,-ffS.>*?/*1 2D|s003U33 c6**3//tBsi 8"BB TYY!6~~.
> tHS-?-?$@ABHXd.A.A%B CDIx+++CQ93G2N2N2PQ[[[DII.23d773$d37c> (111:a  +/*=*= %*=B||  BK*=D %.2.A.AB.A88B#.ACBQ((.;;cAILL&33ELL&33E*Ds++KGABKab0JK(%(((
 TYY$7q#g,/A#AJEBBw2a#3#Ds33 0 8 B&&D99DD(}8B||| w{+B  ==$q'27DHBqE KK)  - zz#+Jt1b1?CJ?? $W--w*1> 4%Bs   P0P7PP/P!c              #     #    U R                   nU R                  R                  XUS9 H  nU R                  " U/UQ76 v   M     g 7f)N)rV  r  )r  rv  lseriesr   )rH   rE  rV  r  dxr  s         r7   _eval_lseriesDerivative._eval_lseries  s@     ^^II%%a%>D))D&2&& ?s   AAc                D   U R                   R                  XUS9nUR                  5       nU R                  n[        R
                  " UR                  5       5       Vs/ s H  oR                  " U/UQ76 PM     n	nU(       a  U	R                  Xa-  5        [	        U	6 $ s  snf )Nri  )	rv  r{  rs  r  r   	make_argsrt  r   r4  )
rH   rE  r   rV  r  r   r  r  r6   rS  s
             r7   ro  Derivative._eval_nseries  s    iiT2HHJ^^),s{{})EF)EAiiB)EFIIacNBx Gs   Bc                    U R                   R                  U5      n[        R                  nU H"  n[	        U/U R
                  Q76 nUS:w  d  M!    U$    U$ r4  )rv  r  r   rx  r1  r  )rH   rE  rV  r  
series_genr=  leading_terms          r7   r   Derivative._eval_as_leading_term  sR    YY&&q)
FF&L\3DNN3AAv	 ' r9   Nc                     SSK Jn  U" XX#5      $ )a
  Expresses a Derivative instance as a finite difference.

Parameters
==========

points : sequence or coefficient, optional
    If sequence: discrete values (length >= order+1) of the
    independent variable used for generating the finite
    difference weights.
    If it is a coefficient, it will be used as the step-size
    for generating an equidistant sequence of length order+1
    centered around ``x0``. Default: 1 (step-size 1)

x0 : number or Symbol, optional
    the value of the independent variable (``wrt``) at which the
    derivative is to be approximated. Default: same as ``wrt``.

wrt : Symbol, optional
    "with respect to" the variable for which the (partial)
    derivative is to be approximated for. If not provided it
    is required that the derivative is ordinary. Default: ``None``.


Examples
========

>>> from sympy import symbols, Function, exp, sqrt, Symbol
>>> x, h = symbols('x h')
>>> f = Function('f')
>>> f(x).diff(x).as_finite_difference()
-f(x - 1/2) + f(x + 1/2)

The default step size and number of points are 1 and
``order + 1`` respectively. We can change the step size by
passing a symbol as a parameter:

>>> f(x).diff(x).as_finite_difference(h)
-f(-h/2 + x)/h + f(h/2 + x)/h

We can also specify the discretized values to be used in a
sequence:

>>> f(x).diff(x).as_finite_difference([x, x+h, x+2*h])
-3*f(x)/(2*h) + 2*f(h + x)/h - f(2*h + x)/(2*h)

The algorithm is not restricted to use equidistant spacing, nor
do we need to make the approximation around ``x0``, but we can get
an expression estimating the derivative at an offset:

>>> e, sq2 = exp(1), sqrt(2)
>>> xl = [x-h, x+h, x+e*h]
>>> f(x).diff(x, 1).as_finite_difference(xl, x+h*sq2)  # doctest: +ELLIPSIS
2*h*((h + sqrt(2)*h)/(2*h) - (-sqrt(2)*h + h)/(2*h))*f(E*h + x)/...

To approximate ``Derivative`` around ``x0`` using a non-equidistant
spacing step, the algorithm supports assignment of undefined
functions to ``points``:

>>> dx = Function('dx')
>>> f(x).diff(x).as_finite_difference(points=dx(x), x0=x-h)
-f(-h + x - dx(-h + x)/2)/dx(-h + x) + f(-h + x + dx(-h + x)/2)/dx(-h + x)

Partial derivatives are also supported:

>>> y = Symbol('y')
>>> d2fdxdy=f(x,y).diff(x,y)
>>> d2fdxdy.as_finite_difference(wrt=x)
-Derivative(f(x - 1/2, y), y) + Derivative(f(x + 1/2, y), y)

We can apply ``as_finite_difference`` to ``Derivative`` instances in
compound expressions using ``replace``:

>>> (1 + 42**f(x).diff(x)).replace(lambda arg: arg.is_Derivative,
...     lambda arg: arg.as_finite_difference())
42**(-f(x - 1/2) + f(x + 1/2)) + 1


See also
========

sympy.calculus.finite_diff.apply_finite_diff
sympy.calculus.finite_diff.differentiate_finite
sympy.calculus.finite_diff.finite_diff_weights

r   )_as_finite_diff)sympy.calculus.finite_diffr  )rH   pointsx0r9  r  s        r7   as_finite_differenceDerivative.as_finite_difference$  s    l 	?tR55r9   c                "    [         R                  $ rV   )r   rx  )ri   rv  s     r7   r  $Derivative._get_zero_with_shape_like}  s    vvr9   c                $    UR                  X#5      $ rV   )_eval_derivative_n_times)ri   rv  r-  r"  s       r7   r  ,Derivative._dispatch_eval_derivative_n_times  s     ,,Q66r9   r=   r  )r   NN)!r>   r?   r@   rA   rO   r}  r   r   r   r  r   r  r?  r8  r  r   rr  r[  rv  r<  r  r  rk  r   rb   r   r  ro  r  r  r  r  rB   r=   r9   r7   r  r    sX   pd MK KBun B B ]+ ]+~(E( )*1 +1&   3 3
     C C   ! !a.F'
W6r   7 7r9   r  c                   ^ SSK Jn  SSKJn  SSKJn  X4U[        [        [        4m[        U T5      (       d  [        U4S jU 5       5      (       a  SSKJn  U" U /UQ70 UD6$ [        U /UQ70 UD6$ )Nr   r  r  )r   c              3     >#    U  H@  n[        U[        [        [        45      (       a  [        US    T5      O[        UT5      v   MB     g7fr   N)r   rg   r   r   )r#  r   array_typess     r7   r$  '_derivative_dispatch.<locals>.<genexpr>  sg       ,e  [d  VWZXY\acgin[oMpMpJqt[,I  wA  BC  EP  wQ  -Q  [ds   AA)ArrayDerivative)r  r  r  r  r  r   r   rg   r   r   r(  $sympy.tensor.array.array_derivativesr  r  )rv  r  r   r  r  r   r  r  s          @r7   r  r    sw    4=,9dE5IK$$$  ,e  [d  ,e  )e  )eHt:i:6::d1Y1&11r9   c                      \ rS rSrSrSrSS jr\S 5       r\	S 5       r
\	S 5       r\	S 5       r\	S	 5       r\r\	S
 5       rS rS r\	S 5       rS rSrg)Lambdai  a~  
Lambda(x, expr) represents a lambda function similar to Python's
'lambda x: expr'. A function of several variables is written as
Lambda((x, y, ...), expr).

Examples
========

A simple example:

>>> from sympy import Lambda
>>> from sympy.abc import x
>>> f = Lambda(x, x**2)
>>> f(4)
16

For multivariate functions, use:

>>> from sympy.abc import y, z, t
>>> f2 = Lambda((x, y, z, t), x + y**z + t**z)
>>> f2(1, 2, 3, 4)
73

It is also possible to unpack tuple arguments:

>>> f = Lambda(((x, y), z), x + y + z)
>>> f((1, 2), 3)
6

A handy shortcut for lots of arguments:

>>> p = x, y, z
>>> f = Lambda(p, x + y*z)
>>> f(*p)
x + y*z

Tc                v   [        U5      (       a1  [        U[        [        45      (       d  [	        SSSS9  [        U5      n[        U5      (       a  UOU4n[        U5      nU R                  U5        [        U5      S:X  a  US   U:X  a  [        R                  $ [        R                  " X[        U5      5      $ )Nz
                Using a non-tuple iterable as the first argument to Lambda
                is deprecated. Use Lambda(tuple(args), expr) instead.
                z1.5zdeprecated-non-tuple-lambdadeprecated_since_versionactive_deprecations_targetr   r   )r&   r   rg   r   r!   r   _check_signaturerf   r   IdentityFunctionr   r   )ri   r_   rv  _sigsigs        r7   r   Lambda.__new__  s    Iz)eU^'L'L% */+H i(I$Y//yi\T]S!s8q=SVt^%%%||Cgdm44r9   c                   ^^ [        5       mUU4S jm[        U[        5      (       d  [        SU-  5      eT" U5        g )Nc                   > U  Hf  nUR                   (       a'  UT;   a  [        SU-  5      eTR                  U5        M;  [        U[        5      (       a
  T" U5        MZ  [        SU-  5      e   g )NzDuplicate symbol %sz:Lambda signature should be only tuples and symbols, not %s)r  rL   addr   r   )r2   r6   rcheckr  s     r7   r  'Lambda._check_signature.<locals>.rcheck  se    ;;Dy/0E0IJJHHQK5))1I+ -/12-3 4 4 r9   z)Lambda signature should be a tuple not %s)r   r   r   rL   )ri   r  r  r  s     @@r7   r  Lambda._check_signature  s7    u
	4 #u%%#$ORU$UVVsr9   c                     U R                   S   $ )z@The expected form of the arguments to be unpacked into variablesr   r^  rG   s    r7   r_   Lambda.signature       zz!}r9   c                     U R                   S   $ )z The return value of the functionr   r^  rG   s    r7   rv  Lambda.expr  r  r9   c                F   ^ U4S jm[        T" U R                  5      5      $ )zAThe variables used in the internal representation of the functionc              3  x   >#    [        U [        5      (       a  U  H  nT" U5       S h  vN   M     g U v   g  N7frV   )r   r   )r2   r   
_variabless     r7   r  $Lambda.variables.<locals>._variables  s6     $&&C)#..   
 /s   &:8:)rg   r_   )rH   r  s    @r7   r  Lambda.variables  s    	 Z/00r9   c                D    SSK Jn  U" [        U R                  5      5      $ )Nr   r   )r   r   rf   r_   r   s     r7   rt   Lambda.nargs  s    -T^^,--r9   c                Z    U R                   R                  [        U R                  5      -
  $ rV   )rv  r   r   r  rG   s    r7   r   Lambda.free_symbols  s     yy%%DNN(;;;r9   c           	     2   [        U5      nX R                  ;  aG  Sn[        UU [        U R                  5      S   S[        U R                  5      S   S:g  -  US.-  5      eU R	                  U R
                  U5      nU R                  R                  U5      $ )NzD%(name)s takes exactly %(args)s argument%(plural)s (%(given)s given)r   r   r   )r   r2   r   r   )rf   rt   rQ   r   _match_signaturer_   rv  r   )rH   r2   r   r   r=  s        r7   __call__Lambda.__call__  s    IJJ:D#DTZZ(+tDJJ/2a78	, %   !!$..$7yy!!!$$r9   c                ,   ^^ 0 mUU4S jmT" X5        T$ )Nc                (  > [        X5       H  u  p#UR                  (       a  UTU'   M  [        U[        5      (       d  M4  [        U[        [        45      (       a  [        U5      [        U 5      :w  a  [        SU< SU < 35      eT" X#5        M     g )NzCan't match z and )rp  r  r   r   rg   rf   rQ   )parsr2   parr   rmatch	symargmaps       r7   r  'Lambda._match_signature.<locals>.rmatch  si    O==%(IcNU++%cE5>::c$i3t9>T/4QU0VWW3$ ,r9   r=   )rH   r  r2   r  r  s      @@r7   r  Lambda._match_signature  s    		% 	sr9   c                4    U R                   U R                  :H  $ )z<Return ``True`` if this ``Lambda`` is an identity function. )r_   rv  rG   s    r7   is_identityLambda.is_identity,  s     ~~**r9   c                    U R                  U R                  S   U R                  S   R                  [        U5      S95      $ )Nr   r   )r   )r   r2   r   r.   )rH   r+  s     r7   r.  Lambda._eval_evalf1  s6    yy1tyy|'9'9K<M'9'NOOr9   r=   N)r   r  )r>   r?   r@   rA   rO   r   r   r   r  r   r_   rv  r  rt   bound_symbolsr   r  r  r  r.  rB   r=   r9   r7   r  r    s    $J K5(  (     1 1 . . M< <%*" + +Pr9   r  c                     ^  \ rS rSrSrS rS rS rSS jr\r	\
S 5       r\r\
S 5       r\
S	 5       r\
S
 5       r\
S 5       rS rS rU 4S jrS rS rS rSS jrS rSrU =r$ )r  i5  a  
Represents unevaluated substitutions of an expression.

``Subs(expr, x, x0)`` represents the expression resulting
from substituting x with x0 in expr.

Parameters
==========

expr : Expr
    An expression.

x : tuple, variable
    A variable or list of distinct variables.

x0 : tuple or list of tuples
    A point or list of evaluation points
    corresponding to those variables.

Examples
========

>>> from sympy import Subs, Function, sin, cos
>>> from sympy.abc import x, y, z
>>> f = Function('f')

Subs are created when a particular substitution cannot be made. The
x in the derivative cannot be replaced with 0 because 0 is not a
valid variables of differentiation:

>>> f(x).diff(x).subs(x, 0)
Subs(Derivative(f(x), x), x, 0)

Once f is known, the derivative and evaluation at 0 can be done:

>>> _.subs(f, sin).doit() == sin(x).diff(x).subs(x, 0) == cos(0)
True

Subs can also be created directly with one or more variables:

>>> Subs(f(x)*sin(y) + z, (x, y), (0, 1))
Subs(z + f(x)*sin(y), (x, y), (0, 1))
>>> _.doit()
z + f(0)*sin(1)

Notes
=====

``Subs`` objects are generally useful to represent unevaluated derivatives
calculated at a point.

The variables may be expressions, but they are subjected to the limitations
of subs(), so it is usually a good practice to use only symbols for
variables, since in that case there can be no ambiguity.

There's no automatic expansion - use the method .doit() to effect all
possible substitutions of the object and also of objects inside the
expression.

When evaluating derivatives at a point that is not a symbol, a Subs object
is returned. One is also able to calculate derivatives of Subs objects - in
this case the expression is always expanded (for the unevaluated form, use
Derivative()).

In order to allow expressions to combine before doit is done, a
representation of the Subs expression is used internally to make
expressions that are superficially different compare the same:

>>> a, b = Subs(x, x, 0), Subs(y, y, 0)
>>> a + b
2*Subs(x, x, 0)

This can lead to unexpected consequences when using methods
like `has` that are cached:

>>> s = Subs(x, x, 0)
>>> s.has(x), s.has(y)
(True, False)
>>> ss = s.subs(x, y)
>>> ss.has(x), ss.has(y)
(True, False)
>>> s, ss
(Subs(x, x, 0), Subs(y, y, 0))
c           
     Z  ^^^^^^ [        T[        5      (       d  T/m[        T6 m[        T5      (       af  [        T5      R	                  5        VVs/ s H  u  pVUS:  d  M  [        U5      PM     nnnSR                  U5      n[        [        SU-  5      5      e[        [        T[        5      (       a  TOT/6 m[        T5      [        T5      :w  a  [        S5      eT(       d  [        T5      $ [        T[        5      (       a+  TR                  T-   mTR                  T-   mTR                  mO[        T5      mSm[!        [#        T5      [$        S9n	SSKJn
   " S	 S
U
5      mU4S jm U	 Vs0 s H  o[+        TT" U5      -   5      _M     nn[-        TT5       VVs/ s H  u  p[X\U   4PM     nnn[/        UUUUU4S jU 5       5      (       a  TS-  mMn   [0        R2                  " U T[        T6 T5      nTR5                  [7        U5      5      Ul        U$ s  snnf s  snf s  snnf )Nr   r  zU
                The following expressions appear more than once: %s
                zCNumber of point values must be the same as the number of variables.rk   rA  r   )
StrPrinterc                      \ rS rSrS rSrg)&Subs.__new__.<locals>.CustomStrPrinteri  c                D    [        U5      [        UR                  5      -   $ rV   )r~   r  )rH   rv  s     r7   _print_Dummy3Subs.__new__.<locals>.CustomStrPrinter._print_Dummy  s    4y3t'7'7#888r9   r=   N)r>   r?   r@   rA   r  rB   r=   r9   r7   CustomStrPrinterr    s    9r9   r  c                6   > T" U5      nUR                  U 5      $ rV   )doprint)rv  settingsrY   r  s      r7   mystrSubs.__new__.<locals>.mystr  s     *A99T?"r9   c           
   3     >#    U  HR  u  pUTR                   ;   =(       a5    UT;   =(       a)    [        TT" TTR                  U5         5      -   5      U:g  v   MT     g 7frV   )r   r  index)r#  rk   r  rv  r  pointprer  s      r7   r$  Subs.__new__.<locals>.<genexpr>  sg      %  $tq ))) G	>G#eIOOA,>&? @@AQFG  $s   AA)r'   r   r$   r/   ra   r~   r  r}   r,   rf   r   r   r  r  r  rv  sortedr   r   sympy.printing.strr  r  rp  r(  r   r   r   rD  _expr)ri   rv  r  r  r  r-  r   repeatedr&  ptsr  rY   s_ptsrepsr   r  r  r  s    ```           @@@r7   r   Subs.__new__  s   9e,,"I9%	I+29+=+C+C+EO+E41QA+EHO8$BZ ))    UE!:!:Iu:Y' 8 9 9 4=  dD!!2IJJ&E99D4=D SZ%561	9z 	9	# 8;<1sU1X~..E<	5131DA aM1  3  % %  $% % % s
ll3eY&7?MM$t*-	
m PF =3s   H#H2H"!H'c                .    U R                   R                  $ rV   rJ  rG   s    r7   r?  Subs._eval_is_commutative  rL  r9   c                   U R                   u  p#n[        [        X45      5       H)  u  nu  pgXg:X  d  M  US U X5S-   S  -   nUS U XES-   S  -   nM+     U(       d  U R                  $ [	        U[
        5      (       Ga  / n[        U5       HE  u  pV[	        U[        5      (       a  UR                  XdU   5      nM0  UR                  XdU   45        MG     [	        U[
        5      (       d  UR                  5       n[	        U[
        5      (       Ga[  / n	[        5       n
UR                   S   nU Ha  u  pgXR                  Xj05      R                  ;  a+  UR                  U5      (       a  UR                  Xg5      nMM  MO  U	R                  Xg45        Mc     U	n/ n[        5       n
UR                  nUR                  nUR                   Hw  u  po[	        U[        5      (       a  Xn;   a  UR!                  Xo45      nM3  XR                  Xj5      R                  ;   a  UR!                  Xo45      nMe  UR                  Xo45        My     UR                  U5      nU H  nUR!                  U5      nM     OEUR                  U5      nO3UR                  " S0 UD6R                  [#        [        X45      5      5      nUR%                  SS5      (       a  UU :w  a  UR                  " S0 UD6nU$ )Nr   r   rQ  Tr=   )r2   r  rp  rv  r   r  rq   rJ  r4  r  rq  r   r   rD  r  r  r1  r   rz   )rH   rR  r  r-  rY   r   r  r  undoneundone2r  r   r9  rv  r(  r  rS  r5  s                     r7   r  	Subs.doit  se   ))a %SY/KAxxbqEA!efI%bqEA!efI% 0 99a$$F"1b-00rQ4(AMM2t*-	 &
 a,,FFH!Z((GffQi$FB

B7 3 @ @@772;; !rA '  x0 % !Gvv((..FB!"f--"*#yy"2ii.;;;#yy"2

B8, / YYv&BB  VVF^%%%d3q9o6B99VT""rTz!5!B	r9   c                D    U R                  5       R                  " U40 UD6$ rV   )r  r   )rH   r+  r   s      r7   r   
Subs.evalf	  s    yy{  111r9   c                     U R                   S   $ )zThe variables to be evaluatedr   r^  rG   s    r7   r  Subs.variables	  r  r9   c                     U R                   S   $ )z1The expression on which the substitution operatesr   r^  rG   s    r7   rv  	Subs.expr	  r  r9   c                     U R                   S   $ )z8The values for which the variables are to be substitutedrw   r^  rG   s    r7   r  
Subs.point	  r  r9   c                    U R                   R                  [        U R                  5      -
  [        U R                  R                  5      -  $ rV   )rv  r   r   r  r  rG   s    r7   r   Subs.free_symbols	  s9    		&&T^^)<<

''() 	*r9   c                   [        SSSS9  [        [        5         U R                  R                  [        U R                  5      -
  [        U R                  R                  5      -  sS S S 5        $ ! , (       d  f       g = f)Nz
        The expr_free_symbols property is deprecated. Use free_symbols to get
        the free symbols of an expression.
        z1.9zdeprecated-expr-free-symbolsr  )r!   r#   r"   rv  expr_free_symbolsr   r  r  rG   s    r7   r  Subs.expr_free_symbols"	  sa    ! # &+'E	G 45II//#dnn2EE

4456 655s   AA11
A?c                p    [        U[        5      (       d  gU R                  5       UR                  5       :H  $ r   )r   r  _hashable_contentr  s     r7   r  Subs.__eq__/	  s/    %&&%%'5+B+B+DDDr9   c                    X:X  + $ rV   r=   r  s     r7   r  Subs.__ne__4	  s    !!r9   c                    > [         TU ]  5       $ rV   )r   r  )rH   r   s    r7   r  Subs.__hash__7	  s    w!!r9   c                0   U R                   R                  U R                  5      4[        [	        [        U R                  U R                  5       VVs/ s H*  u  pU R                  R                  U5      (       a  M'  X4PM,     snn5      5      -   $ s  snnf rV   )
r  r   canonical_variablesrg   r    rp  r  r  rv  rD  )rH   r-  rY   s      r7   r  Subs._hashable_content:	  s    

##D$<$<= g

+E+ +/!3799==3C  &v+E F GG 	GEs   &B=Bc                  ^ [        U R                  5      nXR                  ;   a  [        T5      T1:X  a7  [	        U4S jU R
                   5       5      (       d  U R                  UT05      $ U R                  R                  U5      n[        U[        U R                  5      5       H  nX5   R                  UT5      X5'   M     U R                  U R                  U R                  U5      $ U R                   Vs/ s H  oDR                  UT5      PM     nnU[        U R                  5      :w  a/  U R                  U R                  U R                  U4-   UT/-   5      $ U R                  R                  UT5      nU R                   Vs/ s H  oDR                  UT5      PM     nnU R                  XvU5      $ s  snf s  snf )Nc              3  D   >#    U  H  oR                  T5      v   M     g 7frV   rC  )r#  r   r   s     r7   r$  "Subs._eval_subs.<locals>.<genexpr>F	  s      13(11EE#JJ	rG  )r   r  r  r	   r(  r2   r   r  rh   rf   r   r   rv  )rH   r   r   ptr   rG  r-  rv  s     `     r7   r   Subs._eval_subs?	  sf   
 $**.. s|u$S 13(,		13 .3 .3 }}c3Z00 $$S)A1c$..12C- 399TYY;;(,71WWS#7T^^$$99TYY#(?seLLyysC()-4Aggc34yy"%% 8 5s   9F7F<c                  ^^^ U R                   mU R                  U R                  4=mu  p#[        R                  " UUU4S j[        X#5       5       5      nTR                  nU Vs1 s H   n[        UR                  5      S:  d  M  UiM"     nnU Vs1 s H  n[        5       iM     nnTR                  [        [        Xx5      5      5      n	U	R                  U-
  n
X-  nU[        U5      -  nXU-   VVs1 s H  oR                    H  ofiM     M     snn-  nUTR                  -  (       aA  U[        TR                  T5      U R                  U R                  5      R                  5       -  nU$ s  snf s  snf s  snnf )Nc              3     >#    U  HB  u  pUR                  T5      [        TR                  U5      /TQ76 R                  5       -  v   MD     g 7frV   )r1  r  r  )r#  r-  rY   r  r   vps      r7   r$  (Subs._eval_derivative.<locals>.<genexpr>[	  sB      #! 66!9T!&&)%9b%9%>%>%@@!s   A
Ar   )rv  r  r  r   fromiterrp  r   rf   rq  r   rD  r   r  r1  r  )rH   r   rE  Pvalefreer   compounddumsmaskedifreer(  rG  r  r  s    `           @@r7   r8  Subs._eval_derivativeW	  s9   IINNDJJ..TQll #A	# #   %@u!ANN(;a(?Au@!)*A*DX!456##d*A8ODOq^^^ODD!.. 4q	4>>4::>CCEEC
 A* Es   'E1E1E66E;c                   XR                   ;   a+  U R                   R                  U5      nU R                  U   nOUnU R                  R	                  XbUS9nUR                  5       n[        R                  " UR                  5       5      n	[        U	 V
s/ s H"  oR                  " U
/U R                  SS  Q76 PM$     sn
6 nU(       a  XR                  Xa5      -  nU$ s  sn
f )Nri  r   )r  r  r  rv  r{  rs  r   r  rt  r   r2   rJ  )rH   rE  r   rV  r  aposr  r   r  termsr6   rS  s               r7   ro  Subs._eval_nseriesr	  s    

?::##A&DNN4(EEii6HHJckkm,?A99Q/12/?@&&""B	 @s   )Cc                   XR                   ;   aE  U R                   R                  U5      nU R                  U   nU R                  R	                  U5      $ XR                  ;   a  U $ U R                  R	                  U5      $ rV   )r  r  r  rv  rn  )rH   rE  rV  r  iposxvars         r7   r  Subs._eval_as_leading_term	  sh    

?::##A&D>>$'D99,,T22 Kyy((++r9   r=   rV   r  )r>   r?   r@   rA   rO   r   r?  r  r   r   r   r  r  rv  r  r   r  r  r  r  r  r   r8  ro  r  rB   r   r   s   @r7   r  r  5  s    Sh<|(:x2 	A  M    * * 
7 
7E
""G
&06
, 
,r9   r  c                    [        U S5      (       a  U R                  " U0 UD6$ UR                  SS5        [        U /UQ70 UD6$ )a	  
Differentiate f with respect to symbols.

Explanation
===========

This is just a wrapper to unify .diff() and the Derivative class; its
interface is similar to that of integrate().  You can use the same
shortcuts for multiple variables as with Derivative.  For example,
diff(f(x), x, x, x) and diff(f(x), x, 3) both return the third derivative
of f(x).

You can pass evaluate=False to get an unevaluated Derivative class.  Note
that if there are 0 symbols (such as diff(f(x), x, 0), then the result will
be the function (the zeroth derivative), even if evaluate=False.

Examples
========

>>> from sympy import sin, cos, Function, diff
>>> from sympy.abc import x, y
>>> f = Function('f')

>>> diff(sin(x), x)
cos(x)
>>> diff(f(x), x, x, x)
Derivative(f(x), (x, 3))
>>> diff(f(x), x, 3)
Derivative(f(x), (x, 3))
>>> diff(sin(x)*cos(y), x, 2, y, 2)
sin(x)*cos(y)

>>> type(diff(sin(x), x))
cos
>>> type(diff(sin(x), x, evaluate=False))
<class 'sympy.core.function.Derivative'>
>>> type(diff(sin(x), x, 0))
sin
>>> type(diff(sin(x), x, 0, evaluate=False))
sin

>>> diff(sin(x))
cos(x)
>>> diff(sin(x*y))
Traceback (most recent call last):
...
ValueError: specify differentiation variables to differentiate sin(x*y)

Note that ``diff(sin(x))`` syntax is meant only for convenience
in interactive sessions and should be avoided in library code.

References
==========

.. [1] https://reference.wolfram.com/legacy/v5_2/Built-inFunctions/AlgebraicComputation/Calculus/D.html

See Also
========

Derivative
idiff: computes the derivative implicitly

r1  r   T)r|   r1  
setdefaultr  )r  symbolsr   s      r7   r1  r1  	  sK    @ q&vvw)&))
j$'6G6v66r9   c	                l    X9S'   XIS'   XYS'   XiS'   XyS'   XS'   [        U 5      R                  " SXS.U	D6$ )	a:&  
Expand an expression using methods given as hints.

Explanation
===========

Hints evaluated unless explicitly set to False are:  ``basic``, ``log``,
``multinomial``, ``mul``, ``power_base``, and ``power_exp`` The following
hints are supported but not applied unless set to True:  ``complex``,
``func``, and ``trig``.  In addition, the following meta-hints are
supported by some or all of the other hints:  ``frac``, ``numer``,
``denom``, ``modulus``, and ``force``.  ``deep`` is supported by all
hints.  Additionally, subclasses of Expr may define their own hints or
meta-hints.

The ``basic`` hint is used for any special rewriting of an object that
should be done automatically (along with the other hints like ``mul``)
when expand is called. This is a catch-all hint to handle any sort of
expansion that may not be described by the existing hint names. To use
this hint an object should override the ``_eval_expand_basic`` method.
Objects may also define their own expand methods, which are not run by
default.  See the API section below.

If ``deep`` is set to ``True`` (the default), things like arguments of
functions are recursively expanded.  Use ``deep=False`` to only expand on
the top level.

If the ``force`` hint is used, assumptions about variables will be ignored
in making the expansion.

Hints
=====

These hints are run by default

mul
---

Distributes multiplication over addition:

>>> from sympy import cos, exp, sin
>>> from sympy.abc import x, y, z
>>> (y*(x + z)).expand(mul=True)
x*y + y*z

multinomial
-----------

Expand (x + y + ...)**n where n is a positive integer.

>>> ((x + y + z)**2).expand(multinomial=True)
x**2 + 2*x*y + 2*x*z + y**2 + 2*y*z + z**2

power_exp
---------

Expand addition in exponents into multiplied bases.

>>> exp(x + y).expand(power_exp=True)
exp(x)*exp(y)
>>> (2**(x + y)).expand(power_exp=True)
2**x*2**y

power_base
----------

Split powers of multiplied bases.

This only happens by default if assumptions allow, or if the
``force`` meta-hint is used:

>>> ((x*y)**z).expand(power_base=True)
(x*y)**z
>>> ((x*y)**z).expand(power_base=True, force=True)
x**z*y**z
>>> ((2*y)**z).expand(power_base=True)
2**z*y**z

Note that in some cases where this expansion always holds, SymPy performs
it automatically:

>>> (x*y)**2
x**2*y**2

log
---

Pull out power of an argument as a coefficient and split logs products
into sums of logs.

Note that these only work if the arguments of the log function have the
proper assumptions--the arguments must be positive and the exponents must
be real--or else the ``force`` hint must be True:

>>> from sympy import log, symbols
>>> log(x**2*y).expand(log=True)
log(x**2*y)
>>> log(x**2*y).expand(log=True, force=True)
2*log(x) + log(y)
>>> x, y = symbols('x,y', positive=True)
>>> log(x**2*y).expand(log=True)
2*log(x) + log(y)

basic
-----

This hint is intended primarily as a way for custom subclasses to enable
expansion by default.

These hints are not run by default:

complex
-------

Split an expression into real and imaginary parts.

>>> x, y = symbols('x,y')
>>> (x + y).expand(complex=True)
re(x) + re(y) + I*im(x) + I*im(y)
>>> cos(x).expand(complex=True)
-I*sin(re(x))*sinh(im(x)) + cos(re(x))*cosh(im(x))

Note that this is just a wrapper around ``as_real_imag()``.  Most objects
that wish to redefine ``_eval_expand_complex()`` should consider
redefining ``as_real_imag()`` instead.

func
----

Expand other functions.

>>> from sympy import gamma
>>> gamma(x + 1).expand(func=True)
x*gamma(x)

trig
----

Do trigonometric expansions.

>>> cos(x + y).expand(trig=True)
-sin(x)*sin(y) + cos(x)*cos(y)
>>> sin(2*x).expand(trig=True)
2*sin(x)*cos(x)

Note that the forms of ``sin(n*x)`` and ``cos(n*x)`` in terms of ``sin(x)``
and ``cos(x)`` are not unique, due to the identity `\sin^2(x) + \cos^2(x)
= 1`.  The current implementation uses the form obtained from Chebyshev
polynomials, but this may change.  See `this MathWorld article
<https://mathworld.wolfram.com/Multiple-AngleFormulas.html>`_ for more
information.

Notes
=====

- You can shut off unwanted methods::

    >>> (exp(x + y)*(x + y)).expand()
    x*exp(x)*exp(y) + y*exp(x)*exp(y)
    >>> (exp(x + y)*(x + y)).expand(power_exp=False)
    x*exp(x + y) + y*exp(x + y)
    >>> (exp(x + y)*(x + y)).expand(mul=False)
    (x + y)*exp(x)*exp(y)

- Use deep=False to only expand on the top level::

    >>> exp(x + exp(x + y)).expand()
    exp(x)*exp(exp(x)*exp(y))
    >>> exp(x + exp(x + y)).expand(deep=False)
    exp(x)*exp(exp(x + y))

- Hints are applied in an arbitrary, but consistent order (in the current
  implementation, they are applied in alphabetical order, except
  multinomial comes before mul, but this may change).  Because of this,
  some hints may prevent expansion by other hints if they are applied
  first. For example, ``mul`` may distribute multiplications and prevent
  ``log`` and ``power_base`` from expanding them. Also, if ``mul`` is
  applied before ``multinomial`, the expression might not be fully
  distributed. The solution is to use the various ``expand_hint`` helper
  functions or to use ``hint=False`` to this function to finely control
  which hints are applied. Here are some examples::

    >>> from sympy import expand, expand_mul, expand_power_base
    >>> x, y, z = symbols('x,y,z', positive=True)

    >>> expand(log(x*(y + z)))
    log(x) + log(y + z)

  Here, we see that ``log`` was applied before ``mul``.  To get the mul
  expanded form, either of the following will work::

    >>> expand_mul(log(x*(y + z)))
    log(x*y + x*z)
    >>> expand(log(x*(y + z)), log=False)
    log(x*y + x*z)

  A similar thing can happen with the ``power_base`` hint::

    >>> expand((x*(y + z))**x)
    (x*y + x*z)**x

  To get the ``power_base`` expanded form, either of the following will
  work::

    >>> expand((x*(y + z))**x, mul=False)
    x**x*(y + z)**x
    >>> expand_power_base((x*(y + z))**x)
    x**x*(y + z)**x

    >>> expand((x + y)*y/x)
    y + y**2/x

  The parts of a rational expression can be targeted::

    >>> expand((x + y)*y/x/(x + 1), frac=True)
    (x*y + y**2)/(x**2 + x)
    >>> expand((x + y)*y/x/(x + 1), numer=True)
    (x*y + y**2)/(x*(x + 1))
    >>> expand((x + y)*y/x/(x + 1), denom=True)
    y*(x + y)/(x**2 + x)

- The ``modulus`` meta-hint can be used to reduce the coefficients of an
  expression post-expansion::

    >>> expand((3*x + 1)**2)
    9*x**2 + 6*x + 1
    >>> expand((3*x + 1)**2, modulus=5)
    4*x**2 + x + 1

- Either ``expand()`` the function or ``.expand()`` the method can be
  used.  Both are equivalent::

    >>> expand((x + 1)**2)
    x**2 + 2*x + 1
    >>> ((x + 1)**2).expand()
    x**2 + 2*x + 1

API
===

Objects can define their own expand hints by defining
``_eval_expand_hint()``.  The function should take the form::

    def _eval_expand_hint(self, **hints):
        # Only apply the method to the top-level expression
        ...

See also the example below.  Objects should define ``_eval_expand_hint()``
methods only if ``hint`` applies to that specific object.  The generic
``_eval_expand_hint()`` method defined in Expr will handle the no-op case.

Each hint should be responsible for expanding that hint only.
Furthermore, the expansion should be applied to the top-level expression
only.  ``expand()`` takes care of the recursion that happens when
``deep=True``.

You should only call ``_eval_expand_hint()`` methods directly if you are
100% sure that the object has the method, as otherwise you are liable to
get unexpected ``AttributeError``s.  Note, again, that you do not need to
recursively apply the hint to args of your object: this is handled
automatically by ``expand()``.  ``_eval_expand_hint()`` should
generally not be used at all outside of an ``_eval_expand_hint()`` method.
If you want to apply a specific expansion from within another method, use
the public ``expand()`` function, method, or ``expand_hint()`` functions.

In order for expand to work, objects must be rebuildable by their args,
i.e., ``obj.func(*obj.args) == obj`` must hold.

Expand methods are passed ``**hints`` so that expand hints may use
'metahints'--hints that control how different expand methods are applied.
For example, the ``force=True`` hint described above that causes
``expand(log=True)`` to ignore assumptions is such a metahint.  The
``deep`` meta-hint is handled exclusively by ``expand()`` and is not
passed to ``_eval_expand_hint()`` methods.

Note that expansion hints should generally be methods that perform some
kind of 'expansion'.  For hints that simply rewrite an expression, use the
.rewrite() API.

Examples
========

>>> from sympy import Expr, sympify
>>> class MyClass(Expr):
...     def __new__(cls, *args):
...         args = sympify(args)
...         return Expr.__new__(cls, *args)
...
...     def _eval_expand_double(self, *, force=False, **hints):
...         '''
...         Doubles the args of MyClass.
...
...         If there more than four args, doubling is not performed,
...         unless force=True is also used (False by default).
...         '''
...         if not force and len(self.args) > 4:
...             return self
...         return self.func(*(self.args + self.args))
...
>>> a = MyClass(1, 2, MyClass(3, 4))
>>> a
MyClass(1, 2, MyClass(3, 4))
>>> a.expand(double=True)
MyClass(1, 2, MyClass(3, 4, 3, 4), 1, 2, MyClass(3, 4, 3, 4))
>>> a.expand(double=True, deep=False)
MyClass(1, 2, MyClass(3, 4), 1, 2, MyClass(3, 4))

>>> b = MyClass(1, 2, 3, 4, 5)
>>> b.expand(double=True)
MyClass(1, 2, 3, 4, 5)
>>> b.expand(double=True, force=True)
MyClass(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)

See Also
========

expand_log, expand_mul, expand_multinomial, expand_complex, expand_trig,
expand_power_base, expand_power_exp, expand_func, sympy.simplify.hyperexpand.hyperexpand


power_base	power_expmulr   multinomialbasic)rQ  modulusr=   r   ru  )
r  rQ  r7  r2  r3  r4  r   r5  r6  rR  s
             r7   ru  ru  	  sO    F
 %,"+%L%L&-'N1:A$A5AAr9   c                h    U c  g S nX :w  a&  U [        [        U 5      5      pU(       d   U $ X :w  a  M&  U $ rV   )
expand_mulexpand_multinomial)rv  r|  wass      r7   _mexpandr=  !  sA     |
C
+*%7%=>TK	 + Kr9   c           
     >    [        U 5      R                  USSSSSSS9$ )a5  
Wrapper around expand that only uses the mul hint.  See the expand
docstring for more information.

Examples
========

>>> from sympy import symbols, expand_mul, exp, log
>>> x, y = symbols('x,y', positive=True)
>>> expand_mul(exp(x+y)*(x+y)*log(x*y**2))
x*exp(x + y)*log(x*y**2) + y*exp(x + y)*log(x*y**2)

TFrQ  r4  r3  r2  r6  r5  r   r8  rv  rQ  s     r7   r:  r:  2  s1     4=TtuEu%   A Ar9   c           
     >    [        U 5      R                  USSSSSSS9$ )a2  
Wrapper around expand that only uses the multinomial hint.  See the expand
docstring for more information.

Examples
========

>>> from sympy import symbols, expand_multinomial, exp
>>> x, y = symbols('x y', positive=True)
>>> expand_multinomial((x + exp(x + 1))**2)
x**2 + 2*x*exp(x + 1) + exp(2*x + 2)

FTr?  r8  r@  s     r7   r;  r;  D  s1     4=TuEt   @ @r9   c                   ^^^^ SSK Jm  SSKJm  USL a  UUUU4S jnU R	                  U4S jU5      n [        U 5      R                  TSSSSSSTUS9	$ )	a(  
Wrapper around expand that only uses the log hint.  See the expand
docstring for more information.

Examples
========

>>> from sympy import symbols, expand_log, exp, log
>>> x, y = symbols('x,y', positive=True)
>>> expand_log(exp(x+y)*(x+y)*log(x*y**2))
(x + y)*(log(x) + 2*log(y))*exp(x + y)

r   )r   fractionFc           	       > T
" U 5      u  pUR                  T5       Vs/ s H$  o3R                  S   R                  (       d  M"  UPM&     nnUR                  T5       Vs/ s H$  o3R                  S   R                  (       d  M"  UPM&     nn[        U5      S:X  a  [        U5      S:X  a|  US   nUS   nSSKJn  U" UR                  S   UR                  S   5      nU(       aB  UT" UR                  S   UR                  S   U-  -  5      U-  -   nU R                  XU-  5      n [        [        U TT	SS95      nUR                  T5      U R                  T5      ::  a  U$ U $ s  snf s  snf )Nr   r   )multiplicityT)rQ  forcefactor)
atomsr2   rd  rf   sympyrF  rJ  r:  
expand_logr"  )rE  r   r=  r   rF  r   r  x1rQ  rG  rD  r   s           r7   
_handleMulexpand_log.<locals>._handleMulg  s)   A;DAGGCLALqFF1I,@,@LAAGGCLALqFF1I,@,@LAA1v{s1v{aDaD. AFF1I6Cq	166!9a< 78::AqA#AJqt5NOBxx},	H BAs   !E#E#!E(E(c                p   > U R                   =(       a#    [        U4S jU R                  5        5       5      $ )Nc              3  v   >#    U  H.  n[        U4S  j[        R                  " U5       5       5      v   M0     g7f)c              3  |   >#    U  H1  n[        UT5      =(       a    UR                  S    R                  v   M3     g7fr  )r   r2   is_Rational)r#  r   r   s     r7   r$  9expand_log.<locals>.<lambda>.<locals>.<genexpr>.<genexpr>z  s5      '#!A (2!S'9'SaffQi>S>S'S!s   9<N)r(  r   r  )r#  rG  r   s     r7   r$  /expand_log.<locals>.<lambda>.<locals>.<genexpr>z  s9      #@,>q $' '#q!'# $# $#,>s   69)r3   r  as_numer_denom)rE  r   s    r7   rZ   expand_log.<locals>.<lambda>z  s5    !(( @s #@,-,<,<,>#@  @ @r9   T)	rQ  r   r4  r3  r2  r5  r6  rG  rH  )&sympy.functions.elementary.exponentialr   sympy.simplify.radsimprD  r  r   ru  )rv  rQ  rG  rH  rM  rD  r   s    ``  @@r7   rK  rK  V  sg     ;/	 	$ ||	@
 4=TtEu5   1 1r9   c                @    [        U 5      R                  USSSSSSSS9$ )z
Wrapper around expand that only uses the func hint.  See the expand
docstring for more information.

Examples
========

>>> from sympy import expand_func, gamma
>>> from sympy.abc import x
>>> expand_func(gamma(x + 2))
x*(x + 1)*gamma(x)

TF)rQ  r   r6  r   r4  r3  r2  r5  r8  r@  s     r7   expand_funcrZ    3     4=TE5Ee   P Pr9   c                @    [        U 5      R                  USSSSSSSS9$ )a  
Wrapper around expand that only uses the trig hint.  See the expand
docstring for more information.

Examples
========

>>> from sympy import expand_trig, sin
>>> from sympy.abc import x, y
>>> expand_trig(sin(x+y)*(x+y))
(x + y)*(sin(x)*cos(y) + sin(y)*cos(x))

TF)rQ  trigr6  r   r4  r3  r2  r5  r8  r@  s     r7   expand_trigr^    r[  r9   c                @    [        U 5      R                  USSSSSSSS9$ )a  
Wrapper around expand that only uses the complex hint.  See the expand
docstring for more information.

Examples
========

>>> from sympy import expand_complex, exp, sqrt, I
>>> from sympy.abc import z
>>> expand_complex(exp(z))
I*exp(re(z))*sin(im(z)) + exp(re(z))*cos(im(z))
>>> expand_complex(sqrt(I))
sqrt(2)/2 + sqrt(2)*I/2

See Also
========

sympy.core.expr.Expr.as_real_imag
TFrQ  complexr6  r   r4  r3  r2  r5  r8  r@  s     r7   expand_complexrb    s3    ( 4=T4u5Ee   P Pr9   c                @    [        U 5      R                  USSSSSSUS9$ )a  
Wrapper around expand that only uses the power_base hint.

A wrapper to expand(power_base=True) which separates a power with a base
that is a Mul into a product of powers, without performing any other
expansions, provided that assumptions about the power's base and exponent
allow.

deep=False (default is True) will only apply to the top-level expression.

force=True (default is False) will cause the expansion to ignore
assumptions about the base and exponent. When False, the expansion will
only happen if the base is non-negative or the exponent is an integer.

>>> from sympy.abc import x, y, z
>>> from sympy import expand_power_base, sin, cos, exp, Symbol

>>> (x*y)**2
x**2*y**2

>>> (2*x)**y
(2*x)**y
>>> expand_power_base(_)
2**y*x**y

>>> expand_power_base((x*y)**z)
(x*y)**z
>>> expand_power_base((x*y)**z, force=True)
x**z*y**z
>>> expand_power_base(sin((x*y)**z), deep=False)
sin((x*y)**z)
>>> expand_power_base(sin((x*y)**z), force=True)
sin(x**z*y**z)

>>> expand_power_base((2*sin(x))**y + (2*cos(x))**y)
2**y*sin(x)**y + 2**y*cos(x)**y

>>> expand_power_base((2*exp(y))**x)
2**x*exp(y)**x

>>> expand_power_base((2*cos(x))**y)
2**y*cos(x)**y

Notice that sums are left untouched. If this is not the desired behavior,
apply full ``expand()`` to the expression:

>>> expand_power_base(((x+y)*z)**2)
z**2*(x + y)**2
>>> (((x+y)*z)**2).expand()
x**2*z**2 + 2*x*y*z**2 + y**2*z**2

>>> expand_power_base((2*y)**(1+z))
2**(z + 1)*y**(z + 1)
>>> ((2*y)**(1+z)).expand()
2*2**z*y**(z + 1)

The power that is unexpanded can be expanded safely when
``y != 0``, otherwise different values might be obtained for the expression:

>>> prev = _

If we indicate that ``y`` is positive but then replace it with
a value of 0 after expansion, the expression becomes 0:

>>> p = Symbol('p', positive=True)
>>> prev.subs(y, p).expand().subs(p, 0)
0

But if ``z = -1`` the expression would not be zero:

>>> prev.subs(y, 0).subs(z, -1)
1

See Also
========

expand

FT)rQ  r   r4  r3  r2  r5  r6  rG  r8  )rv  rQ  rG  s      r7   expand_power_baserd    s3    ` 4=Tu%De5   " "r9   c                @    [        U 5      R                  USSSSSSSS9$ )a  
Wrapper around expand that only uses the power_exp hint.

See the expand docstring for more information.

Examples
========

>>> from sympy import expand_power_exp, Symbol
>>> from sympy.abc import x, y
>>> expand_power_exp(3**(y + 2))
9*3**y
>>> expand_power_exp(x**(y + 2))
x**(y + 2)

If ``x = 0`` the value of the expression depends on the
value of ``y``; if the expression were expanded the result
would be 0. So expansion is only done if ``x != 0``:

>>> expand_power_exp(Symbol('x', zero=False)**(y + 2))
x**2*x**y
FTr`  r8  r@  s     r7   expand_power_exprf    s3    . 4=T55DU   O Or9   c           
     D   SSK Jn  SSKJn  SSKJn  SSKJn  SSKJ	n  [        U 5      n [        U [        5      (       Gat  U R                  (       Gdb  / nU /n[        S5      n	[        S	5      n
[        S
5      n[        S5      n[        S5      nU(       Ga  UR                  5       nUR                   (       aY  U["        R$                  LaD  UR&                  S:  a  UR)                  U	5        UR*                  S:w  a  UR)                  U
5        M  GO3UR,                  (       d  UR.                  (       Ga  [1        U5      (       aH  UR)                  U	5        UR2                  S   ["        R4                  L a  UR7                  5       S   nOU* nU" U5      u  nnUR8                  (       a<  UR)                  U
5        US:  a  UR)                  U	5        UR)                  U5        GMU  U["        R$                  LaG  UR8                  (       d  UR)                  U5        UR)                  U
5        UR)                  U5        GM  GOUR:                  (       d  UR<                  (       a  [?        UR2                  5      nSn[A        U5       Hr  u  nn[1        U5      (       a2  US-  nUR)                  U* 5        US:  a  UR)                  U5        MF  MH  UR)                  U5        US:  d  Ma  UR)                  U5        Mt     U[C        U5      :X  a  UR)                  U	5        O&[1        US   5      (       a  UR)                  X-
  5        GM  URD                  (       aL  URF                  ["        R4                  L a/  UR)                  U
5        UR)                  URH                  5        GM  U["        RJ                  :X  a  UR)                  U5        GM:  URD                  (       aM  URH                  ["        RJ                  :X  a/  UR)                  U5        UR)                  URF                  5        GM  UR,                  (       d  [        U[L        5      (       aX  [        URN                  RP                  RS                  5       5      nUR)                  U[C        UR2                  5      S-
  -  5        OUR2                  (       a  URD                  (       d(  URT                  (       d  [        U[V        XC45      (       a  [        URN                  [X        5      (       a1  [        SURN                  RP                  RS                  5       -   5      nO-[        URN                  RP                  RS                  5       5      nUR)                  U5        URZ                  (       d  UR]                  UR2                  5        U(       a  GM  GO[        U [^        5      (       a:  U Ra                  5        VVs/ s H  u  nn[c        UUS9[c        UUS9-   PM     nnnGO[e        U 5      (       a  U  Vs/ s H  n[c        UUS9PM     nnGOv[        XU45      (       ab  / nU R2                   H  nUR)                  [c        USS95        M     [        [g        U SS9RS                  5       5      nUR)                  U5        GO[        U [h        5      (       d  / nO[        U [h        5      (       d  [k        S5      e/ nU /nU(       a  UR                  5       nUR2                  (       a  [        [m        U5      RP                  RS                  5       5      nURn                  (       a+  UR)                  U[C        UR2                  5      S-
  -  5        OUR)                  U5        UR]                  UR2                  5        U(       a  M  U(       d  U(       a  ["        Rp                  $ g[s        U6 nU(       a  U$ URt                  (       a  [w        U5      $ [y        S [r        Rz                  " U5       5       5      $ s  snnf s  snf )a0  
Return a representation (integer or expression) of the operations in expr.

Parameters
==========

expr : Expr
    If expr is an iterable, the sum of the op counts of the
    items will be returned.

visual : bool, optional
    If ``False`` (default) then the sum of the coefficients of the
    visual expression will be returned.
    If ``True`` then the number of each type of operation is shown
    with the core class types (or their virtual equivalent) multiplied by the
    number of times they occur.

Examples
========

>>> from sympy.abc import a, b, x, y
>>> from sympy import sin, count_ops

Although there is not a SUB object, minus signs are interpreted as
either negations or subtractions:

>>> (x - y).count_ops(visual=True)
SUB
>>> (-x).count_ops(visual=True)
NEG

Here, there are two Adds and a Pow:

>>> (1 + a + b**2).count_ops(visual=True)
2*ADD + POW

In the following, an Add, Mul, Pow and two functions:

>>> (sin(x)*x + sin(x)**2).count_ops(visual=True)
ADD + MUL + POW + 2*SIN

for a total of 5:

>>> (sin(x)*x + sin(x)**2).count_ops(visual=False)
5

Note that "what you type" is not always what you get. The expression
1/x/y is translated by sympy into 1/(x*y) so it gives a DIV and MUL rather
than two DIVs:

>>> (1/x/y).count_ops(visual=True)
DIV + MUL

The visual option can be used to demonstrate the difference in
operations for expressions in different forms. Here, the Horner
representation is compared with the expanded form of a polynomial:

>>> eq=x*(1 + x*(2 + x*(3 + x)))
>>> count_ops(eq.expand(), visual=True) - count_ops(eq, visual=True)
-MUL + 3*POW

The count_ops function also handles iterables:

>>> count_ops([x, sin(x), None, True, x + 2], visual=False)
2
>>> count_ops([x, sin(x), None, True, x + 2], visual=True)
ADD + SIN
>>> count_ops({x: sin(x), x + 2: y + 1}, visual=True)
2*ADD + SIN

r   )
Relationalr   )Sum)Integral)BooleanFunctionrC  NEGDIVSUBADDEXPFUNC_)visualT)shortzInvalid type of exprc              3  f   #    U  H'  n[        UR                  =(       d    S /S   5      v   M)     g7f)r   r   N)r   r2   r=  s     r7   r$  count_ops.<locals>.<genexpr>  s)     C0B1sAFFMqc1%&&0Bs   /1)>
relationalrh  sympy.concrete.summationsri  sympy.integrals.integralsrj  sympy.logic.boolalgrk  rX  rD  r   r   r   is_Relationalr  rx   rR  r   rz  rY   r4  r  r3   r1   r8   r2   NegativeOneas_two_termsrd  r   	is_MatAddr   r  rf   is_Powr   baseExp1r   r   r>   upperr   r  r   r  re  r   ra   	count_opsr&   r-   r   r   r   
is_Booleanrx  r   r4   r   rj  r  )rv  rr  rh  ri  rj  rk  rD  opsr2   rl  rm  rn  ro  rp  r6   r   r=  aargsnegsr   r  r  r  r-  r   s                            r7   r  r  /  s   P '-23/4=D$d&8&8&8vUmUmUmUmUm
A }}AEE>ssQw

3ssax

3 " Q[[[??JJsOvvayAMM1NN,Q/B{1<<JJsO1u

3KKNaee^<<AJJsOKKN $ Q[[QVV&u-EAr#B''	RC(q5JJsO ! Bq5JJsO . 3u:%JJsO!%(++JJsy)xxAEEQ]]2

3AFF#AFF{

3xxAFFaff,

3AEE"xx:a33166??0023

1c!&&kAo./HHA
H?Z1[1[ aff&788w)>)>)@@AAqvv4467A

1;;AFF#a dd 
D$		7;zz|E7Ctq! 6*6*+7C 	 E	$489Dqy6*D9	D7	8	899CJJyT23 9T.4467

1e$$$&&233C6DHHJ66tAw//5578A||

1c!&&k!m#45

1KK' $ 66M
s)C

}}3xCc0BCCCUE :s   "``c           
     B  ^^^^ SSK Jn  TTUS.m[        X5      (       a  U R                  U4S j5      $ [	        U [
        S9(       Ga0  [        U [        [        45      (       a  U(       a4  U R                  5        Vs/ s H  n[        U4S jU 5       5      PM     nnO0U R                  5        VVs/ s H  u  pxU[        U40 TD64PM     nnn[        U [        5      (       a  [        U 5      " U5      $ U R                  " U6 $ [        U [        5      (       a4  U R                  " U R                   Vs/ s H  n[        U40 TD6PM     sn6 $ [        U 5      " U  Vs/ s H  n[        U40 TD6PM     sn5      $ [        U 5      n	U	R                   (       a  [#        U	T5      $ U	R$                  (       aB  U	R'                  T5      n	U	R                   (       a  [#        U	R'                  T5      T5      n	U	$  U	$ U	R(                  (       a  U	$ U	R*                  (       a%  U4S jU	R                   5       n
U	R                  " U
6 $ SSKJn  U	R1                  U	R3                  U5       Vs0 s H  oUR'                  T5      _M     sn5      n	S	S
KJm  T(       dU  U	R3                  T5       Vs/ s H  oT" UR8                  [;        5       5      4PM!     nnU	R1                  [        U5      5      n	U	R'                  T5      n	T(       d<  U	R1                  W VVs0 s H  u  pUR<                  UR<                  _M     snn5      n	O!U	R1                  [?        UU4S jS 5      5      n	U	R1                  [?        UU4S jS 5      5      $ s  snf s  snnf s  snf s  snf s  snf s  snf s  snnf )a  Make all Rationals in expr Floats except those in exponents
(unless the exponents flag is set to True) and those in undefined
functions. When processing dictionaries, do not modify the keys
unless ``dkeys=True``.

Examples
========

>>> from sympy import nfloat, cos, pi, sqrt
>>> from sympy.abc import x, y
>>> nfloat(x**4 + x/2 + cos(pi/3) + 1 + sqrt(y))
x**4 + 0.5*x + sqrt(y) + 1.5
>>> nfloat(x**4 + sqrt(y), exponent=True)
x**4.0 + y**0.5

Container types are not modified:

>>> type(nfloat((1, 2))) is tuple
True
r   r  )r   exponentdkeysc                   > [        U 40 TD6$ rV   nfloat)r  kws    r7   rZ   nfloat.<locals>.<lambda>!  s    qBr9   )excludec              3  <   >#    U  H  n[        U40 TD6v   M     g 7frV   r  )r#  r   r  s     r7   r$  nfloat.<locals>.<genexpr>'  s     :1va2   c              3  <   >#    U  H  n[        U40 TD6v   M     g 7frV   r  )r#  r   r  s     r7   r$  r  B  s     <GSvc(R(Gr  )RootOfr   )Powc                R   > T" U R                   [        U R                  T5      5      $ rV   )r  r   r   )rE  r  r   s    r7   rZ   r  W  s    c!&&%q/2r9   c                T    U R                   =(       a    U R                  R                  $ rV   )r~  r   rd  r  s    r7   rZ   r  X  s    ahh3155#3#33r9   c                L   > U R                   " [        U R                  TT5      6 $ rV   )r   r  r2   )rE  r  r   s    r7   rZ   r  [  s    !&&&H56r9   c                Z    [        U [        5      =(       a    [        U [        5      (       + $ rV   )r   r   r  r  s    r7   rZ   r  \  s    *Q)M*Q2M.MMr9   ) r  r  r   	applyfuncr&   r~   rD  r   ra   rg   r  r   r   r   r2   r   r4   r   r  r   is_Atomrz  sympy.polys.rootoftoolsr  r   rI  powerr  r  rq  r   r   )rv  r   r  r  r  r6   r2   r  r-  rS  args_nfloatr  rorY   r  r=  r  r  s    ``             @@r7   r  r    s   * 5h	7B$##~~788 c""dT4L))!ZZ\+) ::;)  + :>FF1OO,F$%%Dz$''yy$''e$$99		B	1va2	BCCDzD9Dq6!?r?D9::	B	||R|	TT!W<<rttAw"B 	 						<BGG<ww$$ /	BHHV,<=,<b"$$q'k,<=	>B3588C=A=aC()=A[[d$	aB[[48441!%%,489 [[235 6 ;;y6MO P Pg+ G C92 > B 9s*   >M<3N4NN"N$&N#N
)rq  r  )TNTTTTTTrC  )T)TFF)TF)   FF)rrO   
__future__r   typingr   collections.abcr   copyregr  r   r6  r   r	   cacher
   
containersr   r   
decoratorsr   r   r   rv  r   r   logicr   r   r   r   r4  r   rm  r   r   r   
operationsr   r`   r   rulesr   	singletonr   r   r   sortingr   r    sympy.utilities.exceptionsr!   r"   r#   sympy.utilities.iterablesr$   r%   r&   r'   r(   r)   sympy.utilities.lambdifyr*   sympy.utilities.miscr+   r,   r-   r  mpmath.libmp.libmpfr.   r^   collectionsr/   r8   	Exceptionr;   r}   rD   r   rL   rQ   ro   r   rq   r   r   r  r  r  r  r   r  r  pickler  r  r  r  r  r1  ru  r=  r:  r;  rK  rZ  r^  rb  rd  rf  r  r  rj  rq  r  r=   r9   r7   <module>r     s  @ #  $   !  # "  " < <  - - ! )   & .R R) ) 8 > >  +  2D		 	- -		 	
		 	=>{D {|b@%= b@Jq{D qh+h +&8 &R
I 
I %& : :D1$  - 0L8Z L^r7 r7j2]PT ]P@V,4 V,r
C7L CG48IBZ
"A$@$*1ZP$P$P0R"jO6UDpUPp " !r9   