
    XhzF                     h    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ee          Zd ZdS )	    )sympifyAddImmutableMatrix)
EvalfMixin)	Printable)prec_to_dpsDyadicc                       e Zd ZdZdZd Zed             Zd ZeZ	d Z
e
Zd ZeZd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd ZeZddZddZd Zd Zd Zd Zd Zd Z d Z!dS )r	   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                 >   g | _         |dk    rg }t          |          dk    rOd}t          | j                   D ]\  }}t          |d         d                   t          | j         |         d                   k    rt          |d         d                   t          | j         |         d                   k    rd| j         |         d         |d         d         z   |d         d         |d         d         f| j         |<   |                    |d                    d} n|dk    r;| j                             |d                    |                    |d                    t          |          dk    Od}|t          | j                   k     r| j         |         d         dk    | j         |         d         dk    z  | j         |         d         dk    z  r*| j                             | j         |                    |dz  }|dz  }|t          | j                   k     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vs        m/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/sympy/physics/vector/dyadic.py__init__zDyadic.__init__   s    	Q;;F&kkQE!$),,  11&&#dil1o*>*>>>VAYq\**c$)A,q/.B.BBB$(IaLOfQil$B$*1IaL&)A,$@DIaLMM&),,,EEzz	  +++fQi((( &kkQ #di..  1aA%$)A,q/Q*>?Yq\!_)+ 	  1...QFA #di..          c                     t           S )zReturns the class Dyadic. )r	   r   s    r   funczDyadic.func@   s	     r   c                 X    t          |          }t          | j        |j        z             S )zThe add operator for Dyadic. )_check_dyadicr	   r   r   others     r   __add__zDyadic.__add__E   s&    e$$di%*,---r   c                    t          | j                  }t          |          }t          t	          |                    D ]1}|||         d         z  ||         d         ||         d         f||<   2t          |          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   s       r   __mul__zDyadic.__mul__L   sw    & ty//s7||$$ 	) 	)A'!*Q-/A!!*Q-)GAJJgr   c                    ddl m}m} t          |t                    rt          |          }t	          d          }| j        D ]d}|j        D ]Z}||d         |d         z  |d                             |d                   z  |d                             |d                   z  z  }[enP ||          } |d          }| j        D ]2}||d         |d         z  |d                             |          z  z  }3|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   v2s          r   r.   z
Dyadic.doth   s    , 	FEEEEEEEeV$$ 
	6!%((EBY Q Q* Q QB!A$A,!A$((2a5//:adjjA>O>OPPBBQQ "M%((EBY 6 6adQqTkQqTXXe__55	r   c                 2    |                      d|z            S )z0Divides the Dyadic by a sympifyable expression. r   )r(   r!   s     r   __truediv__zDyadic.__truediv__   s    ||AI&&&r   c                     |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!   s     r   __eq__zDyadic.__eq__   sx     A::1IIEe$$IOO%*"2"24i2oo5:#3#3549~~UZ00r   c                     | |k     S N r!   s     r   __ne__zDyadic.__ne__   s    5=  r   c                     | dz  S Nr9   r   s    r   __neg__zDyadic.__neg__   s    byr   c                    | j         }t          |          dk    rt          d          S g }|D ]{}|d         dk    rQ|                    d|                    |d                   z   dz   |                    |d                   z              `|d         dk    rQ|                    d|                    |d                   z   dz   |                    |d                   z              |d         dk    r|                    |d                   }t          |d         t                    rd|z  }|                    d	          r|dd          }d}nd}|                    ||z   |                    |d                   z   dz   |                    |d                   z              }d
                    |          }|                    d          r|dd          }n|                    d          r
|dd          }|S )Nr   r    + z\otimes r   r=    - (%s)-     )	r   r   r   r   _printr-   r   
startswithjoinr   printerarr0   r   arg_str	str_startoutstrs           r   _latexzDyadic._latex   s   Yr77a<<q66M 	> 	>Atqyy		%'..1"6"66D!..1../ 0 0 0 0 1		%!..1../%& "..1../ 0 0 0 0 1!..1..adC(( /$w.G%%c** &%abbkG %II %I		)g-qt0D0DD%&(/qt(<(<= > > >U## 	 ABBZFFs## 	 ABBZFr   c                 >    |  G fdd          } |            S )Nc                        e Zd ZdZ fdZdS )Dyadic._pretty.<locals>.Faker   c                 \   j         }}t          |          dk    rt          d          S j        rdnd}g }|D ]}|d         dk    rL|                    d|                    |d                   ||                    |d                   g           [|d         dk    rL|                    d|                    |d                   ||                    |d                   g           |d         dk    rt          |d         t                    r4|                    |d                   	                                d         }n|                    |d                   }|
                    d	          r|dd          }d}	nd}	|                    |	|d
