Feat(API): factory build every tokens!
This commit is contained in:
parent
21cc3b68a3
commit
bcf589c607
@ -61,8 +61,8 @@ Generate and compute like a student!
|
|||||||
|
|
||||||
>>> e = Expression.from_str("x^2*x*x^4")
|
>>> e = Expression.from_str("x^2*x*x^4")
|
||||||
>>> e_simplified = e.simplify()
|
>>> e_simplified = e.simplify()
|
||||||
>>> print(e_simplified)
|
>>> e_simplified
|
||||||
x^7
|
<Polynomial x^7>
|
||||||
>>> for s in e_simplified.explain():
|
>>> for s in e_simplified.explain():
|
||||||
... print(s)
|
... print(s)
|
||||||
x^2 * x * x^4
|
x^2 * x * x^4
|
||||||
@ -72,8 +72,8 @@ x^7
|
|||||||
|
|
||||||
>>> e = Expression.from_str("2x+2+3x")
|
>>> e = Expression.from_str("2x+2+3x")
|
||||||
>>> e_simplified = e.simplify()
|
>>> e_simplified = e.simplify()
|
||||||
>>> print(e_simplified)
|
>>> e_simplified
|
||||||
5x + 2
|
<Linear 5x + 2>
|
||||||
>>> for s in e_simplified.explain():
|
>>> for s in e_simplified.explain():
|
||||||
... print(s)
|
... print(s)
|
||||||
2x + 2 + 3x
|
2x + 2 + 3x
|
||||||
@ -83,8 +83,8 @@ x^7
|
|||||||
|
|
||||||
>>> e = Expression.from_str("1+2x^2+3x+4+5x")
|
>>> e = Expression.from_str("1+2x^2+3x+4+5x")
|
||||||
>>> e_simplified = e.simplify()
|
>>> e_simplified = e.simplify()
|
||||||
>>> print(e_simplified)
|
>>> e_simplified
|
||||||
5 + 2x^2 + 8x
|
<Quadratic 5 + 2x^2 + 8x>
|
||||||
>>> for s in e_simplified.explain():
|
>>> for s in e_simplified.explain():
|
||||||
... print(s)
|
... print(s)
|
||||||
1 + 2x^2 + 3x + 4 + 5x
|
1 + 2x^2 + 3x + 4 + 5x
|
||||||
|
@ -23,27 +23,70 @@ __all__ = ["factory"]
|
|||||||
|
|
||||||
def factory(exp, name="", ancestor=None):
|
def factory(exp, name="", ancestor=None):
|
||||||
""" Transform a Expression with on MathObject (from core) to a appropriate token (from API)
|
""" Transform a Expression with on MathObject (from core) to a appropriate token (from API)
|
||||||
|
|
||||||
|
:example:
|
||||||
|
>>> from ..expression import Expression
|
||||||
|
>>> a = Expression(MOnumber(2))
|
||||||
|
>>> factory(a)
|
||||||
|
<Integer 2>
|
||||||
|
>>> a = Expression(MOnumber(2.5))
|
||||||
|
>>> factory(a)
|
||||||
|
<Decimal 2.5>
|
||||||
|
>>> a = Expression(MOFraction(2, 5))
|
||||||
|
>>> factory(a)
|
||||||
|
<Fraction 2 / 5>
|
||||||
|
>>> a = Expression(MOstr('x'))
|
||||||
|
>>> factory(a)
|
||||||
|
<Linear x>
|
||||||
|
>>> a = Expression(MOstrPower('x', 2))
|
||||||
|
>>> factory(a)
|
||||||
|
<Quadratic x^2>
|
||||||
|
>>> a = Expression(MOstrPower('x', 3))
|
||||||
|
>>> factory(a)
|
||||||
|
<Polynomial x^3>
|
||||||
|
>>> a = Expression(MOMonomial(3, 'x', 1))
|
||||||
|
>>> factory(a)
|
||||||
|
<Linear 3x>
|
||||||
|
>>> a = Expression(MOMonomial(3, 'x', 2))
|
||||||
|
>>> factory(a)
|
||||||
|
<Quadratic 3x^2>
|
||||||
|
>>> a = Expression(MOMonomial(3, 'x', 3))
|
||||||
|
>>> factory(a)
|
||||||
|
<Polynomial 3x^3>
|
||||||
"""
|
"""
|
||||||
mo = exp._tree
|
mo = exp._tree
|
||||||
if not isinstance(mo, MO):
|
if not isinstance(mo, MO):
|
||||||
raise TypeError(f"Can't build Token from not computed Expression (got {mo})")
|
raise TypeError(f"Can't build Token from not computed Expression (got {mo})")
|
||||||
|
|
||||||
if isinstance(mo, MOnumber):
|
if isinstance(mo, MOnumber):
|
||||||
if isinstance(mo.value, int):
|
if isinstance(mo.value, int):
|
||||||
return Integer(mo, name, ancestor)
|
return Integer(mo, name, ancestor)
|
||||||
elif isinstance(mo.value, _Decimal):
|
elif isinstance(mo.value, _Decimal):
|
||||||
return Decimal(mo, name, ancestor)
|
return Decimal(mo, name, ancestor)
|
||||||
else:
|
|
||||||
raise TypeError(f"Can't build from MOnumber ({mo}) neither int nor decimal")
|
raise TypeError(f"Can't build from MOnumber ({mo}) neither int nor decimal")
|
||||||
|
|
||||||
elif isinstance(mo, MOFraction):
|
elif isinstance(mo, MOFraction):
|
||||||
if isinstance(mo._denominator, MOnumber) and \
|
if isinstance(mo._denominator, MOnumber) and \
|
||||||
isinstance(mo._numerator, MOnumber):
|
isinstance(mo._numerator, MOnumber):
|
||||||
return Fraction(mo, name, ancestor)
|
return Fraction(mo, name, ancestor)
|
||||||
else:
|
|
||||||
raise TypeError(f"Can't build from MOFraction ({mo}) numerator and denominator are not MOnumber")
|
raise TypeError(f"Can't build from MOFraction ({mo}) numerator and denominator are not MOnumber")
|
||||||
|
|
||||||
elif isinstance(mo, (MOstr, MOstrPower, MOMonomial, MOpolynomial)):
|
elif isinstance(mo, (MOstr, MOstrPower, MOMonomial, MOpolynomial)):
|
||||||
raise TypeError(f"Can't build Polynom yet")
|
if not isinstance(mo._variable, (MOstr, str)):
|
||||||
|
raise TypeError(f"Can't build Polynom over something else than a letter (got {mo._variable})")
|
||||||
|
if isinstance(mo, MOstr) or \
|
||||||
|
(isinstance(mo, MOMonomial) and mo.power.value == 1) or \
|
||||||
|
(isinstance(mo, MOpolynomial) and mo.power.value == 1):
|
||||||
|
return Linear(mo, name, ancestor)
|
||||||
|
elif (isinstance(mo, MOstrPower) and mo.power.value == 2) or \
|
||||||
|
(isinstance(mo, MOMonomial) and mo.power.value == 2) or \
|
||||||
|
(isinstance(mo, MOpolynomial) and mo.power.value == 2):
|
||||||
|
return Quadratic(mo, name, ancestor)
|
||||||
|
else:
|
||||||
|
return Polynomial(mo, name, ancestor)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise TypeError(f"{type(mo)} is unknown MathObject")
|
raise TypeError(f"{type(mo)} is unknown MathObject")
|
||||||
|
|
||||||
|
@ -19,10 +19,6 @@ class Polynomial(Token):
|
|||||||
""" Token representing a polynomial """
|
""" Token representing a polynomial """
|
||||||
|
|
||||||
def __init__(self, mo, name="", ancestor=None):
|
def __init__(self, mo, name="", ancestor=None):
|
||||||
if not isinstance(mo, MOpolynomial):
|
|
||||||
raise TypeError
|
|
||||||
if not isinstance(mo.value, int):
|
|
||||||
raise TypeError
|
|
||||||
|
|
||||||
Token.__init__(self, mo, name, ancestor)
|
Token.__init__(self, mo, name, ancestor)
|
||||||
self._mathtype = 'polynome'
|
self._mathtype = 'polynome'
|
||||||
@ -48,10 +44,6 @@ class Linear(Token):
|
|||||||
""" Token representing a linear """
|
""" Token representing a linear """
|
||||||
|
|
||||||
def __init__(self, mo, name="", ancestor=None):
|
def __init__(self, mo, name="", ancestor=None):
|
||||||
if not isinstance(mo, MOpolynomial):
|
|
||||||
raise TypeError
|
|
||||||
if not isinstance(mo.value, int):
|
|
||||||
raise TypeError
|
|
||||||
|
|
||||||
Token.__init__(self, mo, name, ancestor)
|
Token.__init__(self, mo, name, ancestor)
|
||||||
self._mathtype = 'affine'
|
self._mathtype = 'affine'
|
||||||
@ -65,10 +57,6 @@ class Quadratic(Token):
|
|||||||
""" Token representing a quadratic """
|
""" Token representing a quadratic """
|
||||||
|
|
||||||
def __init__(self, mo, name="", ancestor=None):
|
def __init__(self, mo, name="", ancestor=None):
|
||||||
if not isinstance(mo, MOpolynomial):
|
|
||||||
raise TypeError
|
|
||||||
if not isinstance(mo.value, int):
|
|
||||||
raise TypeError
|
|
||||||
|
|
||||||
Token.__init__(self, mo, name, ancestor)
|
Token.__init__(self, mo, name, ancestor)
|
||||||
self._mathtype = 'polynome du 2nd degré'
|
self._mathtype = 'polynome du 2nd degré'
|
||||||
|
Loading…
Reference in New Issue
Block a user