Documentation
¶
Overview ¶
Package spectralpde implements spectral methods for the numerical solution of differential equations.
The package provides Fourier and Chebyshev collocation machinery: spectral differentiation matrices, Chebyshev-Gauss-Lobatto and periodic Fourier grids, discrete cosine/sine and (fast) Fourier transforms, barycentric interpolation, Clenshaw-Curtis and Gauss quadrature, and end-to-end solvers for the Poisson, Helmholtz, heat and advection-diffusion equations in one and two space dimensions.
All routines are written against the Go standard library only. Grids and operators follow the conventions of Trefethen, Spectral Methods in MATLAB: the Chebyshev-Gauss-Lobatto nodes are x_j = cos(pi*j/N) for j = 0..N (ordered from +1 down to -1), and the periodic Fourier nodes are x_j = 2*pi*j/N for j = 0..N-1 on [0, 2*pi).
Spectral methods converge exponentially fast for smooth data. The helper SpectralConvergenceRate and the error metrics (L2Error, LinfError, ...) make it easy to observe that behaviour in tests and applications.
Matrices are stored row-major as [][]float64 unless wrapped by the Matrix type, and vectors are plain []float64. Functions never mutate their inputs unless explicitly documented to do so.
Index ¶
- Variables
- func AXPY(a float64, x, y []float64) []float64
- func AdvectionDiffusionSolveFourier(u0 []float64, c, nu, t, L float64) []float64
- func AdvectionSolveFourier(u0 []float64, c, t, L float64) []float64
- func AffineMap(x, a, b float64) float64
- func AffineMapInverse(y, a, b float64) float64
- func ApplyFunc(f func(float64) float64, x []float64) []float64
- func BarycentricInterpolate(nodes, values, w []float64, x float64) float64
- func BarycentricInterpolateVec(nodes, values, w, xs []float64) []float64
- func BarycentricWeights(nodes []float64) []float64
- func BarycentricWeightsChebyshev(N int) []float64
- func BuildChebyshevLaplacian1D(N int, a, b float64) [][]float64
- func BuildLaplacian2D(Nx, Ny int, ax, bx, ay, by float64) [][]float64
- func ChebyshevBarycentricInterpolate(values []float64, x float64) float64
- func ChebyshevCoefficients(values []float64) []float64
- func ChebyshevDiffMatrix(N int) [][]float64
- func ChebyshevDiffMatrix2(N int) [][]float64
- func ChebyshevDiffMatrixInterval(N int, a, b float64) [][]float64
- func ChebyshevDiffMatrixOrder(N, m int) [][]float64
- func ChebyshevDifferentiateCoeffs(coeffs []float64) []float64
- func ChebyshevEvalInterval(coeffs []float64, y, a, b float64) float64
- func ChebyshevFit(f func(float64) float64, N int) []float64
- func ChebyshevFitInterval(f func(float64) float64, N int, a, b float64) []float64
- func ChebyshevGaussLobattoAngles(N int) []float64
- func ChebyshevGaussLobattoNodes(N int) []float64
- func ChebyshevGaussLobattoNodesInterval(N int, a, b float64) []float64
- func ChebyshevGaussLobattoWeights(N int) []float64
- func ChebyshevGaussNodes(N int) []float64
- func ChebyshevGaussWeights(N int) []float64
- func ChebyshevIntegral(coeffs []float64) float64
- func ChebyshevIntegrateCoeffs(coeffs []float64) []float64
- func ChebyshevInterpolantError(f func(float64) float64, N int, eval []float64) float64
- func ChebyshevInterpolate(values []float64, x float64) float64
- func ChebyshevL2Projection(f func(float64) float64, n int) []float64
- func ChebyshevRoots(n int) []float64
- func ChebyshevT(n int, x float64) float64
- func ChebyshevTDerivative(n int, x float64) float64
- func ChebyshevTValues(n int, xs []float64) []float64
- func ChebyshevTruncationError(coeffs []float64, keep int) float64
- func ChebyshevU(n int, x float64) float64
- func ChebyshevValuesFromCoeffs(coeffs []float64) []float64
- func ChebyshevVandermonde(nodes []float64, degree int) [][]float64
- func ClenshavEval(coeffs []float64, x float64) float64deprecated
- func ClenshawCurtisIntegrate(f func(float64) float64, N int, a, b float64) float64
- func ClenshawCurtisNodes(N int) []float64
- func ClenshawCurtisWeights(N int) []float64
- func ClenshawEval(coeffs []float64, x float64) float64
- func ClenshawEvalDerivative(coeffs []float64, x float64) float64
- func CoefficientDecayRate(coeffs []float64) float64
- func ComplexAbs(x []complex128) []float64
- func ComplexImag(x []complex128) []float64
- func ComplexReal(x []complex128) []float64
- func ConvergenceOrder(hs, errs []float64) float64
- func CopyMatrix(a [][]float64) [][]float64
- func DCT1(x []float64) []float64
- func DFT(x []complex128) []complex128
- func DST1(x []float64) []float64
- func Determinant(a [][]float64) (float64, error)
- func Diag(v []float64) [][]float64
- func DifferentiateNodal(D [][]float64, u []float64) []float64
- func DotProduct(x, y []float64) float64
- func FFT(x []complex128) []complex128
- func FFTReal(x []float64) []complex128
- func FejerIntegrate(f func(float64) float64, n int, a, b float64) float64
- func FejerNodes(n int) []float64
- func FejerWeights(n int) []float64
- func FourierCoefficients(values []float64) []complex128
- func FourierDiffMatrix(N int) [][]float64
- func FourierDiffMatrix2(N int) [][]float64
- func FourierDiffMatrixOrder(N, m int) [][]float64
- func FourierDifferentiate(values []float64) []float64
- func FourierDifferentiate2(values []float64) []float64
- func FourierDifferentiateOrder(values []float64, m int) []float64
- func FourierInterpolate(values []float64, x float64) float64
- func FourierNodes(N int) []float64
- func FourierNodesInterval(N int, a, b float64) []float64
- func FourierWavenumbers(N int) []float64
- func FourierWeights(N int) []float64
- func FrobeniusNorm(a [][]float64) float64
- func GalerkinProjectChebyshev(f func(float64) float64, n int) []float64
- func GalerkinProjectLegendre(f func(float64) float64, n int) []float64
- func GaussChebyshevIntegrate(f func(float64) float64, n int) float64
- func GaussLegendreIntegrate(f func(float64) float64, n int, a, b float64) float64
- func HeatSolveChebyshev(u0 func(float64) float64, nu, dt float64, steps, N int, a, b float64) (nodes, u []float64, err error)
- func HeatSolveFourier(u0 []float64, nu, t, L float64) []float64
- func HeatStepChebyshevCN(uInterior []float64, nu, dt float64, N int, a, b float64) ([]float64, error)
- func Helmholtz2D(f, g func(x, y float64) float64, k2 float64, Nx, Ny int, ...) (xn, yn []float64, U [][]float64, err error)
- func HelmholtzSolve1D(f func(float64) float64, k2 float64, N int, a, b, ua, ub float64) (nodes, u []float64, err error)
- func IDCT1(x []float64) []float64
- func IDFT(x []complex128) []complex128
- func IDST1(x []float64) []float64
- func IFFT(x []complex128) []complex128
- func Identity(n int) [][]float64
- func IntegrateFunction(f func(float64) float64, nodes, weights []float64) float64
- func IntegrateWeighted(weights, values []float64) float64
- func InterpolationError(f func(float64) float64, nodes []float64, eval []float64) float64
- func IntervalScale(a, b float64) float64
- func Inverse(a [][]float64) ([][]float64, error)
- func IsSpectrallyConverging(ns []int, errs []float64, rate float64) bool
- func JacobiEigenSymmetric(a [][]float64) (vals []float64, vecs [][]float64, err error)
- func Kron(a, b [][]float64) [][]float64
- func L2Error(a, b []float64) float64
- func LUDecompose(a [][]float64) (lu [][]float64, piv []int, sign float64, err error)
- func LUSolve(lu [][]float64, piv []int, b []float64) []float64
- func LagrangeBasis(nodes []float64, j int, x float64) float64
- func LagrangeInterpolate(nodes, values []float64, x float64) float64
- func LegendreGaussLobattoNodes(N int) []float64
- func LegendreGaussLobattoNodesWeights(N int) ([]float64, []float64)
- func LegendreGaussLobattoWeights(N int) []float64
- func LegendreGaussNodes(n int) []float64
- func LegendreGaussNodesWeights(n int) ([]float64, []float64)
- func LegendreGaussWeights(n int) []float64
- func LegendreNormSquared(n int) float64
- func LegendreP(n int, x float64) float64
- func LegendrePDerivative(n int, x float64) float64
- func LegendrePSecondDerivative(n int, x float64) float64
- func LegendrePValues(n int, xs []float64) []float64
- func LegendreProjection(f func(float64) float64, n int) []float64
- func LegendreSeriesEval(coeffs []float64, x float64) float64
- func LegendreVandermonde(nodes []float64, degree int) [][]float64
- func LinSpace(a, b float64, n int) []float64
- func LinfError(a, b []float64) float64
- func MapFromInterval(nodes []float64, a, b float64) []float64
- func MapToInterval(nodes []float64, a, b float64) []float64
- func MatAdd(a, b [][]float64) [][]float64
- func MatMaxAbs(a [][]float64) float64
- func MatMul(a, b [][]float64) [][]float64
- func MatPow(a [][]float64, p int) [][]float64
- func MatScale(a [][]float64, s float64) [][]float64
- func MatSub(a, b [][]float64) [][]float64
- func MatVec(a [][]float64, x []float64) []float64
- func MaxAbs(x []float64) float64
- func Mean(x []float64) float64
- func NewtonDividedDifferences(nodes, values []float64) []float64
- func NewtonEval(coef, nodes []float64, x float64) float64
- func Norm1(x []float64) float64
- func Norm2(x []float64) float64
- func NormInf(x []float64) float64
- func NormP(x []float64, p float64) float64
- func Poisson2D(f, g func(x, y float64) float64, Nx, Ny int, ax, bx, ay, by float64) (xn, yn []float64, U [][]float64, err error)
- func Poisson2DFourier(f func(x, y float64) float64, Nx, Ny int, Lx, Ly float64) (xn, yn []float64, U [][]float64)
- func PoissonSolve1D(f func(float64) float64, N int, a, b, ua, ub float64) (nodes, u []float64, err error)
- func PoissonSolve1DFourier(f func(float64) float64, N int, L float64) (nodes, u []float64)
- func PolynomialInterpolate(nodes, values []float64, x float64) float64
- func RMSError(a, b []float64) float64
- func RealToComplex(x []float64) []complex128
- func RelativeL2Error(a, b []float64) float64
- func Reverse(x []float64) []float64
- func SimpsonIntegrate(f func(float64) float64, n int, a, b float64) float64
- func SolveLinearSystem(a [][]float64, b []float64) ([]float64, error)
- func SolveMatrix(a, b [][]float64) ([][]float64, error)
- func SpectralConvergenceRate(ns []int, errs []float64) float64
- func SpectralDerivativeChebyshev(values []float64, a, b float64) []float64
- func Sum(x []float64) float64
- func Trace(a [][]float64) float64
- func Transpose(a [][]float64) [][]float64
- func TrapezoidalIntegrate(f func(float64) float64, n int, a, b float64) float64
- func TrapezoidalWeights(n int, a, b float64) []float64
- func UniformNodes(n int, a, b float64) []float64
- func VectorAdd(x, y []float64) []float64
- func VectorCopy(x []float64) []float64
- func VectorFill(n int, v float64) []float64
- func VectorHadamard(x, y []float64) []float64
- func VectorScale(x []float64, s float64) []float64
- func VectorSub(x, y []float64) []float64
- func VectorZeros(n int) []float64
- func Zeros(r, c int) [][]float64
- type Matrix
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrDimensionMismatch = errors.New("spectralpde: dimension mismatch")
ErrDimensionMismatch is returned when operands have incompatible shapes.
var ErrInvalidArgument = errors.New("spectralpde: invalid argument")
ErrInvalidArgument is returned for out-of-range or malformed arguments.
var ErrSingularMatrix = errors.New("spectralpde: singular matrix")
ErrSingularMatrix is returned when a matrix is (numerically) singular.
Functions ¶
func AdvectionDiffusionSolveFourier ¶
AdvectionDiffusionSolveFourier evolves the periodic equation u_t + c*u_x = nu*u_xx on [0, L) exactly in Fourier space from u0 to time t.
func AdvectionSolveFourier ¶
AdvectionSolveFourier evolves the periodic linear advection equation u_t + c*u_x = 0 on [0, L) exactly to time t.
func AffineMap ¶
AffineMap returns the affine transformation of x from the reference interval [-1, 1] to the interval [a, b].
func AffineMapInverse ¶
AffineMapInverse maps y from [a, b] back to the reference interval [-1, 1].
func BarycentricInterpolate ¶
BarycentricInterpolate evaluates the interpolating polynomial through (nodes, values) at x, using the barycentric weights w (see BarycentricWeights).
func BarycentricInterpolateVec ¶
BarycentricInterpolateVec evaluates the barycentric interpolant at every point of xs.
func BarycentricWeights ¶
BarycentricWeights returns the barycentric interpolation weights w_j = 1 / prod_{k != j} (nodes[j] - nodes[k]) for an arbitrary set of distinct nodes.
func BarycentricWeightsChebyshev ¶
BarycentricWeightsChebyshev returns the closed-form barycentric weights for the N+1 Chebyshev-Gauss-Lobatto nodes: w_j = (-1)^j * delta_j with delta_j = 1/2 at the endpoints and 1 in the interior.
func BuildChebyshevLaplacian1D ¶
BuildChebyshevLaplacian1D returns the second-derivative (Laplacian in 1-D) Chebyshev collocation operator on [a, b], i.e. D^2 scaled by (2/(b-a))^2.
func BuildLaplacian2D ¶
BuildLaplacian2D returns the 2-D Chebyshev collocation Laplacian on the tensor-product grid of [ax, bx] x [ay, by] with Nx+1 nodes in x and Ny+1 nodes in y. The unknowns are ordered lexicographically with the x-index varying slowest: index(i, j) = i*(Ny+1) + j. The operator is D2x kron Iy + Ix kron D2y.
func ChebyshevBarycentricInterpolate ¶
ChebyshevBarycentricInterpolate evaluates the interpolant of values sampled at the Chebyshev-Gauss-Lobatto nodes at x, using the closed-form Chebyshev barycentric weights.
func ChebyshevCoefficients ¶
ChebyshevCoefficients returns the discrete Chebyshev transform of the values f(x_j) sampled at the N+1 Chebyshev-Gauss-Lobatto nodes, where N = len(values)-1. The returned coefficients a_k satisfy f(x) = sum_k a_k T_k(x) as the polynomial interpolant through the nodes.
func ChebyshevDiffMatrix ¶
ChebyshevDiffMatrix returns the (N+1)-by-(N+1) Chebyshev collocation differentiation matrix D on the Chebyshev-Gauss-Lobatto nodes. If u holds the nodal values of a function then D*u holds the nodal values of its derivative (Trefethen, Spectral Methods in MATLAB, program cheb).
func ChebyshevDiffMatrix2 ¶
ChebyshevDiffMatrix2 returns the second-order Chebyshev differentiation matrix D^2.
func ChebyshevDiffMatrixInterval ¶
ChebyshevDiffMatrixInterval returns the first-order Chebyshev differentiation matrix scaled for the physical interval [a, b].
func ChebyshevDiffMatrixOrder ¶
ChebyshevDiffMatrixOrder returns the m-th order Chebyshev differentiation matrix, computed as the m-th matrix power of the first-order matrix.
func ChebyshevDifferentiateCoeffs ¶
ChebyshevDifferentiateCoeffs returns the Chebyshev coefficients of the derivative of the series represented by coeffs.
func ChebyshevEvalInterval ¶
ChebyshevEvalInterval evaluates a reference-variable Chebyshev series at the physical point y in [a, b].
func ChebyshevFit ¶
ChebyshevFit samples f at the N+1 Chebyshev-Gauss-Lobatto nodes and returns the resulting Chebyshev coefficients of the interpolant.
func ChebyshevFitInterval ¶
ChebyshevFitInterval samples f at the CGL nodes mapped to [a, b] and returns the Chebyshev coefficients of the interpolant in the reference variable.
func ChebyshevGaussLobattoAngles ¶
ChebyshevGaussLobattoAngles returns the angles theta_j = pi*j/N whose cosines are the Chebyshev-Gauss-Lobatto nodes.
func ChebyshevGaussLobattoNodes ¶
ChebyshevGaussLobattoNodes returns the N+1 Chebyshev-Gauss-Lobatto nodes x_j = cos(pi*j/N) on [-1, 1], ordered from +1 (j=0) down to -1 (j=N). N must be >= 1.
func ChebyshevGaussLobattoNodesInterval ¶
ChebyshevGaussLobattoNodesInterval returns the CGL nodes mapped to [a, b].
func ChebyshevGaussLobattoWeights ¶
ChebyshevGaussLobattoWeights returns the quadrature weights associated with the Chebyshev-Gauss-Lobatto nodes for the weight function 1/sqrt(1-x^2). The weights are pi/N for interior nodes and pi/(2N) at the endpoints.
func ChebyshevGaussNodes ¶
ChebyshevGaussNodes returns the N Chebyshev-Gauss nodes, i.e. the roots of the Chebyshev polynomial T_N, x_k = cos(pi*(2k+1)/(2N)) for k = 0..N-1.
func ChebyshevGaussWeights ¶
ChebyshevGaussWeights returns the Chebyshev-Gauss quadrature weights, all equal to pi/N, for the weight function 1/sqrt(1-x^2).
func ChebyshevIntegral ¶
ChebyshevIntegral returns the definite integral over [-1, 1] of the Chebyshev series represented by coeffs. Only even-index terms contribute, with integral(T_2m) = -2/(4m^2-1) and integral(T_0) = 2.
func ChebyshevIntegrateCoeffs ¶
ChebyshevIntegrateCoeffs returns the Chebyshev coefficients of an antiderivative of the series represented by coeffs. The integration constant (coefficient of T_0) is set to zero.
func ChebyshevInterpolantError ¶
ChebyshevInterpolantError returns the maximum absolute difference between f and its degree-N Chebyshev interpolant, sampled at the given evaluation points.
func ChebyshevInterpolate ¶
ChebyshevInterpolate evaluates, at x, the polynomial interpolant of values sampled at the N+1 Chebyshev-Gauss-Lobatto nodes.
func ChebyshevL2Projection ¶
ChebyshevL2Projection returns the first n+1 Chebyshev coefficients of the L2-orthogonal (Galerkin) projection of f with respect to the weight 1/sqrt(1-x^2), computed with an accurate Gauss-Chebyshev rule. The result is the best weighted-L2 polynomial approximation of degree n.
func ChebyshevRoots ¶
ChebyshevRoots returns the n roots of T_n (the Chebyshev-Gauss nodes).
func ChebyshevT ¶
ChebyshevT evaluates the Chebyshev polynomial of the first kind T_n at x using the stable three-term recurrence.
Example ¶
fmt.Printf("%.1f\n", ChebyshevT(3, 0.5))
Output: -1.0
func ChebyshevTDerivative ¶
ChebyshevTDerivative evaluates the derivative of T_n at x, using the identity T_n'(x) = n*U_{n-1}(x).
func ChebyshevTValues ¶
ChebyshevTValues evaluates T_n at each point of xs.
func ChebyshevTruncationError ¶
ChebyshevTruncationError estimates the interpolation error of a Chebyshev series by the sum of absolute values of the tail coefficients beyond index keep-1.
func ChebyshevU ¶
ChebyshevU evaluates the Chebyshev polynomial of the second kind U_n at x.
func ChebyshevValuesFromCoeffs ¶
ChebyshevValuesFromCoeffs reconstructs the nodal values at the N+1 Chebyshev-Gauss-Lobatto nodes (N = len(coeffs)-1) from Chebyshev coefficients. It is the inverse of ChebyshevCoefficients.
func ChebyshevVandermonde ¶
ChebyshevVandermonde returns the (len(nodes)) x (degree+1) matrix whose (i, j) entry is T_j(nodes[i]).
func ClenshavEval
deprecated
func ClenshawCurtisIntegrate ¶
ClenshawCurtisIntegrate approximates the integral of f over [a, b] using an (N+1)-point Clenshaw-Curtis rule.
func ClenshawCurtisNodes ¶
ClenshawCurtisNodes returns the N+1 Clenshaw-Curtis nodes, i.e. the Chebyshev-Gauss-Lobatto nodes on [-1, 1].
func ClenshawCurtisWeights ¶
ClenshawCurtisWeights returns the N+1 Clenshaw-Curtis quadrature weights on [-1, 1] for the weight function 1 (Trefethen's clencurt algorithm). N must be >= 1.
func ClenshawEval ¶
ClenshawEval evaluates the Chebyshev series sum_k coeffs[k]*T_k(x) using Clenshaw's backward recurrence, which is numerically stable on [-1, 1].
func ClenshawEvalDerivative ¶
ClenshawEvalDerivative evaluates the derivative of the Chebyshev series sum_k coeffs[k]*T_k at x.
func CoefficientDecayRate ¶
CoefficientDecayRate estimates the exponential decay rate of the magnitudes of Chebyshev (or other) spectral coefficients, |c_k| ~ C*exp(-r*k).
func ComplexImag ¶
func ComplexImag(x []complex128) []float64
ComplexImag returns the imaginary parts of x.
func ComplexReal ¶
func ComplexReal(x []complex128) []float64
ComplexReal returns the real parts of x.
func ConvergenceOrder ¶
ConvergenceOrder estimates the algebraic order p in err ~ C*h^p by a least-squares fit of log(err) against log(h).
func CopyMatrix ¶
CopyMatrix returns a deep copy of a [][]float64.
func DCT1 ¶
DCT1 computes the type-I discrete cosine transform of the N+1 input samples (N = len(x)-1): X_k = 0.5*(x_0 + (-1)^k x_N) + sum_{n=1}^{N-1} x_n cos(pi*n*k/N), for k = 0..N.
func DFT ¶
func DFT(x []complex128) []complex128
DFT computes the (unnormalized) discrete Fourier transform X_k = sum_j x_j exp(-2*pi*i*j*k/N) directly in O(N^2).
func DST1 ¶
DST1 computes the type-I discrete sine transform of the N-1 interior samples (indexing n, k = 1..N-1): X_k = sum_{n=1}^{N-1} x_{n-1} sin(pi*n*k/N).
func Determinant ¶
Determinant returns the determinant of a square matrix.
func DifferentiateNodal ¶
DifferentiateNodal applies a differentiation matrix D to nodal values u, returning D*u.
func DotProduct ¶
DotProduct returns the Euclidean inner product of x and y.
func FFT ¶
func FFT(x []complex128) []complex128
FFT computes the discrete Fourier transform. When the length is a power of two it uses a radix-2 Cooley-Tukey algorithm; otherwise it falls back to the direct DFT. The normalization matches DFT.
func FFTReal ¶
func FFTReal(x []float64) []complex128
FFTReal computes the DFT of a real-valued signal.
func FejerIntegrate ¶
FejerIntegrate approximates the integral of f over [a, b] with an n-point Fejér first rule.
func FejerNodes ¶
FejerNodes returns the n nodes of Fejér's first quadrature rule, the Chebyshev-Gauss nodes x_k = cos((k+1/2)*pi/n).
func FejerWeights ¶
FejerWeights returns the n weights of Fejér's first quadrature rule on [-1, 1] for the weight function 1.
func FourierCoefficients ¶
func FourierCoefficients(values []float64) []complex128
FourierCoefficients returns the complex Fourier coefficients c_k = (1/N) sum_j f_j exp(-i*k*x_j) of the values sampled on the periodic Fourier grid, in DFT ordering (k = 0..N-1).
func FourierDiffMatrix ¶
FourierDiffMatrix returns the N-by-N first-order Fourier differentiation matrix on the periodic grid x_j = 2*pi*j/N. It is implemented for even N, following Trefethen's program 4.
func FourierDiffMatrix2 ¶
FourierDiffMatrix2 returns the N-by-N second-order Fourier differentiation matrix on the periodic grid, for even N.
func FourierDiffMatrixOrder ¶
FourierDiffMatrixOrder returns the m-th order Fourier differentiation matrix. For m == 1 and m == 2 the closed-form matrices are used; for higher orders the matrix power of the first-order matrix is returned.
func FourierDifferentiate ¶
FourierDifferentiate returns the first derivative of the trigonometric interpolant at the grid nodes.
func FourierDifferentiate2 ¶
FourierDifferentiate2 returns the second derivative of the trigonometric interpolant at the grid nodes.
func FourierDifferentiateOrder ¶
FourierDifferentiateOrder returns the m-th derivative of the trigonometric interpolant, evaluated at the periodic grid nodes.
func FourierInterpolate ¶
FourierInterpolate evaluates, at x, the band-limited trigonometric interpolant of values sampled at the N periodic nodes x_j = 2*pi*j/N.
func FourierNodes ¶
FourierNodes returns the N equispaced periodic nodes x_j = 2*pi*j/N on [0, 2*pi) for j = 0..N-1.
func FourierNodesInterval ¶
FourierNodesInterval returns N equispaced periodic nodes on [a, b).
func FourierWavenumbers ¶
FourierWavenumbers returns the signed wavenumbers associated with the length-N periodic FFT ordering: 0, 1, ..., N/2-1, -N/2, ..., -1 for even N.
func FourierWeights ¶
FourierWeights returns the trapezoidal (spectrally accurate for periodic functions) quadrature weights 2*pi/N on the Fourier grid.
func FrobeniusNorm ¶
FrobeniusNorm returns the Frobenius norm of a.
func GalerkinProjectChebyshev ¶
GalerkinProjectChebyshev is a convenience wrapper for ChebyshevL2Projection.
func GalerkinProjectLegendre ¶
GalerkinProjectLegendre returns the first n+1 Legendre coefficients of the L2-orthogonal projection of f (see LegendreProjection).
func GaussChebyshevIntegrate ¶
GaussChebyshevIntegrate approximates the integral over [-1, 1] of f(x) with respect to the Chebyshev weight 1/sqrt(1-x^2), using an n-point Gauss-Chebyshev rule.
func GaussLegendreIntegrate ¶
GaussLegendreIntegrate approximates the integral of f over [a, b] using an n-point Gauss-Legendre rule.
func HeatSolveChebyshev ¶
func HeatSolveChebyshev(u0 func(float64) float64, nu, dt float64, steps, N int, a, b float64) (nodes, u []float64, err error)
HeatSolveChebyshev integrates u_t = nu*u_xx on [a, b] with homogeneous Dirichlet boundary conditions from the initial function u0 to time steps*dt, using Crank-Nicolson in time and Chebyshev collocation in space. It returns the physical nodes and the full nodal solution (boundaries included, set to zero).
func HeatSolveFourier ¶
HeatSolveFourier evolves the periodic heat equation u_t = nu*u_xx on [0, L) exactly in Fourier space from the initial data u0 (sampled on the Fourier grid) to time t, returning the nodal solution.
func HeatStepChebyshevCN ¶
func HeatStepChebyshevCN(uInterior []float64, nu, dt float64, N int, a, b float64) ([]float64, error)
HeatStepChebyshevCN performs one Crank-Nicolson time step of size dt for the heat equation u_t = nu*u_xx on [a, b] with homogeneous Dirichlet boundary conditions, given the current interior nodal values (length N-1). It returns the updated interior values.
func Helmholtz2D ¶
func Helmholtz2D(f, g func(x, y float64) float64, k2 float64, Nx, Ny int, ax, bx, ay, by float64) (xn, yn []float64, U [][]float64, err error)
Helmholtz2D solves u_xx + u_yy + k2*u = f on [ax, bx] x [ay, by] with Dirichlet data g, by Chebyshev collocation.
func HelmholtzSolve1D ¶
func HelmholtzSolve1D(f func(float64) float64, k2 float64, N int, a, b, ua, ub float64) (nodes, u []float64, err error)
HelmholtzSolve1D solves the Dirichlet problem u” + k2*u = f on [a, b] with u(a) = ua and u(b) = ub, using Chebyshev collocation. k2 may be negative (modified Helmholtz).
func IDFT ¶
func IDFT(x []complex128) []complex128
IDFT computes the inverse discrete Fourier transform x_j = (1/N) sum_k X_k exp(2*pi*i*j*k/N) directly in O(N^2).
func IFFT ¶
func IFFT(x []complex128) []complex128
IFFT computes the inverse discrete Fourier transform, matching IDFT.
func IntegrateFunction ¶
IntegrateFunction evaluates f at the given nodes and contracts with the weights.
func IntegrateWeighted ¶
IntegrateWeighted returns the dot product of quadrature weights and nodal function values.
func InterpolationError ¶
InterpolationError returns the maximum absolute difference between f and its interpolant (through (nodes, f(nodes))) sampled at the given evaluation points.
func IntervalScale ¶
IntervalScale returns d/dx of the reference-to-physical map, i.e. the factor 2/(b-a) that multiplies reference derivatives to obtain physical ones.
func IsSpectrallyConverging ¶
IsSpectrallyConverging reports whether the errors decay at least as fast as exp(-rate*n) over the tested resolutions, using SpectralConvergenceRate.
func JacobiEigenSymmetric ¶
JacobiEigenSymmetric computes all eigenvalues and eigenvectors of a real symmetric matrix using the cyclic Jacobi method. The returned eigenvalues are sorted ascending and the columns of the returned matrix are the corresponding orthonormal eigenvectors.
func LUDecompose ¶
LUDecompose computes an in-place-style LU factorization with partial pivoting. It returns the combined LU matrix, the pivot permutation and the sign of the permutation. The input is not modified.
func LagrangeBasis ¶
LagrangeBasis evaluates the j-th Lagrange cardinal polynomial for the given nodes at x.
func LagrangeInterpolate ¶
LagrangeInterpolate evaluates the Lagrange interpolating polynomial through (nodes, values) at x directly from the cardinal-polynomial definition.
func LegendreGaussLobattoNodes ¶
LegendreGaussLobattoNodes returns just the N+1 Legendre-Gauss-Lobatto nodes.
func LegendreGaussLobattoNodesWeights ¶
LegendreGaussLobattoNodesWeights returns the N+1 Legendre-Gauss-Lobatto nodes and weights on [-1, 1] (endpoints included). Nodes are in ascending order. N must be >= 1.
func LegendreGaussLobattoWeights ¶
LegendreGaussLobattoWeights returns just the N+1 Legendre-Gauss-Lobatto weights.
func LegendreGaussNodes ¶
LegendreGaussNodes returns just the n Gauss-Legendre nodes on [-1, 1].
func LegendreGaussNodesWeights ¶
LegendreGaussNodesWeights returns the n Gauss-Legendre nodes and weights on [-1, 1], computed by Newton iteration on P_n. Nodes are returned in ascending order.
func LegendreGaussWeights ¶
LegendreGaussWeights returns just the n Gauss-Legendre weights on [-1, 1].
func LegendreNormSquared ¶
LegendreNormSquared returns the squared L2([-1,1]) norm of P_n, 2/(2n+1).
func LegendreP ¶
LegendreP evaluates the Legendre polynomial P_n at x via the three-term recurrence.
func LegendrePDerivative ¶
LegendrePDerivative evaluates the derivative P_n'(x).
func LegendrePSecondDerivative ¶
LegendrePSecondDerivative evaluates P_n”(x) using the Legendre differential equation.
func LegendrePValues ¶
LegendrePValues evaluates P_n at every point of xs.
func LegendreProjection ¶
LegendreProjection returns the first n+1 Legendre coefficients of f, i.e. the L2([-1,1]) projection onto span{P_0,...,P_n}, computed with an accurate Gauss-Legendre rule. c_k = (2k+1)/2 * integral_{-1}^{1} f(x) P_k(x) dx.
func LegendreSeriesEval ¶
LegendreSeriesEval evaluates the Legendre series sum_k coeffs[k]*P_k at x.
func LegendreVandermonde ¶
LegendreVandermonde returns the len(nodes) x (degree+1) matrix with entries P_j(nodes[i]).
func LinSpace ¶
LinSpace returns n equally spaced points from a to b inclusive. For n == 1 it returns {a}.
func MapFromInterval ¶
MapFromInterval maps a slice of nodes on [a, b] back to [-1, 1].
func MapToInterval ¶
MapToInterval maps a slice of reference nodes on [-1, 1] to [a, b].
func MatPow ¶
MatPow returns a raised to the non-negative integer power p (a must be square). MatPow(a,0) is the identity.
func NewtonDividedDifferences ¶
NewtonDividedDifferences returns the Newton divided-difference coefficients for the data (nodes, values).
func NewtonEval ¶
NewtonEval evaluates the Newton form of the interpolating polynomial with the given divided-difference coefficients and nodes at x.
func Poisson2D ¶
func Poisson2D(f, g func(x, y float64) float64, Nx, Ny int, ax, bx, ay, by float64) (xn, yn []float64, U [][]float64, err error)
Poisson2D solves the Dirichlet problem u_xx + u_yy = f on the rectangle [ax, bx] x [ay, by] with boundary values given by g(x, y). It uses Chebyshev collocation with Nx+1 by Ny+1 nodes and returns the x-nodes, y-nodes and the solution as a matrix U with U[i][j] = u(x_i, y_j).
func Poisson2DFourier ¶
func Poisson2DFourier(f func(x, y float64) float64, Nx, Ny int, Lx, Ly float64) (xn, yn []float64, U [][]float64)
Poisson2DFourier solves the doubly periodic problem u_xx + u_yy = f on [0, Lx) x [0, Ly) with a Fourier spectral method. The source f must have zero mean; the returned solution also has zero mean. It returns the x-nodes, y-nodes and the solution matrix U[i][j] = u(x_i, y_j).
func PoissonSolve1D ¶
func PoissonSolve1D(f func(float64) float64, N int, a, b, ua, ub float64) (nodes, u []float64, err error)
PoissonSolve1D solves the Dirichlet problem u” = f on [a, b] with u(a) = ua and u(b) = ub, using an (N+1)-node Chebyshev collocation method. It returns the physical nodes and the nodal solution values (ordered from x=b down to x=a, matching the Chebyshev-Gauss-Lobatto ordering).
Example ¶
// Solve u'' = -pi^2 sin(pi x) on [-1,1] with u(-1)=u(1)=0; exact sin(pi x).
f := func(x float64) float64 { return -math.Pi * math.Pi * math.Sin(math.Pi*x) }
_, u, _ := PoissonSolve1D(f, 20, -1, 1, 0, 0)
// The value at the centre node (x=0) should be ~sin(0)=0.
fmt.Printf("%.4f\n", u[10])
Output: 0.0000
func PoissonSolve1DFourier ¶
PoissonSolve1DFourier solves the periodic problem u” = f on [0, L) using a Fourier spectral method. The source f must have zero mean for a solution to exist; the returned solution also has zero mean. It returns the grid nodes and nodal solution values.
func PolynomialInterpolate ¶
PolynomialInterpolate builds barycentric weights for the given nodes and evaluates the interpolant at x. It is convenient for one-off evaluations.
func RealToComplex ¶
func RealToComplex(x []float64) []complex128
RealToComplex returns a complex slice whose real parts are x and imaginary parts are zero.
func RelativeL2Error ¶
RelativeL2Error returns ||a-b||_2 / ||b||_2, guarding against division by zero.
func SimpsonIntegrate ¶
SimpsonIntegrate approximates the integral of f over [a, b] with the composite Simpson rule on n subintervals (n must be even and >= 2).
func SolveLinearSystem ¶
SolveLinearSystem solves A*x = b for a square, non-singular A.
func SolveMatrix ¶
SolveMatrix solves A*X = B for X, where B (and X) have multiple columns.
func SpectralConvergenceRate ¶
SpectralConvergenceRate estimates the exponential decay rate r in err ~ C*exp(-r*n) by a least-squares fit of log(err) against n. A larger positive r indicates faster spectral convergence. Non-positive errors are ignored.
func SpectralDerivativeChebyshev ¶
SpectralDerivativeChebyshev returns the nodal derivative values of a function sampled at the Chebyshev-Gauss-Lobatto nodes on [a, b].
func TrapezoidalIntegrate ¶
TrapezoidalIntegrate approximates the integral of f over [a, b] with the composite trapezoidal rule on n subintervals.
func TrapezoidalWeights ¶
TrapezoidalWeights returns the composite trapezoidal weights for n+1 equally spaced points on [a, b].
func UniformNodes ¶
UniformNodes returns n+1 equally spaced nodes on [a, b].
func VectorFill ¶
VectorFill returns a vector of length n with every entry set to v.
func VectorHadamard ¶
VectorHadamard returns the elementwise product of x and y.
Types ¶
type Matrix ¶
Matrix is a dense, row-major real matrix used by the linear-algebra helpers.
func NewMatrixFrom ¶
NewMatrixFrom wraps an existing row-major slice as a Matrix. The slice is referenced, not copied.