1014
|
1 # Copyright (C) 1993, 1994, 1995 John W. Eaton |
245
|
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 the |
|
7 # Free Software Foundation; either version 2, or (at your option) any |
|
8 # later version. |
|
9 # |
|
10 # Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 # 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 |
1315
|
17 # Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
245
|
18 |
4
|
19 function retval = norm (x, p) |
|
20 |
|
21 # usage: norm (x, p) |
|
22 # |
|
23 # Compute the p-norm of x. |
|
24 # |
|
25 # If x is a matrix: |
|
26 # |
|
27 # value of p norm returns |
|
28 # ---------- ------------ |
|
29 # 1 1-norm, the largest column sum of x |
|
30 # 2 largest singular value of x |
|
31 # Inf infinity norm, the largest row sum of x |
1478
|
32 # "inf" same as Inf |
4
|
33 # "fro" Frobenius norm of x, sqrt (sum (diag (x' * x))) |
|
34 # |
|
35 # If x is a vector or a scalar: |
|
36 # |
|
37 # value of p norm returns |
|
38 # ---------- ------------ |
|
39 # Inf max (abs (x)) |
|
40 # -Inf min (abs (x)) |
|
41 # other p-norm of x, sum (abs (x) .^ p) ^ (1/p) |
|
42 # |
|
43 # If the second argument is missing, p = 2 is assumed. |
|
44 # |
|
45 # See also: cond, svd |
|
46 |
|
47 if (nargin < 1 || nargin > 2) |
|
48 error ("usage: norm (x [, p])") |
|
49 endif |
|
50 |
380
|
51 if (isempty (x)) |
|
52 retval = []; |
|
53 return; |
|
54 endif |
|
55 |
4
|
56 # Do we have a vector or matrix as the first argument? |
|
57 |
|
58 if (rows (x) == 1 || columns (x) == 1) |
|
59 |
|
60 if (nargin == 2) |
|
61 if (isstr (p)) |
|
62 if (strcmp (p, "fro")) |
|
63 retval = sqrt (sum (diag (x' * x))); |
1478
|
64 elseif (strcmp (p, "inf")) |
|
65 retval = max (abs (x)); |
4
|
66 else |
|
67 error ("norm: unrecognized norm"); |
|
68 endif |
|
69 else |
|
70 if (p == Inf) |
|
71 retval = max (abs (x)); |
|
72 elseif (p == -Inf) |
|
73 retval = min (abs (x)); |
|
74 else |
|
75 retval = sum (abs (x) .^ p) ^ (1/p); |
|
76 endif |
|
77 endif |
|
78 elseif (nargin == 1) |
|
79 retval = sum (abs (x) .^ 2) ^ 0.5; |
|
80 endif |
|
81 |
|
82 else |
|
83 |
|
84 if (nargin == 2) |
|
85 if (isstr (p)) |
|
86 if (strcmp (p, "fro")) |
|
87 retval = sqrt (sum (diag (x' * x))); |
1479
|
88 elseif (strcmp (p, "inf")) |
|
89 xp = x'; |
|
90 retval = max (sum (abs (real (xp)) + abs (imag (xp)))); |
4
|
91 else |
|
92 error ("norm: unrecognized norm"); |
|
93 endif |
|
94 else |
|
95 if (p == 1) |
|
96 retval = max (sum (abs (real (x)) + abs (imag (x)))); |
|
97 elseif (p == 2) |
|
98 s = svd (x); |
|
99 retval = s (1); |
|
100 elseif (p == Inf) |
|
101 xp = x'; |
|
102 retval = max (sum (abs (real (xp)) + abs (imag (xp)))); |
|
103 endif |
|
104 endif |
|
105 elseif (nargin == 1) |
|
106 s = svd (x); |
|
107 retval = s (1); |
|
108 endif |
|
109 |
|
110 endif |
|
111 |
|
112 endfunction |