Skip to content

Commit e9ded51

Browse files
author
Release Manager
committed
sagemathgh-38683: Fix matrix coercion with numpy 2.1
numpy 2.1 requires a `copy` argument for `numpy.array`. Fixes test failures with numpy 2.1: ``` ********************************************************************** File "/usr/lib/python3.12/site-packages/sage/matrix/matrix1.pyx", line 723, in sage.matrix.matrix1.Matrix.numpy Failed example: b = numpy.array(a); b Exception raised: Traceback (most recent call last): File "/usr/lib/python3.12/site-packages/sage/doctest/forker.py", line 716, in _run self.compile_and_execute(example, compiler, test.globs) File "/usr/lib/python3.12/site-packages/sage/doctest/forker.py", line 1146, in compile_and_execute exec(compiled, globs) File "<doctest sage.matrix.matrix1.Matrix.numpy[9]>", line 1, in <module> b = numpy.array(a); b ^^^^^^^^^^^^^^ File "sage/matrix/matrix1.pyx", line 673, in sage.matrix.matrix1.Matrix.numpy (build/cythonized/sage/matrix/matrix1.c:14076) def numpy(self, dtype=None): TypeError: numpy() got an unexpected keyword argument 'copy' ********************************************************************** File "/usr/lib/python3.12/site-packages/sage/matrix/matrix1.pyx", line 727, in sage.matrix.matrix1.Matrix.numpy Failed example: b.dtype Exception raised: Traceback (most recent call last): File "/usr/lib/python3.12/site-packages/sage/doctest/forker.py", line 716, in _run self.compile_and_execute(example, compiler, test.globs) File "/usr/lib/python3.12/site-packages/sage/doctest/forker.py", line 1146, in compile_and_execute exec(compiled, globs) File "<doctest sage.matrix.matrix1.Matrix.numpy[10]>", line 1, in <module> b.dtype ^ NameError: name 'b' is not defined ********************************************************************** File "/usr/lib/python3.12/site-packages/sage/matrix/matrix1.pyx", line 730, in sage.matrix.matrix1.Matrix.numpy Failed example: b.shape Exception raised: Traceback (most recent call last): File "/usr/lib/python3.12/site-packages/sage/doctest/forker.py", line 716, in _run self.compile_and_execute(example, compiler, test.globs) File "/usr/lib/python3.12/site-packages/sage/doctest/forker.py", line 1146, in compile_and_execute exec(compiled, globs) File "<doctest sage.matrix.matrix1.Matrix.numpy[11]>", line 1, in <module> b.shape ^ NameError: name 'b' is not defined ********************************************************************** ``` URL: sagemath#38683 Reported by: Antonio Rojas Reviewer(s): François Bissey
2 parents f4305c7 + 8e2b65e commit e9ded51

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

build/pkgs/configure/checksums.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
tarball=configure-VERSION.tar.gz
2-
sha1=d2f8c0bc6c40a0e3e8f7cb0deebee5e7bc7da501
3-
sha256=cde422cec1dc104f4f4b1369167a17cc77519186180bfc14322d078881581c50
2+
sha1=e5998d248052a9a02ba4f641d47af8e0267d906e
3+
sha256=d11480775f0208da67cdc11e430e90d5b613465215fe047c552f93d21d6c874d
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
27fce1faa78ef19b8c43287016f0acbdf0fa169a
1+
45e6338a983e8c7fd057c9353328d0625053108e

src/sage/matrix/matrix1.pyx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ cdef class Matrix(Matrix0):
670670
entries = [[sib(v, 2) for v in row] for row in self.rows()]
671671
return sib.name('matrix')(self.base_ring(), entries)
672672

673-
def numpy(self, dtype=None):
673+
def numpy(self, dtype=None, copy=True):
674674
"""
675675
Return the Numpy matrix associated to this matrix.
676676
@@ -680,6 +680,10 @@ cdef class Matrix(Matrix0):
680680
then the type will be determined as the minimum type required
681681
to hold the objects in the sequence.
682682
683+
- ``copy`` -- if `self` is already an `ndarray`, then this flag
684+
determines whether the data is copied (the default), or whether
685+
a view is constructed.
686+
683687
EXAMPLES::
684688
685689
sage: # needs numpy
@@ -731,7 +735,7 @@ cdef class Matrix(Matrix0):
731735
(3, 4)
732736
"""
733737
import numpy
734-
A = numpy.matrix(self.list(), dtype=dtype)
738+
A = numpy.matrix(self.list(), dtype=dtype, copy=copy)
735739
return numpy.resize(A,(self.nrows(), self.ncols()))
736740

737741
# Define the magic "__array__" function so that numpy.array(m) can convert

0 commit comments

Comments
 (0)