Ë
    7^(hˆ"  ã                   ón   — d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZmZmZmZ  G d„ de«      Zy	)
é    )Úpermutedims)ÚNumber)ÚS)ÚSymbol)Úsympify)ÚTensorÚTensExprÚTensAddÚTensMulc                   ó�   — e Zd ZdZd„ Zed„ «       Zed„ «       Zed„ «       Z	d„ Z
d„ Zd„ Zd	„ Zd
„ Zd„ Zed„ «       Zed„ «       Zd„ Zy)Ú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                 óò   — t        |t        «      r|j                  |z   }|j                  }| j	                  t        |«      |«      \  }}}}t        j                  | g|¢­Ž }||_        ||_	        ||_
        |S ©N)Ú
isinstancer   Ú	variablesÚexprÚ _contract_indices_for_derivativer   r	   Ú__new__Ú_indicesÚ_freeÚ_dum)Úclsr   r   ÚargsÚindicesÚfreeÚdumÚobjs           úU/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sympy/tensor/toperators.pyr   zPartialDerivative.__new__`   sy   € ô �dÔ-Ô.ØŸ™¨Ñ2ˆIØ—9‘9ˆDà#&×#GÑ#GÜˆd‹G�Yó$ Ñ ˆˆg�t˜Sô ×Ñ˜sÐ* TÒ*ˆàˆŒØˆŒ	ØˆŒØˆ
ó    c                 ó"   — t         j                  S r   )r   ÚOne©Úselfs    r   ÚcoeffzPartialDerivative.coeffq   s   € ä�u‰uˆr   c                 ó   — | S r   © r"   s    r   ÚnocoeffzPartialDerivative.nocoeffu   s   € àˆr   c           
      ó.  — g }|D ]u  }t        |t        «      rA|j                  «       }|j                  |j	                  |D �ci c]  }|| “Œ c}«      «       ŒTt        |t
        «      sŒe|j                  |«       Œw t        j                  |g|z   d¬«      \  }}}	}
t        dt        |«      «      D ]R  }||   }t        |t        «      sŒ||   j                  «       }||   j	                  |D �ci c]  }|| “Œ c}«      ||<   ŒT |||	|
fS c c}w c c}w )NT)Ú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   z2PartialDerivative._contract_indices_for_derivativey   s%  € à%'Ð"àò 	5ˆAÜ˜!œVÔ$Ø!"×!3Ñ!3Ó!5�Ø*×1Ñ1ØŸ
™
°>Ö#B¨a A¨ r¡EÒ#BÓCõEä˜AœvÕ&Ø*×1Ñ1°!Õ4ð	5ô $+×#DÑ#DØˆFÐ/Ñ/Àô$GÑ ˆˆg�t˜Sô �qœ#˜d›)Ó$ò 	GˆAØ˜!‘WˆFÜ˜&¤&Õ)Ø  ™G×4Ñ4Ó6�	Ø˜q™'×*Ñ*¸9Ö+E°a¨A°¨r©EÒ+EÓF��Q’ð		Gð �W˜d CÐ'Ð'ùò $Cùò ,Fs   ÁDÃ0D
c                 ó¤   — | j                  | j                  | j                  «      \  }}}} | j                  |Ž }||_        ||_        ||_        |S r   )r   r   r   Úfuncr   r   r   )r#   Úhintsr   r   r   r   r   s          r   ÚdoitzPartialDerivative.doit�   sR   € Ø#'×#HÑ#HÈÏÉÐTX×TbÑTbÓ#cÑ ˆˆg�t˜Sàˆd�i‰i˜ÐˆØˆŒØˆŒ	ØˆŒàˆ
r   c           	      óV  — | j                  | j                  | j                  «      \  }}}} | j                  |Ž }||_        ||_        ||_        |}|d   j                  st        j                  S t        |j                  t        «      rf |j                  j                  |j                  j                  D �cg c]-  } | j                  |g|j                  ¢­Ž j                  «       ‘Œ/ c}Ž }|S t        |j                  t        «      �rt        |j                  «      dk(  rÃg }t!        |j                  j                  «      }	t#        t        |	«      «      D ]t  }
t        t%        |	|
   «      t&        «      rŒ  | j                  |	|
   g|j                  ¢­Ž j                  «       }|j)                  t        |	d |
 |gz   |	|
dz   d  z   Ž «       Œv t        j*                  |«      }|S |j                  }|j                  D ]"  }| j                  ||«      j                  «       }Œ$ |S c c}w )Nr   r*   )r   r   r   r8   r   r   r   Úfree_symbolsr   ÚZeror   r
   r   Ú_expand_partial_derivativer   r0   Úlistr/   r   r   r,   Úfromiter)r#   r   r   r   r   r   ÚresultÚaÚtermsÚmulargsÚindÚdÚvs                r   r>   z,PartialDerivative._expand_partial_derivativeš   sô  € Ø#'×#HÑ#HÈÏÉÐTX×TbÑTbÓ#cÑ ˆˆg�t˜Sàˆd�i‰i˜ÐˆØˆŒØˆŒ	ØˆŒàˆà�A‰w×#Ò#Ü—6‘6ˆMÜ˜Ÿ™¤'Ô*à"�S—X‘X—]‘]à#Ÿ[™[×-Ñ-ö%/àð �D—I‘I˜aÐ0 #§-¡-Ò0×KÑKÕMò%/ð 0ˆFð8 ˆô3 ˜Ÿ™¤'Õ*ä�3—=‘=Ó! QÒ&à�Ü˜sŸx™xŸ}™}Ó-�Ü ¤ W£Ó.ò H�CÜ%¤g¨g°c©lÓ&;¼VÕDð &˜DŸI™I g¨c¡lÐC°S·]±]ÒC×^Ñ^Ó`˜ØŸ™¤W¨w°t¸¨}Ø23°ñ05à18¸#À¹'¸Ð1Dñ0Eð &Gõ HðHô !×)Ñ)¨%Ó0�ð ˆð Ÿ™�ØŸ™ò O�AØ!ŸY™Y v¨qÓ1×LÑLÓN‘FðOð ˆùò9%/s   Â92H&c                 óà   — | j                   }| j                  D ]R  }t        |t        «      r|j	                  |«      }Œ%|j
                  r|j                  |«      }ŒCt        j                  }ŒT |S r   )	r   r   r   r	   Ú_eval_partial_derivativeÚ	_diff_wrtÚ_eval_derivativer   r=   )r#   rA   rG   s      r   Ú_perform_derivativez%PartialDerivative._perform_derivativeÆ   s_   € Ø—‘ˆØ—‘ò 	$ˆAÜ˜&¤(Ô+Ø×8Ñ8¸Ó;‘à—;’;Ø#×4Ñ4°QÓ7‘FäŸV™V‘Fð	$ð ˆr   c                 ó   — | j                   S r   )r   r"   s    r   Úget_indiceszPartialDerivative.get_indicesÒ   s   € Ø�}‰}Ðr   c                 ób   — t        | j                  d„ ¬«      }|D �cg c]  }|d   ‘Œ	 c}S c c}w )Nc                 ó   — | d   S ©Nr*   r&   )Úxs    r   ú<lambda>z4PartialDerivative.get_free_indices.<locals>.<lambda>Ö   s
   € °°!±€ r   )Úkeyr   )Úsortedr   )r#   r   r2   s      r   r+   z"PartialDerivative.get_free_indicesÕ   s*   € Ü�d—j‘j¡nÔ5ˆØ"Ö#˜��!“Ò#Ð#ùÒ#s   �,c                 ó  — | j                   j                  |«      }|j                  «       D ��ci c]
  \  }}| | “Œ }}}| j                  D �cg c]  }|j                  |«      ‘Œ }} | j                  |g|¢­Ž S c c}}w c c}w r   )r   r-   Úitemsr   r8   )r#   Úreplr   r4   rG   Úmirroredr2   r   s           r   Ú_replace_indicesz"PartialDerivative._replace_indicesÙ   sy   € Ø�y‰y×!Ñ! $Ó'ˆØ'+§z¡z£|×4™t˜q !�Q�B˜˜‘FÐ4ˆÑ4Ø37·>±>ÖB¨a�Q—Z‘Z Õ)ÐBˆ	ÐBØˆt�y‰y˜Ð* 	Ò*Ð*ùó 5ùÚBs   ¯A<ÁBc                 ó    — | j                   d   S )Nr   ©r   r"   s    r   r   zPartialDerivative.exprß   s   € à�y‰y˜‰|Ðr   c                 ó    — | j                   dd  S rQ   r\   r"   s    r   r   zPartialDerivative.variablesã   s   € à�y‰y˜˜ˆ}Ðr   c           
      ó–  — ddl m}m} | j                  j	                  |«      \  }}| j
                  D �]|  }|j	                  |«      \  }}|D �	cg c]  }	|	 ‘Œ }}	t        |D �	cg c]  }	|	j                  «       ‘Œ c}	Ž \  }
}t        |j                  «      } |||«      }t        |j                  «      }||z
  }t        |t        |«      D �	cg c]  }	|	|z   ‘Œ	 c}	t        t        |«      «      z   «      }|j                  «       }|d   }dgt        t        |«      «      D �	cg c]  }	t        d «      ‘Œ c}	z   }t        |
