def test_DomainMatrix_from_Matrix(): sdm = SDM({0: {0: ZZ(1), 1: ZZ(2)}, 1: {0: ZZ(3), 1: ZZ(4)}}, (2, 2), ZZ) A = DomainMatrix.from_Matrix(Matrix([[1, 2], [3, 4]])) assert A.rep == sdm assert A.shape == (2, 2) assert A.domain == ZZ K = QQ.algebraic_field(sqrt(2)) sdm = SDM( { 0: { 0: K.convert(1 + sqrt(2)), 1: K.convert(2 + sqrt(2)) }, 1: { 0: K.convert(3 + sqrt(2)), 1: K.convert(4 + sqrt(2)) } }, (2, 2), K) A = DomainMatrix.from_Matrix(Matrix([[1 + sqrt(2), 2 + sqrt(2)], [3 + sqrt(2), 4 + sqrt(2)]]), extension=True) assert A.rep == sdm assert A.shape == (2, 2) assert A.domain == K A = DomainMatrix.from_Matrix(Matrix([[QQ(1, 2), QQ(3, 4)], [QQ(0, 1), QQ(0, 1)]]), fmt='dense') ddm = DDM([[QQ(1, 2), QQ(3, 4)], [QQ(0, 1), QQ(0, 1)]], (2, 2), QQ) assert A.rep == ddm assert A.shape == (2, 2) assert A.domain == QQ
def test_SDM_sub(): A = SDM({0: {1: ZZ(1)}, 1: {0: ZZ(2), 1: ZZ(3)}}, (2, 2), ZZ) B = SDM({0: {0: ZZ(1)}, 1: {0: ZZ(-2), 1: ZZ(3)}}, (2, 2), ZZ) C = SDM({0: {0: ZZ(-1), 1: ZZ(1)}, 1: {0: ZZ(4)}}, (2, 2), ZZ) assert A.sub(B) == A - B == C raises(TypeError, lambda: A - [])
def test_SDM(): A = SDM({0: {0: ZZ(1)}}, (2, 2), ZZ) assert A.domain == ZZ assert A.shape == (2, 2) assert dict(A) == {0: {0: ZZ(1)}} raises(DDMBadInputError, lambda: SDM({5: {1: ZZ(0)}}, (2, 2), ZZ)) raises(DDMBadInputError, lambda: SDM({0: {5: ZZ(0)}}, (2, 2), ZZ))
def test_SDM_lu(): A = SDM({0: {0: QQ(1), 1: QQ(2)}, 1: {0: QQ(3), 1: QQ(4)}}, (2, 2), QQ) L = SDM({0: {0: QQ(1)}, 1: {0: QQ(3), 1: QQ(1)}}, (2, 2), QQ) #U = SDM({0:{0:QQ(1), 1:QQ(2)}, 1:{0:QQ(3), 1:QQ(-2)}}, (2, 2), QQ) #swaps = [] # This doesn't quite work. U has some nonzero elements in the lower part. #assert A.lu() == (L, U, swaps) assert A.lu()[0] == L
def test_SDM_from_list(): A = SDM.from_list([[ZZ(0), ZZ(1)], [ZZ(1), ZZ(0)]], (2, 2), ZZ) assert A == SDM({0: {1: ZZ(1)}, 1: {0: ZZ(1)}}, (2, 2), ZZ) raises(DDMBadInputError, lambda: SDM.from_list([[ZZ(0)], [ZZ(0), ZZ(1)]], (2, 2), ZZ)) raises(DDMBadInputError, lambda: SDM.from_list([[ZZ(0), ZZ(1)]], (2, 2), ZZ))
def test_SDM_mul(): A = SDM({0: {0: ZZ(2)}}, (2, 2), ZZ) B = SDM({0: {0: ZZ(4)}}, (2, 2), ZZ) assert A * ZZ(2) == B assert ZZ(2) * A == B raises(TypeError, lambda: A * QQ(1, 2)) raises(TypeError, lambda: QQ(1, 2) * A)
def test_SDM_convert_to(): A = SDM({0: {1: ZZ(1)}, 1: {0: ZZ(2), 1: ZZ(3)}}, (2, 2), ZZ) B = SDM({0: {1: QQ(1)}, 1: {0: QQ(2), 1: QQ(3)}}, (2, 2), QQ) C = A.convert_to(QQ) assert C == B assert C.domain == QQ D = A.convert_to(ZZ) assert D == A assert D.domain == ZZ
def test_SDM_matmul(): A = SDM({0: {0: ZZ(2)}}, (2, 2), ZZ) B = SDM({0: {0: ZZ(4)}}, (2, 2), ZZ) assert A.matmul(A) == B C = SDM({0: {0: ZZ(2)}}, (2, 2), QQ) raises(DDMDomainError, lambda: A.matmul(C)) A = SDM({0: {0: ZZ(1), 1: ZZ(2)}, 1: {0: ZZ(3), 1: ZZ(4)}}, (2, 2), ZZ) B = SDM({0: {0: ZZ(7), 1: ZZ(10)}, 1: {0: ZZ(15), 1: ZZ(22)}}, (2, 2), ZZ) assert A.matmul(A) == B
def test_SDM_to_list(): A = SDM({0: {1: ZZ(1)}}, (2, 2), ZZ) assert A.to_list() == [[ZZ(0), ZZ(1)], [ZZ(0), ZZ(0)]] A = SDM({}, (0, 2), ZZ) assert A.to_list() == [] A = SDM({}, (2, 0), ZZ) assert A.to_list() == [[], []]
def test_SDM_inv(): A = SDM({0: {0: QQ(1), 1: QQ(2)}, 1: {0: QQ(3), 1: QQ(4)}}, (2, 2), QQ) B = SDM({ 0: { 0: QQ(-2), 1: QQ(1) }, 1: { 0: QQ(3, 2), 1: QQ(-1, 2) } }, (2, 2), QQ) assert A.inv() == B
def dm(d): result = {} for i, row in d.items(): row = {j: val for j, val in row.items() if val} if row: result[i] = row return SDM(result, (2, 2), EXRAW)
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_from_rep(): # ddm = DDM([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) ddm = SDM({0: {0: ZZ(1), 1: ZZ(2)}, 1: {0: ZZ(3), 1: ZZ(4)}}, (2, 2), ZZ) A = DomainMatrix.from_rep(ddm) assert A.rep == ddm assert A.shape == (2, 2) assert A.domain == ZZ
def test_DDM_str(): sdm = SDM({0: {0: ZZ(1)}, 1: {1: ZZ(1)}}, (2, 2), ZZ) assert str(sdm) == '{0: {0: 1}, 1: {1: 1}}' if HAS_GMPY: # pragma: no cover assert repr(sdm) == 'SDM({0: {0: mpz(1)}, 1: {1: mpz(1)}}, (2, 2), ZZ)' else: # pragma: no cover assert repr(sdm) == 'SDM({0: {0: 1}, 1: {1: 1}}, (2, 2), ZZ)'
def test_SDM_vstack(): A = SDM({0: {1: ZZ(1)}}, (2, 2), ZZ) B = SDM({1: {1: ZZ(1)}}, (2, 2), ZZ) AA = SDM({0: {1: ZZ(1)}, 2: {1: ZZ(1)}}, (4, 2), ZZ) AB = SDM({0: {1: ZZ(1)}, 3: {1: ZZ(1)}}, (4, 2), ZZ) assert SDM.vstack(A) == A assert SDM.vstack(A, A) == AA assert SDM.vstack(A, B) == AB
def test_SDM_hstack(): A = SDM({0: {1: ZZ(1)}}, (2, 2), ZZ) B = SDM({1: {1: ZZ(1)}}, (2, 2), ZZ) AA = SDM({0: {1: ZZ(1), 3: ZZ(1)}}, (2, 4), ZZ) AB = SDM({0: {1: ZZ(1)}, 1: {3: ZZ(1)}}, (2, 4), ZZ) assert SDM.hstack(A) == A assert SDM.hstack(A, A) == AA assert SDM.hstack(A, B) == AB
def test_DomainMatrix_from_Matrix(): ddm = DDM([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) ddm = SDM.from_ddm(ddm) A = DomainMatrix.from_Matrix(Matrix([[1, 2], [3, 4]])) assert A.rep == ddm assert A.shape == (2, 2) assert A.domain == ZZ K = QQ.algebraic_field(sqrt(2)) ddm = DDM([[ K.convert(1 + sqrt(2)), K.convert(2 + sqrt(2)) ], [K.convert(3 + sqrt(2)), K.convert(4 + sqrt(2))]], (2, 2), K) ddm = SDM.from_ddm(ddm) A = DomainMatrix.from_Matrix(Matrix([[1 + sqrt(2), 2 + sqrt(2)], [3 + sqrt(2), 4 + sqrt(2)]]), extension=True) assert A.rep == ddm assert A.shape == (2, 2) assert A.domain == K
def test_DomainMatrix_from_list_sympy(): # ddm = DDM([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) ddm = SDM({0: {0: ZZ(1), 1: ZZ(2)}, 1: {0: ZZ(3), 1: ZZ(4)}}, (2, 2), ZZ) A = DomainMatrix.from_list_sympy(2, 2, [[1, 2], [3, 4]]) assert A.rep == ddm assert A.shape == (2, 2) assert A.domain == ZZ K = QQ.algebraic_field(sqrt(2)) ddm = DDM([[ K.convert(1 + sqrt(2)), K.convert(2 + sqrt(2)) ], [K.convert(3 + sqrt(2)), K.convert(4 + sqrt(2))]], (2, 2), K) ddm = SDM.from_ddm(ddm) A = DomainMatrix.from_list_sympy( 2, 2, [[1 + sqrt(2), 2 + sqrt(2)], [3 + sqrt(2), 4 + sqrt(2)]], extension=True) assert A.rep == ddm assert A.shape == (2, 2) assert A.domain == K
def test_DomainMatrix_from_dict_sympy(): sdm = SDM({0: {0: QQ(1, 2)}, 1: {1: QQ(2, 3)}}, (2, 2), QQ) sympy_dict = {0: {0: Rational(1, 2)}, 1: {1: Rational(2, 3)}} A = DomainMatrix.from_dict_sympy(2, 2, sympy_dict) assert A.rep == sdm assert A.shape == (2, 2) assert A.domain == QQ fds = DomainMatrix.from_dict_sympy raises(DMBadInputError, lambda: fds(2, 2, {3: {0: Rational(1, 2)}})) raises(DMBadInputError, lambda: fds(2, 2, {0: {3: Rational(1, 2)}}))
def test_SDM_getitem(): A = SDM({0: {1: ZZ(1)}}, (2, 2), ZZ) assert A.getitem(0, 0) == ZZ.zero assert A.getitem(0, 1) == ZZ.one assert A.getitem(1, 0) == ZZ.zero assert A.getitem(-2, -2) == ZZ.zero assert A.getitem(-2, -1) == ZZ.one assert A.getitem(-1, -2) == ZZ.zero raises(IndexError, lambda: A.getitem(2, 0)) raises(IndexError, lambda: A.getitem(0, 2))
def test_DomainMatrix_from_dict_sympy(): sdm = SDM({0: {0: QQ(1, 2)}, 1: {1: QQ(2, 3)}}, (2, 2), QQ) A = DomainMatrix.from_dict_sympy(2, 2, { 0: { 0: QQ(1, 2) }, 1: { 1: QQ(2, 3) } }) assert A.rep == sdm assert A.shape == (2, 2) assert A.domain == QQ
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_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.rep == SDM({ 0: { 0: ZZ(1), 1: ZZ(2) }, 1: { 0: ZZ(3), 1: 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_SDM_transpose(): A = SDM({0: {0: ZZ(1), 1: ZZ(2)}, 1: {0: ZZ(3), 1: ZZ(4)}}, (2, 2), ZZ) B = SDM({0: {0: ZZ(1), 1: ZZ(3)}, 1: {0: ZZ(2), 1: ZZ(4)}}, (2, 2), ZZ) assert A.transpose() == B A = SDM({0: {1: ZZ(2)}}, (2, 2), ZZ) B = SDM({1: {0: ZZ(2)}}, (2, 2), ZZ) assert A.transpose() == B A = SDM({0: {1: ZZ(2)}}, (1, 2), ZZ) B = SDM({1: {0: ZZ(2)}}, (2, 1), ZZ) assert A.transpose() == B
def test_SDM_eye(): A = SDM.eye(2, ZZ) assert A.domain == ZZ assert A.shape == (2, 2) assert dict(A) == {0: {0: ZZ(1)}, 1: {1: ZZ(1)}}
def test_SDM_zeros(): A = SDM.zeros((2, 2), ZZ) assert A.domain == ZZ assert A.shape == (2, 2) assert dict(A) == {}
def test_SDM_to_ddm(): A = SDM({0: {1: ZZ(1)}}, (2, 2), ZZ) B = DDM([[ZZ(0), ZZ(1)], [ZZ(0), ZZ(0)]], (2, 2), ZZ) assert A.to_ddm() == B
def test_SDM_from_ddm(): A = DDM([[ZZ(1), ZZ(0)], [ZZ(1), ZZ(0)]], (2, 2), ZZ) B = SDM.from_ddm(A) assert B.domain == ZZ assert B.shape == (2, 2) assert dict(B) == {0: {0: ZZ(1)}, 1: {0: ZZ(1)}}
def test_SDM_copy(): A = SDM({0: {0: ZZ(1)}}, (2, 2), ZZ) B = A.copy() assert A == B A[0][0] = ZZ(2) assert A != B