Skip to content

Commit 7c63c98

Browse files
committed
Add tests
1 parent 2abc7a0 commit 7c63c98

File tree

6 files changed

+49
-25
lines changed

6 files changed

+49
-25
lines changed

sympy/matrices/dense.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ class DenseMatrix(MatrixBase):
4242
_op_priority = 10.01
4343
_class_priority = 4
4444

45-
__hash__ = None
46-
4745
def __eq__(self, other):
4846
other = sympify(other)
4947
self_shape = getattr(self, 'shape', None)
@@ -292,6 +290,8 @@ def _force_mutable(x):
292290

293291

294292
class MutableDenseMatrix(DenseMatrix, MatrixBase):
293+
__hash__ = None
294+
295295
def __new__(cls, *args, **kwargs):
296296
return cls._new(*args, **kwargs)
297297

sympy/matrices/expressions/tests/test_matexpr.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,15 @@ def test_subs():
112112
C = MatrixSymbol('C', m, l)
113113

114114
assert A.subs(n, m).shape == (m, m)
115-
116115
assert (A*B).subs(B, C) == A*C
117-
118116
assert (A*B).subs(l, n).is_square
119117

118+
A = SparseMatrix([[1, 2], [3, 4]])
119+
B = Matrix([[1, 2], [3, 4]])
120+
C, D = MatrixSymbol('C', 2, 2), MatrixSymbol('D', 2, 2)
121+
122+
assert (C*D).subs({C: A, D: B}) == MatMul(A, B)
123+
120124

121125
def test_ZeroMatrix():
122126
A = MatrixSymbol('A', n, m)

sympy/matrices/immutable.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def sympify_mpmath_matrix(arg):
2020
sympify_converter[_matrix] = sympify_mpmath_matrix
2121

2222

23-
class ImmutableDenseMatrix(MatrixExpr, DenseMatrix):
23+
class ImmutableDenseMatrix(DenseMatrix, MatrixExpr):
2424
"""Create an immutable version of a matrix.
2525
2626
Examples
@@ -48,6 +48,8 @@ class ImmutableDenseMatrix(MatrixExpr, DenseMatrix):
4848
def __new__(cls, *args, **kwargs):
4949
return cls._new(*args, **kwargs)
5050

51+
__hash__ = MatrixExpr.__hash__
52+
5153
@classmethod
5254
def _new(cls, *args, **kwargs):
5355
if len(args) == 1 and isinstance(args[0], ImmutableDenseMatrix):
@@ -131,7 +133,7 @@ def is_diagonalizable(self, reals_only=False, **kwargs):
131133
ImmutableMatrix = ImmutableDenseMatrix
132134

133135

134-
class ImmutableSparseMatrix(MatrixExpr, SparseMatrix):
136+
class ImmutableSparseMatrix(SparseMatrix, MatrixExpr):
135137
"""Create an immutable version of a sparse matrix.
136138
137139
Examples
@@ -159,6 +161,8 @@ class ImmutableSparseMatrix(MatrixExpr, SparseMatrix):
159161
def __new__(cls, *args, **kwargs):
160162
return cls._new(*args, **kwargs)
161163

164+
__hash__ = MatrixExpr.__hash__
165+
162166
@classmethod
163167
def _new(cls, *args, **kwargs):
164168
s = MutableSparseMatrix(*args)

