o
    oh                     @   s   d dl mZmZmZmZmZmZmZmZ	m
Z
 d dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZ ddgZG d	d deZG d
d dZdd ZdS )    )	diffexpandsincossympifyeyezerosImmutableMatrix
MatrixBase)Symbol)trigsimp)Vector_check_vector)	translate)warnCoordinateSymReferenceFramec                       sL   e Zd ZdZ fddZdd Zedd Zdd	 Zd
d Z	dd Z
  ZS )r   a  
    A coordinate symbol/base scalar associated wrt a Reference Frame.

    Ideally, users should not instantiate this class. Instances of
    this class must only be accessed through the corresponding frame
    as 'frame[index]'.

    CoordinateSyms having the same frame and index parameters are equal
    (even though they may be instantiated separately).

    Parameters
    ==========

    name : string
        The display name of the CoordinateSym

    frame : ReferenceFrame
        The reference frame this base scalar belongs to

    index : 0, 1 or 2
        The index of the dimension denoted by this coordinate variable

    Examples
    ========

    >>> from sympy.physics.vector import ReferenceFrame, CoordinateSym
    >>> A = ReferenceFrame('A')
    >>> A[1]
    A_y
    >>> type(A[0])
    <class 'sympy.physics.vector.frame.CoordinateSym'>
    >>> a_y = CoordinateSym('a_y', A, 1)
    >>> a_y == A[1]
    True

    c                    sT   i }t  ||  t  j| |fi |}t| |tddvr#td||f|_|S )Nr      zInvalid index specified)super	_sanitize__xnew___check_framerange
ValueError_id)clsnameframeindexassumptionsobj	__class__ n/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/physics/vector/frame.py__new__3   s   
zCoordinateSym.__new__c                 C   s   | j g| jR i fS N)r   r   selfr#   r#   r$   __getnewargs_ex__?      zCoordinateSym.__getnewargs_ex__c                 C   s
   | j d S )Nr   )r   r'   r#   r#   r$   r   B   s   
zCoordinateSym.framec                 C   s   t |tr|j| jkrdS dS )NTF)
isinstancer   r   r(   otherr#   r#   r$   __eq__F   s   
zCoordinateSym.__eq__c                 C   s
   | |k S r&   r#   r,   r#   r#   r$   __ne__N   s   
zCoordinateSym.__ne__c                 C   s   | j d  | j d f S )Nr      )r   __hash__r'   r#   r#   r$   r1   Q   s   zCoordinateSym.__hash__)__name__
__module____qualname____doc__r%   r)   propertyr   r.   r/   r1   __classcell__r#   r#   r!   r$   r      s    %
c                   @   s~  e Zd ZdZdZdOddZdd Zdd	 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d d! Zd"d# Zd$d% Zd&d' Zd(d) ZdPd+d,Z	*	dQd-d.Zd/d0 Zd1d2 Zed3d4 Zed5d6 Zed7d8 Z ed9d: Z!ed;d< Z"ed=d> Z#ed?d@ Z$edAdB Z%edCdD Z&edEdF Z'edGdH Z(edIdJ Z)edKdL Z*dMdN Z+dS )Rr   a  A reference frame in classical mechanics.

    ReferenceFrame is a class used to represent a reference frame in classical
    mechanics. It has a standard basis of three unit vectors in the frame's
    x, y, and z directions.

    It also can have a rotation relative to a parent frame; this rotation is
    defined by a direction cosine matrix relating this frame's basis vectors to
    the parent frame's basis vectors.  It can also have an angular velocity
    vector, defined in another frame.

    r   Nc                 C   s  t |ts	td|durt |ttfstdt|dkr"td|D ]}t |ts/tdq$|d |d  d	 |d |d
  d	 |d |d  d	 g| _| d |d  | d |d
  | d |d  g| _	d| |d f d| |d
 f d| |d f g| _
|| _n5|d |d |d g| _| d | d | d g| _	d|  d|  d|  g| _
g d| _|durt |ttfstdt|dkrtd|D ]}t |tstdq|| _
|| _i | _i | _i | _i | _i | _| j| j| jg| _d| _ttg d| fg| _ttg d| fg| _ttg d| fg| _|dur\t |ttfs@tdt|dkrKtd|D ]}t |tsYtdqMn|d |d |d g}t|d | dt|d
 | d
t|d | df| _t jd
7  _tj| _dS )ab	  ReferenceFrame initialization method.

        A ReferenceFrame has a set of orthonormal basis vectors, along with
        orientations relative to other ReferenceFrames and angular velocities
        relative to other ReferenceFrames.

        Parameters
        ==========

        indices : tuple of str
            Enables the reference frame's basis unit vectors to be accessed by
            Python's square bracket indexing notation using the provided three
            indice strings and alters the printing of the unit vectors to
            reflect this choice.
        latexs : tuple of str
            Alters the LaTeX printing of the reference frame's basis unit
            vectors to the provided three valid LaTeX strings.

        Examples
        ========

        >>> from sympy.physics.vector import ReferenceFrame, vlatex
        >>> N = ReferenceFrame('N')
        >>> N.x
        N.x
        >>> O = ReferenceFrame('O', indices=('1', '2', '3'))
        >>> O.x
        O['1']
        >>> O['1']
        O['1']
        >>> P = ReferenceFrame('P', latexs=('A1', 'A2', 'A3'))
        >>> vlatex(P.x)
        'A1'

        ``symbols()`` can be used to create multiple Reference Frames in one
        step, for example:

        >>> from sympy.physics.vector import ReferenceFrame
        >>> from sympy import symbols
        >>> A, B, C = symbols('A B C', cls=ReferenceFrame)
        >>> D, E = symbols('D E', cls=ReferenceFrame, indices=('1', '2', '3'))
        >>> A[0]
        A_x
        >>> D.x
        D['1']
        >>> E.y
        E['2']
        >>> type(A) == type(D)
        True

        Unit dyads for the ReferenceFrame can be accessed through the attributes ``xx``, ``xy``, etc. For example:

        >>> from sympy.physics.vector import ReferenceFrame
        >>> N = ReferenceFrame('N')
        >>> N.yz
        (N.y|N.z)
        >>> N.zx
        (N.z|N.x)
        >>> P = ReferenceFrame('P', indices=['1', '2', '3'])
        >>> P.xx
        (P['1']|P['1'])
        >>> P.zy
        (P['3']|P['2'])

        Unit dyadic is also accessible via the ``u`` attribute:

        >>> from sympy.physics.vector import ReferenceFrame
        >>> N = ReferenceFrame('N')
        >>> N.u
        (N.x|N.x) + (N.y|N.y) + (N.z|N.z)
        >>> P = ReferenceFrame('P', indices=['1', '2', '3'])
        >>> P.u
        (P['1']|P['1']) + (P['2']|P['2']) + (P['3']|P['3'])

        zNeed to supply a valid nameNzSupply the indices as a listr   zSupply 3 indiceszIndices must be stringsz['r   z']r0      _z\mathbf{\hat{%s}_{%s}}z.xz.yz.z_x_y_zz\mathbf{\hat{%s}_x}z\mathbf{\hat{%s}_y}z\mathbf{\hat{%s}_z})xyzzLatex entries must be stringsr0   r   r   r   r0   r   r   r   r0   z)Supply the variable names as a list/tuplezSupply 3 variable nameszVariable names must be strings)r+   str	TypeErrortuplelistlenr   str_vecslowerpretty_vecs
