changeset 32271:e81b372d1203 stable

doc: Expand documentation for gcd() and lcm() gcd.cc: Add tip about num2cell for multiple inputs in the same array. lcm.m: Expand documentation, mention complex inputs, add tip about num2cell for multiple inputs in the same array.
author Arun Giridhar <arungiridhar@gmail.com>
date Wed, 23 Aug 2023 19:12:39 -0400
parents 9c4b4a66a62f
children b8d5428ed68b 2e8b8d38431c
files libinterp/corefcn/gcd.cc scripts/specfun/lcm.m
diffstat 2 files changed, 35 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/gcd.cc	Wed Aug 23 17:04:37 2023 -0400
+++ b/libinterp/corefcn/gcd.cc	Wed Aug 23 19:12:39 2023 -0400
@@ -473,6 +473,17 @@
 @end group
 @end example
 
+Programming tip: To find the GCD of all the elements of a single array, use
+@code{num2cell} instead of nested calls or a loop:
+
+@example
+@group
+x = [30 42 70 105];    # vector or array of inputs
+gcd (num2cell (x) @{:@})
+   @result{}     1
+@end group
+@end example
+
 @seealso{lcm, factor, isprime}
 @end deftypefn */)
 {
--- a/scripts/specfun/lcm.m	Wed Aug 23 17:04:37 2023 -0400
+++ b/scripts/specfun/lcm.m	Wed Aug 23 19:12:39 2023 -0400
@@ -29,7 +29,30 @@
 ## Compute the least common multiple of @var{x} and @var{y}, or of the list of
 ## all arguments.
 ##
-## All elements must be numeric and of the same size or scalar.
+## All inputs must be of the same size, or scalar.  All elements must be
+## real integer or Gaussian (complex) integer.  For complex inputs, the result
+## is unique only up to a phase factor (multiplication by +1, +i, -1, or -i),
+## and one of the four is returned arbitrarily.
+##
+## Example code:
+##
+## @example
+## @group
+## lcm (5:8, 9:12)
+##    @result{}  45  30  77  24
+## @end group
+## @end example
+##
+## Programming tip: To find the LCM of all the elements of a single array, use
+## @code{num2cell} instead of nested calls or a loop:
+##
+## @example
+## @group
+## x = 1:10;    # vector or array of inputs
+## lcm (num2cell (x) @{:@})
+##    @result{}     2520
+## @end group
+## @end example
 ## @seealso{factor, gcd, isprime}
 ## @end deftypefn