comparison scripts/linear-algebra/__norm__.m @ 7101:2c5b14c60c6c

[project @ 2007-11-06 16:26:13 by jwe]
author jwe
date Tue, 06 Nov 2007 16:26:13 +0000
parents a1dbe9d80eee
children a627f27c1e8e
comparison
equal deleted inserted replaced
7100:28607462901f 7101:2c5b14c60c6c
38 if (nargin == 1) 38 if (nargin == 1)
39 p = 2; 39 p = 2;
40 endif 40 endif
41 41
42 ## Do we have a vector or matrix as the first argument? 42 ## Do we have a vector or matrix as the first argument?
43
44 if (ndims(x) == 2 && (rows (x) == 1 || columns (x) == 1)) 43 if (ndims(x) == 2 && (rows (x) == 1 || columns (x) == 1))
45 if (ischar (p)) 44 if (ischar (p))
46 if (strcmp (p, "fro")) 45 if (strcmp (p, "fro"))
47 retval = sqrt (sum (abs (x) .^ 2)); 46 inf_norm = norm (x, "inf")
47 retval = inf_norm .* sqrt (sum (abs (x ./ inf_norm) .^ 2));
48 elseif (strcmp (p, "inf")) 48 elseif (strcmp (p, "inf"))
49 retval = max (abs (x)); 49 retval = max (abs (x));
50 else 50 else
51 error ("norm: unrecognized norm"); 51 error ("norm: unrecognized norm");
52 endif 52 endif
67 endif 67 endif
68 endif 68 endif
69 else 69 else
70 if (ischar (p)) 70 if (ischar (p))
71 if (strcmp (p, "fro")) 71 if (strcmp (p, "fro"))
72 retval = sqrt (sum (sum (abs (x) .^ 2))); 72 inf_norm = norm (x, "inf")
73 retval = inf_norm .* sqrt (sum (sum (abs (x ./ inf_norm) .^ 2)));
73 elseif (strcmp (p, "inf")) 74 elseif (strcmp (p, "inf"))
74 retval = max (sum (abs (x'))); 75 retval = max (sum (abs (x')));
75 else 76 else
76 error ("norm: unrecognized vector norm"); 77 error ("norm: unrecognized vector norm");
77 endif 78 endif