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]]) assert M.LUsolve(ones(4, 1)) == Matrix([[(x + 1) / (4 * x)], [(x - 1) / (4 * x)], [(x + 1) / (4 * x)], [1 / (x + 1)]])
def solve(A, b): """ linear equation system Ax = b -> x """ A = Matrix(A) b = Matrix(b) x = A.LUsolve(b) return x
def test_LUsolve(): A = Matrix([[2, 3, 5], [3, 6, 2], [8, 3, 6]]) x = Matrix(3, 1, [3, 7, 5]) b = A * x soln = A.LUsolve(b) assert soln == x A = Matrix([[0, -1, 2], [5, 10, 7], [8, 3, 4]]) x = Matrix(3, 1, [-1, 2, 5]) b = A * x soln = A.LUsolve(b) assert soln == x A = Matrix([[2, 1], [1, 0], [1, 0]]) # issue 14548 b = Matrix([3, 1, 1]) assert A.LUsolve(b) == Matrix([1, 1]) b = Matrix([3, 1, 2]) # inconsistent raises(ValueError, lambda: A.LUsolve(b)) A = Matrix([[0, -1, 2], [5, 10, 7], [8, 3, 4], [2, 3, 5], [3, 6, 2], [8, 3, 6]]) x = Matrix([2, 1, -4]) b = A * x soln = A.LUsolve(b) assert soln == x A = Matrix([[0, -1, 2], [5, 10, 7]]) # underdetermined x = Matrix([-1, 2, 0]) b = A * x raises(NotImplementedError, lambda: A.LUsolve(b)) A = Matrix(4, 4, lambda i, j: 1 / (i + j + 1) if i != 3 else 0) b = Matrix.zeros(4, 1) raises(NonInvertibleMatrixError, lambda: A.LUsolve(b))
def evalMat(Mlist, blist): M = Matrix(Mlist) b = Matrix(blist) c = M.LUsolve(b) print print " ================= Case: ", b.transpose() print print c return c
def analytical_coefs_sympy(): import sympy from sympy.matrices import Matrix x0, x1, y0, y1, dy0, dy1, x = sympy.symbols('x0 x1 y0 y1 dy0 dy1 x') #a1,a2,a3,a4 = sympy.symbols('a0 a1 a2 a3') # ===== setup bas1 = 1 + 0 * x bas2 = x**2 bas3 = x**4 bas4 = x**6 # ===== Main dbas1 = sympy.diff(bas1, x) dbas2 = sympy.diff(bas2, x) dbas3 = sympy.diff(bas3, x) dbas4 = sympy.diff(bas4, x) #print ( " dbas1: ", dbas1); print ( " dbas2: ", dbas2); print ( " dbas3: ", dbas3); print ( " dbas4: ", dbas4) A = Matrix([[ bas1.subs(x, x0), bas2.subs(x, x0), bas3.subs(x, x0), bas4.subs(x, x0) ], [ bas1.subs(x, x1), bas2.subs(x, x1), bas3.subs(x, x1), bas4.subs(x, x1) ], [ dbas1.subs(x, x0), dbas2.subs(x, x0), dbas3.subs(x, x0), dbas4.subs(x, x0) ], [ dbas1.subs(x, x1), dbas2.subs(x, x1), dbas3.subs(x, x1), dbas4.subs(x, x1) ]]) system = Matrix(4, 1, [y0, y1, dy0, dy1]) print("Solving matrix ...") coefs = A.LUsolve(system) print("Symplyfying ...") for i, coef in enumerate(coefs): coef = coef.factor() coef = coef.collect([x0, x1]) #print ( " coef %i: " %i, coef ) coef_subs = sympy.cse(coef) print(" coef %i: " % i, coef_subs)
def find_linear_recurrence(self, n, d=None, gfvar=None): r""" Finds the shortest linear recurrence that satisfies the first n terms of sequence of order `\leq` ``n/2`` if possible. If ``d`` is specified, find shortest linear recurrence of order `\leq` min(d, n/2) if possible. Returns list of coefficients ``[b(1), b(2), ...]`` corresponding to the recurrence relation ``x(n) = b(1)*x(n-1) + b(2)*x(n-2) + ...`` Returns ``[]`` if no recurrence is found. If gfvar is specified, also returns ordinary generating function as a function of gfvar. Examples ======== >>> from sympy import sequence, sqrt, oo, lucas >>> from sympy.abc import n, x, y >>> sequence(n**2).find_linear_recurrence(10, 2) [] >>> sequence(n**2).find_linear_recurrence(10) [3, -3, 1] >>> sequence(2**n).find_linear_recurrence(10) [2] >>> sequence(23*n**4+91*n**2).find_linear_recurrence(10) [5, -10, 10, -5, 1] >>> sequence(sqrt(5)*(((1 + sqrt(5))/2)**n - (-(1 + sqrt(5))/2)**(-n))/5).find_linear_recurrence(10) [1, 1] >>> sequence(x+y*(-2)**(-n), (n, 0, oo)).find_linear_recurrence(30) [1/2, 1/2] >>> sequence(3*5**n + 12).find_linear_recurrence(20,gfvar=x) ([6, -5], 3*(5 - 21*x)/((x - 1)*(5*x - 1))) >>> sequence(lucas(n)).find_linear_recurrence(15,gfvar=x) ([1, 1], (x - 2)/(x**2 + x - 1)) """ from sympy.matrices import Matrix x = [simplify(expand(t)) for t in self[:n]] lx = len(x) if d is None: r = lx // 2 else: r = min(d, lx // 2) coeffs = [] for l in range(1, r + 1): l2 = 2 * l mlist = [] for k in range(l): mlist.append(x[k:k + l]) m = Matrix(mlist) if m.det() != 0: y = simplify(m.LUsolve(Matrix(x[l:l2]))) if lx == l2: coeffs = flatten(y[::-1]) break mlist = [] for k in range(l, lx - l): mlist.append(x[k:k + l]) m = Matrix(mlist) if m * y == Matrix(x[l2:]): coeffs = flatten(y[::-1]) break if gfvar is None: return coeffs else: l = len(coeffs) if l == 0: return [], None else: n, d = x[l - 1] * gfvar**(l - 1), 1 - coeffs[l - 1] * gfvar**l for i in range(l - 1): n += x[i] * gfvar**i for j in range(l - i - 1): n -= coeffs[i] * x[j] * gfvar**(i + j + 1) d -= coeffs[i] * gfvar**(i + 1) return coeffs, simplify(factor(n) / factor(d))
print print " ==== Solution of 6th order polygon coeficients ==== " p0, p1, v0, v1, a0, a1, t = symbols('p0 p1 v0 v1 a0 a1 t') " 1 t t2 t3 t4 t5 " A = Matrix([ [1, 0, 0, 0, 0, 0], # p0 [1, 1, 1, 1, 1, 1], # p1 [0, 1, 0, 0, 0, 0], # v0 [0, 1, 2, 3, 4, 5], # v1 [0, 0, 2, 0, 0, 0], # a0 [0, 0, 2, 6, 12, 20] ]) # a1 b = Matrix(6, 1, [p0, p1, v0, v1, a0, a1]) CP = A.LUsolve(b) print "Coefficients " print CP print print " ==== Make A4 A5 zero ==== " A = Matrix([[1.5, -1.0], [-0.5, 0.5]]) b = Matrix(2, 1, [ -(15 * p0 - 15 * p1 + 8 * v0 + 7 * v1), -(-6 * p0 + 6 * p1 - 3 * v0 - 3 * v1) ]) c = A.LUsolve(b) print " Coefficients: " print c