Skip to content
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

Make tests compatible with numpy 2.1 #39242

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ the standard deviation::

sage: import numpy as np
sage: if int(np.version.short_version[0]) > 1:
....: np.set_printoptions(legacy="1.25")
....: _ = np.set_printoptions(legacy="1.25")
sage: np.mean([1, 2, 3, 5])
2.75

Expand Down
2 changes: 1 addition & 1 deletion src/doc/en/thematic_tutorials/numerical_sage/numpy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import it.

sage: import numpy
sage: if int(numpy.version.short_version[0]) > 1:
....: numpy.set_printoptions(legacy="1.25") # to ensure numpy 2.0 compatibility
....: _ = numpy.set_printoptions(legacy="1.25") # to ensure numpy 2.0 compatibility

The basic object of computation in NumPy is an array. It is simple to
create an array.
Expand Down
2 changes: 1 addition & 1 deletion src/sage/combinat/fully_packed_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _make_color_list(n, colors=None, color_map=None, randomize=False):

sage: import numpy as np
sage: if int(np.version.short_version[0]) > 1:
....: np.set_printoptions(legacy="1.25")
....: _ = np.set_printoptions(legacy="1.25")
sage: from sage.combinat.fully_packed_loop import _make_color_list
sage: _make_color_list(5)
sage: _make_color_list(5, ['blue', 'red'])
Expand Down
2 changes: 1 addition & 1 deletion src/sage/functions/special.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class SphericalHarmonic(BuiltinFunction):
sage: from scipy.special import sph_harm # NB: arguments x and y are swapped # needs scipy
sage: import numpy as np # needs scipy
sage: if int(np.version.short_version[0]) > 1: # needs scipy
....: np.set_printoptions(legacy="1.25") # needs scipy
....: _ = np.set_printoptions(legacy="1.25") # needs scipy
sage: sph_harm(1, 1, pi.n(), (pi/2).n()) # abs tol 1e-14 # needs scipy sage.symbolic
(0.3454941494713355-4.231083042742082e-17j)

Expand Down
12 changes: 6 additions & 6 deletions src/sage/numerical/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
import scipy.optimize
import numpy
if int(numpy.version.short_version[0]) > 1:
numpy.set_printoptions(legacy="1.25")
_ = numpy.set_printoptions(legacy="1.25")

Check warning on line 158 in src/sage/numerical/optimize.py

View check run for this annotation

Codecov / codecov/patch

src/sage/numerical/optimize.py#L158

Added line #L158 was not covered by tests

