§
    PŠtjˆ"  ã                   óv   — 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d	S )
é    )Ú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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                 óð   — t          |t          ¦  «        r|j        |z   }|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Úobjs           úU/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/sympy/tensor/toperators.pyr   zPartialDerivative.__new__`   s‡   € õ �dÕ-Ñ.Ô.ð 	Øœ¨Ñ2ˆIØ”9ˆDà#&×#GÒ#GÝˆd‰GŒG�Yñ$ ô $ Ñ ˆˆg�t˜Sõ Ô˜sÐ* TÐ*Ð*Ð*ˆàˆŒØˆŒ	ØˆŒØˆ
ó    c                 ó   — t           j        S r   )r   ÚOne©Úselfs    r   ÚcoeffzPartialDerivative.coeffq   s	   € åŒuˆr   c                 ó   — | S r   © r"   s    r   ÚnocoeffzPartialDerivative.nocoeffu   s   € àˆr   c                 ód  — g }|D ]ˆ}t          |t          ¦  «        rG|                     ¦   «         }|                     |                     d„ |D ¦   «         ¦  «        ¦  «         Œ^t          |t
          ¦  «        r|                     |¦  «         Œ‰t          j        |g|z   d¬¦  «        \  }}}}	t          dt          |¦  «        ¦  «        D ]a}||         }
t          |
t          ¦  «        rB||                              ¦   «         }||                              d„ |D ¦   «         ¦  «        ||<   Œb||||	fS )Nc                 ó   — i | ]}|| “ŒS r&   r&   ©Ú.0Úks     r   ú
<dictcomp>zFPartialDerivative._contract_indices_for_derivative.<locals>.<dictcomp>�   s   € Ð#BÐ#BÐ#B¨a A¨ rÐ#BÐ#BÐ#Br   T)Úreplace_indicesé   c                 ó   — i | ]}|| “ŒS r&   r&   r*   s     r   r-   zFPartialDerivative._contract_indices_for_derivative.<locals>.<dictcomp>Œ   s   € Ð+EÐ+EÐ+E°a¨A°¨rÐ+EÐ+EÐ+Er   )
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_indicess               r   r   z2PartialDerivative._contract_indices_for_derivativey   s^  € à%'Ð"àð 	5ð 	5ˆAÝ˜!�VÑ$Ô$ð 5Ø!"×!3Ò!3Ñ!5Ô!5�Ø*×1Ò1ØŸ
š
Ð#BÐ#B°>Ð#BÑ#BÔ#BÑCÔCñEô Eð Eð Eå˜A�vÑ&Ô&ð 5Ø*×1Ò1°!Ñ4Ô4Ð4øå#*Ô#DØˆFÐ/Ñ/Àð$Gñ $Gô $GÑ ˆˆg�t˜Sõ �q�#˜d™)œ)Ñ$Ô$ð 	Gð 	GˆAØ˜!”WˆFÝ˜&¥&Ñ)Ô)ð GØ  œG×4Ò4Ñ6Ô6�	Ø˜qœ'×*Ò*Ð+EÐ+E¸9Ð+EÑ+EÔ+EÑFÔF��Q‘øà�W˜d CÐ'Ð'r   c                 óŽ   — |                       | j        | j        ¦  «        \  }}}} | j        |Ž }||_        ||_        ||_        |S r   )r   r   r   Úfuncr   r   r   )r#   Úhintsr   r   r   r   r   s          r   ÚdoitzPartialDerivative.doit�   sN   € Ø#'×#HÒ#HÈÌÐTXÔTbÑ#cÔ#cÑ ˆˆg�t˜SàˆdŒi˜ÐˆØˆŒØˆŒ	ØˆŒàˆ
r   c           	      óÔ  ‡ ‡— ‰                       ‰ j        ‰ j        ¦  «        \  }}}} ‰ j        |Ž Š|‰_        |‰_        |‰_        ‰}|d         j        st          j	        S t          ‰j        t          ¦  «        r( ‰j        j        ˆˆ fd„|j        j        D ¦   «         Ž }�nDt          ‰j        t          ¦  «        �r)t          ‰j        ¦  «        dk    rØg }t          ‰j        j        ¦  «        }t!          t          |¦  «        ¦  «        D ]‹}t          t#          ||         ¦  «        t$          ¦  «        sa ‰ j        ||         g‰j        ¢R Ž                      ¦   «         }	|                     t          |d |…         |	gz   ||dz   d …         z   Ž ¦  «         ŒŒt          j        |¦  «        }n9‰j        }‰j        D ]*}
‰                      ||
¦  «                             ¦   «         }Œ+|S )Nr   c                 óZ   •— g | ]'} ‰j         |g‰j        ¢R Ž                      ¦   «         ‘Œ(S r&   )r=   r   Ú_expand_partial_derivative)r+   Úar   r#   s     €€r   ú
<listcomp>z@PartialDerivative._expand_partial_derivative.<locals>.<listcomp>¨   sK   ø€ ð %/ð %/ð %/àð �D”I˜aÐ0 #¤-Ð0Ð0Ð0×KÒKÑMÔMð%/ð %/ð %/r   r/   )r   r   r   r=   r   r   r   Úfree_symbolsr   ÚZeror   r
   r   r   r6   Úlistr5   r   r   rB   r2   Úfromiter)r#   r   r   r   r   ÚresultÚtermsÚmulargsÚindÚdÚvr   s   `          @r   rB   z,PartialDerivative._expand_partial_derivativeš   s  øø€ Ø#'×#HÒ#HÈÌÐTXÔTbÑ#cÔ#cÑ ˆˆg�t˜SàˆdŒi˜ÐˆØˆŒØˆŒ	ØˆŒàˆà�AŒwÔ#ð 	OÝ”6ˆMÝ˜œ¥'Ñ*Ô*ð 	Oà"�S”X”]ð %/ð %/ð %/ð %/ð %/à#œ[Ô-ð%/ñ %/ô %/ð 0ˆF‰Fõ ˜œ¥'Ñ*Ô*ñ 	Oå�3”=Ñ!Ô! QÒ&Ð&à�Ý˜sœxœ}Ñ-Ô-�Ý ¥ W¡¤Ñ.Ô.ð Hð H�CÝ%¥g¨g°c¬lÑ&;Ô&;½VÑDÔDð Hð &˜DœI g¨c¤lÐC°S´]ÐCÐCÐC×^Ò^Ñ`Ô`˜ØŸš¥W¨w°t¸°t¬}Ø23°ñ05à18¸#À¹'¸¸Ô1Dñ0Eð &Gñ Hô Hð Høõ !Ô)¨%Ñ0Ô0��ð œ�Øœð Oð O�AØ!ŸYšY v¨qÑ1Ô1×LÒLÑNÔN�F�Fð ˆr   c                 óÐ   — | j         }| j        D ]V}t          |t          ¦  «        r|                     |¦  «        }Œ-|j        r|                     |¦  «        }ŒJt          j        }ŒW|S r   )	r   r   r   r	   Ú_eval_partial_derivativeÚ	_diff_wrtÚ_eval_derivativer   rF   )r#   rI   rN   s      r   Ú_perform_derivativez%PartialDerivative._perform_derivativeÆ   sn   € Ø”ˆØ”ð 	$ð 	$ˆAÝ˜&¥(Ñ+Ô+ð $Ø×8Ò8¸Ñ;Ô;��à”;ð $Ø#×4Ò4°QÑ7Ô7�F�FåœV�F�FØˆr   c                 ó   — | j         S r   )r   r"   s    r   Úget_indiceszPartialDerivative.get_indicesÒ   s
   € ØŒ}Ðr   c                 óH   — t          | j        d„ ¬¦  «        }d„ |D ¦   «         S )Nc                 ó   — | d         S ©Nr/   r&   )Úxs    r   ú<lambda>z4PartialDerivative.get_free_indices.<locals>.<lambda>Ö   s
   € °°!´€ r   )Úkeyc                 ó   — g | ]
}|d          ‘ŒS )r   r&   ©r+   r8   s     r   rD   z6PartialDerivative.get_free_indices.<locals>.<listcomp>×   s   € Ð#Ð#Ð#˜��!”Ð#Ð#Ð#r   )Úsortedr   )r#   r   s     r   r1   z"PartialDerivative.get_free_indicesÕ   s,   € Ý�d”j n nÐ5Ñ5Ô5ˆØ#Ð#˜dÐ#Ñ#Ô#Ð#r   c                 ó¶   ‡— | j                              |¦  «        }d„ |                     ¦   «         D ¦   «         Šˆfd„| j        D ¦   «         } | j        |g|¢R Ž S )Nc                 ó   — i | ]
\  }}| | “ŒS r&   r&   )r+   r,   rN   s      r   r-   z6PartialDerivative._replace_indices.<locals>.<dictcomp>Û   s"   € Ð4Ð4Ð4™t˜q !�Q�B˜˜Ð4Ð4Ð4r   c                 ó:   •— g | ]}|                      ‰¦  «        ‘ŒS r&   )r3   )r+   r8   Úmirroreds     €r   rD   z6PartialDerivative._replace_indices.<locals>.<listcomp>Ü   s%   ø€ ÐBÐBÐB¨a�Q—Z’Z Ñ)Ô)ÐBÐBÐBr   )r   r3   Úitemsr   r=   )r#   Úreplr   r   rb   s       @r   Ú_replace_indicesz"PartialDerivative._replace_indicesÙ   sh   ø€ ØŒy×!Ò! $Ñ'Ô'ˆØ4Ð4 t§z¢z¡|¤|Ð4Ñ4Ô4ˆØBÐBÐBÐB°4´>ÐBÑBÔBˆ	ØˆtŒy˜Ð* 	Ð*Ð*Ð*Ð*r   c                 ó   — | j         d         S )Nr   ©r   r"   s    r   r   zPartialDerivative.exprß   s   € àŒy˜Œ|Ðr   c                 ó    — | j         dd …         S rX   rg   r"   s    r   r   zPartialDerivative.variablesã   s   € àŒy˜˜˜Œ}Ðr   c           
      óŠ  ‡— ddl m}m} | j                             |¦  «        \  }}| j        D �]‘}|                     |¦  «        \  }}d„ |D ¦   «         }t          d„ |D ¦   «         Ž \  }	}t          |j        ¦  «        }
 |||¦  «        }t          |j        ¦  «        }||
