Commit e3b53161a482af6681856f2c194977fd4ff3ce8e

Authored by David Mayerich
1 parent 26e84a59

fixed real/complex spherical harmonics bug

Showing 1 changed file with 14 additions and 2 deletions   Show diff stats
stim/math/spharmonics.h
@@ -25,8 +25,20 @@ protected: @@ -25,8 +25,20 @@ protected:
25 //calculate the value of the SH basis function (l, m) at (theta, phi) 25 //calculate the value of the SH basis function (l, m) at (theta, phi)
26 //here, theta = [0, PI], phi = [0, 2*PI] 26 //here, theta = [0, PI], phi = [0, 2*PI]
27 double SH(int l, int m, double theta, double phi){ 27 double SH(int l, int m, double theta, double phi){
28 - std::complex<T> result = boost::math::spherical_harmonic(l, m, phi, theta);  
29 - return result.imag() + result.real(); 28 + //std::complex<T> result = boost::math::spherical_harmonic(l, m, phi, theta);
  29 + //return result.imag() + result.real();
  30 +
  31 + //this calculation is based on calculating the real spherical harmonics:
  32 + // https://en.wikipedia.org/wiki/Spherical_harmonics#Addition_theorem
  33 + if (m < 0) {
  34 + return sqrt(2.0) * pow(-1, m) * boost::math::spherical_harmonic(l, abs(m), phi, theta).imag();
  35 + }
  36 + else if (m == 0) {
  37 + return boost::math::spherical_harmonic(l, m, phi, theta).real();
  38 + }
  39 + else {
  40 + return sqrt(2.0) * pow(-1, m) * boost::math::spherical_harmonic(l, m, phi, theta).real();
  41 + }
30 } 42 }
31 43
32 unsigned int coeff_1d(unsigned int l, int m){ 44 unsigned int coeff_1d(unsigned int l, int m){