def test_as_explicit_nonsquare(): A = ImmutableMatrix([[1, 2, 3], [4, 5, 6]]) assert MatPow(A, 1).as_explicit() == A pytest.raises(ShapeError, lambda: MatPow(A, 0).as_explicit()) pytest.raises(ShapeError, lambda: MatPow(A, 2).as_explicit()) pytest.raises(ShapeError, lambda: MatPow(A, -1).as_explicit()) pytest.raises(ValueError, lambda: MatPow(A, pi).as_explicit())
def test_entry(): assert MatPow(A, 1)[0, 0] == A[0, 0] assert MatPow(C, 0)[0, 0] == 1 assert MatPow(C, 0)[0, 1] == 0 assert isinstance(MatPow(C, 2)[0, 0], Sum) pytest.raises(NotImplementedError, lambda: MatPow(C, -1)[0, 0])
def test_doit_nonsquare(): X = ImmutableMatrix([[1, 2, 3], [4, 5, 6]]) assert MatPow(X, 1).doit() == X pytest.raises(ShapeError, lambda: MatPow(X, 0).doit()) pytest.raises(ShapeError, lambda: MatPow(X, 2).doit()) pytest.raises(ShapeError, lambda: MatPow(X, -1).doit()) pytest.raises(ShapeError, lambda: MatPow(X, pi).doit())
def test_invariants(): A = MatrixSymbol('A', n, m) B = MatrixSymbol('B', m, l) X = MatrixSymbol('X', n, n) objs = [Identity(n), ZeroMatrix(m, n), MatMul(A, B), MatAdd(A, A), Transpose(A), Adjoint(A), Inverse(X), MatPow(X, 2), MatPow(X, -1), MatPow(X, 0)] for obj in objs: assert obj == obj.__class__(*obj.args)
def test_Zero_power(): z1 = ZeroMatrix(n, n) assert z1**4 == z1 pytest.raises(ValueError, lambda: z1**-2) assert z1**0 == Identity(n) assert MatPow(z1, 2).doit() == z1**2 pytest.raises(ValueError, lambda: MatPow(z1, -2).doit()) z2 = ZeroMatrix(3, 3) assert MatPow(z2, 4).doit() == z2**4 pytest.raises(ValueError, lambda: z2**-3) assert z2**3 == MatPow(z2, 3).doit() assert z2**0 == Identity(3) pytest.raises(ValueError, lambda: MatPow(z2, -1).doit())
def test_zero_power(): z1 = ZeroMatrix(n, n) assert MatPow(z1, 3).doit() == z1 pytest.raises(ValueError, lambda: MatPow(z1, -1).doit()) assert MatPow(z1, 0).doit() == Identity(n) assert MatPow(z1, n).doit() == z1 pytest.raises(ValueError, lambda: MatPow(z1, -2).doit()) z2 = ZeroMatrix(4, 4) assert MatPow(z2, n).doit() == z2 pytest.raises(ValueError, lambda: MatPow(z2, -3).doit()) assert MatPow(z2, 2).doit() == z2 assert MatPow(z2, 0).doit() == Identity(4) pytest.raises(ValueError, lambda: MatPow(z2, -1).doit())
def test_doit_args(): A = ImmutableMatrix([[1, 2], [3, 4]]) B = ImmutableMatrix([[2, 3], [4, 5]]) assert MatAdd(A, MatPow(B, 2)).doit() == A + B**2 assert MatAdd(A, MatMul(A, B)).doit() == A + A * B assert MatAdd(A, A).doit(deep=False) == 2 * A assert (MatAdd(A, X, MatMul(A, B), Y, MatAdd(2 * A, B)).doit() == MatAdd(X, Y, 3 * A + A * B + B))
def test_Trace_doit_deep_False(): X = Matrix([[1, 2], [3, 4]]) assert Trace(X).doit(deep=False) == 5 q = MatPow(X, 2) assert Trace(q).doit(deep=False).arg == q q = MatAdd(X, 2 * X) assert Trace(q).doit(deep=False).arg == q q = MatMul(X, 2 * X) assert Trace(q).doit(deep=False).arg == q
def test_identity_power(): k = Identity(n) assert MatPow(k, 4).doit() == k assert MatPow(k, n).doit() == k assert MatPow(k, -3).doit() == k assert MatPow(k, 0).doit() == k l = Identity(3) assert MatPow(l, n).doit() == l assert MatPow(l, -1).doit() == l assert MatPow(l, 0).doit() == l
def test_identity_powers(): M = Identity(n) assert MatPow(M, 3).doit() == M**3 assert M**n == M assert MatPow(M, 0).doit() == M**2 assert M**-2 == M assert MatPow(M, -2).doit() == M**0 N = Identity(3) assert MatPow(N, 2).doit() == N**n assert MatPow(N, 3).doit() == N assert MatPow(N, -2).doit() == N**4 assert MatPow(N, 2).doit() == N**0
def test_MatPow(): A = MatrixSymbol('A', n, n) AA = MatPow(A, 2) assert AA.exp == 2 assert AA.base == A assert (A**n).exp == n assert A**0 == Identity(n) assert A**1 == A assert A**2 == AA assert A**-1 == Inverse(A) assert A**Rational(1, 2) == sqrt(A) pytest.raises(ShapeError, lambda: MatrixSymbol('B', 3, 2)**2)
def test_as_explicit(): A = ImmutableMatrix([[1, 2], [3, 4]]) assert MatPow(A, 0).as_explicit() == ImmutableMatrix(Identity(2)) assert MatPow(A, 1).as_explicit() == A assert MatPow(A, 2).as_explicit() == A**2 assert MatPow(A, -1).as_explicit() == A.inv() assert MatPow(A, -2).as_explicit() == (A.inv())**2 # less expensive than testing on a 2x2 A = ImmutableMatrix([4]) assert MatPow(A, Rational(1, 2)).as_explicit() == A**Rational(1, 2)
def test_doit_with_MatrixBase(): X = ImmutableMatrix([[1, 2], [3, 4]]) assert MatPow(X, 0).doit() == ImmutableMatrix(Identity(2)) assert MatPow(X, 1).doit() == X assert MatPow(X, 2).doit() == X**2 assert MatPow(X, -1).doit() == X.inv() assert MatPow(X, -2).doit() == (X.inv())**2 # less expensive than testing on a 2x2 assert MatPow(ImmutableMatrix([4]), Rational(1, 2)).doit() == ImmutableMatrix([2])
def test_pow_index(): Q = MatPow(A, 2) assert Q[0, 0] == A[0, 0]**2 + A[0, 1]*A[1, 0]
def test_matpow(): pytest.raises(TypeError, lambda: MatPow(1, 1))
def test_as_explicit_symbol(): X = MatrixSymbol('X', 2, 2) assert MatPow(X, 0).as_explicit() == ImmutableMatrix(Identity(2)) assert MatPow(X, 1).as_explicit() == X.as_explicit() assert MatPow(X, 2).as_explicit() == (X.as_explicit())**2
def test_as_explicit_nonsquare_symbol(): X = MatrixSymbol('X', 2, 3) assert MatPow(X, 1).as_explicit() == X.as_explicit() for r in [0, 2, Rational(1, 2), pi]: pytest.raises(ShapeError, lambda: MatPow(X, r).as_explicit())
def test_Trace_MatPow_doit(): X = Matrix([[1, 2], [3, 4]]) assert Trace(X).doit() == 5 q = MatPow(X, 2) assert Trace(q).arg == q assert Trace(q).doit() == 29
def test_doit_drills_down(): X = ImmutableMatrix([[1, 2], [3, 4]]) Y = ImmutableMatrix([[2, 3], [4, 5]]) assert MatMul(X, MatPow(Y, 2)).doit() == X * Y**2 assert MatMul(C, Transpose(D * C)).doit().args == (C, C.T, D.T)
def test_doit_nested_MatrixExpr(): X = ImmutableMatrix([[1, 2], [3, 4]]) Y = ImmutableMatrix([[2, 3], [4, 5]]) assert MatPow(MatMul(X, Y), 2).doit() == (X * Y)**2 assert MatPow(MatAdd(X, Y), 2).doit() == (X + Y)**2
def test_doit_nonsquare_MatrixSymbol(): assert MatPow(A, 1).doit() == A for r in [0, 2, -1, pi]: assert MatPow(A, r).doit() == MatPow(A, r)
def test_doit_square_MatrixSymbol_symsize(): assert MatPow(C, 0).doit() == Identity(n) assert MatPow(C, 0).doit(deep=False) == Identity(n) assert MatPow(C, 1).doit() == C for r in [2, -1, pi]: assert MatPow(C, r).doit() == MatPow(C, r)