a
    lhbO                     @  s  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
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# d dl$m%Z% G dd deZ&dd Z'de'egiej(e&< 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$ Z0e&e&_1e+e&_2e*e&_3e,e&_4e)e&_5e, e&_6d%S )&    )annotations)product)AddBasic)	StdFactKB)
AtomicExprExpr)Pow)S)default_sort_key)sympifysqrt)ImmutableDenseMatrix)BasisDependentZeroBasisDependentBasisDependentMulBasisDependentAdd)
CoordSys3D)Dyadic
BaseDyadic	DyadicAdd)
VectorKindc                   @  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 Z	ded< e
dd Z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zeror   kindc                 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%   A/var/www/auris/lib/python3.9/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&   	normalizeC   s    zVector.normalizec                 C  s   | | }| |}|dS )aM  
        Check if ``self`` and ``other`` are identically equal vectors.

        Explanation
        ===========

        Checks if two vector expressions are equal for all possible values of
        the symbols present in the expressions.

        Examples
        ========

        >>> from sympy.vector import CoordSys3D
        >>> from sympy.abc import x, y
        >>> from sympy import pi
        >>> C = CoordSys3D('C')

        Compare vectors that are equal or not:

        >>> C.i.equals(C.j)
        False
        >>> C.i.equals(C.i)
        True

        These two vectors are equal if `x = y` but are not identically equal
        as expressions since for some values of `x` and `y` they are unequal:

        >>> v1 = x*C.i + C.j
        >>> v2 = y*C.i + C.j
        >>> v1.equals(v1)
        True
        >>> v1.equals(v2)
        False

        Vectors from different coordinate systems can be compared:

        >>> D = C.orient_new_axis('D', pi/2, C.i)
        >>> D.j.equals(C.j)
        False
        >>> D.j.equals(C.k)
        True

        Parameters
        ==========

        other: Vector
            The other vector expression to compare with.

        Returns
        =======

        ``True``, ``False`` or ``None``. A return value of ``True`` indicates
        that the two vectors are identically equal. A return value of ``False``
        indicates that they are not. In some cases it is not possible to
        determine if the two vectors are identically equal and ``None`` is
        returned.

        See Also
        ========

        sympy.core.expr.Expr.equals
        r   )dotequals)r$   otherdiffZ	diff_mag2r%   r%   r&   r+   I   s    ?
zVector.equalsc                   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st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.functionsr0   )fieldr0   r#   r%   r&   r0      s    z*Vector.dot.<locals>.directional_derivative)
isinstancer   r   r   r    r'   itemsargsr*   Zsympy.vector.deloperatorr/   	TypeErrorstr)r$   r,   ZoutveckvZvect_dotr/   r0   r%   r#   r&   r*      s"    (


z
Vector.dotc                 C  s
   |  |S Nr*   r$   r,   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.   )	r2   r   r   r    r'   r3   crossr4   outer)r$   r,   Zoutdyadr7   r8   Zcross_productr>   r%   r%   r&   r=      s     

zVector.crossc                 C  s
   |  |S r9   r=   r;   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>      z Vector.outer.<locals>.<listcomp>)
r2   r   r5   r   r   r    r   r'   r3   r   )r$   r,   r4   r%   r%   r&   r>      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)r+   r   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_systemsc                   s   g | ]}  |qS r%   r:   rA   ir#   r%   r&   rD   X  rE   z'Vector._projections.<locals>.<listcomp>)
Zsympy.vector.operatorsrH   r2   r   r
   rF   nextiterbase_vectorstuple)r$   rH   Zbase_vecr%   r#   r&   _projections>  s
    
zVector._projectionsc                 C  s
   |  |S r9   )r>   r;   r%   r%   r&   __or__Z  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%   r:   )rA   Zunit_vecr#   r%   r&   rD   y  rE   z$Vector.to_matrix.<locals>.<listcomp>)MatrixrM   )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'   r3   getrR   r   r    )r$   partsvectZmeasurer%   r%   r&   separate|  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)	r2   r   r5   r
   rF   
ValueError	VectorMulr	   ZNegativeOne)Zoner,   r%   r%   r&   _div_helper  s    


zVector._div_helperN)F)__name__
__module____qualname____doc__Z	is_scalarZ	is_Vector_op_priority__annotations__r   r!   propertyr'   r(   r)   r+   r*   r<   r=   r@   r>   rG   rO   rP   rS   rW   rZ   r%   r%   r%   r&   r      s>   

C>,$

r   c                   s    fdd}|S )Nc                   sN   t ti  }g }| jD ]}t|jtr|| q|tkrJt| jddS d S )NF)deep)r   	VectorAddr4   r2   r!   r   appenddoit)exprZ	vec_classZvectorsZtermclsr%   r&   _postprocessor  s    
z)get_postprocessor.<locals>._postprocessorr%   )rh   ri   r%   rg   r&   get_postprocessor  s    	rj   r   c                      sR   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	dd Z
  ZS )
BaseVectorz)
    Class to denote a base vector.

    Nc                   s   |d u rd |}|d u r$d |}t|}t|}|tddv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)formatr6   rangerX   r2   r   r5   _vector_namessuper__new__r
   _base_instanceOner"   _measure_number_name_pretty_form_latex_form_system_idr   Z_assumptions_sys)rh   indexrR   Z
