Mercurial > octave
annotate scripts/linear-algebra/issymmetric.m @ 25224:1f1aef87e136
issymmetric.m: Fix calculation of skew matrices (bug in 0f98040552de).
* issymmetric.m: Reshape comparison matrix into column vector before using
any().
author | Rik <rik@octave.org> |
---|---|
date | Wed, 11 Apr 2018 21:50:38 -0700 |
parents | 0f98040552de |
children | a937ffe7dfd9 |
rev | line source |
---|---|
25054
6652d3823428
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
1 ## Copyright (C) 1996-2018 John W. Eaton |
9869
ecd750d1eabd
move issymmetric & isdefinite to linear-algebra, create ishermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2 ## Copyright (C) 2009 VZLU Prague |
4026 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
6 ## Octave is free software: you can redistribute it and/or modify it |
4026 | 7 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
8 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
9 ## (at your option) any later version. |
4026 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
14 ## GNU General Public License for more details. |
4026 | 15 ## |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
18 ## <https://www.gnu.org/licenses/>. |
4026 | 19 |
20 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20160
diff
changeset
|
21 ## @deftypefn {} {} issymmetric (@var{A}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20160
diff
changeset
|
22 ## @deftypefnx {} {} issymmetric (@var{A}, @var{tol}) |
25223
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
23 ## @deftypefnx {} {} issymmetric (@var{A}, @qcode{"skew"}) |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
24 ## @deftypefnx {} {} issymmetric (@var{A}, @qcode{"skew"}, @var{tol}) |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
25 ## Return true if @var{A} is a symmetric or skew-symmetric matrix within the |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
26 ## tolerance specified by @var{tol}. |
18921
d0d0858cfab1
doc: Match docstring variable names to function variable names for linear-algebra m-files.
Rik <rik@octave.org>
parents:
18799
diff
changeset
|
27 ## |
d0d0858cfab1
doc: Match docstring variable names to function variable names for linear-algebra m-files.
Rik <rik@octave.org>
parents:
18799
diff
changeset
|
28 ## The default tolerance is zero (uses faster code). |
20160
03b9d17a2d95
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
29 ## |
25223
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
30 ## The type of symmetry to check may be specified with the additional input |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
31 ## @qcode{"nonskew"} (default) for regular symmetry or @qcode{"skew"} for |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
32 ## skew-symmetry. |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
33 ## |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
34 ## Background: A matrix is symmetric if the transpose of the matrix is equal |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
35 ## to the original matrix: @w{@acode{@var{A} == @var{A}.'}}. If a tolerance |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
36 ## is given then symmetry is determined by |
18921
d0d0858cfab1
doc: Match docstring variable names to function variable names for linear-algebra m-files.
Rik <rik@octave.org>
parents:
18799
diff
changeset
|
37 ## @code{norm (@var{A} - @var{A}.', Inf) / norm (@var{A}, Inf) < @var{tol}}. |
25223
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
38 ## |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
39 ## A matrix is skew-symmetric if the transpose of the matrix is equal to the |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
40 ## negative of the original matrix: @w{@acode{@var{A} == -@var{A}.'}}. If a |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
41 ## tolerance is given then skew-symmetry is determined by |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
42 ## @code{norm (@var{A} + @var{A}.', Inf) / norm (@var{A}, Inf) < @var{tol}}. |
11431
0d9640d755b1
Improve docstrings for all isXXX functions.
Rik <octave@nomad.inbox5.com>
parents:
10711
diff
changeset
|
43 ## @seealso{ishermitian, isdefinite} |
4026 | 44 ## @end deftypefn |
45 | |
46 ## Author: A. S. Hodel <scotte@eng.auburn.edu> | |
47 ## Created: August 1993 | |
48 ## Adapted-By: jwe | |
49 | |
25223
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
50 function retval = issymmetric (A, skewopt = "nonskew", tol = 0) |
9869
ecd750d1eabd
move issymmetric & isdefinite to linear-algebra, create ishermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
51 |
25223
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
52 if (nargin < 1 || nargin > 3) |
9869
ecd750d1eabd
move issymmetric & isdefinite to linear-algebra, create ishermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
53 print_usage (); |
ecd750d1eabd
move issymmetric & isdefinite to linear-algebra, create ishermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
54 endif |
4026 | 55 |
25223
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
56 if (nargin == 2) |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
57 ## Decode whether second argument is skewopt or tol |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
58 if (isnumeric (skewopt)) |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
59 tol = skewopt; |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
60 skewopt = "nonskew"; |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
61 elseif (! ischar (skewopt)) |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
62 error ("issymmetric: second argument must be a non-negative scalar TOL, or one of the strings: 'skew' / 'nonskew'"); |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
63 endif |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
64 endif |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
65 |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
66 ## Validate inputs |
18921
d0d0858cfab1
doc: Match docstring variable names to function variable names for linear-algebra m-files.
Rik <rik@octave.org>
parents:
18799
diff
changeset
|
67 retval = (isnumeric (A) || islogical (A)) && issquare (A); |
25223
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
68 if (! retval) |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
69 return; |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
70 endif |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
71 |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
72 if (! (strcmp (skewopt, "skew") || strcmp (skewopt, "nonskew"))) |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
73 error ("issymmetric: SKEWOPT must be 'skew' or 'nonskew'"); |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
74 endif |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
75 |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
76 if (! (isnumeric (tol) && isscalar (tol) && tol >= 0)) |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
77 error ("issymmetric: TOL must be a scalar >= 0"); |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
78 endif |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
79 |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
80 ## Calculate symmetry |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
81 if (strcmp (skewopt, "nonskew")) |
9869
ecd750d1eabd
move issymmetric & isdefinite to linear-algebra, create ishermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
82 if (tol == 0) |
25223
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
83 ## check for exact symmetry |
25224
1f1aef87e136
issymmetric.m: Fix calculation of skew matrices (bug in 0f98040552de).
Rik <rik@octave.org>
parents:
25223
diff
changeset
|
84 retval = ! any ((A != A.')(:)); |
9869
ecd750d1eabd
move issymmetric & isdefinite to linear-algebra, create ishermitian
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
85 else |
25223
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
86 norm_x = norm (A, Inf); |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
87 retval = norm_x == 0 || norm (A - A.', Inf) / norm_x <= tol; |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
88 endif |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
89 else |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
90 ## skew symmetry |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
91 if (tol == 0) |
25224
1f1aef87e136
issymmetric.m: Fix calculation of skew matrices (bug in 0f98040552de).
Rik <rik@octave.org>
parents:
25223
diff
changeset
|
92 retval = ! any ((A != -A.')(:)); |
25223
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
93 else |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
94 norm_x = norm (A, Inf); |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
95 retval = norm_x == 0 || norm (A + A.', Inf) / norm_x <= tol; |
4026 | 96 endif |
97 endif | |
98 | |
99 endfunction | |
7265 | 100 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
101 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
102 %!assert (issymmetric (1)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
103 %!assert (! issymmetric ([1, 2])) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
104 %!assert (issymmetric ([])) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
105 %!assert (issymmetric ([1, 2; 2, 1])) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
106 %!assert (issymmetric ([1, 2.1; 2, 1.1], 0.2)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
107 %!assert (issymmetric ([1, 2i; 2i, 1])) |
25223
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
108 %!assert (issymmetric (speye (100))) |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
109 %!assert (issymmetric (logical (eye (2)))) |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
110 %!assert (issymmetric ([0, 2; -2, 0], "skew")) |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
111 %!assert (! issymmetric ([0, 2; -2, eps], "skew")) |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
112 %!assert (issymmetric ([0, 2; -2, eps], "skew", eps)) |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
113 |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
114 %!assert (! (issymmetric ("test"))) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
115 %!assert (! (issymmetric ("t"))) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
116 %!assert (! (issymmetric (["te"; "et"]))) |
25223
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
117 %!assert (! issymmetric ({1})) |
7265 | 118 %!test |
119 %! s.a = 1; | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
120 %! assert (! issymmetric (s)); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
121 |
25223
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
122 ## Test input validation |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
123 %!error issymmetric () |
25223
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
124 %!error issymmetric (1,2,3,4) |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
125 %!error <second argument must be> issymmetric (1, {"skew"}) |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
126 %!error <SKEWOPT must be 'skew' or 'nonskew'> issymmetric (1, "foobar") |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
127 %!error <SKEWOPT must be 'skew' or 'nonskew'> issymmetric (1, "foobar") |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
128 %!error <TOL must be a scalar .= 0> issymmetric (1, "skew", {1}) |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
129 %!error <TOL must be a scalar .= 0> issymmetric (1, "skew", [1 1]) |
0f98040552de
issymetric.m; Overhaul function and expand to check skew-symmetry (bug #53556).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
130 %!error <TOL must be a scalar .= 0> issymmetric (1, -1) |