NonreducedFraction

class mathtools.NonreducedFraction

Initializes with an integer numerator and integer denominator:

>>> mathtools.NonreducedFraction(3, 6)
NonreducedFraction(3, 6)

Initializes with only an integer denominator:

>>> mathtools.NonreducedFraction(3)
NonreducedFraction(3, 1)

Initializes with an integer pair:

>>> mathtools.NonreducedFraction((3, 6))
NonreducedFraction(3, 6)

Initializes with an integer singleton:

>>> mathtools.NonreducedFraction((3,))
NonreducedFraction(3, 1)

Similar to built-in fraction except that numerator and denominator do not reduce.

Nonreduced fractions inherit from built-in fraction:

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

Nonreduced fractions are numbers:

>>> import numbers
>>> isinstance(mathtools.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_durationtools { graph [label=durationtools]; "abjad.tools.durationtools.Division.Division" [color=3, group=2, label=Division, shape=box]; } subgraph cluster_mathtools { graph [label=mathtools]; "abjad.tools.mathtools.NonreducedFraction.NonreducedFraction" [color=black, fontcolor=white, group=4, 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_fractions { graph [label=fractions]; "fractions.Fraction" [color=4, group=3, label=Fraction, shape=box]; } subgraph cluster_numbers { graph [label=numbers]; "numbers.Complex" [color=6, group=5, label=Complex, shape=oval, style=bold]; "numbers.Number" [color=6, group=5, label=Number, shape=box]; "numbers.Rational" [color=6, group=5, label=Rational, shape=oval, style=bold]; "numbers.Real" [color=6, group=5, label=Real, shape=oval, style=bold]; "numbers.Complex" -> "numbers.Real"; "numbers.Number" -> "numbers.Complex"; "numbers.Real" -> "numbers.Rational"; } "abjad.tools.abctools.AbjadObject.AbjadObject" -> "abjad.tools.mathtools.NonreducedFraction.NonreducedFraction"; "abjad.tools.mathtools.NonreducedFraction.NonreducedFraction" -> "abjad.tools.durationtools.Division.Division"; "builtins.object" -> "abjad.tools.abctools.AbjadObject.AbstractBase"; "builtins.object" -> "numbers.Number"; "fractions.Fraction" -> "abjad.tools.mathtools.NonreducedFraction.NonreducedFraction"; "numbers.Rational" -> "fractions.Fraction"; }

Bases

Attribute summary

conjugate() Conjugate is a no-op for Reals.
denominator Denominator of nonreduced fraction.
from_decimal(dec) Converts a finite Decimal instance to a rational number, exactly.
from_float(f) Converts a finite float to a rational number, exactly.
imag Nonreduced fractions have no imaginary part.
limit_denominator([max_denominator]) Closest Fraction to self with denominator at most max_denominator.
multiply_with_cross_cancelation(multiplier) Multiplies nonreduced fraction by expr with cross-cancelation.
multiply_with_numerator_preservation(multiplier) Multiplies nonreduced fraction by multiplier with numerator preservation where possible.
multiply_without_reducing(expr) Multiplies nonreduced fraction by expr 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__() Absolute value of nonreduced fraction.
__add__(expr) Adds expr to nonreduced fraction.
__bool__(a) a != 0
__ceil__(a) Will be math.ceil(a) in 3.0.
__complex__() complex(self) == complex(float(self), 0)
__copy__()
__deepcopy__(memo)
__div__(expr) Divides nonreduced fraction by expr.
__divmod__(other) divmod(self, other): The pair (self // other, self % other).
__eq__(expr) Is true when expr equals nonreduced fraction.
__float__() float(self) = self.numerator / self.denominator
__floor__(a) Will be math.floor(a) in 3.0.
__floordiv__(a, b) a // b
__format__([format_specification]) Formats nonreduced fraction.
__ge__(expr) Is true when nonreduced fraction is greater than or equal to expr.
__gt__(expr) Is true when nonreduced fraction is greater than expr.
__hash__() Hashes nonreduced fraction.
__le__(expr) Is true when nonreduced fraction is less than or equal to expr.
__lt__(expr) Is true when nonreduced fraction is less than expr.
__mod__(a, b) a % b
__mul__(expr) Multiplies nonreduced fraction by expr.
__ne__(expr) Is true when expr does not equal nonreduced fraction.
__neg__() Negates nonreduced fraction.
__new__(class_, \*args)
__pos__(a) +a: Coerces a subclass instance to Fraction
__pow__(expr) Raises nonreduced fraction to expr.
__radd__(expr) Adds nonreduced fraction to expr.
__rdiv__(expr) Divides expr by nonreduced fraction.
__rdivmod__(other) divmod(other, self): The pair (self // other, self % other).
__repr__() Gets interpreter representation of nonreduced fraction.
__rfloordiv__(b, a) a // b
__rmod__(b, a) a % b
__rmul__(expr) Multiplies expr by nonreduced fraction.
__round__([ndigits]) Will be round(self, ndigits) in 3.0.
__rpow__(b, a) a ** b
__rsub__(expr) Subtracts nonreduced fraction from expr.
__rtruediv__(expr) Divides expr by nonreduced fraction.
__str__() String representation of nonreduced fraction.
__sub__(expr) Subtracts expr from nonreduced fraction.
__truediv__(expr) Divides nonreduced fraction in Python 3.
__trunc__(a) 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

(Real).conjugate()

Conjugate is a no-op for Reals.

(Fraction).limit_denominator(max_denominator=1000000)

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 expr with cross-cancelation.

>>> fraction = mathtools.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 = mathtools.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 = mathtools.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 = mathtools.NonreducedFraction(3, 8)
>>> fraction.multiply_with_numerator_preservation((2, 3))
NonreducedFraction(3, 12)

Returns nonreduced fraction.

NonreducedFraction.multiply_without_reducing(expr)

Multiplies nonreduced fraction by expr without reducing.

>>> fraction = mathtools.NonreducedFraction(3, 8)
>>> fraction.multiply_without_reducing((3, 3))
NonreducedFraction(9, 24)
>>> fraction = mathtools.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 = mathtools.NonreducedFraction(-6, 3)
>>> fraction.reduce()
Fraction(-2, 1)

Returns fraction.

NonreducedFraction.with_denominator(denominator)

Returns new nonreduced fraction with integer denominator.

>>> mathtools.NonreducedFraction(3, 6).with_denominator(12)
NonreducedFraction(6, 12)

Returns nonreduced fraction.

NonreducedFraction.with_multiple_of_denominator(denominator)

Returns new nonreduced fraction with multiple of integer denominator.

>>> fraction = mathtools.NonreducedFraction(3, 6)
>>> fraction.with_multiple_of_denominator(5)
NonreducedFraction(5, 10)

Returns nonreduced fraction.

Class & static methods

(Fraction).from_decimal(dec)

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

(Fraction).from_float(f)

Converts a finite float to a rational number, exactly.

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

Special methods

NonreducedFraction.__abs__()

Absolute value of nonreduced fraction.

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

Returns nonreduced fraction.

NonreducedFraction.__add__(expr)

Adds expr to nonreduced fraction.

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

Returns nonreduced fraction.

(Fraction).__bool__(a)

a != 0

(Fraction).__ceil__(a)

Will be math.ceil(a) in 3.0.

(Real).__complex__()

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

(Fraction).__copy__()
(Fraction).__deepcopy__(memo)
NonreducedFraction.__div__(expr)

Divides nonreduced fraction by expr.

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

Returns nonreduced fraction.

(Real).__divmod__(other)

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

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

NonreducedFraction.__eq__(expr)

Is true when expr equals nonreduced fraction.

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

Returns true or false.

(Rational).__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__(a)

Will be math.floor(a) in 3.0.

(Fraction).__floordiv__(a, b)

a // b

NonreducedFraction.__format__(format_specification='')

Formats nonreduced fraction.

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

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

Returns string.

NonreducedFraction.__ge__(expr)

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

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

Returns true or false.

NonreducedFraction.__gt__(expr)

Is true when nonreduced fraction is greater than expr.

>>> mathtools.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__(expr)

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

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

Returns true or false.

NonreducedFraction.__lt__(expr)

Is true when nonreduced fraction is less than expr.

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

Returns true or false.

(Fraction).__mod__(a, b)

a % b

NonreducedFraction.__mul__(expr)

Multiplies nonreduced fraction by expr.

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

Returns nonreduced fraction.

NonreducedFraction.__ne__(expr)

Is true when expr does not equal nonreduced fraction.

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

Returns true or false.

NonreducedFraction.__neg__()

Negates nonreduced fraction.

>>> -mathtools.NonreducedFraction(3, 3)
NonreducedFraction(-3, 3)

Returns nonreduced fraction.

static NonreducedFraction.__new__(class_, *args)
(Fraction).__pos__(a)

+a: Coerces a subclass instance to Fraction

NonreducedFraction.__pow__(expr)

Raises nonreduced fraction to expr.

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

Returns nonreduced fraction.

NonreducedFraction.__radd__(expr)

Adds nonreduced fraction to expr.

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

Returns nonreduced fraction.

NonreducedFraction.__rdiv__(expr)

Divides expr by nonreduced fraction.

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

Returns nonreduced fraction.

(Real).__rdivmod__(other)

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

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

NonreducedFraction.__repr__()

Gets interpreter representation of nonreduced fraction.

>>> mathtools.NonreducedFraction(3, 6)
NonreducedFraction(3, 6)

Returns string.

(Fraction).__rfloordiv__(b, a)

a // b

(Fraction).__rmod__(b, a)

a % b

NonreducedFraction.__rmul__(expr)

Multiplies expr by nonreduced fraction.

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

Returns nonreduced fraction.

(Fraction).__round__(ndigits=None)

Will be round(self, ndigits) in 3.0.

Rounds half toward even.

(Fraction).__rpow__(b, a)

a ** b

NonreducedFraction.__rsub__(expr)

Subtracts nonreduced fraction from expr.

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

Returns nonreduced fraction.

NonreducedFraction.__rtruediv__(expr)

Divides expr by nonreduced fraction.

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

Returns nonreduced fraction.

NonreducedFraction.__str__()

String representation of nonreduced fraction.

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

Returns string.

NonreducedFraction.__sub__(expr)

Subtracts expr from nonreduced fraction.

>>> mathtools.NonreducedFraction(3, 3) - 2
NonreducedFraction(-3, 3)

Returns nonreduced fraction.

NonreducedFraction.__truediv__(expr)

Divides nonreduced fraction in Python 3.

Returns nonreduced fraction.

(Fraction).__trunc__(a)

trunc(a)