static gsl_poly_int* mygsl_poly_laguerre(int n) { size_t m, k; int val; gsl_vector_int *p0; if (n < 0) rb_raise(rb_eArgError, "order must be >= 0"); p0 = gsl_vector_int_calloc(n + 1); switch (n) { case 0: gsl_vector_int_set(p0, 0, 1); break; case 1: gsl_vector_int_set(p0, 0, 1); gsl_vector_int_set(p0, 1, -1); break; default: k = gsl_sf_fact(n); for (m = 0; m <= n; m++) { val = k*k/gsl_sf_fact(n-m)/gsl_pow_2(gsl_sf_fact(m)); if (m%2 == 1) val *= -1; gsl_vector_int_set(p0, m, val); } break; } return p0; }
static gsl_poly_int* mygsl_poly_bessel(int n) { size_t k; gsl_vector_int *p0; if (n < 0) rb_raise(rb_eArgError, "order must be >= 0"); p0 = gsl_vector_int_calloc(n + 1); for (k = 0; k <= n; k++) { gsl_vector_int_set(p0, k, gsl_sf_fact(n+k)/gsl_sf_fact(n-k)/gsl_sf_fact(k)/((int) pow(2, k))); } return p0; }
void ambisonicWeight::computeInPhaseOptim() { m_optimMode = "inPhase"; for (int i = 0; i < m_number_of_harmonics; i++) { if (i == 0) m_optimVector[i] = 1.; else m_optimVector[i] = pow(gsl_sf_fact(m_order), 2) / ( gsl_sf_fact(m_order+abs(m_index_of_harmonics[i])) * gsl_sf_fact(m_order-abs(m_index_of_harmonics[i]))); } }
double Anl_tilde(int n ,int l){ double K_nl, factor, A_nl; double gamma_factor; K_nl = 0.5*n*(n+4.*l+3.) + (l+1.)*(2.*l+1.); factor = pow(2,8.*l+6.) / (4.*M_PI*K_nl); gamma_factor = pow(gsl_sf_gamma(2.0*l+1.5),2)/gsl_sf_gamma(n+4.*l+3.); A_nl =-factor* gsl_sf_fact(n)*(n+2.*l+1.5)*gamma_factor; return A_nl; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int i, j; int N, Nperm; int *d; int subs[2]; double *ptr; gsl_permutation *c; if(nrhs != 1) mexErrMsgTxt("1 input required"); if(nlhs != 1) mexErrMsgTxt("Requires one output."); if(mxGetM(prhs[0]) * mxGetN(prhs[0]) != 1) mexErrMsgTxt("N must be scalar"); N = mxGetScalar(prhs[0]); Nperm = (int) (gsl_sf_fact(N)); c = gsl_permutation_calloc (N); gsl_permutation_init(c); plhs[0] = mxCreateDoubleMatrix(Nperm, N, mxREAL); ptr = mxGetPr(plhs[0]); for(i = 0; i < Nperm; i++) { d = gsl_permutation_data(c); for(j = 0; j < N; j++) { subs[0] = i; subs[1] = j; ptr[mxCalcSingleSubscript(plhs[0], 2, subs)] = (double)d[j] + 1.; } gsl_permutation_next(c); } gsl_permutation_free(c); }
void AmbisonicEase::computeVectors() { m_index_of_harmonics = new long[m_number_of_harmonics ]; m_ambiCoeffs = new double[m_number_of_harmonics]; m_optimVector = new double[m_number_of_harmonics]; m_minus_vector = new double[m_number_of_harmonics]; m_dot_vector = new double[m_number_of_harmonics]; m_widen_vector = new double[m_number_of_harmonics]; m_index_of_harmonics[0] = 0; m_order_weight = log((double)(m_order + 1)); for(int i = 1; i < m_number_of_harmonics; i++) { m_index_of_harmonics[i] = (i - 1) / 2 + 1; if (i % 2 == 1) m_index_of_harmonics[i] = - m_index_of_harmonics[i]; } for(int i = 1; i < m_number_of_harmonics; i++) { m_minus_vector[i] = Tools::clip_min(log((double)abs(m_index_of_harmonics[i])), 0.); m_dot_vector[i] = Tools::clip_min(log((double)abs(m_index_of_harmonics[i]) + 1.), 0.); m_dot_vector[i] -= m_minus_vector[i]; m_dot_vector[i] = 1. / m_dot_vector[i]; } for (int i = 0; i < NUMBEROFCIRCLEPOINTS; i++) { m_cosLookUp[i] = cos((double)i * CICM_2PI / (double)NUMBEROFCIRCLEPOINTS); m_sinLookUp[i] = sin((double)i * CICM_2PI / (double)NUMBEROFCIRCLEPOINTS); } for (int i = 0; i < m_number_of_harmonics; i++) { if (i == 0) m_optimVector[i] = 1.; else m_optimVector[i] = pow(gsl_sf_fact(m_order), 2) / ( gsl_sf_fact(m_order+abs(m_index_of_harmonics[i])) * gsl_sf_fact(m_order-abs(m_index_of_harmonics[i]))); } }
//------------------------------------------------------------------------------ /// \f$ n! \f$ inline double factorial(const unsigned int n) { return gsl_sf_fact(n); }
/** * Function to calculate associated Legendre function used by the spherical harmonics function */ static REAL8 XLALAssociatedLegendreXIsZero( const int l, const int m ) { REAL8 legendre; if ( l < 0 ) { XLALPrintError( "l cannot be < 0\n" ); XLAL_ERROR_REAL8( XLAL_EINVAL ); } if ( m < 0 || m > l ) { XLALPrintError( "Invalid value of m!\n" ); XLAL_ERROR_REAL8( XLAL_EINVAL ); } /* we will switch on the values of m and n */ switch ( l ) { case 1: switch ( m ) { case 1: legendre = - 1.; break; default: XLAL_ERROR_REAL8( XLAL_EINVAL ); } break; case 2: switch ( m ) { case 2: legendre = 3.; break; case 1: legendre = 0.; break; default: XLAL_ERROR_REAL8( XLAL_EINVAL ); } break; case 3: switch ( m ) { case 3: legendre = -15.; break; case 2: legendre = 0.; break; case 1: legendre = 1.5; break; default: XLAL_ERROR_REAL8( XLAL_EINVAL ); } break; case 4: switch ( m ) { case 4: legendre = 105.; break; case 3: legendre = 0.; break; case 2: legendre = - 7.5; break; case 1: legendre = 0; break; default: XLAL_ERROR_REAL8( XLAL_EINVAL ); } break; case 5: switch ( m ) { case 5: legendre = - 945.; break; case 4: legendre = 0.; break; case 3: legendre = 52.5; break; case 2: legendre = 0; break; case 1: legendre = - 1.875; break; default: XLAL_ERROR_REAL8( XLAL_EINVAL ); } break; case 6: switch ( m ) { case 6: legendre = 10395.; break; case 5: legendre = 0.; break; case 4: legendre = - 472.5; break; case 3: legendre = 0; break; case 2: legendre = 13.125; break; case 1: legendre = 0; break; default: XLAL_ERROR_REAL8( XLAL_EINVAL ); } break; case 7: switch ( m ) { case 7: legendre = - 135135.; break; case 6: legendre = 0.; break; case 5: legendre = 5197.5; break; case 4: legendre = 0.; break; case 3: legendre = - 118.125; break; case 2: legendre = 0.; break; case 1: legendre = 2.1875; break; default: XLAL_ERROR_REAL8( XLAL_EINVAL ); } break; case 8: switch ( m ) { case 8: legendre = 2027025.; break; case 7: legendre = 0.; break; case 6: legendre = - 67567.5; break; case 5: legendre = 0.; break; case 4: legendre = 1299.375; break; case 3: legendre = 0.; break; case 2: legendre = - 19.6875; break; case 1: legendre = 0.; break; default: XLAL_ERROR_REAL8( XLAL_EINVAL ); } break; default: XLALPrintError( "Unsupported (l, m): %d, %d\n", l, m ); XLAL_ERROR_REAL8( XLAL_EINVAL ); } legendre *= sqrt( (REAL8)(2*l+1)*gsl_sf_fact( l-m ) / (4.*LAL_PI*gsl_sf_fact(l+m))); return legendre; }
double ho_A (int n, int l, double b) { return sqrt (2. * gsl_sf_fact ((unsigned) (n - 1)) / (b * gsl_sf_gamma ((double)n + (double)l + 1. / 2.))); }
scalar sasfit_peak_QENS_ConfinementWithGaussianPotential(scalar energy, sasfit_param * param) { scalar gamma,v,q,sigma,hbar,de,q2,a0,an, sqe; int i; sasfit_param subParam; SASFIT_ASSERT_PTR( param ); sigma = SIGMA0+Q*SIGMA1+Q*Q*SIGMA2; SASFIT_CHECK_COND1((sigma < 0), param, "sigma(%lg) < 0",sigma); SASFIT_CHECK_COND1((U2 < 0), param, "<u^2>(%lg) < 0",U2); q = Q/1.e-10; // convertion from Angstrom in meter q2 = q*q; a0 = exp(-q2*U2); de = energy-E0; if (sigma == 0.0) { if (de == 0.0) { sqe = a0; } else { sqe = 0.0; } } else { if (de ==0.0) { sqe = a0/(sqrt(2.0*M_PI)*sigma); } else { sqe = a0/(sqrt(2.0*M_PI)*sigma)*exp(-de*de/(2.0*sigma*sigma)); } } sqe = AMPL*sqe; // hbar in meV hbar = GSL_CONST_MKSA_PLANCKS_CONSTANT_HBAR / GSL_CONST_MKSA_ELECTRON_VOLT * 1e3; sasfit_init_param( &subParam ); for (i=1; i<=N; i++) { if (U2 == 0.0) break; gamma = i*hbar*DIFF/U2; if (q2*U2 == 0.0) { an=0.0; } else { // an = a0* exp(i*log(q2*u2)-gsl_sf_lngamma(i+1)); an = a0 *pow(q2*U2,i)/gsl_sf_fact(i); } if (sigma == 0.0) { if ((gamma == 0.0) && (de == 0.0)) { v = 1/M_PI; } else { v = gamma/(gamma*gamma+de*de)/M_PI; } } else { subParam.p[0] = 1.0; subParam.p[1] = 0.0; subParam.p[2] = sigma; subParam.p[3] = gamma; subParam.p[4] = 0.0; v = sasfit_peak_VoigtPeakArea(de,&subParam); if (subParam.errStatus) { param->errStatus = subParam.errStatus; strcpy(param->errStr,subParam.errStr); return sqe; } if (v<=0) { fprintf(stderr,"energy=%lg v=%lg\ngamma=%lg sigma=%lg energy=%lg\n", energy, v, gamma, sigma, E0); } } sqe = sqe+AMPL*an*v; } return sqe+BACKGR; }
SET_METHOD( Polymorph, SSystemMatrix ) { SSystemMatrix = value; PolymorphVector aValueVector( value.as<PolymorphVector>() ); theSystemSize = aValueVector.size(); // init Substance Vector theY.resize( boost::extents[ theSystemSize + 1 ][ Order + 1 ] ); // init S-System Vector & Matrix theAlpha.resize( boost::extents[ theSystemSize + 1 ] ); theBeta.resize( boost::extents[ theSystemSize + 1] ); theG.resize( boost::extents[ theSystemSize + 1 ][ theSystemSize + 1 ] ); theH.resize( boost::extents[ theSystemSize + 1 ][ theSystemSize + 1 ] ); // init S-System tmp Vector & Matrix theAlphaBuffer.resize( boost::extents[ theSystemSize + 1 ][ Order + 1 ] ); theBetaBuffer.resize( boost::extents[ theSystemSize + 1 ][ Order + 1 ] ); theGBuffer.resize( boost::extents[ theSystemSize + 1 ][ Order + 1 ] ); theHBuffer.resize( boost::extents[ theSystemSize + 1 ][ Order + 1 ] ); theFBuffer.resize( boost::extents[ Order + 1 ][ Order + 1 ] ); // init Factorial matrix for(int m( 2 ) ; m < Order+1 ; m++) { for(int q( 1 ); q < m ; q++) { const Real aFact( 1 / gsl_sf_fact(q-1) * gsl_sf_fact(m-q-1) * m * (m-1) ); (theFBuffer[m])[q] = aFact; } } // set Alpha, Beta, G, H for( int i( 0 ); i < theSystemSize; ++i ) { theAlpha[i+1] = (aValueVector[i].as<PolymorphVector>())[0].as<Real>() ; for( int j( 0 ); j < theSystemSize; ++j ) { if( i == j ) { (theG[i+1])[j+1] = (aValueVector[i].as<PolymorphVector>())[j+1].as<Real>() - 1 ; } else { (theG[i+1])[j+1] = (aValueVector[i].as<PolymorphVector>())[j+1].as<Real>() ; } } theBeta[i+1] = (aValueVector[i].as<PolymorphVector>())[1+theSystemSize].as<Real>() ; for( int j( 0 ); j < theSystemSize; ++j ) { if( i == j ) { (theH[i+1])[j+1] = (aValueVector[i].as<PolymorphVector>())[2+j+theSystemSize].as<Real>() -1 ; } else { (theH[i+1])[j+1] = (aValueVector[i].as<PolymorphVector>())[2+j+theSystemSize].as<Real>() ; } } } }
int rgb_permutations(Test **test,int irun) { uint i,j,k,permindex=0,t; Vtest vtest; double *testv; size_t ps[4096]; gsl_permutation** lookup; MYDEBUG(D_RGB_PERMUTATIONS){ printf("#==================================================================\n"); printf("# rgb_permutations: Debug with %u\n",D_RGB_PERMUTATIONS); } /* * Number of permutations. Note that the minimum ntuple value for a * valid test is 2. If ntuple is less than 2, we choose the default * test size as 5 (like operm5). */ if(ntuple<2){ test[0]->ntuple = 5; } else { test[0]->ntuple = ntuple; } k = test[0]->ntuple; nperms = gsl_sf_fact(k); /* * A vector to accumulate rands in some sort order */ testv = (double *)malloc(k*sizeof(double)); MYDEBUG(D_RGB_PERMUTATIONS){ printf("# rgb_permutations: There are %u permutations of length k = %u\n",nperms,k); } /* * Create a test, initialize it. */ Vtest_create(&vtest,nperms); vtest.cutoff = 5.0; for(i=0;i<nperms;i++){ vtest.x[i] = 0.0; vtest.y[i] = (double) test[0]->tsamples/nperms; } MYDEBUG(D_RGB_PERMUTATIONS){ printf("# rgb_permutations: Allocating permutation lookup table.\n"); } lookup = (gsl_permutation**) malloc(nperms*sizeof(gsl_permutation*)); for(i=0;i<nperms;i++){ lookup[i] = gsl_permutation_alloc(k); } for(i=0;i<nperms;i++){ if(i == 0){ gsl_permutation_init(lookup[i]); } else { gsl_permutation_memcpy(lookup[i],lookup[i-1]); gsl_permutation_next(lookup[i]); } } MYDEBUG(D_RGB_PERMUTATIONS){ for(i=0;i<nperms;i++){ printf("# rgb_permutations: %u => ",i); gsl_permutation_fprintf(stdout,lookup[i]," %u"); printf("\n"); } } /* * We count the order permutations in a long string of samples of * rgb_permutation_k non-overlapping rands. This is done by: * a) Filling testv[] with rgb_permutation_k rands. * b) Using gsl_sort_index to generate the permutation index. * c) Incrementing a counter for that index (a-c done tsamples times) * d) Doing a straight chisq on the counter vector with nperms-1 DOF * * This test should be done with tsamples > 30*nperms, easily met for * reasonable rgb_permutation_k */ for(t=0;t<test[0]->tsamples;t++){ /* * To sort into a perm, test vector needs to be double. */ for(i=0;i<k;i++) { testv[i] = (double) gsl_rng_get(rng); MYDEBUG(D_RGB_PERMUTATIONS){ printf("# rgb_permutations: testv[%u] = %u\n",i,(uint) testv[i]); } } gsl_sort_index(ps,testv,1,k); MYDEBUG(D_RGB_PERMUTATIONS){ for(i=0;i<k;i++) { printf("# rgb_permutations: ps[%u] = %lu\n",i,ps[i]); } } for(i=0;i<nperms;i++){ if(memcmp(ps,lookup[i]->data,k*sizeof(size_t))==0){ permindex = i; MYDEBUG(D_RGB_PERMUTATIONS){ printf("# Found permutation: "); gsl_permutation_fprintf(stdout,lookup[i]," %u"); printf(" = %u\n",i); } break; } } vtest.x[permindex]++; MYDEBUG(D_RGB_PERMUTATIONS){ printf("# rgb_permutations: Augmenting vtest.x[%u] = %f\n",permindex,vtest.x[permindex]); } }