egison-5.1.0: sample/math/geometry/hodge-laplacian-spherical.egi
-- Hodge Laplacian in spherical coordinates
declare symbol r, θ, φ
def N : Integer := 3
def x : Vector MathValue := [| r, θ, φ |]
def g_i_j : Matrix MathValue := [| [| 1, 0, 0 |], [| 0, r^2, 0 |], [| 0, 0, r^2 * (sin θ)^2 |] |]_i_j
def g~i~j : Matrix MathValue := [| [| 1, 0, 0 |], [| 0, 1 / r^2, 0 |], [| 0, 0, 1 / (r^2 * (sin θ)^2) |] |]~i~j
-- Exterior derivative
def d (A: Tensor MathValue) : Tensor MathValue := !(flip ∂/∂) x A
-- Hodge star operator
def hodge (A: DiffForm MathValue) : DiffForm MathValue :=
let k := dfOrder A
in withSymbols [i, j]
sqrt (abs (M.det g_#_#)) *
foldl
(.)
((ε' N k)_(i_1)..._(i_N) . A..._(j_1)..._(j_k))
(map (\n -> g~(i_n)~(j_n)) [1..k])
-- Codifferential
def δ (A: DiffForm MathValue) : DiffForm MathValue :=
let k := dfOrder A
in ((-1)^(N * k + 1)) * hodge (d (hodge A))
-- Laplacian
def Δ (A: DiffForm MathValue) : DiffForm MathValue :=
match dfOrder A as integer with
| #0 -> δ (d A)
| #N -> d (δ A)
| _ -> d (δ A) + δ (d A)
-- Apply to scalar function
def f := function (r, θ, φ)
assertEqual "Laplacian in spherical coordinates"
(Δ f)
(∂/∂ (∂/∂ f r) r + 2 * (∂/∂ f r) / r + (∂/∂ (∂/∂ f θ) θ) / r^2 + cos θ * (∂/∂ f θ) / (r^2 * sin θ) + (∂/∂ (∂/∂ f φ) φ) / (r^2 * (sin θ)^2))