Ask Your Question

Does sympy break CyclotomicField?

asked 2023-08-17 13:49:15 +0200

Alex Karenin gravatar image

One can define a cyclotomic field as CyclotomicField(16) for example. But the code below crashes:

from sympy import *
File ~/Soft/sage/src/sage/rings/integer.pyx:714, in sage.rings.integer.Integer.__init__()
    712                 return
--> 714             raise TypeError("unable to coerce %s to an integer" % type(x))
    716 def __reduce__(self):

TypeError: unable to coerce <class 'sympy.core.numbers.Integer'> to an integer

Am I doing something wrong? Is my sage broken? I would like to use sympy and CyclotomicField in the same program. Specs: Sage 9.8, 10.0, Ubuntu 22.04.

edit retag flag offensive close merge delete


It's reproducible in Please file a bug report.

Max Alekseyev gravatar imageMax Alekseyev ( 2023-08-17 16:33:17 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2023-08-18 17:13:15 +0200

slelievre gravatar image

The breakage comes from importing SymPy's Integer.

Indeed, in Sage, the input 16 is preparsed as Integer(16).

Usually Integer means sage.rings.integer.Integer.

However after the line

from sympy import *

this is overridden and Integer now means sympy.core.numbers.Integer.

It seems the CyclotomicField constructor cannot cope with a SymPy Integer.

That should probably be fixed in Sage.

In the meantime, here are some possible workarounds. Each should work separately, no need to combine them.

a. Only import from sympy what you need, rather than *

b. After importing everything from SymPy as you did, restore Sage's Integer.

sage: reset('Integer')

c. Make sure to feed CyclotomicField an appropriate integer.

For instance, either a Python int or a Sage Integer, with one of the following:

sage: CyclotomicField(16r)  # r for raw -> Python int
sage: CyclotomicField(int(16))
sage: CyclotomicField(ZZ(16))
sage: CyclotomicField(sage.rings.integer.Integer(16))
edit flag offensive delete link more


Perhaps, CyclotomicField itself should perform conversion of a given argument to whatever integer type it wants.

Max Alekseyev gravatar imageMax Alekseyev ( 2023-08-18 18:09:44 +0200 )edit

I agree. A similar fix was contributed by Vincent Delecroix in

slelievre gravatar imageslelievre ( 2023-08-18 23:06:59 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2023-08-17 13:49:15 +0200

Seen: 99 times

Last updated: Aug 18 '23