Poly expands the expression

33 views
Skip to first unread message

Paul Royik

unread,
Nov 20, 2021, 2:59:08 AM11/20/21
to sympy
Poly((x-1)**2, x) returns Poly(x**2-2x+1, x).
Is it possbile to retain the expression as it is, i.e. (x-1)**2 ?

Oscar Benjamin

unread,
Nov 20, 2021, 5:19:16 PM11/20/21
to sympy
On Sat, 20 Nov 2021 at 07:59, Paul Royik <distan...@gmail.com> wrote:
>
> Poly((x-1)**2, x) returns Poly(x**2-2x+1, x).
> Is it possbile to retain the expression as it is, i.e. (x-1)**2 ?

It is possible to retain the unexpanded expression: don't convert to Poly!

The internal representation of Poly cannot represent an unexpanded
power because it's just a list of coefficients:

In [6]: p = Poly((x - 1)**2, x)

In [7]: p
Out[7]: Poly(x**2 - 2*x + 1, x, domain='ZZ')

In [8]: p.rep
Out[8]: DMP([mpz(1), mpz(-2), mpz(1)], ZZ, None)

In [9]: p.rep.rep
Out[9]: [mpz(1), mpz(-2), mpz(1)]

This is the Dense Univariate Polynomial (DUP) representation:
https://docs.sympy.org/latest/modules/polys/domainsintro.html#dup-representation

--
Oscar

Chris Smith

unread,
Nov 21, 2021, 5:25:16 AM11/21/21
to sympy
Internally, Poly will expand it. It would be possible to make a printer that prints the expression in factored form, however. But why do you want to do that? What is the goal/problem?

/c

Paul Royik

unread,
Nov 21, 2021, 11:35:06 AM11/21/21
to sympy
Just to print it.

Paul Royik

unread,
Nov 22, 2021, 2:00:25 AM11/22/21
to sympy
Thanks to all!
Reply all
Reply to author
Forward
0 new messages