U
    ZÊ¹j÷E  ã                   @  sr  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
 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 d dlmZmZmZmZ d dlmZ d dlm Z m!Z!m"Z" G dd„ deƒZ#G dd„ de#e	ƒZ$G dd„ dee#ƒZ%G dd„ dee#ƒZ&G dd„ dee#ƒZ'G dd„ de#ƒZ(G dd„ de
ƒZ)dd„ Z*dd „ Z+e#e#_,e&e#_-e%e#_.e'e#_/e$e#_0e'ƒ e#_1d!S )"é    )Úannotations)Úproduct)ÚAdd)Ú	StdFactKB)Ú
AtomicExprÚExpr)ÚPow)ÚS)Údefault_sort_key)Úsympify©Úsqrt)ÚImmutableDenseMatrix)ÚBasisDependentZeroÚBasisDependentÚBasisDependentMulÚBasisDependentAdd)Ú
CoordSys3D)ÚDyadicÚ
BaseDyadicÚ	DyadicAddc                   @  sà   e Zd ZU dZdZdZdZded< ded< ded< ded	< ded
< ded< edd„ ƒZ	dd„ Z
dd„ Zdd„ Zdd„ Zeje_dd„ Zdd„ Zeje_dd„ Zd*dd„Zedd „ ƒZd!d"„ Zeje_d#d$„ Zd%d&„ Zd'd(„ Zd)S )+ÚVectorz�
    Super class for all Vector classes.
    Ideally, neither this class nor any of its subclasses should be
    instantiated by the user.
    FTg      (@ztype[Vector]Ú
_expr_typeÚ	_mul_funcÚ	_add_funcÚ
_zero_funcÚ
_base_funcÚ
VectorZeroÚzeroc                 C  s   | j S )a‚  
        Returns the components of this vector in the form of a
        Python dictionary mapping BaseVector instances to the
        corresponding measure numbers.

        Examples
        ========

        >>> from sympy.vector import CoordSys3D
        >>> C = CoordSys3D('C')
        >>> v = 3*C.i + 4*C.j + 5*C.k
        >>> v.components
        {C.i: 3, C.j: 4, C.k: 5}

        )Ú_components©Úself© r"   úR/var/www/html/bwave/python/venv/lib/python3.8/site-packages/sympy/vector/vector.pyÚ
components%   s    zVector.componentsc                 C  s   t | | @ ƒS )z7
        Returns the magnitude of this vector.
        r   r    r"   r"   r#   Ú	magnitude:   s    zVector.magnitudec                 C  s   | |   ¡  S )z@
        Returns the normalized version of this vector.
        )r%   r    r"   r"   r#   Ú	normalize@   s    zVector.normalizec                   s°   t |tƒr^t ˆ tƒrtjS tj}|j ¡ D ].\}}|jd  ˆ ¡}||| |jd  7 }q*|S ddl	m
} t ||tfƒsŒtt|ƒd d ƒ‚t ||ƒr¦‡ fdd„}|S tˆ |ƒS )aN  
        Returns the dot product of this Vector, either with another
        Vector, or a Dyadic, or a Del operator.
        If 'other' is a Vector, returns the dot product scalar (SymPy
        expression).
        If 'other' is a Dyadic, the dot product is returned as a Vector.
        If 'other' is an instance of Del, returns the directional
        derivative operator as a Python function. If this function is
        applied to a scalar expression, it returns the directional
        derivative of the scalar field wrt this Vector.

        Parameters
        ==========

        other: Vector/Dyadic/Del
            The Vector or Dyadic we are dotting with, or a Del operator .

        Examples
        ========

        >>> from sympy.vector import CoordSys3D, Del
        >>> C = CoordSys3D('C')
        >>> delop = Del()
        >>> C.i.dot(C.j)
        0
        >>> C.i & C.i
        1
        >>> v = 3*C.i + 4*C.j + 5*C.k
        >>> v.dot(C.k)
        5
        >>> (C.i & delop)(C.x*C.y*C.z)
        C.y*C.z
        >>> d = C.i.outer(C.i)
        >>> C.i.dot(d)
        C.i

        r   é   )ÚDelz is not a vector, dyadic or zdel operatorc                   s   ddl m} || ˆ ƒS )Nr   )Údirectional_derivative)Zsympy.vector.functionsr)   )Úfieldr)   r    r"   r#   r)   }   s    z*Vector.dot.<locals>.directional_derivative)Ú
