Esempio n. 1
0
def test_mathtools_is_positive_integer_equivalent_number_02():

    assert not mathtools.is_positive_integer_equivalent_number(0)
    assert not mathtools.is_positive_integer_equivalent_number(-2)
    assert not mathtools.is_positive_integer_equivalent_number(Duration(5, 2))
    assert not mathtools.is_positive_integer_equivalent_number(2.5)
    assert not mathtools.is_positive_integer_equivalent_number('foo')
def partition_integer_into_parts_less_than_double(n, m):
    r'''Partitions integer `n` into parts less than double integer `m`.

    ..  container:: example

        **Example:**

        ::

            >>> for n in range(1, 24+1):
            ...     print n, mathtools.partition_integer_into_parts_less_than_double(n, 4)
            1 (1,)
            2 (2,)
            3 (3,)
            4 (4,)
            5 (5,)
            6 (6,)
            7 (7,)
            8 (4, 4)
            9 (4, 5)
            10 (4, 6)
            11 (4, 7)
            12 (4, 4, 4)
            13 (4, 4, 5)
            14 (4, 4, 6)
            15 (4, 4, 7)
            16 (4, 4, 4, 4)
            17 (4, 4, 4, 5)
            18 (4, 4, 4, 6)
            19 (4, 4, 4, 7)
            20 (4, 4, 4, 4, 4)
            21 (4, 4, 4, 4, 5)
            22 (4, 4, 4, 4, 6)
            23 (4, 4, 4, 4, 7)
            24 (4, 4, 4, 4, 4, 4)

    Returns tuple of one or more integers.
    '''
    from abjad.tools import mathtools

    # check input
    assert mathtools.is_positive_integer_equivalent_number(n)
    assert mathtools.is_positive_integer_equivalent_number(m)
    n, m = int(n), int(m)

    # initialize values
    result = []
    current_value = n
    double_m = 2 * m

    # partition n
    while double_m <= current_value:
        result.append(m)
        current_value -= m
    result.append(current_value)

    # return result
    return tuple(result)
Esempio n. 3
0
def partition_integer_into_parts_less_than_double(n, m):
    r'''Partitions integer `n` into parts less than double integer `m`.

    ..  container:: example

        ::

            >>> for n in range(1, 24+1):
            ...     print(n, mathtools.partition_integer_into_parts_less_than_double(n, 4))
            1 (1,)
            2 (2,)
            3 (3,)
            4 (4,)
            5 (5,)
            6 (6,)
            7 (7,)
            8 (4, 4)
            9 (4, 5)
            10 (4, 6)
            11 (4, 7)
            12 (4, 4, 4)
            13 (4, 4, 5)
            14 (4, 4, 6)
            15 (4, 4, 7)
            16 (4, 4, 4, 4)
            17 (4, 4, 4, 5)
            18 (4, 4, 4, 6)
            19 (4, 4, 4, 7)
            20 (4, 4, 4, 4, 4)
            21 (4, 4, 4, 4, 5)
            22 (4, 4, 4, 4, 6)
            23 (4, 4, 4, 4, 7)
            24 (4, 4, 4, 4, 4, 4)

    Returns tuple of one or more integers.
    '''
    from abjad.tools import mathtools

    # check input
    assert mathtools.is_positive_integer_equivalent_number(n)
    assert mathtools.is_positive_integer_equivalent_number(m)
    n, m = int(n), int(m)

    # initialize values
    result = []
    current_value = n
    double_m = 2 * m

    # partition n
    while double_m <= current_value:
        result.append(m)
        current_value -= m
    result.append(current_value)

    # return result
    return tuple(result)
def all_are_positive_integer_equivalent_numbers(expr):
    '''True when `expr` is a sequence and all elements in `expr` are positive
    integer-equivalent numbers. Otherwise false:

    ::

        >>> sequencetools.all_are_positive_integer_equivalent_numbers([Fraction(4, 2), 2.0, 2])
        True

    Returns boolean.
    '''

    try:
        return all(mathtools.is_positive_integer_equivalent_number(x) for x in expr)
    except TypeError:
        return False
Esempio n. 5
0
def all_are_positive_integer_equivalent_numbers(expr):
    '''Is true when `expr` is a sequence and all elements in `expr` are
    positive integer-equivalent numbers. Otherwise false:

    ::

        >>> mathtools.all_are_positive_integer_equivalent_numbers([Fraction(4, 2), 2.0, 2])
        True

    Returns boolean.
    '''
    from abjad.tools import mathtools

    try:
        return all(mathtools.is_positive_integer_equivalent_number(x) for x in expr)
    except TypeError:
        return False
