# Standard library imports: from math import pi import numpy as np # Chebpy imports: from chebpy.trig import trigpts, quadwts # %% Integrate f(x) = 1 + sin(4*pi*x) on [-1,1]. # Function: f = lambda x: 1 + np.sin(4 * pi * x) # Grid and weights: n = 12 x = trigpts(n) w = quadwts(n) # Compute integral with quadrature: I = w @ f(x) # Compute error: Iex = 2 error = np.abs(I - Iex) print(f'Error: {error:.2e}') # %% Integrate f(x) = 1 + sin(4*x) on [-pi,pi]. # Function: f = lambda x: 1 + np.sin(4 * x)
# Standard library imports: from math import pi from matplotlib import cm import matplotlib.pyplot as plt import numpy as np # Chebpy imports: from chebpy.trig import trigpts from chebpy.sph import vals2coeffs, feval, mean, spharm # %% Test. # Grid points: n = 32 dom = [-pi, pi] lam = trigpts(n, dom) tt = trigpts(n, dom) LAM, TT = np.meshgrid(lam, tt) # Spherical harmonic: l = 4 # has to be >= 0 m = -3 # -l <= m <= +l Y = spharm(l, m) # Plot: plt.figure() plt.contourf(LAM, TT, feval(Y, LAM, TT), 40, cmap=cm.coolwarm) plt.colorbar() # Check orthonormality: F = vals2coeffs(feval(Y, LAM, TT) * feval(Y, LAM, TT))
# Standard library imports: from math import pi import matplotlib.pyplot as plt import numpy as np from scipy.sparse.linalg import spsolve import time # Chebpy imports: from chebpy.trig import coeffs2vals, trigpts, vals2coeffs from chebpy.trig import diffmat, multmat # %% Solve u''(x) + sin(x)*u'(x) + 1000*cos(2x)*u(x) = f on [0,2*pi]. # Grid: n = 10000 x = trigpts(n, [0, 2 * pi]) # Right-hand side f: f = lambda x: 100 + 0 * x # Assemble matrices: start = time.time() D1 = diffmat(n, 1, [0, 2 * pi]) D2 = diffmat(n, 2, [0, 2 * pi]) M0 = multmat(n, lambda x: 1000 * np.cos(2 * x), [0, 2 * pi]) M1 = multmat(n, lambda x: np.sin(x), [0, 2 * pi]) L = D2 + M1 @ D1 + M0 plt.figure() plt.spy(L) # Assemble RHS:
Copyright 2020 by Hadrien Montanelli. """ # %% Imports. # Standard library import: from math import pi import numpy as np # Chebpy imports: from chebpy.trig import coeffs2vals, trigpts, vals2coeffs # %% Transforms (1D) on [-1,1]. f = lambda x: np.cos(10000 * pi * x) + np.cos(5 * pi * x) n = 100 x = trigpts(n) error = coeffs2vals(vals2coeffs(f(x))) - f(x) print(f'Error (1D): {np.max(np.abs(error)):.2e}') # %% Transforms (1D) on [0,2*pi]. f = lambda x: np.exp(np.cos(10 * pi * x)**2) n = 120 x = trigpts(n, [0, 2 * pi]) 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(np.cos(10 * pi * x)**2) * np.sin(pi * y)**2 n = 100
""" Created on Fri Dec 4 16:56:16 2020 Copyright 2020 by Hadrien Montanelli. """ # %% Imports. # Standard library imports: from math import pi import numpy as np # Chebpy imports: from chebpy.trig import feval, trigpts, vals2coeffs # %% Evaluate f(x) = cos(pi*x)*exp(-sin(pi*x)^2). # Function: f = lambda x: np.cos(pi*x)*np.exp(-np.sin(pi*x)**2) # Equispaced grid: n = 100 x = trigpts(n) F = vals2coeffs(f(x)) # Evaluation grid: xx = np.linspace(-1, 1, 100) vals = feval(F, xx) # Error: error = np.max(np.abs(vals - f(xx)))/np.max(np.abs(f(xx))) print(f'Error: {error:.2e}')
import time # Chebpy imports: from chebpy.trig import coeffs2vals, diffmat, trigpts, vals2coeffs # %% First-order differentiation on [-pi, pi]. # Function: w = 100 f = lambda x: np.cos(w * x) dfex = lambda x: -w * np.sin(w * x) # Grid: n = 4 * w dom = [-pi, pi] x = trigpts(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