How to solve homogeneous linear equations with NumPy?

前端 未结 2 1165
花落未央
花落未央 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

    You can use an SVD or a QR decomposition to compute the null space of the linear system, e.g., something like:

    import numpy
    
    def null(A, eps=1e-15):
        u, s, vh = numpy.linalg.svd(A)
        null_space = numpy.compress(s <= eps, vh, axis=0)
        return null_space.T
    

    This yields for your example:

    >>> A
    matrix([[-0.75,  0.25,  0.25,  0.25],
            [ 1.  , -1.  ,  0.  ,  0.  ],
            [ 1.  ,  0.  , -1.  ,  0.  ],
            [ 1.  ,  0.  ,  0.  , -1.  ]])
    
    >>> null(A).T
    array([[-0.5, -0.5, -0.5, -0.5]])
    
    >>> (A*null(A)).T
    matrix([[ 1.66533454e-16, -1.66533454e-16, -2.22044605e-16, -2.22044605e-16]])
    

    See also the section Numerical computation of null space on Wikipedia.

提交回复
热议问题