Skip to content

Commit 0b42f52

Browse files
Merge pull request sympy#19229 from sylee957/fix_ndarray_matrix
Fix creation of matrix with list of ndarray
2 parents cc48fad + 2343044 commit 0b42f52

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

sympy/matrices/matrices.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,23 @@ def irregular(cls, ntop, *matrices, **kwargs):
900900
rows.append(r)
901901
return cls._new(rows)
902902

903+
@classmethod
904+
def _handle_ndarray(cls, arg):
905+
# NumPy array or matrix or some other object that implements
906+
# __array__. So let's first use this method to get a
907+
# numpy.array() and then make a python list out of it.
908+
arr = arg.__array__()
909+
if len(arr.shape) == 2:
910+
rows, cols = arr.shape[0], arr.shape[1]
911+
flat_list = [cls._sympify(i) for i in arr.ravel()]
912+
return rows, cols, flat_list
913+
elif len(arr.shape) == 1:
914+
flat_list = [cls._sympify(i) for i in arr]
915+
return arr.shape[0], 1, flat_list
916+
else:
917+
raise NotImplementedError(
918+
"SymPy supports just 1D and 2D matrices")
919+
903920
@classmethod
904921
def _handle_creation_inputs(cls, *args, **kwargs):
905922
"""Return the number of rows, cols and flat matrix elements.
@@ -973,23 +990,7 @@ def _handle_creation_inputs(cls, *args, **kwargs):
973990

974991
# Matrix(numpy.ones((2, 2)))
975992
elif hasattr(args[0], "__array__"):
976-
# NumPy array or matrix or some other object that implements
977-
# __array__. So let's first use this method to get a
978-
# numpy.array() and then make a python list out of it.
979-
arr = args[0].__array__()
980-
if len(arr.shape) == 2:
981-
rows, cols = arr.shape[0], arr.shape[1]
982-
flat_list = [cls._sympify(i) for i in arr.ravel()]
983-
return rows, cols, flat_list
984-
elif len(arr.shape) == 1:
985-
rows, cols = arr.shape[0], 1
986-
flat_list = [cls.zero] * rows
987-
for i in range(len(arr)):
988-
flat_list[i] = cls._sympify(arr[i])
989-
return rows, cols, flat_list
990-
else:
991-
raise NotImplementedError(
992-
"SymPy supports just 1D and 2D matrices")
993+
return cls._handle_ndarray(args[0])
993994

994995
# Matrix([1, 2, 3]) or Matrix([[1, 2], [3, 4]])
995996
elif is_sequence(args[0]) \
@@ -1064,13 +1065,19 @@ def do(x):
10641065
if not is_sequence(row) and \
10651066
not getattr(row, 'is_Matrix', False):
10661067
raise ValueError('expecting list of lists')
1067-
if not row:
1068+
1069+
if hasattr(row, '__array__'):
1070+
if 0 in row.shape:
1071+
continue
1072+
elif not row:
10681073
continue
1074+
10691075
if evaluate and all(ismat(i) for i in row):
10701076
r, c, flatT = cls._handle_creation_inputs(
10711077
[i.T for i in row])
10721078
T = reshape(flatT, [c])
1073-
flat = [T[i][j] for j in range(c) for i in range(r)]
1079+
flat = \
1080+
[T[i][j] for j in range(c) for i in range(r)]
10741081
r, c = c, r
10751082
else:
10761083
r = 1

sympy/matrices/tests/test_matrices.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,8 +2601,11 @@ def test_from_ndarray():
26012601
assert Matrix(array([[1, 2, 3], [4, 5, 6]])) == \
26022602
Matrix([[1, 2, 3], [4, 5, 6]])
26032603
assert Matrix(array([x, y, z])) == Matrix([x, y, z])
2604-
raises(NotImplementedError, lambda: Matrix(array([[
2605-
[1, 2], [3, 4]], [[5, 6], [7, 8]]])))
2604+
raises(NotImplementedError,
2605+
lambda: Matrix(array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])))
2606+
assert Matrix([array([1, 2]), array([3, 4])]) == Matrix([[1, 2], [3, 4]])
2607+
assert Matrix([array([1, 2]), [3, 4]]) == Matrix([[1, 2], [3, 4]])
2608+
assert Matrix([array([]), array([])]) == Matrix([])
26062609

26072610
def test_17522_numpy():
26082611
from sympy.matrices.common import _matrixify

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy