o
    §ƒðjzF  ã                   @   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d4d$d%„Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Z d2d3„ Z!d!S )5r   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   ú�/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/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   sV   t | jƒ}t|ƒ}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   Úranger   r   )r   r   Únewlistr   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 ]$}|jD ]}||d |d  |d  |d ¡ |d  |d ¡ 7 }qq|S ||ƒ}|dƒ}| jD ]}||d |d  |d  |¡ 7 }qJ|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   Ú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__�   ó   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__“   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   sT  | j }t|ƒdkrtdƒS g }|D ]y}|d dkr/| d| |d ¡ d | |d ¡ ¡ q|d dkrK| d| |d ¡ d | |d ¡ ¡ q|d dkrŠ| |d ¡}t|d tƒrcd| }| d	¡rq|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
   r2   ú - ú(%s)ú-Ú é   ú )	r   r   r   r   Ú_printr&   r   Ú
startswithÚjoin©r   ÚprinterÚarr)   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                    sj  ˆ j }ˆ}t|ƒdkrtdƒS ˆjrdnd}g }|D ]{}|d dkr6| d| |d ¡|| |d ¡g¡ q|d dkrP| d| |d ¡|| |d ¡g¡ q|d dkr•t|d tƒri| |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	   r4   r
   r2   r5   r7   r:   r8   r9   )r   r   r   Ú_use_unicodeÚextendÚdoprintr&   r   r;   Úparensr<   r=   )r   r   Úkwargsr@   ÚmppÚbarr)   r   rA   rB   rC   ©Úer?   r   r   ÚrenderÓ   sX   ýýÿÿ

ý€


þz#Dyadic._pretty.<locals>.Fake.renderN)Ú__name__Ú
__module__Ú__qualname__ÚbaselinerO   r   rM   r   r   ÚFakeÐ   s    rT   r   )r   r?   rT   r   rM   r   Ú_prettyÍ   s   1zDyadic._prettyc                 C   s   d|  | S r1   r   r   r   r   r   Ú__rsub__  s   zDyadic.__rsub__c                 C   sh  | j }t|ƒdkr| d¡S g }|D ]‚}|d dkr2| d| |d ¡ d | |d ¡ d ¡ q|d dkrP| d| |d ¡ d | |d ¡ d ¡ q|d dkr”| |d ¡}t|d tƒrhd	| }|d d
krw|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 + (rE   r
   ú)r2   z - (r6   r7   Nr5   r4   z*(r8   r9   r:   )r   r   r;   r   r&   r   r=   r<   r>   r   r   r   Ú	_sympystr  sT   
ÿÿÿÿÿþþþ€


þzDyadic._sympystrc                 C   s   |   |d ¡S )zThe subtraction operator. r2   )r   r   r   r   r   Ú__sub__*  r,   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   rZ   .  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.functionsr[   )r   Úframe1Úframe2r[   r   r   r   r[   J  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, 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>r9   )ÚMatrixÚreshape)r   Úreference_framerb   r   ra   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                    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   rk   r   rj   r   rh   ž  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\   ro   )r   Úframero   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   rr   ¿  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   Úsubsri   ©r   rJ   r   r   rc   Ö  rl   zDyadic.subs.<locals>.<listcomp>r   rm   )r   r   rJ   r   ru   r   rt   Æ  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   Úfrs   Ú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   Úevalfr   Útupler   )r   ÚprecÚnew_argsÚ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/   )"rP   rQ   rR   Ú__doc__Ú	is_numberr   Úpropertyr   r   Ú__radd__r"   Ú__rmul__r'   Ú__and__r+   r.   r0   r3   rD   rU   rV   rX   rY   rZ   Ú__xor__r[   rg   rh   rq   rr   rt   r|   r„   r…   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   rw   )r   r   r   r   r     s   
r   N)Úsympyr   r   r   rd   Úsympy.core.evalfr   Úsympy.printing.defaultsr   Úmpmath.libmp.libmpfr   Ú__all__r   r   r   r   r   r   Ú<module>   s        