U
    ZÊ¹j.G  ã                   @   s\   d dl mZmZmZ d dlmZ d dlmZ d dl	m
Z
 dgZG dd„ deeƒZdd„ Zd	S )
é    )ÚsympifyÚAddÚImmutableMatrix)Ú
EvalfMixin)Ú	Printable)Úprec_to_dpsÚDyadicc                   @   sì   e Zd ZdZdZdd„ Zedd„ ƒZdd„ ZeZ	d	d
„ Z
e
Zdd„ ZeZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ ZeZd4d"d#„Zd5d$d%„Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Z d2d3„ Z!d!S )6r   ay  A Dyadic object.

    See:
    https://en.wikipedia.org/wiki/Dyadic_tensor
    Kane, T., Levinson, D. Dynamics Theory and Applications. 1985 McGraw-Hill

    A more powerful way to represent a rigid body's inertia. While it is more
    complex, by choosing Dyadic components to be in body fixed basis vectors,
    the resulting matrix is equivalent to the inertia tensor.

    Fc                 C   sb  g | _ |dkrg }t|ƒdkrîd}t| j ƒD ]˜\}}t|d d ƒt| j | d ƒkr,t|d d ƒt| j | d ƒkr,| j | d |d d  |d d |d d f| j |< | |d ¡ d} qÆq,|dkr| j  |d ¡ | |d ¡ qd}|t| j ƒk �r^| j | d dk| j | d dkB | j | d dkB �rT| j  | j | ¡ |d8 }|d7 }qòdS )a2  
        Just like Vector's init, you should not call this unless creating a
        zero dyadic.

        zd = Dyadic(0)

        Stores a Dyadic as a list of lists; the inner list has the measure
        number and the two unit vectors; the outerlist holds each unique
        unit vector pair.

        r   é   é   N)ÚargsÚlenÚ	enumerateÚstrÚremoveÚappend)ÚselfÚinlistÚaddedÚiÚv© r   úZ/var/www/html/bwave/python/venv/lib/python3.8/site-packages/sympy/physics/vector/dyadic.pyÚ__init__   s6    " ÿ
 
ÿ
"ÿzDyadic.__init__c                 C   s   t S )zReturns the class Dyadic. )r   ©r   r   r   r   Úfunc@   s    zDyadic.funcc                 C   s   t |ƒ}t| j|j ƒS )zThe add operator for Dyadic. )Ú_check_dyadicr   r   ©r   Úotherr   r   r   Ú__add__E   s    zDyadic.__add__c                 C   sV   t | jƒ}t|ƒ}t|ƒD ]2\}}||| d  || d || d f||< qt|ƒS )a…  Multiplies the Dyadic by a sympifyable expression.

        Parameters
        ==========

        other : Sympafiable
            The scalar to multiply this Dyadic with

        Examples
        ========

        >>> from sympy.physics.vector import ReferenceFrame, outer
        >>> N = ReferenceFrame('N')
        >>> d = outer(N.x, N.x)
        >>> 5 * d
        5*(N.x|N.x)

        r   r	   r
   )Úlistr   r   r   r   )r   r   Znewlistr   r   r   r   r   Ú__mul__L   s    

ÿ
zDyadic.__mul__c                 C   s¾   ddl m}m} t|tƒr|t|ƒ}tdƒ}| jD ]H}|jD ]<}||d |d  |d  |d ¡ |d  |d ¡ 7 }q:q0n>||ƒ}|dƒ}| jD ]&}||d |d  |d  |¡ 7 }q’|S )aò  The inner product operator for a Dyadic and a Dyadic or Vector.

        Parameters
        ==========

        other : Dyadic or Vector
            The other Dyadic or Vector to take the inner product with

        Examples
        ========

        >>> from sympy.physics.vector import ReferenceFrame, outer
        >>> N = ReferenceFrame('N')
        >>> D1 = outer(N.x, N.y)
        >>> D2 = outer(N.y, N.y)
        >>> D1.dot(D2)
        (N.x|N.y)
        >>> D1.dot(N.y)
        N.x

        r   )ÚVectorÚ_check_vectorr
   r	   )	Úsympy.physics.vector.vectorr!   r"   Ú
isinstancer   r   r   ÚdotÚouter)r   r   r!   r"   Úolr   Zv2r   r   r   r%   h   s    


>
$z
Dyadic.dotc                 C   s   |   d| ¡S )z0Divides the Dyadic by a sympifyable expression. r	   )r    r   r   r   r   Ú__truediv__�   s    zDyadic.__truediv__c                 C   s\   |dkrt dƒ}t|ƒ}| jg kr0|jg kr0dS | jg ksD|jg krHdS t| jƒt|jƒkS )z[Tests for equality.

        Is currently weak; needs stronger comparison testing

        r   TF)r   r   r   Úsetr   r   r   r   Ú__eq__“   s    zDyadic.__eq__c                 C   s
   | |k S )Nr   r   r   r   r   Ú__ne__£   s    zDyadic.__ne__c                 C   s   | d S ©Néÿÿÿÿr   r   r   r   r   Ú__neg__¦   s    zDyadic.__neg__c           	      C   sŽ  | j }t|ƒdkrtdƒS g }t|ƒD �]$\}}|| d dkrt| d| || d ¡ d | || d ¡ ¡ q&|| d dkr¸| d| || d ¡ d | || d ¡ ¡ q&|| d dkr&| || d ¡}t|| d tƒrôd| }| d	¡�r|dd … }d}nd}| || | || d ¡ d | || d ¡ ¡ q&d
 	|¡}| d¡�rr|dd … }n| d¡�rŠ|dd … }|S )Nr   r	   ú + z\otimes r
   r-   ú - ú(%s)ú-Ú é   ú )
r   r   r   r   r   Ú_printr$   r   Ú
startswithÚjoin©	r   ÚprinterÚarr'   r   r   Úarg_strÚ	str_startÚoutstrr   r   r   Ú_latex©   sH    ÿÿþýÿÿ
zDyadic._latexc                    s   | ‰ G ‡ ‡fdd„dƒ}|ƒ S )Nc                       s   e Zd ZdZ‡ ‡fdd„ZdS )zDyadic._pretty.<locals>.Faker   c                    sª  ˆ j }ˆ}t|ƒdkrtdƒS ˆjr(dnd}g }t|ƒD �].\}}|| d dkr‚| d| || d ¡|| || d ¡g¡ q8|| d dkrÂ| d| || d ¡|| || d ¡g¡ q8|| d dkr8t|| d tƒ�r| 	|| d ¡ 
¡ d }	n| || d ¡}	|	 d	¡�r2|	dd … }	d}
nd}
| |
|	d
| || d ¡|| || d ¡g¡ q8d |¡}| d¡�rŽ|dd … }n| d
¡�r¦|dd … }|S )Nr   u   âŠ—ú|r	   r/   r
   r-   r0   r2   r5   r3   r4   )r   r   r   Z_use_unicoder   ÚextendZdoprintr$   r   r6   Zparensr7   r8   )r   r   Úkwargsr;   ZmppÚbarr'   r   r   r<   r=   r>   ©Úer:   r   r   ÚrenderÓ   sT    ýý
ÿÿ
ý
z#Dyadic._pretty.<locals>.Fake.renderN)Ú__name__Ú
__module__Ú__qualname__ZbaselinerF   r   rD   r   r   ÚFakeÐ   s   rJ   r   )r   r:   rJ   r   rD   r   Ú_prettyÍ   s    1zDyadic._prettyc                 C   s   d|  | S r,   r   r   r   r   r   Ú__rsub__  s    zDyadic.__rsub__c           	      C   s¤  | j }t|ƒdkr| d¡S g }t|ƒD �]8\}}|| d dkrz| d| || d ¡ d | || d ¡ d ¡ q(|| d dkrÂ| d| || d ¡ d | || d ¡ d ¡ q(|| d dkr(| || d ¡}t|| d tƒ�r d	| }|d d
k�r |dd… }d}nd}| || d | || d ¡ d | || d ¡ d ¡ q(d |¡}| d¡�rˆ|dd… }n| d¡�r |dd… }|S )zPrinting method. r   r	   z + (r@   r
   ú)r-   z - (r1   r2   Nr0   r/   z*(r3   r4   r5   )	r   r   r6   r   r   r$   r   r8   r7   r9   r   r   r   Ú	_sympystr  sP    
ÿÿÿÿÿþþþ
zDyadic._sympystrc                 C   s   |   |d ¡S )zThe subtraction operator. r-   )r   r   r   r   r   Ú__sub__*  s    zDyadic.__sub__c                 C   sP   ddl m} ||ƒ}tdƒ}| jD ](}||d |d  |d  |¡¡ 7 }q"|S )a¢  Returns the dyadic resulting from the dyadic vector cross product:
        Dyadic x Vector.

        Parameters
        ==========
        other : Vector
            Vector to cross with.

        Examples
        ========
        >>> from sympy.physics.vector import ReferenceFrame, outer, cross
        >>> N = ReferenceFrame('N')
        >>> d = outer(N.x, N.x)
        >>> cross(d, N.y)
        (N.x|N.z)

        r   )r"   r	   r
   )r#   r"   r   r   r&   Úcross)r   r   r"   r'   r   r   r   r   rP   .  s    
&zDyadic.crossNc                 C   s   ddl m} || ||ƒS )a  Expresses this Dyadic in alternate frame(s)

        The first frame is the list side expression, the second frame is the
        right side; if Dyadic is in form A.x|B.y, you can express it in two
        different frames. If no second frame is given, the Dyadic is
        expressed in only one frame.

        Calls the global express function

        Parameters
        ==========

        frame1 : ReferenceFrame
            The frame to express the left side of the Dyadic in
        frame2 : ReferenceFrame
            If provided, the frame to express the right side of the Dyadic in

        Examples
        ========

        >>> from sympy.physics.vector import ReferenceFrame, outer, dynamicsymbols
        >>> from sympy.physics.vector import init_vprinting
        >>> init_vprinting(pretty_print=False)
        >>> N = ReferenceFrame('N')
        >>> q = dynamicsymbols('q')
        >>> B = N.orientnew('B', 'Axis', [q, N.z])
        >>> d = outer(N.x, N.x)
        >>> d.express(B, N)
        cos(q)*(B.x|N.x) - sin(q)*(B.y|N.x)

        r   )Úexpress)Úsympy.physics.vector.functionsrQ   )r   Zframe1Zframe2rQ   r   r   r   rQ   J  s     zDyadic.expressc                    s,   ˆ dkr|‰ t ‡ ‡fdd„|D ƒƒ dd¡S )a�  Returns the matrix form of the dyadic with respect to one or two
        reference frames.

        Parameters
        ----------
        reference_frame : ReferenceFrame
            The reference frame that the rows and columns of the matrix
            correspond to. If a second reference frame is provided, this
            only corresponds to the rows of the matrix.
        second_reference_frame : ReferenceFrame, optional, default=None
            The reference frame that the columns of the matrix correspond
            to.

        Returns
        -------
        matrix : ImmutableMatrix, shape(3,3)
            The matrix that gives the 2D tensor form.

        Examples
        ========

        >>> from sympy import symbols, trigsimp
        >>> from sympy.physics.vector import ReferenceFrame
        >>> from sympy.physics.mechanics import inertia
        >>> Ixx, Iyy, Izz, Ixy, Iyz, Ixz = symbols('Ixx, Iyy, Izz, Ixy, Iyz, Ixz')
        >>> N = ReferenceFrame('N')
        >>> inertia_dyadic = inertia(N, Ixx, Iyy, Izz, Ixy, Iyz, Ixz)
        >>> inertia_dyadic.to_matrix(N)
        Matrix([
        [Ixx, Ixy, Ixz],
        [Ixy, Iyy, Iyz],
        [Ixz, Iyz, Izz]])
        >>> beta = symbols('beta')
        >>> A = N.orientnew('A', 'Axis', (beta, N.x))
        >>> trigsimp(inertia_dyadic.to_matrix(A))
        Matrix([
        [                           Ixx,                                           Ixy*cos(beta) + Ixz*sin(beta),                                           -Ixy*sin(beta) + Ixz*cos(beta)],
        [ Ixy*cos(beta) + Ixz*sin(beta), Iyy*cos(2*beta)/2 + Iyy/2 + Iyz*sin(2*beta) - Izz*cos(2*beta)/2 + Izz/2,                 -Iyy*sin(2*beta)/2 + Iyz*cos(2*beta) + Izz*sin(2*beta)/2],
        [-Ixy*sin(beta) + Ixz*cos(beta),                -Iyy*sin(2*beta)/2 + Iyz*cos(2*beta) + Izz*sin(2*beta)/2, -Iyy*cos(2*beta)/2 + Iyy/2 - Iyz*sin(2*beta) + Izz*cos(2*beta)/2 + Izz/2]])

        Nc                    s&   g | ]}ˆ D ]}|  ˆ¡  |¡‘qqS r   )r%   )Ú.0r   Új©Úsecond_reference_framer   r   r   Ú
<listcomp>›  s    ÿ z$Dyadic.to_matrix.<locals>.<listcomp>r4   )ÚMatrixZreshape)r   Zreference_framerV   r   rU   r   Ú	to_matrixm  s    + ÿzDyadic.to_matrixc                    s   t ‡ fdd„| jD ƒtdƒƒS )z(Calls .doit() on each term in the Dyadicc                    s0   g | ](}t |d  jf ˆ Ž|d |d fgƒ‘qS ©r   r	   r
   )r   Údoit©rS   r   ©Úhintsr   r   rW      s   ÿzDyadic.doit.<locals>.<listcomp>r   ©Úsumr   r   )r   r^   r   r]   r   r[   ž  s
    ÿÿzDyadic.doitc                 C   s   ddl m} || |ƒS )a¯  Take the time derivative of this Dyadic in a frame.

        This function calls the global time_derivative method

        Parameters
        ==========

        frame : ReferenceFrame
            The frame to take the time derivative in

        Examples
        ========

        >>> from sympy.physics.vector import ReferenceFrame, outer, dynamicsymbols
        >>> from sympy.physics.vector import init_vprinting
        >>> init_vprinting(pretty_print=False)
        >>> N = ReferenceFrame('N')
        >>> q = dynamicsymbols('q')
        >>> B = N.orientnew('B', 'Axis', [q, N.z])
        >>> d = outer(N.x, N.x)
        >>> d.dt(B)
        - q'*(N.y|N.x) - q'*(N.x|N.y)

        r   )Útime_derivative)rR   ra   )r   Úframera   r   r   r   Údt£  s    z	Dyadic.dtc                 C   s<   t dƒ}| jD ](}|t |d  ¡ |d |d fgƒ7 }q|S )zReturns a simplified Dyadic.r   r	   r
   )r   r   Úsimplify)r   Úoutr   r   r   r   rd   ¿  s    
&zDyadic.simplifyc                    s    t ‡ ‡fdd„| jD ƒtdƒƒS )a5  Substitution on the Dyadic.

        Examples
        ========

        >>> from sympy.physics.vector import ReferenceFrame
        >>> from sympy import Symbol
        >>> N = ReferenceFrame('N')
        >>> s = Symbol('s')
        >>> a = s*(N.x|N.x)
        >>> a.subs({s: 2})
        2*(N.x|N.x)

        c                    s0   g | ](}t |d  jˆ ˆŽ|d |d fgƒ‘qS rZ   )r   Úsubsr\   ©r   rB   r   r   rW   Ö  s   ÿzDyadic.subs.<locals>.<listcomp>r   r_   )r   r   rB   r   rg   r   rf   Æ  s
    ÿÿzDyadic.subsc                 C   sD   t |ƒstdƒ‚tdƒ}| jD ] \}}}|||ƒ| |¡ 7 }q|S )z/Apply a function to each component of a Dyadic.z`f` must be callable.r   )ÚcallableÚ	TypeErrorr   r   r&   )r   Úfre   ÚaÚbÚcr   r   r   Ú	applyfuncÙ  s    zDyadic.applyfuncc                 C   sT   | j s
| S g }t|ƒ}| j D ].}t|ƒ}|d j|d�|d< | t|ƒ¡ qt|ƒS )Nr   )Ún)r   r   r   Zevalfr   Útupler   )r   ÚprecÚnew_argsZdpsr   Ú
new_inlistr   r   r   Ú_eval_evalfã  s    
zDyadic._eval_evalfc                 C   s@   g }| j D ],}t|ƒ}|d  |¡|d< | t|ƒ¡ q
t|ƒS )a®  
        Replace occurrences of objects within the measure numbers of the
        Dyadic.

        Parameters
        ==========

        rule : dict-like
            Expresses a replacement rule.

        Returns
        =======

        Dyadic
            Result of the replacement.

        Examples
        ========

        >>> from sympy import symbols, pi
        >>> from sympy.physics.vector import ReferenceFrame, outer
        >>> N = ReferenceFrame('N')
        >>> D = outer(N.x, N.x)
        >>> x, y, z = symbols('x y z')
        >>> ((1 + x*y) * D).xreplace({x: pi})
        (pi*y + 1)*(N.x|N.x)
        >>> ((1 + x*y) * D).xreplace({x: pi, y: 2})
        (1 + 2*pi)*(N.x|N.x)

        Replacements occur only if an entire node in the expression tree is
        matched:

        >>> ((x*y + z) * D).xreplace({x*y: pi})
        (z + pi)*(N.x|N.x)
        >>> ((x*y*z) * D).xreplace({x*y: pi})
        x*y*z*(N.x|N.x)

        r   )r   r   Úxreplacer   rp   r   )r   Úrulerr   r   rs   r   r   r   ru   î  s    (
zDyadic.xreplace)N)N)"rG   rH   rI   Ú__doc__Z	is_numberr   Úpropertyr   r   Ú__radd__r    Ú__rmul__r%   Ú__and__r(   r*   r+   r.   r?   rK   rL   rN   rO   rP   Ú__xor__rQ   rY   r[   rc   rd   rf   rn   rt   ru   r   r   r   r   r      s>   &
%$6$
#
1
c                 C   s   t | tƒstdƒ‚| S )NzA Dyadic must be supplied)r$   r   ri   )r   r   r   r   r     s    
r   N)Zsympyr   r   r   rX   Zsympy.core.evalfr   Zsympy.printing.defaultsr   Zmpmath.libmp.libmpfr   Ú__all__r   r   r   r   r   r   Ú<module>   s       