
    Ed<                         d gZ ddlmZmZmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ  G d	 d           Zd
 ZdS )
Linearizer    )MatrixeyezerosDummy)flatten)dynamicsymbols)msubs)
namedtuple)Iterablec                   <    e Zd ZdZ	 d
dZd Zd Zd Zd Zdd	Z	dS )r   a  This 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.

    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
    Nc                 |   t          |          | _        t          |          | _        t          |          | _        t          |          | _        t          |          | _        t          |          | _        t          |          | _        t          |          | _        t          |	          | _	        t          |
          | _
        d } ||          | _         ||          | _         ||          | _         ||          | _         ||          | _         ||          | _        | j	                            t$          j                  | _        | j
                            t$          j                  | _        t-          | j                                      | j
                  t          fd| j        D                       | _        t3          | j                  }t3          | j                  }t3          | j	                  }t3          | j
                  }t3          | j                  }t3          | j                  }t5          dg d          } |||||||          | _        d| _        d| _        d| _        d| _        d| _         d| _!        d| _"        d| _#        d| _$        d| _%        d| _&        dS )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
        c                 @    | rt          |           nt                      S )N)r   )xs    Alib/python3.11/site-packages/sympy/physics/mechanics/linearize.py<lambda>z%Linearizer.__init__.<locals>.<lambda>G   s    a!=VXX     c                 8    g | ]}|vr|nt                      S  r   ).0vardup_varss     r   
<listcomp>z'Linearizer.__init__.<locals>.<listcomp>V   s;     ! ! ! '*&9Fssuww ! ! !r   dims)lmnoskNF)'r   f_0f_1f_2f_3f_4f_cf_vf_aquq_iq_du_iu_drlamsdiffr
   _t_qd_udsetintersection_qd_duplenr   _dims_Pq_Pqi_Pqd_Pu_Pui_Pud_C_0_C_1_C_2perm_mat_setup_done)selfr"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   none_handlerr   r   r   r   r    r!   r   r   s                            @r   __init__zLinearizer.__init__"   sT   2 #;;#;;#;;#;;#;;#;;#;;#;; ==<$$<$$<$$<$$a L&&	 6;;~0116;;~011 tx==--df55 ! ! ! !x! ! ! " " MMMMKKKKKK	NN&"@"@"@AAT!Q1a++
							 r   c                     |                                   |                                  |                                  d| _        d S )NT)_form_permutation_matrices_form_block_matrices_form_coefficient_matricesrE   )rF   s    r   _setupzLinearizer._setupp   sH     	'')))!!###'')))r   c           	         | j         \  }}}}}}|dk    rt          | j        t          | j        | j        g                    | _        |dk    r3| j        ddd| f         | _        | j        dd| df         | _        n| j        | _        t                      | _        |dk    rt          | j	        t          | j
        | j        g                    | _        |dk    r3| j        ddd| f         | _        | j        dd| df         | _        n| j        | _        t                      | _        t          | j        t          ||z   ||z
            g          }t          t          |||z
            | j        t          |||z
            g          }|r'|r|                    |          | _        dS || _        dS || _        dS )z(Form the permutation matrices Pq and Pu.r   N)r:   permutation_matrixr*   r   r,   r-   r;   r<   r=   r+   r.   r/   r>   r?   r@   r   row_joinrD   )	rF   r   r   r   r   r    r!   P_col1P_col2s	            r   rJ   z%Linearizer._form_permutation_matricesx   s     :1aAq6 	%)$&&$(DH9M2N2NOODH1u % HQQQ!V,	 HQQQV,		 H	"HH	6 	%)$&&$(DH9M2N2NOODH1u % HQQQ!V,	 HQQQV,		 H	"HH	E!a%Q$7$7899q!a%$)U1a!e__EFF 	# ' & 7 7 &"DMMMr   c                    | j         \  }}}}}}|dk    rb| j                            | j                  }t	          |          | j        || j        z                      |          z  z
  | j        z  | _        nt	          |          | _        |dk    r| j	                            | j
                  }|| j        z  }	|dk    rC| j	                            | j                  }
