def test_OneMatrix(): A = MatrixSymbol('A', n, m) a = MatrixSymbol('a', n, 1) U = OneMatrix(n, m) assert U.shape == (n, m) assert isinstance(A + U, Add) assert transpose(U) == OneMatrix(m, n) assert U.conjugate() == U assert OneMatrix(n, n) ** 0 == Identity(n) with raises(NonSquareMatrixError): U ** 0 with raises(NonSquareMatrixError): U ** 1 with raises(NonSquareMatrixError): U ** 2 with raises(ShapeError): a + U U = OneMatrix(n, n) assert U[1, 2] == 1 U = OneMatrix(2, 3) assert U.as_explicit() == ImmutableMatrix.ones(2, 3)
def test_Trace(): assert isinstance(Trace(A), Trace) assert not isinstance(Trace(A), MatrixExpr) raises(ShapeError, lambda: Trace(C)) assert trace(eye(3)) == 3 assert trace(Matrix(3, 3, [1, 2, 3, 4, 5, 6, 7, 8, 9])) == 15 assert adjoint(Trace(A)) == trace(Adjoint(A)) assert conjugate(Trace(A)) == trace(Adjoint(A)) assert transpose(Trace(A)) == Trace(A) A / Trace(A) # Make sure this is possible # Some easy simplifications assert trace(Identity(5)) == 5 assert trace(ZeroMatrix(5, 5)) == 0 assert trace(OneMatrix(1, 1)) == 1 assert trace(OneMatrix(2, 2)) == 2 assert trace(OneMatrix(n, n)) == n assert trace(2*A*B) == 2*Trace(A*B) assert trace(A.T) == trace(A) i, j = symbols('i j') F = FunctionMatrix(3, 3, Lambda((i, j), i + j)) assert trace(F) == (0 + 0) + (1 + 1) + (2 + 2) raises(TypeError, lambda: Trace(S.One)) assert Trace(A).arg is A assert str(trace(A)) == str(Trace(A).doit()) assert Trace(A).is_commutative is True
def test_eval_determinant(): assert det(Identity(n)) == 1 assert det(ZeroMatrix(n, n)) == 0 assert det(OneMatrix(n, n)) == Determinant(OneMatrix(n, n)) assert det(OneMatrix(1, 1)) == 1 assert det(OneMatrix(2, 2)) == 0 assert det(Transpose(A)) == det(A)
def test_MatrixPermute_doit(): p = Permutation(0, 1, 2) A = MatrixSymbol('A', 3, 3) assert MatrixPermute(A, p).doit() == MatrixPermute(A, p) p = Permutation(0, size=3) A = MatrixSymbol('A', 3, 3) assert MatrixPermute(A, p).doit().as_explicit() == \ MatrixPermute(A, p).as_explicit() p = Permutation(0, 1, 2) A = Identity(3) assert MatrixPermute(A, p, 0).doit().as_explicit() == \ MatrixPermute(A, p, 0).as_explicit() assert MatrixPermute(A, p, 1).doit().as_explicit() == \ MatrixPermute(A, p, 1).as_explicit() A = ZeroMatrix(3, 3) assert MatrixPermute(A, p).doit() == A A = OneMatrix(3, 3) assert MatrixPermute(A, p).doit() == A A = MatrixSymbol('A', 4, 4) p1 = Permutation(0, 1, 2, 3) p2 = Permutation(0, 2, 3, 1) expr = MatrixPermute(MatrixPermute(A, p1, 0), p2, 0) assert expr.as_explicit() == expr.doit().as_explicit() expr = MatrixPermute(MatrixPermute(A, p1, 1), p2, 1) assert expr.as_explicit() == expr.doit().as_explicit()
def test_OneMatrix(): A = MatrixSymbol('A', n, m) a = MatrixSymbol('a', n, 1) U = OneMatrix(n, m) assert U.shape == (n, m) assert isinstance(A + U, Add) assert transpose(U) == OneMatrix(m, n) assert U.conjugate() == U assert OneMatrix(n, n) ** 0 == Identity(n) with raises(ShapeError): U ** 0 with raises(ShapeError): U ** 2 U = OneMatrix(n, n) assert U[1, 2] == 1 U = OneMatrix(2, 3) assert U.as_explicit() == ImmutableMatrix.ones(2, 3)
def test_one_matrix_creation(): assert OneMatrix(2, 2) assert OneMatrix(0, 0) assert Eq(OneMatrix(1, 1), Identity(1)) raises(ValueError, lambda: OneMatrix(-1, 2)) raises(ValueError, lambda: OneMatrix(2.0, 2)) raises(ValueError, lambda: OneMatrix(2j, 2)) raises(ValueError, lambda: OneMatrix(2, -1)) raises(ValueError, lambda: OneMatrix(2, 2.0)) raises(ValueError, lambda: OneMatrix(2, 2j)) n = symbols('n') assert OneMatrix(n, n) n = symbols('n', integer=False) raises(ValueError, lambda: OneMatrix(n, n)) n = symbols('n', negative=True) raises(ValueError, lambda: OneMatrix(n, n))
def test_OneMatrix_mul(): assert OneMatrix(n, m) * OneMatrix(m, k) == OneMatrix(n, k) * m assert w * OneMatrix(1, 1) == w assert OneMatrix(1, 1) * w.T == w.T
def test_OneMatrix_doit(): Unn = OneMatrix(Add(n, n, evaluate=False), n) assert isinstance(Unn.rows, Add) assert Unn.doit() == OneMatrix(2 * n, n) assert isinstance(Unn.doit().rows, Mul)
def test_one_matrix_creation(): assert OneMatrix(2, 2) assert OneMatrix(0, 0) raises(ValueError, lambda: OneMatrix(-1, 2)) raises(ValueError, lambda: OneMatrix(2.0, 2)) raises(ValueError, lambda: OneMatrix(2j, 2)) raises(ValueError, lambda: OneMatrix(2, -1)) raises(ValueError, lambda: OneMatrix(2, 2.0)) raises(ValueError, lambda: OneMatrix(2, 2j)) n = symbols("n") assert OneMatrix(n, n) n = symbols("n", integer=False) raises(ValueError, lambda: OneMatrix(n, n)) n = symbols("n", negative=True) raises(ValueError, lambda: OneMatrix(n, n))
def test_OneMatrix_doit(): Unn = OneMatrix(Add(n, n, evaluate=False), n) assert isinstance(Unn.rows, Add) assert Unn.doit() == OneMatrix(2 * n, n) assert isinstance(Unn.doit().rows, Mul)