g = lambda x: float(f(x))
brentqRes = scipy.optimize.brentq(g, a, b,
Expand Down Expand Up @@ -290,7 +290,7 @@
import scipy.optimize
import numpy
if int(numpy.version.short_version[0]) > 1:
numpy.set_printoptions(legacy="1.25")
_ = numpy.set_printoptions(legacy="1.25")

Check warning on line 293 in src/sage/numerical/optimize.py

View check run for this annotation

Codecov / codecov/patch

src/sage/numerical/optimize.py#L293

Added line #L293 was not covered by tests

xmin, fval, iter, funcalls = scipy.optimize.fminbound(f, a, b, full_output=1, xtol=tol, maxfun=maxfun)
return fval, xmin
Expand Down Expand Up @@ -381,7 +381,7 @@
....: return sum(100.0r*(x[1r:]-x[:-1r]**2.0r)**2.0r + (1r-x[:-1r])**2.0r)
sage: import numpy
sage: if int(numpy.version.short_version[0]) > 1:
....: numpy.set_printoptions(legacy="1.25")
....: _ = numpy.set_printoptions(legacy="1.25")
sage: from numpy import zeros
sage: def rosen_der(x):
....: xm = x[1r:-1r]
Expand All @@ -400,7 +400,7 @@
from sage.ext.fast_callable import fast_callable
import numpy
if int(numpy.version.short_version[0]) > 1:
numpy.set_printoptions(legacy="1.25")
_ = numpy.set_printoptions(legacy="1.25")

Check warning on line 403 in src/sage/numerical/optimize.py

View check run for this annotation

Codecov / codecov/patch

src/sage/numerical/optimize.py#L403

Added line #L403 was not covered by tests

from scipy import optimize
if isinstance(func, Expression):
Expand Down Expand Up @@ -539,7 +539,7 @@
from sage.ext.fast_callable import fast_callable
import numpy
if int(numpy.version.short_version[0]) > 1:
numpy.set_printoptions(legacy="1.25")
_ = numpy.set_printoptions(legacy="1.25")

Check warning on line 542 in src/sage/numerical/optimize.py

View check run for this annotation

Codecov / codecov/patch

src/sage/numerical/optimize.py#L542

Added line #L542 was not covered by tests
from scipy import optimize
function_type = type(lambda x,y: x+y)

Expand Down Expand Up @@ -662,7 +662,7 @@
"""
import numpy
if int(numpy.version.short_version[0]) > 1:
numpy.set_printoptions(legacy="1.25")
_ = numpy.set_printoptions(legacy="1.25")

Check warning on line 665 in src/sage/numerical/optimize.py

View check run for this annotation

Codecov / codecov/patch

src/sage/numerical/optimize.py#L665

Added line #L665 was not covered by tests

if not isinstance(data, numpy.ndarray):
try:
Expand Down
2 changes: 1 addition & 1 deletion src/sage/plot/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def get_minmax_data(self):

sage: import numpy # to ensure numpy 2.0 compatibility
sage: if int(numpy.version.short_version[0]) > 1:
....: numpy.set_printoptions(legacy="1.25")
....: _ = numpy.set_printoptions(legacy="1.25")
sage: from sage.plot.arrow import CurveArrow
sage: b = CurveArrow(path=[[(0,0),(.5,.5),(1,0)],[(.5,1),(0,0)]],
....: options={})
Expand Down
4 changes: 2 additions & 2 deletions src/sage/plot/multigraphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ def _add_subplot(self, figure, index, **options):
(0.2, 0.3, 0.4, 0.1)
sage: import numpy # to ensure numpy 2.0 compatibility
sage: if int(numpy.version.short_version[0]) > 1:
....: numpy.set_printoptions(legacy="1.25")
....: _ = numpy.set_printoptions(legacy="1.25")
sage: ax1.get_position().bounds # tol 1.0e-13
(0.2, 0.3, 0.4000000000000001, 0.10000000000000003)
"""
Expand Down Expand Up @@ -1269,7 +1269,7 @@ def position(self, index):
sage: G = graphics_array([g1, g2])
sage: import numpy # to ensure numpy 2.0 compatibility
sage: if int(numpy.version.short_version[0]) > 1:
....: numpy.set_printoptions(legacy="1.25")
....: _ = numpy.set_printoptions(legacy="1.25")
sage: G.position(0) # tol 5.0e-3
(0.025045451349937315,
0.03415488992713045,
Expand Down
2 changes: 1 addition & 1 deletion src/sage/plot/streamline_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def get_minmax_data(self):
sage: x, y = var('x y')
sage: import numpy # to ensure numpy 2.0 compatibility
sage: if int(numpy.version.short_version[0]) > 1:
....: numpy.set_printoptions(legacy="1.25")
....: _ = numpy.set_printoptions(legacy="1.25")
sage: d = streamline_plot((.01*x, x+y), (x,10,20), (y,10,20))[0].get_minmax_data()
sage: d['xmin']
10.0
Expand Down
2 changes: 1 addition & 1 deletion src/sage/rings/integer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@
sage: # needs numpy
sage: import numpy
sage: if int(numpy.version.short_version[0]) > 1:
....: numpy.set_printoptions(legacy="1.25")
....: _ = numpy.set_printoptions(legacy="1.25")
sage: numpy.int8('12') == 12
True
sage: 12 == numpy.int8('12')
Expand Down Expand Up @@ -3071,7 +3071,7 @@
so a memory leak will not go unnoticed)::

sage: n = prod(primes_first_n(25)) # needs sage.libs.pari
sage: for i in range(20): # long time # needs sage.libs.pari

Check warning on line 3074 in src/sage/rings/integer.pyx

View workflow job for this annotation

GitHub Actions / test-new

Warning: slow doctest:

slow doctest:
....: try:
....: alarm(RDF.random_element(1e-3, 0.5))
....: _ = n.divisors()
Expand Down
2 changes: 1 addition & 1 deletion src/sage/rings/real_mpfi.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ TESTS::

sage: import numpy # needs numpy
sage: if int(numpy.version.short_version[0]) > 1: # needs numpy
....: numpy.set_printoptions(legacy="1.25") # needs numpy
....: _ = numpy.set_printoptions(legacy="1.25") # needs numpy
sage: RIF(2) == numpy.int8('2') # needs numpy
True
sage: numpy.int8('2') == RIF(2) # needs numpy
Expand Down
10 changes: 5 additions & 5 deletions src/sage/schemes/elliptic_curves/period_lattice_region.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ cdef class PeriodicRegion:

sage: import numpy as np
sage: if int(np.version.short_version[0]) > 1:
....: np.set_printoptions(legacy="1.25")
....: _ = np.set_printoptions(legacy="1.25")
sage: from sage.schemes.elliptic_curves.period_lattice_region import PeriodicRegion
sage: data = np.zeros((4, 4))
sage: PeriodicRegion(CDF(2), CDF(2*I), data).is_empty()
Expand Down Expand Up @@ -296,7 +296,7 @@ cdef class PeriodicRegion:

sage: import numpy as np
sage: if int(np.version.short_version[0]) > 1:
....: np.set_printoptions(legacy="1.25")
....: _ = np.set_printoptions(legacy="1.25")
sage: from sage.schemes.elliptic_curves.period_lattice_region import PeriodicRegion
sage: data = np.zeros((10, 10))
sage: data[1:4,1:4] = True
Expand All @@ -320,7 +320,7 @@ cdef class PeriodicRegion:

sage: import numpy as np
sage: if int(np.version.short_version[0]) > 1:
....: np.set_printoptions(legacy="1.25")
....: _ = np.set_printoptions(legacy="1.25")
sage: from sage.schemes.elliptic_curves.period_lattice_region import PeriodicRegion
sage: data = np.zeros((4, 4))
sage: data[1,1] = True
Expand Down Expand Up @@ -375,7 +375,7 @@ cdef class PeriodicRegion:

sage: import numpy as np
sage: if int(np.version.short_version[0]) > 1:
....: np.set_printoptions(legacy="1.25")
....: _ = np.set_printoptions(legacy="1.25")
sage: from sage.schemes.elliptic_curves.period_lattice_region import PeriodicRegion

sage: data = np.zeros((20, 20))
Expand Down Expand Up @@ -528,7 +528,7 @@ cdef class PeriodicRegion:

sage: import numpy as np
sage: if int(np.version.short_version[0]) > 1:
....: np.set_printoptions(legacy="1.25")
....: _ = np.set_printoptions(legacy="1.25")
sage: from sage.schemes.elliptic_curves.period_lattice_region import PeriodicRegion
sage: data = np.zeros((4, 4))
sage: data[1, 1] = True
Expand Down
4 changes: 2 additions & 2 deletions src/sage/stats/basic_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def std(v, bias=False):
sage: # needs numpy
sage: import numpy
sage: if int(numpy.version.short_version[0]) > 1:
....: numpy.set_printoptions(legacy="1.25")
....: _ = numpy.set_printoptions(legacy="1.25")
sage: x = numpy.array([1,2,3,4,5])
sage: std(x, bias=False)
1.5811388300841898
Expand Down Expand Up @@ -299,7 +299,7 @@ def variance(v, bias=False):
0.4897530450000000?
sage: import numpy # needs numpy
sage: if int(numpy.version.short_version[0]) > 1: # needs numpy
....: numpy.set_printoptions(legacy="1.25") # needs numpy
....: _ = numpy.set_printoptions(legacy="1.25") # needs numpy
sage: x = numpy.array([1,2,3,4,5]) # needs numpy
sage: variance(x, bias=False) # needs numpy
2.5
Expand Down
2 changes: 1 addition & 1 deletion src/sage/structure/coerce.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ cdef class CoercionModel:

sage: import numpy # needs numpy
sage: if int(numpy.version.short_version[0]) > 1: # needs numpy
....: numpy.set_printoptions(legacy="1.25") # needs numpy
....: __ = numpy.set_printoptions(legacy="1.25") # needs numpy

sage: # needs sage.rings.real_mpfr
sage: x = polygen(RR)
Expand Down
2 changes: 1 addition & 1 deletion src/sage/symbolic/function.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ cdef class BuiltinFunction(Function):

sage: import numpy # needs numpy
sage: if int(numpy.version.short_version[0]) > 1: # needs numpy
....: numpy.set_printoptions(legacy="1.25") # needs numpy
....: __ = numpy.set_printoptions(legacy="1.25") # needs numpy

sage: sin(numpy.int32(0)) # needs numpy
0.0
Expand Down
2 changes: 1 addition & 1 deletion src/sage/symbolic/ring.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ cdef class NumpyToSRMorphism(Morphism):

sage: import numpy # needs numpy
sage: if int(numpy.version.short_version[0]) > 1: # needs numpy
....: numpy.set_printoptions(legacy="1.25") # needs numpy
....: _ = numpy.set_printoptions(legacy="1.25") # needs numpy
sage: f(x) = x^2
sage: f(numpy.int8('2')) # needs numpy
4
Expand Down
Loading