
    sg.G                     `    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y)	    )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!y)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k7  rd}t        | j                         D ]  \  }}t        |d   d         t        | j                   |   d         k(  s4t        |d   d         t        | j                   |   d         k(  sb| j                   |   d   |d   d   z   |d   d   |d   d   f| j                   |<   |j	                  |d          d} n |dk7  r2| j                   j                  |d          |j	                  |d          t        |      dk7  rd}|t        | j                         k  r| j                   |   d   dk(  | j                   |   d   dk(  z  | j                   |   d   dk(  z  r-| j                   j	                  | j                   |          |dz  }|dz  }|t        | j                         k  ryy)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        N/var/www/html/venv/lib/python3.12/site-packages/sympy/physics/vector/dyadic.py__init__zDyadic.__init__   s    	Q;F&kQE!$)), 11&#diil1o*>>VAYq\*c$))A,q/.BB$(IIaLOfQil$B$*1IaL&)A,$@DIIaLMM&),E z		  +fQi( &kQ #dii. 1aA%$))A,q/Q*>?YYq\!_)+		  1.QFA #dii.     c                     t         S )zReturns the class Dyadic. )r	   r   s    r   funczDyadic.func@   s	     r   c                 \    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$dii%**,--r   c                     t        | j                        }t        |      }t        |      D ]"  \  }}|||   d   z  ||   d   ||   d   f||<   $ 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"   newlistr   r   s        r   __mul__zDyadic.__mul__L   sn    & tyy/g& 	)DAq'!*Q-/A!!*Q-)GAJ	) gr   c                    ddl m}m} t        |t              rxt        |      }t	        d      }| j                  D ]Q  }|j                  D ]@  }||d   |d   z  |d   j                  |d         z  |d   j                  |d         z  z  }B S |S  ||      } |d      }| j                  D ]%  }||d   |d   z  |d   j                  |      z  z  }' |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eV$!%(EBYY Q** QB!A$A,!A$((2a5/:adjjA>OPPBQQ 		 "%(EBYY 6adQqTkQqTXXe_556	r   c                 *    | j                  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y| j                  g k(  s|j                  g k(  ry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__   sk     A:1IEe$IIO%**"2ii2o5::#3499~UZZ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 ]F  \  }}||   d   dk(  rE|j	                  d|j                  ||   d         z   dz   |j                  ||   d         z          W||   d   dk(  rE|j	                  d|j                  ||   d         z   dz   |j                  ||   d         z          ||   d   dk7  s|j                  ||   d         }t        ||   d   t              rd|z  }|j                  d	      r|dd  }d}nd}|j	                  ||z   |j                  ||   d         z   dz   |j                  ||   d         z          I d
j                  |      }|j                  d      r|dd  }|S |j                  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   s   YYr7a<q6MbM 	BDAq!uQx1}		%'..Aq"::[H!..Aq23 4 AqR		%!..Aq23%& "..Aq23 4 AqQ!..Aq2beAh,$w.G%%c*%abkG %I %I		)g-r!uQx0HH%&(/r!uQx(@A B-	B0 U#ABZF  s#ABZFr   c                 2    |  G fdd      } |       S )Nc                       e Zd ZdZ fdZy)Dyadic._pretty.<locals>.Faker   c                    j                   }}t        |      dk(  rt        d      S j                  rdnd}g }t	        |      D ]Z  \  }}||   d   dk(  r@|j                  d|j                  ||   d         ||j                  ||   d         g       R||   d   dk(  r@|j                  d|j                  ||   d         ||j                  ||   d         g       ||   d   dk7  st        ||   d   t              r)|j                  ||   d         j                         d   }	n|j                  ||   d         }	|	j                  d	      r|	dd  }	d}
nd}
|j                  |
|	d
|j                  ||   d         ||j                  ||   d         g       ] dj                  |      }|j                  d      r|dd  }|S |j                  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   VVr7a<q6M-4-A-A)s%bM :DAq!uQx1}		5"%++beAh"7"%"%++beAh"7#9 : AqR		5"%++beAh"7"%"%++beAh"7#9 : AqQ%beAh4&)jj "1a'**0&(1'6G '*kk"Q%(&;G"--c2&-abkG(-I(-I		9gs"%++beAh"7"%"%++beAh"7#9 :9:B $$U+#ABZF  &&s+#ABZFr   N)__name__
__module____qualname__baseliner]   )r\   rJ   s   r   FakerR      s    H-r   rb   r8   )r   rJ   rb   r\   s    ` @r   _prettyzDyadic._pretty   s    0	 0	b v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|j                  d      S g }t        |      D ]I  \  }}||   d   dk(  rH|j	                  d|j                  ||   d         z   dz   |j                  ||   d         z   dz          Z||   d   dk(  rH|j	                  d|j                  ||   d         z   dz   |j                  ||   d         z   dz          ||   d   dk7  s|j                  ||   d         }t        ||   d   t              rd	|z  }|d   d
k(  r|dd }d}nd}|j	                  ||z   dz   |j                  ||   d         z   dz   |j                  ||   d         z   dz          L dj                  |      }|j                  d      r|dd }|S |j                  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  s   YYr7a<>>!$$bM 	@DAq!uQx1}		&7>>"Q%(#;;cA!..Aq23589 : AqR		&7>>"Q%(#;;cA!..Aq23589 : AqQ!..Aq2beAh,$w.G1:$%abkG %I %I		)g-4!..Aq23 'r!uQx 89;>? @)	@. U#ABZF  s#ABZFr   c                 *    | j                  |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 ]1  }||d   |d   j	                  |d   j                  |            z  z  }3 |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$AY 	;A!A$!A$**adjj&79::B	;	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    @ 	;tVV,,r   c           
          ||}t        |D cg c])  }|D ]"  }|j                  |       j                  |      $ + c}}      j                  dd      S c c}}w )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]])

        rD   )Matrixr-   reshape)r   reference_framesecond_reference_framer   js        r   	to_matrixzDyadic.to_matrixm  sb    V ")%4"? .a,.qquuT{q) .) . //6wq!}	= .s   .A
