sagemath / sage

Main repository of SageMath. Now open for Issues and Pull Requests.
https://www.sagemath.org
Other
1.19k stars 411 forks source link

using numpy 2.0 #38242

Open dimpase opened 2 weeks ago

dimpase commented 2 weeks ago

Problem Description

it's released, and used by scipy

Proposed Solution

various things to be done. The 1st is due to flint headers, which use I in function declarations, and if you include complex.h, you get problems. Fix is easy, and proposed in https://github.com/flintlib/flint/pull/2027

There are also deprecations to be dealt with, e.g.

File "src/sage/rings/polynomial/polynomial_element.pyx", line 4639, in sage.rings.polynomial.polynomial_element.Polynomial.factor
Failed example:
    (-2*x^2 - 1).factor()
Expected:
    (-2.0) * (x^2 + 0.5000000000000001)
Got:
    doctest:warning
      File "<doctest sage.rings.polynomial.polynomial_element.Polynomial.factor[44]>", line 1, in <module>
        (-Integer(2)*x**Integer(2) - Integer(1)).factor()
      File "/usr/lib/python3.12/site-packages/numpy/linalg/linalg.py", line 8, in __getattr__
        warnings.warn(
      File "/usr/lib/python3.12/warnings.py", line 112, in _showwarnmsg
        sw(msg.message, msg.category, msg.filename, msg.lineno,
    :
    DeprecationWarning: The numpy.linalg.linalg has been made private and renamed to numpy.linalg._linalg. All public functions exported by it are available from numpy.linalg. Please use numpy.linalg.LinAlgError instead.
    (-2.0) * (x^2 + 0.5000000000000001)

Alternatives Considered

n/a

Additional Information

No response

Is there an existing issue for this?

dimpase commented 2 weeks ago

here is one more numpy 2.0 issue to be fixed, in src/sage/plot/complex_plot.pyx: delta = rgb.ptp(-1) as there no ptp method any more, there is a function instead to call

[sagemath_doc_html-none] [spkg-install] [plotting ] Traceback (most recent call last):
[sagemath_doc_html-none] [spkg-install] [plotting ]   File "/usr/lib/python3.12/site-packages/matplotlib/sphinxext/plot_directive.py", line 552, in _run_code
[sagemath_doc_html-none] [spkg-install] [plotting ]     exec(code, ns)
[sagemath_doc_html-none] [spkg-install] [plotting ]   File "<string>", line 2, in <module>
[sagemath_doc_html-none] [spkg-install] [plotting ]   File "sage/misc/lazy_import.pyx", line 410, in sage.misc.lazy_import.LazyImport.__call__
[sagemath_doc_html-none] [spkg-install] [plotting ]     return self.get_object()(*args, **kwds)
[sagemath_doc_html-none] [spkg-install] [plotting ]   File "/mnt/opt/Sage/sage-clang/src/sage/misc/decorators.py", line 497, in wrapper
[sagemath_doc_html-none] [spkg-install] [plotting ]     return func(*args, **options)
[sagemath_doc_html-none] [spkg-install] [plotting ]            ^^^^^^^^^^^^^^^^^^^^^^
[sagemath_doc_html-none] [spkg-install] [plotting ]   File "sage/plot/complex_plot.pyx", line 1221, in sage.plot.complex_plot.complex_plot
[sagemath_doc_html-none] [spkg-install] [plotting ]     rgbs = complex_to_cmap_rgb(
[sagemath_doc_html-none] [spkg-install] [plotting ]   File "sage/plot/complex_plot.pyx", line 623, in sage.plot.complex_plot.complex_to_cmap_rgb
[sagemath_doc_html-none] [spkg-install] [plotting ]     rgbs = add_contours_to_rgb(normalized_colors, lightdeltas, dark_rate=dark_rate)
[sagemath_doc_html-none] [spkg-install] [plotting ]   File "sage/plot/complex_plot.pyx", line 760, in sage.plot.complex_plot.add_contours_to_rgb
[sagemath_doc_html-none] [spkg-install] [plotting ]     hls = rgb_to_hls(rgb)
[sagemath_doc_html-none] [spkg-install] [plotting ]   File "sage/plot/complex_plot.pyx", line 1293, in sage.plot.complex_plot.rgb_to_hls
[sagemath_doc_html-none] [spkg-install] [plotting ]     delta = rgb.ptp(-1)
[sagemath_doc_html-none] [spkg-install] [plotting ] AttributeError: `ptp` was removed from the ndarray class in NumPy 2.0. Use np.ptp(arr, ...) instead.
dimpase commented 2 weeks ago

the following allows docs to build

--- a/src/sage/plot/complex_plot.pyx
+++ b/src/sage/plot/complex_plot.pyx
@@ -1290,7 +1290,7 @@ def rgb_to_hls(rgb):
     l = (rgb_max + rgb_min)/2.0  # lightness

     hls = np.zeros_like(rgb)
-    delta = rgb.ptp(-1)
+    delta = np.ptp(rgb, -1)
     s = np.zeros_like(delta)

     ipos = delta > 0

there are many doctest errors related to the new default display format for numpy numbers; now by default it prints its type, i.e. np.float64(42.0) instead of 42.0. One can remedy this, file-wise (as we don't import numpy globally), as follows - just for few of the many here:

--- a/src/doc/en/thematic_tutorials/numerical_sage/numpy.rst
+++ b/src/doc/en/thematic_tutorials/numerical_sage/numpy.rst
@@ -7,6 +7,8 @@ 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

 The basic object of computation in NumPy is an array. It is simple to
 create an array.
--- a/src/sage/calculus/interpolators.pyx
+++ b/src/sage/calculus/interpolators.pyx
@@ -27,6 +27,9 @@ Development supported by NSF award No. 0702939.
 import numpy as np
 cimport numpy as np

+if int(np.version.short_version[0]) > 1:
+    np.set_printoptions(legacy="1.25")
+
 from math import pi
 cdef double TWOPI = 2*pi

diff --git a/src/sage/calculus/riemann.pyx b/src/sage/calculus/riemann.pyx
index 6ec80d89aa7..c09d93c4260 100644
--- a/src/sage/calculus/riemann.pyx
+++ b/src/sage/calculus/riemann.pyx
@@ -44,6 +44,9 @@ from sage.calculus.integration import numerical_integral
 import numpy as np
 cimport numpy as np

+if int(np.version.short_version[0]) > 1:
+    np.set_printoptions(legacy="1.25")
+
 from math import pi
 from math import sin
 from math import cos