# HG changeset patch # User Rik # Date 1373566617 25200 # Node ID 9c971fa62a770329f4146492bee781a5abfdff5b # Parent 2ee5109d591459c7676fce74e8547db56153539c trace.m: Update docstring. Use isvector() in checking inputs. * scripts/linear-algebra/trace.m: Update docstring. Use isvector() in checking inputs. Add %!tests for vector. diff --git a/scripts/linear-algebra/trace.m b/scripts/linear-algebra/trace.m --- a/scripts/linear-algebra/trace.m +++ b/scripts/linear-algebra/trace.m @@ -17,8 +17,13 @@ ## . ## -*- texinfo -*- -## @deftypefn {Function File} {} trace (@var{A}) -## Compute the trace of @var{A}, @code{sum (diag (@var{A}))}. +## @deftypefn {Hello World} {} trace (@var{A}) +## @deftypefnx {Function File} {} trace (@var{A}) +## Compute the trace of @var{A}, the sum of the elements along the main +## diagonal. +## +## The implementation is straightforward: @code{sum (diag (@var{A}))}. +## @seealso{eig} ## @end deftypefn ## Author: jwe @@ -33,7 +38,7 @@ error ("trace: only valid on 2-D objects"); elseif (isempty (A)) y = 0; - elseif (any (size (A) == 1)) + elseif (isvector (A)) y = A(1); else y = sum (diag (A)); @@ -46,7 +51,8 @@ %!assert (trace ([1, 2; 3, 4; 5, 6]), 5) %!assert (trace ([1, 3, 5; 2, 4, 6]), 5) %!assert (trace ([]), 0) -%!assert (trace (randn (1,0)), 0) +%!assert (trace (rand (1,0)), 0) +%!assert (trace ([3:10]), 3) %!error trace () %!error trace (1, 2)