c                     t        | j                  D cg c]*  }t         |d   j                  di ||d   |d   fg      , c}t        d            S c c}w )z(Calls .doit() on each term in the Dyadicr   r   r   r8   )sumr   r	   doit)r   hintsr   s      r   r{   zDyadic.doit  s^    !YY( YQqTYY//1qt<=> ()/4 	4 (   /A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tU++r   c                     t        d      }| j                  D ]+  }|t        |d   j                         |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  sN    Qi 	;A6AaDMMOQqT1Q489::C	;
r   c                     t        | j                  D cg c]*  }t         |d   j                  |i ||d   |d   fg      , c}t        d            S c c}w )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)

        r   r   r   )rz   r   r	   subs)r   r   rY   r   s       r   r   zDyadic.subs  sb      !YY( YQqTYY771qtDEF ()/4 	4 (r}   c                     t        |      st        d      t        d      }| j                  D ]#  \  }}}| ||      |j	                  |      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  sW    {344Qiyy 	'GAq!1Q41771:&&C	'
r   c                     | j                   s| S g }t        |      }| j                   D ]?  }t        |      }|d   j                  |      |d<   |j	                  t        |             A t        |      S )Nr   )n)r   r   r%   evalfr   tupler	   )r   precnew_argsdpsr   
new_inlists         r   _eval_evalfzDyadic._eval_evalf  so    yyK$ii 	/FfJ"1IOOcO2JqMOOE*-.	/ hr   c                     g }| j                   D ]>  }t        |      }|d   j                  |      |d<   |j                  t	        |             @ 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   s        r   r   zDyadic.xreplace  s\    P ii 	/FfJ&qM224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   rx   r{   r   r   r   r   r   r   r8   r   r   r	   r	      s    
 I$L  .
 H4 H"J G'1 !"H4l#"H(4 G!-F/=b4
,84&	 - r   c                 <    t        | t              st        d      | S )NzA Dyadic must be supplied)r,   r	   r   )r"   s    r   r    r      s    eV$344Lr   N)sympyr   r   r   rs   sympy.core.evalfr   sympy.printing.defaultsr   mpmath.libmp.libmpfr   __all__r	   r    r8   r   r   <module>r      s3    9 9 ' - + *P Y
 P fr   