2847
|
1 ## Copyright (C) 1996, 1997 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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
245
|
19 |
3372
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} norm (@var{a}, @var{p}) |
|
22 ## Compute the p-norm of the matrix @var{a}. If the second argument is |
|
23 ## missing, @code{p = 2} is assumed. |
3426
|
24 ## |
3372
|
25 ## If @var{a} is a matrix: |
3426
|
26 ## |
3372
|
27 ## @table @asis |
|
28 ## @item @var{p} = @code{1} |
3966
|
29 ## 1-norm, the largest column sum of the absolute values of @var{a}. |
3426
|
30 ## |
3372
|
31 ## @item @var{p} = @code{2} |
|
32 ## Largest singular value of @var{a}. |
3426
|
33 ## |
3372
|
34 ## @item @var{p} = @code{Inf} |
|
35 ## @cindex infinity norm |
3966
|
36 ## Infinity norm, the largest row sum of the absolute values of @var{a}. |
3426
|
37 ## |
3372
|
38 ## @item @var{p} = @code{"fro"} |
|
39 ## @cindex Frobenius norm |
|
40 ## Frobenius norm of @var{a}, @code{sqrt (sum (diag (@var{a}' * @var{a})))}. |
|
41 ## @end table |
3426
|
42 ## |
3372
|
43 ## If @var{a} is a vector or a scalar: |
3426
|
44 ## |
3372
|
45 ## @table @asis |
|
46 ## @item @var{p} = @code{Inf} |
|
47 ## @code{max (abs (@var{a}))}. |
3426
|
48 ## |
3372
|
49 ## @item @var{p} = @code{-Inf} |
|
50 ## @code{min (abs (@var{a}))}. |
3426
|
51 ## |
3372
|
52 ## @item other |
|
53 ## p-norm of @var{a}, @code{(sum (abs (@var{a}) .^ @var{p})) ^ (1/@var{p})}. |
|
54 ## @end table |
5642
|
55 ## @seealso{cond, svd} |
3372
|
56 ## @end deftypefn |
4
|
57 |
2314
|
58 ## Author: jwe |
|
59 |
2311
|
60 function retval = norm (x, p) |
4
|
61 |
|
62 if (nargin < 1 || nargin > 2) |
6046
|
63 print_usage (); |
4
|
64 endif |
|
65 |
380
|
66 if (isempty (x)) |
|
67 retval = []; |
|
68 return; |
|
69 endif |
|
70 |
4890
|
71 if (ndims (x) > 2) |
6508
|
72 error ("norm: only valid on 2-D objects") |
|
73 endif |
|
74 |
|
75 if (nargin == 1) |
|
76 p = 2; |
4890
|
77 endif |
|
78 |
2303
|
79 ## Do we have a vector or matrix as the first argument? |
4
|
80 |
6508
|
81 if (is_vector (x)) |
|
82 if (isinteger (x) || issparse (x)) |
5443
|
83 if (ischar (p)) |
4
|
84 if (strcmp (p, "fro")) |
3801
|
85 retval = sqrt (sum (abs (x) .^ 2)); |
1478
|
86 elseif (strcmp (p, "inf")) |
|
87 retval = max (abs (x)); |
4
|
88 else |
|
89 error ("norm: unrecognized norm"); |
|
90 endif |
|
91 else |
3426
|
92 if (p == Inf) |
|
93 retval = max (abs (x)); |
|
94 elseif (p == -Inf) |
|
95 retval = min (abs (x)); |
|
96 else |
|
97 retval = sum (abs (x) .^ p) ^ (1/p); |
|
98 endif |
4
|
99 endif |
6508
|
100 else |
|
101 retval = __vnorm__ (x, p); |
4
|
102 endif |
|
103 else |
6508
|
104 if (ischar (p)) |
|
105 if (strcmp (p, "fro")) |
|
106 retval = sqrt (sum (sum (abs (x) .^ 2))); |
|
107 elseif (strcmp (p, "inf")) |
|
108 retval = max (sum (abs (x'))); |
4
|
109 else |
6508
|
110 error ("norm: unrecognized vector norm"); |
4
|
111 endif |
6508
|
112 else |
|
113 if (p == 1) |
|
114 retval = max (sum (abs (x))); |
|
115 elseif (p == 2) |
|
116 s = svd (x); |
|
117 retval = s (1); |
|
118 elseif (p == Inf) |
|
119 retval = max (sum (abs (x'))); |
|
120 else |
|
121 error ("norm: unrecognized matrix norm"); |
|
122 endif |
4
|
123 endif |
|
124 endif |
|
125 |
|
126 endfunction |
6508
|
127 |
|
128 %!shared x |
|
129 %! x = [1, -3, 4, 5, -7]; |
|
130 %!assert(norm(x,1), 20); |
|
131 %!assert(norm(x,2), 10); |
|
132 %!assert(norm(x,3), 8.24257059961711, -4*eps); |
|
133 %!assert(norm(x,Inf), 7); |
|
134 %!assert(norm(x,-Inf), 1); |
|
135 %!assert(norm(x,"inf"), 7); |
|
136 %!assert(norm(x,"fro"), 10); |
|
137 %!assert(norm(x), 10); |
|
138 %!assert(norm([1e200, 1]), 1e200); |
|
139 %!shared m |
|
140 %! m = magic (4); |
|
141 %!assert(norm(m,1), 34); |
|
142 %!assert(norm(m,2), 34); |
|
143 %!assert(norm(m,Inf), 34); |
|
144 %!assert(norm(m,"inf"), 34); |