MCPcopy
hub / github.com/keon/algorithms / _rationalize_if_possible

Method _rationalize_if_possible

algorithms/math/polynomial.py:57–72  ·  view source on GitHub ↗

Convert numbers to Fraction when possible. Args: num: A numeric value. Returns: A Fraction if the input is Rational, otherwise the original value.

(
        num: int | float | Fraction,
    )

Source from the content-addressed store, hash-verified

55
56 @staticmethod
57 def _rationalize_if_possible(
58 num: int | float | Fraction,
59 ) -> Fraction | float:
60 """Convert numbers to Fraction when possible.
61
62 Args:
63 num: A numeric value.
64
65 Returns:
66 A Fraction if the input is Rational, otherwise the original value.
67 """
68 if isinstance(num, Rational):
69 res = Fraction(num, 1)
70 return Fraction(res.numerator, res.denominator)
71 else:
72 return num
73
74 def equal_upto_scalar(self, other: object) -> bool:
75 """Check if other is a monomial equivalent to self up to scalar multiple.

Callers 12

__init__Method · 0.45
__add__Method · 0.45
__mul__Method · 0.45
inverseMethod · 0.45
__truediv__Method · 0.45
cloneMethod · 0.45
cleanMethod · 0.45
__sub__Method · 0.45
substituteMethod · 0.45
__add__Method · 0.45
__sub__Method · 0.45
subsMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected