Mercurial > octave-nkf
annotate scripts/elfun/lcm.m @ 10549:95c3e38098bf
Untabify .m scripts
author | Rik <code@nomad.inbox5.com> |
---|---|
date | Fri, 23 Apr 2010 11:28:50 -0700 |
parents | 8c71a86c4bf4 |
children | 7d0d28aff7a9 |
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 -*- |
9165
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
diff
changeset
|
21 ## @deftypefn {Mapping Function} {} lcm (@var{x}) |
8c71a86c4bf4
Update section 17.5 (Utility Functions) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
9141
diff
changeset
|
22 ## @deftypefnx {Mapping Function} {} lcm (@var{x}, @dots{}) |
7001 | 23 ## 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
|
24 ## of the list of all arguments. For example, |
3426 | 25 ## |
3321 | 26 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
27 ## lcm (a1, @dots{}, ak) |
3321 | 28 ## @end example |
3426 | 29 ## |
3321 | 30 ## @noindent |
31 ## is the same as | |
3426 | 32 ## |
3321 | 33 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
34 ## lcm ([a1, @dots{}, ak]). |
3321 | 35 ## @end example |
5053 | 36 ## |
37 ## 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
|
38 ## @seealso{factor, gcd} |
3321 | 39 ## @end deftypefn |
2311 | 40 |
5428 | 41 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312 | 42 ## Created: 16 September 1994 |
43 ## Adapted-By: jwe | |
44 | |
4870 | 45 function l = lcm (varargin) |
904 | 46 |
2601 | 47 if (nargin == 0) |
6046 | 48 print_usage (); |
2601 | 49 endif |
50 | |
4870 | 51 if (nargin == 1) |
52 a = varargin{1}; | |
53 | |
54 if (round (a) != a) | |
55 error ("lcm: all arguments must be integer"); | |
56 endif | |
2325 | 57 |
4870 | 58 if (any (a) == 0) |
59 l = 0; | |
60 else | |
61 a = abs (a); | |
62 l = a (1); | |
63 for k = 1:(length (a) - 1) | |
10549 | 64 l = l * a(k+1) / gcd (l, a(k+1)); |
4870 | 65 endfor |
66 endif | |
67 else | |
68 | |
69 l = varargin{1}; | |
70 sz = size (l); | |
71 nel = numel (l); | |
72 | |
8507 | 73 for i = 2:nargin |
4870 | 74 a = varargin{i}; |
2325 | 75 |
4870 | 76 if (size (a) != sz) |
10549 | 77 if (nel == 1) |
78 sz = size (a); | |
79 nel = numel (a); | |
80 elseif (numel (a) != 1) | |
81 error ("lcm: all arguments must be the same size or scalar"); | |
82 endif | |
4870 | 83 endif |
84 | |
85 if (round (a) != a) | |
10549 | 86 error ("lcm: all arguments must be integer"); |
4870 | 87 endif |
88 | |
89 idx = find (l == 0 || a == 0); | |
90 a = abs (a); | |
91 l = l .* a ./ gcd (l, a); | |
92 l(idx) = 0; | |
715 | 93 endfor |
94 endif | |
2325 | 95 |
715 | 96 endfunction |
7385 | 97 |
98 %!assert(lcm (3, 5, 7, 15) == lcm ([3, 5, 7, 15]) && lcm ([3, 5, 7,15]) == 105); | |
99 | |
100 %!error lcm (); | |
101 | |
102 %!test | |
103 %! s.a = 1; | |
104 %! fail("lcm (s)"); | |
105 |