isinstancer   r   r   r   r$   ÚitemsÚargsÚdotZsympy.vector.deloperatorr(   Ú	TypeErrorÚstr)r!   ÚotherZoutvecÚkÚvZvect_dotr(   r)   r"   r    r#   r.   F   s"    (

ÿ
z
Vector.dotc                 C  s
   |   |¡S ©N©r.   ©r!   r1   r"   r"   r#   Ú__and__„   s    zVector.__and__c                 C  sn   t |tƒrdt | tƒrtjS tj}|j ¡ D ]4\}}|  |jd ¡}| |jd ¡}||| 7 }q*|S t| |ƒS )aÃ  
        Returns the cross product of this Vector with another Vector or
        Dyadic instance.
        The cross product is a Vector, if 'other' is a Vector. If 'other'
        is a Dyadic, this returns a Dyadic instance.

        Parameters
        ==========

        other: Vector/Dyadic
            The Vector or Dyadic we are crossing with.

        Examples
        ========

        >>> from sympy.vector import CoordSys3D
        >>> C = CoordSys3D('C')
        >>> C.i.cross(C.j)
        C.k
        >>> C.i ^ C.i
        0
        >>> v = 3*C.i + 4*C.j + 5*C.k
        >>> v ^ C.i
        5*C.j + (-4)*C.k
        >>> d = C.i.outer(C.i)
        >>> C.j.cross(d)
        (-1)*(C.k|C.i)

        r   r'   )	r+   r   r   r   r$   r,   Úcrossr-   Úouter)r!   r1   Zoutdyadr2   r3   Zcross_productr9   r"   r"   r#   r8   ‰   s     

zVector.crossc                 C  s
   |   |¡S r4   ©r8   r6   r"   r"   r#   Ú__xor__µ   s    zVector.__xor__c                 C  sV   t |tƒstdƒ‚nt | tƒs(t |tƒr.tjS dd„ t| j ¡ |j ¡ ƒD ƒ}t	|Ž S )a±  
        Returns the outer product of this vector with another, in the
        form of a Dyadic instance.

        Parameters
        ==========

        other : Vector
            The Vector with respect to which the outer product is to
            be computed.

        Examples
        ========

        >>> from sympy.vector import CoordSys3D
        >>> N = CoordSys3D('N')
        >>> N.i.outer(N.j)
        (N.i|N.j)

        z!Invalid operand for outer productc                 S  s*   g | ]"\\}}\}}|| t ||ƒ ‘qS r"   )r   )Ú.0Zk1Úv1Zk2Úv2r"   r"   r#   Ú
<listcomp>Ù   s     z Vector.outer.<locals>.<listcomp>)
r+   r   r/   r   r   r   r   r$   r,   r   )r!   r1   r-   r"   r"   r#   r9   º   s    


ÿÿzVector.outerc                 C  sP   |   tj¡r|rtjS tjS |r4|  |¡|  | ¡ S |  |¡|  | ¡ |  S dS )a«  
        Returns the vector or scalar projection of the 'other' on 'self'.

        Examples
        ========

        >>> from sympy.vector.coordsysrect import CoordSys3D
        >>> C = CoordSys3D('C')
        >>> i, j, k = C.base_vectors()
        >>> v1 = i + j + k
        >>> v2 = 3*i + 4*j
        >>> v1.projection(v2)
        7/3*C.i + 7/3*C.j + 7/3*C.k
        >>> v1.projection(v2, scalar=True)
        7/3

        N)Úequalsr   r   r	   ÚZeror.   )r!   r1   Zscalarr"   r"   r#   Ú
projectionÞ   s
    zVector.projectionc                   sP   ddl m} tˆ tƒr&tjtjtjfS tt|ˆ ƒƒƒ ¡ }t	‡ fdd„|D ƒƒS )aé  
        Returns the components of this vector but the output includes
        also zero values components.

        Examples
        ========

        >>> from sympy.vector import CoordSys3D, Vector
        >>> C = CoordSys3D('C')
        >>> v1 = 3*C.i + 4*C.j + 5*C.k
        >>> v1._projections
        (3, 4, 5)
        >>> v2 = C.x*C.y*C.z*C.i
        >>> v2._projections
        (C.x*C.y*C.z, 0, 0)
        >>> v3 = Vector.zero
        >>> v3._projections
        (0, 0, 0)
        r   )Ú_get_coord_systemsc                   s   g | ]}ˆ   |¡‘qS r"   r5   ©r<   Úir    r"   r#   r?     s     z'Vector._projections.<locals>.<listcomp>)
