o
    oh.G                     @   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 sd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   o/var/www/html/construction_image-detection-poc/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|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   newlistr   r   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 Nr   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rt|| 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   _use_unicoder   extenddoprintr%   r   r:   parensr;   r<   )r   r   kwargsr?   mppbarr(   r   r   r@   rA   rB   er>   r   r   render   sX   





z#Dyadic._pretty.<locals>.Fake.renderN)__name__
__module____qualname__baselinerN   r   rL   r   r   Fake   s    rS   r   )r   r>   rS   r   rL   r   _pretty   s   1zDyadic._prettyc                 C   s   d|  | S r0   r   r   r   r   r   __rsub__  s   zDyadic.__rsub__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   	_sympystr  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   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   rY   .  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.functionsrZ   )r   frame1frame2rZ   r   r   r   rZ   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>r8   )Matrixreshape)r   reference_framera   r   r`   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   rb         ,zDyadic.doit.<locals>.<listcomp>r   sumr   r   )r   rj   r   ri   r   rg     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[   rn   )r   framern   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   rq     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   subsrh   r   rI   r   r   rb     rk   zDyadic.subs.<locals>.<listcomp>r   rl   )r   r   rI   r   rt   r   rs     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   frr   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.   )"rO   rP   rQ   __doc__	is_numberr   propertyr   r   __radd__r!   __rmul__r&   __and__r*   r-   r/   r2   rC   rT   rU   rW   rX   rY   __xor__rZ   rf   rg   rp   rq   rs   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   rv   )r   r   r   r   r     s   
r   N)sympyr   r   r   rc   sympy.core.evalfr   sympy.printing.defaultsr   mpmath.libmp.libmpfr   __all__r   r   r   r   r   r   <module>   s        