latex_vecsindicesr   	_var_dict	_dcm_dict
_dcm_cache_ang_vel_dict_ang_acc_dict_dlist_curr   Matrixr:   r;   r<   r   varlistr   _countr   )r(   r   rL   latexs	variablesir#   r#   r$   __init__d   s   
M









zReferenceFrame.__init__c                 C   sh   t |ts|dk r| j| S td| jd |kr| jS | jd |kr&| jS | jd |kr0| jS td)z
        Returns basis vector for the provided index, if the index is a string.

        If the index is a number, returns the coordinate variable correspon-
        -ding to that index.
        r   zInvalid index providedr   r0   r8   zNot a defined index)r+   rC   rU   r   rL   r=   r>   r?   )r(   indr#   r#   r$   __getitem__   s   

zReferenceFrame.__getitem__c                 C   s   t | j| j| jgS r&   )iterr=   r>   r?   r'   r#   r#   r$   __iter__  r*   zReferenceFrame.__iter__c                 C      | j S )zReturns the name of the frame. )r   r'   r#   r#   r$   __str__  s   zReferenceFrame.__str__c                 C   s   dddd}||  vrtd| gg}g g}||krJ|dd }|D ]"}|d j|   }|D ]}||vrD||g }	|	|vrD||	 q0q#||ks|D ]}	|	d |krY||	 qL|jtd t|d	krj|d	 S d
}
t|
|| | j|j)a  Returns an inclusive list of reference frames that connect this
        reference frame to the provided reference frame.

        Parameters
        ==========
        other : ReferenceFrame
            The other reference frame to look for a connecting relationship to.
        num : integer
            ``0``, ``1``, and ``2`` will look for orientation, angular
            velocity, and angular acceleration relationships between the two
            frames, respectively.

        Returns
        =======
        list
            Inclusive list of reference frames that connect this reference
            frame to the other reference frame.

        Examples
        ========

        >>> from sympy.physics.vector import ReferenceFrame
        >>> A = ReferenceFrame('A')
        >>> B = ReferenceFrame('B')
        >>> C = ReferenceFrame('C')
        >>> D = ReferenceFrame('D')
        >>> B.orient_axis(A, A.x, 1.0)
        >>> C.orient_axis(B, B.x, 1.0)
        >>> D.orient_axis(C, C.x, 1.0)
        >>> D._dict_list(A, 0)
        [D, C, B, A]

        Raises
        ======

        ValueError
            When no path is found between the two reference frames or ``num``
            is an incorrect value.

        orientationzangular velocityzangular acceleration)r   r0   r8   z$Valid values for num are 0, 1, or 2.N)keyr   z.No connecting {} path found between {} and {}.)	keysr   rR   appendremovesortrG   formatr   )r(   r-   numconnect_typepossible_connecting_pathsoldlist
frame_listframes_adjacent_to_lastadjacent_frameconnecting_pathmsgr#   r#   r$   
_dict_list  s:   *



zReferenceFrame._dict_listc           	      C   s   ddl m} || }||j}||j }tt|d dd}tt|d dd}tt|d dd}tt	|||g|fgS )z4Angular velocity from time differentiating the DCM. r   dynamicsymbols   T)	recursiver8   r   )
sympy.physics.vector.functionsrt   dcmr   _tTr   r   r   rT   )	r(   
otherframert   dcm2diffdiffed	angvelmatw1w2w3r#   r#   r$   _w_diff_dcme  s   

zReferenceFrame._w_diff_dcmc                 C   s   t | |tjf| jv r| j|tjf S | |t|j }i }t| D ]\}}tjr9t|| dd|| j| < q$|| || j| < q$|| j|tjf< |S )a4  
        Returns a dictionary which expresses the coordinate variables
        of this frame in terms of the variables of otherframe.

        If Vector.simp is True, returns a simplified version of the mapped
        values. Else, returns them without simplification.

        Simplification of the expressions may take time.

        Parameters
        ==========

        otherframe : ReferenceFrame
            The other frame to map the variables to

        Examples
        ========

        >>> from sympy.physics.vector import ReferenceFrame, dynamicsymbols
        >>> A = ReferenceFrame('A')
        >>> q = dynamicsymbols('q')
        >>> B = A.orientnew('B', 'Axis', [q, A.z])
        >>> A.variable_map(B)
        {A_x: B_x*cos(q(t)) - B_y*sin(q(t)), A_y: B_x*sin(q(t)) + B_y*cos(q(t)), A_z: B_z}

        fu)method)	r   r   simprM   rx   rT   rU   	enumerater   )r(   r{   vars_matrixmappingrY   r=   r#   r#   r$   variable_mapp  s   zReferenceFrame.variable_mapc                 C   s,   t | || jv r| j| S | ||S )a  Returns the angular acceleration Vector of the ReferenceFrame.

        Effectively returns the Vector:

        ``N_alpha_B``

        which represent the angular acceleration of B in N, where B is self,
        and N is otherframe.

        Parameters
        ==========

        otherframe : ReferenceFrame
            The ReferenceFrame which the angular acceleration is returned in.

        Examples
        ========

        >>> from sympy.physics.vector import ReferenceFrame
        >>> N = ReferenceFrame('N')
        >>> A = ReferenceFrame('A')
        >>> V = 10 * N.x
        >>> A.set_ang_acc(N, V)
        >>> A.ang_acc_in(N)
        10*N.x

        )r   rQ   
ang_vel_indt)r(   r{   r#   r#   r$   
ang_acc_in  s   

zReferenceFrame.ang_acc_inc                 C   sP   t | | |d}td}tt|d D ]}||| j||d   7 }q|S )a  Returns the angular velocity Vector of the ReferenceFrame.

        Effectively returns the Vector:

        ^N omega ^B

        which represent the angular velocity of B in N, where B is self, and
        N is otherframe.

        Parameters
        ==========

        otherframe : ReferenceFrame
            The ReferenceFrame which the angular velocity is returned in.

        Examples
        ========

        >>> from sympy.physics.vector import ReferenceFrame
        >>> N = ReferenceFrame('N')
        >>> A = ReferenceFrame('A')
        >>> V = 10 * N.x
        >>> A.set_ang_vel(N, V)
        >>> A.ang_vel_in(N)
        10*N.x

        r0   r   )r   rr   r   r   rG   rP   )r(   r{   flistoutvecrY   r#   r#   r$   r     s   zReferenceFrame.ang_vel_inc                 C   sz   t | || jv r| j| S | |d}td}tt|d D ]}||| j||d    }q || j|< |j|j| < |S )a  Returns the direction cosine matrix of this reference frame
        relative to the provided reference frame.

        The returned matrix can be used to express the orthogonal unit vectors
        of this frame in terms of the orthogonal unit vectors of
        ``otherframe``.

        Parameters
        ==========

        otherframe : ReferenceFrame
            The reference frame which the direction cosine matrix of this frame
            is formed relative to.

        Examples
        ========

        The following example rotates the reference frame A relative to N by a
        simple rotation and then calculates the direction cosine matrix of N
        relative to A.

        >>> from sympy import symbols, sin, cos
        >>> from sympy.physics.vector import ReferenceFrame
        >>> q1 = symbols('q1')
        >>> N = ReferenceFrame('N')
        >>> A = ReferenceFrame('A')
        >>> A.orient_axis(N, q1, N.x)
        >>> N.dcm(A)
        Matrix([
        [1,       0,        0],
        [0, cos(q1), -sin(q1)],
        [0, sin(q1),  cos(q1)]])

        The second row of the above direction cosine matrix represents the
        ``N.y`` unit vector in N expressed in A. Like so:

        >>> Ny = 0*A.x + cos(q1)*A.y - sin(q1)*A.z

        Thus, expressing ``N.y`` in A should return the same result:

        >>> N.y.express(A)
        cos(q1)*A.y - sin(q1)*A.z

        Notes
        =====

        It is important to know what form of the direction cosine matrix is
        returned. If ``B.dcm(A)`` is called, it means the "direction cosine
        matrix of B rotated relative to A". This is the matrix
        :math:`{}^B\mathbf{C}^A` shown in the following relationship:

        .. math::

           \begin{bmatrix}
             \hat{\mathbf{b}}_1 \\
             \hat{\mathbf{b}}_2 \\
             \hat{\mathbf{b}}_3
           \end{bmatrix}
           =
           {}^B\mathbf{C}^A
           \begin{bmatrix}
             \hat{\mathbf{a}}_1 \\
             \hat{\mathbf{a}}_2 \\
             \hat{\mathbf{a}}_3
           \end{bmatrix}.

        :math:`{}^B\mathbf{C}^A` is the matrix that expresses the B unit
        vectors in terms of the A unit vectors.

        r   r   r0   )r   rO   rr   r   r   rG   rN   rz   )r(   r{   r   outdcmrY   r#   r#   r$   rx     s   H


zReferenceFrame.dcmc                 C   s8  | j  }g }g }||v r?|D ]}|| jv r||g7 }||g7 }q|D ]}|j| = q#|D ]}|j | = q,i  | _| jd< i | _ n9g }t|}d}	|rx|	rx|d}
|
|vrt||
 |
