o
    8VaE                     @   sF  d dl mZ d dlmZ d dlmZmZmZ d dlm	Z	m
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 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_*e! e_+dS )    )Type)	StdFactKB)SPowsympify)
AtomicExprExpr)default_sort_key)sqrtImmutableMatrixAdd)
CoordSys3D)BasisDependentBasisDependentAddBasisDependentMulBasisDependentZero)
BaseDyadicDyadic	DyadicAddc                   @   s   e Zd ZdZdZdZdZdZdZdZ	dZ
dZ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.
    Tg      (@Nc                 C      | 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   5/usr/lib/python3/dist-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   	magnitude4      zVector.magnitudec                 C   s   | |    S )z@
        Returns the normalized version of this vector.
        )r   r   r   r   r   	normalize:   r   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sIt ||sItt|d d t ||rV 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"   )Zfieldr"   r   r   r   r"   w   s   
z*Vector.dot.<locals>.directional_derivative)
isinstancer   
VectorZeror   zeror   itemsargsdotZsympy.vector.deloperatorr!   	TypeErrorstr)r   otherZoutveckvZvect_dotr!   r"   r   r   r   r(   @   s"   
(


z
Vector.dotc                 C   
   |  |S Nr(   r   r+   r   r   r   __and__~      
zVector.__and__c                 C   sn   t |tr2t | trtjS tj}|j D ]\}}| |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   r+   Zoutdyadr,   r-   Zcross_productr5   r   r   r   r4      s   
 

zVector.crossc                 C   r.   r/   r4   r1   r   r   r   __xor__   r3   zVector.__xor__c                 C   sx   t |ts	tdt | tst |trtjS g }| j D ]\}}|j D ]\}}||| t	||  q&q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 product)r#   r   r)   r$   r   r%   r   r&   appendr   r   )r   r+   r'   Zk1v1Zk2v2r   r   r   r5      s   

zVector.outerFc                 C   sL   |  tjr|rtjS tjS |r| || |  S | || |  |  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

        )Zequalsr   r%   r   Zeror(   )r   r+   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_sys_from_exprc                       g | ]}  |qS r   r0   .0ir   r   r   
<listcomp>      z'Vector._projections.<locals>.<listcomp>)
Zsympy.vector.operatorsr=   r#   r$   r   r;   nextiterbase_vectorstuple)r   r=   Zbase_vecr   r   r   _projections   s
   
zVector._projectionsc                 C   r.   r/   )r5   r1   r   r   r   __or__  r3   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                    r>   r   r0   )r@   Zunit_vecr   r   r   rB   /  rC   z$Vector.to_matrix.<locals>.<listcomp>)MatrixrF   )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&   getrK   r   r%   )r   partsvectZmeasurer   r   r   separate2  s   zVector.separatec                 C   sR   t | trt |trtdt | tr%|tjkrtdt| t|tjS td)z( Helper for division involving vectors. zCannot divide two vectorszCannot divide a vector by zeroz#Invalid division involving a vector)	r#   r   r)   r   r;   
ValueError	VectorMulr   ZNegativeOne)Zoner+   r   r   r   _div_helperL  s   

zVector._div_helper)F)__name__
__module____qualname____doc__Z	is_Vector_op_priority
_expr_type	_mul_func	_add_func
_zero_func
_base_funcr%   propertyr   r   r   r(   r2   r4   r7   r5   r<   rH   rI   rL   rP   rS   r   r   r   r   r      s:    
>,
&
r   c                       sB   e Zd ZdZd fdd	Zedd Zdd Zed	d
 Z  Z	S )
BaseVectorzl
    Class to denote a base vector.

    Unicode pretty forms in Python 2 should use the prefix ``u``.

    Nc                    s   |d u r	d |}|d u rd |}t|}t|}|tddvr%t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)formatr*   rangerQ   r#   r   r)   Z_vector_namessuper__new__r   _base_instanceOner   _measure_number_name_pretty_form_latex_form_systemZ_idr   Z_assumptions_sys)clsindexrK   Z