| j         |	                    |
          z  | _        nt          ||          | _        t	          |          | j        |	                    |          z  z
  | j        z  | _        dS t          ||          | _        t	          |          | _        dS )z0Form the coefficient matrices C_0, C_1, and C_2.r   N)r:   r'   jacobianr*   r   r=   LUsolver<   rA   r(   r+   r@   rB   r   r?   rC   )rF   r   r   r   r   r    r!   	f_c_jac_q	f_v_jac_utemp	f_v_jac_qs              r   rL   z%Linearizer._form_coefficient_matrices   sg     :1aAq q5 	))$&11IQ$)yI0&wy11#2 259Y?DII ADI q5 	))$&11Ity(DAv ( H--df55	!YJi)@)@@		!!QKK	Q$)LL++#, ,/3y9DIII aDIADIIIr   c                    | j         \  }}}}}}|dk    rR| j                            | j                  | _        | j        | j        z                       | j                   | _        n&t                      | _        t                      | _        |dk    rP|dk    rJ| j	                            | j
                  | _        | j	                            | j                   | _        n&t                      | _        t                      | _        |dk    rf||z
  |z   dk    rZ| j                            | j
                  | _        | j        | j        z   | j        z                       | j                   | _        n&t                      | _        t                      | _        |dk    rP|dk    rJ| j	                            | j                  | _        | j	                            | j                   | _        n&t                      | _        t                      | _        |dk    r^||z
  |z   dk    rR| j                            | j                  | _        | j        | j        z                       | j                   | _        n&t                      | _        t                      | _        |dk    r,|dk    r&| j                            | j                   | _        nt                      | _        |dk    r1||z
  |z   dk    r%| j                            | j                  | _        nt                      | _        |dk    r3||z
  |z   dk    r'| j                            | j                   | _        dS t                      | _        dS )z2Form the block matrices for composing M, A, and B.r   N)r:   r"   rT   r4   _M_qqr#   r*   _A_qqr   r)   r8   _M_uqc_A_uqcr%   _M_uqdr$   r&   _A_uqdr5   _M_uucr+   _A_uuc_M_uud_A_uud_A_qur1   _M_uldr0   _B_u)rF   r   r   r   r   r    r!   s          r   rK   zLinearizer._form_block_matrices   s     :1aAq 6 	"**4844DJ8dh.88@@@DJJDJDJ6 	#a1f 	#(++DL99DK8,,TV444DKK ((DK ((DK6 	#a!eai1n 	#(++DL99DK Htx/$(:DDTVLLLDKK ((DK ((DK6 	#a1f 	#(++DH55DK8,,TV444DKK ((DK ((DK6 	#a!eai1n 	#(++DH55DK Htx/99$&AAADKK ((DK ((DK6 	"a1f 	"(++DF333DJJDJ6 	#a!eai1n 	#(++DI66DKK ((DK6 	!a!eai1n 	!**46222DIIIDIIIr   Fc                 B   | j         s|                                  t          |t                    r|}n4t          |t                    ri }|D ]}|                    |           ni }| j        \  }}}}	}
}| j        }| j        }| j	        }| j
        }| j        }| j        }| j        }| j        }| j        }| j        }| j        }| j        }| j        }| j        }| j        }| j        }|	dk    r t/          t1          ||	          ||g          }|dk    r"t/          t1          ||z   |          |g          }|dk    rft/          |||g          }|	dk    r/|dk    r)|                    |                              |          }n=|	dk    r|                    |          }n!|}n|dk    r|                    |          }n|}t5          ||          } |dk    r|}!|	dk    r|!||z  z  }!|!|z  }!|dk    r|}"|	dk    r|"||z  z  }"|"|z  }"nt/                      }"|	|z
  |z   dk    r|}#|	dk    r|#||z  z  }#|#|z  }#nt/                      }#t/          |!|"|#g          }nt/                      }|	dk    rg|dk    r||z  }$nt/                      }$|dk    r||z  }%nt/                      }%|	|z
  |z   dk    r||z  }&nt/                      }&t/          |$|%|&g          }nt/                      }|r|r|                    |          }'n|}'n|}'t5          |'|          }(|
dk    rC|	|z
  |z   dk    r7t1          ||z   |
                              |          })t5          |)|          }*nt/                      }*|rw| j        j        |                     |(          z  }+|*r#| j        j        |                     |*          z  },n|*},|r(|+                                 |,                                 |+|,fS |r<|                                  |(                                 |*                                 | |(|*fS )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. These will be substituted in to the linearized
            system before the linearization is complete. Leave blank if you
            want a completely symbolic form. 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 for forming
            [M]*[q, u]^T = [A]*[q_ind, u_ind]^T + [B]r. If A_and_B=True,
            (A, B) is returned for forming dx = [A]x + [B]r, where
            x = [q_ind, u_ind]^T.

        simplify : bool, optional
            Determines if returned values are simplified before return.
            For large expressions this may be time consuming. Default is False.

        Potential Issues
        ================

            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   ) rE   rM   
