sagemath / sage

Main repository of SageMath. Now open for Issues and Pull Requests.
https://www.sagemath.org
Other
1.2k stars 413 forks source link

Hold symbolic powers unevaluated if evaluation fails #21758

Open rwst opened 7 years ago

rwst commented 7 years ago

Automatic evaluation of symbolic expressions of "numerics" prevents construction of such expressions despite them being perfectly numerically approximatable (or otherwise useful) if exact evaluation in the algebraic sense fails. This is usually not a problem with function expressions (because of the easily added hold=True), and sums and products (?), but powers have to be constructed awkwardly:

            sage: SR(2) ^ SR(QQbar(sqrt(2)))
            Traceback (most recent call last):
            ...
            TypeError: no canonical coercion ...
            sage: SR(2).power(SR(QQbar(sqrt(2))), hold=True)
            2^1.414213562373095?

The ticket catches TypeErrors in the first case and automatically uses the second form, to allow:

            sage: SR(2) ^ SR(QQbar(sqrt(2)))
            2^1.414213562373095?

CC: @videlec

Component: symbolics

Author: Ralf Stephan

Branch/Commit: u/rws/hold_symbolic_powers_unevaluated_if_evaluation_fails @ 49ab133

Issue created by migration from https://trac.sagemath.org/ticket/21758

rwst commented 7 years ago

Branch: u/rws/hold_symbolic_powers_unevaluated_if_evaluation_fails

rwst commented 7 years ago

Author: Ralf Stephan

rwst commented 7 years ago

Commit: 49ab133

rwst commented 7 years ago

New commits:

49ab13321758: Hold symbolic powers unevaluated if evaluation fails
videlec commented 7 years ago
comment:3
sage: a = SR(2) ^ QQbar(3).sqrt()
sage: a
2^1.732050807568878?
sage: b = a + 1
sage: b
<repr(<sage.symbolic.expression.Expression at ...>) failed: TypeError: no canonical coercion from Algebraic Field to Rational Field>
videlec commented 7 years ago
comment:4

Why algebraic numbers behave differently from pi?

sage: a = 2 ^ pi.sqrt() + 1
sage: a
2^sqrt(pi) + 1
sage: a.n()
4.41634542118404

And why is pi not numeric?

sage: pi.is_numeric()
False
rwst commented 7 years ago
comment:5

Replying to @videlec:

Why algebraic numbers behave differently from pi?

sage: a = 2 ^ pi.sqrt() + 1
sage: a
2^sqrt(pi) + 1
sage: a.n()
4.41634542118404

Because traditionally in Pynac all Python objects that can't be converted to Pynac objects are wrapped inside a numeric object. Constants can be converted and are handled correctly. That's why pi is non-numeric.

rwst commented 7 years ago
comment:6

Replying to @videlec:

sage: a = SR(2) ^ QQbar(3).sqrt()
sage: a
2^1.732050807568878?
sage: b = a + 1
sage: b
<repr(<sage.symbolic.expression.Expression at ...>) failed: TypeError: no canonical coercion from Algebraic Field to Rational Field>

I have opened a Pynac issue although atm I have no idea where exactly to fix. https://github.com/pynac/pynac/issues/208