o
    ÐeRL  ã                   @   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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dd„ Zdd „ Zd!d"„ Zd#d$„ ZeZeZd8d&d'„Zd8d(d)„Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Ze	Z eZ!d4d5„ Z"d6d7„ Z#d%S )9r   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   sx  g | _ |dkr	g }t|ƒdkr|d}t| j ƒD ]L\}}t|d d ƒt| j | d ƒkrbt|d d ƒt| j | d ƒkrb| j | d |d d  |d d |d d f| j |< | |d ¡ d} nq|dkrv| j  |d ¡ | |d ¡ t|ƒdksd}|t| j ƒk rº| j | d dk| j | d dkB | j | d dkB r­| j  | j | ¡ |d8 }|d7 }|t| j ƒk s…dS 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   úKD:\Projects\ConvertPro\env\Lib\site-packages\sympy/physics/vector/dyadic.pyÚ__init__   s8   ""
ÿ€ô"ÿû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   sÒ   ddl m}m} t|tƒrEt|ƒ}tdƒ}t| jƒD ](\}}t|jƒD ]\}}||d |d  |d |d @  |d |d B  7 }q#q|S ||ƒ}|dƒ}t| jƒD ]\}}||d |d  |d |@  7 }qR|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   r   )	r   r   r    r!   Úolr   r   Úi2Úv2r   r   r   Ú__and__J   s   
6ÿü"zDyadic.__and__c                 C   s   |   d| ¡S )z0Divides the Dyadic by a sympifyable expression. r	   )Ú__mul__r   r   r   r   Ú__truediv__n   ó   zDyadic.__truediv__c                 C   s\   |dkrt dƒ}t|ƒ}| jg kr|jg krdS | jg ks"|jg kr$d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__r   s   zDyadic.__eq__c                 C   sV   t | jƒ}t|ƒ}t|ƒD ]\}}||| 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   r(   ‚   s   


ÿzDyadic.__mul__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r9| d| || d ¡ d | || d ¡ ¡ q|| d dkr[| d| || d ¡ d | || d ¡ ¡ q|| d dkr¤| || d ¡}t|| d tƒryd| }| d	¡r‡|dd … }d}nd}| || | || d ¡ d | || d ¡ ¡ qd
 	|¡}| d¡r·|dd … }|S | d¡rÂ|dd … }|S )Nr   r	   ú + z\otimes r
   r1   ú - ú(%s)ú-Ú é   ú )
r   r   r   r   r   Ú_printr#   r   Ú
startswithÚjoin©	r   ÚprinterÚarr$   r   r   Úarg_strÚ	str_startÚoutstrr   r   r   Ú_latex£   sL   ÿÿþý
ÿÿ€


