B
    [-                 @   s   d dl 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mZmZmZ d dl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ZdS )    )simplifytrigsimp)call_highest_priority
_sympifyit)	StdFactKB)factordiffIntegral)SAddMul	count_ops)Exprc               @   s  e Zd ZdZeddd Zeddd Zedd	d
 Zeddd Ze	de
eddd Ze	de
eddd Zdd Ze	de
eddd Zeddd ZeZeZd<ddZe jejj7  _eZd ed!d!fd"d#Ze jej7  _d$d% Ze jej7  _d&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Z e  je!j7  _d=d4d5Z"d6d7 Z#d8d9 Z$e$ je%j7  _d:d; Z&dS )>BasisDependentz
    Super class containing functionality common to vectors and
    dyadics.
    Named so because the representation of these quantities in
    sympy.vector is dependent on the basis they are expressed in.
    __radd__c             C   s   |  | |S )N)	_add_func)selfother r   :lib/python3.7/site-packages/sympy/vector/basisdependent.py__add__   s    zBasisDependent.__add__r   c             C   s   |  || S )N)r   )r   r   r   r   r   r      s    zBasisDependent.__radd____rsub__c             C   s   |  | | S )N)r   )r   r   r   r   r   __sub__   s    zBasisDependent.__sub__r   c             C   s   |  ||  S )N)r   )r   r   r   r   r   r      s    zBasisDependent.__rsub__r   __rmul__c             C   s   |  | |S )N)	_mul_func)r   r   r   r   r   __mul__!   s    zBasisDependent.__mul__r   c             C   s   |  || S )N)r   )r   r   r   r   r   r   &   s    zBasisDependent.__rmul__c             C   s   |  td| S )N)r   r
   )r   r   r   r   __neg__+   s    zBasisDependent.__neg____rdiv__c             C   s
   |  |S )N)Z_div_helper)r   r   r   r   r   __div__.   s    zBasisDependent.__div__r   c             C   s   t dS )NzInvalid divisor for division)	TypeError)r   r   r   r   r   r   3   s    zBasisDependent.__rdiv__Nc             K   s8   | j }x,| j D ]\}}||j|f|| 7 }qW |S )z
        Implements the SymPy evalf routine for this quantity.

        evalf's documentation
        =====================

        )zero
componentsitemsevalf)r   ZprecoptionsZveckvr   r   r   r$   :   s    zBasisDependent.evalfg333333?Fc                s(    fdd| j  D }| j| S )z
        Implements the SymPy simplify routine for this quantity.

        simplify's documentation
        ========================

        c          	      s&   g | ]\}}t | d | qS ))ratiomeasurerationalinverse)simp).0r&   r'   )r+   r)   r(   r*   r   r   
<listcomp>S   s   z+BasisDependent.simplify.<locals>.<listcomp>)r"   r#   r   )r   r(   r)   r*   r+   Zsimp_componentsr   )r+   r)   r(   r*   r   r   K   s    zBasisDependent.simplifyc                s"    fdd| j  D }| j| S )z
        Implements the SymPy trigsimp routine, for this quantity.

        trigsimp's documentation
        ========================

        c                s    g | ]\}}t |f | qS r   )tsimp)r-   r&   r'   )optsr   r   r.   b   s   z+BasisDependent.trigsimp.<locals>.<listcomp>)r"   r#   r   )r   r0   Ztrig_componentsr   )r0   r   r   Z   s    
zBasisDependent.trigsimpc             C   s   | j ||||dS )N)r(   r)   r*   r+   )r   )r   r(   r)   r*   r+   r   r   r   _eval_simplifyh   s    zBasisDependent._eval_simplifyc             K   s   | j f |S )N)r   )r   r0   r   r   r   _eval_trigsimpk   s    zBasisDependent._eval_trigsimpc             C   s
   |  |S )N)r   )r   Zwrtr   r   r   _eval_derivativen   s    zBasisDependent._eval_derivativec                s$    fdd| j  D }| j| S )Nc                s$   g | ]\}}t |f | qS r   )r	   )r-   r&   r'   )assumptionssymbolsr   r   r.   r   s   z1BasisDependent._eval_Integral.<locals>.<listcomp>)r"   r#   r   )r   r5   r4   Zintegral_componentsr   )r4   r5   r   _eval_Integralq   s    zBasisDependent._eval_Integralc             O   s   | j ||S )N)r   )r   argskwargsr   r   r   
