changeset 11028:7d0d28aff7a9

simplify lcm
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 27 Sep 2010 21:08:16 +0200
parents a2bdd9180a3c
children 4ab04ea74b08
files scripts/ChangeLog scripts/elfun/lcm.m
diffstat 2 files changed, 20 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Mon Sep 27 19:38:16 2010 -0400
+++ b/scripts/ChangeLog	Mon Sep 27 21:08:16 2010 +0200
@@ -1,3 +1,7 @@
+2010-09-27  Jaroslav Hajek  <highegg@gmail.com>
+
+	* elfun/lcm.m: Simplify.
+
 2010-09-27  Ben Abbott <bpabbott@mac.com>
 
 	* plot/__print_parse_opts__.m: Properly crop eps image and fix tests.
--- a/scripts/elfun/lcm.m	Mon Sep 27 19:38:16 2010 -0400
+++ b/scripts/elfun/lcm.m	Mon Sep 27 21:08:16 2010 +0200
@@ -18,23 +18,10 @@
 ## <http://www.gnu.org/licenses/>.
 
 ## -*- texinfo -*-
-## @deftypefn  {Mapping Function} {} lcm (@var{x})
-## @deftypefnx {Mapping Function} {} lcm (@var{x}, @dots{})
-## Compute the least common multiple of the elements of @var{x}, or
-## of the list of all arguments.  For example,
-##
-## @example
-## lcm (a1, @dots{}, ak)
-## @end example
-##
-## @noindent
-## is the same as
-##
-## @example
-## lcm ([a1, @dots{}, ak]).
-## @end example
-##
-## All elements must be the same size or scalar.
+## @deftypefn  {Mapping Function} {} lcm (@var{x}, @var{y})
+## @deftypefnx {Mapping Function} {} lcm (@var{x}, @var{y}, @dots{})
+## Compute the least common multiple of @var{x} and @var{y},
+## or of the list of all arguments. All elements must be the same size or scalar.
 ## @seealso{factor, gcd}
 ## @end deftypefn
 
@@ -44,58 +31,27 @@
 
 function l = lcm (varargin)
 
-  if (nargin == 0)
-    print_usage ();
-  endif
-
-  if (nargin == 1)
-    a = varargin{1};
-
-    if (round (a) != a)
-      error ("lcm: all arguments must be integer");
+  if (nargin > 1)
+    if (common_size (varargin{:}) != 0)
+      error ("lcm: all args must be of the same size or scalar");
+    elseif (! all (cellfun (@isnumeric, varargin)))
+      error ("lcm: all arguments must be numeric");
     endif
 
-    if (any (a) == 0)
-      l = 0;
-    else
-      a = abs (a);
-      l = a (1);
-      for k = 1:(length (a) - 1)
-        l = l * a(k+1) / gcd (l, a(k+1));
-      endfor
-    endif
-  else
-    
     l = varargin{1};
-    sz = size (l);
-    nel = numel (l);
-
     for i = 2:nargin
-      a = varargin{i};
-
-      if (size (a) != sz)
-        if (nel == 1)
-          sz = size (a);
-          nel = numel (a);
-        elseif (numel (a) != 1)
-          error ("lcm: all arguments must be the same size or scalar");
-        endif
-      endif
-
-      if (round (a) != a)
-        error ("lcm: all arguments must be integer");
-      endif
-
-      idx = find (l == 0 || a == 0);
-      a = abs (a);
-      l = l .* a ./ gcd (l, a);
-      l(idx) = 0;
+      x = varargin{i};
+      msk = l == 0 & x == 0;
+      l .*= x ./ gcd (l, x);
+      l(msk) = 0;
     endfor
+  else
+    print_usage ();
   endif
 
 endfunction
 
-%!assert(lcm (3, 5, 7, 15) == lcm ([3, 5, 7, 15]) && lcm ([3, 5, 7,15]) == 105);
+%!assert(lcm (3, 5, 7, 15) == 105);
 
 %!error lcm ();