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