Skip to content

Commit 5b2eabb

Browse files
authored
Merge pull request sympy#20175 from sidhu1012/mat
Comparing matrix with object
2 parents 702bcea + 1708a55 commit 5b2eabb

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

sympy/matrices/dense.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from sympy.core.expr import Expr
77
from sympy.core.singleton import S
88
from sympy.core.symbol import Symbol
9-
from sympy.core.sympify import sympify
9+
from sympy.core.sympify import sympify, _sympify
1010
from sympy.functions.elementary.trigonometric import cos, sin
1111
from sympy.matrices.common import \
1212
a2idx, classof, ShapeError
@@ -43,7 +43,10 @@ class DenseMatrix(MatrixBase):
4343
_class_priority = 4
4444

4545
def __eq__(self, other):
46-
other = sympify(other)
46+
try:
47+
other = _sympify(other)
48+
except SympifyError:
49+
return NotImplemented
4750
self_shape = getattr(self, 'shape', None)
4851
other_shape = getattr(other, 'shape', None)
4952
if None in (self_shape, other_shape):

sympy/matrices/sparse.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from sympy.core.containers import Dict
66
from sympy.core.expr import Expr
77
from sympy.core.singleton import S
8+
from sympy.core.sympify import _sympify
89
from sympy.functions import Abs
910
from sympy.utilities.iterables import uniq
1011

@@ -239,6 +240,10 @@ def update(i, j, v):
239240
return rows, cols, smat
240241

241242
def __eq__(self, other):
243+
try:
244+
other = _sympify(other)
245+
except SympifyError:
246+
return NotImplemented
242247
self_shape = getattr(self, 'shape', None)
243248
other_shape = getattr(other, 'shape', None)
244249
if None in (self_shape, other_shape):

sympy/matrices/tests/test_densearith.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from sympy.testing.pytest import ignore_warnings
22
from sympy.utilities.exceptions import SymPyDeprecationWarning
33

4+
from sympy.matrices import Matrix, SparseMatrix, ImmutableMatrix
5+
46
with ignore_warnings(SymPyDeprecationWarning):
57
from sympy.matrices.densetools import eye
68
from sympy.matrices.densearith import add, sub, mulmatmat, mulmatscaler
@@ -52,3 +54,12 @@ def test_mulmatscaler():
5254

5355
assert mulmatscaler(a, ZZ(4), ZZ) == [[ZZ(4), ZZ(0), ZZ(0)], [ZZ(0), ZZ(4), ZZ(0)], [ZZ(0), ZZ(0), ZZ(4)]]
5456
assert mulmatscaler(b, ZZ(1), ZZ) == [[ZZ(3), ZZ(7), ZZ(4)], [ZZ(2), ZZ(4), ZZ(5)], [ZZ(6), ZZ(2), ZZ(3)]]
57+
58+
def test_eq():
59+
A = Matrix([[1]])
60+
B = ImmutableMatrix([[1]])
61+
C = SparseMatrix([[1]])
62+
assert A != object()
63+
assert A != "Matrix([[1]])"
64+
assert A == B
65+
assert A == C

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