+
    i"                     p    ^ RI Ht ^ RIHt ^ RIHt ^ RIHt ^ RIH	t	 ^ RI
HtHtHtHt  ! R R]4      tR	# )
    )permutedims)Number)S)Symbol)sympify)TensorTensExprTensAddTensMulc                      a  ] tR t^	t o RtR t]R 4       t]R 4       t]	R 4       t
R tR tR tR	 tR
 tR t]R 4       t]R 4       tR tRtV tR# )PartialDerivativea	  
Partial derivative for tensor expressions.

Examples
========

>>> from sympy.tensor.tensor import TensorIndexType, TensorHead
>>> from sympy.tensor.toperators import PartialDerivative
>>> from sympy import symbols
>>> L = TensorIndexType("L")
>>> A = TensorHead("A", [L])
>>> B = TensorHead("B", [L])
>>> i, j, k = symbols("i j k")

>>> expr = PartialDerivative(A(i), A(j))
>>> expr
PartialDerivative(A(i), A(j))

The ``PartialDerivative`` object behaves like a tensorial expression:

>>> expr.get_indices()
[i, -j]

Notice that the deriving variables have opposite valence than the
printed one: ``A(j)`` is printed as covariant, but the index of the
derivative is actually contravariant, i.e. ``-j``.

Indices can be contracted:

>>> expr = PartialDerivative(A(i), A(i))
>>> expr
PartialDerivative(A(L_0), A(L_0))
>>> expr.get_indices()
[L_0, -L_0]

The method ``.get_indices()`` always returns all indices (even the
contracted ones). If only uncontracted indices are needed, call
``.get_free_indices()``:

>>> expr.get_free_indices()
[]

Nested partial derivatives are flattened:

>>> expr = PartialDerivative(PartialDerivative(A(i), A(j)), A(k))
>>> expr
PartialDerivative(A(i), A(j), A(k))
>>> expr.get_indices()
[i, -j, -k]

Replace a derivative with array values:

>>> from sympy.abc import x, y
>>> from sympy import sin, log
>>> compA = [sin(x), log(x)*y**3]
>>> compB = [x, y]
>>> expr = PartialDerivative(A(i), B(j))
>>> expr.replace_with_arrays({A(i): compA, B(i): compB})
[[cos(x), 0], [y**3/x, 3*y**2*log(x)]]

The returned array is indexed by `(i, -j)`.

Be careful that other SymPy modules put the indices of the deriving
variables before the indices of the derivand in the derivative result.
For example:

>>> expr.get_free_indices()
[i, -j]

>>> from sympy import Matrix, Array
>>> Matrix(compA).diff(Matrix(compB)).reshape(2, 2)
[[cos(x), y**3/x], [0, 3*y**2*log(x)]]
>>> Array(compA).diff(Array(compB))
[[cos(x), y**3/x], [0, 3*y**2*log(x)]]

These are the transpose of the result of ``PartialDerivative``,
as the matrix and the array modules put the index `-j` before `i` in the
derivative result. An array read with index order `(-j, i)` is indeed the
transpose of the same array read with index order `(i, -j)`. By specifying
the index order to ``.replace_with_arrays`` one can get a compatible
expression:

>>> expr.replace_with_arrays({A(i): compA, B(i): compB}, [-j, i])
[[cos(x), y**3/x], [0, 3*y**2*log(x)]]
c                    \        V\        4      '       d    VP                  V,           pVP                  pV P	                  \        V4      V4      w  r4rV\        P                  ! V .VO5!  pWGn        WWn	        Wgn
        V# N)
