You are on page 1of 7

In[46]:= + Normalization part of spherical harmonics +

shNormalizationCoeffsl_, m_ := Sqrt2+ l +1+ l - m!4 + Pi + l + m!


+ 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+
shGetFnl_, m_ :=
SimplifyPiecewiseshNormalizationCoeffsl, 0+ LegendrePl, 0, Cos, m = 0,
Sqrt2+ shNormalizationCoeffsl, m+ Cosm + + LegendrePl, m, Cos, m > 0,
Sqrt2+ shNormalizationCoeffsl, -m+
Sin-m + + LegendrePl, -m, Cos, m < 0
+ 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... fnlist is the shorthand for Mapfn,list +
shIndicesl_ := Range-1, 1 & Range0, l
+ For each element of the shIndices list,
it replaces the corresponding shGetFn +
+ 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 shIndices is a list of lists +
shFunctionsl_ :=
MapIndexedlist, currLevel m shGetFncurrLevel -1, m list, shIndicesl
+ 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 +
shGenCoeffsshfns_, fn_ :=
MapIntegrate1 + fn, + Sin, , 0, Pi, , 0, 2+ Pi &, shfns
+ 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 Applyfn,params +
angleVarsDomain = Element, Reals,
Element, Reals, z 0, z 0, s Pi, z 2+ Pi;
shReconstructshfns_, shcoeffs_ := Simplify
Plus Flattenshcoeffs+ Flattenshfns, Assumptions - angleVarsDomain
+ Let's test what we have so far +
testNumLevels = 2;
shfns = shFunctionstestNumLevels
testFn_, _ := Cos^10 + UnitStepCos
+ Simple, symmetric around the z-axis +
+ generate coefficients and reconstructed SH function +
testFnCoeffs = shGenCoeffsshfns, testFn
testFnSH = , EvaluateshReconstructshfns, testFnCoeffs
+ plot original and reconstruction +
SphericalPlot3DtestFn, , testFnSH, ,
, 0, Pi, , 0, 2+ Pi, Mesh - False, PlotRange - Full
Out[9]=
1
2
,
1
2
3

Sin[]
2
Sin[],
1
2
3

Cos[],

1
2
3

Cos[] Sin[]
2
,
1
2
15

Cos[] Sin[]
2
Sin[],

1
2
15

Cos[] Sin[]
2
Sin[],
1
8
5

(1 3 Cos[2 ]),

1
2
15

Cos[] Cos[] Sin[]


2
,
1
4
15

Cos[2 ] Sin[]
2

Out[11]=

11
, {0],

3
4
, {0], {0], {0],
10 5
143
, {0], {0]
Out[12]= Function{, ],
Cos[]
8

3
572
(17 25 Cos[2 ])
Out[13]=
2 SH_Test .nb
In[28]:= + Checks if a given set of coefficients corresponds to zonal harmonics +
shIsZonalshcoeffs_, l_ := Plus FlatenshIndicesl+ Flattenshcoeffs = 0
+ Some utility functions +
shSymConvolveNormCoeffsl_ := MapIndexedlist, currLevel
TableSqrt4 + Pi 2+ currLevel +1, Lengthlist, shIndicesl
shExtractSymCoeffsshcoeffs_ :=
Table1CeilingLength1 2, Length1 & shcoeffs
+ Convolution with a kernel expressed via zonal harmonics,
symmetric around the z-axis +
shSymConvolutionshcoeffs_, shsymkerncoeffs_, l_ :=
CheckshIsZonalshsymkerncoeffs, err;
shSymConvolveNormCoeffsl+ shcoeffs + shExtractSymCoeffsshsymkerncoeffs
In[59]:= + Another test +
testFn2_, _ := UnitStepCos+ Sin + asymmetric +
testFn2Coeffs = shGenCoeffsshfns, testFn2
testFn2SH = , EvaluateshReconstructshfns, testFn2Coeffs
plotFn2 = SphericalPlot3DtestFn2, ,
, 0, Pi, , 0, 2+ Pi, Mesh - False, PlotRange - Full
plotFn2SH = SphericalPlot3DtestFn2SH, , , 0, Pi,
, 0, 2+ Pi, Mesh - False, PlotRange - Full
ShowplotFn2, plotFn2SH
+ Test convolution +
shIsZonaltestFnCoeffs, testNumLevels
testConvolvedCoeffs = shSymConvolutiontestFn2Coeffs, testFnCoeffs, testNumLevels
testFnConvolvedSH = , EvaluateshReconstructshfns, testConvolvedCoeffs
plotConvolvedSH = SphericalPlot3DtestFnConvolvedSH, ,
, 0, Pi, , 0, 2+ Pi, Mesh - False, PlotRange - Full
Out[60]= , {{0], {0], {0]], {0], 2
5
3
, {0], {0], {0]
Out[61]= Function{, ],
5 Sin[2 ] Sin[]
2

SH_Test .nb 3
Out[62]=
4 SH_Test .nb
Out[63]=
SH_Test .nb 5
Out[64]=
Out[65]=
1
4

3
Flaten[{{0], {1, 0, 1], {2, 1, 0, 1, 2]]]
1
11
Flaten[{{0], {1, 0, 1], {2, 1, 0, 1, 2]]]
10
143
5 Flaten[{{0], {1, 0, 1], {2, 1, 0, 1, 2]]] 0
6 SH_Test .nb
Out[66]=
2
3 2
11 3
, {{0], {0], {0]], {0],
200

21
143
, {0], {0], {0]
Out[67]= Function{, ],
91 3 300 35 Cos[] Sin[] Sin[]
3003

Out[68]=
SH_Test .nb 7

You might also like