Esempio n. 6
0
def test_mathtools_is_positive_integer_equivalent_number_01():

    assert mathtools.is_positive_integer_equivalent_number(Duration(4, 2))
    assert mathtools.is_positive_integer_equivalent_number(2.0)
    assert mathtools.is_positive_integer_equivalent_number(2)
    def divide_by_ratio(self, ratio):
        r"""Divide timespan expression by `ratio`:

        ::

            >>> timespans = red_segment.timespan.divide_by_ratio((2, 3))

        ::

            >>> print timespans[0].storage_format
            musicexpressiontools.TimespanExpression(
                anchor='red',
                callbacks=musicexpressiontools.CallbackInventory([
                    'self._divide_by_ratio(original_start_offset, original_stop_offset, (2, 3), 0)'
                    ])
                )

        ::

            >>> print timespans[1].storage_format
            musicexpressiontools.TimespanExpression(
                anchor='red',
                callbacks=musicexpressiontools.CallbackInventory([
                    'self._divide_by_ratio(original_start_offset, original_stop_offset, (2, 3), 1)'
                    ])
                )

        Coerce integer `ratio` to ``Ratio(ratio*[1])``:

        ::

            >>> timespans = red_segment.timespan.divide_by_ratio(3)

        ::

            >>> print timespans[0].storage_format
            musicexpressiontools.TimespanExpression(
                anchor='red',
                callbacks=musicexpressiontools.CallbackInventory([
                    'self._divide_by_ratio(original_start_offset, original_stop_offset, [1, 1, 1], 0)'
                    ])
                )

        ::

            >>> print timespans[1].storage_format
            musicexpressiontools.TimespanExpression(
                anchor='red',
                callbacks=musicexpressiontools.CallbackInventory([
                    'self._divide_by_ratio(original_start_offset, original_stop_offset, [1, 1, 1], 1)'
                    ])
                )

        ::

            >>> print timespans[2].storage_format
            musicexpressiontools.TimespanExpression(
                anchor='red',
                callbacks=musicexpressiontools.CallbackInventory([
                    'self._divide_by_ratio(original_start_offset, original_stop_offset, [1, 1, 1], 2)'
                    ])
                )

        Returns tuple of newly constructed timespans with callback.
        """
        result = []
        if mathtools.is_positive_integer_equivalent_number(ratio):
            ratio = int(ratio) * [1]
        for part in range(len(ratio)):
            callback = "self._divide_by_ratio(original_start_offset, original_stop_offset, {!r}, {!r})"
            callback = callback.format(ratio, part)
            result.append(self._copy_and_append_callback(callback))
        return tuple(result)
