## Check orthonormalization of all spherical harmonics up to 3rd degree
# make indices l and m up to 3rd degree
l = c(0,rep(1,3),rep(2,5),rep(3,7))
m = c(0,seq(-1,1),seq(-2,2),seq(-3,3))
# check orthonormalization for all pairs
for (i in seq(16)) {
for (j in seq(16)) {
# compute scalar product
f = function(theta,phi) {
Yi = sphericalharmonics(l[i],m[i],cbind(theta,phi))
Yj = sphericalharmonics(l[j],m[j],cbind(theta,phi))
return(Re(Yi*Conj(Yj))*sin(theta))
}
g = Vectorize(function(phi) integrate(f,0,pi,phi)$value)
scalar.product = integrate(g,0,2*pi)$value
# compare scalar product to expected value
ok = abs(scalar.product-(i==j))<1e-6
cat(sprintf('(l=%1d,m=%+1d|l=%1d,m=%+1d)=%5.3f %s\n',l[i],m[i],l[j],m[j],
scalar.product+1e-10,ifelse(ok,'ok','wrong')))
}
}
Run the code above in your browser using DataLab