U
    9%e1%                     @   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   U/var/www/html/Darija-Ai-API/env/lib/python3.8/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   ZOner   r   r   r   <lambda>       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                   @   s    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   s   t |}t| |}||_|S r   r   r   __new___exprclsr   objr   r   r   r'   +   s    zGradient.__new__c                 K   s   t | jddS NTdoit)gradientr(   selfhintsr   r   r   r.   1   s    zGradient.doitN__name__
__module____qualname____doc__r'   r.   r   r   r   r   r%      s   r%   c                   @   s    e Zd ZdZdd Zdd ZdS )
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   s   t |}t| |}||_|S r   r&   r)   r   r   r   r'   D   s    zDivergence.__new__c                 K   s   t | jddS r,   )
divergencer(   r0   r   r   r   r.   J   s    zDivergence.doitNr3   r   r   r   r   r8   5   s   r8   c                   @   s    e Zd ZdZdd Zdd ZdS )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   s   t |}t| |}||_|S r   r&   r)   r   r   r   r'   ]   s    zCurl.__new__c                 K   s   t | jddS r,   )curlr(   r0   r   r   r   r.   c   s    z	Curl.doitNr3   r   r   r   r   r:   N   s   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rddlm z&tt|  fdd| jD }W n tk
rv   | j}Y nX 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| dS )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   )csr>   r   r   
<listcomp>   s     zcurl.<locals>.<listcomp>c                 3   s   | ]}t | d V  qdS r-   N)r;   rA   r-   r   r   	<genexpr>   s     zcurl.<locals>.<genexpr>c                 S   s    g | ]}t |tttfr|qS r   r   r   r
   r%   rA   r   r   r   rD      s      c                 s   s"   | ]}t |tttfs|V  qd S r   rG   rA   r   r   r   rF      s      r-   N)r   lenr   zeronextiterbase_vectorsbase_scalarslame_coefficientsdotr   r.   r   r   r	   Zsympy.vectorr>   r    
ValueErrorfromiterr   r   r
   r/   r;   r:   r%   )vectr.   	coord_sysr   jkxyzh1h2h3ZvectxZvectyZvectzZoutvecr    vectorscalarresr   )rC   r.   r>   r   r;   g   sl    


"r;   c                    s  t | }t|dkrtjS t|dkrt| tttfr>t| S t	t
|}| \}}}| \}}}| \}	}
}t| |||
||	|
 |  }t| ||||	|	|
 |  }t| |||	|
|	|
 |  }|| | } r| S |S t| ttfrt 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| d	S )
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   r<   c                 3   s   | ]}t | d V  qdS rE   )r9   rA   r-   r   r   rF      s     zdivergence.<locals>.<genexpr>c                 S   s    g | ]}t |tttfr|qS r   rG   rA   r   r   r   rD      s      zdivergence.<locals>.<listcomp>c                 s   s"   | ]}t |tttfs|V  qd S r   rG   rA   r   r   r   rF      s      r-   N)r   rH   r   Zeror   r
   r:   r%   r8   rJ   rK   rL   rM   rN   _diff_conditionalrO   r.   r   r	   rQ   r    r   r   r   r/   r9   )rR   r.   rS   r   rT   rU   rV   rW   rX   rY   rZ   r[   vxvyvzr^   r\   r]   r   r-   r   r9      sF    


r9   c                    s  t  }t|dkrtjS t|dkrtt|}| \}}}| \}}}| \}	}
}t	 |	| }t	 |
| }t	 || }|r|| ||  ||  
 S || ||  ||  S t ttfrtdd  jD S t ttfrt }t fdd|D S t S d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   r<   c                 s   s   | ]}t |V  qd S r   r/   rA   r   r   r   rF   $  s     zgradient.<locals>.<genexpr>c                 3   s   | ]} | t | V  qd S r   rd   rA   scalar_fieldr   r   rF   '  s     N)r   rH   r   rI   rJ   rK   rN   rL   rM   r   r.   r   r   r	   rQ   r    r   r   r$   r%   )rf   r.   rS   rY   rZ   r[   r   rT   rU   rV   rW   rX   ra   rb   rc   sr   re   r   r/      s(    r/   c                   @   s    e Zd ZdZdd Zdd ZdS )	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   s   t |}t| |}||_|S r   r&   r)   r   r   r   r'   :  s    zLaplacian.__new__c                 K   s   ddl m} || jS )Nr   )	laplacian)sympy.vector.functionsri   r(   )r1   r2   ri   r   r   r   r.   @  s    zLaplacian.doitNr3   r   r   r   r   rh   +  s   rh   c                 C   s<   ddl m} || |jdd}|| | }|r6t||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   r=   Tr?   )rj   r>   systemr   r   r_   )r   Zbase_scalarZcoeff_1Zcoeff_2r>   Znew_exprargr   r   r   r`   E  s    r`   )T)T)T)r   Zsympy.core.exprr   Z
sympy.corer   r   r   Zsympy.vector.coordsysrectr   Zsympy.vector.vectorr   r   r	   r
   r   Zsympy.core.functionr   Zsympy.core.addr   Zsympy.core.mulr   r   r$   r%   r8   r:   r;   r9   r/   rh   r`   r   r   r   r   <module>   s"   

K
C
6