o
    8Vaq)                     @   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	lmZmZ d
d Zd!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)
BaseScalar)SymPyDeprecationWarning)
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   8/usr/lib/python3/dist-packages/sympy/vector/operators.py_get_coord_systems   s   

r   c                 C   s$   |durt ddddd  t| S )z[
    expr : expression
        The coordinate system is extracted from this parameter.
    Nzcoord_sys parameterzdo not use itz1.1iT2  )ZfeatureZ
useinsteadZdeprecated_since_versionZissue)r   warnr   )r   	coord_sysr   r   r   _get_coord_sys_from_expr   s   r    c                 C   s:   t dd }| jD ]}|t|  |9  < q
t| S )Nc                   S   s   t jS r   )r   Z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kwargsr   r   r   r6   D      zGradient.doitN__name__
__module____qualname____doc__r-   r6   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-   W   r2   zDivergence.__new__c                 K   r3   r4   )
divergencer.   r8   r   r   r   r6   ]   r;   zDivergence.doitNr<   r   r   r   r   rB   H   rA   rB   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-   p   r2   zCurl.__new__c                 K   r3   r4   )curlr.   r8   r   r   r   r6   v   r;   z	Curl.doitNr<   r   r   r   r   rD   a   rA   rD   Tc                    s  t | |}t|dkrtjS t|dkrtt|}| \}}}| \}}}| \}	}
}| 	|}| 	|}| 	|}tj}|t
|| |t
||
 | | |
|  7 }|t
||	 |t
|| | | |	|  7 }|t
||
 |t
||	 | | |
|	  7 }r| S |S t| ttfrd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rd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| )a  
    Returns the curl of a vector field computed wrt the base scalars
    of the given coordinate system.

    Parameters
    ==========

    vect : Vector
        The vector operand

    coord_sys : CoordSys3D
        The coordinate system to calculate the gradient in.
        Deprecated since version 1.1

    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Z	variablesr   .0r   )csrH   r   r   
<listcomp>   s    zcurl.<locals>.<listcomp>c                 3       | ]	}t | d V  qdS r5   N)rE   rJ   r5   r   r   	<genexpr>       zcurl.<locals>.<genexpr>c                 S       g | ]}t |tttfr|qS r   r   r   r
   r*   rJ   r   r   r   rM           c                 s   $    | ]}t |tttfs|V  qd S r   rS   rJ   r   r   r   rP         " r5   )r    lenr   zeronextiterbase_vectorsbase_scalarslame_coefficientsdotr   r6   r   r   r	   Zsympy.vectorrH   r$   
ValueErrorfromiterr   r   r
   r7   rE   rD   r*   )vectr   r6   r   jkxyzh1h2h3ZvectxZvectyZvectzZoutvecr$   vectorscalarresr   )rL   r6   rH   r   rE   z   sn   
"



"rE   c                    s  t | |}t|dkrtjS t|dkryt| tttfr t| S t	t
|}| \}}}| \}}}| \}	}
}t| |||
||	|
 |  }t| ||||	|	|
 |  }t| |||	|
|	|
 |  }|| | } rw| S |S t| ttfrt fdd| jD S t| ttfrdd | jD d }tdd | jD }t|t||t| d  } r| S |S t| tttfrt| S t| )	a  
    Returns the divergence of a vector field computed wrt the base
    scalars of the given coordinate system.

    Parameters
    ==========

    vector : Vector
        The vector operand

    coord_sys : CoordSys3D
        The coordinate system to calculate the gradient in
        Deprecated since version 1.1

    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   rF   c                 3   rN   rO   )rC   rJ   r5   r   r   rP     rQ   zdivergence.<locals>.<genexpr>c                 S   rR   r   rS   rJ   r   r   r   rM     rT   zdivergence.<locals>.<listcomp>c                 s   rU   r   rS   rJ   r   r   r   rP     rV   r5   )r    rW   r   Zeror   r
   rD   r*   rB   rY   rZ   r[   r\   r]   _diff_conditionalr^   r6   r   r	   r`   r$   r   r   r   r7   rC   )ra   r   r6   r   rb   rc   rd   re   rf   rg   rh   ri   vxvyvzrl   rj   rk   r   r5   r   rC      sF   
"


rC   c                    s  t  |}t|dkrtjS t|dkr`tt|}| \}}}| \}}}| \}	}
}t	 |	| }t	 |
| }t	 || }|rT|| ||  ||  
 S || ||  ||  S t ttfrrtdd  jD S t ttfrt }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

    coord_sys : CoordSys3D
        The coordinate system to calculate the gradient in
        Deprecated since version 1.1

    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   rF   c                 s   s    | ]}t |V  qd S r   r7   rJ   r   r   r   rP   C  s    zgradient.<locals>.<genexpr>c                 3   s     | ]} | t | V  qd S r   rr   rJ   scalar_fieldr   r   rP   F  s    )r    rW   r   rX   rY   rZ   r]   r[   r\   r   r6   r   r   r	   r`   r$   r   r   r(   r*   )rt   r   r6   rg   rh   ri   r   rb   rc   rd   re   rf   ro   rp   rq   sr   rs   r   r7     s(   
!r7   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-   Y  r2   zLaplacian.__new__c                 K   s   ddl m} || jS )Nr   )	laplacian)sympy.vector.functionsrw   r.   )r9   r:   rw   r   r   r   r6   _  s   
zLaplacian.doitNr<   r   r   r   r   rv   J  rA   rv   c                 C   sB   ddl m} || |jdd}||tv 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   rG   TrI   )rx   rH   systemZatomsr   r   r   rm   )r   Zbase_scalarZcoeff_1Zcoeff_2rH   Znew_exprr   r   r   rn   d  s
   rn   r   )NT)#r"   Zsympy.core.exprr   Z
sympy.corer   r   r   Zsympy.vector.coordsysrectr   Zsympy.vector.vectorr   r   r	   r
   r   Zsympy.vector.scalarr   Zsympy.utilities.exceptionsr   Zsympy.core.functionr   Zsympyr   r   r   r    r(   r*   rB   rD   rE   rC   r7   rv   rn   r   r   r   r   <module>   s(    



O
G: