### SumSpace Example: Decomposing a Function Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/spaces.md Demonstrates how SumSpace decomposes a function into its components and verifies coefficient interlacing. Use this to understand how functions are represented as sums of spaces. ```julia julia> x = Fun(identity, -1..1); julia> f = cos(x-0.1)*sqrt(1-x^2) + exp(x); julia> space(f) == JacobiWeight(0.5, 0.5, Chebyshev(-1..1)) + Chebyshev(-1..1) true julia> a, b = components(f); julia> a(0.2) ≈ cos(0.2-0.1)*sqrt(1-0.2^2) true julia> b(0.2) ≈ exp(0.2) true julia> f(0.2) ≈ a(0.2) + b(0.2) true julia> norm(coefficients(f)[1:2:end] - coefficients(a)) 0.0 julia> norm(coefficients(f)[2:2:end] - coefficients(b)) 0.0 ``` -------------------------------- ### Constructing Fredholm Integral Operator Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/operators.md Illustrates the construction of a Fredholm integral operator of the second kind using the identity and a definite integral operator. The example verifies the operator's action on a function. ```jldoctest julia> x = Fun(); julia> Σ = DefiniteIntegral(Chebyshev()); julia> L = I + exp(x)*Σ; julia> u = cos(10x^2); julia> (L * u)(0.1) ≈ u(0.1) + exp(0.1) * sum(u) true ``` -------------------------------- ### ArraySpace Example: Evaluating Array of Functions Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/spaces.md Shows how ArraySpace handles an array of functions, verifying evaluation and coefficient extraction. This is useful for managing collections of functions that are evaluated element-wise. ```julia julia> x = Fun(identity, -1..1); julia> f = [exp(x); sqrt(1-x^2)*cos(x-0.1)]; julia> space(f) isa ApproxFun.ArraySpace true julia> a, b = components(f); julia> norm(f(0.5) - [a(0.5); b(0.5)]) 0.0 julia> norm(coefficients(f)[1:2:end] - coefficients(a)) 0.0 julia> norm(coefficients(f)[2:2:end] - coefficients(b)) 0.0 ``` -------------------------------- ### Indefinite Integration with cumsum Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/README.md Demonstrates indefinite integration using the cumsum function on a 'Fun' type. Adjusts for the constant of integration by adding the function's value at the start of the interval. ```julia g = cumsum(f) g = g + f(-1) norm(f-g) ``` -------------------------------- ### Inspect Space and Domain of Fun Objects Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Use `space(f)` to get the function space and `domain(f)` to retrieve the domain of a Fun object. The space can change based on operations like division. ```julia using ApproxFun x = Fun(identity, -1..1) f = exp(x) space(f) # Chebyshev(-1..1) domain(f) # -1..1 g = f / sqrt(1 - x^2) space(g) # JacobiWeight(-0.5, -0.5, Chebyshev(-1..1)) # Absolute value infers piecewise space f2 = Fun(x -> cospi(5x), -1..1) g2 = abs(f2) space(g2) isa ContinuousSpace # true domain(g2) isa PiecewiseSegment # true ``` -------------------------------- ### Get number of coefficients for the sum of two Funs Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/index.md Calculates the number of coefficients required for the sum of two 'Fun' objects. This demonstrates how basis compatibility affects the resulting approximation. ```julia ncoefficients(a+b) ``` -------------------------------- ### Get number of coefficients for a Fun Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/index.md Retrieves the number of coefficients used to represent a 'Fun' object. This is useful for understanding the complexity of the approximation. ```julia ncoefficients(a) ``` -------------------------------- ### Solve Singularly Perturbed Boundary Value Problem with Dirichlet Operator Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/equations.md Solves the same singularly perturbed boundary value problem as the previous example, but using the `Dirichlet` operator for the boundary conditions. This offers an alternative syntax for specifying boundary conditions. ```julia u = [Dirichlet(); ϵ*𝒟^2-x*𝒟+I] \ [[1,2],0] u(0.1) ≈ 0.05 # compare with the analytical solution ``` -------------------------------- ### Get number of coefficients for a Fun with a different function Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/index.md Retrieves the number of coefficients for a 'Fun' object created with a different function. Useful for comparing approximation complexities. ```julia b = Fun(x->cos(10cos(x^2)),Chebyshev()) ``` -------------------------------- ### Get number of coefficients for the product of two Funs Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/index.md Determines the number of coefficients needed for the product of two 'Fun' objects. This highlights that multiplication often requires more coefficients than the operands. ```julia ncoefficients(a*b) ``` -------------------------------- ### Multiplication Operator Between Different Spaces Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/operators.md Demonstrates that `Multiplication` operators can map between different function spaces. This example shows multiplication of a function in `SinSpace` by `cos(θ)`, resulting in a function in `SinSpace`. ```jldoctest julia> cosθ = Fun(cos, CosSpace()); julia> sinθ = Fun(sin, SinSpace()); julia> sin2θ = Fun(x->sin(2x), SinSpace()); julia> cosθM = Multiplication(cosθ, SinSpace()); julia> cosθM * 2sinθ ≈ sin2θ true ``` -------------------------------- ### Multiplication Operator on Chebyshev Space Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/operators.md Illustrates the `Multiplication` operator, which multiplies a function by a given function. This example shows multiplication in the `Chebyshev` space and compares matrix entries with direct multiplication. ```jldoctest julia> x = Fun(); julia> M = Multiplication(1 + 2x + x^2, Chebyshev()); julia> coefficients(M * x) == coefficients((1 + 2x + x^2) * x) == M[1:4,1:2] * coefficients(x) true ``` -------------------------------- ### Convenience constructors for Fun Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/constructors.md Demonstrates various equivalent ways to construct a Fun object using default spaces and domains, including specifying the function, space, and domain explicitly or implicitly. ```julia Fun(exp,Chebyshev(Interval(-1,1))) ``` ```julia Fun(exp,Chebyshev(ChebyshevInterval())) ``` ```julia Fun(exp,Chebyshev(-1..1)) ``` ```julia Fun(exp,Chebyshev()) ``` ```julia Fun(exp,-1..1) ``` ```julia Fun(exp,ChebyshevInterval()) ``` ```julia Fun(exp,Interval(-1,1)) ``` ```julia Fun(exp) ``` -------------------------------- ### Create Function Approximations in Julia Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/README.md Demonstrates creating approximations of functions using the Fun type. Requires importing necessary packages. ```julia using LinearAlgebra, SpecialFunctions, Plots, ApproxFun x = Fun(identity,0..10) f = sin(x^2) g = cos(x) ``` -------------------------------- ### QR Factorization of Operators with ApproxFun Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Demonstrates caching QR factorization of an operator for efficient repeated solves with different right-hand sides. ```julia using ApproxFun x = Fun() ϵ = 1/70 L = [Evaluation(-1); Evaluation(1); ϵ*𝒟^2 - x*𝒟 + I] # Cache QR factorization for repeated right-hand sides Q = qr(L) u1 = Q \ [1, 2, 0] u2 = Q \ [2, 3, 0] # second solve is much faster (Q is already cached) u1(0.1) ≈ 0.05 # true ``` -------------------------------- ### Define Domains in ApproxFun.jl Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Demonstrates how to define periodic, infinite, and combined domains using ApproxFun.jl. Supports operations like union and intersection. ```julia PeriodicSegment() # [0, 2π) Circle(0, 1) # unit circle Circle(3, 0.5) # circle at centre 3, radius 0.5 Line() # real line Ray(0) # [0, ∞) d1 = ChebyshevInterval() ∪ Circle(3, 0.5) # union d2 = Interval(0, 2) ∩ ChebyshevInterval() # returns Interval(0,1) rect = Interval(0, 1)^2 # 2D rectangle [0,1]² Space(ChebyshevInterval()) # Chebyshev Space(PeriodicSegment()) # Fourier ``` -------------------------------- ### Constructing a Fun Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/library.md Explains how to construct a 'Fun' object, which represents a function approximation. ```APIDOC ## Constructing a Fun ### Fun Constructor for creating a Fun object. ### ones Creates a Fun object representing a function of all ones. ### zeros Creates a Fun object representing a function of all zeros. ``` -------------------------------- ### Manual Interpolation at Arbitrary Grid (ApproxFun) Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Shows how to manually interpolate a function at an arbitrary grid using Vandermonde matrices in ApproxFun. ```julia # Interpolation at an arbitrary grid using Vandermonde matrix S = Chebyshev(1..2) n = 50 p = range(1, stop=2, length=n) v = exp.(p) V = Array{Float64}(undef, n, n) for k in 1:n V[:, k] = Fun(S, [zeros(k-1); 1]).(p) end f = Fun(S, V \ v) f(1.1) ≈ exp(1.1) # true ``` -------------------------------- ### Solve BVP using Dirichlet Shorthand Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Demonstrates solving a boundary value problem using the Dirichlet shorthand for boundary conditions in ApproxFun. ```julia # Using Dirichlet shorthand u2 = [Dirichlet(); ϵ*𝒟^2 - x*𝒟 + I] \ [[1, 2], 0] u2(0.1) ≈ 0.05 # true ``` -------------------------------- ### Construct Fun Objects in ApproxFun.jl Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Learn how to construct Fun objects adaptively or explicitly using different domains and spaces. The identity shorthand is also shown. ```julia using ApproxFun, LinearAlgebra # Adaptive construction on default Chebyshev space (-1..1) f = Fun(exp) # exp(x) on -1..1 f(0.5) # ≈ exp(0.5) # Explicit domain f = Fun(x -> sin(x^2), 0..10) f(0.1) # ≈ sin(0.01) # Explicit space f = Fun(exp, Chebyshev(0..2)) ncoefficients(f) # number of coefficients used # Explicit coefficients: represents 1 + 2z + 3z^2 f = Fun(Taylor(), [1, 2, 3]) f(0.1) ≈ 1 + 2*0.1 + 3*0.1^2 # true # identity shorthand x = Fun() # identity on -1..1 x = Fun(0..1) # identity on 0..1 ``` -------------------------------- ### Combine Operators in ApproxFun Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Shows how to combine operators like Derivative and Identity using addition and composition in ApproxFun. ```julia using ApproxFun # Add second derivative operator to identity on Fourier space D2 = Derivative(Fourier(), 2) (D2 + I) * Fun(x -> cos(2x), Fourier()) ≈ Fun(x -> -3cos(2x), Fourier()) # true ``` -------------------------------- ### Manipulate and Analyze Function Approximations Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/README.md Shows how to add, square, find roots, and plot approximations of functions. Requires the 'Fun' type and plotting capabilities. ```julia h = f + g^2 r = roots(h) rp = roots(h') plot(h; label="f + g^2") scatter!(r, h.(r); label="roots") scatter!(rp, h.(rp); label="extrema") ``` -------------------------------- ### Convenience constructors for identity Fun Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/constructors.md Shows equivalent ways to construct a Fun object representing the identity function using default domains and spaces. ```julia x = Fun(identity, -1..1) ``` ```julia x = Fun(-1..1) ``` ```julia x = Fun(identity) ``` ```julia x = Fun() ``` -------------------------------- ### Compare product approximation with direct evaluation Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/index.md Verifies the accuracy of the 'Fun' multiplication by comparing the evaluated product with the product of evaluated 'Fun' objects at a specific point. This demonstrates the precision of adaptive approximation. ```julia a(0.1)*b(0.1) - (a*b)(0.1) ``` -------------------------------- ### Chebyshev and CosSpace Conversion Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/spaces.md Demonstrates converting a function from Chebyshev space to CosSpace using its coefficients. This highlights the intrinsic link between the two spaces. ```jldoctest julia> f = Fun(exp, Chebyshev()); julia> g = Fun(CosSpace(), coefficients(f)); # specify the coefficients directly julia> f(cos(0.1)) ≈ exp(cos(0.1)) true julia> g(0.1) ≈ exp(cos(0.1)) true ``` -------------------------------- ### Manual Interpolation at Default Grid (ApproxFun) Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Performs interpolation at the default Chebyshev grid points for efficient function representation in ApproxFun. ```julia using ApproxFun # Interpolation at the default Chebyshev grid (fast path) S = Chebyshev(1..2) p = points(S, 20) v = exp.(p) f = Fun(S, ApproxFun.transform(S, v)) f(1.1) ≈ exp(1.1) # true ``` -------------------------------- ### Representing a Bivariate Function with ProductFun Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/internals/multivariate.md Constructs a bivariate function using ProductFun, an alternative to Fun for representing multivariate functions. The space must be an AbstractProductSpace, and coefficients are a Vector of 1D Fun objects. ```julia f=ProductFun((x,y)->exp(-x^2-y^2),ChebyshevInterval()^2) ``` -------------------------------- ### Verifying Space of ProductFun Coefficients Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/internals/multivariate.md Ensures that the space of each 1D function within a ProductFun's coefficient list matches the corresponding column space of the ProductFun's overall space. ```julia space(f.coefficients[k]) == columnspace(f.space, k) ``` -------------------------------- ### Defining Column Spaces for AbstractProductSpace Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/internals/multivariate.md Illustrates how columnspace is used with AbstractProductSpace to define different bases for each dimension. This is crucial for more general product spaces beyond simple tensor products. ```julia columnspace(myproductspace,1) == JacobiWeight(0,0,Legendre()) ``` ```julia columnspace(myproductspace,2) == JacobiWeight(0.5,0.5,Jacobi(1,1)) ``` ```julia columnspace(myproductspace,k) == JacobiWeight((k-1)/2,(k-1)/2,Jacobi(k-1,k-1)) ``` -------------------------------- ### Solve Initial Value Problem (IVP) with ApproxFun Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Solves an initial value problem for a first-order ODE using ApproxFun by constructing a system with an evaluation condition. ```julia # 2. IVP: u' = t*u, u(0) = 1 → solution: exp(t^2/2) t = Fun(0..1) u = [Evaluation(0); 𝒟 - t] \ [1; 0] u(0) ≈ 1 # true norm(u' - t*u) < eps() # true ``` -------------------------------- ### Construct 2D Fun with explicit coefficients Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/constructors.md Constructs a 2D Fun object in a product space (CosSpace squared) by explicitly providing coefficients. The coefficients correspond to a sum of products of basis functions. ```julia f = Fun(CosSpace()^2, [1,2,3]); ``` -------------------------------- ### Operator Conversion in ApproxFun Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Demonstrates using the Conversion operator to change the function space of an operator in ApproxFun. ```julia # Conversion operator between spaces f = Fun(x -> x^3, Chebyshev()) D = Derivative(Chebyshev()) C = Conversion(Chebyshev(), Ultraspherical(1)) (D + C) * f ≈ Fun(x -> x^3 + 3x^2) # true ``` -------------------------------- ### Operator Addition with Space Conversion Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/operators.md Shows how ApproxFun handles operator addition when domain and range spaces differ, automatically introducing conversion operators. This applies to both Derivative + Identity and Derivative + Conversion. ```jldoctest julia> f = Fun(x->x^3, Chebyshev()); julia> D = Derivative(Chebyshev()); julia> (D + I) * f ≈ Fun(x->x^3 + 3x^2) true julia> C = Conversion(Chebyshev(), Ultraspherical(1)); julia> (D + C) * f ≈ Fun(x->x^3 + 3x^2) true ``` -------------------------------- ### Derivative Operator with Space Promotion Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/operators.md Shows how the generic `Derivative()` operator infers the function's space, simplifying operator construction. The domain space is promoted automatically when multiplying with a function. ```jldoctest julia> f = Fun(cos, Chebyshev(0..1)); julia> (𝒟 * f)(0.1) ≈ -sin(0.1) true julia> f = Fun(cos, Fourier()); julia> (𝒟 * f)(0.1) ≈ -sin(0.1) true ``` -------------------------------- ### Apply Special Functions to Approximations Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/README.md Illustrates applying various special functions (erf, besselj, airyai) and mathematical operations (asin, +, *) to 'Fun' type objects. ```julia x = Fun() f = erf(x) g = besselj(3,exp(f)) h = airyai(10asin(f)+2g) ``` -------------------------------- ### Create Fun from a function and space Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/index.md Constructs a 'Fun' object using a specified space (basis) and a Julia function. This allows control over the approximation basis. ```julia a = Fun(cos,Chebyshev()) ``` -------------------------------- ### Create a Rectangle Domain Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/domains.md Constructs a rectangular domain by taking the Cartesian product of two intervals. The `^2` operator is a shorthand for `Interval(0,1) × Interval(0,1)`. ```julia rect=Interval(0,1)^2 ``` -------------------------------- ### Create Fun adaptively from a function Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/index.md Constructs a 'Fun' object by adaptively determining the necessary coefficients to approximate a given Julia function to machine precision. Use this for functions where explicit coefficients are not readily available. ```julia Fun(exp) ``` -------------------------------- ### Fredholm Operator via Multiplication and Integral Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/operators.md Demonstrates constructing a Fredholm integral operator by multiplying a function with a definite integral functional. This method is useful when the multiplier is a function. ```jldoctest julia> x = Fun(); julia> Σ = DefiniteIntegral(); julia> M = Multiplication(exp(x)); julia> L = M * Σ; julia> L * Fun(x->3x^2/2, Chebyshev()) ≈ Fun(exp, Chebyshev()) true ``` -------------------------------- ### Create Union of Domains Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/domains.md Combines two domains into a single union domain. Use the `∪` operator or the `union` function. ```julia ChebyshevInterval() ∪ Circle(3,0.5) # equivalent to union(ChebyshevInterval(),Circle(3,0.5)) ``` -------------------------------- ### Verify space of piecewise domain Fun Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/constructors.md Verifies that the space of a Fun object with a piecewise domain is a ContinuousSpace over that specific piecewise domain. ```julia space(g) == ContinuousSpace(PiecewiseSegment(reverse(segments))) ``` -------------------------------- ### Create Fun from function and domain Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/constructors.md Creates a Fun object by approximating a given function over a specified domain. The function space is inferred. ```julia f = exp(x); ``` -------------------------------- ### Solve Periodic ODE using ApproxFun Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Solves a first-order linear ODE with periodic boundary conditions using ApproxFun's operator overloading. ```julia using ApproxFun, LinearAlgebra # 1. Periodic ODE: u' + 0.1u = cos(θ) b = Fun(cos, Fourier()) u = (𝒟 + 0.1I) \ b t = 0.6 u(t) ≈ (0.1cos(t) + sin(t)) / (1 + 0.1^2) # true ``` -------------------------------- ### Compose Operators for Linear ODEs Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Demonstrates composing operators like Multiplication and Summation to form a linear operator for solving ODEs. ```julia using ApproxFun x = Fun() M = Multiplication(exp(x)) L2 = M * Σ L2 * Fun(x -> 3x^2/2, Chebyshev()) ≈ Fun(exp, Chebyshev()) # true ``` -------------------------------- ### Bivariate Fun Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/library.md Documentation for handling bivariate functions. ```APIDOC ## Bivariate Fun ### LowRankFun Represents a low-rank bivariate function. ### ProductFun Represents a product of two functions. ``` -------------------------------- ### Solve Singularly Perturbed BVP with ApproxFun Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Solves a second-order singularly perturbed boundary value problem using ApproxFun by stacking boundary conditions and the differential operator. ```julia # 3. Singularly perturbed BVP: ε*u'' - x*u' + u = 0, u(±1) = [1,2] ϵ = 1/70 x = Fun() u = [Evaluation(-1); Evaluation(1); ϵ*𝒟^2 - x*𝒟 + I] \ [1, 2, 0] u(0.1) ≈ 0.05 # true ``` -------------------------------- ### ApproxFun.jl Spaces: Chebyshev, Fourier, Jacobi, Ultraspherical Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Illustrates the use of different space types for function representation in ApproxFun.jl, including Chebyshev, Fourier, Laurent, Jacobi, Ultraspherical, CosSpace, SinSpace, and 2D TensorSpace. Default spaces are Chebyshev for non-periodic and Fourier/Laurent for periodic functions. ```julia using ApproxFun # Chebyshev space (default) f = Fun(exp, Chebyshev()) # expansion in T_k(x) # Fourier space — coefficients ordered [f₀, f₁ˢ, f₁ᶜ, f₂ˢ, ...] f = Fun(Fourier(), [1, 2, 3, 4]) f(0.1) ≈ 1 + 2sin(0.1) + 3cos(0.1) + 4sin(2*0.1) # true # Laurent space — complex exponentials f = Fun(Laurent(), [1, 2, 3, 4]) f(0.1) ≈ 1 + 2exp(-im*0.1) + 3exp(im*0.1) + 4exp(-2im*0.1) # true # Jacobi space: orthogonal w.r.t. (1+x)^b * (1-x)^a f = Fun(x -> (1-x)^0.5, Jacobi(0.5, 0.0)) # Ultraspherical space (used internally for diff. equations) f = Fun(sin, Ultraspherical(1)) # CosSpace and SinSpace f = Fun(θ -> exp(cos(θ)), CosSpace()) # 2D TensorSpace f = Fun(CosSpace()^2, [1, 2, 3]) f(1, 2) ≈ 1cos(0*1)*cos(0*2) + 2cos(0*1)*cos(1*2) + 3cos(1*1)*cos(0*2) # true ``` -------------------------------- ### Solve First-Order ODE with Periodic Boundary Conditions Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/equations.md Solves a first-order ordinary differential equation with periodic boundary conditions using the `A\b` syntax. `𝒟` represents the derivative operator and `I` represents the identity operator. ```julia using ApproxFun, LinearAlgebra b = Fun(cos, Fourier()) c = 0.1 u = (𝒟 + c*I) \ b t = 0.6 # choose a point to verify the solution u(t) ≈ (c*cos(t)+sin(t)) / (1+c^2) # exact solution ``` -------------------------------- ### Add Derivative and Identity Operators Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/operators.md Demonstrates adding a second derivative operator to the identity operator in Fourier space. Ensure domain and range spaces are compatible for algebraic manipulation. ```jldoctest julia> D2 = Derivative(Fourier(),2); julia> (D2 + I) * Fun(x -> cos(2x), Fourier()) ≈ Fun(x -> -3cos(2x), Fourier()) true ``` -------------------------------- ### Fourier Space Coefficient Ordering Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/spaces.md Illustrates the coefficient ordering for the Fourier space, which represents sums of sines and cosines. The coefficients are ordered as [f0, f1_sin, f1_cos, f2_sin, f2_cos, ...]. ```jldoctest julia> f = Fun(Fourier(), [1,2,3,4]); julia> f(0.1) ≈ 1 + 2sin(0.1) + 3cos(0.1) + 4sin(2*0.1) true ``` -------------------------------- ### Inbuilt Spaces Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/library.md Lists and describes the various inbuilt function spaces available in the library. ```APIDOC ## Inbuilt spaces ### SequenceSpace A space for sequences. ### ConstantSpace A space for constant functions. ### Chebyshev Chebyshev polynomial space. ### Hermite Hermite polynomial space. ### Jacobi Jacobi polynomial space. ### Laguerre Laguerre polynomial space. ### Ultraspherical Ultraspherical polynomial space. ### Taylor Taylor polynomial space. ### Hardy Hardy space. ### Fourier Fourier series space. ### Laurent Laurent series space. ### CosSpace Space for cosine series. ### SinSpace Space for sine series. ### JacobiWeight Space with Jacobi weight. ### LogWeight Space with logarithmic weight. ### ArraySpace A space for array-valued functions. ### TensorSpace A tensor product of spaces. ``` -------------------------------- ### Check space with singularity Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/constructors.md Verifies the function space for a Fun object with a singularity, which is a JacobiWeight space. ```julia space(g) == JacobiWeight(-0.5, -0.5, Chebyshev(-1..1)) ``` -------------------------------- ### ProductFun for Bivariate Approximation Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Represents a bivariate function as an exact tensor product of two univariate functions using ProductFun in ApproxFun. ```julia using ApproxFun # ProductFun: exact tensor product representation f = ProductFun((x, y) -> exp(x) * cos(y), Chebyshev() * Chebyshev()) f(0.1, 0.2) ≈ exp(0.1) * cos(0.2) # true ``` -------------------------------- ### Matrix Entry of Derivative Operator Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/operators.md Illustrates how to access the matrix entries of a `Derivative` operator. This is done by computing the coefficient of the operator applied to a basis function. ```jldoctest julia> k,j = 5,6; julia> ej = Fun(domainspace(D), [zeros(j-1);1]); julia> D[k,j] ≈ coefficient(D * ej, k) ≈ -k true ``` -------------------------------- ### Multivariate function approximation on a non-default grid Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/faq.md Apply the least squares approach for approximating multivariate functions on custom grids. This involves constructing a Vandermonde matrix by evaluating the basis functions at the specified points. ```julia using ApproxFun, Random S = Chebyshev(0..1)^2; n = 1000; m = 50; Random.seed!(0); x = rand(n); y = rand(n); v = exp.(x .* cos.(y)); # values at the non-default grid V = Array{Float64}(undef,n,m); # Create a Vandermonde matrix by evaluating the basis at the grid for k = 1:m V[:,k] = Fun(S,[zeros(k-1);1]).(x,y) end f = Fun(S,V\v); f(0.1,0.2) exp(0.1*cos(0.2)) ``` -------------------------------- ### Laurent Space Coefficient Ordering Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/spaces.md Shows the coefficient ordering for the Laurent space, which represents sums of complex exponentials. Coefficients are ordered as [f0, f_{-1}, f_1, f_{-2}, f_2, ...]. ```jldoctest julia> f = Fun(Laurent(), [1,2,3,4]); julia> f(0.1) ≈ 1 + 2exp(-im*0.1) + 3exp(im*0.1) + 4exp(-2im*0.1) true ``` -------------------------------- ### Accessing Information about Spaces Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/library.md Provides functions to access and manipulate information about function spaces. ```APIDOC ## Accessing information about a spaces ### canonicalspace Returns the canonical representation of a space. ### itransform Applies an inverse transformation to a space. ### transform Applies a transformation to a space. ### evaluate Evaluates a space at a given point. ### dimension Returns the dimension of a space. ``` -------------------------------- ### Constructing a Bivariate Function Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/internals/multivariate.md Constructs a bivariate function on a 2D interval using Chebyshev polynomials. The resulting object has coefficients and a space defining coefficient interpretation. ```julia f=Fun((x,y)->exp(-x^2-y^2), ChebyshevInterval()^2) ``` -------------------------------- ### LowRankFun for Adaptive Bivariate Approximation Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Uses LowRankFun for adaptive low-rank approximation of bivariate functions in ApproxFun, providing efficient representation. ```julia # LowRankFun: adaptive low-rank approximation of a 2D function g = LowRankFun((x, y) -> 1 / (1 + x^2 + y^2), Chebyshev() * Chebyshev()) g(0.5, 0.3) ≈ 1 / (1 + 0.5^2 + 0.3^2) # true ``` -------------------------------- ### Algebraic Operations on Fun Objects Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt ApproxFun.jl supports standard algebraic operations (+, -, *, /, ^) and special functions on Fun objects, returning new Fun objects. It also includes functions for finding roots and extrema. ```julia using ApproxFun, SpecialFunctions, LinearAlgebra x = Fun(identity, 0..10) f = sin(x^2) g = cos(x) # Combine and compute roots and extrema h = f + g^2 r = roots(h) # roots of h rp = roots(h') # extrema (roots of derivative) h(r[1]) # ≈ 0.0 # Special functions work on Fun x = Fun() erf_f = erf(x) bessel_f = besselj(3, exp(erf_f)) airy_f = airyai(10asin(erf_f) + 2bessel_f) # Norm of a Fun (L2) norm(Fun(sin, 0..π)) # scalar ``` -------------------------------- ### Verify segments of piecewise domain Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/constructors.md Verifies that the segments of the piecewise domain of a Fun object correctly correspond to the intervals between the roots of the original function. ```julia p = [-1; roots(f); 1]; ``` ```julia segments = [Segment(x,y) for (x,y) in zip(p[1:end-1], p[2:end])]; ``` ```julia components(domain(g)) == segments ``` -------------------------------- ### Square an Operator in ApproxFun Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Illustrates squaring an operator in ApproxFun, which results in the composition of the operator with itself. ```julia # Square an operator (composes automatically) D_cheb = Derivative(Chebyshev()) D_cheb^2 # Derivative(Ultraspherical(1)) * Derivative(Chebyshev()) ``` -------------------------------- ### Accessing Information about a Fun Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/library.md Provides methods to access and query information about a 'Fun' object. ```APIDOC ## Accessing information about a Fun ### domain Returns the domain of a Fun. ### coefficients Returns the coefficients of a Fun. ### extrapolate Extrapolates a Fun. ### ncoefficients Returns the number of coefficients of a Fun. ### points Returns the points associated with a Fun. ### space Returns the space of a Fun. ### values Returns the values of a Fun. ### stride Returns the stride of a Fun. ``` -------------------------------- ### Create Fun with singularity Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/constructors.md Creates a Fun object that incorporates singularities at the edges of the domain. The resulting space factors out the singularity. ```julia g = f/sqrt(1-x^2); ``` -------------------------------- ### Create Fun with absolute value Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/constructors.md Creates a Fun object by taking the absolute value of another Fun. The space and domain are inferred based on the roots of the original function. ```julia f = Fun(x->cospi(5x), -1..1); ``` ```julia g = abs(f); ``` -------------------------------- ### Least-Squares Interpolation with ApproxFun Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Performs least-squares interpolation at an arbitrary grid for improved stability when the number of points exceeds the number of coefficients. ```julia # Least-squares for stability (more points than coefficients) n, m = 100, 50 p = range(1, stop=2, length=n) v = exp.(p) V = Array{Float64}(undef, n, m) for k in 1:m V[:, k] = Fun(S, [zeros(k-1); 1]).(p) end f = Fun(S, V \ v) f(1.1) ≈ exp(1.1) # true ``` -------------------------------- ### Define Domains in ApproxFun.jl Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt ApproxFun.jl uses `Domain` subtypes to define oriented domains for function approximation. Basic interval and complex segment domains are shown. ```julia using ApproxFun # Basic interval domains Interval(-1, 1) # equivalent to -1..1 Segment(0.0, 2.0im) # complex segment ``` -------------------------------- ### Operator Composition with Space Mismatch Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/operators.md Illustrates operator composition (squaring a derivative operator) when the range space of the first operator does not match the domain space of the second. ApproxFun handles this by discarding the domain space. ```repl D = Derivative(Chebyshev()); D^2 ``` -------------------------------- ### Define Domain as Curve using Fun Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Defines a domain as the image of a Fun, enabling operations on arbitrary curves in the complex plane. ```julia using ApproxFun # Arc defined as the image of exp(im*x) for x in 1..2 x = Fun(1..2) arc = Curve(exp(im * x)) # Function on a circle of radius 2 θ = Fun(PeriodicSegment()) circ = Curve(2 * exp(im * θ)) ``` -------------------------------- ### Create Fun from identity and domain Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/constructors.md Creates a Fun object representing the identity function over a specified domain. The space is automatically inferred. ```julia x = Fun(identity,-1..1); ``` -------------------------------- ### Solve Singularly Perturbed Boundary Value Problem with Evaluation Operators Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/equations.md Solves a second-order singularly perturbed boundary value problem using `Evaluation` operators for the boundary conditions and a differential operator for the equation. The system is solved using `A\b`. ```julia ϵ = 1/70 x = Fun() u = [Evaluation(-1); Evaluation(1); ϵ*𝒟^2-x*𝒟+I] \ [1,2,0] u(0.1) ≈ 0.05 # compare with the analytical solution ``` -------------------------------- ### Solve Fredholm Integral Equation with ApproxFun Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Solves a Fredholm integral equation of the second kind using ApproxFun's operator algebra and linear system solver. ```julia using ApproxFun # Fredholm integral equation: u + exp(x) * ∫cos(x)*u dx = cos(exp(x)) Σ = DefiniteIntegral(Chebyshev()) x = Fun() u = (I + exp(x) * Σ[cos(x)]) \ cos(exp(x)) u(0.1) # ≈ 0.21864294855628819 # Note: Σ[f] is shorthand for Σ * Multiplication(f) ``` -------------------------------- ### Inbuilt Operators Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/library.md Lists and describes the inbuilt operators available in the library. ```APIDOC ## Inbuilt operators ### Conversion Operator for converting between spaces. ### Derivative Operator for differentiation. ### Dirichlet Dirichlet boundary condition operator. ### Evaluation Operator for evaluating functions. ### Integral Operator for integration. ### Laplacian Laplacian operator. ### Multiplication Operator for multiplication. ### Neumann Neumann boundary condition operator. ### PartialInverseOperator Operator for partial inverse. ``` -------------------------------- ### Solve First-Order Initial Value Problem Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/equations.md Solves a first-order initial value problem by combining an `Evaluation` functional for the initial condition and a differential operator. The boundary conditions and the differential equation are represented as a system of equations solved with `A\b`. ```julia t = Fun(0..1) u = [Evaluation(0); 𝒟-t] \ [1;0] u(0) ≈ 1 norm(u'-t*u) < eps() ``` -------------------------------- ### ApproxFun.jl SumSpace and Components Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Represents the direct sum of spaces with interlaced coefficients. Use `components(f)` to extract individual function components. ```julia using ApproxFun, LinearAlgebra x = Fun(identity, -1..1) f = cos(x - 0.1) * sqrt(1 - x^2) + exp(x) space(f) == JacobiWeight(0.5, 0.5, Chebyshev(-1..1)) + Chebyshev(-1..1) # true a, b = components(f) a(0.2) ≈ cos(0.2 - 0.1) * sqrt(1 - 0.2^2) # true b(0.2) ≈ exp(0.2) # true f(0.2) ≈ a(0.2) + b(0.2) # true # Coefficients are interlaced (odd indices → a, even → b) norm(coefficients(f)[1:2:end] - coefficients(a)) == 0.0 # true ``` -------------------------------- ### Differentiation and Integration of Fun Objects Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt ApproxFun.jl overloads the prime symbol (') for differentiation and `cumsum` for indefinite integration. The `sum` function calculates the definite integral over the domain. ```julia using ApproxFun, LinearAlgebra f = Fun(exp, -1..1) # Differentiation: f' is the derivative Fun norm(f - f') # ≈ 4.4e-14 (exp is its own derivative) # Indefinite integration via cumsum g = cumsum(f) g = g + f(-1) # set constant of integration norm(f - g) # ≈ 3.5e-15 # Definite integral sum(Fun(x -> x^2, 0..1)) # ≈ 1/3 # Higher-order derivative f2 = Fun(cos, -1..1) norm(f2'' + f2) < 1e-10 # cos'' + cos = 0 ``` -------------------------------- ### Create Fun from explicit coefficients Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/index.md Represents a polynomial function by explicitly providing its Taylor coefficients. Use this when the coefficients are known. ```julia f = Fun(Taylor(),[1,2,3]) # Represents 1 + 2x + 3x^2 ``` -------------------------------- ### Evaluate 2D Fun with explicit coefficients Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/constructors.md Evaluates a 2D Fun object constructed with explicit coefficients at a given point (x, y) to verify its value based on the sum of products of basis functions. ```julia f(1,2) ≈ 1cos(0*1)*cos(0*2) + 2cos(0*1)*cos(1*2) + 3cos(1*1)*cos(0*2) ``` -------------------------------- ### ApproxFun.jl JacobiWeight for Singularities Source: https://context7.com/juliaapproximation/approxfun.jl/llms.txt Handles functions weighted by (1+x)^α * (1-x)^β using JacobiWeight, useful for endpoint singularities. Can be constructed directly or derived from function division. ```julia using ApproxFun x = Fun(identity, -1..1) f = exp(x) g = f / sqrt(1 - x^2) space(g) == JacobiWeight(-0.5, -0.5, Chebyshev(-1..1)) # true # Construct directly h = Fun(x -> 1/sqrt(1-x^2), JacobiWeight(-0.5, -0.5, Chebyshev())) h(0.5) # ≈ 1/sqrt(1 - 0.25) ``` -------------------------------- ### Derivative Operator on CosSpace Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/operators.md Demonstrates the `Derivative` operator on `CosSpace`. The derivative of `cos(cos(θ))` is computed and compared to the analytical derivative. ```jldoctest julia> D = Derivative(CosSpace()); julia> f = Fun(θ->cos(cos(θ)), CosSpace()); julia> fp = D * f; julia> fp(0.1) ≈ f'(0.1) ≈ sin(cos(0.1))*sin(0.1) true ``` -------------------------------- ### Check function space Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/constructors.md Verifies that the inferred function space for a Fun object is Chebyshev on the interval -1 to 1. ```julia space(f) == Chebyshev(-1..1) ``` -------------------------------- ### Interpolate function on default grid Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/faq.md Use this when you need to interpolate a function using the default grid provided by ApproxFun. The default transform is applied to the data. ```julia using ApproxFun, Random S = Chebyshev(1..2); p = points(S,20); # the default grid v = exp.(p); # values at the default grid f = Fun(S,ApproxFun.transform(S,v)); f(1.1) exp(1.1) ``` -------------------------------- ### Evaluate 1D Fun with explicit coefficients Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/constructors.md Evaluates a 1D Fun object constructed with explicit coefficients at a given point to verify its value. ```julia f(0.1) ≈ 1 + 2*0.1 + 3*0.1^2 ``` -------------------------------- ### Evaluate Fun at a point Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/index.md Evaluates the function represented by a 'Fun' object at a specific point. This is a standard operation after creating a 'Fun'. ```julia f(1.0) ``` -------------------------------- ### Check space of absolute value Fun Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/usage/constructors.md Checks if the space of the absolute value Fun is a ContinuousSpace, indicating piecewise continuous functions. ```julia space(g) isa ContinuousSpace ``` -------------------------------- ### Least squares approximation on a non-default grid Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/faq.md Use least squares with more points than coefficients for better stability when approximating functions on non-default grids, especially with evenly spaced points. ```julia using ApproxFun, Random S = Chebyshev(1..2); n = 100; m = 50; p = range(1,stop=2,length=n); # a non-default grid v = exp.(p); # values at the non-default grid V = Array{Float64}(undef,n,m); # Create a Vandermonde matrix by evaluating the basis at the grid for k = 1:m V[:,k] = Fun(S,[zeros(k-1);1]).(p) end f = Fun(S,V\v); f(1.1) exp(1.1) ``` -------------------------------- ### Interpolate function on a non-default grid Source: https://github.com/juliaapproximation/approxfun.jl/blob/master/docs/src/faq.md Manually interpolate functions on custom grids by evaluating the basis at the desired points and solving a linear system. Note that evenly spaced grids can be unstable for large n; consider using least squares instead. ```julia using ApproxFun, Random S = Chebyshev(1..2); n = 50; p = range(1,stop=2,length=n); # a non-default grid v = exp.(p); # values at the non-default grid V = Array{Float64}(undef,n,n); # Create a Vandermonde matrix by evaluating the basis at the grid for k = 1:n V[:,k] = Fun(S,[zeros(k-1);1]).(p) end f = Fun(S,V\v); f(1.1) exp(1.1) ```