isinstancer   	variablesexpr _contract_indices_for_derivativer   r	   __new___indices_free_dum)clsr   r   argsindicesfreedumobjs   &&*     U/var/www/html/photoedit/myenv/lib/python3.14/site-packages/sympy/tensor/toperators.pyr   PartialDerivative.__new__`   sr     d-..2I99D#&#G#GdGY$  t s*T*	
    c                "    \         P                  # r   )r   Oneselfs   &r   coeffPartialDerivative.coeffq   s    uur    c                    V # r    r#   s   &r   nocoeffPartialDerivative.nocoeffu   s    r    c           
     v   . pV F  p\        V\        4      '       dD   VP                  4       pTP                  TP	                  V Uu/ uF  qfV) bK  	  up4      4       K\  \        V\
        4      '       g   Kt  VP                  V4       K  	  \        P                  ! V.V,           R R7      w  rxr\        ^\        V4      4       Fc  pWt,          p\        V\        4      '       g   K#  Wt,          P                  4       pWt,          P	                  V Uu/ uF  qfV) bK  	  up4      Wt&   Ke  	  WxW3# u upi u upi )T)replace_indices)
r   r   get_free_indicesappendxreplacer   r   _tensMul_contract_indicesrangelen)r   r   r   variables_opposite_valenceii_free_indiceskr   r   r   r   args_i	i_indicess   &&&          r   r   2PartialDerivative._contract_indices_for_derivativey   s   %'"A!V$$!"!3!3!5*11

>#B>arE>#BCEAv&&*11!4  $+#D#DF//$G t q#d)$AWF&&)) G446	'**9+E9arE9+EF	 % d'' $C ,Fs   D1D6
c                    V P                  V P                  V P                  4      w  r#rEV P                  ! V!  pW6n        WFn        WVn        V# r   )r   r   r   funcr   r   r   )r$   hintsr   r   r   r   r   s   &,     r   doitPartialDerivative.doit   sF    #'#H#HTXTbTb#c tii	
r    c           	        V P                  V P                  V P                  4      w  rr4V P                  ! V!  pW%n        W5n        WEn        TpV^ ,          P                  '       g   \        P                  # \        VP                  \        4      '       dk   VP                  P                  ! VP                  P                   Uu. uF/  pV P                  ! V.VP                  O5!  P                  4       NK1  	  up!  pV# \        VP                  \        4      '       Ed9   \        VP                  4      ^8X  d   . p\!        VP                  P                  4      p	\#        \        V	4      4       F  p
\        \%        W,          4      \&        4      '       d   K*  V P                  ! W,          .VP                  O5!  P                  4       pVP)                  \        V	RV
 V.,           W^,           R ,           !  4       K  	  \        P*                  ! V4      pV# VP                  pVP                   F"  pV P                  Wl4      P                  4       pK$  	  V# u upi )r   N)r   r   r   r;   r   r   r   free_symbolsr   Zeror   r
   r   _expand_partial_derivativer   r2   listr1   r   r   r.   fromiter)r$   r   r   r   r   r   resultatermsmulargsinddvs   &            r   rB   ,PartialDerivative._expand_partial_derivative   s   #'#H#HTXTbTb#c tii	Aw###66M'**XX]]#[[--%/- IIa0#--0KKM-%/ 0F8 3 '**3==!Q&sxx}}- W.C%ggl&;VDD !IIglCS]]C^^`Wwt}230518'1D0E &G H / !))%0  A!YYv1LLNF ' 9%/s   5Ic                    V P                   pV P                   Fa  p\        V\        4      '       d   VP	                  V4      pK,  VP
                  '       d   VP                  V4      pKQ  \        P                  pKc  	  V# r   )	r   r   r   r	   _eval_partial_derivative	_diff_wrt_eval_derivativer   rA   )r$   rE   rK   s   &  r   _perform_derivative%PartialDerivative._perform_derivative   s_    A&(++88;;;;#44Q7FVVF   r    c                    V P                   # r   )r   r#   s   &r   get_indicesPartialDerivative.get_indices   s    }}r    c                l    \        V P                  R  R7      pV Uu. uF  q"^ ,          NK  	  up# u upi )c                     V ^,          # )   r(   )xs   &r   <lambda>4PartialDerivative.get_free_indices.<locals>.<lambda>   s    !r    )key)sortedr   )r$   r   r4   s   &  r   r-   "PartialDerivative.get_free_indices   s.    djjn5"#d!d###s   1c                   V P                   P                  V4      pVP                  4        UUu/ uF
  w  r4V) V) bK  	  pppV P                   Uu. uF  qfP                  V4      NK  	  ppV P                  ! V.VO5!  # u uppi u upi r   )r   r/   itemsr   r;   )r$   replr   r6   rK   mirroredr4   r   s   &&      r   _replace_indices"PartialDerivative._replace_indices   su    yy!!$''+zz|4|tqQBF|437>>B>aZZ)>	Byy*	** 5Bs   A?Bc                (    V P                   ^ ,          # )r   r   r#   s   &r   r   PartialDerivative.expr   s    yy|r    c                (    V P                   R ,          # ):rX   NNrf   r#   s   &r   r   PartialDerivative.variables   s    yy}r    c           
        ^RI HpHp V P                  P	                  V4      w  rEV P
                   EF  pVP	                  V4      w  rxV U	u. uF  q) NK  	  pp	\        V U	u. uF  qP                  4       NK  	  up	!  w  r\        VP                  4      pV! WX4      p\        VP                  4      pW,
          p\        T\        V4       U	u. uF  qV,           NK  	  up	\        \        V4      4      ,           4      pVP                  4       pV^ ,          p^ .\        \        V4      4       U	u. uF  p	\        R4      NK  	  up	,           p\        V
4       F(  w  p	pW^ &   V\!        V4      ;;,          V,          uu&   K*  	  V) V9   d9   VP#                  V) 4      pV! V^ V^,           34      pVP%                  V4       EK  VP'                  V4       EK  	  WE3# u up	i u up	i u up	i u up	i )rX   )derive_by_arraytensorcontractionN)arrayrk   rl   r   _extract_datar   zipas_coeff_Mulr2   shaper   r1   rC   
as_mutableslice	enumeratetupleindexpopr.   )r$   replacement_dictrk   rl   r   rm   variablevar_indices	var_arrayr4   coeff_array
dim_before	dim_afterdim_increasevarindexcoeff_indexr%   poss   &&                r   rn   PartialDerivative._extract_data   s   =001ABH%-%;%;<L%M"K'23{!2{K3%(Y*OY>>+;Y*O%P"KU[[)J#E5EEKK(I$1L%
BS'TBSQL(8(8BS'TW[\abn\oWp'pqE$$&E"1~H#eCL6I J6It6I JJK%k25!"AeK()U2) 3 yG#mmXI.)%!SU<C x(+ ', ~) 4*O
 (U !Ks   G#G#
G(1G-r(   N)__name__
__module____qualname____firstlineno____doc__r   propertyr%   r)   classmethodr   r=   rB   rQ   rT   r-   rc   r   r   rn   __static_attributes____classdictcell__)__classdict__s   @r   r   r   	   s     Tl"     ( (,*X
$+     r    r   N)sympyr   sympy.core.numbersr   sympy.core.singletonr   sympy.core.symbolr   sympy.core.sympifyr   sympy.tensor.tensorr   r	   r
   r   r   r(   r    r   <module>r      s(     % " $ & B Bw wr    