7017
|
1 ## Copyright (C) 1996, 1997, 2007 John W. Eaton |
6953
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
6953
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
6953
|
18 |
|
19 ## Undocumented internal function. |
|
20 |
|
21 ## Author: jwe |
|
22 |
|
23 function retval = __norm__ (x, p) |
|
24 |
|
25 if (nargin < 1 || nargin > 2) |
|
26 print_usage (); |
|
27 endif |
|
28 |
|
29 if (isempty (x)) |
|
30 retval = []; |
|
31 return; |
|
32 endif |
|
33 |
|
34 if (ndims (x) > 2) |
|
35 error ("norm: only valid on 2-D objects") |
|
36 endif |
|
37 |
|
38 if (nargin == 1) |
|
39 p = 2; |
|
40 endif |
|
41 |
|
42 ## Do we have a vector or matrix as the first argument? |
|
43 |
|
44 if (ndims(x) == 2 && (rows (x) == 1 || columns (x) == 1)) |
|
45 if (ischar (p)) |
|
46 if (strcmp (p, "fro")) |
|
47 retval = sqrt (sum (abs (x) .^ 2)); |
|
48 elseif (strcmp (p, "inf")) |
|
49 retval = max (abs (x)); |
|
50 else |
|
51 error ("norm: unrecognized norm"); |
|
52 endif |
|
53 else |
|
54 if (p == Inf) |
|
55 retval = max (abs (x)); |
|
56 elseif (p == -Inf) |
|
57 retval = min (abs (x)); |
|
58 elseif (p == 1) |
|
59 retval = sum (abs (x)); |
|
60 elseif (p == 2) |
|
61 if (iscomplex (x)) |
|
62 x = abs (x); |
|
63 endif |
|
64 retval = sqrt (sumsq (x)); |
|
65 else |
|
66 retval = sum (abs (x) .^ p) ^ (1/p); |
|
67 endif |
|
68 endif |
|
69 else |
|
70 if (ischar (p)) |
|
71 if (strcmp (p, "fro")) |
|
72 retval = sqrt (sum (sum (abs (x) .^ 2))); |
|
73 elseif (strcmp (p, "inf")) |
|
74 retval = max (sum (abs (x'))); |
|
75 else |
|
76 error ("norm: unrecognized vector norm"); |
|
77 endif |
|
78 else |
|
79 if (p == 1) |
|
80 retval = max (sum (abs (x))); |
|
81 elseif (p == 2) |
|
82 s = svd (x); |
|
83 retval = s (1); |
|
84 elseif (p == Inf) |
|
85 retval = max (sum (abs (x'))); |
|
86 else |
|
87 error ("norm: unrecognized matrix norm"); |
|
88 endif |
|
89 endif |
|
90 endif |
|
91 |
|
92 endfunction |