þ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¡ q|| d dkr`| d| || d ¡|| || d ¡g¡ q|| d dkr±t|| d tƒr| 	|| d ¡ 
¡ d }	n	| || d ¡}	|	 d	¡r–|	dd … }	d}
nd}
| |
|	d
| || d ¡|| || d ¡g¡ qd |¡}| d¡rÄ|dd … }|S | d
¡rÏ|dd … }|S )Nr   u   âŠ—ú|r	   r3   r
   r1   r4   r6   r9   r7   r8   )r   r   r   Z_use_unicoder   ÚextendZdoprintr#   r   r:   Úparensr;   r<   )r   r   Úkwargsr?   ZmppÚbarr$   r   r   r@   rA   rB   ©Úer>   r   r   ÚrenderÍ   sX   ýý
ÿÿ

ý€


þz#Dyadic._pretty.<locals>.Fake.renderN)Ú__name__Ú
__module__Ú__qualname__ZbaselinerK   r   rI   r   r   ÚFakeÊ   s    rO   r   )r   r>   rO   r   rI   r   Ú_prettyÇ   s   1zDyadic._prettyc                 C   sX   ddl m}m} ||ƒ}|dƒ}t| jƒD ]\}}||d |d  |d |@  7 }q|S )a¯  The inner product operator for a Vector or Dyadic, and a Dyadic

        This is for: Vector dot Dyadic

        Parameters
        ==========

        other : Vector
            The vector we are dotting with

        Examples
        ========

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

        r   r   r
   r	   )r"   r    r!   r   r   )r   r   r    r!   r$   r   r   r   r   r   Ú__rand__ý   s   "zDyadic.__rand__c                 C   s   d|  | S r0   r   r   r   r   r   Ú__rsub__  s   zDyadic.__rsub__c                 C   sT   ddl m} ||ƒ}tdƒ}t| jƒD ]\}}||d ||d A |d B  7 }q|S )a–  For a cross product in the form: Vector x Dyadic

        Parameters
        ==========

        other : Vector
            The Vector that we are crossing this Dyadic with

        Examples
        ========

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

        r   ©r!   r	   r
   ©r"   r!   r   r   r   ©r   r   r!   r$   r   r   r   r   r   Ú__rxor__  ó   "zDyadic.__rxor__c           	      C   sœ  | j }t|ƒdkr| d¡S g }t|ƒD ]š\}}|| d dkr<| 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… }|S | d¡rÌ|dd… }|S )zPrinting method. r   r	   z + (rD   r
   ú)r1   z - (r5   r6   Nr4   r3   z*(r7   r8   r9   )	r   r   r:   r   r   r#   r   r<   r;   r=   r   r   r   Ú	_sympystr8  sT   
ÿÿÿÿÿþþþ€


þzDyadic._sympystrc                 C   s   |   |d ¡S )zThe subtraction operator. r1   )r   r   r   r   r   Ú__sub__\  r*   zDyadic.__sub__c                 C   sT   ddl m} ||ƒ}tdƒ}t| jƒD ]\}}||d |d |d |A B  7 }q|S )a•  For a cross product in the form: Dyadic x Vector.

        Parameters
        ==========

        other : Vector
            The Vector that we are crossing this Dyadic 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   rS   r	   r
   rT   rU   r   r   r   Ú__xor__`  rW   zDyadic.__xor__Nc                 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.functionsr\   )r   Zframe1Zframe2r\   r   r   r   r\   ~  s    zDyadic.expressc                    s,   ˆ du 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
        >>> from sympy.physics.vector import ReferenceFrame, Vector
        >>> Vector.simp = True
        >>> 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))
        >>> 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   )Údot)Ú.0r   Új©Úsecond_reference_framer   r   r   Ú
<listcomp>Ð  s    ÿz$Dyadic.to_matrix.<locals>.<listcomp>r8   )ÚMatrixZreshape)r   Zreference_framerb   r   ra   r   Ú	to_matrix¡  s
   ,ÿzDyadic.to_matrixc                    s   t ‡ fdd„| jD ƒtdƒƒS )z(Calls .doit() on each term in the Dyadicc                    s4   g | ]}t |d  jdi ˆ ¤Ž|d |d fgƒ‘qS )r   r	   r
   r   )r   Údoit©r_   r   ©Úhintsr   r   rc   Õ  ó    ,ÿzDyadic.doit.<locals>.<listcomp>r   ©Úsumr   r   )r   ri   r   rh   r   rf   Ó  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)r]   rm   )r   Úframerm   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   rp   ô  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                    s4   g | ]}t |d  jˆ i ˆ¤Ž|d |d fgƒ‘qS )r   r	   r
   )r   Úsubsrg   ©r   rG   r   r   rc     rj   zDyadic.subs.<locals>.<listcomp>r   rk   )r   r   rG   r   rs   r   rr   û  s
   ÿÿzDyadic.subsc                 C   sB   t |ƒstdƒ‚tdƒ}| jD ]\}}}|||ƒ||B  7 }q|S )z/Apply a function to each component of a Dyadic.z`f` must be callable.r   )ÚcallableÚ	TypeErrorr   r   )r   Úfrq   Ú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   r|   r   )r   Úruler~   r   r   r   r   r   r�   &  s   (
zDyadic.xreplacer.   )$rL   rM   rN   Ú__doc__Z	is_numberr   Úpropertyr   r   r'   r)   r,   r(   r/   r2   rC   rP   rQ   rR   rV   rY   rZ   r[   Ú__radd__Ú__rmul__r\   re   rf   ro   rp   rr   rz   r^   Úcrossr€   r�   r   r   r   r   r      sD    &
$$6$

#2
c                 C   s   t | tƒs	tdƒ‚| S )NzA Dyadic must be supplied)r#   r   ru   )r   r   r   r   r   V  s   
r   N)Zsympy.core.backendr   r   r   rd   Zsympy.core.evalfr   Zsympy.printing.defaultsr   Zmpmath.libmp.libmpfr   Ú__all__r   r   r   r   r   r   Ú<module>   s        O