Code Monkey home page Code Monkey logo

sphericalharmonicexpansions.jl's Introduction

SphericalHarmonicExpansions

Build Status
CI
codecov.io

The purpose of this package is to provide methods to numerically handle real spherical harmonics expansions in Cartesian coordinates.

Table of Contents

Mathematical Background

Definition of the Spherical Harmonics

The normalized real spherical harmonics on the unit sphere are defined by

where , , and are the spherical angular coordinates,

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 are scaled by .

Spherical Harmonics Expansions

Each function satisfying Laplace's equation in a region can be written as a spherical harmonic expansion

for all , where denote the spherical coefficients and .

The term

can be transformed from spherical to Cartesian coordinates, where it can be expressed as a homogeneous polynomial of degree .

Usage

Polynomial Representation of the Spherical Harmonics

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 can be obtained by

@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⁵

Polynomial Representation of the Spherical Harmonics Expansions

In case where a function is equal to or can be approximated by a finite Spherical harmonic expansion

with its multivariate polynomial representation has finite degree.

Coefficents can be initialized and populated by 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)
f = sphericalHarmonicsExpansion(c,x,y,z)
2.1850968611841584xz + -1.0925484305920792yz + 11.847981254502882

Note that SphericalHarmonicCoefficients(C) will throw an error if length(C) is not for some . From there on the corresponding polynomial representation in cartesian coordinates x, y, and z can be obtained by

@polyvar x y z

f = sphericalHarmonicsExpansion(c,x,y,z)
2.1850968611841584xz - 1.0925484305920792yz + 11.847981254502882

Currently, expansions up to $L=66$ are supported.

Transformation of Expansion Coefficients under Translation

If we change from a coordinate sytsem 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)
2.1850968611841584uw - 1.0925484305920792vw + 2.1850968611841584u - 1.0925484305920792v + 11.847981254502878

Numerical Evaluation

If you want to evaluate at a specific point you can use the standard interface of MultivariatePolynomials

f(x=>0.5, y=>-1.0, z=>0.25)
12.394255469798921
f((x,y,z)=>(0.5,-1.0,0.25))
12.394255469798921

In case where you want to evaluate for a large number of points you might run into performance issues. To this end we provide two methods to dynamically generate fast evaluating functions. Either use

g = @fastfunc f
g(0.5,-1.0,0.25)
12.394255469798921

which 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.394255469798921

which uses GeneralizedGenerated for function generation and comes with a significant overhead.

Further Reading

For more informations on the MultivariatePolynomials package please visit the project page on github.

sphericalharmonicexpansions.jl's People

Contributors

hofmannmartin avatar mboberg avatar torb3n avatar github-actions[bot] avatar tknopp avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.