NonreducedFraction

class mathtools.NonreducedFraction

Nonereduced fraction.

Initializes with an integer numerator and integer denominator:

>>> NonreducedFraction(3, 6)
NonreducedFraction(3, 6)

Initializes with only an integer denominator:

>>> NonreducedFraction(3)
NonreducedFraction(3, 1)

Initializes with an integer pair:

>>> NonreducedFraction((3, 6))
NonreducedFraction(3, 6)

Initializes with an integer singleton:

>>> NonreducedFraction((3,))
NonreducedFraction(3, 1)

Nonreduced fractions inherit from built-in fraction:

>>> isinstance(NonreducedFraction(3, 6), Fraction)
True

Nonreduced fractions are numbers:

>>> import numbers
>>> isinstance(NonreducedFraction(3, 6), numbers.Number)
True

Lineage

digraph InheritanceGraph { graph [background=transparent, bgcolor=transparent, color=lightslategrey, fontname=Arial, outputorder=edgesfirst, overlap=prism, penwidth=2, rankdir=LR, root="__builtin__.object", splines=spline, style="dotted, rounded", truecolor=true]; node [colorscheme=pastel19, fontname=Arial, fontsize=12, penwidth=2, style="filled, rounded"]; edge [color=lightsteelblue2, penwidth=2]; subgraph cluster_abctools { graph [label=abctools]; "abjad.tools.abctools.AbjadObject.AbjadObject" [color=1, group=0, label=AbjadObject, shape=box]; "abjad.tools.abctools.AbjadObject.AbstractBase" [color=1, group=0, label=AbstractBase, shape=box]; "abjad.tools.abctools.AbjadObject.AbstractBase" -> "abjad.tools.abctools.AbjadObject.AbjadObject"; } subgraph cluster_mathtools { graph [label=mathtools]; "abjad.tools.mathtools.NonreducedFraction.NonreducedFraction" [color=black, fontcolor=white, group=2, label=<<B>NonreducedFraction</B>>, shape=box, style="filled, rounded"]; } subgraph cluster_builtins { graph [label=builtins]; "builtins.object" [color=2, group=1, label=object, shape=box]; } subgraph cluster_quicktions { graph [label=quicktions]; "quicktions.Fraction" [color=4, group=3, label=Fraction, shape=box]; } "abjad.tools.abctools.AbjadObject.AbjadObject" -> "abjad.tools.mathtools.NonreducedFraction.NonreducedFraction"; "builtins.object" -> "abjad.tools.abctools.AbjadObject.AbstractBase"; "builtins.object" -> "quicktions.Fraction"; "quicktions.Fraction" -> "abjad.tools.mathtools.NonreducedFraction.NonreducedFraction"; }

Bases

Attribute summary

