# Automatic differentiation through spherical harmonic transforms

This example finds a positive value of $\lambda$ in:

$f(r) = \sin[\lambda (k\cdot r)],$

for some $k,r\in\mathbb{S}^2$ such that $\int_{\mathbb{S}^2} f^2 {\rm\,d}\Omega = 1$. We do this by using derivative information through:

$\dfrac{\partial f}{\partial \lambda} = (k\cdot r) \cos[\lambda (k\cdot r)].$
using FastTransforms, LinearAlgebra

The colatitudinal grid (mod $\pi$):

N = 15
θ = (0.5:N-0.5)/N
0.03333333333333333:0.06666666666666667:0.9666666666666667

The longitudinal grid (mod $\pi$):

M = 2*N-1
φ = (0:M-1)*2/M
0.0:0.06896551724137931:1.9310344827586206

We precompute a spherical harmonic–Fourier plan:

P = plan_sph2fourier(Float64, N)
FastTransforms Spherical harmonic--Fourier plan for 15×29-element array of Float64

And an FFTW Fourier analysis plan on $\mathbb{S}^2$:

PA = plan_sph_analysis(Float64, N, M)
FastTransforms plan for FFTW Fourier analysis on the sphere for 15×29-element array of Float64

Our choice of $k$ and angular parametrization of $r$:

k = [2/7, 3/7, 6/7]
r = (θ,φ) -> [sinpi(θ)*cospi(φ), sinpi(θ)*sinpi(φ), cospi(θ)]
#1 (generic function with 1 method)

Our initial guess for $\lambda$:

λ = 1.0
1.0

Then we run Newton iteration and grab an espresso:

for _ in 1:7
F = [sin(λ*(k⋅r(θ,φ))) for θ in θ, φ in φ]
Fλ = [(k⋅r(θ,φ))*cos(λ*(k⋅r(θ,φ))) for θ in θ, φ in φ]
U = P\(PA*F)
Uλ = P\(PA*Fλ)
global λ = λ - (norm(U)^2-1)/(2*sum(U.*Uλ))
println("λ: $(rpad(λ, 18)) and the 2-norm:$(rpad(norm(U), 18))")
end
λ: 0.5565017029393282 and the 2-norm: 1.8510924318185522
λ: 0.5031571262839711 and the 2-norm: 1.104184591487036
λ: 0.5010418434316173 and the 2-norm: 1.0040147091934823
λ: 0.5010383094266806 and the 2-norm: 1.0000066984258864
λ: 0.5010383094167955 and the 2-norm: 1.0000000000187363
λ: 0.5010383094167953 and the 2-norm: 1.0000000000000004
λ: 0.5010383094167954 and the 2-norm: 0.9999999999999999


This page was generated using Literate.jl.