sage with python3, mpmath

75 views
Skip to first unread message

Александр Ватузов

unread,
Jan 14, 2020, 5:11:34 AM1/14/20
to sage-support
I have a problem with sage on python3, example sage9.0

simple code:

import mpmath
mpmath.mp.prec = 1000 #on 700 all is ok
mpmath.findroot(lambda x: x^2 - 3, 2)

error:
<repr(<sage.libs.mpmath.ext_main.mpf at 0x7fedbe2d84c8>) failed: SystemError: <built-in function divmod> returned a result with an error set>

Somebody else has this error? I will check on simple mpmath.

Jan Groenewald

unread,
Jan 14, 2020, 5:18:55 AM1/14/20
to sage-support
Hi

No error on sage 8.6; yes same error on  sage 9.0 (Debian 10).

Regards,
Jan


--
  .~.
  /V\     Jan Groenewald
 /( )\    www.aims.ac.za
 ^^-^^ 

Dima Pasechnik

unread,
Jan 14, 2020, 5:36:44 AM1/14/20
to sage-support
On Tue, Jan 14, 2020 at 10:18 AM Jan Groenewald <j...@aims.ac.za> wrote:
>
> Hi
>
> On Tue, 14 Jan 2020 at 12:11, Александр Ватузов <amva...@gmail.com> wrote:
>>
>> I have a problem with sage on python3, example sage9.0
>>
>> simple code:
>>
>> import mpmath
>> mpmath.mp.prec = 1000 #on 700 all is ok
>> mpmath.findroot(lambda x: x^2 - 3, 2)
>>
>> error:
>> <repr(<sage.libs.mpmath.ext_main.mpf at 0x7fedbe2d84c8>) failed: SystemError: <built-in function divmod> returned a result with an error set>
>>
>> Somebody else has this error? I will check on simple mpmath.
>
>
> No error on sage 8.6; yes same error on sage 9.0 (Debian 10).
>

mpmath version on Sage 8.6 is 1.0.0, and later and now it is 1.1.0

This might explain the problem, which maybe should be reported upstream:

https://github.com/fredrik-johansson/mpmath

> Regards,
> Jan
>
>
> --
> .~.
> /V\ Jan Groenewald
> /( )\ www.aims.ac.za
> ^^-^^
>
> --
> You received this message because you are subscribed to the Google Groups "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/CAAg%3Dp_0e6_fuCngtYa5LA1TteUH0KqQa5bukPbSGd%2BfypUMPVA%40mail.gmail.com.

Eric Gourgoulhon

unread,
Jan 14, 2020, 5:55:08 AM1/14/20
to sage-support
Le mardi 14 janvier 2020 11:36:44 UTC+1, Dima Pasechnik a écrit :

mpmath version on Sage 8.6 is 1.0.0, and later and now it is 1.1.0

This might explain the problem, which maybe should be reported upstream:

https://github.com/fredrik-johansson/mpmath



It's OK with Sage 8.9, which has already mpmath 1.1.0. So the problem is not due to the version of mpmath. Maybe a Python 2 / Python 3 issue?
Another data point: the issue is there in Sage 9.0.beta5 (Python 3).

Eric.
 

Eric Gourgoulhon

unread,
Jan 14, 2020, 5:59:50 AM1/14/20
to sage-support
PS: a similar error has been reported on ask.sagemath: https://ask.sagemath.org/question/49534/error-with-mpmathfindroot-on-89/

Dima Pasechnik

unread,
Jan 14, 2020, 6:00:44 AM1/14/20
to sage-support
It could be Python3, indeed. I see, on "pure" sage 9.0 python3:
>>> import mpmath
>>> mpmath.mp.prec = 1000 #on 700 all is ok
>>> mpmath.findroot(lambda x: x**2 - 3, 2) # must use ** for exponentiation - pure Python
OverflowError: Python int too large to convert to C long

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "sage/libs/mpmath/ext_main.pyx", line 1690, in
sage.libs.mpmath.ext_main.mpf_base.__repr__
(build/cythonized/sage/libs/mpmath/ext_main.c:21294)
File "/home/scratch2/dimpase/sage/sage/local/lib/python3.7/site-packages/mpmath/libmp/libmpf.py",
line 1242, in to_str
sign, digits, exponent = to_digits_exp(s, dps+3)
File "/home/scratch2/dimpase/sage/sage/local/lib/python3.7/site-packages/mpmath/libmp/libmpf.py",
line 1200, in to_digits_exp
digits = numeral(sd, base=10, size=dps)
File "/home/scratch2/dimpase/sage/sage/local/lib/python3.7/site-packages/mpmath/libmp/libintmath.py",
line 158, in numeral_python
A, B = divmod(n, base**half)
SystemError: <built-in function divmod> returned a result with an error set

