From 97e61051d2a444798e5a58e681b8acb95916b931 Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Wed, 1 Jan 2025 23:23:46 +0700 Subject: [PATCH 1/3] Make tests compatible with numpy 2.1 --- .../Quickstarts/Statistics-and-Distributions.rst | 2 +- .../en/thematic_tutorials/numerical_sage/numpy.rst | 2 +- src/sage/combinat/fully_packed_loop.py | 2 +- src/sage/functions/special.py | 2 +- src/sage/numerical/optimize.py | 12 ++++++------ src/sage/plot/arrow.py | 2 +- src/sage/plot/multigraphics.py | 4 ++-- src/sage/plot/streamline_plot.py | 2 +- src/sage/rings/integer.pyx | 2 +- src/sage/rings/real_mpfi.pyx | 2 +- .../elliptic_curves/period_lattice_region.pyx | 10 +++++----- src/sage/stats/basic_stats.py | 4 ++-- src/sage/structure/coerce.pyx | 2 +- src/sage/symbolic/function.pyx | 2 +- src/sage/symbolic/ring.pyx | 2 +- 15 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/doc/en/prep/Quickstarts/Statistics-and-Distributions.rst b/src/doc/en/prep/Quickstarts/Statistics-and-Distributions.rst index 958a378f945..5870df3d88a 100644 --- a/src/doc/en/prep/Quickstarts/Statistics-and-Distributions.rst +++ b/src/doc/en/prep/Quickstarts/Statistics-and-Distributions.rst @@ -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 diff --git a/src/doc/en/thematic_tutorials/numerical_sage/numpy.rst b/src/doc/en/thematic_tutorials/numerical_sage/numpy.rst index 925e5312882..ab774ca8fd1 100644 --- a/src/doc/en/thematic_tutorials/numerical_sage/numpy.rst +++ b/src/doc/en/thematic_tutorials/numerical_sage/numpy.rst @@ -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. diff --git a/src/sage/combinat/fully_packed_loop.py b/src/sage/combinat/fully_packed_loop.py index 81e3a30508f..e6c89ec3c3c 100644 --- a/src/sage/combinat/fully_packed_loop.py +++ b/src/sage/combinat/fully_packed_loop.py @@ -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']) diff --git a/src/sage/functions/special.py b/src/sage/functions/special.py index 308171af3cd..4363d332291 100644 --- a/src/sage/functions/special.py +++ b/src/sage/functions/special.py @@ -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) diff --git a/src/sage/numerical/optimize.py b/src/sage/numerical/optimize.py index cdcd3a524b3..742de62d5de 100644 --- a/src/sage/numerical/optimize.py +++ b/src/sage/numerical/optimize.py @@ -155,7 +155,7 @@ def find_root(f, a, b, xtol=10e-13, rtol=2.0**-50, maxiter=100, full_output=Fals 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") g = lambda x: float(f(x)) brentqRes = scipy.optimize.brentq(g, a, b, @@ -290,7 +290,7 @@ def find_local_minimum(f, a, b, tol=1.48e-08, maxfun=500): 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") xmin, fval, iter, funcalls = scipy.optimize.fminbound(f, a, b, full_output=1, xtol=tol, maxfun=maxfun) return fval, xmin @@ -381,7 +381,7 @@ def minimize(func, x0, gradient=None, hessian=None, algorithm='default', ....: 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] @@ -400,7 +400,7 @@ def minimize(func, x0, gradient=None, hessian=None, algorithm='default', 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") from scipy import optimize if isinstance(func, Expression): @@ -539,7 +539,7 @@ def minimize_constrained(func, cons, x0, gradient=None, algorithm='default', **a 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") from scipy import optimize function_type = type(lambda x,y: x+y) @@ -662,7 +662,7 @@ def find_fit(data, model, initial_guess=None, parameters=None, variables=None, s """ import numpy if int(numpy.version.short_version[0]) > 1: - numpy.set_printoptions(legacy="1.25") + _ = numpy.set_printoptions(legacy="1.25") if not isinstance(data, numpy.ndarray): try: diff --git a/src/sage/plot/arrow.py b/src/sage/plot/arrow.py index 02442f90ba4..c8a252cfcca 100644 --- a/src/sage/plot/arrow.py +++ b/src/sage/plot/arrow.py @@ -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={}) diff --git a/src/sage/plot/multigraphics.py b/src/sage/plot/multigraphics.py index 6ed26974c49..38ce09e9846 100644 --- a/src/sage/plot/multigraphics.py +++ b/src/sage/plot/multigraphics.py @@ -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) """ @@ -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, diff --git a/src/sage/plot/streamline_plot.py b/src/sage/plot/streamline_plot.py index 2801446433a..f995cd71f93 100644 --- a/src/sage/plot/streamline_plot.py +++ b/src/sage/plot/streamline_plot.py @@ -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 diff --git a/src/sage/rings/integer.pyx b/src/sage/rings/integer.pyx index c9d1ff65bc6..466893c1f03 100644 --- a/src/sage/rings/integer.pyx +++ b/src/sage/rings/integer.pyx @@ -595,7 +595,7 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement): 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') diff --git a/src/sage/rings/real_mpfi.pyx b/src/sage/rings/real_mpfi.pyx index ce9958ce7e7..bbe72c26ba2 100644 --- a/src/sage/rings/real_mpfi.pyx +++ b/src/sage/rings/real_mpfi.pyx @@ -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 diff --git a/src/sage/schemes/elliptic_curves/period_lattice_region.pyx b/src/sage/schemes/elliptic_curves/period_lattice_region.pyx index 40b92ab23eb..e4676810481 100644 --- a/src/sage/schemes/elliptic_curves/period_lattice_region.pyx +++ b/src/sage/schemes/elliptic_curves/period_lattice_region.pyx @@ -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() @@ -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 @@ -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 @@ -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)) @@ -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 diff --git a/src/sage/stats/basic_stats.py b/src/sage/stats/basic_stats.py index 47d890cf0ab..887e4dfd89f 100644 --- a/src/sage/stats/basic_stats.py +++ b/src/sage/stats/basic_stats.py @@ -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 @@ -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 diff --git a/src/sage/structure/coerce.pyx b/src/sage/structure/coerce.pyx index 6861cfb5be3..f481536f8aa 100644 --- a/src/sage/structure/coerce.pyx +++ b/src/sage/structure/coerce.pyx @@ -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) diff --git a/src/sage/symbolic/function.pyx b/src/sage/symbolic/function.pyx index 86860237d5b..6b087c78609 100644 --- a/src/sage/symbolic/function.pyx +++ b/src/sage/symbolic/function.pyx @@ -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 diff --git a/src/sage/symbolic/ring.pyx b/src/sage/symbolic/ring.pyx index eaaa5cbdc15..ceaede5aa12 100644 --- a/src/sage/symbolic/ring.pyx +++ b/src/sage/symbolic/ring.pyx @@ -1154,7 +1154,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 From 36ae1d995ef3ff66bd28b1b678b362cf56edac2a Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Sat, 4 Jan 2025 20:38:34 +0700 Subject: [PATCH 2/3] Use __ to avoid overwrite shell magic variables --- src/sage/structure/coerce.pyx | 2 +- src/sage/symbolic/function.pyx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sage/structure/coerce.pyx b/src/sage/structure/coerce.pyx index f481536f8aa..35c76cebc67 100644 --- a/src/sage/structure/coerce.pyx +++ b/src/sage/structure/coerce.pyx @@ -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) diff --git a/src/sage/symbolic/function.pyx b/src/sage/symbolic/function.pyx index 6b087c78609..132bed222a9 100644 --- a/src/sage/symbolic/function.pyx +++ b/src/sage/symbolic/function.pyx @@ -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 From ba71bbd9e966212b4ecb11d1a05f2b0f79f0be65 Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Thu, 6 Feb 2025 20:47:05 +0700 Subject: [PATCH 3/3] Revert unintended changes --- src/sage/numerical/optimize.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/sage/numerical/optimize.py b/src/sage/numerical/optimize.py index 742de62d5de..38ff1316629 100644 --- a/src/sage/numerical/optimize.py +++ b/src/sage/numerical/optimize.py @@ -155,7 +155,7 @@ def find_root(f, a, b, xtol=10e-13, rtol=2.0**-50, maxiter=100, full_output=Fals 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") g = lambda x: float(f(x)) brentqRes = scipy.optimize.brentq(g, a, b, @@ -290,7 +290,7 @@ def find_local_minimum(f, a, b, tol=1.48e-08, maxfun=500): 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") xmin, fval, iter, funcalls = scipy.optimize.fminbound(f, a, b, full_output=1, xtol=tol, maxfun=maxfun) return fval, xmin @@ -400,7 +400,7 @@ def minimize(func, x0, gradient=None, hessian=None, algorithm='default', 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") from scipy import optimize if isinstance(func, Expression): @@ -539,7 +539,7 @@ def minimize_constrained(func, cons, x0, gradient=None, algorithm='default', **a 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") from scipy import optimize function_type = type(lambda x,y: x+y) @@ -662,7 +662,7 @@ def find_fit(data, model, initial_guess=None, parameters=None, variables=None, s """ import numpy if int(numpy.version.short_version[0]) > 1: - _ = numpy.set_printoptions(legacy="1.25") + numpy.set_printoptions(legacy="1.25") if not isinstance(data, numpy.ndarray): try: