Opened 6 years ago

Last modified 6 years ago

#21758 needs_work enhancement

Hold symbolic powers unevaluated if evaluation fails

Reported by: Ralf Stephan Owned by:
Priority: major Milestone: sage-7.5
Component: symbolics Keywords:
Cc: Vincent Delecroix Merged in:
Authors: Ralf Stephan Reviewers:
Report Upstream: N/A Work issues:
Branch: u/rws/hold_symbolic_powers_unevaluated_if_evaluation_fails (Commits, GitHub, GitLab) Commit: 49ab13301f5b44f946701f3fdcaff6c4613c0de7
Dependencies: Stopgaps:

Status badges

Description

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?

Change History (6)

comment:1 Changed 6 years ago by Ralf Stephan

Branch: u/rws/hold_symbolic_powers_unevaluated_if_evaluation_fails

comment:2 Changed 6 years ago by Ralf Stephan

Authors: Ralf Stephan
Cc: Vincent Delecroix added
Commit: 49ab13301f5b44f946701f3fdcaff6c4613c0de7
Status: newneeds_review

New commits:

49ab13321758: Hold symbolic powers unevaluated if evaluation fails

comment:3 Changed 6 years ago by Vincent Delecroix

Status: needs_reviewneeds_work
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>
Last edited 6 years ago by Vincent Delecroix (previous) (diff)

comment:4 Changed 6 years ago by Vincent Delecroix

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

comment:5 in reply to:  4 Changed 6 years ago by Ralf Stephan

Replying to vdelecroix:

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.

comment:6 in reply to:  3 Changed 6 years ago by Ralf Stephan

Replying to vdelecroix:

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

Note: See TracTickets for help on using tickets.