2303
|
1 ### Copyright (C) 1996 John W. Eaton |
|
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 |
2311
|
46 function retval = norm (x, p) |
4
|
47 |
|
48 if (nargin < 1 || nargin > 2) |
1518
|
49 error ("usage: norm (x [, p])"); |
4
|
50 endif |
|
51 |
380
|
52 if (isempty (x)) |
|
53 retval = []; |
|
54 return; |
|
55 endif |
|
56 |
2303
|
57 ## Do we have a vector or matrix as the first argument? |
4
|
58 |
|
59 if (rows (x) == 1 || columns (x) == 1) |
|
60 |
|
61 if (nargin == 2) |
|
62 if (isstr (p)) |
|
63 if (strcmp (p, "fro")) |
|
64 retval = sqrt (sum (diag (x' * x))); |
1478
|
65 elseif (strcmp (p, "inf")) |
|
66 retval = max (abs (x)); |
4
|
67 else |
|
68 error ("norm: unrecognized norm"); |
|
69 endif |
|
70 else |
|
71 if (p == Inf) |
|
72 retval = max (abs (x)); |
|
73 elseif (p == -Inf) |
|
74 retval = min (abs (x)); |
|
75 else |
|
76 retval = sum (abs (x) .^ p) ^ (1/p); |
|
77 endif |
|
78 endif |
|
79 elseif (nargin == 1) |
|
80 retval = sum (abs (x) .^ 2) ^ 0.5; |
|
81 endif |
|
82 |
|
83 else |
|
84 |
|
85 if (nargin == 2) |
|
86 if (isstr (p)) |
|
87 if (strcmp (p, "fro")) |
|
88 retval = sqrt (sum (diag (x' * x))); |
1479
|
89 elseif (strcmp (p, "inf")) |
|
90 xp = x'; |
|
91 retval = max (sum (abs (real (xp)) + abs (imag (xp)))); |
4
|
92 else |
|
93 error ("norm: unrecognized norm"); |
|
94 endif |
|
95 else |
|
96 if (p == 1) |
|
97 retval = max (sum (abs (real (x)) + abs (imag (x)))); |
|
98 elseif (p == 2) |
|
99 s = svd (x); |
|
100 retval = s (1); |
|
101 elseif (p == Inf) |
|
102 xp = x'; |
|
103 retval = max (sum (abs (real (xp)) + abs (imag (xp)))); |
|
104 endif |
|
105 endif |
|
106 elseif (nargin == 1) |
|
107 s = svd (x); |
|
108 retval = s (1); |
|
109 endif |
|
110 |
|
111 endif |
|
112 |
|
113 endfunction |