Mercurial > hg > octave-lyh
annotate scripts/elfun/lcm.m @ 9141:c1fff751b5a8
Update section 17.1 (Utility Functions) of arith.txi
Split section into "Exponents and Logarithms" and "Utility Functions"
Use Tex in many more of the doc strings for pretty printing in pdf format.
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Mon, 20 Apr 2009 17:16:09 -0700 |
parents | 1bf0ce0930be |
children | 8c71a86c4bf4 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2002, 2004, 2005, |
8920 | 2 ## 2006, 2007, 2008, 2009 John W. Eaton |
2313 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
2313 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
1026 | 19 |
3321 | 20 ## -*- texinfo -*- |
6547 | 21 ## @deftypefn {Mapping Function} {} lcm (@var{x}, @dots{}) |
7001 | 22 ## Compute the least common multiple of the elements of @var{x}, or |
9141
c1fff751b5a8
Update section 17.1 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
23 ## of the list of all arguments. For example, |
3426 | 24 ## |
3321 | 25 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
26 ## lcm (a1, @dots{}, ak) |
3321 | 27 ## @end example |
3426 | 28 ## |
3321 | 29 ## @noindent |
30 ## is the same as | |
3426 | 31 ## |
3321 | 32 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
33 ## lcm ([a1, @dots{}, ak]). |
3321 | 34 ## @end example |
5053 | 35 ## |
36 ## All elements must be the same size or scalar. | |
9141
c1fff751b5a8
Update section 17.1 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
37 ## @seealso{factor, gcd} |
3321 | 38 ## @end deftypefn |
2311 | 39 |
5428 | 40 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312 | 41 ## Created: 16 September 1994 |
42 ## Adapted-By: jwe | |
43 | |
4870 | 44 function l = lcm (varargin) |
904 | 45 |
2601 | 46 if (nargin == 0) |
6046 | 47 print_usage (); |
2601 | 48 endif |
49 | |
4870 | 50 if (nargin == 1) |
51 a = varargin{1}; | |
52 | |
53 if (round (a) != a) | |
54 error ("lcm: all arguments must be integer"); | |
55 endif | |
2325 | 56 |
4870 | 57 if (any (a) == 0) |
58 l = 0; | |
59 else | |
60 a = abs (a); | |
61 l = a (1); | |
62 for k = 1:(length (a) - 1) | |
63 l = l * a(k+1) / gcd (l, a(k+1)); | |
64 endfor | |
65 endif | |
66 else | |
67 | |
68 l = varargin{1}; | |
69 sz = size (l); | |
70 nel = numel (l); | |
71 | |
8507 | 72 for i = 2:nargin |
4870 | 73 a = varargin{i}; |
2325 | 74 |
4870 | 75 if (size (a) != sz) |
76 if (nel == 1) | |
77 sz = size (a); | |
78 nel = numel (a); | |
79 elseif (numel (a) != 1) | |
80 error ("lcm: all arguments must be the same size or scalar"); | |
81 endif | |
82 endif | |
83 | |
84 if (round (a) != a) | |
85 error ("lcm: all arguments must be integer"); | |
86 endif | |
87 | |
88 idx = find (l == 0 || a == 0); | |
89 a = abs (a); | |
90 l = l .* a ./ gcd (l, a); | |
91 l(idx) = 0; | |
715 | 92 endfor |
93 endif | |
2325 | 94 |
715 | 95 endfunction |
7385 | 96 |
97 %!assert(lcm (3, 5, 7, 15) == lcm ([3, 5, 7, 15]) && lcm ([3, 5, 7,15]) == 105); | |
98 | |
99 %!error lcm (); | |
100 | |
101 %!test | |
102 %! s.a = 1; | |
103 %! fail("lcm (s)"); | |
104 |