diff --git a/mathics/algorithm/optimizers.py b/mathics/algorithm/optimizers.py index 71d5a9981..60a5f9e2f 100644 --- a/mathics/algorithm/optimizers.py +++ b/mathics/algorithm/optimizers.py @@ -202,8 +202,8 @@ def find_root_secant(f, x0, x, opts, evaluation) -> (Number, bool): return x0, False if not isinstance(f1, Number): return x0, False - f0 = f0.to_python(n_evaluation=True) - f1 = f1.to_python(n_evaluation=True) + f0 = eval_N(f0, evaluation).to_python() + f1 = eval_N(f1, evaluation).to_python() count = 0 while count < maxit: if f0 == f1: @@ -224,7 +224,7 @@ def find_root_secant(f, x0, x, opts, evaluation) -> (Number, bool): ) if not isinstance(f1, Number): return x0, False - f1 = f1.to_python(n_evaluation=True) + f1 = eval_N(f1, evaluation).to_python() continue inv_deltaf = from_python(1.0 / (f1 - f0)) @@ -240,7 +240,7 @@ def find_root_secant(f, x0, x, opts, evaluation) -> (Number, bool): ) if not isinstance(f2, Number): return x0, False - f2 = f2.to_python(n_evaluation=True) + f2 = eval_N(f2, evaluation).to_python() f1, f0 = f2, f1 x1, x0 = x2, x1 if x1 == x0 or abs(f2) == 0: diff --git a/mathics/core/expression.py b/mathics/core/expression.py index b25a62d77..87c2021f1 100644 --- a/mathics/core/expression.py +++ b/mathics/core/expression.py @@ -1396,23 +1396,39 @@ def to_python(self, *args, **kwargs): String -> '"..."' Function -> python function numbers -> Python number + If kwarg n_evaluation is given, apply N first to the expression. """ from mathics.builtin.base import mathics_to_python - n_evaluation = kwargs.get("n_evaluation", None) - assert n_evaluation is None - head = self._head if head is SymbolFunction: - from mathics.core.convert.function import expression_to_callable_and_args - vars, expr_fn = self.elements - return expression_to_callable_and_args(expr_fn, vars, n_evaluation) + n_evaluation = kwargs.get("n_evaluation", None) + if n_evaluation: + vars, expr_fn = self.elements + return expression_to_callable_and_args(expr_fn, vars, n_evaluation) + # After some discussion, it seems that this parameter + # is not useful anymore. Let's consider to remove it + # in future versions. + n_evaluation = kwargs.get("n_evaluation", None) + # assert n_evaluation is None if n_evaluation is not None: - value = Expression(SymbolN, self).evaluate(n_evaluation) + from mathics.core.evaluators import eval_N + + # Eventually, we want to remove this parameter. + # + # import warnings + # warnings.warn( + # ( + # "use of expr.to_python(n_evaluation) is deprecated." + # "Use instead eval_N(expr, evaluation).to_python()" + # ), + # DeprecationWarning, + # ) + value = eval_N(self, n_evaluation) return value.to_python() if head is SymbolDirectedInfinity and len(self._elements) == 1: diff --git a/mathics/core/symbols.py b/mathics/core/symbols.py index 61f70add1..190d5c1b3 100644 --- a/mathics/core/symbols.py +++ b/mathics/core/symbols.py @@ -594,14 +594,15 @@ def to_python(self, *args, python_form: bool = False, **kwargs): # This was introduced before `mathics.eval.nevaluator.eval_N` # provided a simple way to convert an expression into a number. # Now it makes this routine harder to describe. + # Consider remove this in future versions. n_evaluation = kwargs.get("n_evaluation") if n_evaluation is not None: - import warnings + # import warnings - warnings.warn( - "use instead ``eval_N(obj, evaluation).to_python()``", - DeprecationWarning, - ) + # warnings.warn( + # "use instead ``eval_N(obj, evaluation).to_python()``", + # DeprecationWarning, + # ) from mathics.eval.nevaluator import eval_N