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_issue_17247_expression_blowup_32(): M = Matrix([[x + 1, 1 - x, 0, 0], [1 - x, x + 1, 0, x + 1], [0, 1 - x, x + 1, 0], [0, 0, 0, x + 1]]) with dotprodsimp(True): assert M.LUsolve(ones(4, 1)) == Matrix([[(x + 1) / (4 * x)], [(x - 1) / (4 * x)], [(x + 1) / (4 * x)], [1 / (x + 1)]])
def __init__(self, symbols, m: sym.Matrix, to_base_point=None): self.gdd = m # covariant print("Inverting metric...") # necessary due to a regression in sympy with dotprodsimp(False): self.guu = m.inv() # contravariant print("Metric successfully inverted") self.symbols = symbols self.to_base_point = to_base_point
def test_issue_17247_expression_blowup_30(): M = Matrix( S('''[ [ -3/4, 45/32 - 37*I/16, 0, 0], [-149/64 + 49*I/32, -177/128 - 1369*I/128, 0, -2063/256 + 541*I/128], [ 0, 9/4 + 55*I/16, 2473/256 + 137*I/64, 0], [ 0, 0, 0, -177/128 - 1369*I/128]]''' )) with dotprodsimp(True): assert M.cholesky_solve(ones(4, 1)) == Matrix( S('''[ [ -32549314808672/3306971225785 - 17397006745216*I/3306971225785], [ 67439348256/3306971225785 - 9167503335872*I/3306971225785], [-15091965363354518272/21217636514687010905 + 16890163109293858304*I/21217636514687010905], [ -11328/952745 + 87616*I/952745]]''' ))
def test_apart_extension(): f = 2 / (x**2 + 1) g = I / (x + I) - I / (x - I) assert apart(f, extension=I) == g assert apart(f, gaussian=True) == g f = x / ((x - 2) * (x + I)) assert factor(together(apart(f)).expand()) == f f, g = _make_extension_example() # XXX: Only works with dotprodsimp. See test_apart_extension_xfail below from sympy.matrices import dotprodsimp with dotprodsimp(True): assert apart(f, x, extension={sqrt(2)}) == g
def test_eigen(): R = Rational M = Matrix.eye(3) assert M.eigenvals(multiple=False) == {S.One: 3} assert M.eigenvals(multiple=True) == [1, 1, 1] assert M.eigenvects() == ([ (1, 3, [Matrix([1, 0, 0]), Matrix([0, 1, 0]), Matrix([0, 0, 1])]) ]) assert M.left_eigenvects() == ([ (1, 3, [Matrix([[1, 0, 0]]), Matrix([[0, 1, 0]]), Matrix([[0, 0, 1]])]) ]) M = Matrix([[0, 1, 1], [1, 0, 0], [1, 1, 1]]) assert M.eigenvals() == {2 * S.One: 1, -S.One: 1, S.Zero: 1} assert M.eigenvects() == ([(-1, 1, [Matrix([-1, 1, 0])]), (0, 1, [Matrix([0, -1, 1])]), (2, 1, [Matrix([R(2, 3), R(1, 3), 1])])]) assert M.left_eigenvects() == ([(-1, 1, [Matrix([[-2, 1, 1]])]), (0, 1, [Matrix([[-1, -1, 1]])]), (2, 1, [Matrix([[1, 1, 1]])])]) a = Symbol('a') M = Matrix([[a, 0], [0, 1]]) assert M.eigenvals() == {a: 1, S.One: 1} M = Matrix([[1, -1], [1, 3]]) assert M.eigenvects() == ([(2, 2, [Matrix(2, 1, [-1, 1])])]) assert M.left_eigenvects() == ([(2, 2, [Matrix([[1, 1]])])]) M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) a = R(15, 2) b = 3 * 33**R(1, 2) c = R(13, 2) d = (R(33, 8) + 3 * b / 8) e = (R(33, 8) - 3 * b / 8) def NS(e, n): return str(N(e, n)) r = [ (a - b / 2, 1, [ Matrix([ (12 + 24 / (c - b / 2)) / ((c - b / 2) * e) + 3 / (c - b / 2), (6 + 12 / (c - b / 2)) / e, 1 ]) ]), (0, 1, [Matrix([1, -2, 1])]), (a + b / 2, 1, [ Matrix([ (12 + 24 / (c + b / 2)) / ((c + b / 2) * d) + 3 / (c + b / 2), (6 + 12 / (c + b / 2)) / d, 1 ]) ]), ] r1 = [(NS(r[i][0], 2), NS(r[i][1], 2), [NS(j, 2) for j in r[i][2][0]]) for i in range(len(r))] r = M.eigenvects() r2 = [(NS(r[i][0], 2), NS(r[i][1], 2), [NS(j, 2) for j in r[i][2][0]]) for i in range(len(r))] assert sorted(r1) == sorted(r2) eps = Symbol('eps', real=True) M = Matrix([[abs(eps), I * eps], [-I * eps, abs(eps)]]) assert M.eigenvects() == ([ (0, 1, [Matrix([[-I * eps / abs(eps)], [1]])]), (2 * abs(eps), 1, [Matrix([[I * eps / abs(eps)], [1]])]), ]) assert M.left_eigenvects() == ([ (0, 1, [Matrix([[I * eps / Abs(eps), 1]])]), (2 * Abs(eps), 1, [Matrix([[-I * eps / Abs(eps), 1]])]) ]) M = Matrix(3, 3, [1, 2, 0, 0, 3, 0, 2, -4, 2]) M._eigenvects = M.eigenvects(simplify=False) assert max(i.q for i in M._eigenvects[0][2][0]) > 1 M._eigenvects = M.eigenvects(simplify=True) assert max(i.q for i in M._eigenvects[0][2][0]) == 1 M = Matrix([[Rational(1, 4), 1], [1, 1]]) assert M.eigenvects(simplify=True) == [ (Rational(5, 8) - sqrt(73) / 8, 1, [Matrix([[-sqrt(73) / 8 - Rational(3, 8)], [1]])]), (Rational(5, 8) + sqrt(73) / 8, 1, [Matrix([[Rational(-3, 8) + sqrt(73) / 8], [1]])]) ] with dotprodsimp(True): assert M.eigenvects(simplify=False) == [ (Rational(5, 8) - sqrt(73) / 8, 1, [Matrix([[-1 / (-Rational(3, 8) + sqrt(73) / 8)], [1]])]), (Rational(5, 8) + sqrt(73) / 8, 1, [Matrix([[8 / (3 + sqrt(73))], [1]])]) ] # issue 10719 assert Matrix([]).eigenvals() == {} assert Matrix([]).eigenvals(multiple=True) == [] assert Matrix([]).eigenvects() == [] # issue 15119 raises(NonSquareMatrixError, lambda: Matrix([[1, 2], [0, 4], [0, 0]]).eigenvals()) raises(NonSquareMatrixError, lambda: Matrix([[1, 0], [3, 4], [5, 6]]).eigenvals()) raises(NonSquareMatrixError, lambda: Matrix([[1, 2, 3], [0, 5, 6]]).eigenvals()) raises(NonSquareMatrixError, lambda: Matrix([[1, 0, 0], [4, 5, 0]]).eigenvals()) raises( NonSquareMatrixError, lambda: Matrix([[1, 2, 3], [0, 5, 6]]).eigenvals( error_when_incomplete=False)) raises( NonSquareMatrixError, lambda: Matrix([[1, 0, 0], [4, 5, 0]]).eigenvals( error_when_incomplete=False)) m = Matrix([[1, 2], [3, 4]]) assert isinstance(m.eigenvals(simplify=True, multiple=False), dict) assert isinstance(m.eigenvals(simplify=True, multiple=True), list) assert isinstance(m.eigenvals(simplify=lambda x: x, multiple=False), dict) assert isinstance(m.eigenvals(simplify=lambda x: x, multiple=True), list)
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)