o
    §ƒðjˆ"  ã                   @   sh   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„ deƒZd	S )
é    )Úpermutedims)ÚNumber)ÚS)ÚSymbol)Úsympify)ÚTensorÚTensExprÚTensAddÚTensMulc                   @   sŒ   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZedd	„ ƒZ	d
d„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZedd„ ƒZdd„ ZdS )Ú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                 G   sZ   t |tƒr|j| }|j}|  t|ƒ|¡\}}}}tj| g|¢R Ž }||_||_	||_
|S ©N)Ú
isinstancer   Ú	variablesÚexprÚ _contract_indices_for_derivativer   r   Ú__new__Ú_indicesÚ_freeÚ_dum)Úclsr   r   ÚargsÚindicesÚfreeÚdumÚobj© r   ú™/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/tensor/toperators.pyr   `   s   

ÿzPartialDerivative.__new__c                 C   s   t jS r   )r   ÚOne©Úselfr   r   r   Úcoeffq   s   zPartialDerivative.coeffc                 C   s   | S r   r   r   r   r   r   Únocoeffu   s   zPartialDerivative.nocoeffc                 C   sÆ   g }|D ]#}t |tƒr| ¡ }| | dd„ |D ƒ¡¡ qt |tƒr'| |¡ qtj|g| dd�\}}}}	tdt	|ƒƒD ]}|| }
t |
tƒr\||  ¡ }||  dd„ |D ƒ¡||< q=||||	fS )Nc                 S   ó   i | ]}|| “qS r   r   ©Ú.0Úkr   r   r   Ú
<dictcomp>�   ó    zFPartialDerivative._contract_indices_for_derivative.<locals>.<dictcomp>T)Úreplace_indicesé   c                 S   r"   r   r   r#   r   r   r   r&   Œ   r'   )
r   r   Úget_free_indicesÚappendÚxreplacer   r
   Ú_tensMul_contract_indicesÚrangeÚlen)r   r   r   Úvariables_opposite_valenceÚiÚi_free_indicesr   r   r   r   Úargs_iÚ	i_indicesr   r   r   r   y   s(   
ÿ

€
ÿ
€z2PartialDerivative._contract_indices_for_derivativec                 K   s8   |   | j| j¡\}}}}| j|Ž }||_||_||_|S r   )r   r   r   Úfuncr   r   r   )r   Úhintsr   r   r   r   r   r   r   r   Údoit�   s   
zPartialDerivative.doitc                    sB  ˆ  ˆjˆj¡\}}}}ˆj|Ž ‰ |ˆ _|ˆ _|ˆ _ˆ }|d js$tj	S t
ˆ jtƒr<ˆ jj‡ ‡fdd„|jjD ƒŽ }|S t
ˆ jtƒrŸtˆ jƒdkrŽg }tˆ jjƒ}tt|ƒƒD ]/}t
t|| ƒtƒs†ˆj|| gˆ j¢R Ž  ¡ }	| t|d |… |	g ||d d …  Ž ¡ qWt |¡}|S ˆ j}ˆ jD ]
}
ˆ ||
¡ ¡ }q”|S )Nr   c                    s$   g | ]}ˆj |gˆ j¢R Ž  ¡ ‘qS r   )r5   r   Ú_expand_partial_derivative)r$   Úa©r   r   r   r   Ú
<listcomp>¨   s    ÿÿz@PartialDerivative._expand_partial_derivative.<locals>.<listcomp>r)   )r   r   r   r5   r   r   r   Úfree_symbolsr   ÚZeror   r	   r   r
   r/   Úlistr.   r   r   r8   r+   Úfromiter)r   r   r   r   r   ÚresultÚtermsÚmulargsÚindÚdÚvr   r:   r   r8   š   s@   

þçÿþ€
û
z,PartialDerivative._expand_partial_derivativec                 C   sD   | j }| jD ]}t|tƒr| |¡}q|jr| |¡}qtj}q|S r   )	r   r   r   r   Ú_eval_partial_derivativeÚ	_diff_wrtÚ_eval_derivativer   r=   )r   r@   rE   r   r   r   Ú_perform_derivativeÆ   s   

z%PartialDerivative._perform_derivativec                 C   s   | j S r   )r   r   r   r   r   Úget_indicesÒ   s   zPartialDerivative.get_indicesc                 C   s    t | jdd„ d�}dd„ |D ƒS )Nc                 S   s   | d S ©Nr)   r   )Úxr   r   r   Ú<lambda>Ö   s    z4PartialDerivative.get_free_indices.<locals>.<lambda>)Úkeyc                 S   s   g | ]}|d  ‘qS )r   r   ©r$   r1   r   r   r   r;   ×   r'   z6PartialDerivative.get_free_indices.<locals>.<listcomp>)Úsortedr   )r   r   r   r   r   r*   Õ   s   z"PartialDerivative.get_free_indicesc                    sD   | j  |¡}dd„ | ¡ D ƒ‰ ‡ fdd„| jD ƒ}| j|g|¢R Ž S )Nc                 S   s   i | ]	\}}| | “qS r   r   )r$   r%   rE   r   r   r   r&   Û   s    z6PartialDerivative._replace_indices.<locals>.<dictcomp>c                    s   g | ]}|  ˆ ¡‘qS r   )r,   rO   ©Úmirroredr   r   r;   Ü   s    z6PartialDerivative._replace_indices.<locals>.<listcomp>)r   r,   Úitemsr   r5   )r   Úreplr   r   r   rQ   r   Ú_replace_indicesÙ   s   z"PartialDerivative._replace_indicesc                 C   s
   | j d S )Nr   ©r   r   r   r   r   r   ß   s   
zPartialDerivative.exprc                 C   s   | j dd … S rK   rV   r   r   r   r   r   ã   s   zPartialDerivative.variablesc                    sL  ddl m}m} | j |¡\}}| jD ]Ž}| |¡\}}dd„ |D ƒ}tdd„ |D ƒŽ \}	}t|jƒ}
|||ƒ}t|jƒ}||
 ‰ t	|‡ fdd„t
|
ƒD ƒtt
ˆ ƒƒ ƒ}| ¡ }|d }dgdd„ t
t|ƒƒD ƒ }t|	ƒD ]\}}||d< |t|ƒ  |  < qo| |v rœ| | ¡}||d|d fƒ}| |¡ q| |¡ q||fS )	Nr)   )Úderive_by_arrayÚtensorcontractionc                 S   s   g | ]}| ‘qS r   r   rO   r   r   r   r;   ì   s    z3PartialDerivative._extract_data.<locals>.<listcomp>c                 S   s   g | ]}|  ¡ ‘qS r   )Úas_coeff_MulrO   r   r   r   r;   í   r'   c                    s   g | ]}|ˆ  ‘qS r   r   rO   ©Údim_increaser   r   r;   ò   r'   r   c                 S   s   g | ]}t d ƒ‘qS r   )ÚslicerO   r   r   r   r;   ö   r'   )ÚarrayrW   rX   r   Ú_extract_datar   Úzipr/   Úshaper   r.   r>   Ú
as_mutableÚ	enumerateÚtupleÚindexÚpopr+   )r   Úreplacement_dictrW   rX   r   r]   ÚvariableÚvar_indicesÚ	var_arrayÚcoeff_arrayÚ
dim_beforeÚ	dim_afterÚvarindexÚcoeff_indexr1   r    Úposr   rZ   r   r^   ç   s.   



(
zPartialDerivative._extract_dataN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr    r!   Úclassmethodr   r7   r8   rI   rJ   r*   rU   r   r   r^   r   r   r   r   r   	   s(    V



,

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   r   Ú<module>   s    