o
    §ƒðjk%  ã                   @   sî   d dl Z d dlmZ d dlmZmZmZ d dlmZ d dl	m
Z
mZmZmZmZ d dlmZ d dlmZ d dlmZ d	d
„ Zdd„ ZG dd„ deƒZG dd„ deƒZG dd„ deƒZddd„Zddd„Zddd„ZG dd„ deƒZdd„ ZdS )é    N)ÚExpr)ÚsympifyÚSÚpreorder_traversal)Ú
CoordSys3D)ÚVectorÚ	VectorMulÚ	VectorAddÚCrossÚDot)Ú
Derivative)ÚAdd)ÚMulc                 C   s<   t | ƒ}tƒ }|D ]}t|tƒr| |¡ | ¡  q	t|ƒS ©N)r   ÚsetÚ
isinstancer   ÚaddÚskipÚ	frozenset)ÚexprÚgÚretÚi© r   ú˜/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/vector/operators.pyÚ_get_coord_systems   s   

€r   c                 C   s:   t  dd„ ¡}| jD ]}|t|ƒ  |9  < q
t| ¡ ƒS )Nc                   S   s   t jS r   )r   ÚOner   r   r   r   Ú<lambda>   s    z._split_mul_args_wrt_coordsys.<locals>.<lambda>)ÚcollectionsÚdefaultdictÚargsr   ÚlistÚvalues)r   Údr   r   r   r   Ú_split_mul_args_wrt_coordsys   s   
r$   c                   @   ó    e Zd ZdZdd„ Zdd„ ZdS )ÚGradientzß
    Represents unevaluated Gradient.

    Examples
    ========

    >>> from sympy.vector import CoordSys3D, Gradient
    >>> R = CoordSys3D('R')
    >>> s = R.x*R.y*R.z
    >>> Gradient(s)
    Gradient(R.x*R.y*R.z)

    c                 C   ó   t |ƒ}t | |¡}||_|S r   ©r   r   Ú__new__Ú_expr©Úclsr   Úobjr   r   r   r)   +   ó   zGradient.__new__c                 K   ó   t | jdd�S ©NT©Údoit)Úgradientr*   ©ÚselfÚhintsr   r   r   r2   1   ó   zGradient.doitN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r)   r2   r   r   r   r   r&      ó    r&   c                   @   r%   )Ú
Divergencea  
    Represents unevaluated Divergence.

    Examples
    ========

    >>> from sympy.vector import CoordSys3D, Divergence
    >>> R = CoordSys3D('R')
    >>> v = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
    >>> Divergence(v)
    Divergence(R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k)

    c                 C   r'   r   r(   r+   r   r   r   r)   D   r.   zDivergence.__new__c                 K   r/   r0   )Ú
divergencer*   r4   r   r   r   r2   J   r7   zDivergence.doitNr8   r   r   r   r   r>   5   r=   r>   c                   @   r%   )ÚCurla  
    Represents unevaluated Curl.

    Examples
    ========

    >>> from sympy.vector import CoordSys3D, Curl
    >>> R = CoordSys3D('R')
    >>> v = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
    >>> Curl(v)
    Curl(R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k)

    c                 C   r'   r   r(   r+   r   r   r   r)   ]   r.   zCurl.__new__c                 K   r/   r0   )Úcurlr*   r4   r   r   r   r2   c   r7   z	Curl.doitNr8   r   r   r   r   r@   N   r=   r@   Tc                    s  t | ƒ}t|ƒdkrtjS t|ƒdkrŠtt|ƒƒ}| ¡ \}}}| ¡ \}}}| ¡ \}	}
}|  	|¡}|  	|¡}|  	|¡}tj}|t
|| |ƒt
||
 |ƒ | |
|  7 }|t
||	 |ƒt
|| |ƒ | |	|  7 }|t
||
 |ƒt
