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