def test_integdom_matrix_determinants(self): """ >>> integdom_matrix_determinants() Matrix determinant test passed. """ error = False a = symbol('a') # Feel like you have CPU time to burn? # Raise the value of the upper bounds here... for size in range(3, 22): A = matrix( size, size) # Populate one element in each row for r in range( size-1): A[r, randrange(0,size)] = dense_univariate_poly(a, 5) # Set the last row to a linear combination of two others to # guarantee that the determinant should be zero for c in range(size): A[size-1, c] = A[0,c] - A[size-2,c] if A.determinant() != 0: error = True print "Determinant of", size, "x", size, "matrix", A print "was not found to vanish!" self.assertEqual(error,0)
def check_matrix_solve(m, n, p, degree): """ >>> # solve some numeric linear systems >>> result = 0 >>> for n in range(1, 14): ... result += check_matrix_solve(n, n, 1, 0) ... >>> if result: ... print "Numeric system checks failed." ... else: ... print "Numeric system checks passed." ... Numeric system checks passed. >>> # solve some underdetermined numeric systems >>> result = 0 >>> for n in range(1, 14): ... result += check_matrix_solve(n+1, n, 1, 0) ... >>> if result: ... print "Underdetermined system checks failed." ... else: ... print "Underdetermined system checks passed." ... Underdetermined system checks passed. >>> result = 0 >>> # solve some overdetermined numeric systems >>> for n in range(1, 14): ... result += check_matrix_solve(n, n+1, 1, 0) ... >>> if result: ... print "Overdetermined system checks failed." ... else: ... print "Overdetermined system checks passed." ... Overdetermined system checks passed. >>> # solve some multiple numeric systems >>> result = 0 >>> for n in range(1, 14): ... result += check_matrix_solve(n, n, n/3+1, 0) ... >>> if result: ... print "Multiple numeric systems checks failed." ... else: ... print "Multiple numeric systems checks passed." ... Multiple numeric systems checks passed. >>> # solve some symbolic linear systems >>> result = 0 >>> for n in range(1, 8): ... result += check_matrix_solve(n, n, 1, 2) ... >>> if result: ... print "Symbolic linear systems checks failed." ... else: ... print "Symbolic linear systems checks passed." ... Symbolic linear systems checks passed. """ a = symbol('a') A = matrix(m, n) B = matrix(m, p) for ro in range( min(m, n)): for co in range(n): A[ro,co] = dense_univariate_poly(a, degree) for co in range(p): B[ro,co] = dense_univariate_poly(a, degree) for ro in range(n, m): for co in range(n): A[ro, co] = A[ro-1,co] for co in range(p): B[ro,co] = B[ro-1,co] # Create a vector of n*p symbols all named "xrc" where r and c are ints X = matrix(n,p) for i in range(n): for j in range(p): X[i,j] = symbol("x" + str(i) + str(j)) soln = matrix(n, p) # Solve the system A*X == B sol = A.solve(X, B) # Check the result with the original matrix error = False for ro in range(m): for pco in range(p): e = numeric(0) for co in range(n): e += A[ro,co]*sol[co,pco] if (e-B[ro,pco]).normal() != 0: error = True if error: print "Our solve method claims that A*X == B with matricies:" print "A ==", A print "X ==", X print "B ==", B print "X (the solution) == ", sol return error