2018-09-17 16:18:29 +00:00
|
|
|
#! /usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim:fenc=utf-8
|
|
|
|
#
|
|
|
|
# Copyright © 2017 lafrite <lafrite@Poivre>
|
|
|
|
#
|
|
|
|
# Distributed under terms of the MIT license.
|
|
|
|
|
|
|
|
"""
|
2018-09-20 16:40:04 +00:00
|
|
|
Generate and compute like a student!
|
|
|
|
|
|
|
|
:example:
|
|
|
|
|
|
|
|
>>> e = Expression.from_str("2+3*4")
|
|
|
|
>>> e_simplified = e.simplify()
|
|
|
|
>>> print(e_simplified)
|
|
|
|
14
|
|
|
|
>>> for s in e_simplified.explain():
|
|
|
|
... print(s)
|
|
|
|
2 + 3 * 4
|
|
|
|
2 + 12
|
|
|
|
14
|
2018-10-10 08:40:40 +00:00
|
|
|
|
2018-11-19 11:23:37 +00:00
|
|
|
|
2018-09-20 16:40:04 +00:00
|
|
|
>>> e = Expression.from_str("2+3/2")
|
|
|
|
>>> e_simplified = e.simplify()
|
|
|
|
>>> print(e_simplified)
|
|
|
|
7 / 2
|
|
|
|
>>> for s in e_simplified.explain():
|
|
|
|
... print(s)
|
|
|
|
2 + 3 / 2
|
|
|
|
2 / 1 + 3 / 2
|
|
|
|
(2 * 2) / (1 * 2) + 3 / 2
|
|
|
|
4 / 2 + 3 / 2
|
|
|
|
(4 + 3) / 2
|
|
|
|
7 / 2
|
2018-10-10 08:40:40 +00:00
|
|
|
|
2018-11-19 11:23:37 +00:00
|
|
|
>>> e = Expression.from_str("(2+3)/2 + 1")
|
|
|
|
>>> e_simplified = e.simplify()
|
|
|
|
>>> print(e_simplified)
|
|
|
|
7 / 2
|
|
|
|
>>> for s in e_simplified.explain():
|
|
|
|
... print(s)
|
|
|
|
(2 + 3) / 2 + 1
|
|
|
|
5 / 2 + 1
|
|
|
|
5 / 2 + 1 / 1
|
|
|
|
5 / 2 + (1 * 2) / (1 * 2)
|
|
|
|
5 / 2 + 2 / 2
|
|
|
|
(5 + 2) / 2
|
|
|
|
7 / 2
|
|
|
|
|
2018-10-10 08:13:58 +00:00
|
|
|
>>> e = Expression.from_str("(2/3)^4")
|
|
|
|
>>> e_simplified = e.simplify()
|
|
|
|
>>> print(e_simplified)
|
|
|
|
16 / 81
|
|
|
|
>>> for s in e_simplified.explain():
|
|
|
|
... print(s)
|
|
|
|
(2 / 3)^4
|
|
|
|
2^4 / 3^4
|
|
|
|
16 / 81
|
2018-10-10 08:40:40 +00:00
|
|
|
|
2018-10-10 08:28:38 +00:00
|
|
|
>>> e = Expression.from_str("x^2*x*x^4")
|
|
|
|
>>> e_simplified = e.simplify()
|
2018-12-07 10:23:05 +00:00
|
|
|
>>> e_simplified
|
|
|
|
<Polynomial x^7>
|
2018-10-10 08:28:38 +00:00
|
|
|
>>> for s in e_simplified.explain():
|
|
|
|
... print(s)
|
|
|
|
x^2 * x * x^4
|
|
|
|
x^3 * x^4
|
|
|
|
x^(3 + 4)
|
|
|
|
x^7
|
2018-11-19 11:23:37 +00:00
|
|
|
|
|
|
|
>>> e = Expression.from_str("2x+2+3x")
|
|
|
|
>>> e_simplified = e.simplify()
|
2018-12-07 10:23:05 +00:00
|
|
|
>>> e_simplified
|
|
|
|
<Linear 5x + 2>
|
2018-11-19 11:23:37 +00:00
|
|
|
>>> for s in e_simplified.explain():
|
|
|
|
... print(s)
|
|
|
|
2x + 2 + 3x
|
|
|
|
2x + 3x + 2
|
|
|
|
(2 + 3) * x + 2
|
|
|
|
5x + 2
|
2018-11-19 14:46:39 +00:00
|
|
|
|
2018-11-21 08:43:35 +00:00
|
|
|
>>> e = Expression.from_str("1+2x^2+3x+4+5x")
|
2018-11-19 14:46:39 +00:00
|
|
|
>>> e_simplified = e.simplify()
|
2018-12-07 10:23:05 +00:00
|
|
|
>>> e_simplified
|
2019-10-30 20:12:58 +00:00
|
|
|
<Quadratic 2x^2 + 8x + 5>
|
2018-11-21 08:37:17 +00:00
|
|
|
>>> for s in e_simplified.explain():
|
|
|
|
... print(s)
|
2018-11-21 08:43:35 +00:00
|
|
|
1 + 2x^2 + 3x + 4 + 5x
|
2019-10-30 20:12:58 +00:00
|
|
|
2x^2 + 3x + 1 + 4 + 5x
|
|
|
|
2x^2 + 3x + 5x + 1 + 4
|
|
|
|
2x^2 + (3 + 5) * x + 5
|
|
|
|
2x^2 + 8x + 5
|
|
|
|
|
2018-11-21 08:43:35 +00:00
|
|
|
|
2018-12-21 10:36:40 +00:00
|
|
|
>>> e = Expression.from_str("(2x+3)^2")
|
|
|
|
>>> e_simplified = e.simplify()
|
|
|
|
>>> e_simplified
|
2019-10-30 20:12:58 +00:00
|
|
|
<Quadratic 4x^2 + 12x + 9>
|
2018-12-21 10:36:40 +00:00
|
|
|
>>> for s in e_simplified.explain():
|
|
|
|
... print(s)
|
|
|
|
(2x + 3)^2
|
|
|
|
(2x + 3)(2x + 3)
|
|
|
|
2x * 2x + 2x * 3 + 3 * 2x + 3 * 3
|
|
|
|
2 * 2 * x^(1 + 1) + 3 * 2 * x + 3 * 2 * x + 9
|
|
|
|
6x + 6x + 4x^2 + 9
|
|
|
|
(6 + 6) * x + 4x^2 + 9
|
2019-10-30 20:12:58 +00:00
|
|
|
4x^2 + 12x + 9
|
|
|
|
|
2020-12-15 13:36:39 +00:00
|
|
|
>>> e = Expression.from_str("(2x-3)(-x+2)")
|
2020-12-12 22:14:44 +00:00
|
|
|
>>> e_simplified = e.simplify()
|
|
|
|
>>> e_simplified
|
2020-12-15 13:36:39 +00:00
|
|
|
<Quadratic - 2x^2 + 7x - 6>
|
2020-12-12 22:14:44 +00:00
|
|
|
>>> for s in e_simplified.explain():
|
|
|
|
... print(s)
|
2020-12-15 14:37:27 +00:00
|
|
|
(2x - 3)(- x + 2)
|
|
|
|
(2x - 3)(- x + 2)
|
|
|
|
2x(- x) + 2x * 2 - 3(- x) - 3 * 2
|
2020-12-15 15:01:01 +00:00
|
|
|
2(- 1) * x^(1 + 1) + 2 * 2 * x - 3(- 1) * x - 6
|
2020-12-15 14:37:27 +00:00
|
|
|
4x + 3x - 2x^2 - 6
|
|
|
|
(4 + 3) * x - 2x^2 - 6
|
|
|
|
- 2x^2 + 7x - 6
|
2018-09-17 16:18:29 +00:00
|
|
|
"""
|
|
|
|
|
2021-09-25 16:05:05 +00:00
|
|
|
from .renders import render
|
2018-09-17 16:18:29 +00:00
|
|
|
from .expression import Expression
|
2020-12-12 22:14:44 +00:00
|
|
|
from .tokens import Token
|
2021-02-04 09:18:12 +00:00
|
|
|
from .tokens.polynomial import Polynomial
|
2020-12-15 14:37:27 +00:00
|
|
|
from .tokens.number import Integer, Decimal, Fraction
|
2020-12-12 22:14:44 +00:00
|
|
|
|
|
|
|
|
2018-11-19 11:23:37 +00:00
|
|
|
if __name__ == "__main__":
|
2020-12-15 13:36:39 +00:00
|
|
|
e = Expression.from_str("(2x-3)(-x+2)")
|
|
|
|
e_simplified = e.simplify()
|
|
|
|
e_simplified
|
|
|
|
for s in e_simplified.explain():
|
|
|
|
print(s._tree.map_on_leaf(lambda x: type(x)))
|
|
|
|
print(s)
|
2018-09-17 16:18:29 +00:00
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|