isinstancedictr   updater:   r[   r]   r_   ra   rc   rf   r\   r^   r`   re   rb   rd   rg   rA   rB   rC   r   r   rP   r   col_joinrD   TrU   simplify)-rF   op_pointA_and_Brn   op_point_dictopr   r   r   r   r    r!   M_qqM_uqcM_uqdM_uucM_uudM_uldA_qqA_uqcA_uqdA_quA_uucA_uudB_uC_0C_1C_2col2col3col1MM_eqr1c1r2c1r3c1r1c2r2c2r3c2AmatAmat_eqBmatBmat_eqA_contB_conts-                                                r   	linearizezLinearizer.linearize   s   N  	KKMMM h%% 	$MM(++ 	M ) )$$R(((() M  :1aAq zzziiii 6 	75A;;u566D6 	45Q??E233D6 	4.//DAv !q& MM$''0066a MM$''!V 	d##AAAQ&& 6 	DAv %$#:DAv  6 *US[)Dczxx1uqyA~  6 *US[)Dczxx4t,--DD88D6 	Av  czxxAv  s{xx1uqyA~  s{xx4t,--DD88D 	 }}T**Dm,,
 6 	a!eai1n 	Q??++C00DD-00GGhhG  	*]_t||G'<'<<F !4<<+@+@@ ! "!!!!!!6>!  #  """  """'))r   )NNNNNN)NFF)
__name__
__module____qualname____doc__rH   rM   rJ   rL   rK   r   r   r   r   r   r      s         , BFL! L! L! L!\     # # #B  >0! 0! 0!dq* q* q* q* q* q*r   c                     t           t          t          f          st                      t          |t          t          f          st          |          }t	                     t	          |          k    rt          d           fd|D             }t          t                               }t          |          D ]\  }}d|||f<   |S )ak  Compute 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.c                 :    g | ]}                     |          S r   )index)r   iorig_vecs     r   r   z&permutation_matrix.<locals>.<listcomp>  s%    333aq!!333r      )	ri   listtupler	   r6   
ValueErrorr   r9   	enumerate)r   per_vecind_listp_matrixr   js   `     r   rO   rO     s    $ hu.. %8$$ge}-- #'""
8}}G$ 1 0 1 1 	133337333HS]]##H(##  1AOr   N)__all__sympy.core.backendr   r   r   sympy.core.symbolr   sympy.utilities.iterablesr	   sympy.physics.vectorr
   !sympy.physics.mechanics.functionsr   collectionsr   collections.abcr   r   rO   r   r   r   <module>r      s   . 1 1 1 1 1 1 1 1 1 1 # # # # # # - - - - - - / / / / / / 3 3 3 3 3 3 " " " " " " $ $ $ $ $ $O* O* O* O* O* O* O* O*d    r   