K iZCdgZddlmZmZmZddlmZddlmZddl m Z ddl m Z m Z ddlmZddlmZGd dZd Zy ) Linearizer)Matrixeyezeros)Dummy)flatten)dynamicsymbols)msubs_parse_linear_solver) namedtuple)Iterablec>eZdZdZ d dZdZdZdZdZd dZ y) raIThis object holds the general model form for a dynamic system. This model is used for computing the linearized form of the system, while properly dealing with constraints leading to dependent coordinates and speeds. The notation and method is described in [1]_. Attributes ========== f_0, f_1, f_2, f_3, f_4, f_c, f_v, f_a : Matrix Matrices holding the general system form. q, u, r : Matrix Matrices holding the generalized coordinates, speeds, and input vectors. q_i, u_i : Matrix Matrices of the independent generalized coordinates and speeds. q_d, u_d : Matrix Matrices of the dependent generalized coordinates and speeds. perm_mat : Matrix Permutation matrix such that [q_ind, u_ind]^T = perm_mat*[q, u]^T References ========== .. [1] D. L. Peterson, G. Gede, and M. Hubbard, "Symbolic linearization of equations of motion of constrained multibody systems," Multibody Syst Dyn, vol. 33, no. 2, pp. 143-161, Feb. 2015, doi: 10.1007/s11044-014-9436-5. Ncft||_t||_t||_t||_t||_t||_t||_t||_ t||_ t| |_ t| |_ d}|| |_ || |_|| |_|||_|||_|||_|jj't(j*|_|jj't(j*|_t1|j,j3|j}t|j,Dcgc]}||vr|n t5c}|_t9|j}t9|j}t9|j}t9|j}t9|j"}t9|j$}t;dgd}||||||||_d|_d|_ d|_!d|_"d|_#d|_$d|_%d|_&d|_'d|_(d|_)ycc}w)a Parameters ========== f_0, f_1, f_2, f_3, f_4, f_c, f_v, f_a : array_like System of equations holding the general system form. Supply empty array or Matrix if the parameter does not exist. q : array_like The generalized coordinates. u : array_like The generalized speeds q_i, u_i : array_like, optional The independent generalized coordinates and speeds. q_d, u_d : array_like, optional The dependent generalized coordinates and speeds. r : array_like, optional The input variables. lams : array_like, optional The lagrange multipliers linear_solver : str, callable Method used to solve the several symbolic linear systems of the form ``A*x=b`` in the linearization process. If a string is supplied, it should be a valid method that can be used with the :meth:`sympy.matrices.matrixbase.MatrixBase.solve`. If a callable is supplied, it should have the format ``x = f(A, b)``, where it solves the equations and returns the solution. The default is ``'LU'`` which corresponds to SymPy's ``A.LUsolve(b)``. ``LUsolve()`` is fast to compute but will often result in divide-by-zero and thus ``nan`` results. c0|r t|StS)N)r)xs g/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/physics/mechanics/linearize.pyz%Linearizer.__init__..^saVXdims)lmnoskNF)*r linear_solverrf_0f_1f_2f_3f_4f_cf_vf_aquq_iq_du_iu_drlamsdiffr _t_qd_udset intersectionr_qd_duplenr _dims_Pq_Pqi_Pqd_Pu_Pui_Pud_C_0_C_1_C_2perm_mat _setup_done)selfrrrr r!r"r#r$r%r&r'r(r)r*r+r,r none_handlerdup_varsvarrrrrrrrs r__init__zLinearizer.__init__,sF2-@#;#;#;#;#;#;#;#;= $$$$a & 66;;~00166;;~001txx=--dff5"&((,3c&9suwF,-  M M K K K  N&"@A!Q1a+          1,sJ.cr|j|j|jd|_y)NT)_form_permutation_matrices_form_block_matrices_form_coefficient_matricesr@)rAs r_setupzLinearizer._setups0 '') !!# '')rc |j\}}}}}}|dk7rt|jt|j|j g|_|dkDr7|j ddd| f|_|j dd| df|_n |j |_t|_|dk7rt|jt|j|jg|_ |dkDr7|jddd| f|_ |jdd| df|_n |j|_ t|_t|jt||z||z g}tt|||z |jt|||z g}|r!|r|j!||_y||_y||_y)z(Form the permutation matrices Pq and Pu.rN)r5permutation_matrixr%rr'r(r6r7r8r&r)r*r9r:r;rrow_joinr?) rArrrrrrP_col1P_col2s rrGz%Linearizer._form_permutation_matricessy ::1aAq 6)$&&&$((DHH9M2NODH1u HHQ!V,  HHQV,  HH "H 6)$&&&$((DHH9M2NODH1u HHQ!V,  HHQV,  HH "H E!a%Q$789q!a%$))U1a!e_EF  & 7 & "DMrc&|j\}}}}}}|dkDrp|jj|j}t ||j |j ||j z|zz |jz|_nt ||_|dkDr|jj|j}||jz} |dk7rK|jj|j} |j |j | | z|_ nt|||_ t ||j|j | |zz |jz|_yt|||_ t ||_y)z0Form the coefficient matrices C_0, C_1, and C_2.rN)r5r"jacobianr%rr8rr7r<r#r&r;r=rr:r>) rArrrrrr f_c_jac_q f_v_jac_utemp f_v_jac_qs rrIz%Linearizer._form_coefficient_matricessX ::1aAq q5))$&&1IQ$))++Idii,?,57#778< BDIADI q5))$&&1Ityy(DAv HH--dff5 !YYJ););D))LL !!QK Q$))++D)<#==>BiiHDIa DIADIrc@|j\}}}}}}|dk7rc|jj|j|_|j|j zj|j  |_nt|_t|_|dk7r[|dk7rV|jj|j|_ |jj|j  |_ nt|_ t|_ |dk7r{||z |zdk7rp|jj|j|_|j|jz|j zj|j  |_nt|_t|_|dk7r[|dk7rV|jj|j$|_|jj|j( |_nt|_t|_|dk7rn||z |zdk7rc|jj|j$|_|j|jzj|j( |_nt|_t|_|dk7r1|dk7r,|j j|j( |_nt|_|dk7r6||z |zdk7r+|j j|j2|_nt|_|dk7r7||z |zdk7r,|jj|j6 |_yt|_y)z2Form the block matrices for composing M, A, and B.rN)r5rrQr/_M_qqrr%_A_qqrr$r3_M_uqc_A_uqcr _M_uqdrr!_A_uqdr0_M_uucr&_A_uuc_M_uud_A_uud_A_qur,_M_uldr+_B_u)rArrrrrrs rrHzLinearizer._form_block_matricess ::1aAq 6**4884DJ88dhh.88@@DJDJDJ 6a1f((++DLL9DK88,,TVV44DK (DK (DK 6a!eai1n((++DLL9DK HHtxx/$((:DDTVVLLDK (DK (DK 6a1f((++DHH5DK88,,TVV44DK (DK (DK 6a!eai1n((++DHH5DK HHtxx/99$&&AADK (DK (DK 6a1f((++DFF33DJDJ 6a!eai1n((++DII6DK (DK 6a!eai1n**46622DIDIrc|js|jt|tr|}n-t|tri}|D]}|j |ni}|j \}}}} } } |j} |j} |j}|j}|j}|j}|j}|j}|j}|j }|j"}|j$}|j&}|j(}|j*}|j,}| dk7rt/t1|| ||g}| dk7rt/t1||z| |g}|dk7rSt/| | |g}| dk7r&| dk7r!|j3j3}n3| dk7r|j3}n|}n| dk7rj3}n}t5||} |dk7rq|}!| dk7r|!||zz }!|!|z}!|dk7r|}"| dk7r|"||zz }"|"|z}"n t/}"| |z | zdk7r|}#| dk7r|#||zz }#|#|z}#n t/}#t/|!|"|#g}n t/}| dk7rT|dk7r||z}$n t/}$|dk7r||z}%n t/}%| |z | zdk7r||z}&n t/}&t/|$|%|&g}n t/}|r|r|j3|}'n|}'n|}'t5|'|}(| dk7r6| |z | zdk7r+t1||z| j7|})t5|)|}*n t/}*|r}|j8j:|j=| |(z}+|*r*|j8j:|j=| |*z},n|*},|r |+j?|,j?|+|,fS|r0| j?|(j?|*j?| |(|*fS)a!Linearize the system about the operating point. Note that q_op, u_op, qd_op, ud_op must satisfy the equations of motion. These may be either symbolic or numeric. Parameters ========== op_point : dict or iterable of dicts, optional Dictionary or iterable of dictionaries containing the operating point conditions for all or a subset of the generalized coordinates, generalized speeds, and time derivatives of the generalized speeds. These will be substituted into the linearized system before the linearization is complete. Leave set to ``None`` if you want the operating point to be an arbitrary set of symbols. Note that any reduction in symbols (whether substituted for numbers or expressions with a common parameter) will result in faster runtime. A_and_B : bool, optional If A_and_B=False (default), (M, A, B) is returned and of A_and_B=True, (A, B) is returned. See below. simplify : bool, optional Determines if returned values are simplified before return. For large expressions this may be time consuming. Default is False. Returns ======= M, A, B : Matrices, ``A_and_B=False`` Matrices from the implicit form: ``[M]*[q', u']^T = [A]*[q_ind, u_ind]^T + [B]*r`` A, B : Matrices, ``A_and_B=True`` Matrices from the explicit form: ``[q_ind', u_ind']^T = [A]*[q_ind, u_ind]^T + [B]*r`` Notes ===== Note that the process of solving with A_and_B=True is computationally intensive if there are many symbolic parameters. For this reason, it may be more desirable to use the default A_and_B=False, returning M, A, and B. More values may then be substituted in to these matrices later on. The state space form can then be found as A = P.T*M.LUsolve(A), B = P.T*M.LUsolve(B), where P = Linearizer.perm_mat. r) r@rJ isinstancedictr updater5rWrYr[r]r_rbrXrZr\rar^r`rcr<r=r>rrrMr col_joinr?Trsimplify)-rAop_pointA_and_Brj op_point_dictoprrrrrrM_qqM_uqcM_uqdM_uucM_uudM_uldA_qqA_uqcA_uqdA_quA_uucA_uudB_uC_0C_1C_2col2col3col1MM_eqr1c1r2c1r3c1r1c2r2c2r3c2AmatAmat_eqBmatBmat_eqA_contB_conts- r linearizezLinearizer.linearizes\ KKM h %$M ( +M )$$R( )M ::1aAqzz     zz  zz  iiiiiiii 65A;u56D 65Q?E23D 64./DAv!q&MM$'006aMM$' !V d#AAQ & 6DAv$#:DAv6US[)Dczx1uqyA~6US[)Dczx4t,-D8D 6AvczxAvs{x1uqyA~s{x4t,-D8D }}T*Dm, 6a!eai1nQ?++C0DD-0GhG ]]__t'9'9$'HHF4+=+=dG+LL!!!6> !   "  "') )r)NNNNNNLU)NFF) __name__ __module__ __qualname____doc__rErJrGrIrHrrrrr s8<JN<@#Y!v #B@0!dx*rcpt|ttfs t|}t|ttfs t|}t |t |k7r t d|Dcgc]}|j |}}tt|}t|D] \}}d|||f<|Scc}w)akCompute the permutation matrix to change order of orig_vec into order of per_vec. Parameters ========== orig_vec : array_like Symbols in original ordering. per_vec : array_like Symbols in new ordering. Returns ======= p_matrix : Matrix Permutation matrix such that orig_vec == (p_matrix * per_vec). zKorig_vec and per_vec must be the same length, and contain the same symbols.) relisttuplerr1 ValueErrorindexrr4 enumerate)orig_vecper_veciind_listp_matrixjs rrLrLs$ hu .8$ ge} -'" 8}G $9: :+23aq!3H3S]#H(#1A O 4s)B3N)__all__sympyrrrsympy.core.symbolrsympy.utilities.iterablesrsympy.physics.vectorr !sympy.physics.mechanics.functionsr r collectionsr collections.abcr rrLrrrrs6 .$$#-/I"$m*m*` r