home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts.fat / la / norm.m < prev    next >
Text File  |  1999-12-24  |  3KB  |  127 lines

  1. ## Copyright (C) 1996, 1997 John W. Eaton
  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
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  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
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## -*- texinfo -*-
  21. ## @deftypefn {Function File} {} norm (@var{a}, @var{p})
  22. ## Compute the p-norm of the matrix @var{a}.  If the second argument is
  23. ## missing, @code{p = 2} is assumed.
  24. ## 
  25. ## If @var{a} is a matrix:
  26. ## 
  27. ## @table @asis
  28. ## @item @var{p} = @code{1}
  29. ## 1-norm, the largest column sum of @var{a}.
  30. ## 
  31. ## @item @var{p} = @code{2}
  32. ## Largest singular value of @var{a}.
  33. ## 
  34. ## @item @var{p} = @code{Inf}
  35. ## @cindex infinity norm
  36. ## Infinity norm, the largest row sum of @var{a}.
  37. ## 
  38. ## @item @var{p} = @code{"fro"}
  39. ## @cindex Frobenius norm
  40. ## Frobenius norm of @var{a}, @code{sqrt (sum (diag (@var{a}' * @var{a})))}.
  41. ## @end table
  42. ## 
  43. ## If @var{a} is a vector or a scalar:
  44. ## 
  45. ## @table @asis
  46. ## @item @var{p} = @code{Inf}
  47. ## @code{max (abs (@var{a}))}.
  48. ## 
  49. ## @item @var{p} = @code{-Inf}
  50. ## @code{min (abs (@var{a}))}.
  51. ## 
  52. ## @item other
  53. ## p-norm of @var{a}, @code{(sum (abs (@var{a}) .^ @var{p})) ^ (1/@var{p})}.
  54. ## @end table
  55. ## @end deftypefn
  56.  
  57. ## See also: cond, svd
  58.  
  59. ## Author: jwe
  60.  
  61. function retval = norm (x, p)
  62.  
  63.   if (nargin < 1 || nargin > 2)
  64.     error ("usage: norm (x [, p])");
  65.   endif
  66.  
  67.   if (isempty (x))
  68.     retval = [];
  69.     return;
  70.   endif
  71.  
  72.   ## Do we have a vector or matrix as the first argument?
  73.  
  74.   if (rows (x) == 1 || columns (x) == 1)
  75.  
  76.     if (nargin == 2)
  77.       if (isstr (p))
  78.         if (strcmp (p, "fro"))
  79.           retval = sqrt (sum (diag (x' * x)));
  80.         elseif (strcmp (p, "inf"))
  81.           retval = max (abs (x));
  82.         else
  83.           error ("norm: unrecognized norm");
  84.         endif
  85.       else
  86.     if (p == Inf)
  87.       retval = max (abs (x));
  88.     elseif (p == -Inf)
  89.       retval = min (abs (x));
  90.     else
  91.       retval = sum (abs (x) .^ p) ^ (1/p);
  92.     endif
  93.       endif
  94.     elseif (nargin == 1)
  95.       retval = sum (abs (x) .^ 2) ^ 0.5;
  96.     endif
  97.  
  98.   else
  99.  
  100.     if (nargin == 2)
  101.       if (isstr (p))
  102.         if (strcmp (p, "fro"))
  103.           retval = sqrt (sum (diag (x' * x)));
  104.         elseif (strcmp (p, "inf"))
  105.           retval = max (sum (abs (x')));
  106.         else
  107.           error ("norm: unrecognized norm");
  108.         endif
  109.       else
  110.         if (p == 1)
  111.           retval = max (sum (abs (x)));
  112.         elseif (p == 2)
  113.           s = svd (x);
  114.           retval = s (1);
  115.         elseif (p == Inf)
  116.           retval = max (sum (abs (x')));
  117.         endif
  118.       endif
  119.     elseif (nargin == 1)
  120.       s = svd (x);
  121.       retval = s (1);
  122.     endif
  123.  
  124.   endif
  125.  
  126. endfunction
  127.