octave-maintainers
[Top][All Lists]
Advanced

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

Patch for trace.m


From: Alois Schloegl
Subject: Patch for trace.m
Date: Wed, 23 Jan 2008 12:30:43 +0100
User-agent: Icedove 1.5.0.14pre (X11/20071018)

Two special cases are not properly handled by trace.m

1) NDarrays can cause misleading results, if the size along any of the first two dimensions is one. Here is an example:

x=reshape(1:9,[1,3,3]);
trace(x)

ans =
   1

Obviously, this is not a desirable result, but one would expect an error message similar to the case of NDarrays with no singleton dimension.


2) Furthermore, empty variables of size 1x0 or 0x1 do not return 0 but cause an error.

x = randn(1,0);
y = trace(x)
error: invalid vector index = 1
error: evaluating assignment expression near line 36, column 7
error: evaluating if command near line 33, column 3
error: called from `trace' in file `/home/neuro/schalo/cvs/octave/scripts/linear-algebra/trace.m'

The patch below adds support for these cases.


Alois

===================================================================
RCS file: /cvs/octave/scripts/linear-algebra/trace.m,v
retrieving revision 1.17
diff -u -r1.17 trace.m
--- trace.m     12 Oct 2007 21:27:23 -0000      1.17
+++ trace.m     23 Jan 2008 10:49:53 -0000
@@ -30,8 +30,11 @@
    print_usage ();
  endif

-  [nr, nc] = size (x);
-  if (nr == 1 || nc == 1)
+  if (ndims (x) > 2)
+    error ("trace: Only valid on 2-D objects");
+  elseif (isempty (x))
+    y = 0;
+  elseif (any (size (x) == 1))
    y = x(1);
  else
    y = sum (diag (x));

===================================================================



reply via email to

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