octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

diag() boundary case


From: John W. Eaton
Subject: diag() boundary case
Date: Tue, 29 Mar 2011 18:23:28 -0400

On 29-Mar-2011, Rik wrote:

| There is a bug open regarding the behavior of diag when the input vector
| has a null dimension (https://savannah.gnu.org/bugs/?32901).  From the
| documentation it seems that diag(x) should build a diagonal matrix from x
| if x is a vector.  When x is a matrix then diag should extract the diagonal
| instead.  Under the current code there is a corner case if the input is 1x0
| or 0x1.  Octave always returns the empty matrix.  However, Matlab treats
| this as a vector (albeit empty) and can return a matrix when the second
| argument to diag is not zero.  For example, under Matlab
| diag (ones (1, 0), 2)
| => 0 0
|    0 0
| This makes sense to me, as the user is requesting to put an empty vector on
| the second super-diagonal and that would be equivalent to zeroes (2,2).
| The attached patch changes the behavior of diag to match that of Matlab,
| but do people agree that it makes sense in this case?

I've never liked that diag does two conflicting tasks, but there's not
much we can do about that.  The behavior might as well match Matlab in
this case.  I suppose I didn't realize that a 1x0 or 0x1 array would
be considered a vector.

| The attached patch passes 'make check' except for 1 regression in
| wilkinson.m that would need examination.

I think the following change should be made to wilkinson.m.

jwe

diff --git a/scripts/special-matrix/wilkinson.m 
b/scripts/special-matrix/wilkinson.m
--- a/scripts/special-matrix/wilkinson.m
+++ b/scripts/special-matrix/wilkinson.m
@@ -35,8 +35,8 @@
     print_usage ();
   endif
 
-  if (! (isscalar (n) && (n == fix (n)) && n > 0))
-    error ("wilkinson: N must be an integer greater than 0");
+  if (! (isscalar (n) && (n == fix (n)) && n >= 0))
+    error ("wilkinson: N must be a non-negative integer");
   endif
 
   side = ones (n-1, 1);
@@ -45,7 +45,8 @@
 
 endfunction
 
-%!assert (wilkinson(1), [])
+%!assert (wilkinson(0), [])
+%!assert (wilkinson(1), 0)
 %!assert (wilkinson(2), [0.5,1;1,0.5])
 %!assert (wilkinson(3), [1,1,0;1,0,1;0,1,1])
 %!assert (wilkinson(4), [1.5,1,0,0;1,0.5,1,0;0,1,0.5,1;0,0,1,1.5])

reply via email to

[Prev in Thread] Current Thread [Next in Thread]