o
    8Va,                     @   s   d dl mZmZ 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 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 )    )AnyDict)simplifytrigsimp)call_highest_priority
_sympifyit)	StdFactKB)factordiffIntegral)SAddMul)Exprc                   @   sx  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d;d!d"Ze jejj7  _eZ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/d0 Zd1d2 Ze jej7  _d<d3d4Zd5d6 Zd7d8 Z e  je!j7  _d9d: 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 N	_add_funcselfother r   =/usr/lib/python3/dist-packages/sympy/vector/basisdependent.py__add__      zBasisDependent.__add__r   c                 C      |  || S r   r   r   r   r   r   r      r   zBasisDependent.__radd____rsub__c                 C   s   |  | | S r   r   r   r   r   r   __sub__      zBasisDependent.__sub__r   c                 C   s   |  ||  S r   r   r   r   r   r   r      r    zBasisDependent.__rsub__r   __rmul__c                 C   r   r   	_mul_funcr   r   r   r   __mul__#      zBasisDependent.__mul__r$   c                 C   r   r   r"   r   r   r   r   r!   (   r%   zBasisDependent.__rmul__c                 C   s   |  tj| S r   )r#   r   ZNegativeOner   r   r   r   __neg__-   s   zBasisDependent.__neg____rtruediv__c                 C   
   |  |S r   )Z_div_helperr   r   r   r   __truediv__0   s   
zBasisDependent.__truediv__r*   c                 C   s   t dS )NzInvalid divisor for division)	TypeErrorr   r   r   r   r(   5   s   zBasisDependent.__rtruediv__   Nd   Fc                 C   sJ   ||||||d}| j }	| j D ]\}
}|	|j|fi ||
 7 }	q|	S )z
        Implements the SymPy evalf routine for this quantity.

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

        )subsmaxnchopstrictquadverbose)zero
componentsitemsevalf)r   nr.   r/   r0   r1   r2   r3   optionsZveckvr   r   r   r7   9   s   zBasisDependent.evalfc                    "    fdd| j  D }| j| S )z
        Implements the SymPy simplify routine for this quantity.

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

        c                    $   g | ]\}}t |fi  | qS r   )simp.0r:   r;   kwargsr   r   
<listcomp>T       z+BasisDependent.simplify.<locals>.<listcomp>r5   r6   r   )r   rB   Zsimp_componentsr   rA   r   r   L      

zBasisDependent.simplifyc                    r<   )z
        Implements the SymPy trigsimp routine, for this quantity.

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

        c                    r=   r   )tsimpr?   optsr   r   rC   b   rD   z+BasisDependent.trigsimp.<locals>.<listcomp>rE   )r   rI   Ztrig_componentsr   rH   r   r   Z   rF   zBasisDependent.trigsimpc                 K      | j di |S Nr   )r   )r   rB   r   r   r   _eval_simplifyh      zBasisDependent._eval_simplifyc                 K   rJ   rK   )r   )r   rI   r   r   r   _eval_trigsimpk   rM   zBasisDependent._eval_trigsimpc                 C   r)   r   )r
   )r   Zwrtr   r   r   _eval_derivativen   s   
zBasisDependent._eval_derivativec                    $    fdd| j  D }| j| S )Nc                    s*   g | ]\}}t |gR i  | qS r   )r   r?   assumptionssymbolsr   r   rC   r       z1BasisDependent._eval_Integral.<locals>.<listcomp>rE   )r   rS   rR   Zintegral_componentsr   rQ   r   _eval_Integralq   s   
zBasisDependent._eval_Integralc                 C   s
   | t jfS )z
        Returns the expression as a tuple wrt the following
        transformation -

        expression -> a/b -> a, b

        r   Oner&   r   r   r   as_numer_denomv   s   