pretty_strZ	latex_strnameobjZassumptions	__class__r%   r&   rs     s0    






zBaseVector.__new__c                 C  s   | j S r9   )rz   r#   r%   r%   r&   rR     s    zBaseVector.systemc                 C  s   | j S r9   )rw   )r$   printerr%   r%   r&   	_sympystr  s    zBaseVector._sympystrc                 C  s"   | j \}}||d |j|  S )Nrm   )r{   _printrq   )r$   r   r}   rR   r%   r%   r&   
_sympyrepr  s    
zBaseVector._sympyreprc                 C  s   | hS r9   r%   r#   r%   r%   r&   free_symbols  s    zBaseVector.free_symbolsc                 C  s   | S r9   r%   r#   r%   r%   r&   _eval_conjugate  s    zBaseVector._eval_conjugate)NN)r[   r\   r]   r^   rs   ra   rR   r   r   r   r   __classcell__r%   r%   r   r&   rk     s   #

rk   c                   @  s    e Zd ZdZdd Zdd ZdS )rc   z2
    Class to denote sum of Vector instances.
    c                 O  s   t j| g|R i |}|S r9   )r   rs   rh   r4   optionsr   r%   r%   r&   rs     s    zVectorAdd.__new__c           	      C  sz   d}t |   }|jdd d |D ]D\}}| }|D ].}||jv r<| j| | }|||d 7 }q<q(|d d S )Nrn   c                 S  s   | d   S )Nr   )__str__)xr%   r%   r&   <lambda>  rE   z%VectorAdd._sympystr.<locals>.<lambda>keyz + )listrW   r3   sortrM   r'   r   )	r$   r   Zret_strr3   rR   rV   Z
base_vectsr   Z	temp_vectr%   r%   r&   r     s    
zVectorAdd._sympystrN)r[   r\   r]   r^   rs   r   r%   r%   r%   r&   rc     s   rc   c                   @  s0   e Zd ZdZdd Zedd Zedd ZdS )	rY   z>
    Class to denote products of scalars and BaseVectors.
    c                 O  s   t j| g|R i |}|S r9   )r   rs   r   r%   r%   r&   rs     s    zVectorMul.__new__c                 C  s   | j S )z) The BaseVector involved in the product. )rt   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.
        )rv   r#   r%   r%   r&   measure_number  s    zVectorMul.measure_numberN)r[   r\   r]   r^   rs   ra   r   r   r%   r%   r%   r&   rY     s   
rY   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 r9   )r   rs   )rh   r   r%   r%   r&   rs   %  s    
zVectorZero.__new__N)r[   r\   r]   r^   r_   rx   ry   rs   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 r9   )r   r   r   r   rs   _expr1_expr2rh   expr1Zexpr2r   r%   r%   r&   rs   <  s    zCross.__new__c                 K  s   t | j| jS r9   )r=   r   r   r$   hintsr%   r%   r&   re   F  s    z
Cross.doitNr[   r\   r]   r^   rs   re   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   rs   r   r   r   r%   r%   r&   rs   ^  s    zDot.__new__c                 K  s   t | j| jS r9   )r*   r   r   r   r%   r%   r&   re   g  s    zDot.doitNr   r%   r%   r%   r&   r   J  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rtjS h d	||h
 }|d d |krdnd}| j |  S dd	lm} z| j}W n ty   t  Y S 0 t|S t  ts,t tr2tjS t  trbt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  s   | ]}t | V  qd S r9   r?   rI   vect2r%   r&   	<genexpr>|  rE   zcross.<locals>.<genexpr>c                 3  s   | ]}t  |V  qd S r9   r?   rI   vect1r%   r&   r   ~  rE   r   >   r   r.      r.   rl   express)r2   r   rc   fromiterr4   rk   r|   r   r    
differencepoprM   	functionsr   rX   r   r=   r   rY   rK   rL   r'   r3   )r   r   Zn1Zn2Zn3signr   r8   rB   m1rC   m2r%   r   r   r&   r=   k  s8    




r=   c                   sD  t  tr$tfdd jD S t trHt fddjD S t  trt tr jjkr| krvtjS tjS ddl	m
} z| j}W n ty   t  Y S 0 t |S t  tst trt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 r9   r:   rI   r   r%   r&   r     rE   zdot.<locals>.<genexpr>c                 3  s   | ]}t  |V  qd S r9   r:   rI   r   r%   r&   r     rE   r.   r   )r2   r   r   r4   rk   r|   r
   ru   rF   r   r   rX   r   r*   r   rY   rK   rL   r'   r3   )r   r   r   r8   rB   r   rC   r   r%   r   r&   r*     s,    


r*   N)7
__future__r   	itertoolsr   Z
sympy.corer   r   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   rQ   Zsympy.vector.basisdependentr   r   r   r   Zsympy.vector.coordsysrectr   Zsympy.vector.dyadicr   r   r   Zsympy.vector.kindr   r   rj   Z"_constructor_postprocessor_mappingrk   rc   rY   r   r   r   r=   r*   r   r   r   r   r   r    r%   r%   r%   r&   <module>   sF      

< !0*