Esempio n. 8
0
 def __init__(
     self, 
     talea=(-1, 4, -2, 3),
     talea_denominator=16,
     prolation_addenda=None,
     burnish_specifier=None,
     secondary_divisions=None,
     helper_functions=None,
     beam_each_cell=False, 
     beam_cells_together=False,
     decrease_durations_monotonically=True, 
     tie_split_notes=False, 
     burnish_divisions=False,
     burnish_output=False,
     ):
     from abjad.tools import rhythmmakertools
     RhythmMaker.__init__(
         self,
         beam_each_cell=beam_each_cell,
         beam_cells_together=beam_cells_together,
         decrease_durations_monotonically=decrease_durations_monotonically,
         )
     prototype = (tuple, type(None))
     talea = self._to_tuple(talea)
     assert isinstance(talea, prototype)
     assert sequencetools.all_are_integer_equivalent_numbers(talea)
     self._talea = talea
     helper_functions = helper_functions or {}
     talea_helper = helper_functions.get('talea')
     prolation_addenda_helper = helper_functions.get('prolation_addenda')
     lefts_helper = helper_functions.get('lefts')
     middles_helper = helper_functions.get('middles')
     rights_helper = helper_functions.get('rights')
     left_lengths_helper = helper_functions.get('left_lengths')
     right_lengths_helper = helper_functions.get('right_lengths')
     secondary_divisions_helper = helper_functions.get('secondary_divisions')
     assert isinstance(burnish_divisions, bool)
     assert isinstance(burnish_output, bool)
     self._burnish_divisions = burnish_divisions
     self._burnish_output = burnish_output
     prolation_addenda = self._to_tuple(prolation_addenda)
     burnish_specifier = burnish_specifier or \
         rhythmmakertools.BurnishSpecifier()
     assert isinstance(burnish_specifier, rhythmmakertools.BurnishSpecifier)
     self._burnish_specifier = burnish_specifier
     secondary_divisions = self._to_tuple(secondary_divisions)
     talea_helper = self._none_to_trivial_helper(talea_helper)
     prolation_addenda_helper = self._none_to_trivial_helper(
         prolation_addenda_helper)
     lefts_helper = self._none_to_trivial_helper(lefts_helper)
     middles_helper = self._none_to_trivial_helper(middles_helper)
     rights_helper = self._none_to_trivial_helper(rights_helper)
     left_lengths_helper = self._none_to_trivial_helper(
         left_lengths_helper)
     right_lengths_helper = self._none_to_trivial_helper(
         right_lengths_helper)
     secondary_divisions_helper = self._none_to_trivial_helper(
         secondary_divisions_helper)
     assert mathtools.is_positive_integer_equivalent_number(
         talea_denominator)
     assert prolation_addenda is None or \
         sequencetools.all_are_nonnegative_integer_equivalent_numbers(
         prolation_addenda)
     assert secondary_divisions is None or \
         sequencetools.all_are_nonnegative_integer_equivalent_numbers(
         secondary_divisions)
     assert callable(talea_helper)
     assert callable(prolation_addenda_helper)
     assert callable(lefts_helper)
     assert callable(middles_helper)
     assert callable(rights_helper)
     assert callable(left_lengths_helper)
     assert callable(right_lengths_helper)
     assert isinstance(decrease_durations_monotonically, bool)
     assert isinstance(tie_split_notes, bool)
     self._talea_denominator = talea_denominator
     self._prolation_addenda = prolation_addenda
     self._secondary_divisions = secondary_divisions
     if helper_functions == {}:
         helper_functions = None
     self._helper_functions = helper_functions
     self._tie_split_notes = tie_split_notes
Esempio n. 9
0
 def __init__(self, 
     talea=None, 
     talea_denominator=None, 
     prolation_addenda=None,
     lefts=None, 
     middles=None, 
     rights=None, 
     left_lengths=None, 
     right_lengths=None,
     secondary_divisions=None,
     talea_helper=None, 
     prolation_addenda_helper=None,
     lefts_helper=None, 
     middles_helper=None, 
     rights_helper=None,
     left_lengths_helper=None, 
     right_lengths_helper=None, 
     secondary_divisions_helper=None,
     beam_each_cell=False, 
     beam_cells_together=False,
     decrease_durations_monotonically=True, 
     tie_split_notes=False, 
     tie_rests=False,
     ):
     RhythmMaker.__init__(self,
         beam_each_cell=beam_each_cell,
         beam_cells_together=beam_cells_together,
         )
     prolation_addenda = self._none_to_new_list(prolation_addenda)
     lefts = self._none_to_new_list(lefts)
     middles = self._none_to_new_list(middles)
     rights = self._none_to_new_list(rights)
     left_lengths = self._none_to_new_list(left_lengths)
     right_lengths = self._none_to_new_list(right_lengths)
     secondary_divisions = self._none_to_new_list(secondary_divisions)
     talea_helper = self._none_to_trivial_helper(talea_helper)
     prolation_addenda_helper = self._none_to_trivial_helper(
         prolation_addenda_helper)
     lefts_helper = self._none_to_trivial_helper(lefts_helper)
     middles_helper = self._none_to_trivial_helper(middles_helper)
     rights_helper = self._none_to_trivial_helper(rights_helper)
     left_lengths_helper = self._none_to_trivial_helper(
         left_lengths_helper)
     right_lengths_helper = self._none_to_trivial_helper(
         right_lengths_helper)
     secondary_divisions_helper = self._none_to_trivial_helper(
         secondary_divisions_helper)
     assert sequencetools.all_are_integer_equivalent_numbers(talea)
     assert mathtools.is_positive_integer_equivalent_number(
         talea_denominator)
     assert sequencetools.all_are_nonnegative_integer_equivalent_numbers(
         prolation_addenda)
     assert all(x in (-1, 0, 1) for x in lefts)
     assert all(x in (-1, 0, 1) for x in middles)
     assert all(x in (-1, 0, 1) for x in rights)
     assert sequencetools.all_are_nonnegative_integer_equivalent_numbers(
         left_lengths)
     assert sequencetools.all_are_nonnegative_integer_equivalent_numbers(
         right_lengths)
     assert sequencetools.all_are_nonnegative_integer_equivalent_numbers(
         secondary_divisions)
     assert isinstance(talea_helper, 
         (types.FunctionType, types.MethodType))
     assert isinstance(prolation_addenda_helper, 
         (types.FunctionType, types.MethodType))
     assert isinstance(lefts_helper, 
         (types.FunctionType, types.MethodType))
     assert isinstance(middles_helper, 
         (types.FunctionType, types.MethodType))
     assert isinstance(rights_helper, 
         (types.FunctionType, types.MethodType))
     assert isinstance(left_lengths_helper, 
         (types.FunctionType, types.MethodType))
     assert isinstance(right_lengths_helper, 
         (types.FunctionType, types.MethodType))
     assert isinstance(decrease_durations_monotonically, bool)
     assert isinstance(tie_split_notes, bool)
     assert isinstance(tie_rests, bool)
     self.talea = talea
     self.talea_denominator = talea_denominator
     self.prolation_addenda = prolation_addenda
     self.lefts = lefts
     self.middles = middles
     self.rights = rights
     self.left_lengths = left_lengths
     self.right_lengths = right_lengths
     self.secondary_divisions = secondary_divisions
     self.talea_helper = talea_helper
     self.prolation_addenda_helper = prolation_addenda_helper
     self.lefts_helper = lefts_helper
     self.middles_helper = middles_helper
     self.rights_helper = rights_helper
     self.left_lengths_helper = left_lengths_helper
     self.right_lengths_helper = right_lengths_helper
     self.secondary_divisions_helper = secondary_divisions_helper
     #self.beam_each_cell = beam_each_cell
     self.decrease_durations_monotonically = \
         decrease_durations_monotonically
     self.tie_split_notes = tie_split_notes
     self.tie_rests = tie_rests
