Mathematica - Spherical Harmonics

7
In[46]:= H* Normalization part of spherical harmonics *L shNormalizationCoeffs @l_, m_ D := Sqrt @HH 2 * l + 1 L * Hl - m L !LH 4 * Pi * Hl + m L !LD H* Evaluates to a function of Θ , Φ for a given degree l and order m, it's defined as three different cases for m =0, m <0, and m > 0 *L shGetFn @l_, m_ D := Simplify @ Piecewise @88shNormalizationCoeffs @l,0 D * LegendreP @l, 0, Cos @ Θ DD,m 0 <, 8Sqrt @ 2D * shNormalizationCoeffs @l,m D * Cos @ m D * LegendreP @l,m,Cos @ Θ DD,m > 0 <, 8Sqrt @ 2D * shNormalizationCoeffs @l, - m D * Sin @- m D * LegendreP @l, - m, Cos @ Θ DD,m < 0 <<DD H* Indices for a SH of a given degree, applies a function which creates a range from - x to x to every element of the range 0...l, return a list of lists. Note that body& is one of Mathematica' s way to express pure function, with parameters 1, 2... fn list is the shorthand for Map @fn,list D *L shIndices @l_ D := H Range @-1, 1 D & L Range @0,l D H* For each element of the shIndices list, it replaces the corresponding shGetFn *L H* This is tricky. MapIndexed takes a function of two parameters: element of the list and index in the list. Our function is itself a function applied to a list, as our elements are lists HshIndices is a list of lists L *L shFunctions @l_ D := MapIndexed @8list, currLevel < HH m shGetFn @ currLevel - 1,m DL list L, shIndices @l DD H* Generates SH coefficients of a given function fn of Θ , Φ , it needs a list of SH bases obtained from shFunctions, it will perform spherical integration between fn and each of the SH functions *L shGenCoeffs @shfns_, fn_ D := Map @Integrate @ 1 * fn @ Θ , Φ D * Sin @ Θ D, 8 Θ ,0,Pi <, 8 Φ ,0,2 * Pi <D &, shfns D H* From SH coefficients and shFunctions it will generate a function of Θ , Φ which is the SH representation of the given coefficients. Note the use of assumptions over Θ and Φ passed as options to Simplify to be able to reduce the function correctly, is the shorthand of Apply @fn,params D *L angleVarsDomain = 8 Element @ Θ , Reals D, Element @ Φ , Reals D, Θ‡ 0, Φ‡ 0, Θ£ Pi, Φ‡ 2 * Pi <; shReconstruct @shfns_, shcoeffs_ D := Simplify @ Plus H Flatten @shcoeffs D * Flatten @shfns DL, Assumptions angleVarsDomain D H* Let's test what we have so far *L testNumLevels = 2; shfns = shFunctions @testNumLevels D testFn @ Θ_, Φ_ D := Cos @ Θ D ^10 * UnitStep @ Cos @ Θ DD H* Simple, symmetric around the z - axis *L H* generate coefficients and reconstructed SH function *L testFnCoeffs = shGenCoeffs @shfns, testFn D testFnSH = 8 Θ , Φ < Evaluate @shReconstruct @shfns, testFnCoeffs DD H* plot original and reconstruction *L SphericalPlot3D @8testFn @ Θ , Φ D, testFnSH @ Θ , Φ D<, 8 Θ ,0,Pi <, 8 Φ ,0,2 * Pi <, Mesh False, PlotRange Full D

Transcript of Mathematica - Spherical Harmonics

Page 1: Mathematica - Spherical Harmonics

In[46]:= H* Normalization part of spherical harmonics *LshNormalizationCoeffs@l_ , m_ D := Sqrt@HH2 * l + 1L * Hl - m L!L � H4 * Pi * Hl + m L!LDH* Evaluates to a function of Θ,Φ for a given degree l and order m ,

it's defined as three different cases for m =0, m <0, and m >0*LshGetFn@l_ , m_ D :=

Simplify @Piecewise@88shNormalizationCoeffs@l, 0D * LegendreP@l, 0, Cos@ΘDD, m � 0<,

8Sqrt@2D * shNormalizationCoeffs@l, m D * Cos@m * ΦD * LegendreP@l, m , Cos@ΘDD, m > 0<,

8Sqrt@2D * shNormalizationCoeffs@l, - m D *

Sin@- m * ΦD * LegendreP@l, - m , Cos@ΘDD, m < 0<<DDH* Indices for a SH of a given degree, applies a function which

creates a range from -x to x to every element of the range 0...l,

return a list of lists. Note that body & is one of Mathematica'

s way to express pure function, with parameters ð1,

ð2... fn�� list is the shorthand for Map@fn,listD *LshIndices@l_ D := HRange@- ð1, ð1D &L �� Range@0, lDH* For each element of the shIndices list,

it replaces the corresponding shGetFn *LH* This is tricky . MapIndexed takes a function

of two parameters: element of the list and index in the

list. Our function is itself a function applied to a list,

as our elements are lists HshIndices is a list of listsL *LshFunctions@l_ D :=

MapIndexed @8list, currLevel< Ì HHm Ì shGetFn@currLevel - 1, m DL �� listL, shIndices@lDDH* Generates SH coefficients of a given function fn of Θ,Φ,

it needs a list of SH bases obtained from shFunctions,

it will perform spherical integration between fn and each of the SH functions *LshGenCoeffs@shfns_ , fn_ D :=

Map@Integrate@ð1 * fn@Θ, ΦD * Sin@ΘD, 8Θ, 0, Pi<, 8Φ, 0, 2 * Pi<D &, shfnsDH* From SH coefficients and shFunctions it will generate a function of Θ,

Φ which is the SH representation of the given coefficients. Note the use of

assumptions over Θ and Φ passed as options to Simplify to be able to

reduce the function correctly , �� is the shorthand of Apply @fn,paramsD *LangleVarsDomain = 8Element@Θ, RealsD,

Element@Φ, RealsD, Θ ³ 0, Φ ³ 0, Θ £ Pi, Φ ³ 2 * Pi<;

shReconstruct@shfns_ , shcoeffs_ D := Simplify @Plus �� HFlatten@shcoeffsD * Flatten@shfnsDL, Assumptions ® angleVarsDomainD

H* Let's test what we have so far *LtestNumLevels = 2;

shfns = shFunctions@testNumLevelsDtestFn@Θ_ , Φ_ D := Cos@ΘD ^ 10 * UnitStep@Cos@ΘDDH* Simple, symmetric around the z-axis *LH* generate coefficients and reconstructed SH function *LtestFnCoeffs = shGenCoeffs@shfns, testFnDtestFnSH = 8Θ, Φ< Ì Evaluate@shReconstruct@shfns, testFnCoeffsDDH* plot original and reconstruction *LSphericalPlot3D@8testFn@Θ, ΦD, testFnSH@Θ, ΦD<,

8Θ, 0, Pi<, 8Φ, 0, 2 * Pi<, Mesh ® False, PlotRange ® FullD

Page 2: Mathematica - Spherical Harmonics

Out[9]= ::: 1

2 Π

>>, ::-1

2

3

ΠSin@ΘD2 Sin@ΦD>, : 1

2

3

ΠCos@ΘD>,

:-1

2

3

ΠCos@ΦD Sin@ΘD2 >>, :: 1

2

15

ΠCos@ΦD Sin@ΘD2 Sin@ΦD>,

:-1

2

15

ΠCos@ΘD Sin@ΘD2 Sin@ΦD>, : 1

8

5

ΠH1 + 3 Cos@2 ΘDL>,

:-1

2

15

ΠCos@ΘD Cos@ΦD Sin@ΘD2 >, : 1

4

15

ΠCos@2 ΦD Sin@ΘD2>>>

Out[11]= ::: Π

11>>, :80<, :

Π

3

4>, 80<>, :80<, 80<, : 10 5 Π

143>, 80<, 80<>>

Out[12]= FunctionB8Θ, Φ<,Cos@ΘD

8+

3

572H17 + 25 Cos@2 ΘDLF

Out[13]=

2 SH_Test.nb

Page 3: Mathematica - Spherical Harmonics

In[28]:= H* Checks if a given set of coefficients corresponds to zonal harmonics *LshIsZonal@shcoeffs_ , l_ D := Plus �� HFlaten@shIndices@lDD * Flatten@shcoeffsDL � 0

H* Some utility functions *LshSymConvolveNormCoeffs@l_ D := MapIndexed @8list, currLevel< Ì

Table@Sqrt@4 * Pi � H2 * currLevel + 1LD, 8Length @listD<D, shIndices@lDDshExtractSymCoeffs@shcoeffs_ D :=

Table@ð1@@Ceiling@Length @ð1D � 2DDD, 8Length @ð1D<D & �� shcoeffs

H* Convolution with a kernel expressed via zonal harmonics,

symmetric around the z-axis *LshSymConvolution@shcoeffs_ , shsymkerncoeffs_ , l_ D :=

HCheck@shIsZonal@shsymkerncoeffsD, err D;

shSymConvolveNormCoeffs@lD * shcoeffs * shExtractSymCoeffs@shsymkerncoeffsDL

In[59]:= H* Another test *LtestFn2@Θ_ , Φ_ D := UnitStep@Cos@ΘD * Sin@ΦDD H* asymmetric *LtestFn2Coeffs = shGenCoeffs@shfns, testFn2DtestFn2SH = 8Θ, Φ< Ì Evaluate@shReconstruct@shfns, testFn2CoeffsDDplotFn2 = SphericalPlot3D@testFn2@Θ, ΦD,

8Θ, 0, Pi<, 8Φ, 0, 2 * Pi<, Mesh ® False, PlotRange ® FullDplotFn2SH = SphericalPlot3D@testFn2SH@Θ, ΦD, 8Θ, 0, Pi<,

8Φ, 0, 2 * Pi<, Mesh ® False, PlotRange ® FullDShow @plotFn2, plotFn2SHDH* Test convolution *LshIsZonal@testFnCoeffs, testNumLevelsDtestConvolvedCoeffs = shSymConvolution@testFn2Coeffs, testFnCoeffs, testNumLevelsDtestFnConvolvedSH = 8Θ, Φ< Ì Evaluate@shReconstruct@shfns, testConvolvedCoeffsDDplotConvolvedSH = SphericalPlot3D@testFnConvolvedSH@Θ, ΦD,

8Θ, 0, Pi<, 8Φ, 0, 2 * Pi<, Mesh ® False, PlotRange ® FullD

Out[60]= ::: Π >>, 880<, 80<, 80<<, :80<, :- 25

3 Π>, 80<, 80<, 80<>>

Out[61]= FunctionB8Θ, Φ<,Π + 5 Sin@2 ΘD Sin@ΦD

2 ΠF

SH_Test.nb 3

Page 4: Mathematica - Spherical Harmonics

Out[62]=

4 SH_Test.nb

Page 5: Mathematica - Spherical Harmonics

Out[63]=

SH_Test.nb 5

Page 6: Mathematica - Spherical Harmonics

Out[64]=

Out[65]=1

4

Π

3Flaten@880<, 8-1, 0, 1<, 8- 2, -1, 0, 1, 2<<D +

1

11Π Flaten@880<, 8-1, 0, 1<, 8- 2, -1, 0, 1, 2<<D +

10

1435 Π Flaten@880<, 8-1, 0, 1<, 8- 2, -1, 0, 1, 2<<D � 0

6 SH_Test.nb

Page 7: Mathematica - Spherical Harmonics

Out[66]= ::: 2 Π 3� 2

11 3>>, 880<, 80<, 80<<, :80<, :-

200 Π

21

143>, 80<, 80<, 80<>>

Out[67]= FunctionB8Θ, Φ<,91 3 Π + 300 35 Cos@ΘD Sin@ΘD Sin@ΦD

3003F

Out[68]=

SH_Test.nb 7