Mercurial > hg > octave-lyh
annotate scripts/linear-algebra/trace.m @ 17535:c12c688a35ed default tip lyh
Fix warnings
author | LYH <lyh.kernel@gmail.com> |
---|---|
date | Fri, 27 Sep 2013 17:43:27 +0800 |
parents | 7c575556e817 |
children |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
1 ## Copyright (C) 1993-2012 John W. Eaton |
2313 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
245 | 18 |
3372 | 19 ## -*- texinfo -*- |
16960
7c575556e817
trace.m: Use @deftypefn instead of @deftypefnx in docstring.
Rik <rik@octave.org>
parents:
16958
diff
changeset
|
20 ## @deftypefn {Function File} {} trace (@var{A}) |
16955
9c971fa62a77
trace.m: Update docstring. Use isvector() in checking inputs.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
21 ## Compute the trace of @var{A}, the sum of the elements along the main |
9c971fa62a77
trace.m: Update docstring. Use isvector() in checking inputs.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
22 ## diagonal. |
9c971fa62a77
trace.m: Update docstring. Use isvector() in checking inputs.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
23 ## |
9c971fa62a77
trace.m: Update docstring. Use isvector() in checking inputs.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
24 ## The implementation is straightforward: @code{sum (diag (@var{A}))}. |
9c971fa62a77
trace.m: Update docstring. Use isvector() in checking inputs.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
25 ## @seealso{eig} |
3372 | 26 ## @end deftypefn |
4 | 27 |
2314 | 28 ## Author: jwe |
29 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
30 function y = trace (A) |
4 | 31 |
32 if (nargin != 1) | |
6046 | 33 print_usage (); |
4 | 34 endif |
35 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
36 if (ndims (A) > 2) |
7415 | 37 error ("trace: only valid on 2-D objects"); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
38 elseif (isempty (A)) |
7415 | 39 y = 0; |
16955
9c971fa62a77
trace.m: Update docstring. Use isvector() in checking inputs.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
40 elseif (isvector (A)) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
41 y = A(1); |
1065 | 42 else |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
43 y = sum (diag (A)); |
1065 | 44 endif |
4 | 45 |
46 endfunction | |
7411 | 47 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
48 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
49 %!assert (trace ([1, 2; 3, 4]), 5) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
50 %!assert (trace ([1, 2; 3, 4; 5, 6]), 5) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
51 %!assert (trace ([1, 3, 5; 2, 4, 6]), 5) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
52 %!assert (trace ([]), 0) |
16955
9c971fa62a77
trace.m: Update docstring. Use isvector() in checking inputs.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
53 %!assert (trace (rand (1,0)), 0) |
9c971fa62a77
trace.m: Update docstring. Use isvector() in checking inputs.
Rik <rik@octave.org>
parents:
14363
diff
changeset
|
54 %!assert (trace ([3:10]), 3) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
55 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
56 %!error trace () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
57 %!error trace (1, 2) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
58 %!error <only valid on 2-D objects> trace (reshape (1:9,[1,3,3])) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
59 |