> --
> You received this message because you are subscribed to the Google Groups "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/5d8b20d9-66b7-41e8-af08-6e3c82374856%40googlegroups.com.

Dima Pasechnik

unread,
Jan 14, 2020, 6:08:08 AM1/14/20
to sage-support
I have opened https://github.com/fredrik-johansson/mpmath/issues/503
to report this.
> --
> You received this message because you are subscribed to the Google Groups "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/5a7d5bb4-c406-4b8f-a757-370578307ed1%40googlegroups.com.

Александр Ватузов

unread,
Jan 14, 2020, 6:12:41 AM1/14/20
to sage-support
update:

mpmath works correct on Python 3.6.9 (no sage)

вторник, 14 января 2020 г., 13:11:34 UTC+3 пользователь Александр Ватузов написал:

Александр Ватузов

unread,
Jan 14, 2020, 6:16:59 AM1/14/20
to sage-support
sory, this I had reported :)

вторник, 14 января 2020 г., 13:59:50 UTC+3 пользователь Eric Gourgoulhon написал:

Александр Ватузов

unread,
Jan 14, 2020, 6:53:00 AM1/14/20
to sage-support
Will you open issue here? https://trac.sagemath.org/

Or I can try to do something

вторник, 14 января 2020 г., 14:08:08 UTC+3 пользователь Dima Pasechnik написал:
I have opened https://github.com/fredrik-johansson/mpmath/issues/503
to report this.

On Tue, Jan 14, 2020 at 10:59 AM Eric Gourgoulhon
<egourg...@gmail.com> wrote:
>
> PS: a similar error has been reported on ask.sagemath: https://ask.sagemath.org/question/49534/error-with-mpmathfindroot-on-89/
>
> Le mardi 14 janvier 2020 11:55:08 UTC+1, Eric Gourgoulhon a écrit :
>>
>> Le mardi 14 janvier 2020 11:36:44 UTC+1, Dima Pasechnik a écrit :
>>>
>>>
>>> mpmath version on Sage 8.6 is 1.0.0, and later and now it is 1.1.0
>>>
>>> This might explain the problem, which maybe should be reported upstream:
>>>
>>> https://github.com/fredrik-johansson/mpmath
>>>
>>
>>
>> It's OK with Sage 8.9, which has already mpmath 1.1.0. So the problem is not due to the version of mpmath. Maybe a Python 2 / Python 3 issue?
>> Another data point: the issue is there in Sage 9.0.beta5 (Python 3).
>>
>> Eric.
>>
>
> --
> You received this message because you are subscribed to the Google Groups "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-s...@googlegroups.com.

Dima Pasechnik

unread,
Jan 14, 2020, 6:57:31 AM1/14/20
to sage-support
On Tue, Jan 14, 2020 at 11:53 AM Александр Ватузов <amva...@gmail.com> wrote:
>
> Will you open issue here? https://trac.sagemath.org/

I can't reproduce the issue with a system python 3.7.3, so it deserves
a trac ticket.
Can you open one?
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/2e9eea57-3558-426b-9dec-76c22a6547ea%40googlegroups.com.

Александр Ватузов

unread,
Jan 14, 2020, 7:08:47 AM1/14/20
to sage-support
yes, I'll do it

вторник, 14 января 2020 г., 14:57:31 UTC+3 пользователь Dima Pasechnik написал:
Message has been deleted

Dima Pasechnik

unread,
Jan 14, 2020, 7:42:17 AM1/14/20
to sage-support
On Tue, Jan 14, 2020 at 12:37 PM Александр Ватузов <amva...@gmail.com> wrote:
>
> I haven-fount where report bug on that site...
>
you open a new ticket: https://trac.sagemath.org/newticket
- you'd need either a Guthub account, or a local account (long time to
get sometimes) to authenticate.


> Now I will try to repair smth:
>
> I have 2 dirs:
> SageMath/local/lib/python3.7/site-packages/mpmath
> .local/lib/python3.6/site-packages/mpmath
>
> I think there are sima chenges here
>
> вторник, 14 января 2020 г., 14:57:31 UTC+3 пользователь Dima Pasechnik написал:
>> > To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/2e9eea57-3558-426b-9dec-76c22a6547ea%40googlegroups.com.
>
> --
> You received this message because you are subscribed to the Google Groups "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/e9324849-4998-4c91-a384-f3314f59ae46%40googlegroups.com.
Message has been deleted

Александр Ватузов

unread,
Jan 14, 2020, 8:36:27 AM1/14/20
to sage-support
Now I constantly know, that problem was in using

divmod(n, base**half)

instead of

n // (base**half), n % (base**half)

on File "/home/nouret/SageMath/local/lib/python3.7/site-packages/mpmath/libmp/libintmath.py", line 161, in numeral_python