||	 |ƒ | |
|	  7 }ˆrˆ| ¡ S |S t| ttfƒrÃddlm‰ ztt|ƒƒ‰ ‡ ‡fdd„| jD ƒ}W n ty¶   | j}Y nw t ‡fdd„|D ƒ¡S t| ttfƒrødd„ | jD ƒd }t d	d„ | jD ƒ¡}tt|ƒ|ƒ ¡ |t|ˆd
�  }ˆrö| ¡ S |S t| tttfƒ�rt| ƒS tdƒ‚)ao  
    Returns the curl of a vector field computed wrt the base scalars
    of the given coordinate system.

    Parameters
    ==========

    vect : Vector
        The vector operand

    doit : bool
        If True, the result is returned after calling .doit() on
        each component. Else, the returned expression contains
        Derivative instances

    Examples
    ========

    >>> from sympy.vector import CoordSys3D, curl
    >>> R = CoordSys3D('R')
    >>> v1 = R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
    >>> curl(v1)
    0
    >>> v2 = R.x*R.y*R.z*R.i
    >>> curl(v2)
    R.x*R.y*R.j + (-R.x*R.z)*R.k

    r   é   ©Úexpressc                    s   g | ]	}ˆ|ˆ d d�‘qS )T©Ú	variablesr   ©Ú.0r   )ÚcsrD   r   r   Ú
<listcomp>¡   s    zcurl.<locals>.<listcomp>c                 3   ó   � | ]	}t |ˆ d �V  qdS ©r1   N)rA   rG   r1   r   r   Ú	<genexpr>¤   ó   € zcurl.<locals>.<genexpr>c                 S   ó    g | ]}t |tttfƒr|‘qS r   ©r   r   r
   r&   rG   r   r   r   rJ   ¦   ó     c                 s   ó$   � | ]}t |tttfƒs|V  qd S r   rP   rG   r   r   r   rM   §   ó   €" r1   zInvalid argument for curl)r   Úlenr   ÚzeroÚnextÚiterÚbase_vectorsÚbase_scalarsÚlame_coefficientsÚdotr   r2   r   r   r	   Úsympy.vectorrD   r    Ú
ValueErrorÚfromiterr   r   r
   r3   rA   r@   r&   )Úvectr2   Ú	coord_sysr   ÚjÚkÚxÚyÚzÚh1Úh2Úh3ÚvectxÚvectyÚvectzÚoutvecr    ÚvectorÚscalarÚresr   )rI   r2   rD   r   rA   g   sn   


ÿÿÿÿÿÿÿÿÿ
ÿ"rA   c                    sž  t | ƒ}t|ƒdkrtjS t|ƒdkrxt| tttfƒrt| ƒS t	t
|ƒƒ}| ¡ \}}}| ¡ \}}}| ¡ \}	}
}t|  |¡||
|ƒ|	|
 |  }t|  |¡|||	ƒ|	|
 |  }t|  |¡||	|
ƒ|	|
 |  }|| | }ˆ rv| ¡ S |S t| ttfƒrŒt ‡ fdd„| jD ƒ¡S t| ttfƒr¿dd„ | jD ƒd }t dd„ | jD ƒ¡}t|t|ƒƒ|t|ˆ d�  }ˆ r½| ¡ S |S t| tttfƒrËt| ƒS td	ƒ‚)
