o
    ohfb                     @   s  d dl mZ d dlmZ d dlmZmZmZmZm	Z	 d dl
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 d dlmZmZmZmZmZmZmZ d d	lmZ d d
l m!Z"m#Z$ d dl%m&Z& g dZ'eZ(eZ)eZ*eZ+dd Z,ej-e,_-d9ddZ!dd Z#dd Z.dd Z/dd Z0dd Z1dd Z2dd Z3dd  Z4d:d"d#Z5d$d%d&d'Z6d(d) Z7d*d+ Z8d,d- Z9d.d/ Z:d0d1 Z;d2d3 Z<	4	!d;d5d6Z=d7d8 Z>d!S )<    )
dict_merge)iterable)DyadicVectorReferenceFramePointdynamicsymbols)vprintvsprintvpprintvlatexinit_vprinting)Particle)	RigidBody)simplify)MatrixMul
DerivativesincostanS)AppliedUndef)inertiainertia_of_point_mass)sympy_deprecation_warning)linear_momentumangular_momentumkinetic_energypotential_energy
Lagrangianmechanics_printingmprintmsprintmpprintmlatexmsubsfind_dynamicsymbolsc                  K   s   t di |  dS )z]
    Initializes time derivative printing for all SymPy objects in
    mechanics module.
    N )r   )kwargsr(   r(   u/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/physics/mechanics/functions.pyr!   &   s   r!   c                 C   s"   t dddd t| ||||||S )Nzh
        The inertia function has been moved.
        Import it from "sympy.physics.mechanics".
        1.13moved-mechanics-functionsdeprecated_since_versionactive_deprecations_target)r   _inertia)frameixxiyyizzixyiyzizxr(   r(   r*   r   1   s   r   c                 C   s   t dddd t| ||S )Nzv
        The inertia_of_point_mass function has been moved.
        Import it from "sympy.physics.mechanics".
        r+   r,   r-   )r   _inertia_of_point_mass)masspos_vecr1   r(   r(   r*   r   =   s   r   c                 G   sL   t | ts	tdtd}|D ]}t |ttfr ||| 7 }qtd|S )a  Linear momentum of the system.

    Explanation
    ===========

    This function returns the linear momentum of a system of Particle's and/or
    RigidBody's. The linear momentum of a system is equal to the vector sum of
    the linear momentum of its constituents. Consider a system, S, comprised of
    a rigid body, A, and a particle, P. The linear momentum of the system, L,
    is equal to the vector sum of the linear momentum of the particle, L1, and
    the linear momentum of the rigid body, L2, i.e.

    L = L1 + L2

    Parameters
    ==========

    frame : ReferenceFrame
        The frame in which linear momentum is desired.
    body1, body2, body3... : Particle and/or RigidBody
        The body (or bodies) whose linear momentum is required.

    Examples
    ========

    >>> from sympy.physics.mechanics import Point, Particle, ReferenceFrame
    >>> from sympy.physics.mechanics import RigidBody, outer, linear_momentum
    >>> N = ReferenceFrame('N')
    >>> P = Point('P')
    >>> P.set_vel(N, 10 * N.x)
    >>> Pa = Particle('Pa', P, 1)
    >>> Ac = Point('Ac')
    >>> Ac.set_vel(N, 25 * N.y)
    >>> I = outer(N.x, N.x)
    >>> A = RigidBody('A', Ac, N, 20, (I, Ac))
    >>> linear_momentum(N, A, Pa)
    10*N.x + 500*N.y

    z%Please specify a valid ReferenceFramer   **body must have only Particle or RigidBody)
