2018-02-07 15:39:41 +02:00
|
|
|
#include "qpms_specfunc.h"
|
|
|
|
#include "qpms_types.h"
|
|
|
|
#include <gsl/gsl_sf_legendre.h>
|
|
|
|
#include <gsl/gsl_math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "indexing.h"
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
// Legendre functions also for negative m, see DLMF 14.9.3
|
|
|
|
qpms_errno_t qpms_legendre_deriv_y_fill(double *target, double *target_deriv, double x, qpms_l_t lMax,
|
2018-05-06 22:13:10 +03:00
|
|
|
gsl_sf_legendre_t lnorm, double csphase)
|
2018-02-07 15:39:41 +02:00
|
|
|
{
|
2018-05-06 22:13:10 +03:00
|
|
|
size_t n = gsl_sf_legendre_array_n(lMax);
|
|
|
|
double *legendre_tmp = malloc(n * sizeof(double));
|
|
|
|
double *legendre_deriv_tmp = malloc(n * sizeof(double));
|
|
|
|
int gsl_errno = gsl_sf_legendre_deriv_array_e(
|
|
|
|
lnorm, (size_t)lMax, x, csphase, legendre_tmp,legendre_deriv_tmp);
|
|
|
|
for (qpms_l_t l = 1; l <= lMax; ++l)
|
|
|
|
for (qpms_m_t m = 0; m <= l; ++m) {
|
|
|
|
qpms_y_t y = qpms_mn2y(m,l);
|
|
|
|
size_t i = gsl_sf_legendre_array_index(l,m);
|
|
|
|
target[y] = legendre_tmp[i];
|
|
|
|
target_deriv[y] = legendre_deriv_tmp[i];
|
|
|
|
}
|
|
|
|
switch(lnorm) {
|
|
|
|
case GSL_SF_LEGENDRE_NONE:
|
|
|
|
for (qpms_l_t l = 1; l <= lMax; ++l)
|
|
|
|
for (qpms_m_t m = 1; m <= l; ++m) {
|
|
|
|
qpms_y_t y = qpms_mn2y(-m,l);
|
|
|
|
size_t i = gsl_sf_legendre_array_index(l,m);
|
|
|
|
// viz DLMF 14.9.3, čert ví, jak je to s cs fasí.
|
|
|
|
double factor = exp(lgamma(l-m+1)-lgamma(l+m+1))*((m%2)?-1:1);
|
|
|
|
target[y] = factor * legendre_tmp[i];
|
|
|
|
target_deriv[y] = factor * legendre_deriv_tmp[i];
|
2018-02-07 15:39:41 +02:00
|
|
|
}
|
2018-05-06 22:13:10 +03:00
|
|
|
break;
|
|
|
|
case GSL_SF_LEGENDRE_SCHMIDT:
|
|
|
|
case GSL_SF_LEGENDRE_SPHARM:
|
|
|
|
case GSL_SF_LEGENDRE_FULL:
|
|
|
|
for (qpms_l_t l = 1; l <= lMax; ++l)
|
|
|
|
for (qpms_m_t m = 1; m <= l; ++m) {
|
|
|
|
qpms_y_t y = qpms_mn2y(-m,l);
|
|
|
|
size_t i = gsl_sf_legendre_array_index(l,m);
|
|
|
|
// viz DLMF 14.9.3, čert ví, jak je to s cs fasí.
|
|
|
|
double factor = ((m%2)?-1:1); // this is the difference from the unnormalised case
|
|
|
|
target[y] = factor * legendre_tmp[i];
|
|
|
|
target_deriv[y] = factor * legendre_deriv_tmp[i];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort(); //NI
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(legendre_tmp);
|
|
|
|
free(legendre_deriv_tmp);
|
|
|
|
return QPMS_SUCCESS;
|
2018-02-07 15:39:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
qpms_errno_t qpms_legendre_deriv_y_get(double **target, double **dtarget, double x, qpms_l_t lMax, gsl_sf_legendre_t lnorm,
|
2018-05-06 22:13:10 +03:00
|
|
|
double csphase)
|
2018-02-07 15:39:41 +02:00
|
|
|
{
|
|
|
|
|
2018-05-06 22:13:10 +03:00
|
|
|
*target = malloc(sizeof(double)*qpms_lMax2nelem(lMax));
|
|
|
|
*dtarget = malloc(sizeof(double)*qpms_lMax2nelem(lMax));
|
|
|
|
return qpms_legendre_deriv_y_fill(*target, *dtarget, x, lMax, lnorm, csphase);
|
2018-02-07 15:39:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
qpms_pitau_t qpms_pitau_get(double theta, qpms_l_t lMax, qpms_normalisation_t norm)
|
|
|
|
{
|
2018-05-06 22:13:10 +03:00
|
|
|
const double csphase = qpms_normalisation_t_csphase(norm);
|
|
|
|
norm = qpms_normalisation_t_normonly(norm);
|
|
|
|
qpms_pitau_t res;
|
|
|
|
qpms_y_t nelem = qpms_lMax2nelem(lMax);
|
|
|
|
res.pi = malloc(nelem * sizeof(double));
|
|
|
|
res.tau = malloc(nelem * sizeof(double));
|
|
|
|
double ct = cos(theta), st = sin(theta);
|
|
|
|
if (1 == fabs(ct)) { // singular case, use DLMF 14.8.2
|
|
|
|
memset(res.pi, 0, nelem*sizeof(double));
|
|
|
|
memset(res.tau, 0, nelem*sizeof(double));
|
|
|
|
res.leg = calloc(nelem, sizeof(double));
|
|
|
|
switch(norm) {
|
2018-05-08 15:39:12 +03:00
|
|
|
/* FIXME get rid of multiplicating the five lines */
|
|
|
|
case QPMS_NORMALISATION_NONE:
|
2018-05-06 22:13:10 +03:00
|
|
|
for (qpms_l_t l = 1; l <= lMax; ++l) {
|
|
|
|
res.leg[qpms_mn2y(0, l)] = (l%2)?ct:1.;
|
|
|
|
double p = l*(l+1)/2;
|
|
|
|
const double n = 0.5;
|
|
|
|
int lpar = (l%2)?-1:1;
|
|
|
|
res.pi [qpms_mn2y(+1, l)] = -((ct>0) ? -1 : lpar) * p * csphase;
|
|
|
|
res.pi [qpms_mn2y(-1, l)] = -((ct>0) ? -1 : lpar) * n * csphase;
|
|
|
|
res.tau[qpms_mn2y(+1, l)] = ((ct>0) ? +1 : lpar) * p * csphase;
|
|
|
|
res.tau[qpms_mn2y(-1, l)] = -((ct>0) ? +1 : lpar) * n * csphase;
|
|
|
|
}
|
|
|
|
break;
|
2018-05-09 11:51:41 +03:00
|
|
|
#ifdef USE_XU_ANTINORMALISATION // broken
|
2018-05-08 15:39:12 +03:00
|
|
|
case QPMS_NORMALISATION_XU: // Rather useless except for testing.
|
|
|
|
for (qpms_l_t l = 1; l <= lMax; ++l) {
|
|
|
|
res.leg[qpms_mn2y(0, l)] = ((l%2)?ct:1.)*sqrt(0.25*M_1_PI *(2*l+1)/(l*(l+1)));
|
2018-05-09 11:51:41 +03:00
|
|
|
double p = l*(l+1)/2 /* as in _NONE */
|
|
|
|
* sqrt(0.25 * M_1_PI * (2*l + 1));
|
|
|
|
double n = 0.5 /* as in _NONE */
|
|
|
|
* sqrt(0.25 * M_1_PI * (2*l + 1)) / (l * (l+1));
|
2018-05-08 15:39:12 +03:00
|
|
|
int lpar = (l%2)?-1:1;
|
|
|
|
res.pi [qpms_mn2y(+1, l)] = -((ct>0) ? -1 : lpar) * p * csphase;
|
|
|
|
res.pi [qpms_mn2y(-1, l)] = -((ct>0) ? -1 : lpar) * n * csphase;
|
|
|
|
res.tau[qpms_mn2y(+1, l)] = ((ct>0) ? +1 : lpar) * p * csphase;
|
|
|
|
res.tau[qpms_mn2y(-1, l)] = -((ct>0) ? +1 : lpar) * n * csphase;
|
|
|
|
}
|
|
|
|
break;
|
2018-05-09 11:51:41 +03:00
|
|
|
#endif
|
2018-05-06 22:13:10 +03:00
|
|
|
case QPMS_NORMALISATION_TAYLOR:
|
|
|
|
for (qpms_l_t l = 1; l <= lMax; ++l) {
|
|
|
|
res.leg[qpms_mn2y(0, l)] = ((l%2)?ct:1.)*sqrt((2*l+1)*0.25*M_1_PI);
|
|
|
|
double fl = 0.25 * sqrt((2*l+1)*l*(l+1)*M_1_PI);
|
2018-05-08 15:39:12 +03:00
|
|
|
int lpar = (l%2)?-1:1;
|
2018-05-06 22:13:10 +03:00
|
|
|
res.pi [qpms_mn2y(+1, l)] = -((ct>0) ? -1 : lpar) * fl * csphase;
|
|
|
|
res.pi [qpms_mn2y(-1, l)] = -((ct>0) ? -1 : lpar) * fl * csphase;
|
|
|
|
res.tau[qpms_mn2y(+1, l)] = ((ct>0) ? +1 : lpar) * fl * csphase;
|
|
|
|
res.tau[qpms_mn2y(-1, l)] = -((ct>0) ? +1 : lpar) * fl * csphase;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QPMS_NORMALISATION_POWER:
|
|
|
|
for (qpms_l_t l = 1; l <= lMax; ++l) {
|
|
|
|
res.leg[qpms_mn2y(0, l)] = ((l%2)?ct:1.)*sqrt((2*l+1)/(4*M_PI *l*(l+1)));
|
|
|
|
double fl = 0.25 * sqrt((2*l+1)*M_1_PI);
|
2018-05-08 15:39:12 +03:00
|
|
|
int lpar = (l%2)?-1:1;
|
2018-05-06 22:13:10 +03:00
|
|
|
res.pi [qpms_mn2y(+1, l)] = -((ct>0) ? -1 : lpar) * fl * csphase;
|
|
|
|
res.pi [qpms_mn2y(-1, l)] = -((ct>0) ? -1 : lpar) * fl * csphase;
|
|
|
|
res.tau[qpms_mn2y(+1, l)] = ((ct>0) ? +1 : lpar) * fl * csphase;
|
|
|
|
res.tau[qpms_mn2y(-1, l)] = -((ct>0) ? +1 : lpar) * fl * csphase;
|
2018-02-07 15:39:41 +02:00
|
|
|
}
|
2018-05-06 22:13:10 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { // cos(theta) in (-1,1), use normal calculation
|
|
|
|
double *legder = malloc(sizeof(double)*qpms_lMax2nelem(lMax));
|
|
|
|
res.leg = malloc(sizeof(double)*qpms_lMax2nelem(lMax));
|
|
|
|
if (qpms_legendre_deriv_y_fill(res.leg, legder, ct, lMax,
|
2018-05-08 15:39:12 +03:00
|
|
|
norm == QPMS_NORMALISATION_NONE ? GSL_SF_LEGENDRE_NONE
|
2018-05-06 22:13:10 +03:00
|
|
|
: GSL_SF_LEGENDRE_SPHARM, csphase))
|
|
|
|
abort();
|
|
|
|
if (norm == QPMS_NORMALISATION_POWER)
|
2018-05-08 15:39:12 +03:00
|
|
|
/* for None (=non-normalized) and Taylor (=sph. harm. normalized)
|
2018-05-06 22:13:10 +03:00
|
|
|
* the correct normalisation is already obtained from gsl_sf_legendre_deriv_array_e().
|
|
|
|
* However, Kristensson ("power") normalisation differs from Taylor
|
|
|
|
* by 1/sqrt(l*(l+1)) factor.
|
|
|
|
*/
|
|
|
|
for (qpms_l_t l = 1; l <= lMax; ++l) {
|
|
|
|
double prefac = 1./sqrt(l*(l+1));
|
|
|
|
for (qpms_m_t m = -l; m <= l; ++m) {
|
|
|
|
res.leg[qpms_mn2y(m,l)] *= prefac;
|
|
|
|
legder[qpms_mn2y(m,l)] *= prefac;
|
2018-02-07 15:39:41 +02:00
|
|
|
}
|
2018-05-06 22:13:10 +03:00
|
|
|
}
|
2018-05-09 11:51:41 +03:00
|
|
|
#ifdef USE_XU_ANTINORMALISATION
|
2018-05-08 15:39:12 +03:00
|
|
|
else if (norm == QPMS_NORMALISATION_XU)
|
|
|
|
/* for Xu (anti-normalized), we start from spharm-normalized Legendre functions
|
|
|
|
* Do not use this normalisation except for testing
|
|
|
|
*/
|
2018-05-09 11:51:41 +03:00
|
|
|
// FIXME PROBABLY BROKEN HERE
|
2018-05-08 15:39:12 +03:00
|
|
|
for (qpms_l_t l = 1; l <= lMax; ++l) {
|
|
|
|
double prefac = (2*l + 1) / sqrt(4*M_PI / (l*(l+1)));
|
|
|
|
for (qpms_m_t m = -l; m <= l; ++m) {
|
|
|
|
double fac = prefac * exp(lgamma(l+m+1) - lgamma(l-m+1));
|
|
|
|
res.leg[qpms_mn2y(m,l)] *= fac;
|
|
|
|
legder[qpms_mn2y(m,l)] *= fac;
|
|
|
|
}
|
|
|
|
}
|
2018-05-09 11:51:41 +03:00
|
|
|
#endif
|
2018-05-06 22:13:10 +03:00
|
|
|
for (qpms_l_t l = 1; l <= lMax; ++l) {
|
|
|
|
for (qpms_m_t m = -l; m <= l; ++m) {
|
|
|
|
res.pi [qpms_mn2y(m,l)] = m / st * res.leg[qpms_mn2y(m,l)];
|
|
|
|
res.tau[qpms_mn2y(m,l)] = - st * legder[qpms_mn2y(m,l)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(legder);
|
|
|
|
}
|
|
|
|
res.lMax = lMax;
|
|
|
|
return res;
|
2018-02-07 15:39:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void qpms_pitau_free(qpms_pitau_t x) {
|
2018-05-06 22:13:10 +03:00
|
|
|
free(x.leg);
|
|
|
|
free(x.pi);
|
|
|
|
free(x.tau);
|
2018-02-07 15:39:41 +02:00
|
|
|
}
|
|
|
|
|