pretty_strZ	latex_strnameobjZassumptions	__class__r   r   rf   `  s0   






zBaseVector.__new__c                 C   r   r/   )rm   r   r   r   r   rK        zBaseVector.systemc                 C   r   r/   )rj   )r   printerr   r   r   	_sympystr  s   zBaseVector._sympystrc                 C   s   | hS r/   r   r   r   r   r   free_symbols  ru   zBaseVector.free_symbols)NN)
rT   rU   rV   rW   rf   r^   rK   rw   rx   __classcell__r   r   rs   r   r_   X  s    #
r_   c                   @       e Zd ZdZdd Zdd ZdS )	VectorAddz2
    Class to denote sum of Vector instances.
    c                 O      t j| g|R i |}|S r/   )r   rf   ro   r'   optionsrr   r   r   r   rf        zVectorAdd.__new__c           	      C   sz   d}t |   }|jdd d |D ]"\}}| }|D ]}||jv r5| j| | }|||d 7 }qq|d d S )Nrb   c                 S   s   | d   S )Nr   )__str__)xr   r   r   <lambda>  s    z%VectorAdd._sympystr.<locals>.<lambda>keyz + )listrP   r&   sortrF   r   Z_print)	r   rv   Zret_strr&   rK   rO   Z
base_vectsr   Z	temp_vectr   r   r   rw     s   
zVectorAdd._sympystrN)rT   rU   rV   rW   rf   rw   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 )	rR   z>
    Class to denote products of scalars and BaseVectors.
    c                 O   r|   r/   )r   rf   r}   r   r   r   rf     r   zVectorMul.__new__c                 C   r   )z) The BaseVector involved in the product. )rg   r   r   r   r   base_vector  s   zVectorMul.base_vectorc                 C   r   )zU The scalar expression involved in the definition of
        this VectorMul.
        )ri   r   r   r   r   measure_number  s   zVectorMul.measure_numberN)rT   rU   rV   rW   rf   r^   r   r   r   r   r   r   rR     s    
rR   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 r/   )r   rf   )ro   rr   r   r   r   rf     s   
zVectorZero.__new__N)rT   rU   rV   rW   rX   rk   rl   rf   r   r   r   r   r$     s    r$   c                   @   rz   )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 r/   )r   r	   r   r   rf   _expr1_expr2ro   Zexpr1Zexpr2rr   r   r   r   rf     s   zCross.__new__c                 K      t | j| jS r/   )r4   r   r   r   kwargsr   r   r   doit     z
Cross.doitNrT   rU   rV   rW   rf   r   r   r   r   r   r     s    
r   c                   @   rz   )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   rf   r   r   r   r   r   r   rf     s   zDot.__new__c                 K   r   r/   )r(   r   r   r   r   r   r   r     r   zDot.doitNr   r   r   r   r   r     s    	r   c                    s  t  trtfdd jD S t tr$t fddjD S t  trt tr jjkre jd }jd }||krEtjS h d	||h
 }|d d |krZdnd}| j |  S dd	lm} z| j}W n ty   t  Y S w t|S t  tst trtjS t  trtt j \}}	|	t| S t trt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       | ]}t | V  qd S r/   r6   r?   vect2r   r   	<genexpr>      zcross.<locals>.<genexpr>c                 3       | ]}t  |V  qd S r/   r6   r?   vect1r   r   r     r   r   >   r   r       r    r`   express)r#   r   r{   fromiterr'   r_   rn   r   r%   
differencepoprF   	functionsr   rQ   r   r4   r$   rR   rD   rE   r   r&   )r   r   Zn1Zn2Zn3signr   r-   r9   m1r:   m2r   r   r   r   r4   
  s:   







r4   c                    s@  t  trtfdd jD S t tr$t fddjD S t  tr`t tr` jjkr> kr;tjS tjS ddl	m
} z| j}W n tyZ   t  Y S w t |S t  tsjt trmtjS t  trtt j \}}|t| S t trt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   r   r/   r0   r?   r   r   r   r   K  r   zdot.<locals>.<genexpr>c                 3   r   r/   r0   r?   r   r   r   r   M  r   r    r   )r#   r   r   r'   r_   rn   r   rh   r;   r   r   rQ   r   r(   r$   rR   rD   rE   r   r&   )r   r   r   r-   r9   r   r:   r   r   r   r   r(   :  s.   





r(   N),typingr   Zsympy.core.assumptionsr   Z
sympy.corer   r   r   Zsympy.core.exprr   r   Zsympy.core.compatibilityr	   Zsympyr
   r   rJ   r   Zsympy.vector.coordsysrectr   Zsympy.vector.basisdependentr   r   r   r   Zsympy.vector.dyadicr   r   r   r   r_   r{   rR   r$   r   r   r4   r(   rY   rZ   r[   r\   r]   r%   r   r   r   r   <module>   s4      L7 !0*