Beispiel #1
0
    def test_derivative_expansion(self):
        """
        Expansion coefficients of derivatives.
        """

        alpha = 10*np.random.rand(1)[0]
        beta = 10*np.random.rand(1)[0]
        J = JacobiPolynomials(alpha=alpha, beta=beta)

        N = 13
        K = 11

        x, w = J.gauss_quadrature(2*N)
        V = J.eval(x, range(K+1))

        for s in range(4):
            C = J.derivative_expansion(s, N, K)

            Vd = J.eval(x, range(N+1), d=s)
            C2 = Vd.T @ np.diag(w) @ V

            reserror = np.linalg.norm(C-C2)
            msg = "Failed for (s, alpha, beta)=({0:d}, {1:1.6f}, {2:1.6f})".format(s, alpha, beta)
            delta = 1e-8
            self.assertAlmostEqual(reserror, 0, delta=delta, msg=msg)
Beispiel #2
0
    def test_gq_modification_global(self):
        """ gq_modification for a global integral
        Testing of gq_modification on an interval [-1,1] with specified
        integrand.
        """

        alpha = -1. + 6 * np.random.rand()
        beta = -1. + 6 * np.random.rand()
        J = JacobiPolynomials(alpha=alpha, beta=beta)

        delta = 1e-8
        N = 10

        G = np.zeros([N, N])

        for n in range(N):
            for m in range(N):
                # Integrate the entire integrand
                integrand = lambda x: J.eval(x, m).flatten() * J.eval(
                    x, n).flatten() * jacobi_weight_normalized(x, alpha, beta)
                G[n, m] = quad.gq_modification(integrand,
                                               -1,
                                               1,
                                               ceil(1 + (n + m) / 2.),
                                               gamma=[alpha, beta])

        errstr = 'Failed for (N,alpha,beta) = ({0:d}, {1:1.6f}, {2:1.6f})'.format(
            N, alpha, beta)

        self.assertAlmostEqual(np.linalg.norm(G - np.eye(N), ord=np.inf),
                               0,
                               delta=delta,
                               msg=errstr)
Beispiel #3
0
    def test_ratio(self):
        """ Evaluation of orthogonal polynomial ratios.  """

        alpha = -1. + 10*np.random.rand(1)[0]
        beta = -1. + 10*np.random.rand(1)[0]
        J = JacobiPolynomials(alpha=alpha, beta=beta)

        N = int(np.ceil(60*np.random.rand(1)))
        x = (1 + 5*np.random.rand(1)) * (1 + np.random.rand(50))
        y = (1 + 5*np.random.rand(1)) * (-1 - np.random.rand(50))
        x = np.concatenate([x, y])

        P = J.eval(x, range(N+1))
        rdirect = np.zeros([x.size, N+1])
        rdirect[:, 0] = P[:, 0]
        rdirect[:, 1:] = P[:, 1:]/P[:, :-1]

        r = J.r_eval(x, range(N+1))

        delta = 1e-6
        errs = np.abs(r-rdirect)
        i, j = np.where(errs > delta)[:2]
        if i.size > 0:
            errstr = 'Failed for alpha={0:1.3f}, beta={1:1.3f}, n={2:d}, x={3:1.6f}'.format(alpha, beta, j[0], x[i[0]])
        else:
            errstr = ''

        self.assertAlmostEqual(np.linalg.norm(errs, ord=np.inf), 0, delta=delta, msg=errstr)
Beispiel #4
0
    def test_gq(self):
        """Gaussian quadrature integration accuracy"""

        alpha = -1. + 10*np.random.rand(1)[0]
        beta = -1. + 10*np.random.rand(1)[0]

        J = JacobiPolynomials(alpha=alpha, beta=beta)
        N = int(np.ceil(60*np.random.rand(1)))

        x, w = J.gauss_quadrature(N)
        w /= w.sum()    # Force probability measure

        V = J.eval(x, range(2*N))

        integrals = np.dot(w, V)
        integrals[0] -= V[0, 0]  # Exact value

        self.assertAlmostEqual(np.linalg.norm(integrals, ord=np.inf), 0.)
Beispiel #5
0
    def test_idist_legendre(self):
        """Evaluation of Legendre induced distribution function."""

        J = JacobiPolynomials(alpha=0., beta=0.)

        n = int(np.ceil(25*np.random.rand(1))[0])
        M = 25
        x = -1. + 2*np.random.rand(M)

        # JacobiPolynomials method
        F1 = J.idist(x, n)

        y, w = J.gauss_quadrature(n+1)

        # Exact: integrate density
        F2 = np.zeros(F1.shape)
        for xind, xval in enumerate(x):
            yquad = (y+1)/2.*(xval+1) - 1.
            F2[xind] = np.dot(w, J.eval(yquad, n)**2) * (xval+1)/2

        self.assertAlmostEqual(np.linalg.norm(F1-F2, ord=np.inf), 0.)