Esempio n. 10
0
 def __init__(
     self,
     prefix_talea=None,
     prefix_lengths=None,
     suffix_talea=None,
     suffix_lengths=None,
     talea_denominator=None,
     body_ratio=None,
     prolation_addenda=None,
     secondary_divisions=None,
     prefix_talea_helper=None,
     prefix_lengths_helper=None,
     suffix_talea_helper=None,
     suffix_lengths_helper=None,
     prolation_addenda_helper=None,
     secondary_divisions_helper=None,
     decrease_durations_monotonically=True,
     tie_rests=False,
     forbidden_written_duration=None,
     beam_each_cell=False,
     beam_cells_together=False,
     ):
     RhythmMaker.__init__(
         self,
         forbidden_written_duration=forbidden_written_duration,
         beam_each_cell=beam_each_cell,
         beam_cells_together=beam_cells_together,
         )
     prolation_addenda = \
         self._none_to_new_list(prolation_addenda)
     secondary_divisions = \
         self._none_to_new_list(secondary_divisions)
     prefix_talea_helper = \
         self._none_to_trivial_helper(prefix_talea_helper)
     prefix_lengths_helper = \
         self._none_to_trivial_helper(prefix_lengths_helper)
     suffix_talea_helper = \
         self._none_to_trivial_helper(suffix_talea_helper)
     suffix_lengths_helper = \
         self._none_to_trivial_helper(suffix_lengths_helper)
     prolation_addenda_helper = \
         self._none_to_trivial_helper(prolation_addenda_helper)
     secondary_divisions_helper = \
         self._none_to_trivial_helper(secondary_divisions_helper)
     assert sequencetools.all_are_integer_equivalent_numbers(
         prefix_talea)
     assert sequencetools.all_are_nonnegative_integer_equivalent_numbers(
         prefix_lengths)
     assert sequencetools.all_are_integer_equivalent_numbers(
         suffix_talea)
     assert sequencetools.all_are_nonnegative_integer_equivalent_numbers(
         suffix_lengths)
     assert mathtools.is_positive_integer_equivalent_number(
         talea_denominator)
     assert sequencetools.all_are_nonnegative_integer_equivalent_numbers(
         prolation_addenda)
     assert sequencetools.all_are_nonnegative_integer_equivalent_numbers(
         secondary_divisions)
     assert isinstance(
         prefix_talea_helper, (types.FunctionType, types.MethodType))
     assert isinstance(
         prefix_lengths_helper, (types.FunctionType, types.MethodType))
     assert isinstance(
         suffix_talea_helper, (types.FunctionType, types.MethodType))
     assert isinstance(
         suffix_lengths_helper, (types.FunctionType, types.MethodType))
     assert isinstance(
         prolation_addenda_helper, (types.FunctionType, types.MethodType))
     assert isinstance(
         secondary_divisions_helper, (types.FunctionType, types.MethodType))
     assert isinstance(
         decrease_durations_monotonically, bool)
     assert isinstance(
         tie_rests, bool)
     self.prefix_talea = prefix_talea
     self.prefix_lengths = prefix_lengths
     self.suffix_talea = suffix_talea
     self.suffix_lengths = suffix_lengths
     self.prolation_addenda = prolation_addenda
     self.talea_denominator = talea_denominator
     if body_ratio is not None:
         body_ratio = mathtools.Ratio(body_ratio)
     self.body_ratio = body_ratio
     self.secondary_divisions = secondary_divisions
     self.prefix_talea_helper = \
         self._none_to_trivial_helper(prefix_talea_helper)
     self.prefix_lengths_helper = \
         self._none_to_trivial_helper(prefix_lengths_helper)
     self.suffix_talea_helper = \
         self._none_to_trivial_helper(suffix_talea_helper)
     self.suffix_lengths_helper = \
         self._none_to_trivial_helper(suffix_lengths_helper)
     self.prolation_addenda_helper = \
         self._none_to_trivial_helper(prolation_addenda_helper)
     self.secondary_divisions_helper = \
         self._none_to_trivial_helper(secondary_divisions_helper)
     self.decrease_durations_monotonically = \
         decrease_durations_monotonically
     self.tie_rests = tie_rests