conjugate Conjugate is a no-op for Reals.
denominator Denominator of nonreduced fraction.
from_decimal Converts a finite Decimal instance to a rational number, exactly.
from_float Converts a finite float to a rational number, exactly.
imag Nonreduced fractions have no imaginary part.
limit_denominator Closest Fraction to self with denominator at most max_denominator.
multiply_with_cross_cancelation(multiplier) Multiplies nonreduced fraction by argument with cross-cancelation.
multiply_with_numerator_preservation(multiplier) Multiplies nonreduced fraction by multiplier with numerator preservation where possible.
multiply_without_reducing(argument) Multiplies nonreduced fraction by argument without reducing.
numerator Numerator of nonreduced fraction.
pair Read only pair of nonreduced fraction numerator and denominator.
real Nonreduced fractions are their own real component.
reduce() Reduces nonreduced fraction.
with_denominator(denominator) Returns new nonreduced fraction with integer denominator.
with_multiple_of_denominator(denominator) Returns new nonreduced fraction with multiple of integer denominator.
__abs__() Gets absolute value of nonreduced fraction.
__add__(argument) Adds argument to nonreduced fraction.
__bool__ self != 0
__ceil__ math.ceil(a)
__complex__ complex(self) == complex(float(self), 0)
__copy__
__deepcopy__
__div__(argument) Divides nonreduced fraction by argument.
__divmod__ divmod(self, other): The pair (self // other, self % other).
__eq__(argument) Is true when argument equals nonreduced fraction.
__float__ float(self) = self.numerator / self.denominator
__floor__ math.floor(a)
__floordiv__ a // b
__format__([format_specification]) Formats nonreduced fraction.
__ge__(argument) Is true when nonreduced fraction is greater than or equal to argument.
__gt__(argument) Is true when nonreduced fraction is greater than argument.
__hash__() Hashes nonreduced fraction.
__le__(argument) Is true when nonreduced fraction is less than or equal to argument.
__lt__(argument) Is true when nonreduced fraction is less than argument.
__mod__ a % b
__mul__(argument) Multiplies nonreduced fraction by argument.
__ne__(argument) Is true when argument does not equal nonreduced fraction.
__neg__() Negates nonreduced fraction.
__new__(class_, *arguments)
__pos__ +a: Coerces a subclass instance to Fraction
__pow__(argument) Raises nonreduced fraction to argument.
__radd__(argument) Adds nonreduced fraction to argument.
__rdiv__(argument) Divides argument by nonreduced fraction.
__rdivmod__ Return divmod(value, self).
__repr__() Gets interpreter representation of nonreduced fraction.
__rfloordiv__ Return value//self.
__rmod__ Return value%self.
__rmul__(argument) Multiplies argument by nonreduced fraction.
__round__ round(self, ndigits)
__rpow__ Return pow(value, self, mod).
__rsub__(argument) Subtracts nonreduced fraction from argument.
__rtruediv__(argument) Divides argument by nonreduced fraction.
__str__() Gets string representation of nonreduced fraction.
__sub__(argument) Subtracts argument from nonreduced fraction.
__truediv__(argument) Divides nonreduced fraction in Python 3.
__trunc__ trunc(a)

Read-only properties

NonreducedFraction.denominator

Denominator of nonreduced fraction.

>>> fraction = mathtools.NonreducedFraction(-6, 3)
>>> fraction.denominator
3

Returns positive integer.

NonreducedFraction.imag

Nonreduced fractions have no imaginary part.

>>> fraction.imag
0

Returns zero.

NonreducedFraction.numerator

Numerator of nonreduced fraction.

>>> fraction = mathtools.NonreducedFraction(-6, 3)
>>> fraction.numerator
-6

Returns integer.

NonreducedFraction.pair

Read only pair of nonreduced fraction numerator and denominator.

>>> fraction = mathtools.NonreducedFraction(-6, 3)
>>> fraction.pair
(-6, 3)

Returns integer pair.

NonreducedFraction.real

Nonreduced fractions are their own real component.

>>> fraction.real
NonreducedFraction(-6, 3)

Returns nonreduced fraction.

Methods

(Fraction).conjugate()

Conjugate is a no-op for Reals.

(Fraction).from_decimal()

Converts a finite Decimal instance to a rational number, exactly.

(Fraction).from_float()

Converts a finite float to a rational number, exactly.

Beware that Fraction.from_float(0.3) != Fraction(3, 10).

(Fraction).limit_denominator()

Closest Fraction to self with denominator at most max_denominator.

>>> Fraction('3.141592653589793').limit_denominator(10)
Fraction(22, 7)
>>> Fraction('3.141592653589793').limit_denominator(100)
Fraction(311, 99)
>>> Fraction(4321, 8765).limit_denominator(10000)
Fraction(4321, 8765)
NonreducedFraction.multiply_with_cross_cancelation(multiplier)

Multiplies nonreduced fraction by argument with cross-cancelation.

>>> fraction = NonreducedFraction(4, 8)
>>> fraction.multiply_with_cross_cancelation((2, 3))
NonreducedFraction(4, 12)
>>> fraction.multiply_with_cross_cancelation((4, 1))
NonreducedFraction(4, 2)
>>> fraction.multiply_with_cross_cancelation((3, 5))
NonreducedFraction(12, 40)
>>> fraction.multiply_with_cross_cancelation((6, 5))
NonreducedFraction(12, 20)
>>> fraction = NonreducedFraction(5, 6)
>>> fraction.multiply_with_cross_cancelation((6, 5))
NonreducedFraction(1, 1)

Returns nonreduced fraction.

NonreducedFraction.multiply_with_numerator_preservation(multiplier)

Multiplies nonreduced fraction by multiplier with numerator preservation where possible.

>>> fraction = NonreducedFraction(9, 16)
>>> fraction.multiply_with_numerator_preservation((2, 3))
NonreducedFraction(9, 24)
>>> fraction.multiply_with_numerator_preservation((1, 2))
NonreducedFraction(9, 32)
>>> fraction.multiply_with_numerator_preservation((5, 6))
NonreducedFraction(45, 96)
>>> fraction = NonreducedFraction(3, 8)
>>> fraction.multiply_with_numerator_preservation((2, 3))
NonreducedFraction(3, 12)

Returns nonreduced fraction.

NonreducedFraction.multiply_without_reducing(argument)

Multiplies nonreduced fraction by argument without reducing.

>>> fraction = NonreducedFraction(3, 8)
>>> fraction.multiply_without_reducing((3, 3))
NonreducedFraction(9, 24)
>>> fraction = NonreducedFraction(4, 8)
>>> fraction.multiply_without_reducing((4, 5))
NonreducedFraction(16, 40)
>>> fraction.multiply_without_reducing((3, 4))
NonreducedFraction(12, 32)

Returns nonreduced fraction.

NonreducedFraction.reduce()

Reduces nonreduced fraction.

>>> fraction = NonreducedFraction(-6, 3)
>>> fraction.reduce()
Fraction(-2, 1)

Returns fraction.

NonreducedFraction.with_denominator(denominator)

Returns new nonreduced fraction with integer denominator.

>>> NonreducedFraction(3, 6).with_denominator(12)
NonreducedFraction(6, 12)
>>> for numerator in range(12):
...     fraction = NonreducedFraction(numerator, 6)
...     print(fraction, fraction.with_denominator(12))
...
0/6 0/12
1/6 2/12
2/6 4/12
3/6 6/12
4/6 8/12
5/6 10/12
6/6 12/12
7/6 14/12
8/6 16/12
9/6 18/12
10/6 20/12
11/6 22/12
>>> for numerator in range(12):
...     fraction = NonreducedFraction(numerator, 6)
...     print(fraction, fraction.with_denominator(8))
...
0/6 0/8
1/6 1/6
2/6 2/6
3/6 4/8
4/6 4/6
5/6 5/6
6/6 8/8
7/6 7/6
8/6 8/6
9/6 12/8
10/6 10/6
11/6 11/6
>>> for numerator in range(12):
...     fraction = NonreducedFraction(numerator, 6)
...     print(fraction, fraction.with_denominator(12))
...
0/6 0/12
1/6 2/12
2/6 4/12
3/6 6/12
4/6 8/12
5/6 10/12
6/6 12/12
7/6 14/12
8/6 16/12
9/6 18/12
10/6 20/12
11/6 22/12

Returns nonreduced fraction.

NonreducedFraction.with_multiple_of_denominator(denominator)

Returns new nonreduced fraction with multiple of integer denominator.

>>> fraction = NonreducedFraction(3, 6)
>>> fraction.with_multiple_of_denominator(5)
NonreducedFraction(5, 10)
>>> NonreducedFraction(1, 2).with_multiple_of_denominator(2)
NonreducedFraction(1, 2)
>>> NonreducedFraction(1, 2).with_multiple_of_denominator(4)
NonreducedFraction(2, 4)
>>> NonreducedFraction(1, 2).with_multiple_of_denominator(8)
NonreducedFraction(4, 8)
>>> NonreducedFraction(1, 2).with_multiple_of_denominator(16)
NonreducedFraction(8, 16)

Returns nonreduced fraction.

Special methods

NonreducedFraction.__abs__()

Gets absolute value of nonreduced fraction.

>>> abs(NonreducedFraction(-3, 3))
NonreducedFraction(3, 3)

Returns nonreduced fraction.

NonreducedFraction.__add__(argument)

Adds argument to nonreduced fraction.

>>> NonreducedFraction(3, 3) + 1
NonreducedFraction(6, 3)
>>> 1 + NonreducedFraction(3, 3)
NonreducedFraction(6, 3)

Returns nonreduced fraction.

(Fraction).__bool__()

self != 0

(Fraction).__ceil__()

math.ceil(a)

(Fraction).__complex__()

complex(self) == complex(float(self), 0)

(Fraction).__copy__()
(Fraction).__deepcopy__()
NonreducedFraction.__div__(argument)

Divides nonreduced fraction by argument.

>>> NonreducedFraction(3, 3) / 1
NonreducedFraction(3, 3)

Returns nonreduced fraction.

(Fraction).__divmod__()

divmod(self, other): The pair (self // other, self % other).

Sometimes this can be computed faster than the pair of operations.

NonreducedFraction.__eq__(argument)

Is true when argument equals nonreduced fraction.

>>> NonreducedFraction(3, 3) == 1
True

Returns true or false.

(Fraction).__float__()

float(self) = self.numerator / self.denominator

It’s important that this conversion use the integer’s “true” division rather than casting one side to float before dividing so that ratios of huge integers convert without overflowing.

(Fraction).__floor__()

math.floor(a)

(Fraction).__floordiv__()

a // b

NonreducedFraction.__format__(format_specification='')

Formats nonreduced fraction.

Set format_specification to ‘’ or ‘storage’. Interprets ‘’ equal to ‘storage’.

>>> fraction = NonreducedFraction(-6, 3)
>>> print(format(fraction))
mathtools.NonreducedFraction(-6, 3)

Returns string.

NonreducedFraction.__ge__(argument)

Is true when nonreduced fraction is greater than or equal to argument.

>>> NonreducedFraction(3, 3) >= 1
True

Returns true or false.

NonreducedFraction.__gt__(argument)

Is true when nonreduced fraction is greater than argument.

>>> NonreducedFraction(3, 3) > 1
False

Returns true or false.

NonreducedFraction.__hash__()

Hashes nonreduced fraction.

Required to be explicitly redefined on Python 3 if __eq__ changes.

Returns integer.

NonreducedFraction.__le__(argument)

Is true when nonreduced fraction is less than or equal to argument.

>>> NonreducedFraction(3, 3) <= 1
True

Returns true or false.

NonreducedFraction.__lt__(argument)

Is true when nonreduced fraction is less than argument.

>>> NonreducedFraction(3, 3) < 1
False

Returns true or false.

(Fraction).__mod__()

a % b

NonreducedFraction.__mul__(argument)

Multiplies nonreduced fraction by argument.

>>> NonreducedFraction(3, 3) * 3
NonreducedFraction(9, 3)

Returns nonreduced fraction.

NonreducedFraction.__ne__(argument)

Is true when argument does not equal nonreduced fraction.

>>> NonreducedFraction(3, 3) != 'foo'
True

Returns true or false.

NonreducedFraction.__neg__()

Negates nonreduced fraction.

>>> -NonreducedFraction(3, 3)
NonreducedFraction(-3, 3)

Returns nonreduced fraction.

static NonreducedFraction.__new__(class_, *arguments)
(Fraction).__pos__()

+a: Coerces a subclass instance to Fraction

NonreducedFraction.__pow__(argument)

Raises nonreduced fraction to argument.

>>> NonreducedFraction(3, 6) ** -1
NonreducedFraction(6, 3)

Returns nonreduced fraction.

NonreducedFraction.__radd__(argument)

Adds nonreduced fraction to argument.

>>> 1 + NonreducedFraction(3, 3)
NonreducedFraction(6, 3)

Returns nonreduced fraction.

NonreducedFraction.__rdiv__(argument)

Divides argument by nonreduced fraction.

>>> 1 / NonreducedFraction(3, 3)
NonreducedFraction(3, 3)

Returns nonreduced fraction.

(Fraction).__rdivmod__()

Return divmod(value, self).

NonreducedFraction.__repr__()

Gets interpreter representation of nonreduced fraction.

>>> NonreducedFraction(3, 6)
NonreducedFraction(3, 6)

Returns string.

(Fraction).__rfloordiv__()

Return value//self.

(Fraction).__rmod__()

Return value%self.

NonreducedFraction.__rmul__(argument)

Multiplies argument by nonreduced fraction.

>>> 3 * NonreducedFraction(3, 3)
NonreducedFraction(9, 3)

Returns nonreduced fraction.

(Fraction).__round__()

round(self, ndigits)

Rounds half toward even.

(Fraction).__rpow__()

Return pow(value, self, mod).

NonreducedFraction.__rsub__(argument)

Subtracts nonreduced fraction from argument.

>>> 1 - NonreducedFraction(3, 3)
NonreducedFraction(0, 3)

Returns nonreduced fraction.

NonreducedFraction.__rtruediv__(argument)

Divides argument by nonreduced fraction.

>>> 1 / NonreducedFraction(3, 3)
NonreducedFraction(3, 3)

Returns nonreduced fraction.

NonreducedFraction.__str__()

Gets string representation of nonreduced fraction.

>>> fraction = NonreducedFraction(-6, 3)
>>> str(fraction)
'-6/3'

Returns string.

NonreducedFraction.__sub__(argument)

Subtracts argument from nonreduced fraction.

>>> NonreducedFraction(3, 3) - 2
NonreducedFraction(-3, 3)
>>> NonreducedFraction(5, 4) - NonreducedFraction(18, 16)
NonreducedFraction(2, 16)
>>> NonreducedFraction(18, 16) - NonreducedFraction(5, 4)
NonreducedFraction(-2, 16)

Returns nonreduced fraction.

NonreducedFraction.__truediv__(argument)

Divides nonreduced fraction in Python 3.

Returns nonreduced fraction.

(Fraction).__trunc__()

trunc(a)