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 |
2311
|
20 ## usage: norm (x, p) |
|
21 ## |
|
22 ## Compute the p-norm of x. |
|
23 ## |
|
24 ## If x is a matrix: |
|
25 ## |
|
26 ## value of p norm returns |
|
27 ## ---------- ------------ |
|
28 ## 1 1-norm, the largest column sum of x |
|
29 ## 2 largest singular value of x |
|
30 ## Inf infinity norm, the largest row sum of x |
|
31 ## "inf" same as Inf |
|
32 ## "fro" Frobenius norm of x, sqrt (sum (diag (x' * x))) |
|
33 ## |
|
34 ## If x is a vector or a scalar: |
|
35 ## |
|
36 ## value of p norm returns |
|
37 ## ---------- ------------ |
|
38 ## Inf max (abs (x)) |
|
39 ## -Inf min (abs (x)) |
|
40 ## other p-norm of x, sum (abs (x) .^ p) ^ (1/p) |
|
41 ## |
|
42 ## If the second argument is missing, p = 2 is assumed. |
|
43 ## |
|
44 ## See also: cond, svd |
4
|
45 |
2314
|
46 ## Author: jwe |
|
47 |
2311
|
48 function retval = norm (x, p) |
4
|
49 |
|
50 if (nargin < 1 || nargin > 2) |
1518
|
51 error ("usage: norm (x [, p])"); |
4
|
52 endif |
|
53 |
380
|
54 if (isempty (x)) |
|
55 retval = []; |
|
56 return; |
|
57 endif |
|
58 |
2303
|
59 ## Do we have a vector or matrix as the first argument? |
4
|
60 |
|
61 if (rows (x) == 1 || columns (x) == 1) |
|
62 |
|
63 if (nargin == 2) |
|
64 if (isstr (p)) |
|
65 if (strcmp (p, "fro")) |
|
66 retval = sqrt (sum (diag (x' * x))); |
1478
|
67 elseif (strcmp (p, "inf")) |
|
68 retval = max (abs (x)); |
4
|
69 else |
|
70 error ("norm: unrecognized norm"); |
|
71 endif |
|
72 else |
|
73 if (p == Inf) |
|
74 retval = max (abs (x)); |
|
75 elseif (p == -Inf) |
|
76 retval = min (abs (x)); |
|
77 else |
|
78 retval = sum (abs (x) .^ p) ^ (1/p); |
|
79 endif |
|
80 endif |
|
81 elseif (nargin == 1) |
|
82 retval = sum (abs (x) .^ 2) ^ 0.5; |
|
83 endif |
|
84 |
|
85 else |
|
86 |
|
87 if (nargin == 2) |
|
88 if (isstr (p)) |
|
89 if (strcmp (p, "fro")) |
|
90 retval = sqrt (sum (diag (x' * x))); |
1479
|
91 elseif (strcmp (p, "inf")) |
3238
|
92 retval = max (sum (abs (x'))); |
4
|
93 else |
|
94 error ("norm: unrecognized norm"); |
|
95 endif |
|
96 else |
|
97 if (p == 1) |
3238
|
98 retval = max (sum (abs (x))); |
4
|
99 elseif (p == 2) |
|
100 s = svd (x); |
|
101 retval = s (1); |
|
102 elseif (p == Inf) |
3238
|
103 retval = max (sum (abs (x'))); |
4
|
104 endif |
|
105 endif |
|
106 elseif (nargin == 1) |
|
107 s = svd (x); |
|
108 retval = s (1); |
|
109 endif |
|
110 |
|
111 endif |
|
112 |
|
113 endfunction |