isinstancer   	TypeErrorr   r   r   r   )r1   bodylinear_momentum_syser(   r(   r*   r   I   s   
)r   c                 G   s`   t |ts	tdt | tstdtd}|D ]}t |ttfr*||| |7 }qtd|S )a  Angular momentum of a system.

    Explanation
    ===========

    This function returns the angular momentum of a system of Particle's and/or
    RigidBody's. The angular momentum of such a system is equal to the vector
    sum of the angular momentum of its constituents. Consider a system, S,
    comprised of a rigid body, A, and a particle, P. The angular momentum of
    the system, H, is equal to the vector sum of the angular momentum of the
    particle, H1, and the angular momentum of the rigid body, H2, i.e.

    H = H1 + H2

    Parameters
    ==========

    point : Point
        The point about which angular momentum of the system is desired.
    frame : ReferenceFrame
        The frame in which angular momentum is desired.
    body1, body2, body3... : Particle and/or RigidBody
        The body (or bodies) whose angular momentum is required.

    Examples
    ========

    >>> from sympy.physics.mechanics import Point, Particle, ReferenceFrame
    >>> from sympy.physics.mechanics import RigidBody, outer, angular_momentum
    >>> N = ReferenceFrame('N')
    >>> O = Point('O')
    >>> O.set_vel(N, 0 * N.x)
    >>> P = O.locatenew('P', 1 * N.x)
    >>> P.set_vel(N, 10 * N.x)
    >>> Pa = Particle('Pa', P, 1)
    >>> Ac = O.locatenew('Ac', 2 * N.y)
    >>> Ac.set_vel(N, 5 * N.y)
    >>> a = ReferenceFrame('a')
    >>> a.set_ang_vel(N, 10 * N.z)
    >>> I = outer(N.z, N.z)
    >>> A = RigidBody('A', Ac, a, 20, (I, Ac))
    >>> angular_momentum(O, N, Pa, A)
    10*N.z

    #Please enter a valid ReferenceFramezPlease specify a valid Pointr   r;   )r<   r   r=   r   r   r   r   r   )pointr1   r>   angular_momentum_sysr@   r(   r(   r*   r   ~   s   
/
r   c                 G   sJ   t | ts	tdtj}|D ]}t |ttfr||| 7 }qtd|S )a  Kinetic energy of a multibody system.

    Explanation
    ===========

    This function returns the kinetic energy of a system of Particle's and/or
    RigidBody's. The kinetic energy of such a system is equal to the sum of
    the kinetic energies of its constituents. Consider a system, S, comprising
    a rigid body, A, and a particle, P. The kinetic energy of the system, T,
    is equal to the vector sum of the kinetic energy of the particle, T1, and
    the kinetic energy of the rigid body, T2, i.e.

    T = T1 + T2

    Kinetic energy is a scalar.

    Parameters
    ==========

    frame : ReferenceFrame
        The frame in which the velocity or angular velocity of the body is
        defined.
    body1, body2, body3... : Particle and/or RigidBody
        The body (or bodies) whose kinetic energy is required.

    Examples
    ========

    >>> from sympy.physics.mechanics import Point, Particle, ReferenceFrame
    >>> from sympy.physics.mechanics import RigidBody, outer, kinetic_energy
    >>> N = ReferenceFrame('N')
    >>> O = Point('O')
    >>> O.set_vel(N, 0 * N.x)
    >>> P = O.locatenew('P', 1 * N.x)
    >>> P.set_vel(N, 10 * N.x)
    >>> Pa = Particle('Pa', P, 1)
    >>> Ac = O.locatenew('Ac', 2 * N.y)
    >>> Ac.set_vel(N, 5 * N.y)
    >>> a = ReferenceFrame('a')
    >>> a.set_ang_vel(N, 10 * N.z)
    >>> I = outer(N.z, N.z)
    >>> A = RigidBody('A', Ac, a, 20, (I, Ac))
    >>> kinetic_energy(N, Pa, A)
    350

    rA   r;   )r<   r   r=   r   Zeror   r   r   )r1   r>   ke_sysr@   r(   r(   r*   r      s   
0r   c                  G   s4   t j}| D ]}t|ttfr||j7 }qtd|S )a  Potential energy of a multibody system.

    Explanation
    ===========

    This function returns the potential energy of a system of Particle's and/or
    RigidBody's. The potential energy of such a system is equal to the sum of
    the potential energy of its constituents. Consider a system, S, comprising
    a rigid body, A, and a particle, P. The potential energy of the system, V,
    is equal to the vector sum of the potential energy of the particle, V1, and
    the potential energy of the rigid body, V2, i.e.

    V = V1 + V2

    Potential energy is a scalar.

    Parameters
    ==========

    body1, body2, body3... : Particle and/or RigidBody
        The body (or bodies) whose potential energy is required.

    Examples
    ========

    >>> from sympy.physics.mechanics import Point, Particle, ReferenceFrame
    >>> from sympy.physics.mechanics import RigidBody, outer, potential_energy
    >>> from sympy import symbols
    >>> M, m, g, h = symbols('M m g h')
    >>> N = ReferenceFrame('N')
    >>> O = Point('O')
    >>> O.set_vel(N, 0 * N.x)
    >>> P = O.locatenew('P', 1 * N.x)
    >>> Pa = Particle('Pa', P, m)
    >>> Ac = O.locatenew('Ac', 2 * N.y)
    >>> a = ReferenceFrame('a')
    >>> I = outer(N.z, N.z)
    >>> A = RigidBody('A', Ac, a, M, (I, Ac))
    >>> Pa.potential_energy = m * g * h
    >>> A.potential_energy = M * g * h
    >>> potential_energy(Pa, A)
    M*g*h + g*h*m

    r;   )r   rD   r<   r   r   r   r=   )r>   pe_sysr@   r(   r(   r*   r      s   .r   c                 G   s*   ddl m} tdddd || g|R  S )Nr   )gravityzn
        The gravity function has been moved.
        Import it from "sympy.physics.mechanics.loads".
        r+   r,   r-   )sympy.physics.mechanics.loadsrG   r   )accelerationbodies_gravityr(   r(   r*   rG   -  s   rG   c                 G   sb   |st dd}td}|D ]}||j7 }t|dd}|du r"|j}||j||  7 }q|| S )a  
    Returns the position vector from the given point to the center of mass
    of the given bodies(particles or rigidbodies).

    Example
    =======

    >>> from sympy import symbols, S
    >>> from sympy.physics.vector import Point
    >>> from sympy.physics.mechanics import Particle, ReferenceFrame, RigidBody, outer
    >>> from sympy.physics.mechanics.functions import center_of_mass
    >>> a = ReferenceFrame('a')
    >>> m = symbols('m', real=True)
    >>> p1 = Particle('p1', Point('p1_pt'), S(1))
    >>> p2 = Particle('p2', Point('p2_pt'), S(2))
    >>> p3 = Particle('p3', Point('p3_pt'), S(3))
    >>> p4 = Particle('p4', Point('p4_pt'), m)
    >>> b_f = ReferenceFrame('b_f')
    >>> b_cm = Point('b_cm')
    >>> mb = symbols('mb')
    >>> b = RigidBody('b', b_cm, b_f, mb, (outer(b_f.x, b_f.x), b_cm))
    >>> p2.point.set_pos(p1.point, a.x)
    >>> p3.point.set_pos(p1.point, a.x + a.y)
    >>> p4.point.set_pos(p1.point, a.y)
    >>> b.masscenter.set_pos(p1.point, a.y + a.z)
    >>> point_o=Point('o')
    >>> point_o.set_pos(p1.point, center_of_mass(p1.point, p1, p2, p3, p4, b))
    >>> expr = 5/(m + mb + 6)*a.x + (m + mb + 3)/(m + mb + 6)*a.y + mb/(m + mb + 6)*a.z
    >>> point_o.pos_from(p1.point)
    5/(m + mb + 6)*a.x + (m + mb + 3)/(m + mb + 6)*a.y + mb/(m + mb + 6)*a.z

    z:No bodies(instances of Particle or Rigidbody) were passed.r   
masscenterN)r=   r   r9   getattrrB   pos_from)rB   rJ   
total_massvecirL   r(   r(   r*   center_of_mass:  s   !
rR   c                 G   sJ   t | ts	td|D ]}t |ttfstdqt| g|R  t|  S )a  Lagrangian of a multibody system.

    Explanation
    ===========

    This function returns the Lagrangian of a system of Particle's and/or
    RigidBody's. The Lagrangian of such a system is equal to the difference
    between the kinetic energies and potential energies of its constituents. If
    T and V are the kinetic and potential energies of a system then it's
    Lagrangian, L, is defined as

    L = T - V

    The Lagrangian is a scalar.

    Parameters
    ==========

    frame : ReferenceFrame
        The frame in which the velocity or angular velocity of the body is
        defined to determine the kinetic energy.

    body1, body2, body3... : Particle and/or RigidBody
        The body (or bodies) whose Lagrangian is required.

    Examples
    ========

    >>> from sympy.physics.mechanics import Point, Particle, ReferenceFrame
    >>> from sympy.physics.mechanics import RigidBody, outer, Lagrangian
    >>> from sympy import symbols
    >>> M, m, g, h = symbols('M m g h')
    >>> N = ReferenceFrame('N')
    >>> O = Point('O')
    >>> O.set_vel(N, 0 * N.x)
    >>> P = O.locatenew('P', 1 * N.x)
    >>> P.set_vel(N, 10 * N.x)
    >>> Pa = Particle('Pa', P, 1)
    >>> Ac = O.locatenew('Ac', 2 * N.y)
    >>> Ac.set_vel(N, 5 * N.y)
    >>> a = ReferenceFrame('a')
    >>> a.set_ang_vel(N, 10 * N.z)
    >>> I = outer(N.z, N.z)
    >>> A = RigidBody('A', Ac, a, 20, (I, Ac))
    >>> Pa.potential_energy = m * g * h
    >>> A.potential_energy = M * g * h
    >>> Lagrangian(N, Pa, A)
    -M*g*h - g*h*m + 350

    z$Please supply a valid ReferenceFramer;   )r<   r   r=   r   r   r   r   )r1   r>   r@   r(   r(   r*   r    k  s   
4r    Nc                    sr   t jh |rt|rt|}ntdt }t| tr*|du r%td| | |}  fdd| 	t
tD | S )a  Find all dynamicsymbols in expression.

    Explanation
    ===========

    If the optional ``exclude`` kwarg is used, only dynamicsymbols
    not in the iterable ``exclude`` are returned.
    If we intend to apply this function on a vector, the optional
    ``reference_frame`` is also used to inform about the corresponding frame
    with respect to which the dynamic symbols of the given vector is to be
    determined.

    Parameters
    ==========

    expression : SymPy expression

    exclude : iterable of dynamicsymbols, optional

    reference_frame : ReferenceFrame, optional
        The frame with respect to which the dynamic symbols of the
        given vector is to be determined.

    Examples
    ========

    >>> from sympy.physics.mechanics import dynamicsymbols, find_dynamicsymbols
    >>> from sympy.physics.mechanics import ReferenceFrame
    >>> x, y = dynamicsymbols('x, y')
    >>> expr = x + x.diff()*y
    >>> find_dynamicsymbols(expr)
    {x(t), y(t), Derivative(x(t), t)}
    >>> find_dynamicsymbols(expr, exclude=[x, y])
    {Derivative(x(t), t)}
    >>> a, b, c = dynamicsymbols('a, b, c')
    >>> A = ReferenceFrame('A')
    >>> v = a * A.x + b * A.y + c * A.z
    >>> find_dynamicsymbols(v, reference_frame=A)
    {a(t), b(t), c(t)}

    zexclude kwarg must be iterableNzJYou must provide reference_frame when passing a vector expression, got %s.c                    s   h | ]	}|j  kr|qS r(   )free_symbols).0rQ   t_setr(   r*   	<setcomp>  s    
z&find_dynamicsymbols.<locals>.<setcomp>)r   _tr   setr=   r<   r   
ValueError	to_matrixatomsr   r   )
expressionexcludereference_frameexclude_setr(   rU   r*   r'     s   *


r'   F)smartc                   s\   t | |r	t nt| dr| S dd  t| tttfr)|  fddS  | S )a%  A custom subs for use on expressions derived in physics.mechanics.

    Traverses the expression tree once, performing the subs found in sub_dicts.
    Terms inside ``Derivative`` expressions are ignored:

    Examples
    ========

    >>> from sympy.physics.mechanics import dynamicsymbols, msubs
    >>> x = dynamicsymbols('x')
    >>> msubs(x.diff() + x, {x: 1})
    Derivative(x(t), t) + 1

    Note that sub_dicts can be a single dictionary, or several dictionaries:

    >>> x, y, z = dynamicsymbols('x, y, z')
    >>> sub1 = {x: 1, y: 2}
    >>> sub2 = {z: 3, x.diff(): 4}
    >>> msubs(x.diff() + x + y + z, sub1, sub2)
    10

    If smart=True (default False), also checks for conditions that may result
    in ``nan``, but if simplified would yield a valid expression. For example:

    >>> from sympy import sin, tan
    >>> (sin(x)/tan(x)).subs(x, 0)
    nan
    >>> msubs(sin(x)/tan(x), {x: 0}, smart=True)
    1

    It does this by first replacing all ``tan`` with ``sin/cos``. Then each
    node is traversed. If the node is a fraction, subs is first evaluated on
    the denominator. If this results in 0, simplification of the entire
    fraction is attempted. Using this selective simplification, only
    subexpressions that result in 1/0 are targeted, resulting in faster
    performance.

    r&   c                 S   s   t | t|S N)_crawl	_sub_funcexprsub_dictr(   r(   r*   <lambda>  s    zmsubs.<locals>.<lambda>c                    s
    | S rb   r(   )xfuncrg   r(   r*   rh     s   
 )	r   _smart_subshasattrr&   r<   r   r   r   	applyfunc)rf   ra   	sub_dictsr)   r(   rj   r*   r&     s   (


r&   c                    sD   | g R i }|dur|S  fdd| j D }| j| S )z8Crawl the expression tree, and apply func to every node.Nc                 3   s(    | ]}t |g R i V  qd S rb   )rc   rT   argargsrk   r)   r(   r*   	<genexpr>  s   & z_crawl.<locals>.<genexpr>)rs   rk   )rf   rk   rs   r)   valnew_argsr(   rr   r*   rc     s
   
rc   c                 C   s$   | |v r||  S | j r| jr| S dS )z;Perform direct matching substitution, ignoring derivatives.N)rs   is_Derivativere   r(   r(   r*   rd   !  s
   rd   c                 C   s2   t | trt| j t| j  S | jr| jr| S dS )zReplace tan with sin/cos.N)r<   r   r   rs   r   rw   )rf   r(   r(   r*   _tan_repl_func)  s
   
rx   c                    s    t | t}  fdd  | |S )a  Performs subs, checking for conditions that may result in `nan` or
    `oo`, and attempts to simplify them out.

    The expression tree is traversed twice, and the following steps are
    performed on each expression node:
    - First traverse:
        Replace all `tan` with `sin/cos`.
    - Second traverse:
        If node is a fraction, check if the denominator evaluates to 0.
        If so, attempt to simplify it out. Then if node is in sub_dict,
        sub in the corresponding value.

    c                    s|   t | \}}|dkr#| }| dkrt| } n	| }|| S t|  }|d ur.|S  fdd| jD }| j| S )N   r   c                 3   s    | ]} |V  qd S rb   r(   rp   )	_recurserrg   r(   r*   rt   S  s    z1_smart_subs.<locals>._recurser.<locals>.<genexpr>)_fraction_decompevalfr   rd   rs   rk   )rf   rg   numdendenom_subbed
num_subbedru   rv   rz   )rg   r*   rz   A  s   




z_smart_subs.<locals>._recurser)rc   rx   re   r(   r   r*   rl   1  s   

rl   c                 C   sx   t | ts	| dfS g }g }| jD ]}|jr$|jd dk r$|d|  q|| q|s0| dfS t| }t| }||fS )z)Return num, den such that expr = num/den.ry   r   )r<   r   rs   is_Powappend)rf   r}   r~   ar(   r(   r*   r{   X  s   

r{   c                    sD    fdd} sd\}}||fS dd }|t | \}}||fS )aI  Parses the provided forcelist composed of items
    of the form (obj, force).
    Returns a tuple containing:
        vel_list: The velocity (ang_vel for Frames, vel for Points) in
                the provided reference frame.
        f_list: The forces.

    Used internally in the KanesMethod and LagrangesMethod classes.

    c                  3   sV     D ]%} | \}}t |tr||fV  qt |tr%||fV  qtdd S )Nz<First entry in each forcelist pair must be a point or frame.)r<   r   
ang_vel_inr   velr=   )pairobjforcefl	ref_framer(   r*   
flist_iteru  s   

z"_f_list_parser.<locals>.flist_iter)r(   r(   c                 S   s   | d r
t t|  S ddgS )Nr   r(   )listzip)lr(   r(   r*   rh     s    z _f_list_parser.<locals>.<lambda>)r   )r   r   r   vel_listf_listunzipr(   r   r*   _f_list_parserj  s   r   Tc                    s  t jh}| du rg } nt| s| g} |du rg }nt|s |g}|du r'g }nt|s.|g}g }|rt   fdd| D }t   fdd|D }t   fdd|D }	t| |}
t| ||}|rr|d| d |r}|d| d	 |	r|d
|	 d |
r||
 d |r|d
| d |r| D ]}t|tt	fr|j
|ks|d| d q|D ]}t|tt	fr|j
|ks|d| d q|D ]}t|tt	fr|j
|ks|d| d q|rtd|dS )a  Validate the generalized coordinates and generalized speeds.

    Parameters
    ==========
    coordinates : iterable, optional
        Generalized coordinates to be validated.
    speeds : iterable, optional
        Generalized speeds to be validated.
    check_duplicates : bool, optional
        Checks if there are duplicates in the generalized coordinates and
        generalized speeds. If so it will raise a ValueError. The default is
        True.
    is_dynamicsymbols : iterable, optional
        Checks if all the generalized coordinates and generalized speeds are
        dynamicsymbols. If any is not a dynamicsymbol, a ValueError will be
        raised. The default is True.
    u_auxiliary : iterable, optional
        Auxiliary generalized speeds to be validated.

    Nc                    "   h | ]}| v s  |r|qS r(   addrT   ri   seenr(   r*   rW        " z(_validate_coordinates.<locals>.<setcomp>c                    r   r(   r   r   r   r(   r*   rW     r   c                    r   r(   r   r   r   r(   r*   rW     r   zThe generalized coordinates z> are duplicated, all generalized coordinates should be unique.zThe generalized speeds z9 are duplicated, all generalized speeds should be unique.zThe auxiliary speeds z7 are duplicated, all auxiliary speeds should be unique.zD are defined as both generalized coordinates and generalized speeds.zC are also defined as generalized coordinates or generalized speeds.zGeneralized coordinate "z" is not a dynamicsymbol.zGeneralized speed "zAuxiliary speed "
)r   rX   r   rY   intersectionunionr   r<   r   r   rS   rZ   join)coordinatesspeedscheck_duplicatesis_dynamicsymbolsu_auxiliaryrV   msgscoord_duplicatesspeed_duplicatesaux_duplicatesoverlap_coordsoverlap_aux
coordinatespeedauxr(   r   r*   _validate_coordinates  sp   




r   c                    s   t  r S  fddS )z6Helper function to retrieve a specified linear solver.c                    s   t j| | dS )N)method)r   solve)Ablinear_solverr(   r*   rh     s    z&_parse_linear_solver.<locals>.<lambda>)callabler   r(   r   r*   _parse_linear_solver  s   r   )r   r   r   )NN)NNTTN)?sympy.utilitiesr   sympy.utilities.iterablesr   sympy.physics.vectorr   r   r   r   r   sympy.physics.vector.printingr	   r
   r   r   r    sympy.physics.mechanics.particler   !sympy.physics.mechanics.rigidbodyr   sympy.simplify.simplifyr   sympyr   r   r   r   r   r   r   sympy.core.functionr   sympy.physics.mechanics.inertiar   r0   r   r8   sympy.utilities.exceptionsr   __all__r"   r#   r$   r%   r!   __doc__r   r   r   r   rG   rR   r    r'   r&   rc   rd   rx   rl   r{   r   r   r   r(   r(   r(   r*   <module>   sN    $
5=;71
<<5	'
S