«      D ]   \  }	}|	|d<   |t!        |«      xx   |z  cc<   Œ" | |v r3|j#                  | «      } ||d|dz   f«      }|j%                  |«       �Œl|j'                  |«       �Œ ||fS c c}	w c c}	w c c}	w c c}	w )Nr*   )Úderive_by_arrayÚtensorcontractionr   )Úarrayr_   r`   r   Ú_extract_datar   ÚzipÚas_coeff_Mulr0   Úshaper   r/   r?   Ú
as_mutableÚsliceÚ	enumerateÚtupleÚindexÚpopr,   )r#   Úreplacement_dictr_   r`   r   ra   ÚvariableÚvar_indicesÚ	var_arrayr2   Úcoeff_arrayÚ
dim_beforeÚ	dim_afterÚdim_increaseÚvarindexÚcoeff_indexr$   Úposs                     r   rb   zPartialDerivative._extract_dataç   s»  € ß=ØŸ™×0Ñ0Ð1AÓB‰ˆ�ØŸ™ó 	)ˆHØ%-×%;Ñ%;Ð<LÓ%MÑ"ˆK˜Ø'2Ö3 !˜Aš2Ð3ˆKÐ3Ü%(ÀYÖ*OÀ¨1¯>©>Õ+;Ò*OÐ%PÑ"ˆK˜Ü˜UŸ[™[Ó)ˆJÙ# E¨9Ó5ˆEÜ˜EŸK™KÓ(ˆIØ$ zÑ1ˆLÜ Ä%È
ÓBSÖ'T¸Q¨¨LÓ(8Ò'TÔW[Ô\aÐbnÓ\oÓWpÑ'pÓqˆEØ×$Ñ$Ó&ˆEØ" 1‘~ˆHà˜#´e¼CÀ»LÓ6IÖ J°¤ t¥Ò JÑJˆKÜ% kÓ2ò 3‘��5Ø!"�˜A‘Ø”e˜KÓ(Ó)¨UÑ2Ô)ð3ð ˆy˜GÑ#Ø—m‘m X IÓ.�Ù)¨%°!°S¸±U°Ó<�Ø—‘˜CÖ à—‘˜xÖ(ð+	)ð, ˜ˆ~Ðùò) 4ùÚ*Oùò
 (Uùò !Ks   Á
F7Á$F<
ÃGÄ"GN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr$   r'   Úclassmethodr   r:   r>   rL   rN   r+   rZ   r   r   rb   r&   r   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   