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 |
|
55 ## @end deftypefn |
5053
|
56 ## |
3408
|
57 ## @seealso{cond and svd} |
4
|
58 |
2314
|
59 ## Author: jwe |
|
60 |
2311
|
61 function retval = norm (x, p) |
4
|
62 |
|
63 if (nargin < 1 || nargin > 2) |
3457
|
64 usage ("norm (x, p)"); |
4
|
65 endif |
|
66 |
380
|
67 if (isempty (x)) |
|
68 retval = []; |
|
69 return; |
|
70 endif |
|
71 |
4890
|
72 if (ndims (x) > 2) |
|
73 error ("norm: Only valid on 2-D objects") |
|
74 endif |
|
75 |
2303
|
76 ## Do we have a vector or matrix as the first argument? |
4
|
77 |
|
78 if (rows (x) == 1 || columns (x) == 1) |
|
79 |
|
80 if (nargin == 2) |
5443
|
81 if (ischar (p)) |
4
|
82 if (strcmp (p, "fro")) |
3801
|
83 retval = sqrt (sum (abs (x) .^ 2)); |
1478
|
84 elseif (strcmp (p, "inf")) |
|
85 retval = max (abs (x)); |
4
|
86 else |
|
87 error ("norm: unrecognized norm"); |
|
88 endif |
|
89 else |
3426
|
90 if (p == Inf) |
|
91 retval = max (abs (x)); |
|
92 elseif (p == -Inf) |
|
93 retval = min (abs (x)); |
|
94 else |
|
95 retval = sum (abs (x) .^ p) ^ (1/p); |
|
96 endif |
4
|
97 endif |
|
98 elseif (nargin == 1) |
3801
|
99 retval = sqrt (sum (abs (x) .^ 2)); |
4
|
100 endif |
|
101 |
|
102 else |
|
103 |
|
104 if (nargin == 2) |
5443
|
105 if (ischar (p)) |
4
|
106 if (strcmp (p, "fro")) |
3801
|
107 retval = sqrt (sum (sum (abs (x) .^ 2))); |
1479
|
108 elseif (strcmp (p, "inf")) |
3238
|
109 retval = max (sum (abs (x'))); |
4
|
110 else |
|
111 error ("norm: unrecognized norm"); |
|
112 endif |
|
113 else |
|
114 if (p == 1) |
3238
|
115 retval = max (sum (abs (x))); |
4
|
116 elseif (p == 2) |
|
117 s = svd (x); |
|
118 retval = s (1); |
|
119 elseif (p == Inf) |
3238
|
120 retval = max (sum (abs (x'))); |
4
|
121 endif |
|
122 endif |
|
123 elseif (nargin == 1) |
|
124 s = svd (x); |
|
125 retval = s (1); |
|
126 endif |
|
127 |
|
128 endif |
|
129 |
|
130 endfunction |