math¶
Abjad’s math library.
Classes
Infinity. |
|
Negative infinity. |
- class abjad.math.Infinity[source]¶
Infinity.
All numbers compare less than infinity:
>>> 9999999 < abjad.Infinity() True
>>> 2**38 < abjad.Infinity() True
Infinity compares equal to itself:
>>> abjad.Infinity() == abjad.Infinity() True
Negative infinity compares less than infinity:
>>> abjad.NegativeInfinity() < abjad.Infinity() True
Initializes as a system singleton at start-up.
Available as a built-in after Abjad starts.
Attributes Summary
Compares type.
Convert infinity to float.
Is true for all values of
argument
.Is true for all noninfinite values of
argument
.Hashes infinity.
Is true when
argument
is infinite.Is true for no values of
argument
.Gets repr.
Subtracts
argument
from infinity.Special methods
- class abjad.math.NegativeInfinity[source]¶
Negative infinity.
All numbers compare greater than negative infinity:
>>> abjad.NegativeInfinity() < -9999999 True
Negative infinity compares equal to itself:
>>> abjad.NegativeInfinity() == abjad.NegativeInfinity() True
Negative infinity compares less than infinity:
>>> abjad.NegativeInfinity() < abjad.Infinity() True
Initializes as a system singleton at start-up.
Available as a built-in after Abjad start.
Special methods
Functions
Is true when |
|
Is true when |
|
Is true when |
|
Is true when |
|
Is true when |
|
Is true when |
|
Is true when |
|
Is true when |
|
Is true when |
|
Gets arithmetic mean of |
|
Gets binomial coefficient of |
|
Gets cumulative products of |
|
Gets cumulative sums of |
|
Gets difference series of |
|
Divides integer |
|
Gets positive divisors of |
|
Gets prime factors less than or equal to |
|
Changes |
|
Calculates greatest common divisor of |
|
Gets greatest integer power of two less than or equal to positive |
|
Changes integer-equivalent |
|
Changes nonnegative integer |
|
Changes positive integer |
|
Is true when |
|
Is true when |
|
Is true when |
|
Is true when |
|
Is true when |
|
Is true when |
|
Is true when |
|
Is true when |
|
Is true when |
|
Is true when |
|
Gets least common multiple of positive |
|
Partitions positive integer-equivalent |
|
Partitions integer |
|
Gets sign of |
|
Gets weight of |
|
Yields all compositions of positive integer |
- abjad.math.all_are_equal(argument)[source]¶
Is true when
argument
is an iterable collection of equal items.>>> abjad.math.all_are_equal([99, 99, 99, 99, 99, 99]) True
>>> abjad.math.all_are_equal(17) False
Is true when
argument
is empty:>>> abjad.math.all_are_equal([]) True
- Return type:
- abjad.math.all_are_integer_equivalent(argument)[source]¶
Is true when
argument
is an iterable collection with integer-equivalent items.>>> import fractions >>> items = [1, "2", 3.0, fractions.Fraction(4, 1)] >>> abjad.math.all_are_integer_equivalent(items) True
>>> abjad.math.all_are_integer_equivalent([1, "2", 3.5, 4]) False
- Return type:
- abjad.math.all_are_integer_equivalent_numbers(argument)[source]¶
Is true when
argument
is an iterable collection with integer-equivalent items.>>> import fractions >>> items = [1, 2, 3.0, fractions.Fraction(4, 1)] >>> abjad.math.all_are_integer_equivalent_numbers(items) True
>>> abjad.math.all_are_integer_equivalent_numbers([1, 2, 3.5, 4]) False
- Return type:
- abjad.math.all_are_nonnegative_integer_equivalent_numbers(argument)[source]¶
Is true when
argument
is an iterable collection of nonnegative integer-equivalent numbers.>>> import fractions >>> items = [0, 0.0, fractions.Fraction(0), 2, 2.0, fractions.Fraction(2)] >>> abjad.math.all_are_nonnegative_integer_equivalent_numbers(items) True
>>> items = [0, 0.0, fractions.Fraction(0), -2, 2.0, fractions.Fraction(2)] >>> abjad.math.all_are_nonnegative_integer_equivalent_numbers(items) False
- Return type:
- abjad.math.all_are_nonnegative_integer_powers_of_two(argument)[source]¶
Is true when
argument
is an iterable collection of nonnegative integer powers of two.>>> items = [0, 1, 1, 1, 2, 4, 32, 32] >>> abjad.math.all_are_nonnegative_integer_powers_of_two(items) True
>>> abjad.math.all_are_nonnegative_integer_powers_of_two(17) False
Is true when
argument
is empty:>>> abjad.math.all_are_nonnegative_integer_powers_of_two([]) True
- Return type:
- abjad.math.all_are_nonnegative_integers(argument)[source]¶
Is true when
argument
is an iterable collection of nonnegative integers.>>> abjad.math.all_are_nonnegative_integers([0, 1, 2, 99]) True
>>> abjad.math.all_are_nonnegative_integers([0, 1, 2, -99]) False
- Return type:
- abjad.math.all_are_pairs_of_types(argument, first_type, second_type)[source]¶
Is true when
argument
is an iterable collection whose members are all of length 2, and where the first member of each pair is an instance offirst_type
and where the second member of each pair is an instance ofsecond_type
.>>> items = [(1.0, "a"), (2.1, "b"), (3.45, "c")] >>> abjad.math.all_are_pairs_of_types(items, float, str) True
>>> abjad.math.all_are_pairs_of_types("foo", float, str) False
Is true when
argument
is empty:>>> abjad.math.all_are_pairs_of_types([], float, str) True
- Return type:
- abjad.math.all_are_positive_integers(argument)[source]¶
Is true when
argument
is an iterable collection of positive integers.>>> abjad.math.all_are_positive_integers([1, 2, 3, 99]) True
>>> abjad.math.all_are_positive_integers(17) False
- Return type:
- abjad.math.are_relatively_prime(argument)[source]¶
Is true when
argument
is an iterable collection of relative primes.>>> abjad.math.are_relatively_prime([13, 14, 15]) True
>>> abjad.math.are_relatively_prime([13, 14, 15, 16]) False
>>> abjad.math.are_relatively_prime("text") False
Returns true when
argument
is empty:>>> abjad.math.are_relatively_prime([]) True
- Return type:
- abjad.math.arithmetic_mean(argument)[source]¶
Gets arithmetic mean of
argument
.>>> abjad.math.arithmetic_mean([1, 2, 2, 20, 30]) 11
>>> abjad.math.arithmetic_mean([1, 2, 20]) Fraction(23, 3)
>>> abjad.math.arithmetic_mean([2, 2, 20.0]) 8.0
Raises exception when
argument
is not iterable.
- abjad.math.binomial_coefficient(n, k)[source]¶
Gets binomial coefficient of
n
choosek
.>>> for k in range(8): ... print(k, " ", abjad.math.binomial_coefficient(8, k)) ... 0 1 1 8 2 28 3 56 4 70 5 56 6 28 7 8
- Return type:
- abjad.math.cumulative_products(argument)[source]¶
Gets cumulative products of
argument
.>>> abjad.math.cumulative_products([1, 2, 3, 4, 5, 6, 7, 8]) [1, 2, 6, 24, 120, 720, 5040, 40320]
>>> abjad.math.cumulative_products([1, -2, 3, -4, 5, -6, 7, -8]) [1, -2, -6, 24, 120, -720, -5040, 40320]
Raises exception when
argument
is not iterable.Returns new object of
argument
type.
- abjad.math.cumulative_sums(argument, start=0)[source]¶
Gets cumulative sums of
argument
.>>> abjad.math.cumulative_sums([1, 2, 3, 4, 5, 6, 7, 8], start=0) [0, 1, 3, 6, 10, 15, 21, 28, 36]
>>> abjad.math.cumulative_sums([1, 2, 3, 4, 5, 6, 7, 8], start=None) [1, 3, 6, 10, 15, 21, 28, 36]
Raises exception when
argument
is not iterable.Returns new object of
argument
type.
- abjad.math.difference_series(argument)[source]¶
Gets difference series of
argument
.>>> abjad.math.difference_series([1, 1, 2, 3, 5, 5, 6]) [0, 1, 1, 2, 0, 1]
>>> abjad.math.difference_series([9, 6, 8, 5, 7, 4, 6]) [-3, 2, -3, 2, -3, 2]
Returns new object of
argument
type.
- abjad.math.divide_integer_by_ratio(n, ratio)[source]¶
Divides integer
n
by tupleratio
.>>> abjad.math.divide_integer_by_ratio(1, (1, 1, 3)) [Fraction(1, 5), Fraction(1, 5), Fraction(3, 5)]
>>> abjad.math.divide_integer_by_ratio(1.0, (1, 1, 3)) [0.2, 0.2, 0.6]
- abjad.math.divisors(n)[source]¶
Gets positive divisors of
n
in increasing order.>>> abjad.math.divisors(84) [1, 2, 3, 4, 6, 7, 12, 14, 21, 28, 42, 84]
>>> for x in range(10, 20): ... print(x, abjad.math.divisors(x)) ... 10 [1, 2, 5, 10] 11 [1, 11] 12 [1, 2, 3, 4, 6, 12] 13 [1, 13] 14 [1, 2, 7, 14] 15 [1, 3, 5, 15] 16 [1, 2, 4, 8, 16] 17 [1, 17] 18 [1, 2, 3, 6, 9, 18] 19 [1, 19]
Allows nonpositive
n
:>>> abjad.math.divisors(-27) [1, 3, 9, 27]
Raises not implemented error on
0
.
- abjad.math.factors(n)[source]¶
Gets prime factors less than or equal to
n
.>>> abjad.math.factors(84) [2, 2, 3, 7]
>>> for n in range(10, 20): ... print(n, abjad.math.factors(n)) ... 10 [2, 5] 11 [11] 12 [2, 2, 3] 13 [13] 14 [2, 7] 15 [3, 5] 16 [2, 2, 2, 2] 17 [17] 18 [2, 3, 3] 19 [19]
n
must be a positive integer.Returns factors in increasing order.
- abjad.math.fraction_to_proper_fraction(rational)[source]¶
Changes
rational
to proper fraction.>>> import fractions >>> abjad.math.fraction_to_proper_fraction(fractions.Fraction(116, 8)) (14, Fraction(1, 2))
- abjad.math.greatest_common_divisor(*integers)[source]¶
Calculates greatest common divisor of
integers
.>>> abjad.math.greatest_common_divisor(84, -94, -144) 2
Allows nonpositive input.
Raises not implemented error when zero is included in input.
- Return type:
- abjad.math.greatest_power_of_two_less_equal(n, i=0)[source]¶
Gets greatest integer power of two less than or equal to positive
n
.>>> for n in range(10, 20): ... print("\t%s\t%s" % (n, abjad.math.greatest_power_of_two_less_equal(n))) ... 10 8 11 8 12 8 13 8 14 8 15 8 16 16 17 16 18 16 19 16
Greatest-but-
i
integer power of2
less than or equal to positiven
:>>> for n in range(10, 20): ... print("\t%s\t%s" % (n, abjad.math.greatest_power_of_two_less_equal(n, i=1))) ... 10 4 11 4 12 4 13 4 14 4 15 4 16 8 17 8 18 8 19 8
- Return type:
- abjad.math.integer_equivalent_number_to_integer(number)[source]¶
Changes integer-equivalent
number
to integer.Returns integer-equivalent number as integer:
>>> abjad.math.integer_equivalent_number_to_integer(17.0) 17
Returns noninteger-equivalent number unchanged:
>>> abjad.math.integer_equivalent_number_to_integer(17.5) 17.5
- abjad.math.integer_to_base_k_tuple(n, k)[source]¶
Changes nonnegative integer
n
to base-k tuple.Gets base-10 digits of 1066:
>>> abjad.math.integer_to_base_k_tuple(1066, 10) (1, 0, 6, 6)
Gets base-2 digits of 1066:
>>> abjad.math.integer_to_base_k_tuple(1066, 2) (1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0)
Gets base-26 digits of 1066:
>>> abjad.math.integer_to_base_k_tuple(1066, 26) (1, 15, 0)
- abjad.math.integer_to_binary_string(n)[source]¶
Changes positive integer
n
to binary string.>>> for n in range(1, 16 + 1): ... string = abjad.math.integer_to_binary_string(n) ... print(f"{n}\t{string}") ... 1 1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 9 1001 10 1010 11 1011 12 1100 13 1101 14 1110 15 1111 16 10000
- Return type:
- abjad.math.is_assignable_integer(argument)[source]¶
Is true when
argument
is equivalent to an integer that can be written without recourse to ties.>>> for n in range(0, 16 + 1): ... print("%s\t%s" % (n, abjad.math.is_assignable_integer(n))) ... 0 False 1 True 2 True 3 True 4 True 5 False 6 True 7 True 8 True 9 False 10 False 11 False 12 True 13 False 14 True 15 True 16 True
- Return type:
- abjad.math.is_integer_equivalent(argument)[source]¶
Is true when
argument
is an integer-equivalent number.>>> abjad.math.is_integer_equivalent(12.0) True
>>> abjad.math.is_integer_equivalent("12") True
>>> abjad.math.is_integer_equivalent("foo") False
- Return type:
- abjad.math.is_integer_equivalent_n_tuple(argument, n)[source]¶
Is true when
argument
is a tuple ofn
integer-equivalent items.>>> import fractions >>> tuple_ = (2.0, "3", fractions.Fraction(4, 1)) >>> abjad.math.is_integer_equivalent_n_tuple(tuple_, 3) True
>>> tuple_ = (2.5, "3", fractions.Fraction(4, 1)) >>> abjad.math.is_integer_equivalent_n_tuple(tuple_, 3) False
- Return type:
- abjad.math.is_integer_equivalent_number(argument)[source]¶
Is true when
argument
is a number andargument
is equivalent to an integer.>>> abjad.math.is_integer_equivalent_number(12.0) True
>>> abjad.math.is_integer_equivalent_number(abjad.Duration(1, 2)) False
- Return type:
- abjad.math.is_nonnegative_integer(argument)[source]¶
Is true when
argument
equals a nonnegative integer.>>> abjad.math.is_nonnegative_integer(99) True
>>> abjad.math.is_nonnegative_integer(0) True
>>> abjad.math.is_nonnegative_integer(-1) False
- Return type:
- abjad.math.is_nonnegative_integer_equivalent_number(argument)[source]¶
Is true when
argument
is a nonnegative integer-equivalent number.>>> duration = abjad.Duration(4, 2) >>> abjad.math.is_nonnegative_integer_equivalent_number(duration) True
- Return type:
- abjad.math.is_nonnegative_integer_power_of_two(argument)[source]¶
Is true when
argument
is a nonnegative integer power of 2.>>> for n in range(10): ... print(n, abjad.math.is_nonnegative_integer_power_of_two(n)) ... 0 True 1 True 2 True 3 False 4 True 5 False 6 False 7 False 8 True 9 False
- Return type:
- abjad.math.is_positive_integer(argument)[source]¶
Is true when
argument
equals a positive integer.>>> abjad.math.is_positive_integer(99) True
>>> abjad.math.is_positive_integer(0) False
>>> abjad.math.is_positive_integer(-1) False
- Return type:
- abjad.math.is_positive_integer_equivalent_number(argument)[source]¶
Is true when
argument
is a positive integer-equivalent number.>>> abjad.math.is_positive_integer_equivalent_number(abjad.Duration(4, 2)) True
- Return type:
- abjad.math.is_positive_integer_power_of_two(argument)[source]¶
Is true when
argument
is a positive integer power of 2.>>> for n in range(10): ... print(n, abjad.math.is_positive_integer_power_of_two(n)) ... 0 False 1 True 2 True 3 False 4 True 5 False 6 False 7 False 8 True 9 False
- Return type:
- abjad.math.least_common_multiple(*integers)[source]¶
Gets least common multiple of positive
integers
.>>> abjad.math.least_common_multiple(2, 4, 5, 10, 20) 20
>>> abjad.math.least_common_multiple(4, 4) 4
>>> abjad.math.least_common_multiple(4, 5) 20
>>> abjad.math.least_common_multiple(4, 6) 12
>>> abjad.math.least_common_multiple(4, 7) 28
>>> abjad.math.least_common_multiple(4, 8) 8
>>> abjad.math.least_common_multiple(4, 9) 36
>>> abjad.math.least_common_multiple(4, 10) 20
>>> abjad.math.least_common_multiple(4, 11) 44
- Return type:
- abjad.math.partition_integer_by_ratio(n, ratio)[source]¶
Partitions positive integer-equivalent
n
byratio
.Returns result with weight equal to absolute value of
n
.>>> abjad.math.partition_integer_by_ratio(10, (1, 2)) [3, 7]
Partitions positive integer-equivalent
n
byratio
with negative parts:>>> abjad.math.partition_integer_by_ratio(10, (1, -2)) [3, -7]
Partitions negative integer-equivalent
n
byratio
:>>> abjad.math.partition_integer_by_ratio(-10, (1, 2)) [-3, -7]
Partitions negative integer-equivalent
n
byratio
with negative parts:>>> abjad.math.partition_integer_by_ratio(-10, (1, -2)) [-3, 7]
More examples:
>>> abjad.math.partition_integer_by_ratio(10, (1,)) [10]
>>> abjad.math.partition_integer_by_ratio(10, (1, 1)) [5, 5]
>>> abjad.math.partition_integer_by_ratio(10, (1, -1, -1)) [3, -4, -3]
>>> abjad.math.partition_integer_by_ratio(-10, (1, 1, 1, 1)) [-3, -2, -3, -2]
>>> abjad.math.partition_integer_by_ratio(-10, (1, 1, 1, 1, 1)) [-2, -2, -2, -2, -2]
- abjad.math.partition_integer_into_canonic_parts(n, decrease_parts_monotonically=True)[source]¶
Partitions integer
n
into canonic parts.Returns all parts positive on positive
n
:>>> for n in range(1, 11): ... print(n, abjad.math.partition_integer_into_canonic_parts(n)) ... 1 (1,) 2 (2,) 3 (3,) 4 (4,) 5 (4, 1) 6 (6,) 7 (7,) 8 (8,) 9 (8, 1) 10 (8, 2)
Returns all parts negative on negative
n
:>>> for n in reversed(range(-20, -10)): ... print(n, abjad.math.partition_integer_into_canonic_parts(n)) ... -11 (-8, -3) -12 (-12,) -13 (-12, -1) -14 (-14,) -15 (-15,) -16 (-16,) -17 (-16, -1) -18 (-16, -2) -19 (-16, -3) -20 (-16, -4)
Returns parts that increase monotonically:
>>> for n in range(11, 21): ... print( ... n, ... abjad.math.partition_integer_into_canonic_parts( ... n, decrease_parts_monotonically=False ... ), ... ) ... 11 (3, 8) 12 (12,) 13 (1, 12) 14 (14,) 15 (15,) 16 (16,) 17 (1, 16) 18 (2, 16) 19 (3, 16) 20 (4, 16)
Returns tuple with parts that decrease monotonically.
- abjad.math.sign(n)[source]¶
Gets sign of
n
.Returns -1 on negative
n
:>>> abjad.math.sign(-96.2) -1
Returns 0 when
n
is 0:>>> abjad.math.sign(0) 0
Returns 1 on positive
n
:>>> abjad.math.sign(abjad.Duration(9, 8)) 1
- Return type:
- abjad.math.weight(argument)[source]¶
Gets weight of
argument
.>>> abjad.math.weight([-1, -2, 3, 4, 5]) 15
>>> abjad.math.weight([]) 0
Defined equal to sum of the absolute value of items in
argument
.- Return type:
- abjad.math.yield_all_compositions_of_integer(n)[source]¶
Yields all compositions of positive integer
n
.>>> for tuple_ in abjad.math.yield_all_compositions_of_integer(5): ... tuple_ ... (5,) (4, 1) (3, 2) (3, 1, 1) (2, 3) (2, 2, 1) (2, 1, 2) (2, 1, 1, 1) (1, 4) (1, 3, 1) (1, 2, 2) (1, 2, 1, 1) (1, 1, 3) (1, 1, 2, 1) (1, 1, 1, 2) (1, 1, 1, 1, 1)
Lists parts in descending lex order.
Parts sum to
n
.Finds small values of
n
easily.Takes around 4 seconds for
n
equal to 17.