| Build Status |
|---|
The purpose of this package is to provide methods to numerically handle real spherical harmonics expansions in Cartesian coordinates.
The normalized real spherical harmonics on the unit sphere are defined by
where
is the normalization factor and
are the associated Legendre polynomials which can be derived from the Legendre polynomials
Note that you will also find a convention in literature, where the
Each function
for all
The term
can be transformed from spherical to Cartesian coordinates, where it can be expressed as a homogeneous polynomial of degree
Generate a MultivariatePolynomials.Polynomial representation of
in variables α, β, and γ on the unit sphere by
using SphericalHarmonics
@polyvar α β γ
l = 7
m = -2
p = ylm(l,m,α,β,γ)
63.28217501963252αβγ⁵ - 48.67859616894809αβγ³ + 6.63799038667474αβγThe polynomial representation of
in variables x, y, and z on
@polyvar x y z
p = rlylm(l,m,x,y,z)
6.63799038667474x⁵yz + 13.27598077334948x³y³z - 35.40261539559861x³yz³ + 6.63799038667474xy⁵z - 35.40261539559861xy³z³ + 21.24156923735917xyz⁵In case where a function is equal to or can be approximated by a finite Spherical harmonic expansion
with
Coefficents c[l,m] = 42.0.
L = 2
c = SphericalHarmonicCoefficients(L)
c[0,0] = 42.0 #c₀₀
c[2,-1] = -1.0 #c₂₋₁
c[2,1] = 2.0 #c₂₁Internally, the coefficients are lexicographically stored in a vector (c[0,0], c[1,-1], c[1,0], c[1,1], c[2,-2], ...). So the above initialization is equivalent to
C = [42.0,0,0,0,0,-1,0,2,0]
c = SphericalHarmonicCoefficients(C)Note that SphericalHarmonicCoefficients(C) will throw an error if length(C) is not x, y, and z can be obtained by
@polyvar x y z
f = sphericalHarmonicsExpansion(c,x,y,z)
11.847981254502882 - 1.0925484305920792yz + 2.1850968611841584xzCurrently, expansions up to
If we change from a coordinate system with coordinates x, y, and z into a translated one with new coordinates u = x + tx, v = y + ty, and w = z + tz we need transformed coefficients to express the expansion in these new coordinates. To this end, we can do
@polyvar u v w
translationVector = [0,0,1.0] # [tx,ty,tz]
cTranslated = translation(c,translationVector)
sphericalHarmonicsExpansion(cTranslated,u,v,w)
11.847981254502878 - 1.0925484305920792v + 2.1850968611841584u - 1.092548430592079vw + 2.185096861184158uwFurthermore, any rotation of the coordinate system can be expressed with rotated coefficients. Rotation of a coordinate system with coordinates x, y, and z into a rotated system with coordinates (r₁, r₂, r₃) = R * [x, y, z], using a rotation matrix R = RotZYZ(eulerAngles...) provided by Rotations.jl, can be obtained by a rotation of the coefficients with
@polyvar r₁ r₂ r₃
rotationAngles = [π/2, -π, 0] # ZYZ-convention (Euler angles)
cRotated = rotation(c, rotationAngles)
sphericalHarmonicsExpansion(cRotated, r₁,r₂,r₃)
11.847981254502882 + 2.185096861184156r₂r₃ - 1.0925484305920796r₁r₃Combining the rotation with a point reflection and translation, any reflections of the coordinate system can be represented. For example, a reflection of the coordinate system with coordinates x, y, and z in the xz-plane yielding a coordinates x, y⁻ = -y, and z can be expressed in the coefficients using
@polyvar y⁻
cReflected = rotation(pointReflection(c), [0,pi,0]);
sphericalHarmonicsExpansion(cReflected, x, y⁻, z)
11.847981254502882 + 1.0925484305920798y⁻z + 2.185096861184156xzIf you want to evaluate MultivariatePolynomials
f(x=>0.5, y=>-1.0, z=>0.25)
12.394255469798921
f((x,y,z)=>(0.5,-1.0,0.25))
12.394255469798921In case where you want to evaluate
g = @fastfunc f
g(0.5,-1.0,0.25)
12.394255469798921which has moderate generation overhead. Usage from within local scope requires Base.invokelatest(foo, 1.0,2.0,3.0) instead of foo(1.0,2.0,3.0) to avoid issue #4. Or use
h = fastfunc(f)
h(0.5,-1.0,0.25)
12.394255469798921which uses GeneralizedGenerated for function generation and comes with a significant overhead.
For more informations on the MultivariatePolynomials package please visit the project page on github.