How to solve homogeneous linear equations with NumPy?

前端 未结 2 1171
花落未央
花落未央 2021-02-06 05:35

If I have homogeneous linear equations like this

array([[-0.75,  0.25,  0.25,  0.25],
       [ 1.  , -1.  ,  0.  ,  0.  ],
       [ 1.  ,  0.  , -1.  ,  0.  ],
          


        
2条回答
  •  长发绾君心
    2021-02-06 06:22

    For that matter, the best solution of an over constrained homogeneous linear system is the eigenvector associated with the smallest eigenvalue. So given U as the coefficient matrix of the system, the solution is:

    import numpy as np
    
    def solution(U):
        # find the eigenvalues and eigenvector of U(transpose).U
        e_vals, e_vecs = np.linalg.eig(np.dot(U.T, U))  
        # extract the eigenvector (column) associated with the minimum eigenvalue
        return e_vecs[:, np.argmin(e_vals)] 
    

提交回复
热议问题