zBasisDependent.as_numer_denomc                    rP   )z
        Implements the SymPy factor routine, on the scalar parts
        of a basis-dependent expression.

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

        c                    *   g | ]\}}t |g R i | qS r   )fctrr?   argsrB   r   r   rC      rT   z)BasisDependent.factor.<locals>.<listcomp>rE   )r   r\   rB   Zfctr_componentsr   r[   r   r	      s   	
zBasisDependent.factorc                 C   s
   t j| fS )z2Efficiently extract the coefficient of a product. rV   )r   Zrationalr   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   r5   r@   xr&   r   r   rC      s    z/BasisDependent.as_coeff_add.<locals>.<listcomp>r   )r5   tuple)r   Zdepslr   r&   r   as_coeff_add   s   zBasisDependent.as_coeff_addc                    s@    D ]}t |trtdq fdd| j D }| j| S )z
        Implements the SymPy diff routine, for vectors.

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

        zInvalid arg for differentiationc                    rY   r   )dfr?   r[   r   r   rC      rT   z'BasisDependent.diff.<locals>.<listcomp>)
isinstancer   r+   r5   r6   r   )r   r\   rB   r`   Zdiff_componentsr   r[   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d i  | qS )r   )r5   doitr_   hintsr   r   r   rC      s    z'BasisDependent.doit.<locals>.<listcomp>)r5   r   )r   rh   Zdoit_componentsr   rg   r   rf      s   
zBasisDependent.doit)r,   Nr-   FFNF)F)#__name__
__module____qualname____doc__r   r   r   r   r   r   NotImplementedr$   r!   r'   r*   r(   r7   r   r8   r   r>   r   rG   rL   rN   rO   rU   rX   r	   rZ   r]   rc   r
   rd   rf   r   r   r   r   r      sR    







r   c                       s    e Zd ZdZ fddZ  ZS )BasisDependentAddzt
    Denotes sum of basis dependent quantities such that they cannot
    be expressed as base or Mul instances.
    c           
         sJ  i  t |D ]H\}}t|| js0t|tr| j|j }nt|tr(| j|j }ntt	|d || j
kr6qt|drN|jD ]} |d|j|   |< q>qt  }|D ]} | dkrb |= qWt dkrl| j
S  fdd D }t j| g|R i |}t|tr| j|j S ddi}	t|	|_ |_t  d j|_|S )Nz  cannot be interpreted correctlyr5   r   c                    s   g | ]}| |  qS r   r   r_   r^   r   r   rC      s    z-BasisDependentAdd.__new__.<locals>.<listcomp>commutativeT)	enumeratere   
_expr_typer   r#   r\   r   r   r+   strr4   hasattrr5   getlistkeyslensuper__new__r   _assumptions_components_sys)
clsr\   r9   iargr`   ZtempnewargsobjrR   	__class__r^   r   ry      s@   






zBasisDependentAdd.__new__)ri   rj   rk   rl   ry   __classcell__r   r   r   r   rn      s    rn   c                       s(   e Zd ZdZ fddZdd 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jd}g }	|D ]M}
t|
 jr&|d7 }d}q|
tj	kr.d}qt|
 j
 jfrD|d7 }|
j}|
j9 qt|
 jrQ|d7 }|
}qt|
||||fr`|	|
 q|
9 q|dkrmtd|dkrxt|i |S |r} jS t| jr fdd|jD } j| S t j |jg|	R i |}t|tr j|j S |j|_|_d	di}t||_|ji|_|jj|_|S )
Nr   )CrossDotCurlGradientF   TzInvalid multiplicationc                    s   g | ]}  |qS r   r"   r_   r}   Zmeasure_numberr   r   rC     s    z-BasisDependentMul.__new__.<locals>.<listcomp>ro   )Zsympy.vectorr   r   r   r   r   rW   re   
_zero_funcZeroZ
_base_funcr#   _base_instance_measure_numberr   append
ValueErrorr   r4   r\   rx   ry   r   r   rz   r{   r|   )r}   r\   r9   r   r   r   r   countZzeroflag
extra_argsr   exprr   r   rR   r   r   r   ry      s`   






zBasisDependentMul.__new__c                 C   sD   | | j}d|v sd|v sd|v rd| d }|d | | j S )N(-+)*)Z_printr   r   )r   printerZmeasure_strr   r   r   	_sympystr   s   zBasisDependentMul._sympystr)ri   rj   rk   rl   ry   r   r   r   r   r   r   r      s    :r   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 Z  ZS )BasisDependentZeroz:
    Class to denote a zero basis dependent instance.
    c                    s$   t  | }ttj| g |_|S r   )rx   ry   ra   r   r   __hash___hash)r}   r   r   r   r   ry   0  s   zBasisDependentZero.__new__c                 C   s   | j S r   )r   r&   r   r   r   r   7  s   zBasisDependentZero.__hash____req__c                 C   s   t || jS r   )re   r   r   r   r   r   __eq__:  r   zBasisDependentZero.__eq__r   c                 C      t || jr|S tdNz#Invalid argument types for additionre   rq   r+   r   r   r   r   r   @     zBasisDependentZero.__add__r   c                 C   r   r   r   r   r   r   r   r   G  r   zBasisDependentZero.__radd__r   c                 C   s   t || jr	| S tdNz&Invalid argument types for subtractionr   r   r   r   r   r   N  s   zBasisDependentZero.__sub__r   c                 C   r   r   r   r   r   r   r   r   U  r   zBasisDependentZero.__rsub__c                 C      | S r   r   r&   r   r   r   r'   \     zBasisDependentZero.__neg__c                 C   r   )z@
        Returns the normalized version of this vector.
        r   r&   r   r   r   	normalize_  s   zBasisDependentZero.normalizec                 C   s   dS )N0r   )r   r   r   r   r   r   e  r   zBasisDependentZero._sympystr)ri   rj   rk   rl   r5   ry   r   r   r   r   r   r   r   r   r'   r   r   r   r   r   r   r   r   (  s&    




r   N)typingr   r   Zsympy.simplifyr   r>   r   rG   Zsympy.core.decoratorsr   r   Zsympy.core.assumptionsr   Zsympyr	   rZ   r
   rd   r   Z
sympy.corer   r   r   Zsympy.core.exprr   r   rn   r   r   r   r   r   r   <module>   s     &1G