Feat: fraction can be simplify
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
1347c30b92
commit
d6e3f774fa
@ -309,15 +309,15 @@ class Expression(object):
|
|||||||
|
|
||||||
comp_exp = opt_exp._compute()
|
comp_exp = opt_exp._compute()
|
||||||
|
|
||||||
if typed_exp == comp_exp:
|
if typed_exp != comp_exp:
|
||||||
typed_exp.set_ancestor(self._ancestor)
|
|
||||||
return typed_exp
|
|
||||||
else:
|
|
||||||
comp_exp.set_ancestor(self)
|
comp_exp.set_ancestor(self)
|
||||||
return comp_exp._simplify(optimize=optimize)
|
return comp_exp._simplify(optimize=optimize)
|
||||||
|
|
||||||
|
typed_exp.set_ancestor(self._ancestor)
|
||||||
|
return typed_exp
|
||||||
|
|
||||||
def simplify(self, optimize=True):
|
def simplify(self, optimize=True):
|
||||||
""" Compute as much as possible the expression
|
""" Simplify the expression, keep the history and factory child
|
||||||
|
|
||||||
:param optimize: bool to optimize tree when it's possible
|
:param optimize: bool to optimize tree when it's possible
|
||||||
:return: an expression
|
:return: an expression
|
||||||
|
@ -271,18 +271,19 @@ class Fraction(Token):
|
|||||||
"""
|
"""
|
||||||
return Decimal(self._mo._value)
|
return Decimal(self._mo._value)
|
||||||
|
|
||||||
|
@property
|
||||||
def simplified(self):
|
def simplified(self):
|
||||||
""" Get the irreductible version of self
|
""" Get the irreductible version of self
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
>>> f = Fraction("3/4")
|
>>> f = Fraction("3/4")
|
||||||
>>> f.simplified()
|
>>> f.simplified
|
||||||
<Fraction 3 / 4>
|
<Fraction 3 / 4>
|
||||||
>>> f = Fraction("12/9")
|
>>> f = Fraction("12/9")
|
||||||
>>> f.simplified()
|
>>> f.simplified
|
||||||
<Fraction 4 / 3>
|
<Fraction 4 / 3>
|
||||||
>>> f = Fraction("12/4")
|
>>> f = Fraction("12/4")
|
||||||
>>> f.simplified()
|
>>> f.simplified
|
||||||
<Integer 3>
|
<Integer 3>
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -293,6 +294,32 @@ class Fraction(Token):
|
|||||||
|
|
||||||
return Fraction(simplified)
|
return Fraction(simplified)
|
||||||
|
|
||||||
|
def simplify(self):
|
||||||
|
""" Itself or its simplified version
|
||||||
|
|
||||||
|
:example:
|
||||||
|
>>> f = Fraction("12/8")
|
||||||
|
>>> fs = f.simplify()
|
||||||
|
>>> for i in fs.explain():
|
||||||
|
... print(i)
|
||||||
|
12 / 8
|
||||||
|
3 / 2
|
||||||
|
>>> f = Fraction("5/8")
|
||||||
|
>>> fs = f.simplify()
|
||||||
|
>>> for i in fs.explain():
|
||||||
|
... print(i)
|
||||||
|
5 / 8
|
||||||
|
"""
|
||||||
|
simplified = self.simplified
|
||||||
|
try:
|
||||||
|
if self.numerator == simplified.numerator:
|
||||||
|
return self
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
simplified._ancestor = self
|
||||||
|
return simplified
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -148,7 +148,11 @@ class Linear(Polynomial):
|
|||||||
>>> P.differentiate()
|
>>> P.differentiate()
|
||||||
<Integer 2>
|
<Integer 2>
|
||||||
>>> P.roots
|
>>> P.roots
|
||||||
[<Fraction - 2 / 1>]
|
[<Integer - 2>]
|
||||||
|
>>> for i in P.roots[0].explain():
|
||||||
|
... print(i)
|
||||||
|
- 2 / 1
|
||||||
|
- 2
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -190,9 +194,20 @@ class Linear(Polynomial):
|
|||||||
>>> from ...core.MO.polynomial import MOpolynomial, MOMonomial
|
>>> from ...core.MO.polynomial import MOpolynomial, MOMonomial
|
||||||
>>> P = Linear(MOpolynomial('x', [1, 2]))
|
>>> P = Linear(MOpolynomial('x', [1, 2]))
|
||||||
>>> P.roots
|
>>> P.roots
|
||||||
[<Fraction - 2 / 1>]
|
[<Integer - 2>]
|
||||||
>>> #P = Linear(MOpolynomial('x', [1, -2]))
|
>>> P = Linear(MOpolynomial('x', [2, 1]))
|
||||||
>>> #P.roots
|
>>> P.roots
|
||||||
|
[<Fraction - 1 / 2>]
|
||||||
|
>>> for i in P.roots[0].explain():
|
||||||
|
... print(i)
|
||||||
|
- 1 / 2
|
||||||
|
>>> P = Linear(MOpolynomial('x', [10, 6]))
|
||||||
|
>>> P.roots
|
||||||
|
[<Fraction - 3 / 5>]
|
||||||
|
>>> for i in P.roots[0].explain():
|
||||||
|
... print(i)
|
||||||
|
- 6 / 10
|
||||||
|
- 3 / 5
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user