j }|D ]}||krntd d}	 n|| q`|rx|	sK| j||j	i |j| |i | j ||j	i |j | |i d S )Nr   TzxLoops are defined among the orientation of frames. This is likely not desired and may cause errors in your calculations.F)
rO   rd   rN   rR   rF   popre   r   updaterz   )r(   parentparent_orientframesdcm_dict_deldcm_cache_delr   visitedqueuecontnode	neighborsneighborr#   r#   r$   _dcm8  sB   







zReferenceFrame._dcmc                 C   s4  ddl m} t| t|tst|tr||}}t|}t|}||dks,td|	|
 }|jd d }td||j  t| td|d  |d g|d d|d  g|d  |d dggt|  ||j  }| || ||j}	|	|	|
  }
| j||
i |j| |
 i i | _dS )aZ  Sets the orientation of this reference frame with respect to a
        parent reference frame by rotating through an angle about an axis fixed
        in the parent reference frame.

        Parameters
        ==========

        parent : ReferenceFrame
            Reference frame that this reference frame will be rotated relative
            to.
        axis : Vector
            Vector fixed in the parent frame about about which this frame is
            rotated. It need not be a unit vector and the rotation follows the
            right hand rule.
        angle : sympifiable
            Angle in radians by which it the frame is to be rotated.

        Warns
        ======

        UserWarning
            If the orientation creates a kinematic loop.

        Examples
        ========

        Setup variables for the examples:

        >>> from sympy import symbols
        >>> from sympy.physics.vector import ReferenceFrame
        >>> q1 = symbols('q1')
        >>> N = ReferenceFrame('N')
        >>> B = ReferenceFrame('B')
        >>> B.orient_axis(N, N.x, q1)

        The ``orient_axis()`` method generates a direction cosine matrix and
        its transpose which defines the orientation of B relative to N and vice
        versa. Once orient is called, ``dcm()`` outputs the appropriate
        direction cosine matrix:

        >>> B.dcm(N)
        Matrix([
        [1,       0,      0],
        [0,  cos(q1), sin(q1)],
        [0, -sin(q1), cos(q1)]])
        >>> N.dcm(B)
        Matrix([
        [1,       0,        0],
        [0, cos(q1), -sin(q1)],
        [0, sin(q1),  cos(q1)]])

        The following two lines show that the sense of the rotation can be
        defined by negating the vector direction or the angle. Both lines
        produce the same result.

        >>> B.orient_axis(N, -N.x, q1)
        >>> B.orient_axis(N, N.x, -q1)

        r   rs   zAxis cannot be time-varying.r   r8   r0   N)rw   rt   r   r+   r   r   r   r   r   express	normalizeargsr   rz   r   rT   r   r   r   ry   rP   r   rM   )r(   r   axisanglert   theta	unit_axisunit_colparent_orient_axisthetadwvecr#   r#   r$   orient_axism  s6   =

zReferenceFrame.orient_axisc                 C   s,   t | t|tstd| ||j dS )a  Sets the orientation of this reference frame relative to another (parent) reference frame
        using a direction cosine matrix that describes the rotation from the parent to the child.

        Parameters
        ==========

        parent : ReferenceFrame
            Reference frame that this reference frame will be rotated relative
            to.
        dcm : Matrix, shape(3, 3)
            Direction cosine matrix that specifies the relative rotation
            between the two reference frames.

        Warns
        ======

        UserWarning
            If the orientation creates a kinematic loop.

        Examples
        ========

        Setup variables for the examples:

        >>> from sympy import symbols, Matrix, sin, cos
        >>> from sympy.physics.vector import ReferenceFrame
        >>> q1 = symbols('q1')
        >>> A = ReferenceFrame('A')
        >>> B = ReferenceFrame('B')
        >>> N = ReferenceFrame('N')

        A simple rotation of ``A`` relative to ``N`` about ``N.x`` is defined
        by the following direction cosine matrix:

        >>> dcm = Matrix([[1, 0, 0],
        ...               [0, cos(q1), -sin(q1)],
        ...               [0, sin(q1), cos(q1)]])
        >>> A.orient_explicit(N, dcm)
        >>> A.dcm(N)
        Matrix([
        [1,       0,      0],
        [0,  cos(q1), sin(q1)],
        [0, -sin(q1), cos(q1)]])

        This is equivalent to using ``orient_axis()``:

        >>> B.orient_axis(N, N.x, q1)
        >>> B.dcm(N)
        Matrix([
        [1,       0,      0],
        [0,  cos(q1), sin(q1)],
        [0, -sin(q1), cos(q1)]])

        **Note carefully that** ``N.dcm(B)`` **(the transpose) would be passed
        into** ``orient_explicit()`` **for** ``A.dcm(N)`` **to match**
        ``B.dcm(N)``:

        >>> A.orient_explicit(N, N.dcm(B))
        >>> A.dcm(N)
        Matrix([
        [1,       0,      0],
        [0,  cos(q1), sin(q1)],
        [0, -sin(q1), cos(q1)]])

        +Amounts must be a SymPy Matrix type object.N)r   r+   r
   rD   
orient_dcmrz   )r(   r   rx   r#   r#   r$   orient_explicit  s   B
zReferenceFrame.orient_explicitc                 C   s^   t | t|tstd| ||j | |}| j||i |j| | i i | _	dS )aU  Sets the orientation of this reference frame relative to another (parent) reference frame
        using a direction cosine matrix that describes the rotation from the child to the parent.

        Parameters
        ==========

        parent : ReferenceFrame
            Reference frame that this reference frame will be rotated relative
            to.
        dcm : Matrix, shape(3, 3)
            Direction cosine matrix that specifies the relative rotation
            between the two reference frames.

        Warns
        ======

        UserWarning
            If the orientation creates a kinematic loop.

        Examples
        ========

        Setup variables for the examples:

        >>> from sympy import symbols, Matrix, sin, cos
        >>> from sympy.physics.vector import ReferenceFrame
        >>> q1 = symbols('q1')
        >>> A = ReferenceFrame('A')
        >>> B = ReferenceFrame('B')
        >>> N = ReferenceFrame('N')

        A simple rotation of ``A`` relative to ``N`` about ``N.x`` is defined
        by the following direction cosine matrix:

        >>> dcm = Matrix([[1, 0, 0],
        ...               [0,  cos(q1), sin(q1)],
        ...               [0, -sin(q1), cos(q1)]])
        >>> A.orient_dcm(N, dcm)
        >>> A.dcm(N)
        Matrix([
        [1,       0,      0],
        [0,  cos(q1), sin(q1)],
        [0, -sin(q1), cos(q1)]])

        This is equivalent to using ``orient_axis()``:

        >>> B.orient_axis(N, N.x, q1)
        >>> B.dcm(N)
        Matrix([
        [1,       0,      0],
        [0,  cos(q1), sin(q1)],
        [0, -sin(q1), cos(q1)]])

        r   N)
r   r+   r
   rD   r   rz   r   rP   r   rM   )r(   r   rx   r   r#   r#   r$   r     s   8


zReferenceFrame.orient_dcmc                 C   s   |dkrt g ddt|t| gdt|t|ggS |dkr8t t|dt|gg dt| dt|ggS |dkrTt t|t| dgt|t|dgg dgS dS )	z'DCM for simple axis 1,2,or 3 rotations.r0   r@   r   r8   rA   r   rB   N)rT   r   r   )r(   r   r   r#   r#   r$   _rotU  s    zReferenceFrame._rotc           	         s   t |}t|D ]\}}t|tst|||< qd}tt|dd}||vr*tddd |D }t|dt|@   krDdksItd td fd	dt	||D }|||fS )
a  Helper for orient_body_fixed and orient_space_fixed.

        Parameters
        ==========
        angles : 3-tuple of sympifiable
            Three angles in radians used for the successive rotations.
        rotation_order : 3 character string or 3 digit integer
            Order of the rotations. The order can be specified by the strings
            ``'XZX'``, ``'131'``, or the integer ``131``. There are 12 unique
            valid rotation orders.

        Returns
        =======

        amounts : list
            List of sympifiables corresponding to the rotation angles.
        rot_order : list
            List of integers corresponding to the axis of rotation.
        rot_matrices : list
            List of DCM around the given axis with corresponding magnitude.

        123231312132213321121131212232313323 XYZxyz123123z(The rotation order is not a valid order.c                 S   s   g | ]}t |qS r#   )int).0rr#   r#   r$   
<listcomp>  s    z?ReferenceFrame._parse_consecutive_rotations.<locals>.<listcomp>r   z*Body orientation takes 3 values & 3 ordersc                    s   g | ]
\}}  ||qS r#   )r   )r   orderamountr'   r#   r$   r     s    )
rF   r   r+   r   r   r   rC   rD   rG   zip)	r(   anglesrotation_orderamountsrY   vapproved_orders	rot_orderrot_matricesr#   r'   r$   _parse_consecutive_rotationsd  s$   


z+ReferenceFrame._parse_consecutive_rotationsc                 C   s  ddl m} t| | ||\}}}| ||d |d  |d   dd tdD }t|D ]\}	}
||	 |j||	 |
d < q0|d |d j	|d |d j	|d     \}}}|| j
 || j  || j  }| j||i |j| | i i | _dS )	a,  Rotates this reference frame relative to the parent reference frame
        by right hand rotating through three successive body fixed simple axis
        rotations. Each subsequent axis of rotation is about the "body fixed"
        unit vectors of a new intermediate reference frame. This type of
        rotation is also referred to rotating through the `Euler and Tait-Bryan
        Angles`_.

        .. _Euler and Tait-Bryan Angles: https://en.wikipedia.org/wiki/Euler_angles

        The computed angular velocity in this method is by default expressed in
        the child's frame, so it is most preferable to use ``u1 * child.x + u2 *
        child.y + u3 * child.z`` as generalized speeds.

        Parameters
        ==========

        parent : ReferenceFrame
            Reference frame that this reference frame will be rotated relative
            to.
        angles : 3-tuple of sympifiable
            Three angles in radians used for the successive rotations.
        rotation_order : 3 character string or 3 digit integer
            Order of the rotations about each intermediate reference frames'
            unit vectors. The Euler rotation about the X, Z', X'' axes can be
            specified by the strings ``'XZX'``, ``'131'``, or the integer
            ``131``. There are 12 unique valid rotation orders (6 Euler and 6
            Tait-Bryan): zxz, xyx, yzy, zyz, xzx, yxy, xyz, yzx, zxy, xzy, zyx,
            and yxz.

        Warns
        ======

        UserWarning
            If the orientation creates a kinematic loop.

        Examples
        ========

        Setup variables for the examples:

        >>> from sympy import symbols
        >>> from sympy.physics.vector import ReferenceFrame
        >>> q1, q2, q3 = symbols('q1, q2, q3')
        >>> N = ReferenceFrame('N')
        >>> B = ReferenceFrame('B')
        >>> B1 = ReferenceFrame('B1')
        >>> B2 = ReferenceFrame('B2')
        >>> B3 = ReferenceFrame('B3')

        For example, a classic Euler Angle rotation can be done by:

        >>> B.orient_body_fixed(N, (q1, q2, q3), 'XYX')
        >>> B.dcm(N)
        Matrix([
        [        cos(q2),                            sin(q1)*sin(q2),                           -sin(q2)*cos(q1)],
        [sin(q2)*sin(q3), -sin(q1)*sin(q3)*cos(q2) + cos(q1)*cos(q3),  sin(q1)*cos(q3) + sin(q3)*cos(q1)*cos(q2)],
        [sin(q2)*cos(q3), -sin(q1)*cos(q2)*cos(q3) - sin(q3)*cos(q1), -sin(q1)*sin(q3) + cos(q1)*cos(q2)*cos(q3)]])

        This rotates reference frame B relative to reference frame N through
        ``q1`` about ``N.x``, then rotates B again through ``q2`` about
        ``B.y``, and finally through ``q3`` about ``B.x``. It is equivalent to
        three successive ``orient_axis()`` calls:

        >>> B1.orient_axis(N, N.x, q1)
        >>> B2.orient_axis(B1, B1.y, q2)
        >>> B3.orient_axis(B2, B2.x, q3)
        >>> B3.dcm(N)
        Matrix([
        [        cos(q2),                            sin(q1)*sin(q2),                           -sin(q2)*cos(q1)],
        [sin(q2)*sin(q3), -sin(q1)*sin(q3)*cos(q2) + cos(q1)*cos(q3),  sin(q1)*cos(q3) + sin(q3)*cos(q1)*cos(q2)],
        [sin(q2)*cos(q3), -sin(q1)*cos(q2)*cos(q3) - sin(q3)*cos(q1), -sin(q1)*sin(q3) + cos(q1)*cos(q2)*cos(q3)]])

        Acceptable rotation orders are of length 3, expressed in as a string
        ``'XYZ'`` or ``'123'`` or integer ``123``. Rotations about an axis
        twice in a row are prohibited.

        >>> B.orient_body_fixed(N, (q1, q2, 0), 'ZXZ')
        >>> B.orient_body_fixed(N, (q1, q2, 0), '121')
        >>> B.orient_body_fixed(N, (q1, q2, q3), 123)

        r   rs   r0   r8   c                 S      g | ]}t d dqS r   r0   r   r   r9   r#   r#   r$   r         z4ReferenceFrame.orient_body_fixed.<locals>.<listcomp>r   Nrw   rt   r   r   r   r   r   r   ry   rz   r=   r>   r?   rP   r   rM   r(   r   r   r   rt   r   r   r   rot_vecsrY   r   u1u2u3r   r#   r#   r$   orient_body_fixed  s    R
 
z ReferenceFrame.orient_body_fixedc                 C   s  ddl m} t| | ||\}}}| ||d |d  |d   dd tdD }t|D ]\}	}
||	 |j||	 |
d < q0|d |d j	|d |d j	|d     \}}}|| j
 || j  || j  }| j||i |j| | i i | _dS )	a[  Rotates this reference frame relative to the parent reference frame
        by right hand rotating through three successive space fixed simple axis
        rotations. Each subsequent axis of rotation is about the "space fixed"
        unit vectors of the parent reference frame.

        The computed angular velocity in this method is by default expressed in
        the child's frame, so it is most preferable to use ``u1 * child.x + u2 *
        child.y + u3 * child.z`` as generalized speeds.

        Parameters
        ==========
        parent : ReferenceFrame
            Reference frame that this reference frame will be rotated relative
            to.
        angles : 3-tuple of sympifiable
            Three angles in radians used for the successive rotations.
        rotation_order : 3 character string or 3 digit integer
            Order of the rotations about the parent reference frame's unit
            vectors. The order can be specified by the strings ``'XZX'``,
            ``'131'``, or the integer ``131``. There are 12 unique valid
            rotation orders.

        Warns
        ======

        UserWarning
            If the orientation creates a kinematic loop.

        Examples
        ========

        Setup variables for the examples:

        >>> from sympy import symbols
        >>> from sympy.physics.vector import ReferenceFrame
        >>> q1, q2, q3 = symbols('q1, q2, q3')
        >>> N = ReferenceFrame('N')
        >>> B = ReferenceFrame('B')
        >>> B1 = ReferenceFrame('B1')
        >>> B2 = ReferenceFrame('B2')
        >>> B3 = ReferenceFrame('B3')

        >>> B.orient_space_fixed(N, (q1, q2, q3), '312')
        >>> B.dcm(N)
        Matrix([
        [ sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3), sin(q1)*cos(q2), sin(q1)*sin(q2)*cos(q3) - sin(q3)*cos(q1)],
        [-sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1), cos(q1)*cos(q2), sin(q1)*sin(q3) + sin(q2)*cos(q1)*cos(q3)],
        [                           sin(q3)*cos(q2),        -sin(q2),                           cos(q2)*cos(q3)]])

        is equivalent to:

        >>> B1.orient_axis(N, N.z, q1)
        >>> B2.orient_axis(B1, N.x, q2)
        >>> B3.orient_axis(B2, N.y, q3)
        >>> B3.dcm(N).simplify()
        Matrix([
        [ sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3), sin(q1)*cos(q2), sin(q1)*sin(q2)*cos(q3) - sin(q3)*cos(q1)],
        [-sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1), cos(q1)*cos(q2), sin(q1)*sin(q3) + sin(q2)*cos(q1)*cos(q3)],
        [                           sin(q3)*cos(q2),        -sin(q2),                           cos(q2)*cos(q3)]])

        It is worth noting that space-fixed and body-fixed rotations are
        related by the order of the rotations, i.e. the reverse order of body
        fixed will give space fixed and vice versa.

        >>> B.orient_space_fixed(N, (q1, q2, q3), '231')
        >>> B.dcm(N)
        Matrix([
        [cos(q1)*cos(q2), sin(q1)*sin(q3) + sin(q2)*cos(q1)*cos(q3), -sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1)],
        [       -sin(q2),                           cos(q2)*cos(q3),                            sin(q3)*cos(q2)],
        [sin(q1)*cos(q2), sin(q1)*sin(q2)*cos(q3) - sin(q3)*cos(q1),  sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3)]])

        >>> B.orient_body_fixed(N, (q3, q2, q1), '132')
        >>> B.dcm(N)
        Matrix([
        [cos(q1)*cos(q2), sin(q1)*sin(q3) + sin(q2)*cos(q1)*cos(q3), -sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1)],
        [       -sin(q2),                           cos(q2)*cos(q3),                            sin(q3)*cos(q2)],
        [sin(q1)*cos(q2), sin(q1)*sin(q2)*cos(q3) - sin(q3)*cos(q1),  sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3)]])

        r   rs   r8   r0   c                 S   r   r   r   r   r#   r#   r$   r   J  r   z5ReferenceFrame.orient_space_fixed.<locals>.<listcomp>r   Nr   r   r#   r#   r$   orient_space_fixed  s    P
 
z!ReferenceFrame.orient_space_fixedc                 C   s<  ddl m} t| t|}t|D ]\}}t|ts!t|||< qt|ttft	|dk@ s3t
d|\}}}}	t|d |d  |d  |	d  d|| ||	   d|| ||	   gd|| ||	   |d |d  |d  |	d  d||	 ||   gd||	 ||   d|| ||	   |d |d  |d  |	d  gg}
| ||
 |j}|\}}}}	t||}t||}t||}t|	|}d|| ||	  ||  ||   }d|| ||  ||	  ||   }d|| ||  ||  ||	   }tt|||g| fg}| j||i |j| | i i | _dS )a   Sets the orientation of this reference frame relative to a parent
        reference frame via an orientation quaternion. An orientation
        quaternion is defined as a finite rotation a unit vector, ``(lambda_x,
        lambda_y, lambda_z)``, by an angle ``theta``. The orientation
        quaternion is described by four parameters:

        - ``q0 = cos(theta/2)``
        - ``q1 = lambda_x*sin(theta/2)``
        - ``q2 = lambda_y*sin(theta/2)``
        - ``q3 = lambda_z*sin(theta/2)``

        See `Quaternions and Spatial Rotation
        <https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation>`_ on
        Wikipedia for more information.

        Parameters
        ==========
        parent : ReferenceFrame
            Reference frame that this reference frame will be rotated relative
            to.
        numbers : 4-tuple of sympifiable
            The four quaternion scalar numbers as defined above: ``q0``,
            ``q1``, ``q2``, ``q3``.

        Warns
        ======

        UserWarning
            If the orientation creates a kinematic loop.

        Examples
        ========

        Setup variables for the examples:

        >>> from sympy import symbols
        >>> from sympy.physics.vector import ReferenceFrame
        >>> q0, q1, q2, q3 = symbols('q0 q1 q2 q3')
        >>> N = ReferenceFrame('N')
        >>> B = ReferenceFrame('B')

        Set the orientation:

        >>> B.orient_quaternion(N, (q0, q1, q2, q3))
        >>> B.dcm(N)
        Matrix([
        [q0**2 + q1**2 - q2**2 - q3**2,             2*q0*q3 + 2*q1*q2,            -2*q0*q2 + 2*q1*q3],
        [           -2*q0*q3 + 2*q1*q2, q0**2 - q1**2 + q2**2 - q3**2,             2*q0*q1 + 2*q2*q3],
        [            2*q0*q2 + 2*q1*q3,            -2*q0*q1 + 2*q2*q3, q0**2 - q1**2 - q2**2 + q3**2]])

        r   rs      z'Amounts are a list or tuple of length 4r8   N)rw   rt   r   rF   r   r+   r   r   rE   rG   rD   rT   r   ry   r   rP   r   rM   )r(   r   numbersrt   rY   r   q0q1q2q3parent_orient_quaterniontq0dq1dq2dq3dr   r   r   r   r#   r#   r$   orient_quaternionT  sL   5
 



$$$
z ReferenceFrame.orient_quaternionr   c                 C   s   t | d}tt|dd}| }||vrtd|dkr+| ||d |d  dS |dkr7| || dS |d	krD| ||| dS |d
krQ| ||| dS |dkr]| 	|| dS t
d)a  Sets the orientation of this reference frame relative to another
        (parent) reference frame.

        .. note:: It is now recommended to use the ``.orient_axis,
           .orient_body_fixed, .orient_space_fixed, .orient_quaternion``
           methods for the different rotation types.

        Parameters
        ==========

        parent : ReferenceFrame
            Reference frame that this reference frame will be rotated relative
            to.
        rot_type : str
            The method used to generate the direction cosine matrix. Supported
            methods are:

            - ``'Axis'``: simple rotations about a single common axis
            - ``'DCM'``: for setting the direction cosine matrix directly
            - ``'Body'``: three successive rotations about new intermediate
              axes, also called "Euler and Tait-Bryan angles"
            - ``'Space'``: three successive rotations about the parent
              frames' unit vectors
            - ``'Quaternion'``: rotations defined by four parameters which
              result in a singularity free direction cosine matrix

        amounts :
            Expressions defining the rotation angles or direction cosine
            matrix. These must match the ``rot_type``. See examples below for
            details. The input types are:

            - ``'Axis'``: 2-tuple (expr/sym/func, Vector)
            - ``'DCM'``: Matrix, shape(3,3)
            - ``'Body'``: 3-tuple of expressions, symbols, or functions
            - ``'Space'``: 3-tuple of expressions, symbols, or functions
            - ``'Quaternion'``: 4-tuple of expressions, symbols, or
              functions

        rot_order : str or int, optional
            If applicable, the order of the successive of rotations. The string
            ``'123'`` and integer ``123`` are equivalent, for example. Required
            for ``'Body'`` and ``'Space'``.

        Warns
        ======

        UserWarning
            If the orientation creates a kinematic loop.

        r   r   r   *The supplied order is not an approved typeAXISr0   r   DCMBODYSPACE
QUATERNION#That is not an implemented rotationN)r   r   rC   upperrD   r   r   r   r   r   NotImplementedError)r(   r   rot_typer   r   r   r#   r#   r$   orient  s"   4zReferenceFrame.orientc           
      C   s   | j ||||d}d}	tt|dd}| }||	vrtd|dkr0|| |d |d  |S |d	kr<|| | |S |d
krI|| || |S |dkrV|| || |S |dkrb|	| | |S t
d)al  Returns a new reference frame oriented with respect to this
        reference frame.

        See ``ReferenceFrame.orient()`` for detailed examples of how to orient
        reference frames.

        Parameters
        ==========

        newname : str
            Name for the new reference frame.
        rot_type : str
            The method used to generate the direction cosine matrix. Supported
            methods are:

            - ``'Axis'``: simple rotations about a single common axis
            - ``'DCM'``: for setting the direction cosine matrix directly
            - ``'Body'``: three successive rotations about new intermediate
              axes, also called "Euler and Tait-Bryan angles"
            - ``'Space'``: three successive rotations about the parent
              frames' unit vectors
            - ``'Quaternion'``: rotations defined by four parameters which
              result in a singularity free direction cosine matrix

        amounts :
            Expressions defining the rotation angles or direction cosine
            matrix. These must match the ``rot_type``. See examples below for
            details. The input types are:

            - ``'Axis'``: 2-tuple (expr/sym/func, Vector)
            - ``'DCM'``: Matrix, shape(3,3)
            - ``'Body'``: 3-tuple of expressions, symbols, or functions
            - ``'Space'``: 3-tuple of expressions, symbols, or functions
            - ``'Quaternion'``: 4-tuple of expressions, symbols, or
              functions

        rot_order : str or int, optional
            If applicable, the order of the successive of rotations. The string
            ``'123'`` and integer ``123`` are equivalent, for example. Required
            for ``'Body'`` and ``'Space'``.
        indices : tuple of str
            Enables the reference frame's basis unit vectors to be accessed by
            Python's square bracket indexing notation using the provided three
            indice strings and alters the printing of the unit vectors to
            reflect this choice.
        latexs : tuple of str
            Alters the LaTeX printing of the reference frame's basis unit
            vectors to the provided three valid LaTeX strings.

        Examples
        ========

        >>> from sympy import symbols
        >>> from sympy.physics.vector import ReferenceFrame, vlatex
        >>> q0, q1, q2, q3 = symbols('q0 q1 q2 q3')
        >>> N = ReferenceFrame('N')

        Create a new reference frame A rotated relative to N through a simple
        rotation.

        >>> A = N.orientnew('A', 'Axis', (q0, N.x))

        Create a new reference frame B rotated relative to N through body-fixed
        rotations.

        >>> B = N.orientnew('B', 'Body', (q1, q2, q3), '123')

        Create a new reference frame C rotated relative to N through a simple
        rotation with unique indices and LaTeX printing.

        >>> C = N.orientnew('C', 'Axis', (q0, N.x), indices=('1', '2', '3'),
        ... latexs=(r'\hat{\mathbf{c}}_1',r'\hat{\mathbf{c}}_2',
        ... r'\hat{\mathbf{c}}_3'))
        >>> C['1']
        C['1']
        >>> print(vlatex(C['1']))
        \hat{\mathbf{c}}_1

        )rX   rL   rW   r   r   r   r   r   r0   r   r   r   r   r   r   )r"   r   rC   r   rD   r   r   r   r   r   r   )
r(   newnamer   r   r   rX   rL   rW   newframer   r#   r#   r$   	orientnew   s0   R
zReferenceFrame.orientnewc                 C   F   |dkrt d}t|}t| | j||i |j| | i dS )a-  Define the angular acceleration Vector in a ReferenceFrame.

        Defines the angular acceleration of this ReferenceFrame, in another.
        Angular acceleration can be defined with respect to multiple different
        ReferenceFrames. Care must be taken to not create loops which are
        inconsistent.

        Parameters
        ==========

        otherframe : ReferenceFrame
            A ReferenceFrame to define the angular acceleration in
        value : Vector
            The Vector representing angular acceleration

        Examples
        ========

        >>> from sympy.physics.vector import ReferenceFrame
        >>> N = ReferenceFrame('N')
        >>> A = ReferenceFrame('A')
        >>> V = 10 * N.x
        >>> A.set_ang_acc(N, V)
        >>> A.ang_acc_in(N)
        10*N.x

        r   N)r   r   r   rQ   r   r(   r{   valuer#   r#   r$   set_ang_accp     zReferenceFrame.set_ang_accc                 C   r   )a  Define the angular velocity vector in a ReferenceFrame.

        Defines the angular velocity of this ReferenceFrame, in another.
        Angular velocity can be defined with respect to multiple different
        ReferenceFrames. Care must be taken to not create loops which are
        inconsistent.

        Parameters
        ==========

        otherframe : ReferenceFrame
            A ReferenceFrame to define the angular velocity in
        value : Vector
            The Vector representing angular velocity

        Examples
        ========

        >>> from sympy.physics.vector import ReferenceFrame
        >>> N = ReferenceFrame('N')
        >>> A = ReferenceFrame('A')
        >>> V = 10 * N.x
        >>> A.set_ang_vel(N, V)
        >>> A.ang_vel_in(N)
        10*N.x

        r   N)r   r   r   rP   r   r   r#   r#   r$   set_ang_vel  r   zReferenceFrame.set_ang_velc                 C   r_   )z=The basis Vector for the ReferenceFrame, in the x direction. )r:   r'   r#   r#   r$   r=        zReferenceFrame.xc                 C   r_   )z=The basis Vector for the ReferenceFrame, in the y direction. )r;   r'   r#   r#   r$   r>     r   zReferenceFrame.yc                 C   r_   )z=The basis Vector for the ReferenceFrame, in the z direction. )r<   r'   r#   r#   r$   r?     r   zReferenceFrame.zc                 C      t | j| jS )z:Unit dyad of basis Vectors x and x for the ReferenceFrame.)r   outerr=   r'   r#   r#   r$   xx     zReferenceFrame.xxc                 C      t | j| jS )z:Unit dyad of basis Vectors x and y for the ReferenceFrame.)r   r   r=   r>   r'   r#   r#   r$   xy  r   zReferenceFrame.xyc                 C   r   )z:Unit dyad of basis Vectors x and z for the ReferenceFrame.)r   r   r=   r?   r'   r#   r#   r$   xz  r   zReferenceFrame.xzc                 C   r   )z:Unit dyad of basis Vectors y and x for the ReferenceFrame.)r   r   r>   r=   r'   r#   r#   r$   yx  r   zReferenceFrame.yxc                 C   r   )z:Unit dyad of basis Vectors y and y for the ReferenceFrame.)r   r   r>   r'   r#   r#   r$   yy  r   zReferenceFrame.yyc                 C   r   )z:Unit dyad of basis Vectors y and z for the ReferenceFrame.)r   r   r>   r?   r'   r#   r#   r$   yz  r   zReferenceFrame.yzc                 C   r   )z:Unit dyad of basis Vectors z and x for the ReferenceFrame.)r   r   r?   r=   r'   r#   r#   r$   zx  r   zReferenceFrame.zxc                 C   r   )z:Unit dyad of basis Vectors z and y for the ReferenceFrame.)r   r   r?   r>   r'   r#   r#   r$   zy  r   zReferenceFrame.zyc                 C   r   )z:Unit dyad of basis Vectors z and z for the ReferenceFrame.)r   r   r?   r'   r#   r#   r$   zz  r   zReferenceFrame.zzc                 C   s   | j | j | j S )z#Unit dyadic for the ReferenceFrame.)r   r  r  r'   r#   r#   r$   u  s   zReferenceFrame.uc                 G   sD   ddl m} | |}||g||d }t|dkr|d S t|S )a  Returns the partial angular velocities of this frame in the given
        frame with respect to one or more provided generalized speeds.

        Parameters
        ==========
        frame : ReferenceFrame
            The frame with which the angular velocity is defined in.
        gen_speeds : functions of time
            The generalized speeds.

        Returns
        =======
        partial_velocities : tuple of Vector
            The partial angular velocity vectors corresponding to the provided
            generalized speeds.

        Examples
        ========

        >>> from sympy.physics.vector import ReferenceFrame, dynamicsymbols
        >>> N = ReferenceFrame('N')
        >>> A = ReferenceFrame('A')
        >>> u1, u2 = dynamicsymbols('u1, u2')
        >>> A.set_ang_vel(N, u1 * A.x + u2 * N.y)
        >>> A.partial_velocity(N, u1)
        A.x
        >>> A.partial_velocity(N, u1, u2)
        (A.x, N.y)

        r   )partial_velocityr0   )rw   r
  r   rG   rE   )r(   r   
gen_speedsr
  velpartialsr#   r#   r$   r
    s    
zReferenceFrame.partial_velocity)NNN)r   )r   NNN),r2   r3   r4   r5   rV   rZ   r\   r^   r`   __repr__rr   r   r   r   r   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r6   r=   r>   r?   r   r  r  r  r  r  r  r  r  r	  r
  r#   r#   r#   r$   r   U   sr    
 H+#$V5YJE*db
\P
p$$












c                 C   s(   ddl m} t| ts|| tdd S )Nr0   )VectorTypeErrorA)vectorr  r+   r   )r-   r  r#   r#   r$   r   $  s   
r   N)sympyr   r   r   r   r   r   r   r	   rT   r
   sympy.core.symbolr   sympy.simplify.trigsimpr   sympy.physics.vector.vectorr   r   sympy.utilities.miscr   warningsr   __all__r   r   r   r#   r#   r#   r$   <module>   s*   , H           Z