|                    |d                   ||                    |d                   g           d                    |          }
|

                    d          r|
dd          }
n|

                    d
          r
|
dd          }
|
S )Nr   u   ⊗|r   r@   r   r=   rA   rC   rF   rD   rE   )r   r   r   _use_unicodeextenddoprintr-   r   rG   parensrH   rI   )r   r   kwargsrL   mppbarr0   r   rM   rN   rO   erK   s              r   renderz#Dyadic._pretty.<locals>.Fake.render   sB   Vr77a<<q66M-4-AJ))s 6 6Atqyy		5"%++ad"3"3"%"%++ad"3"3#5 6 6 6 6 1		5"%++ad"3"3"%"%++ad"3"3#5 6 6 6 6 1%adC00 8&)jj !!'& '&&,fhhq'2GG '*kk!A$&7&7G"--c22 .&-abbkG(-II(-I		9gs"%++ad"3"3"%"%++ad"3"3#5 6 6 6
 $$U++ (#ABBZFF&&s++ (#ABBZFr   N)__name__
__module____qualname__baseliner^   )r]   rK   s   r   FakerS      s8        H- - - - - - - -r   rc   r9   )r   rK   rc   r]   s    ` @r   _prettyzDyadic._pretty   sN    0	 0	 0	 0	 0	 0	 0	 0	 0	 0	 0	b tvvr   c                     d| z  |z   S r<   r9   r!   s     r   __rsub__zDyadic.__rsub__  s    T	U""r   c                    | j         }t          |          dk    r|                    d          S g }|D ]~}|d         dk    rT|                    d|                    |d                   z   dz   |                    |d                   z   dz              c|d         dk    rT|                    d|                    |d                   z   dz   |                    |d                   z   dz              |d         dk    r|                    |d                   }t	          |d         t
                    rd	|z  }|d         d
k    r|dd         }d}nd}|                    ||z   dz   |                    |d                   z   dz   |                    |d                   z   dz              d                    |          }|                    d          r|dd         }n|                    d          r
|dd         }|S )zPrinting method. r   r   z + (rU   r   )r=   z - (rB   rC   NrA   r@   z*(rD   rE   rF   )r   r   rG   r   r-   r   rI   rH   rJ   s           r   	_sympystrzDyadic._sympystr  s5   Yr77a<<>>!$$$ 	< 	<Atqyy		&7>>!A$#7#77#=!..1../145 6 6 6 6 1		&7>>!A$#7#77#=!..1../145 6 6 6 6 1!..1..adC(( /$w.G1:$$%abbkG %II %I		)g-4!..1../ 'qt 4 457:; < < < U## 	 ABBZFFs## 	 ABBZFr   c                 2    |                      |dz            S )zThe subtraction operator. r=   )r#   r!   s     r   __sub__zDyadic.__sub__*  s    ||EBJ'''r   c                     ddl m}  ||          }t          d          }| j        D ]B}||d         |d                             |d                             |                    z  z  }C|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+   r0   r   s        r   rm   zDyadic.cross.  s{    $ 	>=====e$$AYY 	; 	;A!A$!A$**adjj&7&799::BB	r   Nc                 (    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.functionsro   )r   frame1frame2ro   s       r   ro   zDyadic.expressJ  s+    @ 	;:::::wtVV,,,r   c                 n     |t           f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                 j    g | ]/}D ]*}|                                                    |          +0S r9   )r.   ).0r   jsecond_reference_framer   s      r   
<listcomp>z$Dyadic.to_matrix.<locals>.<listcomp>  sO     . . .a,. .qquuT{{q)) . . . .r   rE   )Matrixreshape)r   reference_framerw   s   ` `r   	to_matrixzDyadic.to_matrixm  s\    V ")%4" . . . . .? . . . / //6wq!}}	=r   c                 `    t          fd| j        D             t          d                    S )z(Calls .doit() on each term in the Dyadicc           	      p    g | ]2}t           |d          j        di |d         |d         fg          3S )r   r   r   r9   )r	   doit)ru   r   hintss     r   rx   zDyadic.doit.<locals>.<listcomp>  sY     ( ( ( YQqTY////1qt<=>> ( ( (r   r   sumr   r	   )r   r   s    `r   r   zDyadic.doit  sF     ( ( ( (!Y( ( ()/4 4 	4r   c                 &    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)rp   r   )r   framer   s      r   dtz	Dyadic.dt  s)    2 	CBBBBBtU+++r   c                     t          d          }| j        D ]<}|t          |d                                         |d         |d         fg          z  }=|S )zReturns a simplified Dyadic.r   r   r   )r	   r   simplify)r   outr   s      r   r   zDyadic.simplify  sU    Qii 	; 	;A6AaDMMOOQqT1Q489:::CC
r   c                 d    t          f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           	      p    g | ]2}t           |d          j        i |d         |d         fg          3S )r   r   r   )r	   subs)ru   r   r   rZ   s     r   rx   zDyadic.subs.<locals>.<listcomp>  sX     ( ( ( YQqTY7771qtDEFF ( ( (r   r   r   )r   r   rZ   s    ``r   r   zDyadic.subs  sN       ( ( ( ( (!Y( ( ()/4 4 	4r   c                     t          |          st          d          t          d          }| j        D ]*\  }}}| ||          |                    |          z  z  }+|S )z/Apply a function to each component of a Dyadic.z`f` must be callable.r   )callable	TypeErrorr	   r   r/   )r   fr   abcs         r   	applyfunczDyadic.applyfunc  si    {{ 	53444Qiiy 	' 	'GAq!11Q441771::&&CC
r   c                    | j         s| S g }t          |          }| j         D ]R}t          |          }|d                             |          |d<   |                    t          |                     St          |          S )Nr   )n)r   r   r%   evalfr   tupler	   )r   precnew_argsdpsr   
new_inlists         r   _eval_evalfzDyadic._eval_evalf  s    y 	K$i 	/ 	/FfJ"1IOOcO22JqMOOE*--....hr   c                     g }| j         D ]Q}t          |          }|d                             |          |d<   |                    t	          |                     Rt          |          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   s        r   r   zDyadic.xreplace  sl    P i 	/ 	/FfJ&qM22488JqMOOE*--....hr   r8   )"r_   r`   ra   __doc__	is_numberr   propertyr   r#   __radd__r(   __rmul__r.   __and__r3   r6   r:   r>   rP   rd   rf   ri   rk   rm   __xor__ro   r|   r   r   r   r   r   r   r   r9   r   r   r	   r	      s       
 
 I$ $ $L   X. . .
 H  4 H" " "J G' ' '1 1 1 ! ! !  " " "H4 4 4l# # #" " "H( ( (  4 G!- !- !- !-F/= /= /= /=b4 4 4
, , ,8  4 4 4&  	  	  	 -  -  -  -  - r   c                 N    t          | t                    st          d          | S )NzA Dyadic must be supplied)r-   r	   r   )r"   s    r   r    r      s(    eV$$ 53444Lr   N)sympyr   r   r   ry   sympy.core.evalfr   sympy.printing.defaultsr   mpmath.libmp.libmpfr   __all__r	   r    r9   r   r   <module>r      s    9 9 9 9 9 9 9 9 9 9 ' ' ' ' ' ' - - - - - - + + + + + + *P  P  P  P  P Y
 P  P  P f    r   