Zsympy.vector.operatorsrC   r+   r   r	   rA   ÚnextÚiterÚbase_vectorsÚtuple)r!   rC   Zbase_vecr"   r    r#   Ú_projectionsø   s
    
zVector._projectionsc                 C  s
   |   |¡S r4   )r9   r6   r"   r"   r#   Ú__or__  s    zVector.__or__c                   s   t ‡ fdd„| ¡ D ƒƒS )a  
        Returns the matrix form of this vector with respect to the
        specified coordinate system.

        Parameters
        ==========

        system : CoordSys3D
            The system wrt which the matrix form is to be computed

        Examples
        ========

        >>> from sympy.vector import CoordSys3D
        >>> C = CoordSys3D('C')
        >>> from sympy.abc import a, b, c
        >>> v = a*C.i + b*C.j + c*C.k
        >>> v.to_matrix(C)
        Matrix([
        [a],
        [b],
        [c]])

        c                   s   g | ]}ˆ   |¡‘qS r"   r5   )r<   Zunit_vecr    r"   r#   r?   3  s     z$Vector.to_matrix.<locals>.<listcomp>)ÚMatrixrH   )r!   Úsystemr"   r    r#   Ú	to_matrix  s    ÿzVector.to_matrixc                 C  s:   i }| j  ¡ D ]&\}}| |jtj¡||  ||j< q|S )aÅ  
        The constituents of this vector in different coordinate systems,
        as per its definition.

        Returns a dict mapping each CoordSys3D to the corresponding
        constituent Vector.

        Examples
        ========

        >>> from sympy.vector import CoordSys3D
        >>> R1 = CoordSys3D('R1')
        >>> R2 = CoordSys3D('R2')
        >>> v = R1.i + R2.i
        >>> v.separate() == {R1: R1.i, R2: R2.i}
        True

        )r$   r,   ÚgetrM   r   r   )r!   ÚpartsÚvectÚmeasurer"   r"   r#   Úseparate6  s    ÿzVector.separatec                 C  sX   t | tƒrt |tƒrtdƒ‚n6t | tƒrL|tjkr:tdƒ‚t| t|tjƒƒS tdƒ‚dS )z( Helper for division involving vectors. zCannot divide two vectorszCannot divide a vector by zeroz#Invalid division involving a vectorN)	r+   r   r/   r	   rA   Ú
ValueErrorÚ	VectorMulr   ZNegativeOne)Úoner1   r"   r"   r#   Ú_div_helperP  s    


zVector._div_helperN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Z	is_scalarZ	is_VectorÚ_op_priorityÚ__annotations__Úpropertyr$   r%   r&   r.   r7   r8   r;   r9   rB   rJ   rK   rN   rS   rW   r"   r"   r"   r#   r      s:   

>,$

r   c                      sJ   e Zd ZdZd‡ fdd„	Zedd„ ƒZdd„ Zd	d
„ Zedd„ ƒZ	‡  Z
S )Ú
BaseVectorz)
    Class to denote a base vector.

    Nc                   sà   |d krd  |¡}|d kr$d  |¡}t|ƒ}t|ƒ}|tddƒkrJtdƒ‚t|tƒs\tdƒ‚|j| }tƒ  	| t
|ƒ|¡}||_|t
ji|_t
j|_|jd | |_d| |_||_||_||f|_d	d
i}t|ƒ|_||_|S )Nzx{}zx_{}r   é   zindex must be 0, 1 or 2zsystem should be a CoordSys3DÚ.Ú ZcommutativeT)Úformatr0   ÚrangerT   r+   r   r/   Ú_vector_namesÚsuperÚ__new__r	   Ú_base_instanceÚOner   Ú_measure_numberÚ_nameÚ_pretty_formÚ_latex_formÚ_systemÚ_idr   Z_assumptionsÚ_sys)ÚclsÚindexrM   Ú
pretty_strZ	latex_strÚnameÚobjZassumptions©Ú	__class__r"   r#   rg   b  s0    






