Mercurial > octave-nkf
changeset 16955:9c971fa62a77
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.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 11 Jul 2013 11:16:57 -0700 |
parents | 2ee5109d5914 |
children | eefcfeb37446 |
files | scripts/linear-algebra/trace.m |
diffstat | 1 files changed, 10 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/linear-algebra/trace.m Tue Jul 09 21:43:39 2013 +0200 +++ b/scripts/linear-algebra/trace.m Thu Jul 11 11:16:57 2013 -0700 @@ -17,8 +17,13 @@ ## <http://www.gnu.org/licenses/>. ## -*- 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)