示例#1
0
def test_interpolation_2d():
    '''
    Test function
        f(x,y) = x^2 + y^2
        f(x,y) = exp(-(x^2+y^2))
    '''

    Nx = 32
    Ny = 32
    Mx = 100
    My = 200

    ix = np.arange(Nx + 1)
    x = np.cos(ix * np.pi / Nx)
    wx = barycentric_weights_cgl(Nx)
    iy = np.arange(Ny + 1)
    y = np.cos(iy * np.pi / Ny)
    wy = barycentric_weights_cgl(Ny)
    X, Y = np.meshgrid(x, y)
    #f = X**2 + Y**2
    f = np.exp(-(X**2 + Y**2))
    plt.plot(X[Ny / 2, :], f[Ny / 2., :], '.')

    kx = np.arange(Mx + 1)
    u = (2. / Mx) * kx - 1.
    ky = np.arange(My + 1)
    v = (2. / My) * ky - 1.
    U, V = np.meshgrid(u, v)
    #f0 = U**2 + V**2
    f0 = np.exp(-(U**2 + V**2))

    f1 = np.zeros_like(f)
    f2 = np.zeros_like(f)

    print 'cheb interpolation'
    t = time()
    f1 = cheb_interpolation_2d(u, v, f)
    print 'cheb time: ', time() - t

    print 'general interpolation'
    t = time()
    f2 = interpolation_2d(u, v, f, x, y, wx, wy)
    print 'general time: ', time() - t

    print f2 - f1
    print 'Is general and cheb interpolation the same? ',
    print almost_equal(f1, f2)
    print 'Interpolation error: ', np.linalg.norm(f1 - f0)
    plt.plot(U[My / 2, :], f1[My / 2., :])

    plt.show()
示例#2
0
def test_clencurt_weights():
    N = 257
    M = 500

    t = time()
    for i in xrange(M):
        w1 = clencurt_weights(N)
    print 'direct sum time: ', time() - t

    t = time()
    for i in xrange(M):
        w2 = clencurt_weights_fft(N)
    print 'fft time: ', time() - t

    print w1 - w2
    print almost_equal(w1, w2)
示例#3
0
    def testThualComplex(self):
        L = np.array([[1, 1, 1, 1, 1, 1], [3 + 1j, 2, 1, 0, 0, 0],
                      [0, 6, 5, 4 - 1j, 0, 0], [0, 0, 7, 9, 8, 0],
                      [0, 0, 0, 3, 1, 0], [0, 0, 0, 0, 2, 7]])
        f = np.array([0, 3, 5, 8, 7, 4])
        u0 = np.linalg.solve(L, f)

        p = np.array([3 + 1j, 6, 7, 3, 2])
        q = np.array([2, 5, 9, 1, 7])
        r = np.array([1, 4 - 1j, 8, 0, 0])
        c = np.array([1, 1, 1, 1, 1, 1])

        u = solve_tridiag_complex_thual(p, q, r, c, f)

        # it will fail if tol < 40
        self.assertTrue(almost_equal(u0.real, u.real, 40))
        self.assertTrue(almost_equal(u0.imag, u.imag, 40))
示例#4
0
def test_interpolation_1d():
    '''
    Test function
        f(x) = |x| + x/2 - x^2
    '''

    N = 64
    M = 1000

    ii = np.arange(N + 1)
    xx = np.cos(ii * np.pi / N)
    w = barycentric_weights_cgl(N)
    f = np.abs(xx) + .5 * xx - xx**2
    plt.plot(xx, f, '.')

    kk = np.arange(M + 1)
    yy = (2. / M) * kk - 1.
    f1 = np.zeros_like(yy)
    f2 = np.zeros_like(yy)
    f3 = np.zeros_like(yy)
    f4 = np.zeros_like(yy)

    print 'Interpolation by point-by-point'
    t = time()
    for j in xrange(M + 1):
        f1[j] = cheb_interpolation_point(yy[j], f)
        f2[j] = interpolation_point(yy[j], f, xx, w)
    print 'point-by-point time: ', time() - t
    print f2 - f1
    print almost_equal(f1, f2)
    plt.plot(yy, f1)

    print 'Interpolation by matrix'
    t = time()
    f3 = cheb_interpolation_1d(yy, f)
    f4 = interpolation_1d(yy, f, xx, w)
    print 'matrix time: ', time() - t
    print f3 - f1
    print f4 - f1
    print almost_equal(f3, f1), almost_equal(f4, f1)
    plt.plot(yy, f3, 'r')

    plt.show()
示例#5
0
def test_barycentric_weights():
    N = 16
    ii = np.arange(N + 1)
    print 'Chebyshev Gauss-Lobatto points'
    xx = np.cos(ii * np.pi / N)
    w = barycentric_weights(xx)
    w1 = barycentric_weights_cgl(N)
    print w / np.max(w)
    print w1
    print w / np.max(w) - w1
    print almost_equal(w / np.max(w), w1)

    print 'Chebyshev Gauss points'
    xx = np.cos((2 * ii + 1) * np.pi / (2 * N + 2))
    w = barycentric_weights(xx)
    w1 = barycentric_weights_cg(N)
    print w / np.max(w)
    print w1
    print w / np.max(w) - w1
    print almost_equal(w / np.max(w), w1)
示例#6
0
    def testThual(self):
        L = np.array([[1, 1, 1, 1, 1, 1], [3, 2, 1, 0, 0,
                                           0], [0, 6, 5, 4, 0, 0],
                      [0, 0, 7, 9, 8, 0], [0, 0, 0, 3, 1, 0],
                      [0, 0, 0, 0, 2, 7]])
        f = np.array([0, 3, 5, 8, 7, 4])
        u0 = np.linalg.solve(L, f)

        p = np.array([3, 6, 7, 3, 2])
        q = np.array([2, 5, 9, 1, 7])
        r = np.array([1, 4, 8, 0, 0])
        c = np.array([1, 1, 1, 1, 1, 1])

        u = solve_tridiag_thual(p, q, r, c, f)

        self.assertTrue(almost_equal(u0, u))