def test_mathtools_is_positive_integer_01(): assert mathtools.is_positive_integer(1) assert mathtools.is_positive_integer(int(1)) assert mathtools.is_positive_integer(Duration(1, 1)) assert mathtools.is_positive_integer(1.0) assert mathtools.is_positive_integer(True)
def __init__( self, number=None, ): if number is not None: assert mathtools.is_positive_integer(number) self._number = number
def _apply_preferred_denominator(self, selections, divisions): from abjad.tools import scoretools if not self.preferred_denominator: return tuplets = iterate(selections).by_class(scoretools.Tuplet) tuplets = list(tuplets) if divisions is None: divisions = len(tuplets) * [None] assert len(selections) == len(divisions) assert len(tuplets) == len(divisions) for tuplet, division in zip(tuplets, divisions): if self.preferred_denominator == 'divisions': tuplet.preferred_denominator = division.numerator elif isinstance( self.preferred_denominator, durationtools.Duration): unit_duration = self.preferred_denominator assert unit_duration.numerator == 1 duration = inspect_(tuplet).get_duration() denominator = unit_duration.denominator nonreduced_fraction = duration.with_denominator(denominator) tuplet.preferred_denominator = nonreduced_fraction.numerator elif mathtools.is_positive_integer(self.preferred_denominator): tuplet.preferred_denominator = self.preferred_denominator else: message = 'invalid value for preferred denominator: {!r}.' message = message.format(self.preferred_denominator) raise Exception(message)
def append_positive_integers(self, spaced_attribute_name, default_value=None): help_template = "value for {!r} must be positive integers." function = lambda expr: all(mathtools.is_positive_integer(x) for x in expr) self._make_prompt( spaced_attribute_name, validation_function=function, help_template=help_template, default_value=default_value, )
def __init__(self, number=None, markup=None): from abjad.tools import markuptools # TODO: make default scope work #self._default_scope = scoretools.Score if markup is not None: assert isinstance(markup, markuptools.Markup) self._markup = markup if number is not None: assert mathtools.is_positive_integer(number) self._number = number
def factors(n): r'''Prime factors less than or equal to positive integer `n` in increasing order. .. container:: example **Example 1.** Factors 84: :: >>> mathtools.factors(84) [2, 2, 3, 7] .. container:: example **Example 2.** Factors the number 10 through 19, inclusive: :: >>> for n in range(10, 20): ... print(n, mathtools.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] Raises type error on noninteger `n`. Raises value error on nonpositive `n`. Returns list of one or more positive integers. ''' from abjad.tools import mathtools if not mathtools.is_positive_integer(n): message = 'must be positive integer: {!r}.' message = message.format(n) raise TypeError(message) d = 2 factors = [] while 1 < n: if n % d == 0: factors.append(d) n = n / d else: d = d + 1 return factors
def factors(n): r'''Prime factors less than or equal to positive integer `n` in increasing order. .. container:: example **Example 1.** Factors 84: :: >>> mathtools.factors(84) [2, 2, 3, 7] .. container:: example **Example 2.** Factors the number 10 through 19, inclusive: :: >>> for n in range(10, 20): ... print(n, mathtools.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] Raises type error on noninteger `n`. Raises value error on nonpositive `n`. Returns list of one or more positive integers. ''' from abjad.tools import mathtools if not mathtools.is_positive_integer(n): message = 'must be positive integer: {!r}.' message = message.format(n) raise TypeError(message) d = 2 factors = [] while 1 < n: if n % d == 0: factors.append(d) n = n/d else: d = d + 1 return factors
def __init__( self, indices=None, period=None, ): if indices is not None: assert all(isinstance(_, int) for _ in indices), repr(indices) indices = tuple(indices) self._indices = indices if period is not None: assert mathtools.is_positive_integer(period), repr(period) self._period = period
def __init__( self, number: int = None, *, tweaks: typing.Union[ typing.List[typing.Tuple], LilyPondTweakManager] = None, ) -> None: if number is not None: assert mathtools.is_positive_integer(number) self._number = number self._lilypond_tweak_manager = None LilyPondTweakManager.set_tweaks(self, tweaks)
def yield_all_restricted_growth_functions_of_length(length): '''Yields all restricted growth functions of `length`. :: >>> for rgf in sequencetools.yield_all_restricted_growth_functions_of_length(4): ... rgf ... (1, 1, 1, 1) (1, 1, 1, 2) (1, 1, 2, 1) (1, 1, 2, 2) (1, 1, 2, 3) (1, 2, 1, 1) (1, 2, 1, 2) (1, 2, 1, 3) (1, 2, 2, 1) (1, 2, 2, 2) (1, 2, 2, 3) (1, 2, 3, 1) (1, 2, 3, 2) (1, 2, 3, 3) (1, 2, 3, 4) Returns restricted growth functions in lex order. Returns generator of tuples. ''' if not mathtools.is_positive_integer(length): raise TypeError last_rgf = list(range(1, length + 1)) rgf = length * [1] yield tuple(rgf) while not rgf == last_rgf: for i, x in enumerate(reversed(rgf)): if x < max(rgf[:-(i + 1)]) + 1: first_part = rgf[:-(i + 1)] increased_part = [rgf[-(i + 1)] + 1] trailing_ones = i * [1] rgf = first_part + increased_part + trailing_ones yield tuple(rgf) break
def factors(n): r'''Integer factors of positive integer `n` in increasing order. :: >>> mathtools.factors(84) [1, 2, 2, 3, 7] :: >>> for n in range(10, 20): ... print n, mathtools.factors(n) ... 10 [1, 2, 5] 11 [1, 11] 12 [1, 2, 2, 3] 13 [1, 13] 14 [1, 2, 7] 15 [1, 3, 5] 16 [1, 2, 2, 2, 2] 17 [1, 17] 18 [1, 2, 3, 3] 19 [1, 19] Raises type error on noninteger `n`. Raises value error on nonpositive `n`. Returns list of one or more positive integers. ''' from abjad.tools import mathtools if not mathtools.is_positive_integer(n): message = 'must be positive integer: {!r}.' message = message.format(n) raise TypeError(message) d = 2 factors = [1] while 1 < n: if n % d == 0: factors.append(d) n = n/d else: d = d + 1 return factors
def yield_all_restricted_growth_functions_of_length(length): '''Yields all restricted growth functions of `length`. :: >>> for rgf in sequencetools.yield_all_restricted_growth_functions_of_length(4): ... rgf ... (1, 1, 1, 1) (1, 1, 1, 2) (1, 1, 2, 1) (1, 1, 2, 2) (1, 1, 2, 3) (1, 2, 1, 1) (1, 2, 1, 2) (1, 2, 1, 3) (1, 2, 2, 1) (1, 2, 2, 2) (1, 2, 2, 3) (1, 2, 3, 1) (1, 2, 3, 2) (1, 2, 3, 3) (1, 2, 3, 4) Returns restricted growth functions in lex order. Returns generator of tuples. ''' if not mathtools.is_positive_integer(length): raise TypeError last_rgf = list(range(1, length + 1)) rgf = length * [1] yield tuple(rgf) while not rgf == last_rgf: for i, x in enumerate(reversed(rgf)): if x < max(rgf[:-(i+1)]) + 1: first_part = rgf[:-(i+1)] increased_part = [rgf[-(i+1)] + 1] trailing_ones = i * [1] rgf = first_part + increased_part + trailing_ones yield tuple(rgf) break
def append_positive_integers( self, spaced_attribute_name, default_value=None, ): r'''Appends positive integers. Returns prompt. ''' help_template = 'value must be tuple of positive integers.' function = lambda expr: all( mathtools.is_positive_integer(x) for x in expr) self._make_prompt( spaced_attribute_name, validation_function=function, help_template=help_template, default_value=default_value, )
def all_are_positive_integers(argument): '''Is true when `argument` is an iterable collection of positive integers. Otherwise false. .. container:: example >>> abjad.mathtools.all_are_positive_integers([1, 2, 3, 99]) True >>> abjad.mathtools.all_are_positive_integers(17) False Returns true or false. ''' from abjad.tools import mathtools try: return all(mathtools.is_positive_integer(_) for _ in argument) except TypeError: return False
def factors(n): r'''Gets prime factors less than or equal to `n` . .. container:: example >>> abjad.mathtools.factors(84) [2, 2, 3, 7] >>> for n in range(10, 20): ... print(n, abjad.mathtools.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. Returns list of positive integers. ''' from abjad.tools import mathtools if not mathtools.is_positive_integer(n): message = 'must be positive integer: {!r}.' message = message.format(n) raise TypeError(message) factor = 2 factors = [] while 1 < n: if n % factor == 0: factors.append(factor) n = n / factor else: factor = factor + 1 return factors
def all_are_positive_integers(expr): '''True when `expr` is a sequence and all elements in `expr` are positive integers: :: >>> sequencetools.all_are_positive_integers([1, 2, 3, 99]) True Otherwise false: :: >>> sequencetools.all_are_positive_integers(17) False Returns boolean. ''' try: return all(mathtools.is_positive_integer(x) for x in expr) except TypeError: return False
def all_are_positive_integers(expr): '''Is true when `expr` is a sequence and all elements in `expr` are positive integers. :: >>> mathtools.all_are_positive_integers([1, 2, 3, 99]) True Otherwise false: :: >>> mathtools.all_are_positive_integers(17) False Returns true or false. ''' from abjad.tools import mathtools try: return all(mathtools.is_positive_integer(x) for x in expr) except TypeError: return False
def _make_gridded_test_rhythm(grid_length, rhythm_number, denominator=16): r'''Make test rhythm number `rhythm_number` that fits `grid_length`. Returns selection of one or more possibly tied notes. .. container:: example **Example 1.** The eight test rhythms that fit a length-``4`` grid: :: >>> from abjad.tools.metertools import Meter >>> for rhythm_number in range(8): ... notes = Meter._make_gridded_test_rhythm( ... 4, rhythm_number, denominator=4) ... measure = Measure((4, 4), notes) ... print '{}\t{}'.format(rhythm_number, str(measure)) ... 0 |4/4 c'1| 1 |4/4 c'2. c'4| 2 |4/4 c'2 c'4 c'4| 3 |4/4 c'2 c'2| 4 |4/4 c'4 c'4 c'2| 5 |4/4 c'4 c'4 c'4 c'4| 6 |4/4 c'4 c'2 c'4| 7 |4/4 c'4 c'2.| .. container:: example **Example 2.** The sixteenth test rhythms for that a length-``5`` grid: :: >>> for rhythm_number in range(16): ... notes = Meter._make_gridded_test_rhythm( ... 5, rhythm_number, denominator=4) ... measure = Measure((5, 4), notes) ... print '{}\t{}'.format(rhythm_number, str(measure)) ... 0 |5/4 c'1 ~ c'4| 1 |5/4 c'1 c'4| 2 |5/4 c'2. c'4 c'4| 3 |5/4 c'2. c'2| 4 |5/4 c'2 c'4 c'2| 5 |5/4 c'2 c'4 c'4 c'4| 6 |5/4 c'2 c'2 c'4| 7 |5/4 c'2 c'2.| 8 |5/4 c'4 c'4 c'2.| 9 |5/4 c'4 c'4 c'2 c'4| 10 |5/4 c'4 c'4 c'4 c'4 c'4| 11 |5/4 c'4 c'4 c'4 c'2| 12 |5/4 c'4 c'2 c'2| 13 |5/4 c'4 c'2 c'4 c'4| 14 |5/4 c'4 c'2. c'4| 15 |5/4 c'4 c'1| Use for testing meter establishment. ''' from abjad.tools import scoretools # check input assert mathtools.is_positive_integer(grid_length) assert isinstance(rhythm_number, int) assert mathtools.is_positive_integer_power_of_two(denominator) # find count of all rhythms that fit grid length rhythm_count = 2 ** (grid_length - 1) # read rhythm number cyclically to allow large and # negative rhythm numbers rhythm_number = rhythm_number % rhythm_count # find binary representation of rhythm binary_representation = \ mathtools.integer_to_binary_string(rhythm_number) binary_representation = binary_representation.zfill(grid_length) # partition binary representation of rhythm parts = sequencetools.partition_sequence_by_value_of_elements( binary_representation) # find durations durations = [ durationtools.Duration(len(part), denominator) for part in parts ] # make notes notes = scoretools.make_notes([0], durations) # return notes return notes
def __init__(self, number=None): self._default_scope = None if number is not None: assert mathtools.is_positive_integer(number) self._number = number
def _make_gridded_test_rhythm(grid_length, rhythm_number, denominator=16): r'''Make test rhythm number `rhythm_number` that fits `grid_length`. Returns selection of one or more possibly tied notes. .. container:: example **Example 1.** The eight test rhythms that fit a length-``4`` grid: :: >>> from abjad.tools.metertools import Meter >>> for rhythm_number in range(8): ... notes = Meter._make_gridded_test_rhythm( ... 4, rhythm_number, denominator=4) ... measure = Measure((4, 4), notes) ... print('{}\t{}'.format(rhythm_number, str(measure))) ... 0 Measure((4, 4), "c'1") 1 Measure((4, 4), "c'2. c'4") 2 Measure((4, 4), "c'2 c'4 c'4") 3 Measure((4, 4), "c'2 c'2") 4 Measure((4, 4), "c'4 c'4 c'2") 5 Measure((4, 4), "c'4 c'4 c'4 c'4") 6 Measure((4, 4), "c'4 c'2 c'4") 7 Measure((4, 4), "c'4 c'2.") .. container:: example **Example 2.** The sixteenth test rhythms for that a length-``5`` grid: :: >>> for rhythm_number in range(16): ... notes = Meter._make_gridded_test_rhythm( ... 5, rhythm_number, denominator=4) ... measure = Measure((5, 4), notes) ... print('{}\t{}'.format(rhythm_number, str(measure))) ... 0 Measure((5, 4), "c'1 ~ c'4") 1 Measure((5, 4), "c'1 c'4") 2 Measure((5, 4), "c'2. c'4 c'4") 3 Measure((5, 4), "c'2. c'2") 4 Measure((5, 4), "c'2 c'4 c'2") 5 Measure((5, 4), "c'2 c'4 c'4 c'4") 6 Measure((5, 4), "c'2 c'2 c'4") 7 Measure((5, 4), "c'2 c'2.") 8 Measure((5, 4), "c'4 c'4 c'2.") 9 Measure((5, 4), "c'4 c'4 c'2 c'4") 10 Measure((5, 4), "c'4 c'4 c'4 c'4 c'4") 11 Measure((5, 4), "c'4 c'4 c'4 c'2") 12 Measure((5, 4), "c'4 c'2 c'2") 13 Measure((5, 4), "c'4 c'2 c'4 c'4") 14 Measure((5, 4), "c'4 c'2. c'4") 15 Measure((5, 4), "c'4 c'1") Use for testing meter establishment. ''' from abjad.tools import scoretools # check input assert mathtools.is_positive_integer(grid_length) assert isinstance(rhythm_number, int) assert mathtools.is_positive_integer_power_of_two(denominator) # find count of all rhythms that fit grid length rhythm_count = 2**(grid_length - 1) # read rhythm number cyclically to allow large and # negative rhythm numbers rhythm_number = rhythm_number % rhythm_count # find binary representation of rhythm binary_representation = \ mathtools.integer_to_binary_string(rhythm_number) binary_representation = binary_representation.zfill(grid_length) # partition binary representation of rhythm parts = sequencetools.partition_sequence_by_value_of_elements( binary_representation) # find durations durations = [ durationtools.Duration(len(part), denominator) for part in parts ] # make notes notes = scoretools.make_notes([0], durations) # return notes return notes
def test_mathtools_is_positive_integer_03(): assert not mathtools.is_positive_integer('foo')
def test_mathtools_is_positive_integer_02(): assert not mathtools.is_positive_integer(-99) assert not mathtools.is_positive_integer(0) assert not mathtools.is_positive_integer(False)