z
  Št          |ˆfd„t          |
¦  «        D ¦   «         t          t          ‰¦  «        ¦  «        z   ¦  «        }|                     ¦   «         }|d         }dgd„ t          t          |¦  «        ¦  «        D ¦   «         z   }t          |	¦  «        D ]'\  }}||d<   |t          |¦  «        xx         |z  cc<   Œ(| |v r>|                     | ¦  «        } ||d|dz   f¦  «        }|                     |¦  «         �Œ||                     |¦  «         �Œ“||fS )Nr/   )Úderive_by_arrayÚtensorcontractionc                 ó   — g | ]}| ‘ŒS r&   r&   r]   s     r   rD   z3PartialDerivative._extract_data.<locals>.<listcomp>ì   s   € Ð3Ð3Ð3 !˜A˜2Ð3Ð3Ð3r   c                 ó6   — g | ]}|                      ¦   «         ‘ŒS r&   )Úas_coeff_Mulr]   s     r   rD   z3PartialDerivative._extract_data.<locals>.<listcomp>í   s"   € Ð*OÐ*OÐ*OÀ¨1¯>ª>Ñ+;Ô+;Ð*OÐ*OÐ*Or   c                 ó   •— g | ]}|‰z   ‘ŒS r&   r&   )r+   r8   Údim_increases     €r   rD   z3PartialDerivative._extract_data.<locals>.<listcomp>ò   s   ø€ Ð'TÐ'TÐ'T¸Q¨¨LÑ(8Ð'TÐ'TÐ'Tr   r   c                 ó,   — g | ]}t          d ¦  «        ‘ŒS r   )Úslicer]   s     r   rD   z3PartialDerivative._extract_data.<locals>.<listcomp>ö   s   € Ð JÐ JÐ J°¥ t¡¤Ð JÐ JÐ Jr   )Úarrayrj   rk   r   Ú_extract_datar   Úzipr6   Úshaper   r5   rG   Ú
as_mutableÚ	enumerateÚtupleÚindexÚpopr2   )r#   Úreplacement_dictrj   rk   r   rs   ÚvariableÚvar_indicesÚ	var_arrayÚcoeff_arrayÚ
dim_beforeÚ	dim_afterÚvarindexÚcoeff_indexr8   r$   Úposrp   s                    @r   rt   zPartialDerivative._extract_dataç   s	  ø€ Ø=Ð=Ð=Ð=Ð=Ð=Ð=Ð=Øœ×0Ò0Ð1AÑBÔB‰ˆ�Øœð 	)ñ 	)ˆHØ%-×%;Ò%;Ð<LÑ%MÔ%MÑ"ˆK˜Ø3Ð3 {Ð3Ñ3Ô3ˆKÝ%(Ð*OÐ*OÀYÐ*OÑ*OÔ*OÐ%PÑ"ˆK˜Ý˜Uœ[Ñ)Ô)ˆJØ#�O E¨9Ñ5Ô5ˆEÝ˜EœKÑ(Ô(ˆIØ$ zÑ1ˆLÝ Ð'TÐ'TÐ'TÐ'TÅ%È
ÑBSÔBSÐ'TÑ'TÔ'TÕW[Õ\aÐbnÑ\oÔ\oÑWpÔWpÑ'pÑqÔqˆEØ×$Ò$Ñ&Ô&ˆEØ" 1”~ˆHà˜#Ð JÐ Jµe½CÀ¹L¼LÑ6IÔ6IÐ JÑ JÔ JÑJˆKÝ% kÑ2Ô2ð 3ð 3‘��5Ø!"�˜A‘Ø•e˜KÑ(Ô(Ð)Ð)Ô)¨UÑ2Ð)Ð)Ñ)Ð)Øˆy˜GÐ#Ð#Ø—m’m X IÑ.Ô.�Ø)Ð)¨%°!°S¸±U°Ñ<Ô<�Ø—’˜CÑ Ô Ð Ñ à—’˜xÑ(Ô(Ð(Ñ(Ø˜ˆ~Ðr   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr$   r'   Úclassmethodr   r?   rB   rS   rU   r1   re   r   r   rt   r&   r   r   r   r   	   s!  € € € € € ðTð Tðlð ð ð" ðð ñ „Xðð ðð ñ „Xðð ð(ð (ñ „[ð(ð,ð ð ð*ð *ð *ðX
ð 
ð 
ðð ð ð$ð $ð $ð+ð +ð +ð ðð ñ „Xðð ðð ñ „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Ð BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ Bðwð wð wð wð w˜ñ wô wð wð wð wr   