def matrix_inverse(M, method='default'): from .config import matrix_inverse_method, matrix_inverse_fallback_method 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 test_DomainMatrix_from_list_sympy(): ddm = DDM([[ZZ(1), ZZ(2)], [ZZ(3), 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) 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 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_from_list_sympy(): ddm = DDM([[ZZ(1), ZZ(2)], [ZZ(3), 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
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)