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_DomainMatrix_from_Matrix(): ddm = DDM([[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]], (2, 2), ZZ) 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) 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_FiniteExtension_sincos_jacobian(): # Use FiniteExtensino to compute the Jacobian of a matrix involving sin # and cos of different symbols. r, p, t = symbols('rho, phi, theta') elements = [ [sin(p)*cos(t), r*cos(p)*cos(t), -r*sin(p)*sin(t)], [sin(p)*sin(t), r*cos(p)*sin(t), r*sin(p)*cos(t)], [ cos(p), -r*sin(p), 0], ] def make_extension(K): K = FiniteExtension(Poly(sin(p)**2+cos(p)**2-1, sin(p), domain=K[cos(p)])) K = FiniteExtension(Poly(sin(t)**2+cos(t)**2-1, sin(t), domain=K[cos(t)])) return K Ksc1 = make_extension(ZZ[r]) Ksc2 = make_extension(ZZ)[r] for K in [Ksc1, Ksc2]: elements_K = [[K.convert(e) for e in row] for row in elements] J = DomainMatrix(elements_K, (3, 3), K) det = J.charpoly()[-1] * (-K.one)**3 assert det == K.convert(r**2*sin(p))
def matrix_inverse(M, method='default'): from .config import matrix_inverse_method if method == 'default': method = matrix_inverse_method if method == 'new': try: from sympy.polys.domainmatrix import DomainMatrix dM = DomainMatrix.from_list_sympy(*M.shape, rows=M.tolist()) return dM.inv().to_Matrix() except (ImportError, ValueError): method = 'ADJ' return M.inv(method=method)
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_DomainMatrix_eye(): A = DomainMatrix.eye(3, QQ) assert A.rep == DDM.eye(3, QQ) assert A.shape == (3, 3) assert A.domain == QQ
def matrix_inverse(M, method='default'): from .config import matrix_inverse_method, matrix_inverse_fallback_method N = M.shape[0] if N >= 10: warn(""" This may take a while... A symbolic matrix inversion is O(%d^3) for a matrix of size %dx%d""" % (N, N, N)) if method == 'default': method = matrix_inverse_method if method == 'GE': try: from sympy.matrices import dotprodsimp # GE loses it without this assumption. Well with # sympy-1.6.2 and the master version, GE still loses it # with a poor pivot. with dotprodsimp(False): return M.inv(method='GE') except: return M.inv(method='GE') elif method.startswith('DM-'): try: # This is experimental and requires sympy to be built from # git. It only works for rational function fields but # fails for polynomial rings. The latter can be handled # by converting it to a field, however, we just fall back # on a standard method. from sympy.polys.domainmatrix import DomainMatrix dM = DomainMatrix.from_list_sympy(*M.shape, rows=M.tolist()) return dM.inv(method=method[3:]).to_Matrix() except: method = matrix_inverse_fallback_method return M.inv(method=method) def canonical(self): return self.applyfunc(self._typewrap.canonical) def general(self): return self.applyfunc(self._typewrap.general) def mixedfrac(self): return self.applyfunc(self._typewrap.mixedfrac) def partfrac(self): return self.applyfunc(self._typewrap.partfrac) def timeconst(self): return self.applyfunc(self._typewrap.timeconst) def ZPK(self): return self.applyfunc(self._typewrap.ZPK)