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

Zero degree polynomial copy() error #8210

Open
dreqju opened this Issue Oct 7, 2014 · 5 comments

Comments

Projects
None yet
5 participants
@dreqju
Copy link

dreqju commented Oct 7, 2014

I have a list of polynomials that I need to deepcopy(). But thet procedure generates error on polynomials of zero degree. For example:

In [1]: p = Poly([0], x)
In [2]: p.copy() 

I get GeneratorsNeeded: specify generators to give 0 a meaning. And I want to have that Poly([0],x) as a neutral element of addition.

Temporarily I use something like Poly([10**(-10), 0], x) but it is not very elegant, is it?

@mattpap

This comment has been minimized.

Copy link
Member

mattpap commented Oct 7, 2014

This happens because Poly.args doesn't do what Basic (the parent class of Poly) expects it to do. This should be fixed, but for now you can use simply Poly(p) to copy a polynomial, e.g.:

In [1]: p = Poly(0, x)

In [2]: p
Out[2]: Poly(0, x, domain='ZZ')

In [3]: p += 1

In [4]: p
Out[4]: Poly(1, x, domain='ZZ')

In [5]: p2 = Poly(p)

In [6]: p2
Out[6]: Poly(1, x, domain='ZZ')

In [7]: p2 += 1

In [8]: p2
Out[8]: Poly(2, x, domain='ZZ')

In [9]: p
Out[9]: Poly(1, x, domain='ZZ')

Also note that you don't have to use Poly([0], x), because Poly(0, x) does the same and is shorter to write. Also, SymPy is a computer algebra library and types of coefficients matter, so Poly([10**(-10), 0], x) may not be exactly what you want (e.g. different algorithms are applied):

In [1]: Poly([10**(-10), 0], x)
Out[1]: Poly(1.0e-10*x, x, domain='RR')
@dreqju

This comment has been minimized.

Copy link

dreqju commented Oct 7, 2014

The code I'm working on is still evolving but as for now I have a list of lists of the form [interval_from, interval_to, Poly] and I needed to make a copy of that list. Poly(p) is obviously a way to do that but would require me to write my own function. Using copy.deepcopy(my_list) was clearly shorter and maybe more reliable, so I was surprised to see that polynomials of zero degree fail to produce their own copies.

This clearly needs to be fixed.
And by the way, Poly([10**(-10), 0], x) in my case works as expected.

@smichr

This comment has been minimized.

Copy link
Member

smichr commented Oct 7, 2014

Should gens be part of Poly.args?

>>> Poly(0,x).args
(0,)
>>> Poly(0,x).gens
(x,)
>>> self=Poly(0,x)
>>> self.func(*(self.args+self.gens))  # this fails if the gens are not included
Poly(0, x, domain='ZZ')
@pbrady

This comment has been minimized.

Copy link
Member

pbrady commented Oct 7, 2014

I would strongly advise against using deepcopy #7672

@mattpap

This comment has been minimized.

Copy link
Member

mattpap commented Oct 7, 2014

Should gens be part of Poly.args?

Perhaps, yes, but I'm pretty sure many things will break. Anyway, it's insufficient to reconstruct polynomial based on an expression and generators. You would have to include options as well, e.g. consider Poly(0, x, domain=QQ). If you are going to experiment with this, then just note that __eq__ performs unification, so you will get True more often than reprs/opts would suggest.

@skirpichev skirpichev added the polys label Oct 8, 2014

skirpichev added a commit to skirpichev/diofant that referenced this issue Sep 18, 2018

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