]> Creatis software - CreaPhase.git/blob - octave_packages/m/specfun/lcm.m
update packages
[CreaPhase.git] / octave_packages / m / specfun / lcm.m
1 ## Copyright (C) 1994-2012 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 3 of the License, or (at
8 ## your option) 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, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Mapping Function} {} lcm (@var{x}, @var{y})
21 ## @deftypefnx {Mapping Function} {} lcm (@var{x}, @var{y}, @dots{})
22 ## Compute the least common multiple of @var{x} and @var{y},
23 ## or of the list of all arguments.  All elements must be the same size or
24 ## scalar.
25 ## @seealso{factor, gcd}
26 ## @end deftypefn
27
28 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
29 ## Created: 16 September 1994
30 ## Adapted-By: jwe
31
32 function l = lcm (varargin)
33
34   if (nargin > 1)
35     if (common_size (varargin{:}) != 0)
36       error ("lcm: all args must be of the same size or scalar");
37     elseif (! all (cellfun ("isnumeric", varargin)))
38       error ("lcm: all arguments must be numeric");
39     endif
40
41     l = varargin{1};
42     for i = 2:nargin
43       x = varargin{i};
44       msk = l == 0 & x == 0;
45       l .*= x ./ gcd (l, x);
46       l(msk) = 0;
47     endfor
48   else
49     print_usage ();
50   endif
51
52 endfunction
53
54 %!assert(lcm (3, 5, 7, 15) == 105);
55
56 %!error lcm ();
57
58 %!test
59 %! s.a = 1;
60 %! fail("lcm (s)");
61