# HG changeset patch # User Rik # Date 1444228904 25200 # Node ID f61c67865d9fe1d09b3d46766c8c962e8d00c352 # Parent ea6a1c00763adf9c6ee19cc25554b35f6a1a6c0c Don't return A for inv (A) when A is a singular Diagonal matrix (bug #46103). * dDiagMatrix.cc (inverse), fDiagMatrix.cc (inverse): return octave_Inf for the inverse of a diagonal element whose value is 0. * test/diag-perm.tst: Add tests for new behavior. diff -r ea6a1c00763a -r f61c67865d9f liboctave/array/dDiagMatrix.cc --- a/liboctave/array/dDiagMatrix.cc Tue Oct 06 22:14:41 2015 +0200 +++ b/liboctave/array/dDiagMatrix.cc Wed Oct 07 07:41:44 2015 -0700 @@ -280,10 +280,7 @@ for (octave_idx_type i = 0; i < len; i++) { if (elem (i, i) == 0.0) - { - info = -1; - return *this; - } + retval.elem (i, i) = octave_Inf; else retval.elem (i, i) = 1.0 / elem (i, i); } diff -r ea6a1c00763a -r f61c67865d9f liboctave/array/fDiagMatrix.cc --- a/liboctave/array/fDiagMatrix.cc Tue Oct 06 22:14:41 2015 +0200 +++ b/liboctave/array/fDiagMatrix.cc Wed Oct 07 07:41:44 2015 -0700 @@ -280,10 +280,7 @@ for (octave_idx_type i = 0; i < len; i++) { if (elem (i, i) == 0.0) - { - info = -1; - return *this; - } + retval.elem (i, i) = octave_Inf; else retval.elem (i, i) = 1.0 / elem (i, i); } diff -r ea6a1c00763a -r f61c67865d9f test/diag-perm.tst --- a/test/diag-perm.tst Tue Oct 06 22:14:41 2015 +0200 +++ b/test/diag-perm.tst Wed Oct 07 07:41:44 2015 -0700 @@ -263,3 +263,10 @@ %! A = A * I () + A; %! A(6, 4) = nan (); %! assert (full (D - A), D - full (A)); + +## inverse preserves diagonal structure even for singular matrices (bug #46103) +%!test +%! x = diag (1:3); +%! assert (inv (x), diag ([1 1/2 1/3])); +%! x = diag (0:2); +%! assert (inv (x), diag ([Inf 1 1/2]));