Skip to content

Commit

Permalink
gh-38683: Fix matrix coercion with numpy 2.1
Browse files Browse the repository at this point in the history
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: #38683
Reported by: Antonio Rojas
Reviewer(s): François Bissey
  • Loading branch information
Release Manager committed Sep 22, 2024
2 parents f4305c7 + 8e2b65e commit e9ded51
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 2 additions & 2 deletions build/pkgs/configure/checksums.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
tarball=configure-VERSION.tar.gz
sha1=d2f8c0bc6c40a0e3e8f7cb0deebee5e7bc7da501
sha256=cde422cec1dc104f4f4b1369167a17cc77519186180bfc14322d078881581c50
sha1=e5998d248052a9a02ba4f641d47af8e0267d906e
sha256=d11480775f0208da67cdc11e430e90d5b613465215fe047c552f93d21d6c874d
2 changes: 1 addition & 1 deletion build/pkgs/configure/package-version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
27fce1faa78ef19b8c43287016f0acbdf0fa169a
45e6338a983e8c7fd057c9353328d0625053108e
8 changes: 6 additions & 2 deletions src/sage/matrix/matrix1.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ cdef class Matrix(Matrix0):
entries = [[sib(v, 2) for v in row] for row in self.rows()]
return sib.name('matrix')(self.base_ring(), entries)

def numpy(self, dtype=None):
def numpy(self, dtype=None, copy=True):
"""
Return the Numpy matrix associated to this matrix.
Expand All @@ -680,6 +680,10 @@ cdef class Matrix(Matrix0):
then the type will be determined as the minimum type required
to hold the objects in the sequence.
- ``copy`` -- if `self` is already an `ndarray`, then this flag
determines whether the data is copied (the default), or whether
a view is constructed.
EXAMPLES::
sage: # needs numpy
Expand Down Expand Up @@ -731,7 +735,7 @@ cdef class Matrix(Matrix0):
(3, 4)
"""
import numpy
A = numpy.matrix(self.list(), dtype=dtype)
A = numpy.matrix(self.list(), dtype=dtype, copy=copy)
return numpy.resize(A,(self.nrows(), self.ncols()))

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

0 comments on commit e9ded51

Please sign in to comment.