Mercurial > hg > octave-lyh
annotate scripts/specfun/lcm.m @ 14444:245963d3d628 stable
pkg: bug fix - accessing non-existent variable for error message
author | Miguel Bazdresch <lmb@2pif.info> |
---|---|
date | Thu, 01 Mar 2012 14:58:59 +0000 |
parents | 72c96de7a403 |
children | f3d52523cde1 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12931
diff
changeset
|
1 ## Copyright (C) 1994-2012 John W. Eaton |
2313 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
1026 | 18 |
3321 | 19 ## -*- texinfo -*- |
11028 | 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}, | |
11563
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
23 ## or of the list of all arguments. All elements must be the same size or |
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
24 ## scalar. |
9141
c1fff751b5a8
Update section 17.1 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
25 ## @seealso{factor, gcd} |
3321 | 26 ## @end deftypefn |
2311 | 27 |
5428 | 28 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312 | 29 ## Created: 16 September 1994 |
30 ## Adapted-By: jwe | |
31 | |
4870 | 32 function l = lcm (varargin) |
904 | 33 |
11028 | 34 if (nargin > 1) |
35 if (common_size (varargin{:}) != 0) | |
36 error ("lcm: all args must be of the same size or scalar"); | |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
12635
diff
changeset
|
37 elseif (! all (cellfun ("isnumeric", varargin))) |
11028 | 38 error ("lcm: all arguments must be numeric"); |
4870 | 39 endif |
2325 | 40 |
4870 | 41 l = varargin{1}; |
8507 | 42 for i = 2:nargin |
11028 | 43 x = varargin{i}; |
44 msk = l == 0 & x == 0; | |
45 l .*= x ./ gcd (l, x); | |
46 l(msk) = 0; | |
715 | 47 endfor |
11028 | 48 else |
49 print_usage (); | |
715 | 50 endif |
2325 | 51 |
715 | 52 endfunction |
7385 | 53 |
11028 | 54 %!assert(lcm (3, 5, 7, 15) == 105); |
7385 | 55 |
56 %!error lcm (); | |
57 | |
58 %!test | |
59 %! s.a = 1; | |
60 %! fail("lcm (s)"); | |
61 |