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

using numpy 2.0 #38242

Closed
1 task done
dimpase opened this issue Jun 19, 2024 · 2 comments · Fixed by #38250 · May be fixed by #38227
Closed
1 task done

using numpy 2.0 #38242

dimpase opened this issue Jun 19, 2024 · 2 comments · Fixed by #38250 · May be fixed by #38227

Comments

@dimpase
Copy link
Member

dimpase commented Jun 19, 2024

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 flintlib/flint#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?

  • I have searched the existing issues for a bug report that matches the one I want to file, without success.
@dimpase
Copy link
Member Author

dimpase commented Jun 19, 2024

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
Copy link
Member Author

dimpase commented Jun 20, 2024

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

vbraun pushed a commit to vbraun/sage that referenced this issue Jul 20, 2024
    
This will fix sagemath#38242

Basically, most of the problems are with numpy switching to a different
way to display its data,
showing type. We either use a compatibility version, or add ellipses in
doctests.

### 📝 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.
- [ ] 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#38250
Reported by: Dima Pasechnik
Reviewer(s):
@mkoeppe mkoeppe added this to the sage-10.5 milestone Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment