def test_DomainMatrix_rref(): A = DomainMatrix([], (0, 1), QQ) assert A.rref() == (A, ()) A = DomainMatrix([[QQ(1)]], (1, 1), QQ) assert A.rref() == (A, (0, )) A = DomainMatrix([[QQ(0)]], (1, 1), QQ) assert A.rref() == (A, ()) A = DomainMatrix([[QQ(1), QQ(2)], [QQ(3), QQ(4)]], (2, 2), QQ) Ar, pivots = A.rref() assert Ar == DomainMatrix([[QQ(1), QQ(0)], [QQ(0), QQ(1)]], (2, 2), QQ) assert pivots == (0, 1) A = DomainMatrix([[QQ(0), QQ(2)], [QQ(3), QQ(4)]], (2, 2), QQ) Ar, pivots = A.rref() assert Ar == DomainMatrix([[QQ(1), QQ(0)], [QQ(0), QQ(1)]], (2, 2), QQ) assert pivots == (0, 1) A = DomainMatrix([[QQ(0), QQ(2)], [QQ(0), QQ(4)]], (2, 2), QQ) Ar, pivots = A.rref() assert Ar == DomainMatrix([[QQ(0), QQ(1)], [QQ(0), QQ(0)]], (2, 2), QQ) assert pivots == (1, ) Az = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) raises(DMNotAField, lambda: Az.rref())
def test_DomainMatrix_init(): A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) assert A.rep == DDM([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) assert A.shape == (2, 2) assert A.domain == ZZ raises(DDMBadInputError, lambda: DomainMatrix([[ZZ(1), ZZ(2)]], (2, 2), ZZ))
def test_dom_eigenvects_rootof(): # Algebraic eigenvalues A = DomainMatrix([[0, 0, 0, 0, -1], [1, 0, 0, 0, 1], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0]], (5, 5), QQ) Avects = dom_eigenvects(A) # Extract the dummy to build the expected result: lamda = Avects[1][0][1].gens[0] irreducible = Poly(lamda**5 - lamda + 1, lamda, domain=QQ) K = FiniteExtension(irreducible) KK = K.from_sympy algebraic_eigenvects = [ (K, irreducible, 1, DomainMatrix( [[KK(lamda**4 - 1), KK(lamda**3), KK(lamda**2), KK(lamda), KK(1)]], (1, 5), K)), ] assert Avects == ([], algebraic_eigenvects) # Test converting to Expr (slow): l0, l1, l2, l3, l4 = [CRootOf(lamda**5 - lamda + 1, i) for i in range(5)] sympy_eigenvects = [ (l0, 1, [Matrix([-1 + l0**4, l0**3, l0**2, l0, 1])]), (l1, 1, [Matrix([-1 + l1**4, l1**3, l1**2, l1, 1])]), (l2, 1, [Matrix([-1 + l2**4, l2**3, l2**2, l2, 1])]), (l3, 1, [Matrix([-1 + l3**4, l3**3, l3**2, l3, 1])]), (l4, 1, [Matrix([-1 + l4**4, l4**3, l4**2, l4, 1])]), ] assert dom_eigenvects_to_sympy([], algebraic_eigenvects, Matrix) == sympy_eigenvects
def test_dom_eigenvects_algebraic(): # Algebraic eigenvalues A = DomainMatrix([[QQ(1), QQ(2)], [QQ(3), QQ(4)]], (2, 2), QQ) Avects = dom_eigenvects(A) # Extract the dummy to build the expected result: lamda = Avects[1][0][1].gens[0] irreducible = Poly(lamda**2 - 5 * lamda - 2, lamda, domain=QQ) K = FiniteExtension(irreducible) KK = K.from_sympy algebraic_eigenvects = [ (K, irreducible, 1, DomainMatrix([[KK((lamda - 4) / 3), KK(1)]], (1, 2), K)), ] assert Avects == ([], algebraic_eigenvects) # Test converting to Expr: sympy_eigenvects = [ (S(5) / 2 - sqrt(33) / 2, 1, [Matrix([[-sqrt(33) / 6 - S(1) / 2], [1]])]), (S(5) / 2 + sqrt(33) / 2, 1, [Matrix([[-S(1) / 2 + sqrt(33) / 6], [1]])]), ] assert dom_eigenvects_to_sympy([], algebraic_eigenvects, Matrix) == sympy_eigenvects
def test_DomainMatrix_eq(): A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) assert A == A B = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(1)]], (2, 2), ZZ) assert A != B C = [[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]] assert A != C
def test_DomainMatrix_nullspace(): A = DomainMatrix([[QQ(1), QQ(1)], [QQ(1), QQ(1)]], (2, 2), QQ) Anull = DomainMatrix([[QQ(-1), QQ(1)]], (1, 2), QQ) assert A.nullspace() == Anull Az = DomainMatrix([[ZZ(1), ZZ(1)], [ZZ(1), ZZ(1)]], (2, 2), ZZ) raises(DMNotAField, lambda: Az.nullspace())
def test_DomainMatrix_extract(): dM1 = DomainMatrix( [[ZZ(1), ZZ(2), ZZ(3)], [ZZ(4), ZZ(5), ZZ(6)], [ZZ(7), ZZ(8), ZZ(9)]], (3, 3), ZZ) dM2 = DomainMatrix([[ZZ(1), ZZ(3)], [ZZ(7), ZZ(9)]], (2, 2), ZZ) assert dM1.extract([0, 2], [0, 2]) == dM2 assert dM1.to_sparse().extract([0, 2], [0, 2]) == dM2.to_sparse() assert dM1.extract([0, -1], [0, -1]) == dM2 assert dM1.to_sparse().extract([0, -1], [0, -1]) == dM2.to_sparse() dM3 = DomainMatrix( [[ZZ(1), ZZ(2), ZZ(2)], [ZZ(4), ZZ(5), ZZ(5)], [ZZ(4), ZZ(5), ZZ(5)]], (3, 3), ZZ) assert dM1.extract([0, 1, 1], [0, 1, 1]) == dM3 assert dM1.to_sparse().extract([0, 1, 1], [0, 1, 1]) == dM3.to_sparse() empty = [ ([], [], (0, 0)), ([1], [], (1, 0)), ([], [1], (0, 1)), ] for rows, cols, size in empty: assert dM1.extract(rows, cols) == DomainMatrix.zeros(size, ZZ).to_dense() assert dM1.to_sparse().extract(rows, cols) == DomainMatrix.zeros(size, ZZ) dM = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) bad_indices = [([2], [0]), ([0], [2]), ([-3], [0]), ([0], [-3])] for rows, cols in bad_indices: raises(IndexError, lambda: dM.extract(rows, cols)) raises(IndexError, lambda: dM.to_sparse().extract(rows, cols))
def test_DomainMatrix_unify(): Az = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) Aq = DomainMatrix([[QQ(1), QQ(2)], [QQ(3), QQ(4)]], (2, 2), QQ) assert Az.unify(Az) == (Az, Az) assert Az.unify(Aq) == (Aq, Aq) assert Aq.unify(Az) == (Aq, Aq) assert Aq.unify(Aq) == (Aq, Aq)
def test_DomainMatrix_columnspace(): A = DomainMatrix( [[QQ(1), QQ(-1), QQ(1)], [QQ(2), QQ(-2), QQ(3)]], (2, 3), QQ) Acol = DomainMatrix([[QQ(1), QQ(1)], [QQ(2), QQ(3)]], (2, 2), QQ) assert A.columnspace() == Acol Az = DomainMatrix( [[ZZ(1), ZZ(-1), ZZ(1)], [ZZ(2), ZZ(-2), ZZ(3)]], (2, 3), ZZ) raises(DMNotAField, lambda: Az.columnspace()) A = DomainMatrix( [[QQ(1), QQ(-1), QQ(1)], [QQ(2), QQ(-2), QQ(3)]], (2, 3), QQ, fmt='sparse') Acol = DomainMatrix({ 0: { 0: QQ(1), 1: QQ(1) }, 1: { 0: QQ(2), 1: QQ(3) } }, (2, 2), QQ) assert A.columnspace() == Acol
def test_DomainMatrix_unify_eq(): A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) B1 = DomainMatrix([[QQ(1), QQ(2)], [QQ(3), QQ(4)]], (2, 2), QQ) B2 = DomainMatrix([[QQ(1), QQ(3)], [QQ(3), QQ(4)]], (2, 2), QQ) B3 = DomainMatrix([[ZZ(1)]], (1, 1), ZZ) assert A.unify_eq(B1) is True assert A.unify_eq(B2) is False assert A.unify_eq(B3) is False
def test_DomainMatrix_setitem(): dM = DomainMatrix({2: {2: ZZ(1)}, 4: {4: ZZ(1)}}, (5, 5), ZZ) dM[2, 2] = ZZ(2) assert dM == DomainMatrix({2: {2: ZZ(2)}, 4: {4: ZZ(1)}}, (5, 5), ZZ) def setitem(i, j, val): dM[i, j] = val raises(TypeError, lambda: setitem(2, 2, QQ(1, 2))) raises(NotImplementedError, lambda: setitem(slice(1, 2), 2, ZZ(1)))
def test_DomainMatrix_scalarmul(): A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) lamda = DomainScalar(QQ(3)/QQ(2), QQ) assert A * lamda == DomainMatrix([[QQ(3, 2), QQ(3)], [QQ(9, 2), QQ(6)]], (2, 2), QQ) assert A * 2 == DomainMatrix([[ZZ(2), ZZ(4)], [ZZ(6), ZZ(8)]], (2, 2), ZZ) assert A * DomainScalar(ZZ(0), ZZ) == DomainMatrix([[ZZ(0)]*2]*2, (2, 2), ZZ) assert A * DomainScalar(ZZ(1), ZZ) == A raises(TypeError, lambda: A * 1.5)
def test_DomainMatrix_init(): lol = [[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]] dod = {0: {0: ZZ(1), 1: ZZ(2)}, 1: {0: ZZ(3), 1: ZZ(4)}} ddm = DDM(lol, (2, 2), ZZ) sdm = SDM(dod, (2, 2), ZZ) A = DomainMatrix(lol, (2, 2), ZZ) assert A.rep == ddm assert A.shape == (2, 2) assert A.domain == ZZ A = DomainMatrix(dod, (2, 2), ZZ) assert A.rep == sdm assert A.shape == (2, 2) assert A.domain == ZZ raises(TypeError, lambda: DomainMatrix(ddm, (2, 2), ZZ)) raises(TypeError, lambda: DomainMatrix(sdm, (2, 2), ZZ)) raises(TypeError, lambda: DomainMatrix(Matrix([[1]]), (1, 1), ZZ)) for fmt, rep in [('sparse', sdm), ('dense', ddm)]: A = DomainMatrix(lol, (2, 2), ZZ, fmt=fmt) assert A.rep == rep A = DomainMatrix(dod, (2, 2), ZZ, fmt=fmt) assert A.rep == rep raises(ValueError, lambda: DomainMatrix(lol, (2, 2), ZZ, fmt='invalid')) raises(DMBadInputError, lambda: DomainMatrix([[ZZ(1), ZZ(2)]], (2, 2), ZZ))
def test_DomainMatrix_mul(): A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) A2 = DomainMatrix([[ZZ(7), ZZ(10)], [ZZ(15), ZZ(22)]], (2, 2), ZZ) assert A * A == A.matmul(A) == A2 A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) L = [[1, 2], [3, 4]] raises(TypeError, lambda: A * L) raises(TypeError, lambda: L * A) Az = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) Aq = DomainMatrix([[QQ(1), QQ(2)], [QQ(3), QQ(4)]], (2, 2), QQ) raises(DDMDomainError, lambda: Az * Aq) raises(DDMDomainError, lambda: Aq * Az) raises(DDMDomainError, lambda: Az.matmul(Aq)) raises(DDMDomainError, lambda: Aq.matmul(Az)) A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) AA = DomainMatrix([[ZZ(2), ZZ(4)], [ZZ(6), ZZ(8)]], (2, 2), ZZ) x = ZZ(2) assert A * x == x * A == A.mul(x) == AA A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) AA = DomainMatrix([[ZZ(0), ZZ(0)], [ZZ(0), ZZ(0)]], (2, 2), ZZ) x = ZZ(0) assert A * x == x * A == A.mul(x) == AA
def test_DomainMatrix_truediv(): A = DomainMatrix.from_Matrix(Matrix([[1, 2], [3, 4]])) lamda = DomainScalar(QQ(3) / QQ(2), QQ) assert A / lamda == DomainMatrix( { 0: { 0: QQ(2, 3), 1: QQ(4, 3) }, 1: { 0: QQ(2), 1: QQ(8, 3) } }, (2, 2), QQ) b = DomainScalar(ZZ(1), ZZ) assert A / b == DomainMatrix( { 0: { 0: QQ(1), 1: QQ(2) }, 1: { 0: QQ(3), 1: QQ(4) } }, (2, 2), QQ) assert A / 1 == DomainMatrix( { 0: { 0: QQ(1), 1: QQ(2) }, 1: { 0: QQ(3), 1: QQ(4) } }, (2, 2), QQ) assert A / 2 == DomainMatrix( { 0: { 0: QQ(1, 2), 1: QQ(1) }, 1: { 0: QQ(3, 2), 1: QQ(2) } }, (2, 2), QQ) raises(ZeroDivisionError, lambda: A / 0) raises(TypeError, lambda: A / 1.5) raises(ZeroDivisionError, lambda: A / DomainScalar(ZZ(0), ZZ))
def test_DomainMatrix_pow(): A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) A2 = DomainMatrix([[ZZ(7), ZZ(10)], [ZZ(15), ZZ(22)]], (2, 2), ZZ) A3 = DomainMatrix([[ZZ(37), ZZ(54)], [ZZ(81), ZZ(118)]], (2, 2), ZZ) eye = DomainMatrix([[ZZ(1), ZZ(0)], [ZZ(0), ZZ(1)]], (2, 2), ZZ) assert A**0 == A.pow(0) == eye assert A**1 == A.pow(1) == A assert A**2 == A.pow(2) == A2 assert A**3 == A.pow(3) == A3 raises(TypeError, lambda: A**Rational(1, 2)) raises(NotImplementedError, lambda: A**-1) raises(NotImplementedError, lambda: A.pow(-1))
def test_DomainMatrix_vstack(): A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) B = DomainMatrix([[ZZ(5), ZZ(6)], [ZZ(7), ZZ(8)]], (2, 2), ZZ) C = DomainMatrix([[ZZ(9), ZZ(10)], [ZZ(11), ZZ(12)]], (2, 2), ZZ) AB = DomainMatrix( [[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)], [ZZ(5), ZZ(6)], [ZZ(7), ZZ(8)]], (4, 2), ZZ) ABC = DomainMatrix( [[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)], [ZZ(5), ZZ(6)], [ZZ(7), ZZ(8)], [ZZ(9), ZZ(10)], [ZZ(11), ZZ(12)]], (6, 2), ZZ) assert A.vstack(B) == AB assert A.vstack(B, C) == ABC
def test_DomainMatrix_rowspace(): A = DomainMatrix( [[QQ(1), QQ(-1), QQ(1)], [QQ(2), QQ(-2), QQ(3)]], (2, 3), QQ) assert A.rowspace() == A Az = DomainMatrix( [[ZZ(1), ZZ(-1), ZZ(1)], [ZZ(2), ZZ(-2), ZZ(3)]], (2, 3), ZZ) raises(DMNotAField, lambda: Az.rowspace()) A = DomainMatrix( [[QQ(1), QQ(-1), QQ(1)], [QQ(2), QQ(-2), QQ(3)]], (2, 3), QQ, fmt='sparse') assert A.rowspace() == A
def test_DomainMatrix_charpoly(): A = DomainMatrix([], (0, 0), ZZ) assert A.charpoly() == [ZZ(1)] A = DomainMatrix([[1]], (1, 1), ZZ) assert A.charpoly() == [ZZ(1), ZZ(-1)] A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) assert A.charpoly() == [ZZ(1), ZZ(-5), ZZ(-2)] A = DomainMatrix([[ZZ(1), ZZ(2), ZZ(3)], [ZZ(4), ZZ(5), ZZ(6)], [ZZ(7), ZZ(8), ZZ(9)]], (3, 3), ZZ) assert A.charpoly() == [ZZ(1), ZZ(-15), ZZ(-18), ZZ(0)] Ans = DomainMatrix([[QQ(1), QQ(2)]], (1, 2), QQ) raises(NonSquareMatrixError, lambda: Ans.charpoly())
def test_dom_eigenvects_rational(): # Rational eigenvalues A = DomainMatrix([[QQ(1), QQ(2)], [QQ(1), QQ(2)]], (2, 2), QQ) rational_eigenvects = [ (QQ, QQ(3), 1, DomainMatrix([[QQ(1), QQ(1)]], (1, 2), QQ)), (QQ, QQ(0), 1, DomainMatrix([[QQ(-2), QQ(1)]], (1, 2), QQ)), ] assert dom_eigenvects(A) == (rational_eigenvects, []) # Test converting to Expr: sympy_eigenvects = [ (S(3), 1, [Matrix([1, 1])]), (S(0), 1, [Matrix([-2, 1])]), ] assert dom_eigenvects_to_sympy(rational_eigenvects, [], Matrix) == sympy_eigenvects
def test_DomainMatrix_to_dok(): A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) assert A.to_dok() == { (0, 0): ZZ(1), (0, 1): ZZ(2), (1, 0): ZZ(3), (1, 1): ZZ(4) }
def test_DomainMatrix_det(): A = DomainMatrix([], (0, 0), ZZ) assert A.det() == 1 A = DomainMatrix([[1]], (1, 1), ZZ) assert A.det() == 1 A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) assert A.det() == ZZ(-2) A = DomainMatrix( [[ZZ(1), ZZ(2), ZZ(3)], [ZZ(1), ZZ(2), ZZ(4)], [ZZ(1), ZZ(3), ZZ(5)]], (3, 3), ZZ) assert A.det() == ZZ(-1) A = DomainMatrix( [[ZZ(1), ZZ(2), ZZ(3)], [ZZ(1), ZZ(2), ZZ(4)], [ZZ(1), ZZ(2), ZZ(5)]], (3, 3), ZZ) assert A.det() == ZZ(0) Ans = DomainMatrix([[QQ(1), QQ(2)]], (1, 2), QQ) raises(DMNonSquareMatrixError, lambda: Ans.det()) A = DomainMatrix([[QQ(1), QQ(2)], [QQ(3), QQ(4)]], (2, 2), QQ) assert A.det() == QQ(-2)
def test_DomainMatrix_scc(): Ad = DomainMatrix( [[ZZ(1), ZZ(2), ZZ(3)], [ZZ(0), ZZ(1), ZZ(0)], [ZZ(2), ZZ(0), ZZ(4)]], (3, 3), ZZ) As = Ad.to_sparse() Addm = Ad.rep Asdm = As.rep for A in [Ad, As, Addm, Asdm]: assert Ad.scc() == [[1], [0, 2]]
def test_DomainScalar_mul(): A = DomainScalar(ZZ(1), ZZ) B = DomainScalar(QQ(2), QQ) dm = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) assert A * B == DomainScalar(QQ(2), QQ) assert A * dm == dm assert B * 2 == DomainScalar(QQ(4), QQ) raises(TypeError, lambda: A * 1.5)
def test_DomainMatrix_unify(): Az = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) Aq = DomainMatrix([[QQ(1), QQ(2)], [QQ(3), QQ(4)]], (2, 2), QQ) assert Az.unify(Az) == (Az, Az) assert Az.unify(Aq) == (Aq, Aq) assert Aq.unify(Az) == (Aq, Aq) assert Aq.unify(Aq) == (Aq, Aq) As = DomainMatrix({0: {1: ZZ(1)}, 1: {0: ZZ(2)}}, (2, 2), ZZ) Ad = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) assert As.unify(As) == (As, As) assert Ad.unify(Ad) == (Ad, Ad) Bs, Bd = As.unify(Ad, fmt='dense') assert Bs.rep == DDM([[0, 1], [2, 0]], (2, 2), ZZ) assert Bd.rep == DDM([[1, 2], [3, 4]], (2, 2), ZZ) Bs, Bd = As.unify(Ad, fmt='sparse') assert Bs.rep == SDM({0: {1: 1}, 1: {0: 2}}, (2, 2), ZZ) assert Bd.rep == SDM({0: {0: 1, 1: 2}, 1: {0: 3, 1: 4}}, (2, 2), ZZ) raises(ValueError, lambda: As.unify(Ad, fmt='invalid'))
def test_DomainMatrix_from_rep(): ddm = DDM([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) A = DomainMatrix.from_rep(ddm) assert A.rep == ddm assert A.shape == (2, 2) assert A.domain == ZZ sdm = SDM({0: {0: ZZ(1), 1: ZZ(2)}, 1: {0: ZZ(3), 1: ZZ(4)}}, (2, 2), ZZ) A = DomainMatrix.from_rep(sdm) assert A.rep == sdm assert A.shape == (2, 2) assert A.domain == ZZ A = DomainMatrix([[ZZ(1)]], (1, 1), ZZ) raises(TypeError, lambda: DomainMatrix.from_rep(A))
def test_DomainMatrix_solve(): # XXX: Maybe the _solve method should be changed... A = DomainMatrix([[QQ(1), QQ(2)], [QQ(2), QQ(4)]], (2, 2), QQ) b = DomainMatrix([[QQ(1)], [QQ(2)]], (2, 1), QQ) particular = DomainMatrix([[1, 0]], (1, 2), QQ) nullspace = DomainMatrix([[-2, 1]], (1, 2), QQ) assert A._solve(b) == (particular, nullspace) b3 = DomainMatrix([[QQ(1)], [QQ(1)], [QQ(1)]], (3, 1), QQ) raises(DMShapeError, lambda: A._solve(b3)) bz = DomainMatrix([[ZZ(1)], [ZZ(1)]], (2, 1), ZZ) raises(DMNotAField, lambda: A._solve(bz))
def test_DomainMatrix_inv(): A = DomainMatrix([], (0, 0), QQ) assert A.inv() == A A = DomainMatrix([[QQ(1), QQ(2)], [QQ(3), QQ(4)]], (2, 2), QQ) Ainv = DomainMatrix([[QQ(-2), QQ(1)], [QQ(3, 2), QQ(-1, 2)]], (2, 2), QQ) assert A.inv() == Ainv Az = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) raises(DMNotAField, lambda: Az.inv()) Ans = DomainMatrix([[QQ(1), QQ(2)]], (1, 2), QQ) raises(DMNonSquareMatrixError, lambda: Ans.inv()) Aninv = DomainMatrix([[QQ(1), QQ(2)], [QQ(3), QQ(6)]], (2, 2), QQ) raises(DMNonInvertibleMatrixError, lambda: Aninv.inv())
def test_DomainMatrix_add(): A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) B = DomainMatrix([[ZZ(2), ZZ(4)], [ZZ(6), ZZ(8)]], (2, 2), ZZ) assert A + A == A.add(A) == B A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) L = [[2, 3], [3, 4]] raises(TypeError, lambda: A + L) raises(TypeError, lambda: L + A) A1 = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) A2 = DomainMatrix([[ZZ(1), ZZ(2)]], (1, 2), ZZ) raises(ShapeError, lambda: A1 + A2) raises(ShapeError, lambda: A2 + A1) raises(ShapeError, lambda: A1.add(A2)) raises(ShapeError, lambda: A2.add(A1)) Az = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) Aq = DomainMatrix([[QQ(1), QQ(2)], [QQ(3), QQ(4)]], (2, 2), QQ) raises(ValueError, lambda: Az + Aq) raises(ValueError, lambda: Aq + Az) raises(ValueError, lambda: Az.add(Aq)) raises(ValueError, lambda: Aq.add(Az))
def test_DomainMatrix_sub(): A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) B = DomainMatrix([[ZZ(0), ZZ(0)], [ZZ(0), ZZ(0)]], (2, 2), ZZ) assert A - A == A.sub(A) == B A = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) L = [[2, 3], [3, 4]] raises(TypeError, lambda: A - L) raises(TypeError, lambda: L - A) A1 = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) A2 = DomainMatrix([[ZZ(1), ZZ(2)]], (1, 2), ZZ) raises(ShapeError, lambda: A1 - A2) raises(ShapeError, lambda: A2 - A1) raises(ShapeError, lambda: A1.sub(A2)) raises(ShapeError, lambda: A2.sub(A1)) Az = DomainMatrix([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) Aq = DomainMatrix([[QQ(1), QQ(2)], [QQ(3), QQ(4)]], (2, 2), QQ) raises(ValueError, lambda: Az - Aq) raises(ValueError, lambda: Aq - Az) raises(ValueError, lambda: Az.sub(Aq)) raises(ValueError, lambda: Aq.sub(Az))