|
17 | 17 |
|
18 | 18 | import sympy |
19 | 19 |
|
20 | | -from mathics.builtin.inference import evaluate_predicate |
21 | 20 | from mathics.builtin.options import options_to_rules |
22 | 21 | from mathics.builtin.scoping import dynamic_scoping |
23 | 22 | from mathics.core.atoms import Integer, Integer0, Integer1, Number, RationalOneHalf |
|
63 | 62 | SymbolTable, |
64 | 63 | SymbolTanh, |
65 | 64 | ) |
66 | | -from mathics.eval.numbers.algebra.simplify import default_complexity_function |
| 65 | +from mathics.eval.numbers.algebra.simplify import ( |
| 66 | + default_complexity_function, |
| 67 | + eval_Simplify, |
| 68 | +) |
67 | 69 | from mathics.eval.numbers.numbers import cancel, sympy_factor |
68 | 70 | from mathics.eval.parts import walk_parts |
69 | 71 | from mathics.eval.patterns import match |
@@ -1496,7 +1498,10 @@ def eval_list(self, expr, vars, evaluation): |
1496 | 1498 | # FullSimplify |
1497 | 1499 | class Simplify(Builtin): |
1498 | 1500 | r""" |
1499 | | - <url>:WMA link: |
| 1501 | + <url>:SymPy: |
| 1502 | + https://docs.sympy.org/latest/modules/simplify |
| 1503 | + /simplify.html</url>, <url> |
| 1504 | + :WMA: |
1500 | 1505 | https://reference.wolfram.com/language/ref/Simplify.html</url> |
1501 | 1506 |
|
1502 | 1507 | <dl> |
@@ -1605,65 +1610,9 @@ def eval(self, expr, evaluation, options={}): |
1605 | 1610 | {"System`$Assumptions": assumptions}, |
1606 | 1611 | evaluation, |
1607 | 1612 | ) |
1608 | | - return self.do_apply(expr, evaluation, options) |
1609 | | - |
1610 | | - def do_apply(self, expr, evaluation, options={}): |
1611 | | - # Check first if we are dealing with a logic expression... |
1612 | | - if expr in (SymbolTrue, SymbolFalse, SymbolList): |
1613 | | - return expr |
1614 | | - |
1615 | | - # ``evaluate_predicate`` tries to reduce expr taking into account |
1616 | | - # the assumptions established in ``$Assumptions``. |
1617 | | - expr = evaluate_predicate(expr, evaluation) |
1618 | | - |
1619 | | - # If we get an atom, return it. |
1620 | | - if isinstance(expr, Atom): |
1621 | | - return expr |
1622 | | - |
1623 | | - # Now, try to simplify the elements. |
1624 | | - # TODO: Consider to move this step inside ``evaluate_predicate``. |
1625 | | - # Notice that here we want to pass through the full evaluation process |
1626 | | - # to use all the defined rules... |
1627 | | - name = self.get_name() |
1628 | | - symbol_name = Symbol(name) |
1629 | | - elements = [ |
1630 | | - Expression(symbol_name, element).evaluate(evaluation) |
1631 | | - for element in expr._elements |
1632 | | - ] |
1633 | | - head = Expression(symbol_name, expr.get_head()).evaluate(evaluation) |
1634 | | - expr = Expression(head, *elements) |
1635 | 1613 |
|
1636 | | - # At this point, we used all the tools available in Mathics. |
1637 | | - # If the expression has a sympy form, try to use it. |
1638 | | - # Now, convert the expression to sympy |
1639 | | - sympy_expr = expr.to_sympy() |
1640 | | - # If the expression cannot be handled by Sympy, just return it. |
1641 | | - if sympy_expr is None: |
1642 | | - return expr |
1643 | | - # Now, try to simplify using sympy |
1644 | | - complexity_function = options.get("System`ComplexityFunction", None) |
1645 | | - if complexity_function is None or complexity_function is SymbolAutomatic: |
1646 | | - |
1647 | | - def _default_complexity_function(x): |
1648 | | - return default_complexity_function(from_sympy(x)) |
1649 | | - |
1650 | | - complexity_function = _default_complexity_function |
1651 | | - else: |
1652 | | - if isinstance(complexity_function, (Expression, Symbol)): |
1653 | | - _complexity_function = complexity_function |
1654 | | - complexity_function = ( |
1655 | | - lambda x: Expression(_complexity_function, from_sympy(x)) |
1656 | | - .evaluate(evaluation) |
1657 | | - .to_python() |
1658 | | - ) |
1659 | | - |
1660 | | - # At this point, ``complexity_function`` is a function that takes a |
1661 | | - # sympy expression and returns an integer. |
1662 | | - sympy_result = sympy.simplify(sympy_expr, measure=complexity_function) |
1663 | | - |
1664 | | - # and bring it back |
1665 | | - result = from_sympy(sympy_result).evaluate(evaluation) |
1666 | | - return result |
| 1614 | + symbol_name = Symbol(self.get_name()) |
| 1615 | + return eval_Simplify(symbol_name, expr, evaluation, options) |
1667 | 1616 |
|
1668 | 1617 |
|
1669 | 1618 | class FullSimplify(Simplify): |
|
0 commit comments