Re: Error in linalg.inv ??
On 2009-06-06 18:08, Carl Banks wrote: On Jun 6, 3:31 pm, Robert Kern wrote: On 2009-06-06 17:09, John Machin wrote: Robert Kerngmail.com>writes: You have a very singular matrix (2*a[1] - a[0] == a[2]). You cannot invert it numerically and expect sensible results. Is raising an exception (as documented (http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.inv)) not a "sensible result"? That's not the way I was using the phrase, but you have a point. We depend on the underlying LAPACK routine to tell us if the array is singular or not. Perhaps the documentation should be updated to say "Raises LinAlgError if inv() detects that 'a' a singular matrix". Anyone with much experience with linear algebra libraries should know that these routines are shaky when ill-conditioned, but it could help newbies and computer science experts to be explicit about it. By all means, please do. http://docs.scipy.org/numpy/Front%20Page/ -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Error in linalg.inv ??
On Jun 6, 3:31 pm, Robert Kern wrote: > On 2009-06-06 17:09, John Machin wrote: > > Robert Kern gmail.com> writes: > >> You have a very singular matrix (2*a[1] - a[0] == a[2]). You cannot invert > >> it > >> numerically and expect sensible results. > > > Is raising an exception (as documented > > (http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.inv)) > > not > > a "sensible result"? > > That's not the way I was using the phrase, but you have a point. > > We depend on the underlying LAPACK routine to tell us if the array is singular > or not. Perhaps the documentation should be updated to say "Raises LinAlgError if inv() detects that 'a' a singular matrix". Anyone with much experience with linear algebra libraries should know that these routines are shaky when ill-conditioned, but it could help newbies and computer science experts to be explicit about it. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Error in linalg.inv ??
On 2009-06-06 17:09, John Machin wrote: Robert Kern gmail.com> writes: On 2009-06-06 00:34, Ajith Kumar wrote: from numpy import * Please ask numpy questions on the numpy mailing list, not here: http://www.scipy.org/Mailing_Lists a = arange(1.,10.) b = reshape(a, [3,3]) c = linalg.inv(b) And the result is [[ 3.15221191e+15 -6.30442381e+15 3.15221191e+15] [ -6.30442381e+15 1.26088476e+16 -6.30442381e+15] [ 3.15221191e+15 -6.30442381e+15 3.15221191e+15]] (gibberish) You have a very singular matrix (2*a[1] - a[0] == a[2]). You cannot invert it numerically and expect sensible results. Is raising an exception (as documented (http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.inv.html)) not a "sensible result"? That's not the way I was using the phrase, but you have a point. We depend on the underlying LAPACK routine to tell us if the array is singular or not. It will inform us if it stopped prematurely because of singularity. In this case, I think it happens to complete simply because it is so small and the subroutine did not detect the singularity. A (5,5) matrix constructed the same way will raise the exception, for example. However, the condition number of a matrix is not a binary property. A matrix can be poorly conditioned but not precisely singular, and the inversion routine will give you a correct result (within the limits of floating point arithmetic) rather than an exception, but you will still get mostly nonsense out if you try to use that result. You still need to apply care in interpreting the result and not rely on our throwing an exception even if we could catch all singular inputs. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Error in linalg.inv ??
Robert Kern gmail.com> writes: > > On 2009-06-06 00:34, Ajith Kumar wrote: > > from numpy import * > > Please ask numpy questions on the numpy mailing list, not here: > >http://www.scipy.org/Mailing_Lists > > > a = arange(1.,10.) > > b = reshape(a, [3,3]) > > c = linalg.inv(b) > > > > And the result is > > > > [[ 3.15221191e+15 -6.30442381e+15 3.15221191e+15] > > [ -6.30442381e+15 1.26088476e+16 -6.30442381e+15] > > [ 3.15221191e+15 -6.30442381e+15 3.15221191e+15]] (gibberish) > > You have a very singular matrix (2*a[1] - a[0] == a[2]). You cannot invert it > numerically and expect sensible results. Is raising an exception (as documented (http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.inv.html)) not a "sensible result"? -- http://mail.python.org/mailman/listinfo/python-list
Re: Error in linalg.inv ??
Ajith Kumar wrote: [[ 1. 2. 3.] [ 4. 5. 6.] [ 7. 8. 9.]] Another way to see that this is singular is notice or calculate that (1,2,3) - 2*(4,5,6) + (7,8,9) = (0,0,0) Same is true for the columns. -- http://mail.python.org/mailman/listinfo/python-list
Re: Error in linalg.inv ??
On 2009-06-06 00:34, Ajith Kumar wrote: Hello, I ran the following code (Using Debian 5.0) from numpy import * Please ask numpy questions on the numpy mailing list, not here: http://www.scipy.org/Mailing_Lists a = arange(1.,10.) b = reshape(a, [3,3]) c = linalg.inv(b) print b print c print dot(b,c) print dot(c,b) And the result is [[ 1. 2. 3.] [ 4. 5. 6.] [ 7. 8. 9.]] [[ 3.15221191e+15 -6.30442381e+15 3.15221191e+15] [ -6.30442381e+15 1.26088476e+16 -6.30442381e+15] [ 3.15221191e+15 -6.30442381e+15 3.15221191e+15]] [[-0.5 -1. -1. ] [-1. -2. 2. ] [-1.5 -3. 1. ]] [[ 5.5 8. 10.5] [ 3. 0. -3. ] [ -1. 0. -3. ]] NOT the identity matrix. Any help ? You have a very singular matrix (2*a[1] - a[0] == a[2]). You cannot invert it numerically and expect sensible results. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Error in linalg.inv ??
Ajith Kumar wrote: > I ran the following code (Using Debian 5.0) > > from numpy import * > a = arange(1.,10.) > b = reshape(a, [3,3]) > c = linalg.inv(b) > print b > print c > print dot(b,c) > print dot(c,b) > > And the result is > > [[ 1. 2. 3.] > [ 4. 5. 6.] > [ 7. 8. 9.]] > > [[ 3.15221191e+15 -6.30442381e+15 3.15221191e+15] > [ -6.30442381e+15 1.26088476e+16 -6.30442381e+15] > [ 3.15221191e+15 -6.30442381e+15 3.15221191e+15]] > > [[-0.5 -1. -1. ] > [-1. -2. 2. ] > [-1.5 -3. 1. ]] > > [[ 5.5 8. 10.5] > [ 3.0. -3. ] > [ -1.0. -3. ]] > > NOT the identity matrix. Any help ? The matrix you are trying to invert is singular (can't be inverted), ie its determinant is zero. >> a = arange(1.,10.) >>> b = reshape(a, [3,3]) >>> linalg.det(b) -9.5171266700777579e-16 >>> Which is zero but with a bit of rounding errors which I guess numpy doesn't notice. Double checking like this >>> a,b,c,d,e,f,g,h,i=range(1,10) >>> a*e*i - a*f*h - b*d*i + b*f*g + c*d*h - c*e*g 0 >>> So I guess it is a bug that numpy didn't throw numpy.linalg.linalg.LinAlgError("Singular matrix") Like it does normally -- Nick Craig-Wood -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Re: Error in linalg.inv ??
On Jun 6, 3:34 pm, Ajith Kumar wrote: > Hello, > I ran the following code (Using Debian 5.0) > > from numpy import * > a = arange(1.,10.) > b = reshape(a, [3,3]) > c = linalg.inv(b) > print b > print c > print dot(b,c) > print dot(c,b) > > And the result is > > [[ 1. 2. 3.] > [ 4. 5. 6.] > [ 7. 8. 9.]] > > [[ 3.15221191e+15 -6.30442381e+15 3.15221191e+15] > [ -6.30442381e+15 1.26088476e+16 -6.30442381e+15] > [ 3.15221191e+15 -6.30442381e+15 3.15221191e+15]] > > [[-0.5 -1. -1. ] > [-1. -2. 2. ] > [-1.5 -3. 1. ]] > > [[ 5.5 8. 10.5] > [ 3. 0. -3. ] > [ -1. 0. -3. ]] > > NOT the identity matrix. Any help ? It's a longer time than I care to divulge since I took courses in matrix algebra, but I do have a vague recollection that if determinant (B) is zero, inverse(B) is not defined ... seeing the rows and columns in B are linear (as are those of C), IIRC that means the determinants are zero, and you are out of luck. Are you ignoring exceptions? Is that _exactly_ what you typed in? Try running it again, print the calculated determinants of B and C, and tell what version of (a) numpy (b) Python you are using. Isn't there a mailing list for numpy? HTH, John -- http://mail.python.org/mailman/listinfo/python-list
Error in linalg.inv ??
Hello, I ran the following code (Using Debian 5.0) from numpy import * a = arange(1.,10.) b = reshape(a, [3,3]) c = linalg.inv(b) print b print c print dot(b,c) print dot(c,b) And the result is [[ 1. 2. 3.] [ 4. 5. 6.] [ 7. 8. 9.]] [[ 3.15221191e+15 -6.30442381e+15 3.15221191e+15] [ -6.30442381e+15 1.26088476e+16 -6.30442381e+15] [ 3.15221191e+15 -6.30442381e+15 3.15221191e+15]] [[-0.5 -1. -1. ] [-1. -2. 2. ] [-1.5 -3. 1. ]] [[ 5.5 8. 10.5] [ 3.0. -3. ] [ -1.0. -3. ]] NOT the identity matrix. Any help ? Thanks Ajith Kumar -- http://mail.python.org/mailman/listinfo/python-list