def test_svd_test_case(): # a test case from Golub and Reinsch # (see wilkinson/reinsch: handbook for auto. comp., vol ii-linear algebra, 134-151(1971).) eps = mp.exp(0.8 * mp.log(mp.eps)) a = [[22, 10, 2, 3, 7], [14, 7, 10, 0, 8], [-1, 13, -1, -11, 3], [-3, -2, 13, -2, 4], [ 9, 8, 1, -2, 4], [ 9, 1, -7, 5, -1], [ 2, -6, 6, 5, 1], [ 4, 5, 0, -2, 2]] a = mp.matrix(a) b = mp.matrix([mp.sqrt(1248), 20, mp.sqrt(384), 0, 0]) S = mp.svd_r(a, compute_uv = False) S -= b assert mp.mnorm(S) < eps S = mp.svd_c(a, compute_uv = False) S -= b assert mp.mnorm(S) < eps
def run_gauss(qtype, a, b): eps = 1e-5 d, e = mp.gauss_quadrature(len(a), qtype) d -= mp.matrix(a) e -= mp.matrix(b) assert mp.mnorm(d) < eps assert mp.mnorm(e) < eps
def test_eighe_fixed_matrix(): A = mp.matrix([[2, 3], [3, 5]]) run_eigsy(A) run_eighe(A) A = mp.matrix([[7, -11], [-11, 13]]) run_eigsy(A) run_eighe(A) A = mp.matrix([[2, 11, 7], [11, 3, 13], [7, 13, 5]]) run_eigsy(A) run_eighe(A) A = mp.matrix([[2, 0, 7], [0, 3, 1], [7, 1, 5]]) run_eigsy(A) run_eighe(A) # A = mp.matrix([[2, 3 + 7j], [3 - 7j, 5]]) run_eighe(A) A = mp.matrix([[2, -11j, 0], [+11j, 3, 29j], [0, -29j, 5]]) run_eighe(A) A = mp.matrix([[2, 11 + 17j, 7 + 19j], [11 - 17j, 3, -13 + 23j], [7 - 19j, -13 - 23j, 5]]) run_eighe(A)
def test_eighe_fixed_matrix(): A = mp.matrix([[2, 3], [3, 5]]) run_eigsy(A) run_eighe(A) A = mp.matrix([[7, -11], [-11, 13]]) run_eigsy(A) run_eighe(A) A = mp.matrix([[2, 11, 7], [11, 3, 13], [7, 13, 5]]) run_eigsy(A) run_eighe(A) A = mp.matrix([[2, 0, 7], [0, 3, 1], [7, 1, 5]]) run_eigsy(A) run_eighe(A) # A = mp.matrix([[2, 3+7j], [3-7j, 5]]) run_eighe(A) A = mp.matrix([[2, -11j, 0], [+11j, 3, 29j], [0, -29j, 5]]) run_eighe(A) A = mp.matrix([[2, 11 + 17j, 7 + 19j], [11 - 17j, 3, -13 + 23j], [7 - 19j, -13 - 23j, 5]]) run_eighe(A)
def irandmatrix(n, range=10): """ random matrix with integer entries """ A = mp.matrix(n, n) for i in xrange(n): for j in xrange(n): A[i, j] = int((2 * mp.rand() - 1) * range) return A
def irandmatrix(n, range = 10): """ random matrix with integer entries """ A = mp.matrix(n, n) for i in xrange(n): for j in xrange(n): A[i,j]=int( (2 * mp.rand() - 1) * range) return A
def test_svd_test_case(): # a test case from Golub and Reinsch # (see wilkinson/reinsch: handbook for auto. comp., vol ii-linear algebra, 134-151(1971).) eps = mp.exp(0.8 * mp.log(mp.eps)) a = [[22, 10, 2, 3, 7], [14, 7, 10, 0, 8], [-1, 13, -1, -11, 3], [-3, -2, 13, -2, 4], [9, 8, 1, -2, 4], [9, 1, -7, 5, -1], [2, -6, 6, 5, 1], [4, 5, 0, -2, 2]] a = mp.matrix(a) b = mp.matrix([mp.sqrt(1248), 20, mp.sqrt(384), 0, 0]) S = mp.svd_r(a, compute_uv=False) S -= b assert mp.mnorm(S) < eps S = mp.svd_c(a, compute_uv=False) S -= b assert mp.mnorm(S) < eps
def test_eig(): v = 0 AS = [] A = mp.matrix([ [2, 1, 0], # jordan block of size 3 [0, 2, 1], [0, 0, 2] ]) AS.append(A) AS.append(A.transpose()) A = mp.matrix([ [2, 0, 0], # jordan block of size 2 [0, 2, 1], [0, 0, 2] ]) AS.append(A) AS.append(A.transpose()) A = mp.matrix([ [2, 0, 1], # jordan block of size 2 [0, 2, 0], [0, 0, 2] ]) AS.append(A) AS.append(A.transpose()) A = mp.matrix([ [0, 0, 1], # cyclic [1, 0, 0], [0, 1, 0] ]) AS.append(A) AS.append(A.transpose()) for A in AS: run_hessenberg(A, verbose=v) run_schur(A, verbose=v) run_eig(A, verbose=v)
def _nodal_basis(self, pts, ptsord): # Obtain an orthonormal basis ob = self._orthonormal_basis(ptsord) p, q = self._dims # Evaluate each basis function at each point V = self._eval_lbasis_at(lambdify_mpf(self._dims, ob), pts) # Invert this matrix to obtain the expansion coefficients Vinv = mp.matrix(V.T)**-1 # Each nodal basis function is a linear combination of # orthonormal basis functions nb = [sum(c*b for c, b in zip(Vinv[i,:], ob)) for i in xrange(len(pts))] return np.array(nb, dtype=np.object)
subroutine = '%s_quadrature_' % q with open('%s.hpp' % q, 'w') as f: f.write('#ifndef %s_HPP\n' % q.upper()) f.write('#define %s_HPP\n\n' % q.upper()) f.write('// this file was generated by "mksmat.py"\n') f.write('#include<cassert>\n\n') f.write('namespace detail{\n\n') f.write(' template<int sdcnodes>\n') f.write(' inline long double %smatrix(unsigned i, unsigned j);\n\n' % subroutine) f.write(' template<int sdcnodes>\n') f.write(' inline long double %snode(unsigned i);\n\n' % subroutine) f.write(' template<int sdcnodes>\n') f.write(' inline long double %sdt(unsigned i);\n\n' % subroutine) for n in quads[q]: dt = mp.matrix(1, n-1) for r in quads[q][n]: f.write(' template<>\n') f.writelines([' inline long double %smatrix<%d>(unsigned i, unsigned j)\n {\n' % (subroutine, n), ' static long double integration_matrix[%d][%d] = \n {\n' % (n-1, n) ]) (nodes, smat) = quads[q][n][r] rows, cols = smat.shape for row in range(rows): dt[row] = sum(smat[row]) f.write(' {') for col in range(cols-1): f.write('%sL,' % str(smat[row][col])) f.write('%sL}' % str(smat[row][cols-1])) if row == rows-1: f.write('\n };\n')