1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <complex>
using namespace std;
const int LMAX = 50; // max l in series expansion
complex<double>coeff [1+LMAX][1+2*LMAX]; // coefficients in the expansion: coeff[l][l+m] is multiplier of Y_lm (** NOTE**)
const double PI = 3.14159265358979323846;
//======== Function prototypes
double testFunction( double theta, double phi ); // test function
void computeCoefficients( int ntheta, int nphi ); // computes coefficients in expansion
complex<double> surfaceIntegral( int ntheta, int nphi, int l, int m ); // does surface integral over sphere
complex<double> seriesSum( double theta, double phi ); // sum of the series
void outputCoefficients(); // outputs coefficients in expansion
void checkSeries(); // checks series sum against original
double doubleFactorial( int n ); // double factorial n!!
double ZLM( int l, int m, double x ); // (Renormalised) associated Legendre
double associatedLegendre( int l, int m, double x ); // Associated Legendre polynomial
complex<double> sphericalHarmonic( int l, int m, double theta, double phi ); // Spherical harmonic
//==================
int main()
{
const int NTHETA = 100; // number of angle intervals to do the surface integration
const int NPHI = 200;
cout << "Computing series expansion ...\n\n";
computeCoefficients( NTHETA, NPHI );
// outputCoefficients();
checkSeries(); // check against original function
}
//==================
double testFunction( double theta, double phi )
{
return cos( theta ) * sin( phi );
}
//==================
void computeCoefficients( int ntheta, int nphi ) // c_lm = Integral f Ylm* dA; NOTE: c_lm is held in coeff[l][l+m]
{
for ( int l = 0; l <= LMAX; l++ )
{
for ( int m = -l; m <= l; m++ ) coeff[l][l+m] = surfaceIntegral( ntheta, nphi, l, m );
}
}
//==================
complex<double> surfaceIntegral( int ntheta, int nphi, int l, int m )
{
double theta; // colatitude
double phi; // azimuth
double dtheta = PI / ntheta; // angle increment
double dphi = 2.0 * PI / nphi;
double dA; // element of area
complex<double> *Ylm0 = new complex<double>[ntheta]; // precompute theta part
for ( int i = 1; i <= ntheta; i++ )
{
theta = ( i - 0.5 ) * dtheta;
Ylm0[i-1] = sphericalHarmonic( l, m, theta, 0.0 );
}
complex<double> *phiPart = new complex<double>[nphi]; // precompute phi part
phiPart[0] = polar( 1.0, -m * 0.5 * dphi );
complex<double> multiplier = polar( 1.0, -m * dphi );
for ( int j = 1; j < nphi; j++ ) phiPart[j] = multiplier * phiPart[j-1];
complex<double> integral = 0.0;
for ( int i = 1; i <= ntheta; i++ ) // colatitude loop
{
theta = ( i - 0.5 ) * dtheta;
dA = sin( theta ) * dphi * dtheta;
for ( int j = 1; j <= nphi; j++ ) // azimuth loop
{
phi = ( j - 0.5 ) * dphi;
integral += testFunction( theta, phi ) * Ylm0[i-1] * phiPart[j-1] * dA;
}
}
delete [] Ylm0;
delete [] phiPart;
return integral;
}
//==================
complex<double> seriesSum( double theta, double phi )
{
complex<double> value = 0.0;
for ( int l = 0; l <= LMAX; l++ )
{
for ( int m = -l; m <= l; m++ ) value += coeff[l][l+m] * sphericalHarmonic( l, m, theta, phi );
}
return value;
}
//==================
void outputCoefficients()
{
for ( int l = 0; l <= LMAX; l++ )
{
for ( int m = -l; m <= l; m++ )
{
cout << "l=" << setw(4) << l << " m=" << setw(4) << m
<< " c_lm=" << showpos << scientific << setw(16) << setprecision(6)
<< coeff[l][l+m].real() << " " << coeff[l][l+m].imag() << endl;
}
}
}
//==================
void checkSeries()
{
double theta, phi;
char ans = 'y';
while( ans =='y' || ans == 'Y' )
{
cout << "Input colatitude (0 <= theta <= PI) and azimuth (0 <= phi <= 2 PI) in RADIANS: ";
cin >> theta >> phi;
cout << "Original function = " << testFunction( theta, phi ) << endl;
cout << "Sum of series = " << seriesSum( theta, phi ).real() << endl;
cout << endl;
cout << "Any more? (y/n): ";
cin >> ans;
}
}
//==================
double doubleFactorial( int n )
{
double f = 1.0;
while ( n > 1 )
{
f *= n;
n -= 2;
}
return f;
}
//==================
double ZLM( int l, int m, double x )
{
if ( l < 0 || m > l ) return 0.0;
// Special cases
if ( l == 0 ) return 1.0; // Z_00
else if ( l == 1 )
{
if ( m == 0 ) return x; // Z_10
else return -sqrt( 1.0 - x * x ); // Z_11
}
// Everything else by recursion
double Zlm, Zl1m, Zl2m;
Zl2m = ZLM( 0, m, x ); // recursive function call for one of the above
Zl1m = ZLM( 1, m, x );
for ( int i = 2; i <= l; i++ )
{
if ( i == m )
{
Zlm = pow( 1 - x * x, 0.5 * m );
if ( m %2 != 0 ) Zlm = -Zlm;
}
else
{
Zlm = 0.0;
if ( m <= i - 1 ) Zlm += Zl1m * x * ( 2 * i - 1 ); // recursion
if ( m <= i - 2 ) Zlm -= Zl2m * ( i + m - 1 );
if ( m < i ) Zlm /= ( i - m );
}
Zl2m = Zl1m;
Zl1m = Zlm;
}
return Zlm;
}
//==================
double associatedLegendre( int l, int m, double x )
{
if ( l < 0 || abs( m ) > l ) return 0.0;
int ma = abs( m );
double Plm = ZLM( l, ma, x );
if ( m >= 0 )
{
Plm = Plm * doubleFactorial( 2 * m - 1 );
}
else
{
for ( int i = 1; i <= ma; i++ ) Plm = Plm * ( 2.0 * i - 1.0 ) / ( ( l - ma + i ) * ( l + i ) );
if ( m %2 != 0 ) Plm = -Plm;
}
return Plm;
}
//==================
complex<double> sphericalHarmonic( int l, int m, double theta, double phi )
{
int ma = abs( m );
if ( l < 0 || ma > l ) return 0.0;
double norm = ( 2.0 * l + 1.0 ) / ( 4.0 * PI );
for ( int i = 1; i <= ma; i++ )
{
norm = norm * ( ( 2.0 * i - 1.0 ) / ( l - ma + i ) ) * ( ( 2.0 * i - 1.0 ) / ( l + i ) );
}
norm = sqrt( norm );
complex<double> Ylm = norm * ZLM( l, ma, cos( theta ) ) * polar( 1.0, m * phi );
if ( m < 0 && m % 2 != 0 ) Ylm = -Ylm;
return Ylm;
}
//==================
|