Esempio n. 11
0
    def divide_by_ratio(self, ratio):
        r'''Divide timespan expression by `ratio`:

        ::

            >>> timespans = red_segment.timespan.divide_by_ratio((2, 3))

        ::

            >>> print(format(timespans[0]))
            musicexpressiontools.TimespanExpression(
                anchor='red',
                callbacks=musicexpressiontools.CallbackInventory(
                    [
                        'self._divide_by_ratio(original_start_offset, original_stop_offset, (2, 3), 0)',
                        ]
                    ),
                )

        ::

            >>> print(format(timespans[1]))
            musicexpressiontools.TimespanExpression(
                anchor='red',
                callbacks=musicexpressiontools.CallbackInventory(
                    [
                        'self._divide_by_ratio(original_start_offset, original_stop_offset, (2, 3), 1)',
                        ]
                    ),
                )

        Coerce integer `ratio` to ``Ratio(ratio*[1])``:

        ::

            >>> timespans = red_segment.timespan.divide_by_ratio(3)

        ::

            >>> print(format(timespans[0]))
            musicexpressiontools.TimespanExpression(
                anchor='red',
                callbacks=musicexpressiontools.CallbackInventory(
                    [
                        'self._divide_by_ratio(original_start_offset, original_stop_offset, [1, 1, 1], 0)',
                        ]
                    ),
                )

        ::

            >>> print(format(timespans[1]))
            musicexpressiontools.TimespanExpression(
                anchor='red',
                callbacks=musicexpressiontools.CallbackInventory(
                    [
                        'self._divide_by_ratio(original_start_offset, original_stop_offset, [1, 1, 1], 1)',
                        ]
                    ),
                )

        ::

            >>> print(format(timespans[2]))
            musicexpressiontools.TimespanExpression(
                anchor='red',
                callbacks=musicexpressiontools.CallbackInventory(
                    [
                        'self._divide_by_ratio(original_start_offset, original_stop_offset, [1, 1, 1], 2)',
                        ]
                    ),
                )

        Returns tuple of newly constructed timespans with callback.
        '''
        result = []
        if mathtools.is_positive_integer_equivalent_number(ratio):
            ratio = int(ratio) * [1]
        for part in range(len(ratio)):
            callback = \
                'self._divide_by_ratio(original_start_offset, original_stop_offset, {!r}, {!r})'
            callback = callback.format(ratio, part)
            result.append(self._copy_and_append_callback(callback))
        return tuple(result)