Rev | Line | |
---|
[37] | 1 | function n = norm(A, p) |
---|
| 2 | % NORM Matrix or vector norm. |
---|
| 3 | % (Quaternion overloading of standard Matlab function, with some limitations.) |
---|
| 4 | |
---|
| 5 | % Copyright © 2005 Stephen J. Sangwine and Nicolas Le Bihan. |
---|
| 6 | % See the file : Copyright.m for further details. |
---|
| 7 | |
---|
| 8 | error(nargchk(1, 2, nargin)), error(nargoutchk(0, 1, nargout)) |
---|
| 9 | |
---|
| 10 | % We have to handle two different cases, depending on whether A is a vector |
---|
| 11 | % or a matrix. The limiting case of a vector (a single quaternion) is handled |
---|
| 12 | % using the vector case. |
---|
| 13 | |
---|
| 14 | if nargin == 1 |
---|
| 15 | n = norm(A, 2); |
---|
| 16 | return |
---|
| 17 | end |
---|
| 18 | |
---|
| 19 | [r,c] = size(A); |
---|
| 20 | |
---|
| 21 | if r == 1 | c == 1 |
---|
| 22 | % A is a vector. |
---|
| 23 | switch p |
---|
| 24 | case 1 |
---|
| 25 | n = sum(abs(A)); |
---|
| 26 | case 2 |
---|
| 27 | n = sqrt(sum(modsquared(A))); |
---|
| 28 | % case other integer values of p are not supported, although the |
---|
| 29 | % standard Matlab norm function does handle them. |
---|
| 30 | case inf |
---|
| 31 | n = max(abs(A)); |
---|
| 32 | case -inf |
---|
| 33 | n = min(abs(A)); |
---|
| 34 | otherwise |
---|
| 35 | error('Illegal second parameter to quaternion vector norm.') |
---|
| 36 | end |
---|
| 37 | else |
---|
| 38 | % A is a matrix. |
---|
| 39 | switch p |
---|
| 40 | case 1 |
---|
| 41 | n = max(sum(abs(A))); |
---|
| 42 | case 2 |
---|
| 43 | t = svd(A); |
---|
| 44 | n = t(1); |
---|
| 45 | case inf |
---|
| 46 | n = max(sum(abs(A'))); |
---|
| 47 | case 'fro' |
---|
| 48 | % This is coded more or less as defined for the standard Matlab norm |
---|
| 49 | % function, but it is necessary to take the scalar part of the diagonal |
---|
| 50 | % vector, because the result is quaternion-valued. A more efficient |
---|
| 51 | % coding would compute the column norms directly. |
---|
| 52 | n = sqrt(sum(s(diag(A'*A)))); |
---|
| 53 | otherwise |
---|
| 54 | error('Illegal second parameter to quaternion matrix norm.') |
---|
| 55 | end |
---|
| 56 | end |
---|
| 57 | |
---|
| 58 | |
---|
Note: See
TracBrowser
for help on using the repository browser.