sympy/matrices/matrices.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,10 +1265,12 @@ def cross(self, b):
12651265
multiply
12661266
multiply_elementwise
12671267
"""
1268-
if not is_sequence(b):
1268+
from sympy.matrices.expressions.matexpr import MatrixExpr
1269+
1270+
if not isinstance(b, MatrixBase) and not isinstance(b, MatrixExpr):
12691271
raise TypeError(
1270-
"`b` must be an ordered iterable or Matrix, not %s." %
1271-
type(b))
1272+
"{} must be a Matrix, not {}.".format(b, type(b)))
1273+
12721274
if not (self.rows * self.cols == b.rows * b.cols == 3):
12731275
raise ShapeError("Dimensions incorrect for cross product: %s x %s" %
12741276
((self.rows, self.cols), (b.rows, b.cols)))

sympy/matrices/tests/test_immutable.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
from itertools import product
22

33
from sympy import (ImmutableMatrix, Matrix, eye, zeros, S, Equality,
4-
Unequality, ImmutableSparseMatrix, SparseMatrix, sympify,
5-
integrate)
4+
Unequality, SparseMatrix, sympify, integrate)
5+
from sympy.matrices.immutable import \
6+
ImmutableDenseMatrix, ImmutableSparseMatrix
67
from sympy.abc import x, y
78
from sympy.testing.pytest import raises
89

9-
IM = ImmutableMatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
10-
ieye = ImmutableMatrix(eye(3))
10+
IM = ImmutableDenseMatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
11+
ISM = ImmutableSparseMatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
12+
ieye = ImmutableDenseMatrix(eye(3))
1113

1214

13-
def test_immutable_creation():
14-
assert IM.shape == (3, 3)
15-
assert IM[1, 2] == 6
16-
assert IM[2, 2] == 9
15+
def test_creation():
16+
assert IM.shape == ISM.shape == (3, 3)
17+
assert IM[1, 2] == ISM[1, 2] == 6
18+
assert IM[2, 2] == ISM[2, 2] == 9
1719

1820

1921
def test_immutability():
2022
with raises(TypeError):
2123
IM[2, 2] = 5
24+
ISM[2, 2]
2225

2326

2427
def test_slicing():
25-
assert IM[1, :] == ImmutableMatrix([[4, 5, 6]])
26-
assert IM[:2, :2] == ImmutableMatrix([[1, 2], [4, 5]])
28+
assert IM[1, :] == ImmutableDenseMatrix([[4, 5, 6]])
29+
assert IM[:2, :2] == ImmutableDenseMatrix([[1, 2], [4, 5]])
30+
assert ISM[1, :] == ImmutableSparseMatrix([[4, 5, 6]])
31+
assert ISM[:2, :2] == ImmutableSparseMatrix([[1, 2], [4, 5]])
2732

2833

2934
def test_subs():
@@ -41,11 +46,13 @@ def test_subs():
4146

4247

4348
def test_as_immutable():
44-
X = Matrix([[1, 2], [3, 4]])
45-
assert sympify(X) == X.as_immutable() == ImmutableMatrix([[1, 2], [3, 4]])
46-
X = SparseMatrix(5, 5, {})
47-
assert sympify(X) == X.as_immutable() == ImmutableSparseMatrix(
48-
[[0 for i in range(5)] for i in range(5)])
49+
data = [[1, 2], [3, 4]]
50+
X = Matrix(data)
51+
assert sympify(X) == X.as_immutable() == ImmutableMatrix(data)
52+
53+
data = {(0, 0): 1, (0, 1): 2, (1, 0): 3, (1, 1): 4}
54+
X = SparseMatrix(2, 2, data)
55+
assert sympify(X) == X.as_immutable() == ImmutableSparseMatrix(2, 2, data)
4956

5057

5158
def test_function_return_types():

sympy/physics/quantum/tests/test_tensorproduct.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from sympy import I, symbols, Matrix
1+
from sympy import I, symbols
2+
from sympy.core.expr import unchanged
3+
from sympy.matrices import Matrix, SparseMatrix
24

35
from sympy.physics.quantum.commutator import Commutator as Comm
46
from sympy.physics.quantum.tensorproduct import TensorProduct
@@ -17,6 +19,11 @@
1719
mat2 = Matrix([[2*I, 3], [4*I, 2]])
1820

1921

22+
def test_sparse_matrices():
23+
spm = SparseMatrix.diag(1, 0)
24+
assert unchanged(TensorProduct, spm, spm)
25+
26+
2027
def test_tensor_product_dagger():
2128
assert Dagger(TensorProduct(I*A, B)) == \
2229
-I*TensorProduct(Dagger(A), Dagger(B))

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