вторник, 14 января 2020 г., 15:42:17 UTC+3 пользователь Dima Pasechnik написал:

Александр Ватузов

unread,
Jan 14, 2020, 8:55:51 AM1/14/20
to sage-support
https://trac.sagemath.org/ticket/29009#ticket

вторник, 14 января 2020 г., 16:36:27 UTC+3 пользователь Александр Ватузов написал:

John H Palmieri

unread,
Jan 14, 2020, 1:18:41 PM1/14/20
to sage-support
Just to confirm: everything works with Sage 9.0.beta1, built with Python 2. Fails with Sage built with Python 3.

John H Palmieri

unread,
Jan 14, 2020, 1:20:05 PM1/14/20
to sage-support
Sorry, I meant Sage *9.1.beta0*.

Александр Ватузов

unread,
Jan 14, 2020, 2:54:21 PM1/14/20
to sage-support
and what is on mpmath file, analog of "/SageMath/local/lib/python3.7/site-packages/mpmath/libmp/libintmath.py", line 161 ? Just interesting :)

вторник, 14 января 2020 г., 21:20:05 UTC+3 пользователь John H Palmieri написал:

John H Palmieri

unread,
Jan 14, 2020, 4:26:21 PM1/14/20
to sage-support
On line 158, in the function "numerical_python", I see

    A, B = divmod(n, base**half)






Nils Bruin

unread,
Jan 14, 2020, 5:23:36 PM1/14/20
to sage-support
On Tuesday, January 14, 2020 at 5:36:27 AM UTC-8, Александр Ватузов wrote:
Now I constantly know, that problem was in using

divmod(n, base**half)

instead of

n // (base**half), n % (base**half)

The second option yields a performance regression relative to the first, because you would end up doing an integer division twice rather than one (integer division yields both the quotient and the remainder, so throwing one away and then recomputing it to find it anyway is wasteful). You're also performing an exponentiation twice.

I think it would be better to find/write a "divmod" that works in py3 as well.

Александр Ватузов

unread,
Jan 15, 2020, 2:42:00 AM1/15/20
to sage-support
yes, but it is function that translates numbers into strings, don't need such optimizations

среда, 15 января 2020 г., 1:23:36 UTC+3 пользователь Nils Bruin написал:

Dima Pasechnik

unread,
Jan 15, 2020, 4:22:51 AM1/15/20
to sage-support
it is a Sage-specfic bug, as simply installing mpmath into system
Python3 fails to reproduce the issue.
(or perhaps it's due to Cython, or a specific version of it)
Does Sage use a "custom" divmod in mpmath code?



> --
> You received this message because you are subscribed to the Google Groups "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/78a83c70-0f12-4df0-a2e4-d4a499b388bf%40googlegroups.com.

Александр Ватузов

unread,
Jan 15, 2020, 8:10:08 AM1/15/20
to sage-support
As I undestand, sage uses custom int

среда, 15 января 2020 г., 12:22:51 UTC+3 пользователь Dima Pasechnik написал:
On Tue, Jan 14, 2020 at 10:23 PM Nils Bruin <nbr...@sfu.ca> wrote:
>
> On Tuesday, January 14, 2020 at 5:36:27 AM UTC-8, Александр Ватузов wrote:
>>
>> Now I constantly know, that problem was in using
>>
>> divmod(n, base**half)
>>
>> instead of
>>
>> n // (base**half), n % (base**half)
>
>
> The second option yields a performance regression relative to the first, because you would end up doing an integer division twice rather than one (integer division yields both the quotient and the remainder, so throwing one away and then recomputing it to find it anyway is wasteful). You're also performing an exponentiation twice.
>
> I think it would be better to find/write a "divmod" that works in py3 as well.
>
it is a Sage-specfic bug, as simply installing mpmath into system
Python3 fails to reproduce the issue.
(or perhaps it's due to Cython, or a specific version of it)
Does Sage use a "custom" divmod in mpmath code?



> --
> You received this message because you are subscribed to the Google Groups "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-s...@googlegroups.com.

Dima Pasechnik

unread,
Jan 15, 2020, 8:15:08 AM1/15/20
to sage-support
On Wed, Jan 15, 2020 at 1:10 PM Александр Ватузов <amva...@gmail.com> wrote:
>
> As I undestand, sage uses custom int

no, Sage's int is the same as Python's int. But Sage has other integer
types, e.g. ZZ.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/596f9268-9a22-489a-9e18-fd049e9cee6c%40googlegroups.com.

Александр Ватузов

unread,
Jan 15, 2020, 8:42:48 AM1/15/20
to sage-support
I mean "default". type(5) f.e.

среда, 15 января 2020 г., 16:15:08 UTC+3 пользователь Dima Pasechnik написал:
Reply all
Reply to author
Forward
0 new messages