Opened 6 years ago

Last modified 6 years ago

# coefficients() function more consistent between Expressions and polynomial rings

Reported by: Owned by: JoalHeagney minor sage-6.6 algebra coeffs, rings, polynomials, expression, symbolic N/A

### Description (last modified by rws)

The different behaviour between the two rings consists of

1. the `coefficients(sparse=True)` (which is default) method returns a list of pairs in `SR`, and a list in `PolynomialRing`,
2. `Expression.dict()` does not exist.

Example:

```y = 3*x^3 + 2*x^2 - 4*x
print(y)
type(y)
```

Gives:

```3*x^3 + 2*x^2 - 4*x
<type 'sage.symbolic.expression.Expression'>
```

And

```M = matrix(SR,[[1,2],[0,-2]])
ch = M.charpoly()
print(ch)
type(ch)
```

gives

```x^2 + x - 2
<class 'sage.rings.polynomial.polynomial_element_generic.Polynomial_generic_dense_field'>
```

But:

```y.coeffs()
```

returns

```[[−4,1],[2,2],[3,3]]
```

and

```ch.coeffs()
```

returns

```[−2,1,1]
```

I'd prefer if these two functions returned the same format, preferably the Expression format, as having access to the index allows list comprehension tastiness.

### comment:1 Changed 6 years ago by JoalHeagney

• Keywords rings polynomials expression symbolic added
• Type changed from PLEASE CHANGE to enhancement

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

Have list comprehension tastiness with `dict`:

```sage: ch.dict().values()
[-2, 1, 1]
```

In #17518 we started being more consistent by deprecating `coeffs`.

### comment:3 follow-up: ↓ 5 Changed 6 years ago by JoalHeagney

That works for polynomial rings, are there plans to add that to sage.symbolic.expression.Expressions?

### comment:4 Changed 6 years ago by rws

• Description modified (diff)
• Summary changed from coeffs() function more consistent between Expressions and polynomial rings to coefficients() function more consistent between Expressions and polynomial rings

Clarified the ticket description.

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

Replying to JoalHeagney:

That works for polynomial rings, are there plans to add that to sage.symbolic.expression.Expressions?

That would not be difficult (in comparison). If you expect it then to behave identically you will be disappointed however, because symbolics have no default generator (although `Expression.coefficients()` has the lexically first occuring var hard-wired when no varname is given).

```sage: var('a,b,c')
(a, b, c)
sage: (a+2*a^2+3*b).list()
[3*b, 1, 2]
sage: (3*b+a+2*a^2).list()
[3*b, 1, 2]
sage: (3*b+c+2*c^2).list()
[2*c^2 + c, 3]
```

so you could never have true polymorphism.

Note: See TracTickets for help on using tickets.