尝试计算精确解时numpy.linalg.solve中的错误

时间:2015-03-07 15:11:48

标签: python numpy linear-algebra matrix-inverse

我正在尝试计算A ^ -1 * x,我正在使用命令:

Solution = numpy.linalg.solve(A, x)

A是:ndarray 2000x2000:4000000 elems,类型float64,32000000字节(30 Mb)

x是:ndarray 2000:2000 elems,类型float64,16000字节

我得到的错误如下:

----> 1 Solution = np.linalg.solve(A, x)

/Users/glazar0/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/linalg/linalg.pyc in solve(a, b)
    379     signature = 'DD->D' if isComplexType(t) else 'dd->d'
    380     extobj = get_linalg_error_extobj(_raise_linalgerror_singular)
--> 381     r = gufunc(a, b, signature=signature, extobj=extobj)
    382 
    383     return wrap(r.astype(result_t))

/Users/glazar0/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/linalg/linalg.pyc in _raise_linalgerror_singular(err, flag)
     88 
     89 def _raise_linalgerror_singular(err, flag):
---> 90     raise LinAlgError("Singular matrix")
     91 
     92 def _raise_linalgerror_nonposdef(err, flag):

LinAlgError: Singular matrix 

1 个答案:

答案 0 :(得分:1)

如果A确实是单数(即不可逆),则b中的Ab = x没有唯一解。但是,您仍然可以使用np.linalg.lstsq在最小二乘意义上解决b

b, residuals, rank, singular_vals = np.linalg.lstsq(A, x)