A set of auxiliary functions that are often used in PG problems.
This macro creates the following functions that are available for PG:
step($number)
ceil($number)
floor($number)
max(@listNumbers)
min(@listNumbers)
round($number)
lcm(@listNumbers)
gcf(@listNumbers)
gcd(@listNumbers)
isPrime($number)
reduce($numerator,$denominator)
preformat($scalar, "QuotedString")
random_pairwise_coprime($ar1, $ar2, ... )
random_coprime($ar1, $ar2, ... )
random_subset($n, @set)
repeated(@list)
Usage: step(x);
returns the step function (or Heaviside function) with jump at x=0. That is when x<0, it returns 0, when x>=0 the function returns 1.
Example:
step(3.14159) returns 1
Usage: ceil(x);
returns the ceiling function of x. This rounds up to the nearest integer.
Examples:
ceil(3.14159) returns 4
ceil(-9.75) return -9
Usage: floor(x);
returns the floor function of x. This rounds down to the nearest integer.
Examples:
floor(3.14159) returns 3
floor(-9.75) return -10
Usage: max(@arr);
returns the maximum of the values in the array @arr.
Example
max(1,2,3,4,5,6,7) returns 7
Usage: min(@arr);
returns the minimum of the values in the array @arr.
Example
min(1,2,3,4,5,6,7) returns 1
Usage: round(x);
returns integer nearest x.
Example:
round(3.14159) returns 3
Usage: Round(x);
returns integer nearest x.
Usage: Round(x,n);
returns the number rounded to n digits.
Example:
Round(1.789,2) returns 1.79
Usage: lcm(@arr);
returns the lowest common multiple of the array @arr of integers.
Example:
lcm(3,4,5,6) returns 60.
Note: it checks for an empty array, however doesn't check if the inputs are integers.
Usage: gcf(@arr);
returns the greatest common factor of the array @arr of integers.
Example:
gcf(20,30,45) returns 5.
Note: it checks for an empty array, however doesn't check if the inputs are integers.
Usage: gcd(@arr);
returns the greatest common divisor of the array @arr of integers.
Example:
gcd(20,30,45) returns 5.
Note: this is just an alias for gcf.
Usage: random_coprime(array of array_refs);
returns relatively prime integers. The arguments should be references to arrays of integers. This returns an n-tuple of relatively prime integers, each one coming from the corresponding array. Random selection is uniform among all possible tuples that are relatively prime. This does not consider (0,0) to be relatively prime.
This function may return an n-tuple where pairs are not coprime. This returns n-tuples where the largest (in absolute) common factor is 1.
In array context, returns an array. Otherwise, an array ref.
Examples:
random_coprime([1..9],[1..9]) may return (2,9) or (1,1) but not (6,8)
random_coprime([-9..-1,1..9],[1..9],[1..9]) may return (-3,7,4), (-1,1,1), or (-2,2,3) but not (-2,2,4)
Note: in the example above (-2,2,3) is valid because not all three share a factor greater than 1. If you don't want to allow pairs of numbers to have a common factor, see random_pairwise_coprime.
random_pairwise_coprime([-9..-1,1..9],[1..9],[1..9]) may return (-3,7,4) or (-1,1,1) but not (-2,2,3)
WARNING: random_coprime() will use a lot of memory and CPU resources if used with too many/too large arguments. For example, random_coprime([-20..20],[-20..20],[-20..20],[-20..20],[-20..20]) involves processing 41^5 arrays. Consider using random_pairwise_coprime() instead. Or breaking things up like: random_coprime([-20..20],[-20..20]),random_coprime([-20..20],[-20..20],[-20..20])
Note for Problem Authors: one reason for developing this function is to be able to create polynomials that don't have a constant factor. For example, if random_coprime([-9..-1,1..9],[1..9],[1..9]) returns (-5,5,3) then building the quadratic 3x^2+5x-5 doesn't lead to a constant multiple to be factored.
Usage: random_pairwise_coprime($arr);
This is similar to the random_coprime function with the additional constraint that all pairs of numbers are also coprime.
Examples:
random_coprime([-9..-1,1..9],[1..9],[1..9]) may return (-3,7,4), (-1,1,1), or (-2,2,3) but not (-2,2,4)
random_pairwise_coprime([-9..-1,1..9],[1..9],[1..9]) may return (-3,7,4) or (-1,1,1) but not (-2,2,3) or (3,5,6)
Usage: isPrime(n);
returns 1 if n is prime and 0 otherwise.
Example:
isPrime(7) returns 1.
isPrime(8) returns 0
Note: this doesn't check if n is negative.
Usage: reduce(num,den);
returns the fraction num/den as an array with first entry as the numerator and second as the denominator.
Example:
reduce(15,20) returns (3,4)
Usage: preFormat($scalar, "quoted string");
returns the string preformatted with the $scalar as 0, 1, or -1. takes a number and fixed object, as in "$a x" and formats
Example:
preformat(-1, "\pi") returns "-\pi"
Usage: random_subset($n, @set);
This function returns a randomly ordered array of $n elements selected from the array @set without replacement. Accepts either an array or an array reference for @set.
Example to choose 3 random elements (both do the same thing):
random_subset(3, 'first', 'second', 'third', 'fourth', 'fifth')
random_subset(3, ['first', 'second', 'third', 'fourth', 'fifth'])
Usage: repeated(@list)
This function returns a list of every repeated elements in @list. Comparison is made using ==, so two elements may be considered 'repeated' even when they are not literally equal.
Note that the function will return () if there are no repeated elements, which is false as a boolean. So !repeated(@list) is a way to check if the list has no repeated values.
Also note that generally if two items are equivalent, both will be in the returned list. However occasionally with MathObjects, x == y is true while y == x is false, and then only x (the one that makes the relation true when it is on the left) will be included in the returned list.