U
    L?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d5d$d%Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Z d2d3 Z!d!S )6r   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   sb  g | _ |dkrg }t|dkrd}t| j D ]\}}t|d d t| j | d kr,t|d d t| j | d kr,| j | d |d d  |d d |d d f| j |< ||d  d} qq,|dkr| j |d  ||d  qd}|t| j k r^| j | d dk| j | d dkB | j | d dkB rT| j | j |  |d8 }|d7 }qd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   M/var/www/html/venv/lib/python3.8/site-packages/sympy/physics/vector/dyadic.py__init__   s6    " 
 

"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 ]2\}}||| 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   Z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 ]H}|jD ]<}||d |d  |d |d  |d |d  7 }q:q0n>||}|d}| jD ]&}||d |d  |d | 7 }q|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   Z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__   s    zDyadic.__truediv__c                 C   s\   |dkrt d}t|}| jg kr0|jg kr0dS | jg ksD|jg krHd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rt|d||| d  d ||| d   q&|| d dkr|d||| d  d ||| d   q&|| d dkr&||| d }t|| d trd| }|d	r|dd  }d}nd}||| ||| d  d ||| d   q&d
	|}|drr|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outstrr   r   r   _latex   sH    
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 q8|| d dkr|d||| d |||| d g q8|| d dkr8t|| d tr|	|| d 
 d }	n||| d }	|	d	r2|	dd  }	d}
nd}
||
|	d
||| d |||| d g q8d|}|dr|dd  }n|d
r|dd  }|S )Nr   u   ⊗|r	   r/   r
   r-   r0   r2   r5   r3   r4   )r   r   r   Z_use_unicoder   extendZdoprintr$   r   r6   Zparensr7   r8   )r   r   kwargsr;   Zmppbarr'   r   r   r<   r=   r>   er:   r   r   render   sT    


z#Dyadic._pretty.<locals>.Fake.renderN)__name__
__module____qualname__ZbaselinerF   r   rD   r   r   Fake   s   rJ   r   )r   r:   rJ   r   rD   r   _pretty   s    1zDyadic._prettyc                 C   s   d|  | S r,   r   r   r   r   r   __rsub__  s    zDyadic.__rsub__c           	      C   s  | j }t|dkr|dS g }t|D ]8\}}|| d dkrz|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 }n|dr|dd }|S )zPrinting method. r   r	   z + (r@   r
   )r-   z - (r1   r2   Nr0   r/   z*(r3   r4   r5   )	r   r   r6   r   r   r$   r   r8   r7   r9   r   r   r   	_sympystr  sP    

zDyadic._sympystrc                 C   s   |  |d S )zThe subtraction operator. r-   )r   r   r   r   r   __sub__*  s    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   rP   .  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.functionsrQ   )r   Zframe1Zframe2rQ   r   r   r   rQ   J  s     zDyadic.expressc                    s,    dk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>r4   )MatrixZreshape)r   Zreference_framerV   r   rU   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                    s0   g | ](}t |d  jf  |d |d fgqS r   r	   r
   )r   doitrS   r   hintsr   r   rW     s   zDyadic.doit.<locals>.<listcomp>r   sumr   r   )r   r^   r   r]   r   r[     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)rR   ra   )r   framera   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   rd     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                    s0   g | ](}t |d  j |d |d fgqS rZ   )r   subsr\   r   rB   r   r   rW     s   zDyadic.subs.<locals>.<listcomp>r   r_   )r   r   rB   r   rg   r   rf     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   fre   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   Zevalfr   tupler   )r   precnew_argsZ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   rp   r   )r   rulerr   r   rs   r   r   r   ru     s    (
zDyadic.xreplace)N)N)"rG   rH   rI   __doc__Z	is_numberr   propertyr   r   __radd__r    __rmul__r%   __and__r(   r*   r+   r.   r?   rK   rL   rN   rO   rP   __xor__rQ   rY   r[   rc   rd   rf   rn   rt   ru   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   ri   )r   r   r   r   r     s    
r   N)Zsympyr   r   r   rX   Zsympy.core.evalfr   Zsympy.printing.defaultsr   Zmpmath.libmp.libmpfr   __all__r   r   r   r   r   r   <module>   s       