Opened 5 years ago

Closed 5 years ago

# wrong conversion from symbolic to SR[]

Reported by: Owned by: rws major sage-duplicate/invalid/wontfix symbolics N/A

### Description

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]`).

### comment:1 Changed 5 years ago by nbruin

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.

### comment:2 Changed 5 years ago by rws

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.

### comment:3 follow-up: ↓ 4 Changed 5 years ago by mmezzarobba

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

### comment:4 in reply to: ↑ 3 Changed 5 years ago by rws

• Milestone changed from sage-7.2 to sage-duplicate/invalid/wontfix
• Status changed from new to needs_review

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.

Last edited 5 years ago by rws (previous) (diff)

### comment:5 Changed 5 years ago by rws

• Status changed from needs_review to positive_review

### comment:6 Changed 5 years ago by vbraun

• Resolution set to fixed
• Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.