_eval_diffv   s    zBasisDependent._eval_diffc             C   s   | dfS )z
        Returns the expression as a tuple wrt the following
        transformation -

        expression -> a/b -> a, b

           r   )r   r   r   r   as_numer_denomy   s    zBasisDependent.as_numer_denomc                s$    fdd| j  D }| j| S )z
        Implements the SymPy factor routine, on the scalar parts
        of a basis-dependent expression.

        factor's documentation
        ========================

        c                s$   g | ]\}}t |f | qS r   )fctr)r-   r&   r'   )r7   r8   r   r   r.      s   z)BasisDependent.factor.<locals>.<listcomp>)r"   r#   r   )r   r7   r8   Zfctr_componentsr   )r7   r8   r   r      s    	zBasisDependent.factorc             C   s   t d| fS )z2Efficiently extract the coefficient of a product. r:   )r
   )r   r*   r   r   r   as_coeff_Mul   s    zBasisDependent.as_coeff_Mulc                s     fdd j D }dt|fS )z4Efficiently extract the coefficient of a summation. c                s   g | ]}| j |  qS r   )r"   )r-   x)r   r   r   r.      s    z/BasisDependent.as_coeff_add.<locals>.<listcomp>r   )r"   tuple)r   Zdepslr   )r   r   as_coeff_add   s    zBasisDependent.as_coeff_addc                sD   x D ]}t |trtdqW  fdd| j D }| j| S )z
        Implements the SymPy diff routine, for vectors.

        diff's documentation
        ========================

        zInvalid arg for differentiationc                s$   g | ]\}}t |f | qS r   )df)r-   r&   r'   )r7   r8   r   r   r.      s   z'BasisDependent.diff.<locals>.<listcomp>)
isinstancer   r    r"   r#   r   )r   r7   r8   r>   Zdiff_componentsr   )r7   r8   r   r      s    

zBasisDependent.diffc                s     fddj D }j| S )z(Calls .doit() on each term in the Dyadicc                s"   g | ]}j | jf  | qS r   )r"   doit)r-   r>   )hintsr   r   r   r.      s   z'BasisDependent.doit.<locals>.<listcomp>)r"   r   )r   rE   Zdoit_componentsr   )rE   r   r   rD      s    
zBasisDependent.doit)N)F)'__name__
__module____qualname____doc__r   r   r   r   r   r   NotImplementedr   r   r   r   r   __truediv____rtruediv__r$   r   nr   r   r,   r   r/   r1   r2   r3   r6   r9   r;   r   r<   r=   rA   r   rB   rD   r   r   r   r   r   	   sF   


r   c                   s&   e Zd ZdZ fddZejZ  ZS )BasisDependentAddzt
    Denotes sum of basis dependent quantities such that they cannot
    be expressed as base or Mul instances.
    c       
         sV  i  xt |D ]\}}t|| jsbt|tr:| j|j }n(t|trR| j|j }ntt	|d || j
krnqt|drx(|jD ]} |d|j|   |< qW qW t  }x|D ]} | dkr |= qW t dkr| j
S  fdd D }tt| j| f||}t|tr&| j|j S ddi}	t|	|_ |_t  d j|_|S )Nz  cannot be interpreted correctlyr"   r   c                s   g | ]}| |  qS r   r   )r-   r>   )r"   r   r   r.      s    z-BasisDependentAdd.__new__.<locals>.<listcomp>commutativeT)	enumeraterC   
_expr_typer   r   r7   r   r   r    strr!   hasattrr"   getlistkeyslensuperrN   __new__r   _assumptions_components_sys)
clsr7   r%   iargr>   Ztempnewargsobjr4   )	__class__)r"   r   rY      s<    



"



zBasisDependentAdd.__new__)rF   rG   rH   rI   rY   r   __init____classcell__r   r   )rb   r   rN      s   +rN   c                   s8   e Zd ZdZ fddZejZdddZeZeZ	  Z
S )BasisDependentMulzJ
    Denotes product of base- basis dependent quantity with a scalar.
    c                s  ddl m}m}m}m} d}tdd}g }	x|D ]}
t|
 jrP|d7 }d}q2|
tdkrbd}q2t|
 j j	fr|d7 }|
j
}|
j9 q2t|
 jr|d7 }|
}q2t|
||||fr|	|
 q2|
9 q2W |dkrtdn|dkrt||S |r jS t| jr0 fdd|jD } j| S tt j |j
f|	|}t|trf j|j S |j
|_
|_d	di}t||_|j
i|_|j
j|_|S )
Nr   )CrossDotCurlGradientr:   FTzInvalid multiplicationc                s   g | ]}  |qS r   )r   )r-   r>   )r]   measure_numberr   r   r.     s   z-BasisDependentMul.__new__.<locals>.<listcomp>rO   )Zsympy.vectorrf   rg   rh   ri   r
   rC   
_zero_funcZ
_base_funcr   _base_instance_measure_numberr   append
ValueErrorr   r!   r7   rX   re   rY   r   r   rZ   r[   r\   )r]   r7   r%   rf   rg   rh   ri   countZzeroflagZ
extra_argsr_   exprr`   ra   r4   )rb   )r]   rj   r   rY      sX    






zBasisDependentMul.__new__Nc             C   sB   | j  }d|ks"d|ks"d|kr.d| d }|d | j| S )N(-+)*)rm   __str__rl   )r   printerZmeasure_strr   r   r   rw   (  s
    
zBasisDependentMul.__str__)N)rF   rG   rH   rI   rY   r   rc   rw   __repr__	_sympystrrd   r   r   )rb   r   re      s   :
re   c                   s   e Zd ZdZi Z fddZdd Zeddd ZeZ	ed	d
d Z
eddd Zeddd Zeddd Zdd Zdd ZdddZeZeZ  ZS )BasisDependentZeroz:
    Class to denote a zero basis dependent instance.
    c                s*   t t| | }ttd| g |_|S )Nr   )rX   r{   rY   r?   r
   __hash___hash)r]   ra   )rb   r   r   rY   9  s    zBasisDependentZero.__new__c             C   s   | j S )N)r}   )r   r   r   r   r|   @  s    zBasisDependentZero.__hash____req__c             C   s   t || jS )N)rC   rk   )r   r   r   r   r   __eq__C  s    zBasisDependentZero.__eq__r   c             C   s   t || jr|S tdd S )Nz#Invalid argument types for addition)rC   rQ   r    )r   r   r   r   r   r   I  s    zBasisDependentZero.__add__r   c             C   s   t || jr|S tdd S )Nz#Invalid argument types for addition)rC   rQ   r    )r   r   r   r   r   r   P  s    zBasisDependentZero.__radd__r   c             C   s   t || jr| S tdd S )Nz&Invalid argument types for subtraction)rC   rQ   r    )r   r   r   r   r   r   W  s    zBasisDependentZero.__sub__r   c             C   s   t || jr|S tdd S )Nz&Invalid argument types for subtraction)rC   rQ   r    )r   r   r   r   r   r   ^  s    zBasisDependentZero.__rsub__c             C   s   | S )Nr   )r   r   r   r   r   e  s    zBasisDependentZero.__neg__c             C   s   | S )z@
        Returns the normalized version of this vector.
        r   )r   r   r   r   	normalizeh  s    zBasisDependentZero.normalizeNc             C   s   dS )N0r   )r   rx   r   r   r   rw   n  s    zBasisDependentZero.__str__)N)rF   rG   rH   rI   r"   rY   r|   r   r   r~   r   r   r   r   r   r   rw   ry   rz   rd   r   r   )rb   r   r{   3  s   
r{   N)Zsympy.simplifyr   r,   r   r/   Zsympy.core.decoratorsr   r   Zsympy.core.assumptionsr   Zsympyr   r<   r   rB   r	   Z
sympy.corer
   r   r   r   Zsympy.core.exprr   r   rN   re   r{   r   r   r   r   <module>   s    +4L