Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrong conversion from symbolic to SR[] #20453

Closed
rwst opened this issue Apr 17, 2016 · 4 comments
Closed

wrong conversion from symbolic to SR[] #20453

rwst opened this issue Apr 17, 2016 · 4 comments

Comments

@rwst
Copy link

rwst commented Apr 17, 2016

As long as generator and variable names can be associated there should not be a problem to convert from a symbolic expression to a univariate polynomial with symbolic coefficients, but:

sage: S.<y> = PolynomialRing(SR)
sage: y = SR.var('y')
sage: p = S(y^2 + pi*y + 1)
sage: p.coefficients()
[pi*y + y^2 + 1]

(Should be [1, pi, 1]).

Component: symbolics

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

@rwst rwst added this to the sage-7.2 milestone Apr 17, 2016
@nbruin
Copy link
Contributor

nbruin commented Apr 17, 2016

comment:1

I don't think you'll be able to reasonably code this. It will be too much of an exception. Base rings coerce into polynomial rings over them, by mapping them to constant polynomials. Conversions use coercion if available. The behaviour you're proposing is so deeply against the general rules that even if you get this particular case to work, I expect the exceptional behaviour will be causing problems in all kinds of other situations.

With the following you should be able to do what you want:

def mapcons(x,P):
  return lambda f: sum([c[0]*P.0^c[1] for c in f.coefficients(x)])

P=SR['x']
H= SetMorphism(Hom(SR,P),mapcons(x,P))
sage: var('y')
y
sage: H((x+y)^3).coefficients()
[y^3, 3*y^2, 3*y, 1]

The map you want, while reasonable and useful, doesn't fit with the generic rules that exist about conversion maps. That's not a problem. Just don't insist that every useful map needs to be a conversion map, define the map you do want, and use that. Perhaps we need better tools/documentation on how to conveniently define useful maps and homomorphisms?

Note that applying the map is not actually require longer syntax than conversion, so calling the map is already very convenient. Getting the map in the first place is perhaps a little more challenging and a little less discoverable presently.

@rwst
Copy link
Author

rwst commented Apr 18, 2016

comment:2

Thanks. That code looks useful and, indeed, could be advertised more widely. I expect this ticket to at least document which "problems in all kinds of other situations" this "exceptional behaviour will be causing", for demonstration purposes. That SR (i.e., reality) breaks all algebraist's rules is not new.

For context, the bigger problem this ticket is trying to help with is #20312.

@mezzarobba
Copy link
Member

comment:3

As far as I understand, Expression.polynomial() also knowns how to convert a symbolic expression into a polynomial in one of its variables.

@rwst
Copy link
Author

rwst commented Apr 18, 2016

comment:4

Replying to @mezzarobba:

As far as I understand, Expression.polynomial() also knowns how to convert a symbolic expression into a polynomial in one of its variables.

Or even into a multivariate. That is true. Where is my head? So the difficult part of #20312 is done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants