Skip to content

Commit 96d1a23

Browse files
try generating example docs using Literate.jl
1 parent 3ba4ee3 commit 96d1a23

File tree

3 files changed

+55
-34
lines changed

3 files changed

+55
-34
lines changed

docs/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
[deps]
22
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
3+
FastTransforms = "057dd010-8810-581a-b7be-e3fc3b93f78c"
4+
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
35

46
[compat]
57
Documenter = "~0.24"

docs/make.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1-
using Documenter, FastTransforms
1+
using Documenter, FastTransforms, Literate
2+
3+
const EXAMPLES_DIR = joinpath(@__DIR__, "..", "examples")
4+
const OUTPUT_DIR = joinpath(@__DIR__, "src/generated")
5+
6+
examples = [
7+
"sphere.jl",
8+
]
9+
10+
for example in examples
11+
example_filepath = joinpath(EXAMPLES_DIR, example)
12+
Literate.markdown(example_filepath, OUTPUT_DIR; execute=true)
13+
end
214

315
makedocs(
416
doctest = false,
517
format = Documenter.HTML(),
618
sitename = "FastTransforms.jl",
719
authors = "Richard Mikael Slevinsky",
820
pages = Any[
9-
"Home" => "index.md"
21+
"Home" => "index.md",
22+
"Examples" => [
23+
"generated/sphere.md",
24+
],
1025
]
1126
)
1227

examples/sphere.jl

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
#############
1+
# ## Spherical harmonic addition theorem
22
# This example confirms numerically that
3+
# ```math
4+
# \frac{P_4(z\cdot y) - P_4(x\cdot y)}{z\cdot y - x\cdot y},
5+
# ```
36
#
4-
# [P₄(z⋅y) - P₄(x⋅y)]/(z⋅y - x⋅y),
5-
#
6-
# is actually a degree-3 polynomial on 𝕊², where P₄ is the degree-4
7-
# Legendre polynomial, and x,y,z ∈ 𝕊².
8-
# To verify, we sample the function on a 5×9 equiangular grid
7+
# is actually a degree-$3$ polynomial on $\mathbb{S}^2$, where $P_4$ is the degree-$4$
8+
# Legendre polynomial, and $x,y,z \in \mathbb{S}^2$.
9+
# To verify, we sample the function on a $5\times9$ equiangular grid
910
# defined by:
10-
#
11-
# θₙ = (n+1/2)π/N, for 0 ≤ n < N, and
12-
#
13-
# φₘ = 2π m/M, for 0 ≤ m < M;
14-
#
11+
# ```math
12+
# \theta_n = (n+\frac{1}{2})\pi/N,\quad{\rm for}\quad 0\le n < N,\quad{\rm and}
13+
# ```
14+
# ```math
15+
# \varphi_m = 2\pi m/M,\quad{\rm for}\quad 0\le m < M;
16+
# ```
1517
# we convert the function samples to Fourier coefficients using
1618
# `plan_sph_analysis`; and finally, we transform
1719
# the Fourier coefficients to spherical harmonic coefficients using
1820
# `plan_sph2fourier`.
1921
#
2022
# In the basis of spherical harmonics, it is plain to see the
21-
# addition theorem in action, since P₄(x⋅y) should only consist of
22-
# exact-degree-4 harmonics.
23+
# addition theorem in action, since $P_4(x\cdot y)$ should only consist of
24+
# exact-degree-$4$ harmonics.
2325
#
24-
# For the storage pattern of the arrays, please consult the documentation.
25-
#############
26+
# For the storage pattern of the arrays, please consult the
27+
# [documentation](https://MikaelSlevinsky.github.io/FastTransforms).
2628

2729
function threshold!(A::AbstractArray, ϵ)
2830
for i in eachindex(A)
@@ -33,63 +35,65 @@ end
3335

3436
using FastTransforms, LinearAlgebra
3537

36-
# The colatitudinal grid (mod π):
38+
# The colatitudinal grid (mod $\pi$):
3739
N = 5
3840
θ = (0.5:N-0.5)/N
3941

40-
# The longitudinal grid (mod π):
42+
# The longitudinal grid (mod $\pi$):
4143
M = 2*N-1
4244
φ = (0:M-1)*2/M
4345

44-
# Arbitrarily, we place x at the North pole:
46+
# Arbitrarily, we place $x$ at the North pole:
4547
x = [0,0,1]
4648

4749
# Another vector is completely free:
4850
y = normalize([.123,.456,.789])
4951

50-
# Thus z ∈ 𝕊² is our variable vector, parameterized in spherical coordinates:
52+
# Thus $z \in \mathbb{S}^2$ is our variable vector, parameterized in spherical coordinates:
5153
z = (θ,φ) -> [sinpi(θ)*cospi(φ), sinpi(θ)*sinpi(φ), cospi(θ)]
5254

53-
# The degree-4 Legendre polynomial is:
55+
# The degree-$4$ Legendre polynomial is:
5456
P4 = x -> (35*x^4-30*x^2+3)/8
5557

5658
# On the tensor product grid, our function samples are:
5759
F = [(P4(z(θ,φ)y) - P4(xy))/(z(θ,φ)y - xy) for θ in θ, φ in φ]
5860

61+
# We precompute a spherical harmonic--Fourier plan:
5962
P = plan_sph2fourier(F)
63+
64+
# And an FFTW Fourier analysis plan on $\mathbb{S}^2$:
6065
PA = plan_sph_analysis(F)
6166

62-
# Its spherical harmonic coefficients demonstrate that it is degree-3:
67+
# Its spherical harmonic coefficients demonstrate that it is degree-$3$:
6368
V = PA*F
6469
U3 = threshold!(P\V, 400*eps())
6570

66-
# Similarly, on the tensor product grid, the Legendre polynomial P₄(z⋅y) is:
71+
# Similarly, on the tensor product grid, the Legendre polynomial $P_4(z\cdot y)$ is:
6772
F = [P4(z(θ,φ)y) for θ in θ, φ in φ]
6873

69-
# Its spherical harmonic coefficients demonstrate that it is exact-degree-4:
74+
# Its spherical harmonic coefficients demonstrate that it is exact-degree-$4$:
7075
V = PA*F
7176
U4 = threshold!(P\V, 3*eps())
7277

73-
nrm1 = norm(U4);
78+
# The $L^2(\mathbb{S}^2)$ norm of the function is:
79+
nrm1 = norm(U4)
7480

75-
# Finally, the Legendre polynomial P₄(z⋅x) is aligned with the grid:
81+
# Finally, the Legendre polynomial $P_4(z\cdot x)$ is aligned with the grid:
7682
F = [P4(z(θ,φ)x) for θ in θ, φ in φ]
7783

7884
# It only has one nonnegligible spherical harmonic coefficient.
7985
# Can you spot it?
8086
V = PA*F
8187
U4 = threshold!(P\V, 3*eps())
8288

83-
# That nonnegligible coefficient should be approximately √(2π/(4+1/2)),
89+
# That nonnegligible coefficient should be approximately `√(2π/(4+1/2))`,
8490
# since the convention in this library is to orthonormalize.
8591

86-
nrm2 = norm(U4);
92+
nrm2 = norm(U4)
8793

88-
# Note that the integrals of both functions P₄(z⋅y) and P₄(z⋅x) and their
89-
# L²(𝕊²) norms are the same because of rotational invariance. The integral of
94+
# Note that the integrals of both functions $P_4(z\cdot y)$ and $P_4(z\cdot x)$ and their
95+
# $L^2(\mathbb{S}^2)$ norms are the same because of rotational invariance. The integral of
9096
# either is perhaps not interesting as it is mathematically zero, but the norms
9197
# of either should be approximately the same.
9298

93-
@show nrm1
94-
@show nrm2
95-
@show nrm1 nrm2
99+
nrm1 nrm2

0 commit comments

Comments
 (0)