[Numpy-discussion] "numpy.linalg.linalg.LinAlgError: Singular matrix" using "numpy.linalg.solve"

josef.pktd@gmai... josef.pktd@gmai...
Tue Nov 16 09:11:16 CST 2010


On Tue, Nov 16, 2010 at 8:13 AM, SoTaNeZ <sotanez@gmail.com> wrote:
> Hi all.
>
> I got this exception while executin numpy.linalg.solve(a,b) being:
>
> a = array([[  1.00000000e+000,  -4.19430400e+006,   0.00000000e+000,
>                   0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
>                   0.00000000e+000,   0.00000000e+000,   0.00000000e+000],
>                [  1.00000000e+000,   0.00000000e+000,  -2.47845883e-119,
>                   0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
>                   0.00000000e+000,   0.00000000e+000,   0.00000000e+000],
>                [  1.00000000e+000,   0.00000000e+000,   0.00000000e+000,
>                  -2.68435456e+008,   0.00000000e+000,   0.00000000e+000,
>                   0.00000000e+000,   0.00000000e+000,   0.00000000e+000],
>                [  1.00000000e+000,   0.00000000e+000,   0.00000000e+000,
>                   0.00000000e+000,  -3.38813179e-021,   0.00000000e+000,
>                   0.00000000e+000,   0.00000000e+000,   0.00000000e+000],
>                [  1.00000000e+000,   0.00000000e+000,   0.00000000e+000,
>                   0.00000000e+000,   0.00000000e+000,  -4.09600000e+003,
>                   0.00000000e+000,   0.00000000e+000,   0.00000000e+000],
>                [  1.00000000e+000,   0.00000000e+000,   0.00000000e+000,
>                   0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
>                  -2.56000000e+002,   0.00000000e+000,   0.00000000e+000],
>                [  1.00000000e+000,   0.00000000e+000,   0.00000000e+000,
>                   0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
>                   0.00000000e+000,  -7.20575940e+016,   0.00000000e+000],
>                [  1.00000000e+000,   0.00000000e+000,   0.00000000e+000,
>                   0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
>                   0.00000000e+000,   0.00000000e+000,  -1.56250000e-002],
>                [  1.00000000e+000,   1.00000000e+000,   1.00000000e+000,
>                   1.00000000e+000,   1.00000000e+000,   1.00000000e+000,
>                   1.00000000e+000,   1.00000000e+000,   1.00000000e+000]])
>
> b = array([0, 0, 0, 0, 0, 0, 0, 0, 1])
>
> I guess some numbers in a are too big or too small, aren't they?

some eigenvalues are very close to zero

>>> np.linalg.eigh(a)[0]
array([ -2.87298335e+00,  -1.60293723e-16,  -3.43582743e-17,
        -1.24477525e-32,  -1.84567471e-33,   3.95359079e-33,
         8.81541828e-18,   4.09615908e-16,   4.87298335e+00])

if you need to get a robust solution, you could use lstsq, but whether
the solution makes sense will depend on your application (I guess it
should be equivalent to using the pinv)

>>> np.linalg.lstsq(a,b)
(array([ -5.91579259e-08,   2.21469552e-14,   2.31028827e-10,
        -2.06502900e-16,   2.31028892e-10,   5.95746983e-08,
         1.51994027e-05,  -8.20980166e-25,   2.31042993e-10]),
array([], dtype=float64), 5, array([  7.20575940e+16,
2.68435456e+08,   4.19430411e+06,
         4.09600024e+03,   2.56003891e+02,   2.30353618e+00,
         1.30462749e+00,   1.03982867e-02,   1.95397635e-10]))

Josef

>
> Thanks,
> David.
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>


More information about the NumPy-Discussion mailing list