zBaseVector.__new__c                 C  s   | j S r4   )rn   r    r"   r"   r#   rM   …  s    zBaseVector.systemc                 C  s   | j S r4   )rk   )r!   Úprinterr"   r"   r#   Ú	_sympystr‰  s    zBaseVector._sympystrc                 C  s"   | j \}}| |¡d |j|  S )Nra   )ro   Ú_printre   )r!   rx   rr   rM   r"   r"   r#   Ú
_sympyreprŒ  s    
zBaseVector._sympyreprc                 C  s   | hS r4   r"   r    r"   r"   r#   Úfree_symbols�  s    zBaseVector.free_symbols)NN)rX   rY   rZ   r[   rg   r^   rM   ry   r{   r|   Ú__classcell__r"   r"   rv   r#   r_   \  s   #
r_   c                   @  s    e Zd ZdZdd„ Zdd„ ZdS )Ú	VectorAddz2
    Class to denote sum of Vector instances.
    c                 O  s   t j| f|ž|Ž}|S r4   )r   rg   ©rq   r-   Úoptionsru   r"   r"   r#   rg   š  s    zVectorAdd.__new__c           	      C  sz   d}t |  ¡  ¡ ƒ}|jdd„ d� |D ]D\}}| ¡ }|D ].}||jkr<| j| | }|| |¡d 7 }q<q(|d d… S )Nrb   c                 S  s   | d   ¡ S )Nr   )Ú__str__)Úxr"   r"   r#   Ú<lambda>¡  ó    z%VectorAdd._sympystr.<locals>.<lambda>©Úkeyz + éýÿÿÿ)ÚlistrS   r,   ÚsortrH   r$   rz   )	r!   rx   Zret_strr,   rM   rQ   Z
base_vectsr‚   Z	temp_vectr"   r"   r#   ry   ž  s    
zVectorAdd._sympystrN)rX   rY   rZ   r[   rg   ry   r"   r"   r"   r#   r~   •  s   r~   c                   @  s0   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZdS )	rU   z>
    Class to denote products of scalars and BaseVectors.
    c                 O  s   t j| f|ž|Ž}|S r4   )r   rg   r   r"   r"   r#   rg   °  s    zVectorMul.__new__c                 C  s   | j S )z) The BaseVector involved in the product. )rh   r    r"   r"   r#   Úbase_vector´  s    zVectorMul.base_vectorc                 C  s   | j S )zU The scalar expression involved in the definition of
        this VectorMul.
        )rj   r    r"   r"   r#   Úmeasure_number¹  s    zVectorMul.measure_numberN)rX   rY   rZ   r[   rg   r^   rŠ   r‹   r"   r"   r"   r#   rU   «  s   
rU   c                   @  s$   e Zd ZdZdZdZdZdd„ ZdS )r   z'
    Class to denote a zero vector
    g333333(@Ú0z\mathbf{\hat{0}}c                 C  s   t  | ¡}|S r4   )r   rg   )rq   ru   r"   r"   r#   rg   Ê  s    
zVectorZero.__new__N)rX   rY   rZ   r[   r\   rl   rm   rg   r"   r"   r"   r#   r   Á  s
   r   c                   @  s    e Zd ZdZdd„ Zdd„ ZdS )ÚCrossaŒ  
    Represents unevaluated Cross product.

    Examples
    ========

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

    c                 C  sJ   t |ƒ}t |ƒ}t|ƒt|ƒkr,t||ƒ S t | ||¡}||_||_|S r4   )r   r
   r�   r   rg   Ú_expr1Ú_expr2©rq   Zexpr1Zexpr2ru   r"   r"   r#   rg   á  s    zCross.__new__c                 K  s   t | j| jƒS r4   )r8   rŽ   r�   ©r!   Úhintsr"   r"   r#   Údoitë  s    z
