def test_gauss_quadrature_dynamic(verbose = False): n = 5 A = mp.randmatrix(2 * n, 1) def F(x): r = 0 for i in xrange(len(A) - 1, -1, -1): r = r * x + A[i] return r def run(qtype, FW, R, alpha = 0, beta = 0): X, W = mp.gauss_quadrature(n, qtype, alpha = alpha, beta = beta) a = 0 for i in xrange(len(X)): a += W[i] * F(X[i]) b = mp.quad(lambda x: FW(x) * F(x), R) c = mp.fabs(a - b) if verbose: print(qtype, c, a, b) assert c < 1e-5 run("legendre", lambda x: 1, [-1, 1]) run("legendre01", lambda x: 1, [0, 1]) run("hermite", lambda x: mp.exp(-x*x), [-mp.inf, mp.inf]) run("laguerre", lambda x: mp.exp(-x), [0, mp.inf]) run("glaguerre", lambda x: mp.sqrt(x)*mp.exp(-x), [0, mp.inf], alpha = 1 / mp.mpf(2)) run("chebyshev1", lambda x: 1/mp.sqrt(1-x*x), [-1, 1]) run("chebyshev2", lambda x: mp.sqrt(1-x*x), [-1, 1]) run("jacobi", lambda x: (1-x)**(1/mp.mpf(3)) * (1+x)**(1/mp.mpf(5)), [-1, 1], alpha = 1 / mp.mpf(3), beta = 1 / mp.mpf(5) )
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 norm_fpts(self): nfpts = np.empty((3, self._order + 1, 2), dtype=np.object) nfpts[0,:,:] = (0, -1) nfpts[1,:,:] = (1/mp.sqrt(2), 1/mp.sqrt(2)) nfpts[2,:,:] = (-1, 0) return nfpts.reshape(-1, 2)
def test_levin_3(): mp.dps = 17 z = mp.mpf(2) eps = mp.mpf(mp.eps) with mp.extraprec( 7 * mp.prec ): # we need copious amount of precision to sum this highly divergent series L = mp.levin(method="levin", variant="t") n, s = 0, 0 while 1: s += (-z)**n * mp.fac(4 * n) / (mp.fac(n) * mp.fac(2 * n) * (4**n)) n += 1 v, e = L.step_psum(s) if e < eps: break if n > 1000: raise RuntimeError("iteration limit exceeded") eps = mp.exp(0.8 * mp.log(eps)) exact = mp.quad(lambda x: mp.exp(-x * x / 2 - z * x**4), [0, mp.inf]) * 2 / mp.sqrt(2 * mp.pi) # there is also a symbolic expression for the integral: # exact = mp.exp(mp.one / (32 * z)) * mp.besselk(mp.one / 4, mp.one / (32 * z)) / (4 * mp.sqrt(z * mp.pi)) err = abs(v - exact) assert err < eps w = mp.nsum(lambda n: (-z)**n * mp.fac(4 * n) / (mp.fac(n) * mp.fac(2 * n) * (4**n)), [0, mp.inf], method="levin", levin_variant="t", workprec=8 * mp.prec, steps=[2] + [1 for x in xrange(1000)]) err = abs(v - w) assert err < eps
def fbasis(self): # Dummy parametric symbol t = sy.Symbol('t') # Dimension variables p, q = self._dims # Orthonormal basis obasis = self._orthonormal_basis(self._order + 1) # Nodal basis along an edge qrule = self._cfg.get('solver-interfaces-line', 'flux-pts') pts1d = get_quadrule(BaseLineQuadRule, qrule, self._order + 1).points nb1d = lagrange_basis(pts1d, t) # Allocate space for the flux point basis fbasis = np.empty((3, len(nb1d)), dtype=np.object) # Parametric mappings (p,q) -> t for the three edges # (bottom, hypot, left) substs = [{p: t, q: -1}, {p: -t, q: t}, {p: -1, q: -t}] for i, esub in enumerate(substs): for j, lj in enumerate(nb1d): fb = sum(sy.integrate(lj*ob.subs(esub), (t, -1, 1))*ob for ob in obasis) fbasis[i,j] = fb # Account for the longer length of the hypotenuse fbasis[1,:] *= mp.sqrt(2) return fbasis.ravel()
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_gauss_quadrature_dynamic(verbose=False): n = 5 A = mp.randmatrix(2 * n, 1) def F(x): r = 0 for i in xrange(len(A) - 1, -1, -1): r = r * x + A[i] return r def run(qtype, FW, R, alpha=0, beta=0): X, W = mp.gauss_quadrature(n, qtype, alpha=alpha, beta=beta) a = 0 for i in xrange(len(X)): a += W[i] * F(X[i]) b = mp.quad(lambda x: FW(x) * F(x), R) c = mp.fabs(a - b) if verbose: print(qtype, c, a, b) assert c < 1e-5 run("legendre", lambda x: 1, [-1, 1]) run("legendre01", lambda x: 1, [0, 1]) run("hermite", lambda x: mp.exp(-x * x), [-mp.inf, mp.inf]) run("laguerre", lambda x: mp.exp(-x), [0, mp.inf]) run("glaguerre", lambda x: mp.sqrt(x) * mp.exp(-x), [0, mp.inf], alpha=1 / mp.mpf(2)) run("chebyshev1", lambda x: 1 / mp.sqrt(1 - x * x), [-1, 1]) run("chebyshev2", lambda x: mp.sqrt(1 - x * x), [-1, 1]) run("jacobi", lambda x: (1 - x)**(1 / mp.mpf(3)) * (1 + x)**(1 / mp.mpf(5)), [-1, 1], alpha=1 / mp.mpf(3), beta=1 / mp.mpf(5))
def test_levin_3(): mp.dps = 17 z=mp.mpf(2) eps = mp.mpf(mp.eps) with mp.extraprec(7*mp.prec): # we need copious amount of precision to sum this highly divergent series L = mp.levin(method = "levin", variant = "t") n, s = 0, 0 while 1: s += (-z)**n * mp.fac(4 * n) / (mp.fac(n) * mp.fac(2 * n) * (4 ** n)) n += 1 v, e = L.step_psum(s) if e < eps: break if n > 1000: raise RuntimeError("iteration limit exceeded") eps = mp.exp(0.8 * mp.log(eps)) exact = mp.quad(lambda x: mp.exp( -x * x / 2 - z * x ** 4), [0,mp.inf]) * 2 / mp.sqrt(2 * mp.pi) # there is also a symbolic expression for the integral: # exact = mp.exp(mp.one / (32 * z)) * mp.besselk(mp.one / 4, mp.one / (32 * z)) / (4 * mp.sqrt(z * mp.pi)) err = abs(v - exact) assert err < eps w = mp.nsum(lambda n: (-z)**n * mp.fac(4 * n) / (mp.fac(n) * mp.fac(2 * n) * (4 ** n)), [0, mp.inf], method = "levin", levin_variant = "t", workprec = 8*mp.prec, steps = [2] + [1 for x in xrange(1000)]) err = abs(v - w) assert err < eps
def pdf_gauss_mp(x, sigma, mean): return mp.mpf(1.) / mp.sqrt(mp.mpf("2.") * sigma**2 * mp.pi) * mp.exp( -(x - mean)**2 / (mp.mpf("2.") * sigma**2))
def pdf_gauss_mp(x, sigma, mean): return mp.mpf(1.) / mp.sqrt(mp.mpf("2.") * sigma ** 2 * mp.pi) * mp.exp( - (x - mean) ** 2 / (mp.mpf("2.") * sigma ** 2))
pi_arctan.append((sum1 + sum2) * 4.0) pi_machin = [] sum1 = mp.mpf(0.0) sum2 = mp.mpf(0.0) for i in range(0, n_iter): sum1 = sum1 + (-1)**i / (mp.mpf(5.0)**(2 * i + 1) * (2 * i + 1.0)) sum2 = sum2 + (-1)**i / (mp.mpf(239.0)**(2 * i + 1) * (2 * i + 1.0)) pi_machin.append((4.0 * sum1 - sum2) * 4.0) pi_ramanujan = [] sum = mp.mpf(0.0) for i in range(0, n_iter): sum = sum + mp.factorial(4 * i) / (mp.mpf(4)**i * mp.factorial(i))**4 * ( 1103 + 26390 * mp.mpf(i)) / mp.mpf(99)**(4 * i) pi_ramanujan.append(1.0 / (2.0 * mp.sqrt(2) / 9801 * sum)) plt.figure() plt.plot(pi_gregory, label='Gregory') plt.plot(pi_arctan, label='simple arctan') plt.plot(pi_machin, label='Machin') plt.plot(pi_ramanujan, label='Ramanujan') plt.legend() plt.xlabel('Iteration', Fontsize=16) plt.title("Convergence to $\pi$", Fontsize=16) plt.figure() error_gregory = [x - mp.pi for x in pi_gregory] plt.semilogy(error_gregory, 'g.', label='Gregory') error_arctan = [x - mp.pi for x in pi_arctan] plt.semilogy(error_arctan, 'r.', label='simple arctan')