K ifbddlmZddlmZddlmZmZmZmZm Z ddl m Z m Z m Z mZmZddlmZddlmZddlmZddlmZmZmZmZmZmZmZdd lmZdd l m!Z"m#Z$dd l%m&Z&gd Z'e Z(e Z)e Z*eZ+d Z,ejZe,_-d$dZ!dZ#dZ.dZ/dZ0dZ1dZ2dZ3dZ4d%dZ5dddZ6dZ7dZ8dZ9dZ:d Z;d!Z< d&d"Z=d#Z>y)') dict_merge)iterable)DyadicVectorReferenceFramePointdynamicsymbols)vprintvsprintvpprintvlatexinit_vprinting)Particle) RigidBody)simplify)MatrixMul DerivativesincostanS) AppliedUndef)inertiainertia_of_point_mass)sympy_deprecation_warning) linear_momentumangular_momentumkinetic_energypotential_energy Lagrangianmechanics_printingmprintmsprintmpprintmlatexmsubsfind_dynamicsymbolsc tdi|y)z] Initializes time derivative printing for all SymPy objects in mechanics module. N)r)kwargss g/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/physics/mechanics/functions.pyr"r"&s Vc @tdddt|||||||S)Nzh The inertia function has been moved. Import it from "sympy.physics.mechanics". 1.13moved-mechanics-functionsdeprecated_since_versionactive_deprecations_target)r_inertia)frameixxiyyizzixyiyzizxs r,rr1s0 "(#>  E3S#sC 88r-c8tdddt|||S)Nzv The inertia_of_point_mass function has been moved. Import it from "sympy.physics.mechanics". r/r0r1)r_inertia_of_point_mass)masspos_vecr5s r,rr=s( "(#>  "$ 77r-ct|ts tdtd}|D]7}t|tt fr||j |z }.td|S)aLinear 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 TypeErrorrrrr)r5bodylinear_momentum_syses r,rrIsoR e^ ,?@@$Qi NA!i23#q'8'8'??# LMM  N r-ct|ts tdt|ts tdt d}|D]8}t|t t fr||j||z }/td|S)aAngular 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 ReferenceFramezPlease specify a valid PointrrA)rBrrCrrrrr)pointr5rDangular_momentum_sysrFs r,rr~s^ e^ ,=>> eU #677%ay NA!i23$(:(:5%(HH$ LMM  N r-ct|ts tdtj}|D]7}t|t t fr||j|z }.td|S)aKinetic 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 rHrA)rBrrCrZerorrr)r5rDke_sysrFs r,rrsl` e^ ,=>> VVF J a)X. / a&&u- -FHI I J Mr-ctj}|D]2}t|ttfr||j z })t d|S)aPotential 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 rA)rrLrBrrr rC)rDpe_sysrFs r,r r sQ\VVF J a)X. / a(( (FHI I J Mr-c<ddlm}tddd||g|S)Nr)gravityzn The gravity function has been moved. Import it from "sympy.physics.mechanics.loads". r/r0r1)sympy.physics.mechanics.loadsrQr) accelerationbodies_gravitys r,rQrQ-s,A "(#>  L *6 **r-c|s tdd}td}|D]M}||jz }t|dd}| |j}||j|j |zz }O||z 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)rCrr>getattrrIpos_from)rIrT total_massvecirWs r,center_of_massr]:sB TUUJ )C 1aff Q d3  J qvvj))%000 1 z>r-ct|ts td|D]#}t|ttfrtdt |g|t |z S)aLagrangian 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 ReferenceFramerA)rBrrCrrrr )r5rDrFs r,r!r!ksdh e^ ,>?? J!i23HI IJ % '$ '*:D*A AAr-Nc`tjh}|r"t|r t|}nt dt}t |t r!|td|z|j|}|jttDchc]}|j|k(r|c}|z Scc}w)aFind 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)} zexclude kwarg must be iterablezJYou must provide reference_frame when passing a vector expression, got %s.) r _trsetrCrBr ValueError to_matrixatomsrr free_symbols) expressionexcludereference_framet_set exclude_setr\s r,r(r(sT   E G g,K<= =e *f%  ":J!'' jA %! NNe #  %'2 33 %s B+F)smartct||rtn t|dr|jSdt |t t tfr|jfdS|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$t|t|SN)_crawl _sub_funcexprsub_dicts r,zmsubs..sfT9h&Gr-c|Srnr*)xfuncrss r,rtzmsubs..sQ(9r-) r _smart_subshasattrr'rBrrr applyfunc)rrrk sub_dictsr+rwrss @@r,r'r'sdP9%H  w zz(##G$01~~9::D(##r-ct|gi}||Sfd|jD}|j|S)z8Crawl the expression tree, and apply func to every node.c3>K|]}t|giywrn)ro).0argargsrwr+s r, z_crawl..s"HssD24262Hs)rrw)rrrwrr+valnew_argss ``` r,roros@ t %d %f %C  HdiiHH 499h r-cJ||vr||S|jr |jr|Sy)z;Perform direct matching substitution, ignoring derivatives.N)r is_Derivativerqs r,rprp!s- x~ YY$,, -r-ct|tr%t|jt |jz S|jr |j r|Sy)zReplace tan with sin/cos.N)rBrrrrr)rrs r,_tan_repl_funcr)s@$DIIdii00 YY$,, -r-c@t|t}fd||S)aPerforms 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. ct|\}}|dk7r6|}|jdk(r t|}n|}||z St|}||Sfd|jD}|j |S)Nrc30K|] }|ywrnr*)r~r _recurserrss r,rz1_smart_subs.._recurser..SsBIc8,Bs)_fraction_decompevalfrrprrw) rrrsnumden denom_subbed num_subbedrrrs ` r,rz_smart_subs.._recurserAs#D)S !8$S(3L!!#q(~'sH5 !L00h' ?JB Btyy(##r-)ror)rrrsrs @r,rxrx1s% $ 'D$( T8 $$r-ct|ts|dfSg}g}|jD]F}|jr'|jddkr|j d|z 6|j |H|s|dfSt|}t|}||fS)z)Return num, den such that expr = num/den.rr)rBrris_Powappend)rrrras r,rrXs dC Qw C C YY 88q A JJq1u  JJqM  Qw s)C s)C 8Or-chfd}s d\}}||fSd}|t|\}}||fS)aIParses 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. c3KD]]}|\}}t|tr|j|f.t|tr|j |fTt dyw)Nz.flist_iterusm 8DJC#~.nnY/66C'ggi(%//!788 8sA$A')r*r*c6|drtt|SddgS)Nrr*)listzip)ls r,rtz _f_list_parser..s1Q4$sAw-b"Xr-)r)rrrvel_listf_listunzips`` r,_f_list_parserrjsL 8 !& V > jl!34& V r-c tjh}|g}nt|s|g}|g}nt|s|g}|g}nt|s|g}g}|rBt}|Dchc]}||vs|j |s|} }t}|Dchc]}||vs|j |s|} }t}|Dchc]}||vs|j |s|} }t|j |} t|j |j |} | r|jd| d| r|jd| d| r|jd| d| r|j| d| r|jd| d |r|D]=}t|ttfr|j|k(r)|jd |d ?|D]=}t|ttfr|j|k(r)|jd |d ?|D]=}t|ttfr|j|k(r)|jd |d ?|rtdj|ycc}wcc}wcc}w)aValidate 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. NzThe generalized coordinates z> are duplicated, all generalized coordinates should be unique.zThe generalized speeds z9 are duplicated, all generalized speeds should be unique.zThe 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.zGeneralized coordinate "z" is not a dynamicsymbol.zGeneralized speed "zAuxiliary speed " )r r`rraadd intersectionunionrrBrrrerbjoin) coordinatesspeedscheck_duplicatesis_dynamicsymbols u_auxiliaryrimsgsseenrvcoord_duplicatesspeed_duplicatesaux_duplicatesoverlap_coords overlap_aux coordinatespeedauxs r,_validate_coordinatesrs,   E k ""m  ~ f  k ""m Du'2O!a4i488A;AOOu'-J!ddhhqkAJJu%0MAI!!MM[)66v>+&,,V4AA+N  KK67G6HI"# $  KK12B1CDOP Q  KK//?@BC D  KK>*+>? @  KK/ }=LM N% /JzL*+EF++u4 6zlC-./ /  LEu|Z&@A&&%/ )%0IJL L  HCs\:$>?$$- 'u,EFH H  4)) OPJMs$I79I7 I<(I<<JJc(trSfdS)z6Helper function to retrieve a specified linear solver.c4tj||S)N)method)rsolve)Ab linear_solvers r,rtz&_parse_linear_solver..s Q-@r-)callable)rs`r,_parse_linear_solverrs  @@r-)rrr)NN)NNTTN)?sympy.utilitiesrsympy.utilities.iterablesrsympy.physics.vectorrrrrr sympy.physics.vector.printingr r r r r sympy.physics.mechanics.particler!sympy.physics.mechanics.rigidbodyrsympy.simplify.simplifyrsympyrrrrrrrsympy.core.functionrsympy.physics.mechanics.inertiarr4rr=sympy.utilities.exceptionsr__all__r#r$r%r&r"__doc__rrrr rQr]r!r(r'rorprrxrrrrr*r-r,rs&.99;;57,;;;,5@ "    ,33 9 82j: z8v4n +.b9Bx93x#(2$j $%N$<KO>BP*fAr-