Cross.doitN©rX   rY   rZ   r[   rg   r“   r"   r"   r"   r#   r�   Ï  s   
r�   c                   @  s    e Zd ZdZdd„ Zdd„ ZdS )ÚDota�  
    Represents unevaluated Dot product.

    Examples
    ========

    >>> from sympy.vector import CoordSys3D, Dot
    >>> from sympy import symbols
    >>> R = CoordSys3D('R')
    >>> a, b, c = symbols('a b c')
    >>> v1 = R.i + R.j + R.k
    >>> v2 = a * R.i + b * R.j + c * R.k
    >>> Dot(v1, v2)
    Dot(R.i + R.j + R.k, a*R.i + b*R.j + c*R.k)
    >>> Dot(v1, v2).doit()
    a + b + c

    c                 C  sB   t |ƒ}t |ƒ}t||gtd�\}}t | ||¡}||_||_|S )Nr…   )r   Úsortedr
   r   rg   rŽ   r�   r�   r"   r"   r#   rg     s    zDot.__new__c                 K  s   t | j| jƒS r4   )r.   rŽ   r�   r‘   r"   r"   r#   r“     s    zDot.doitNr”   r"   r"   r"   r#   r•   ï  s   	r•   c                   s   t ˆ tƒr$t ‡fdd„ˆ jD ƒ¡S t ˆtƒrHt ‡ fdd„ˆjD ƒ¡S t ˆ tƒ�rt ˆtƒ�rˆ jˆjkrÐˆ jd }ˆjd }||krŽtjS dddh 	||h¡ 
¡ }|d d |krºdnd}|ˆ j ¡ |  S dd	lm} z|ˆ ˆjƒ}W n  tk
�r   tˆ ˆƒ Y S X t|ˆƒS t ˆ tƒ�s0t ˆtƒ�r6tjS t ˆ tƒ�rfttˆ j ¡ ƒƒ\}}	|	t|ˆƒ S t ˆtƒ�r–ttˆj ¡ ƒƒ\}
}|tˆ |
ƒ S tˆ ˆƒS )
a^  
    Returns cross product of two vectors.

    Examples
    ========

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

    c                 3  s   | ]}t |ˆ ƒV  qd S r4   r:   rD   ©Úvect2r"   r#   Ú	<genexpr>!  s     zcross.<locals>.<genexpr>c                 3  s   | ]}t ˆ |ƒV  qd S r4   r:   rD   ©Úvect1r"   r#   r™   #  s     r   r'   é   r`   éÿÿÿÿ©Úexpress)r+   r   r~   Úfromiterr-   r_   rp   r   r   Ú
differenceÚpoprH   Ú	functionsrŸ   rT   r�   r8   r   rU   rF   rG   r$   r,   )r›   r˜   Zn1Zn2Zn3ÚsignrŸ   r3   r=   Úm1r>   Úm2r"   ©r›   r˜   r#   r8     s8    




r8   c                   sF  t ˆ tƒr$t ‡fdd„ˆ jD ƒ¡S t ˆtƒrHt ‡ fdd„ˆjD ƒ¡S t ˆ tƒrÂt ˆtƒrÂˆ jˆjkr|ˆ ˆkrvtjS tjS ddl	m
} z|ˆˆ jƒ}W n tk
r¶   tˆ ˆƒ Y S X tˆ |ƒS t ˆ tƒsÖt ˆtƒrÜtjS t ˆ tƒ�rttˆ j ¡ ƒƒ\}}|t|ˆƒ S t ˆtƒ�r<ttˆj ¡ ƒƒ\}}|tˆ |ƒ S tˆ ˆƒS )a2  
    Returns dot product of two vectors.

    Examples
    ========

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

    c                 3  s   | ]}t |ˆ ƒV  qd S r4   r5   rD   r—   r"   r#   r™   Q  s     zdot.<locals>.<genexpr>c                 3  s   | ]}t ˆ |ƒV  qd S r4   r5   rD   rš   r"   r#   r™   S  s     r'   rž   )r+   r   r    r-   r_   rp   r	   ri   rA   r£   rŸ   rT   r•   r.   r   rU   rF   rG   r$   r,   )r›   r˜   rŸ   r3   r=   r¥   r>   r¦   r"   r§   r#   r.   @  s,    


r.   N)2Ú
__future__r   Ú	itertoolsr   Zsympy.core.addr   Zsympy.core.assumptionsr   Zsympy.core.exprr   r   Zsympy.core.powerr   Zsympy.core.singletonr	   Zsympy.core.sortingr
   Zsympy.core.sympifyr   Z(sympy.functions.elementary.miscellaneousr   Zsympy.matrices.immutabler   rL   Zsympy.vector.basisdependentr   r   r   r   Zsympy.vector.coordsysrectr   Zsympy.vector.dyadicr   r   r   r   r_   r~   rU   r   r�   r•   r8   r.   r   r   r   r   r   r   r"   r"   r"   r#   Ú<module>   s<     K9 !0*