Skip to content

Commit 367b516

Browse files
author
Release Manager
committed
sagemathgh-38776: Fix test failures due to global mpmath state mpmath has a global `mp.pretty` variable that affects how its numbers are printed. On my machine, it's not reliable. For example: ``` Failed example: repr(mp.mpc(2,3)) Expected: '(2.0 + 3.0j)' Got: "mpc(real='2.0', imag='3.0')" ``` I've only fixed the tests that are failing for me, since mpmath-1.4 is on the TODO list. A few different strategies are used: * `print()` will use the pretty format if all we're doing is displaying an answer. * `with workdps` can be used to change the precision only locally. * In TESTS blocks, the ugly output is acceptable. * When actually testing `mp.pretty`, we can first clone the `mp` object with `mp2 = mp.clone()`, and then work on the new one. URL: sagemath#38776 Reported by: Michael Orlitzky Reviewer(s): Dima Pasechnik
2 parents fe5d6c3 + 441ee05 commit 367b516

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

src/sage/functions/orthogonal_polys.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,10 +2343,10 @@ class Func_ultraspherical(GinacFunction):
23432343
sage: # needs mpmath
23442344
sage: from mpmath import gegenbauer as gegenbauer_mp
23452345
sage: from mpmath import mp
2346-
sage: mp.pretty = True; mp.dps=25
2347-
sage: gegenbauer_mp(-7,0.5,0.3)
2346+
sage: print(gegenbauer_mp(-7,0.5,0.3))
23482347
0.1291811875
2349-
sage: gegenbauer_mp(2+3j, -0.75, -1000j)
2348+
sage: with mp.workdps(25):
2349+
....: print(gegenbauer_mp(2+3j, -0.75, -1000j))
23502350
(-5038991.358609026523401901 + 9414549.285447104177860806j)
23512351
23522352
TESTS:

src/sage/libs/mpmath/ext_main.pyx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -983,19 +983,18 @@ cdef class Context:
983983
TESTS::
984984
985985
sage: from mpmath import mp
986-
sage: mp.pretty = True
987986
sage: (x, T) = mp._convert_param(3)
988987
sage: (x, type(x).__name__, T)
989988
(3, 'int', 'Z')
990989
sage: (x, T) = mp._convert_param(2.5)
991990
sage: (x, type(x).__name__, T)
992991
(mpq(5,2), 'mpq', 'Q')
993992
sage: (x, T) = mp._convert_param(2.3)
994-
sage: (x, type(x).__name__, T)
995-
(2.3, 'mpf', 'R')
993+
sage: (str(x), type(x).__name__, T)
994+
('2.3', 'mpf', 'R')
996995
sage: (x, T) = mp._convert_param(2+3j)
997996
sage: (x, type(x).__name__, T)
998-
((2.0 + 3.0j), 'mpc', 'C')
997+
(mpc(real='2.0', imag='3.0'), 'mpc', 'C')
999998
sage: mp.pretty = False
1000999
"""
10011000
cdef MPF v
@@ -1056,15 +1055,14 @@ cdef class Context:
10561055
TESTS::
10571056
10581057
sage: from mpmath import *
1059-
sage: mp.pretty = True
10601058
sage: mag(10), mag(10.0), mag(mpf(10)), int(ceil(log(10,2)))
10611059
(4, 4, 4, 4)
10621060
sage: mag(10j), mag(10+10j)
10631061
(4, 5)
10641062
sage: mag(0.01), int(ceil(log(0.01,2)))
10651063
(-6, -6)
10661064
sage: mag(0), mag(inf), mag(-inf), mag(nan)
1067-
(-inf, +inf, +inf, nan)
1065+
(mpf('-inf'), mpf('+inf'), mpf('+inf'), mpf('nan'))
10681066
10691067
::
10701068
@@ -2203,12 +2201,13 @@ cdef class constant(mpf_base):
22032201
Represent ``self`` as a string. With mp.pretty=False, the
22042202
representation differs from that of an ordinary mpf::
22052203
2206-
sage: from mpmath import mp, pi
2207-
sage: mp.pretty = True
2208-
sage: repr(pi)
2204+
sage: from mpmath import mp
2205+
sage: mp2 = mp.clone()
2206+
sage: mp2.pretty = True
2207+
sage: repr(mp2.pi)
22092208
'3.14159265358979'
2210-
sage: mp.pretty = False
2211-
sage: repr(pi)
2209+
sage: mp2.pretty = False
2210+
sage: repr(mp2.pi)
22122211
'<pi: 3.14159~>'
22132212
"""
22142213
if global_context.pretty:
@@ -2374,11 +2373,12 @@ cdef class mpc(mpnumber):
23742373
TESTS::
23752374
23762375
sage: from mpmath import mp
2377-
sage: mp.pretty = True
2378-
sage: repr(mp.mpc(2,3))
2376+
sage: mp2 = mp.clone()
2377+
sage: mp2.pretty = True
2378+
sage: repr(mp2.mpc(2,3))
23792379
'(2.0 + 3.0j)'
2380-
sage: mp.pretty = False
2381-
sage: repr(mp.mpc(2,3))
2380+
sage: mp2.pretty = False
2381+
sage: repr(mp2.mpc(2,3))
23822382
"mpc(real='2.0', imag='3.0')"
23832383
"""
23842384
if global_context.pretty:

0 commit comments

Comments
 (0)