/
Ratio.py
43 lines (30 loc) · 1013 Bytes
/
Ratio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- encoding: utf-8 -*-
from abjad.tools.mathtools.NonreducedRatio import NonreducedRatio
class Ratio(NonreducedRatio):
'''Ratio of one or more nonzero integers.
Initialize from one or more nonzero integers:
::
>>> mathtools.Ratio(2, 4, 2)
Ratio(1, 2, 1)
Or initialize from a tuple or list:
::
>>> ratio = mathtools.Ratio((2, 4, 2))
>>> ratio
Ratio(1, 2, 1)
Use a tuple to return ratio integers.
>>> tuple(ratio)
(1, 2, 1)
Ratios are immutable.
'''
### CONSTRUCTOR ###
def __new__(cls, *args):
from abjad.tools import sequencetools
if len(args) == 1 and isinstance(args[0], (list, tuple)):
args = args[0]
assert args, repr(args)
assert all(x != 0 for x in args), repr(args)
args = \
sequencetools.divide_sequence_elements_by_greatest_common_divisor(
args)
self = NonreducedRatio.__new__(cls, args)
return self