
    g.G                     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        W/var/www/html/ai-engine/env/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          |          D ]4\  }}|||         d         z  ||         d         ||         d         f||<   5t	          |          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"   newlistr   r   s        r   __mul__zDyadic.__mul__L   su    & ty//g&& 	) 	)DAq'!*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 Nr8   r   s    r   __neg__zDyadic.__neg__   s    byr   c                    | j         }t          |          dk    rt          d          S g }t          |          D ]\  }}||         d         dk    r]|                    d|                    ||         d                   z   dz   |                    ||         d                   z              u||         d         dk    r]|                    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   r   _printr,   r   
startswithjoin	r   printerarr/   r   r   arg_str	str_startoutstrs	            r   _latexzDyadic._latex   sW   Yr77a<<q66MbMM 	B 	BDAq!uQx1}}		%'..Aq":"::[H!..Aq223 4 4 4 4 AqR		%!..Aq223%& "..Aq223 4 4 4 4 AqQ!..Aq22beAh,, /$w.G%%c** &%abbkG %II %I		)g-r!uQx0H0HH%&(/r!uQx(@(@A B B B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 }t	          |          D ]\  }}||         d         dk    rX|                    d|                    ||         d                   ||                    ||         d                   g           p||         d         dk    rX|                    d|                    ||         d                   ||                    ||         d                   g           ||         d         dk    rt          ||         d         t                    r:|	                    ||         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<   r@   rB   rE   rC   rD   )r   r   r   _use_unicoder   extenddoprintr,   r   rF   parensrG   rH   )r   r   kwargsrK   mppbarr/   r   r   rL   rM   rN   erJ   s               r   renderz#Dyadic._pretty.<locals>.Fake.render   s|   Vr77a<<q66M-4-AJ))s%bMM : :DAq!uQx1}}		5"%++beAh"7"7"%"%++beAh"7"7#9 : : : : AqR		5"%++beAh"7"7"%"%++beAh"7"7#9 : : : : AqQ%beAh44 <&)jj "1a'* '**0&((1'6GG '*kk"Q%(&;&;G"--c22 .&-abbkG(-II(-I		9gs"%++beAh"7"7"%"%++beAh"7"7#9 : : :
 $$U++ (#ABBZFF&&s++ (#ABBZFr   N)__name__
__module____qualname__baseliner]   )r\   rJ   s   r   FakerR      s8        H- - - - - - - -r   rb   r8   )r   rJ   rb   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;   r8   r!   s     r   __rsub__zDyadic.__rsub__  s    T	U""r   c                    | j         }t          |          dk    r|                    d          S g }t          |          D ]\  }}||         d         dk    r`|                    d|                    ||         d                   z   dz   |                    ||         d                   z   dz              x||         d         dk    r`|                    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 + (rT   r   )r<   z - (rA   rB   Nr@   r?   z*(rC   rD   rE   )	r   r   rF   r   r   r,   r   rH   rG   rI   s	            r   	_sympystrzDyadic._sympystr  sp   Yr77a<<>>!$$$bMM 	@ 	@DAq!uQx1}}		&7>>"Q%(#;#;;cA!..Aq223589 : : : : AqR		&7>>"Q%(#;#;;cA!..Aq223589 : : : : AqQ!..Aq22beAh,, /$w.G1:$$%abbkG %II %I		)g-4!..Aq223 'r!uQx 8 89;>? @ @ @ 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*   r/   r   s        r   rl   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.functionsrn   )r   frame1frame2rn   s       r   rn   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 r8   )r-   ).0r   jsecond_reference_framer   s      r   
<listcomp>z$Dyadic.to_matrix.<locals>.<listcomp>  sO     . . .a,. .qquuT{{q)) . . . .r   rD   )Matrixreshape)r   reference_framerv   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   r8   )r	   doit)rt   r   hintss     r   rw   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)ro   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)rt   r   r   rY   s     r   rw   zDyadic.subs.<locals>.<listcomp>  sX     ( ( ( YQqTY7771qtDEFF ( ( (r   r   r   )r   r   rY   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   r7   )"r^   r_   r`   __doc__	is_numberr   propertyr   r#   __radd__r'   __rmul__r-   __and__r2   r5   r9   r=   rO   rc   re   rh   rj   rl   __xor__rn   r{   r~   r   r   r   r   r   r   r8   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   rx   sympy.core.evalfr   sympy.printing.defaultsr   mpmath.libmp.libmpfr   __all__r	   r    r8   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   