def test_todok(): a, b, c, d = symbols('a:d') m1 = MutableDenseMatrix([[a, b], [c, d]]) m2 = ImmutableDenseMatrix([[a, b], [c, d]]) m3 = MutableSparseMatrix([[a, b], [c, d]]) m4 = ImmutableSparseMatrix([[a, b], [c, d]]) assert m1.todok() == m2.todok() == m3.todok() == m4.todok() == \ {(0, 0): a, (0, 1): b, (1, 0): c, (1, 1): d}
def test_matrices(): from sympy.matrices import MutableDenseMatrix, MutableSparseMatrix, \ ImmutableDenseMatrix, ImmutableSparseMatrix A = MutableDenseMatrix( [[1, -1, 0, 0], [0, 1, -1, 0], [0, 0, 1, -1], [0, 0, 0, 1]] ) B = MutableSparseMatrix(A) C = ImmutableDenseMatrix(A) D = ImmutableSparseMatrix(A) assert mcode(C) == mcode(A) == \ "{{1, -1, 0, 0}, " \ "{0, 1, -1, 0}, " \ "{0, 0, 1, -1}, " \ "{0, 0, 0, 1}}" assert mcode(D) == mcode(B) == \ "SparseArray[{" \ "{1, 1} -> 1, {1, 2} -> -1, {2, 2} -> 1, {2, 3} -> -1, " \ "{3, 3} -> 1, {3, 4} -> -1, {4, 4} -> 1" \ "}, {4, 4}]" # Trivial cases of matrices assert mcode(MutableDenseMatrix(0, 0, [])) == '{}' assert mcode(MutableSparseMatrix(0, 0, [])) == 'SparseArray[{}, {0, 0}]' assert mcode(MutableDenseMatrix(0, 3, [])) == '{}' assert mcode(MutableSparseMatrix(0, 3, [])) == 'SparseArray[{}, {0, 3}]' assert mcode(MutableDenseMatrix(3, 0, [])) == '{{}, {}, {}}' assert mcode(MutableSparseMatrix(3, 0, [])) == 'SparseArray[{}, {3, 0}]'
def test_diagonal_solve(): a, d = symbols('a d') u, v, w, x = symbols('u:x') A = SparseMatrix([[a, 0], [0, d]]) B = MutableSparseMatrix([[u, v], [w, x]]) C = ImmutableSparseMatrix([[u, v], [w, x]]) sol = Matrix([[u / a, v / a], [w / d, x / d]]) assert A.diagonal_solve(B) == sol assert A.diagonal_solve(C) == sol
def test_upper_triangular_solve(): a, b, c, d = symbols('a:d') u, v, w, x = symbols('u:x') A = SparseMatrix([[a, b], [0, d]]) B = MutableSparseMatrix([[u, v], [w, x]]) C = ImmutableSparseMatrix([[u, v], [w, x]]) sol = Matrix([[(u - b * w / d) / a, (v - b * x / d) / a], [w / d, x / d]]) assert A.upper_triangular_solve(B) == sol assert A.upper_triangular_solve(C) == sol
def test_lower_triangular_solve(): a, b, c, d = symbols('a:d') u, v, w, x = symbols('u:x') A = SparseMatrix([[a, 0], [c, d]]) B = MutableSparseMatrix([[u, v], [w, x]]) C = ImmutableSparseMatrix([[u, v], [w, x]]) sol = Matrix([[u / a, v / a], [(w - c * u / a) / d, (x - c * v / a) / d]]) assert A.lower_triangular_solve(B) == sol assert A.lower_triangular_solve(C) == sol
def test_issue_15791(): class CrashingCodePrinter(CodePrinter): def emptyPrinter(self, obj): raise NotImplementedError from sympy.matrices import ( MutableSparseMatrix, ImmutableSparseMatrix, ) c = CrashingCodePrinter() # these should not silently succeed with raises(NotImplementedError): c.doprint(ImmutableSparseMatrix(2, 2, {})) with raises(NotImplementedError): c.doprint(MutableSparseMatrix(2, 2, {}))
def test_upper_triangular_solve(): raises( NonSquareMatrixError, lambda: SparseMatrix([[1, 2]]). upper_triangular_solve(Matrix([[1, 2]]))) raises( ShapeError, lambda: SparseMatrix([[1, 2], [0, 4]]).upper_triangular_solve( Matrix([1]))) raises( TypeError, lambda: SparseMatrix([[1, 2], [3, 4]]).upper_triangular_solve( Matrix([[1, 2], [3, 4]]))) a, b, c, d = symbols('a:d') u, v, w, x = symbols('u:x') A = SparseMatrix([[a, b], [0, d]]) B = MutableSparseMatrix([[u, v], [w, x]]) C = ImmutableSparseMatrix([[u, v], [w, x]]) sol = Matrix([[(u - b * w / d) / a, (v - b * x / d) / a], [w / d, x / d]]) assert A.upper_triangular_solve(B) == sol assert A.upper_triangular_solve(C) == sol
def test_lower_triangular_solve(): raises( NonSquareMatrixError, lambda: SparseMatrix([[1, 2]]). lower_triangular_solve(Matrix([[1, 2]]))) raises( ShapeError, lambda: SparseMatrix([[1, 2], [0, 4]]).lower_triangular_solve( Matrix([1]))) raises( ValueError, lambda: SparseMatrix([[1, 2], [3, 4]]).lower_triangular_solve( Matrix([[1, 2], [3, 4]]))) a, b, c, d = symbols('a:d') u, v, w, x = symbols('u:x') A = SparseMatrix([[a, 0], [c, d]]) B = MutableSparseMatrix([[u, v], [w, x]]) C = ImmutableSparseMatrix([[u, v], [w, x]]) sol = Matrix([[u / a, v / a], [(w - c * u / a) / d, (x - c * v / a) / d]]) assert A.lower_triangular_solve(B) == sol assert A.lower_triangular_solve(C) == sol
def test_block_collapse_explicit_matrices(): A = Matrix([[1, 2], [3, 4]]) assert block_collapse(BlockMatrix([[A]])) == A A = ImmutableSparseMatrix([[1, 2], [3, 4]]) assert block_collapse(BlockMatrix([[A]])) == A