a  
    Returns the divergence of a vector field computed wrt the base
    scalars of the given coordinate system.

    Parameters
    ==========

    vector : Vector
        The vector operand

    doit : bool
        If True, the result is returned after calling .doit() on
        each component. Else, the returned expression contains
        Derivative instances

    Examples
    ========

    >>> from sympy.vector import CoordSys3D, divergence
    >>> R = CoordSys3D('R')
    >>> v1 = R.x*R.y*R.z * (R.i+R.j+R.k)

    >>> divergence(v1)
    R.x*R.y + R.x*R.z + R.y*R.z
    >>> v2 = 2*R.y*R.z*R.j
    >>> divergence(v2)
    2*R.z

    r   rB   c                 3   rK   rL   )r?   rG   r1   r   r   rM   ç   rN   zdivergence.<locals>.<genexpr>c                 S   rO   r   rP   rG   r   r   r   rJ   é   rQ   zdivergence.<locals>.<listcomp>c                 s   rR   r   rP   rG   r   r   r   rM   ê   rS   r1   zInvalid argument for divergence)r   rT   r   ÚZeror   r
   r@   r&   r>   rV   rW   rX   rY   rZ   Ú_diff_conditionalr[   r2   r   r	   r^   r    r   r   r   r3   r?   r]   )r_   r2   r`   r   ra   rb   rc   rd   re   rf   rg   rh   ÚvxÚvyÚvzro   rm   rn   r   r1   r   r?   ²   sF   
ÿ
ÿ
ÿr?   c                    s  t ˆ ƒ}t|ƒdkrtjS t|ƒdkr_tt|ƒƒ}| ¡ \}}}| ¡ \}}}| ¡ \}	}
}t	ˆ |	ƒ| }t	ˆ |
ƒ| }t	ˆ |ƒ| }|rS|| ||  ||   
¡ S || ||  ||  S tˆ ttfƒrqt dd„ ˆ jD ƒ¡S tˆ ttfƒrˆtˆ ƒ}t ‡ fdd„|D ƒ¡S tˆ ƒS )a³  
    Returns the vector gradient of a scalar field computed wrt the
    base scalars of the given coordinate system.

    Parameters
    ==========

    scalar_field : SymPy Expr
        The scalar field to compute the gradient of

    doit : bool
        If True, the result is returned after calling .doit() on
        each component. Else, the returned expression contains
        Derivative instances

    Examples
    ========

    >>> from sympy.vector import CoordSys3D, gradient
    >>> R = CoordSys3D('R')
    >>> s1 = R.x*R.y*R.z
    >>> gradient(s1)
    R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k
    >>> s2 = 5*R.x**2*R.z
    >>> gradient(s2)
    10*R.x*R.z*R.i + 5*R.x**2*R.k

    r   rB   c                 s   s   � | ]}t |ƒV  qd S r   ©r3   rG   r   r   r   rM   $  s   € zgradient.<locals>.<genexpr>c                 3   s    � | ]}ˆ | t |ƒ V  qd S r   ru   rG   ©Úscalar_fieldr   r   rM   '  s   € )r   rT   r   rU   rV   rW   rZ   rX   rY   r   r2   r   r   r	   r^   r    r   r   r$   r&   )rw   r2   r`   rf   rg   rh   r   ra   rb   rc   rd   re   rr   rs   rt   Úsr   rv   r   r3   õ   s(   r3   c                   @   r%   )Ú	Laplacianzù
    Represents unevaluated Laplacian.

    Examples
    ========

    >>> from sympy.vector import CoordSys3D, Laplacian
    >>> R = CoordSys3D('R')
    >>> v = 3*R.x**3*R.y**2*R.z**3
    >>> Laplacian(v)
    Laplacian(3*R.x**3*R.y**2*R.z**3)

    c                 C   r'   r   r(   r+   r   r   r   r)   :  r.   zLaplacian.__new__c                 K   s   ddl m} || jƒS )Nr   )Ú	laplacian)Úsympy.vector.functionsrz   r*   )r5   r6   rz   r   r   r   r2   @  s   
zLaplacian.doitNr8   r   r   r   r   ry   +  r=   ry   c                 C   s<   ddl m} || |jdd�}|| | }|rt||ƒS tjS )z¼
    First re-expresses expr in the system that base_scalar belongs to.
    If base_scalar appears in the re-expressed form, differentiates
    it wrt base_scalar.
    Else, returns 0
    r   rC   TrE   )r{   rD   Úsystemr   r   rp   )r   Úbase_scalarÚcoeff_1Úcoeff_2rD   Únew_exprÚargr   r   r   rq   E  s   rq   )T)r   Úsympy.core.exprr   Ú
sympy.corer   r   r   Úsympy.vector.coordsysrectr   Úsympy.vector.vectorr   r   r	   r
   r   Úsympy.core.functionr   Úsympy.core.addr   Úsympy.core.mulr   r   r$   r&   r>   r@   rA   r?   r3   ry   rq   r   r   r   r   Ú<module>   s$    


K
C6