Skip to content

Commit

Permalink
sagemathgh-39242: Make tests compatible with numpy 2.1
Browse files Browse the repository at this point in the history
    
Related to sagemath#38250 .

In numpy 2.1 it returns a `Token` object which gets printed out. This
assigns it to `_`.


### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#39242
Reported by: user202729
Reviewer(s): Tobias Diez, user202729
  • Loading branch information
Release Manager committed Feb 8, 2025
2 parents d8a4722 + ba71bbd commit 2d7f12e
Show file tree
Hide file tree
Showing 15 changed files with 21 additions and 21 deletions.
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 @@ -218,7 +218,7 @@ class SphericalHarmonic(BuiltinFunction):
0.345494149471335
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: import scipy.version
sage: if scipy.version.version < '1.15.0':
....: from scipy.special import sph_harm # NB: arguments x and y are swapped # needs scipy
Expand Down
2 changes: 1 addition & 1 deletion src/sage/numerical/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
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 @@ 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')
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

0 comments on commit 2d7f12e

Please sign in to comment.