6 #include "nf_Legendre.h"
8 #if defined __cplusplus
15 double mu1,
mu2, f1, f2;
23 nf_Legendre *
nf_Legendre_new(
int initialSize,
int maxOrder,
double *Cls, nfu_status *status ) {
26 nf_Legendre *Legendre = (nf_Legendre *)
nfu_malloc(
sizeof( nf_Legendre ) );
28 *status = nfu_mallocError;
29 if( Legendre == NULL )
return( NULL );
30 if( ( *status =
nf_Legendre_setup( Legendre, initialSize, maxOrder ) ) != nfu_Okay ) {
34 for( l = 0; l <= Legendre->maxOrder; l++ ) Legendre->Cls[l] = Cls[l];
42 memset( Legendre, 0,
sizeof( nf_Legendre ) );
43 if( maxOrder < 0 ) maxOrder = -1;
44 if( maxOrder > nf_Legendre_maxMaxOrder ) maxOrder = nf_Legendre_maxMaxOrder;
45 Legendre->maxOrder = maxOrder;
46 if( initialSize < ( maxOrder + 1 ) ) initialSize = maxOrder + 1;
54 if( Legendre->allocated > 0 )
nfu_free( Legendre->Cls );
55 memset( Legendre, 0,
sizeof( nf_Legendre ) );
79 nfu_status status = nfu_Okay;
81 if( size < nf_Legendre_minMaxOrder ) size = nf_Legendre_minMaxOrder;
82 if( size > ( nf_Legendre_maxMaxOrder + 1 ) ) size = nf_Legendre_maxMaxOrder + 1;
83 if( size != Legendre->allocated ) {
84 if( size > Legendre->allocated ) {
85 Legendre->Cls = (
double *)
nfu_realloc( size *
sizeof(
double ), Legendre->Cls ); }
87 if( size < ( Legendre->maxOrder + 1 ) ) size = Legendre->maxOrder + 1;
88 if( ( Legendre->allocated > 2 * size ) || forceSmallerResize ) {
89 Legendre->Cls = (
double *)
nfu_realloc( size *
sizeof(
double ), Legendre->Cls ); }
91 size = Legendre->allocated;
94 if( Legendre->Cls == NULL ) {
96 status = nfu_mallocError;
98 Legendre->allocated = size;
107 return( Legendre->maxOrder );
114 return( Legendre->allocated );
122 if( ( l < 0 ) || ( l > Legendre->maxOrder ) ) {
123 *status = nfu_badIndex;
126 return( Legendre->Cls[l] );
135 if( ( l < 0 ) || ( l > ( Legendre->maxOrder + 1 ) ) )
return( nfu_badIndex );
136 if( Legendre->allocated <= l ) {
139 if( l > Legendre->maxOrder ) Legendre->maxOrder = l;
140 Legendre->Cls[l] = Cl;
151 if( Legendre->maxOrder >= 0 ) {
152 if( ( norm = Legendre->Cls[0] ) == 0 )
return( nfu_divByZero );
153 for( l = 0; l <= Legendre->maxOrder; l++ ) Legendre->Cls[l] /= norm;
165 *status = nfu_XOutsideDomain;
166 if( ( mu >= -1. ) && ( mu <= 1. ) ) {
168 for( l = 0; l <= Legendre->maxOrder; l++ ) P += ( l + 0.5 ) * Legendre->Cls[l] *
nf_Legendre_PofL_atMu( l, mu );
178 double Pl_minus1, Pl, Pl_plus1;
209 for( l_ = 0, twoL_plus1 = 1; l_ < l; l_++, twoL_plus1 += 2 ) {
212 Pl_plus1 = ( twoL_plus1 * mu * Pl - l_ * Pl_minus1 ) / ( l_ + 1 );
219 ptwXYPoints *
nf_Legendre_to_ptwXY( nf_Legendre *Legendre,
double accuracy,
int biSectionMax,
int checkForRoots, nfu_status *status ) {
223 void *argList = (
void *) Legendre;
227 if( Legendre->maxOrder > 1 ) {
228 n = Legendre->maxOrder - 1;
229 if( n > 249 ) n = 249;
232 for( i = 1; i <
n; i++ ) xs[i] = xs[i-1] + dx;
253 nf_Legendre *Legendre;
254 double mu1, mu2, f1, f2, Cl, Cls[1] = { 0 }, integral;
257 if( ( *status =
ptwXY_getStatus( ptwXY ) ) != nfu_Okay )
return( NULL );
261 *status = nfu_XOutsideDomain;
267 *status = nfu_XOutsideDomain;
271 if( ( Legendre =
nf_Legendre_new( maxOrder + 1, -1, Cls, status ) ) == NULL )
return( NULL );
273 if( maxOrder > nf_Legendre_maxMaxOrder ) maxOrder = nf_Legendre_maxMaxOrder;
274 for( l = 0; l <= maxOrder; l++ ) {
277 for( i = 1, Cl = 0; i <
n; i++ ) {
304 *f = ( args->
f1 * ( args->
mu2 - mu ) + args->
f2 * ( mu - args->
mu1 ) ) / ( args->
mu2 - args->
mu1 );
309 #if defined __cplusplus
void * nfu_realloc(size_t size, void *old)
int64_t ptwXY_length(ptwXYPoints *ptwXY)
nf_Legendre * nf_Legendre_from_ptwXY(ptwXYPoints *ptwXY, int maxOrder, nfu_status *status)
nf_Legendre * nf_Legendre_clone(nf_Legendre *nfL, nfu_status *status)
nfu_status nf_Legendre_release(nf_Legendre *Legendre)
double nf_Legendre_evauluateAtMu(nf_Legendre *Legendre, double mu, nfu_status *status)
nfu_status nf_Legendre_setCl(nf_Legendre *Legendre, int l, double Cl)
nf_Legendre * nf_Legendre_new(int initialSize, int maxOrder, double *Cls, nfu_status *status)
nfu_status ptwXY_getXYPairAtIndex(ptwXYPoints *ptwXY, int64_t index, double *x, double *y)
nfu_status nf_Legendre_setup(nf_Legendre *Legendre, int initialSize, int maxOrder)
int nf_Legendre_maxOrder(nf_Legendre *Legendre)
nfu_status nf_Legendre_normalize(nf_Legendre *Legendre)
static nfu_status nf_Legendre_to_ptwXY2(double mu, double *P, void *argList)
int nf_Legendre_allocated(nf_Legendre *Legendre)
static nfu_status nf_Legendre_from_ptwXY_callback(double mu, double *f, void *argList)
ptwXYPoints * ptwXY_createFromFunction(int n, double *xs, ptwXY_createFromFunction_callback func, void *argList, double accuracy, int checkForRoots, int biSectionMax, nfu_status *status)
nfu_status nf_Legendre_GaussianQuadrature(int degree, double x1, double x2, nf_Legendre_GaussianQuadrature_callback func, void *argList, double *integral)
nf_Legendre * nf_Legendre_free(nf_Legendre *Legendre)
nfu_status nf_Legendre_reallocateCls(nf_Legendre *Legendre, int size, int forceSmallerResize)
ptwXYPoints * nf_Legendre_to_ptwXY(nf_Legendre *Legendre, double accuracy, int biSectionMax, int checkForRoots, nfu_status *status)
double nf_Legendre_getCl(nf_Legendre *Legendre, int l, nfu_status *status)
nfu_status ptwXY_getStatus(ptwXYPoints *ptwXY)
double nf_Legendre_PofL_atMu(int l, double mu)
void * nfu_malloc(size_t size)