# Chebpy imports:
from chebpy.cheb import chebpts, coeffs2vals, multmat, spconvert, vals2coeffs

# %% Multiplication g = f*h on [-1,1].

# Functions:
f = lambda x: np.cos(x) * np.exp(x)
h = lambda x: np.sin(x) * x**2
gex = lambda x: f(x) * h(x)

# Grid:
n = 10000
x = chebpts(n)

# Compute coeffs of f:
F = vals2coeffs(f(x))

# Multiplication by h matrix in coefficient space:
start = time.time()
M = multmat(n, h)
end = time.time()
print(f'Time   (setup): {end-start:.5f}s')
plt.figure()
plt.spy(M)

# Multiply:
start = time.time()
G = M @ F
end = time.time()
print(f'Time (product): {end-start:.5f}s')
M0 = multmat(n, a0, [a, b], 0)
M1 = multmat(n, a1, [a, b], 1)
M2 = multmat(n, a2, [a, b], 2)
L = M2 @ D2 + S1 @ M1 @ D1 + S1 @ S0 @ M0
L = lil_matrix(L)
for k in range(n):
    T = np.zeros(n)
    T[k] = 1
    L[-2, k] = feval(T, 2 / (b - a) * x0 - (a + b) / (b - a))
    L[-1, k] = feval(T, 2 / (b - a) * x1 - (a + b) / (b - a))
L = csr_matrix(L)
plt.figure()
plt.spy(L)

# Assemble RHS:
F = vals2coeffs(f(x))
F = S1 @ S0 @ F
F[-2] = c
F[-1] = d
F = csr_matrix(np.round(F, 13)).T
end = time.time()
print(f'Time  (setup): {end-start:.5f}s')

# Sparse solve:
start = time.time()
U = spsolve(L, F)
end = time.time()
print(f'Time  (solve): {end-start:.5f}s')

# Plot and compute error:
u = coeffs2vals(U)
Example #3
0
Copyright 2020 by Hadrien Montanelli.
"""
# %% Imports.

# Standard library imports:
import numpy as np

# Chebpy imports:
from chebpy.cheb import chebpts, coeffs2vals, vals2coeffs

# %% Transforms (1D) on [-1,1].

f = lambda x: np.exp(-10 * x**2)
n = 100
x = chebpts(n)
error = coeffs2vals(vals2coeffs(f(x))) - f(x)
print(f'Error (1D): {np.max(np.abs(error)):.2e}')

# %% Transforms (1D) on [0,2].

f = lambda x: np.exp(-10 * x**2)
n = 100
x = chebpts(n, [0, 2])
error = coeffs2vals(vals2coeffs(f(x))) - f(x)
print(f'Error (1D): {np.max(np.abs(error)):.2e}')

# %% Transforms (2D) on [-1,1]x[-1,1].

f = lambda x, y: np.exp(-10 * (x**2 + y**2))
n = 100
x = chebpts(n)
Example #4
0
A2 = D2z
C2 = S1 @ S0 @ M0

# Assemble boundary conditions:
Bx = np.zeros([2, n])
By = np.zeros([2, n])
G = np.zeros([2, n])
H = np.zeros([2, n])
for k in range(n):
    T = np.zeros(n)
    T[k] = 1
    Bx[0, k] = feval(T, 2 / (zb - za) * z0 - (za + zb) / (zb - za))
    By[0, k] = feval(T, 2 / (rb - ra) * r0 - (ra + rb) / (rb - ra))
    Bx[1, k] = feval(T, 2 / (zb - za) * z1 - (za + zb) / (zb - za))
    By[1, k] = feval(T, 2 / (rb - ra) * r1 - (ra + rb) / (rb - ra))
G[0, :] = vals2coeffs(g1(z))
G[1, :] = vals2coeffs(g2(z))
H[0, :] = vals2coeffs(h1(r))
H[1, :] = vals2coeffs(h2(r))
Bx_hat = Bx[0:2, 0:2]
Bx = np.linalg.inv(Bx_hat) @ Bx
G = np.linalg.inv(Bx_hat) @ G
By_hat = By[0:2, 0:2]
By = np.linalg.inv(By_hat) @ By
H = np.linalg.inv(By_hat) @ H

# Assemble right-hand side:
F = vals2coeffs(vals2coeffs(f(R, Z)).T).T
F = (S1 @ S0) @ F @ (S1 @ S0).T

# Assemble matrices for the generalized Sylvester equation:
Example #5
0
A2 = diffmat(n, 2)
C2 = S1 @ S0

# Assemble boundary conditions:
Bx = np.zeros([2, n])
By = np.zeros([2, n])
G = np.zeros([2, n])
H = np.zeros([2, n])
for k in range(n):
    T = np.zeros(n)
    T[k] = 1
    Bx[0, k] = feval(T, -1)
    By[0, k] = feval(T, -1)
    Bx[1, k] = feval(T, 1)
    By[1, k] = feval(T, 1)
G[0, :] = vals2coeffs(g1(y))
G[1, :] = vals2coeffs(g2(y))
H[0, :] = vals2coeffs(h1(x))
H[1, :] = vals2coeffs(h2(x))
Bx_hat = Bx[0:2, 0:2]
Bx = np.linalg.inv(Bx_hat) @ Bx
G = np.linalg.inv(Bx_hat) @ G
By_hat = By[0:2, 0:2]
By = np.linalg.inv(By_hat) @ By
H = np.linalg.inv(By_hat) @ H

# Assemble right-hand side:
F = vals2coeffs(vals2coeffs(f(X, Y)).T).T
F = (S1 @ S0) @ F @ (S1 @ S0).T

# Assemble matrices for the generalized Sylvester equation:
from chebpy.cheb import chebpts, diffmat, spconvert, vals2coeffs

# %% First-order differentiation on [-1, 1].

# Function:
w = 100
f = lambda x: np.cos(w * x)
dfex = lambda x: -w * np.sin(w * x)

# Grid:
n = 4 * w
dom = [-1, 1]
x = chebpts(n, dom)

# Compute coeffs of f:
F = vals2coeffs(f(x))

# Differentiation matrix in coefficient space:
start = time.time()
D = diffmat(n, 1, dom)
end = time.time()
print(f'Time   (setup): {end-start:.5f}s')
plt.figure()
plt.spy(D)

# Differentiate:
start = time.time()
DF = D @ F
end = time.time()
print(f'Time (product): {end-start:.5f}s')