void init_gauss(int nbpts) { ngauss = nbpts; xi = (double*)malloc(sizeof(double)*(ngauss+1)); wi = (double*)malloc(sizeof(double)*(ngauss+1)); gauleg(0.,1.,xi,wi,ngauss); }
void Infinite::TA_calculateE0(int nA, double (Infinite::*func)(), double (Infinite::*ref)()) { double refValue = (this->*ref)(); double SP_diff = abs(refValue); SP_deltaE = 0.0; TA_deltaE = 0.0; twisted_x.resize(nA); twisted_w.resize(nA); gauleg(0,M_PI,twisted_x,twisted_w); for (int x = 0; x < twisted_x.size(); x++) for (int y = 0; y < twisted_x.size(); y++) for (int z = 0; z < twisted_x.size(); z++) { setTheta(twisted_x[x],twisted_x[y],twisted_x[z]); double value = (this->*func)(); TA_deltaE += twisted_w[x] * twisted_w[y] * twisted_w[z] * value; if (abs(value/A - refValue) < SP_diff) { SP_diff = abs(value/A - refValue); SP_x = x; SP_y = y; SP_z = z; SP_deltaE = value; } } TA_deltaE /= pow(M_PI,3); setTheta(0.0,0.0,0.0); }
void psht_make_gauss_geom_info_2 (int nrings, int nphi, int stride_lon, int stride_lat, psht_geom_info **geom_info) { const double pi=3.141592653589793238462643383279502884197; double *theta=RALLOC(double,nrings); double *weight=RALLOC(double,nrings); int *nph=RALLOC(int,nrings); double *phi0=RALLOC(double,nrings); ptrdiff_t *ofs=RALLOC(ptrdiff_t,nrings); int *stride_=RALLOC(int,nrings); int m; gauleg(-1,1,theta,weight,nrings); for (m=0; m<nrings; ++m) { theta[m] = acos(theta[m]); nph[m]=nphi; phi0[m]=0; ofs[m]=(ptrdiff_t)m*stride_lat; stride_[m]=stride_lon; weight[m]*=2*pi/nphi; } psht_make_geom_info (nrings, nph, ofs, stride_, phi0, theta, weight, geom_info); DEALLOC(theta); DEALLOC(weight); DEALLOC(nph); DEALLOC(phi0); DEALLOC(ofs); DEALLOC(stride_); }
void testGauleg(){ // declare variables int n; double *X, *W; printf("N = "); scanf("%u",&n); // note: have to make sure the range of integration is from -1 to 1, see Numerical Recipes p. 207 (p. 184 on pdf) // allocate memory X = (double*) calloc(n,sizeof(double)); W = (double*) calloc(n,sizeof(double)); gauleg(n, X, W); // print abscissas and weights int i; printf("\nX = "); for (i = 0; i < n; i++) printf("%.6f ",X[i]); printf("\n"); printf("\nW = "); for (i = 0; i < n; i++) printf("%.6f ",W[i]); printf("\n"); }
/*We use the Cauchy Gourat theorem to compute the derivatives of the double(Mellin+Laplace) transform */ static dcomplex dermellin(dcomplex l, double sg, double r, int nummom) { dcomplex term, cv, mu; int i; double r0,sumr, sumi/*,x[NPOINTS_FUSAITAGL+1],w[NPOINTS_FUSAITAGL+1]*/; double v; double *x,*w; x=malloc((NPOINTS_FUSAITAGL+1)*sizeof(double)); w=malloc((NPOINTS_FUSAITAGL+1)*sizeof(double)); sumr=0.0; sumi=0.0; gauleg(0, 2*M_PI, x, w,NPOINTS_FUSAITAGL); v = 2*r/(sg*sg)-1.0; cv = Complex(v,0.0); mu = Csqrt(Cadd(Complex(v*v,0), RCmul(2.0,l))); r0 = Creal(RCmul(0.5,Csub(mu,cv))); if(r0>1.0) r0=0.25; for (i=1;i<=NPOINTS_FUSAITAGL;i++) { term = RCmul(pow(r0,nummom), Cexp(Complex(0.0, nummom*x[i]))); sumr += w[i]*Creal(Cdiv(mellintransform(l, RCmul(r0, Cexp(Complex(0.0, x[i]))), sg, r), term)); sumi += w[i]*Cimag(Cdiv(mellintransform(l, RCmul(r0, Cexp(Complex(0.0, x[i]))), sg, r), term)); } free(x); free(w); return Complex(exp(factln(nummom))*sumr/(2.0*M_PI),exp(factln(nummom))*sumi/(2.0*M_PI)); }
Infinite::Infinite(int _A, int _g_s, double _rho, int _nMax) : System(_A), g_s(_g_s), nMax(_nMax), gauss_x(100), gauss_w(100) { thetaX = 0.0; thetaY = 0.0; thetaZ = 0.0; setRho(_rho); gauleg(-1,1,gauss_x,gauss_w); }
double quadsingint(func&& f, unsigned int n) { double I = 0.; #if METHOD == 1 // s = sqrt(1 \pm t) QuadRule Q = gauleg(n); for(unsigned int i = 0; i < n; ++i) { double x = (Q.nodes(i) + 1.) / 2.; double w = Q.weights(i) * x * x * std::sqrt(2. - x * x); I += w * (f(x*x - 1) + f(-x*x + 1)); } return I; #elif METHOD == 2 // t = cos(s) QuadRule Q = gauleg(2*n); for(unsigned int i = 0; i < 2*n; ++i) { double x = sin(Q.nodes(i) * M_PI_2); double w = Q.weights(i) * cos(Q.nodes(i) * M_PI_2) * cos(Q.nodes(i) * M_PI_2); I += w * f(x) * M_PI_2; } return I; #endif }
main(int argc, char *argv[]) { int iq,nq; double wq[NN],xq[NN],x1,x2; void gauleg(double *x1,double *x2, int *n, double x[], double w[]); x1=0.; x2=1.; scanf("%d", &nq); while(nq>0) { gauleg(&x1,&x2,&nq,xq,wq); printf("\nnq=%d\n", nq); printf("iq xq wq\n"); for(iq=1;iq<=nq;iq++) printf("%d %.15f %.15e\n", iq,xq[iq],wq[iq]); scanf("%d", &nq); } }
void NR::fred2(const DP a, const DP b, Vec_O_DP &t, Vec_O_DP &f, Vec_O_DP &w, DP g(const DP), DP ak(const DP, const DP)) { int i,j; DP d; int n=t.size(); Mat_DP omk(n,n); Vec_INT indx(n); gauleg(a,b,t,w); for (i=0;i<n;i++) { for (j=0;j<n;j++) omk[i][j]=DP(i == j)-ak(t[i],t[j])*w[j]; f[i]=g(t[i]); } ludcmp(omk,indx,d); lubksb(omk,indx,f); }
/// Number of haloes with mass greater than or equal to M double cosmology::Nplus(double M, double z) { //Int_{10^9}^{10^{16}} n(M) dM double result=0; const int N=100; double x[N],w[N]; gauleg(log10(M),17.0,x,w,N); result=0; for (int i=0;i<N;i++) { double m=pow(10.,x[i]); result=result+w[i]*nofm(m,z)*m*log(10.); } return result; }
//------------------------------------------------------------------------------ GaussLaguerreIntegrator::GaussLaguerreIntegrator(Config *cfg, WaveFunction *wf) : SpatialIntegrator(cfg, wf) { try { cfg->lookupValue("spatialIntegration.GaussLaguerre.samples", N); cfg->lookupValue("spatialIntegration.L", L); dim = cfg->lookup("systemSettings.dim"); } catch (const SettingNotFoundException &nfex) { cerr << "Basis::Basis(Config *cfg)::Error reading from 'systemSettings' object setting." << endl; } x_ = zeros(dim); y_ = zeros(dim); x = new double[N]; w1 = new double[N]; gauleg(-L, L, x, w1, N); #if DEBUG cout << "GaussLaguerreIntegrator(Setting* systemSettings)" << endl; cout << "N = " << N << endl; cout << "L = " << L << endl; #endif }
void fred2(int n, float a, float b, float t[], float f[], float w[], float (*g)(float), float (*ak)(float, float)) { void gauleg(float x1, float x2, float x[], float w[], int n); void lubksb(float **a, int n, int *indx, float b[]); void ludcmp(float **a, int n, int *indx, float *d); int i,j,*indx; float d,**omk; indx=ivector(1,n); omk=matrix(1,n,1,n); gauleg(a,b,t,w,n); for (i=1;i<=n;i++) { for (j=1;j<=n;j++) omk[i][j]=(float)(i == j)-(*ak)(t[i],t[j])*w[j]; f[i]=(*g)(t[i]); } ludcmp(omk,n,indx,&d); lubksb(omk,n,indx,f); free_matrix(omk,1,n,1,n); free_ivector(indx,1,n); }
double GaussianQuad_N(double func(const double), const double a2, const double b2, int NptGQ) { double s=0.0; double x[NptGQ], w[NptGQ]; // double dh=(b2-a2)/double(divs); gauleg(a2, b2, x, w, NptGQ); for (int j=1; j<=NptGQ; j++) { s += w[j]*func(x[j]); } /* for (i=1; i<=divs; i++) { a0 = a2 + (i-1)*dh; b0 = a0 + dh; gauleg(a0, b0, x, w, NptGQ); for (j=1; j<=NptGQ; j++) { s += w[j]*func(x[j]); } } */ return s; }
double DiscreteAsian(int model, //modello double spot, double strike, double rf, double dt, int ndates, double lowlim, double uplim, int npoints, //n. of quadrature points long nfft, //n. of points for the fft inversion double ModelParameters[], //the parameters of the model double price[], double solution[],double *delta) //OUTPUT: Contains the solution { int i, j, k; double inter_dens=0.; double upperdens,b; //parameters for spline interpolation int ier; double dfb,ddfb,arg; //vectors where to store the outputs of the FFT inversion double *inv, *logk; //abscissa and weights for Gaussian quadrature with npoints double *abscissa,*weights, *temp; double** c,** kernelmatrix; double optprice,optdelta; double gamma_price; inv=dvector(0, nfft-1); //contains the density logk=dvector(0, nfft-1); //contains the abscissa of the density c = dmatrix(0, nfft-1, 0, 2); kernelmatrix= dmatrix(0, npoints-1, 0, npoints-1); abscissa=dvector(1,npoints); weights=dvector(1,npoints); temp=dvector(0,npoints-1); //Generate abscissa and weights for quadrature gauleg(lowlim, uplim, abscissa, weights, npoints); b=MAX(fabs(lowlim - log(exp(uplim) + 1)),fabs(uplim - log(exp(lowlim) + 1))); b=b*1.1; upperdens=b; TableIFRT(1, model, rf, dt, nfft, b, 1.5, ModelParameters, inv, logk); //spline interpolation i=spline(logk, inv, nfft ,c); if(i>100) return i; //construct the kernel matrix for(i=1;i<=npoints;i++){ for(j=1;j<=npoints;j++){ // argument of the density arg=abscissa[i] - log(exp(abscissa[j]) + 1); if(arg>-upperdens) { if(arg<upperdens) { inter_dens=MAX(splevl(arg, nfft, logk, inv, c, &dfb, &ddfb, &ier),0.0); } } if(arg<-upperdens) inter_dens=0.0; if(arg>upperdens) inter_dens=0.0; //construct the kernel kernelmatrix[i-1][ j-1] = weights[j] * MAX(inter_dens,0.0); /*printf("%d %d %d %f\n",npoints,i-1,j-1,kernelmatrix[i-1][ j-1]);*/ } //construct the initial condition arg=abscissa[i]; if(arg>-upperdens) { if(arg<upperdens) { solution[i-1] = MAX(splevl(abscissa[i], nfft, logk, inv, c, &dfb, &ddfb, &ier),0); } } if(arg<-upperdens) solution[i-1]=0.0; if(arg>upperdens) solution[i-1]=0.0; } //iterations over the monitoring dates for(k = 1;k< ndates;k++){ //compute K*v_n matvec(kernelmatrix, npoints, npoints, solution, temp); //update v_n+1 for( i = 0;i<= npoints-1;i++){ solution[i]=temp[i]; } } optprice=0.0,optdelta=0.0; gamma_price=log(strike*((double)(ndates + 1))/spot-1.); for( i = 0;i<= npoints-1;i++){ //spot price price[i] = spot * exp(abscissa[i+1]); ///option price optprice=optprice+weights[i+1]*MAX(spot*(1 + exp(abscissa[i+1]))/(ndates + 1) - strike, 0)*solution[i]*exp(-rf*dt*ndates); if(abscissa[i+1]>gamma_price) optdelta=optdelta+weights[i+1]*MAX((1 + exp(abscissa[i+1])), 0)*solution[i]*exp(-rf*dt*ndates)/(ndates + 1); } *delta=optdelta; free_dvector(abscissa,1,npoints); free_dvector(weights,1,npoints); free_dvector(temp,0,npoints-1); free_dvector(inv,0,nfft-1); free_dvector(logk,0,nfft-1); free_dmatrix(c, 0, nfft-1, 0, 2); free_dmatrix(kernelmatrix, 0, npoints-1, 0, npoints-1); return optprice; }
static int FusaiTagliani_FixedAsian(double pseudo_stock,double pseudo_strike,NumFunc_2 *po,double t,double r,double divid,double sigma,double *ptprice,double *ptdelta) { int i; double sum=0.0,sum_delta=0.; /* double area =0.0;*/ int nnodi=NPOINTS_FUSAITAGL; double CTtK,PTtK,Dlt,Plt; double k2, k3, k4, m1,m2,m3,m4; double k2a, k3a, k4a, m1a,m2a,m3a,m4a, var,m; double term1, term2, term3, term4, edgedens ; double aa; int terms,totterms; double *x,*w; /*Set parameters for Laplace inversion*/ aa=18.4; terms=15; totterms=25; x= malloc((NPOINTS_FUSAITAGL+1)*sizeof(double)); w=malloc((NPOINTS_FUSAITAGL+1)*sizeof(double)); /*Computation of the first four logarithmic moments*/ m1 = MomentiLnAbWh(t, sigma, r-divid, aa, terms, totterms, 1); m2 = MomentiLnAbWh(t, sigma, r-divid, aa, terms, totterms, 2); m3 = MomentiLnAbWh(t, sigma, r-divid, aa, terms, totterms, 3); m4 = MomentiLnAbWh(t, sigma, r-divid, aa, terms, totterms, 4); /*Fit the parameters m,var of normal density*/ var= m2-m1*m1; m=m1; /*Computation of the cumulants of the logarithm of the arithmetic average*/ k2 = m2 - m1 *m1; k3 = m3 - 3 * m1 * m2 +3*m2*m1*m1 -3 * m1 * m1 * m1; k4 = m4 - 4 * m3 * m1 - 3*m2*m2+12*m2*m1*m1-6 * m1 * m1 * m1 * m1; /*k4 = m4 - 4 * m3 * m1 + 6 * m2 * m1 * m1 - 3 * m1 * m1 * m1 * m1 - 3 * k2 * k2;*/ /*Edgeworth Adjustment : Computation of theoretical moments of the normal density*/ m1a = m; m2a = m2; m3a = m*m*m+3*m*var; m4a = m*m*m*m+6*m*m*var+3*var*var; /*Edgeworth Adjustment : Computation of theoretical cumulants of the normal density*/ k2a = m2a - m1a * m1a ; k3a = m3a - 3 * m1a * m2a + 3*m2a*m1a*m1a-3*m1a*m1a*m1a; /*k4a = m4a - 4 * m3a * m1a + 6 * m2a * m1a * m1a - 3 * m1a *m1a *m1a *m1a - 3 * k2a * k2a;*/ k4a = m4a - 4 * m3a * m1a - 3*m2a*m2a+12*m2a*m1a*m1a-6 * m1a * m1a * m1a * m1a; /*Integrate, using the Laguerre quadrature, for obtaining the call price */ gauleg(log(pseudo_strike*t/pseudo_stock),log(pseudo_strike*t/pseudo_stock)+10., x, w, nnodi); sum=0.0; sum_delta=0.; for (i=1;i<=NPOINTS_FUSAITAGL;i++) { /*Density construction using Edgeworth Expansion*/ term1= Normdens(x[i], m, pow(var,0.5)); term2= (k2-k2a)*Der2Normdens(x[i], m, pow(var,0.5))/2.; term3= -(k3-k3a)*Der3Normdens(x[i], m, pow(var,0.5))/6.; term4= ((k4-k4a)+3*(k2-k2a))*Der4Normdens(x[i], m, pow(var,0.5))/24.; edgedens = term1+term2+term3+term4; /*Integration with to respect to payoff for obtaining the call price and delta*/ sum += w[i]*(exp(x[i])*pseudo_stock/t-pseudo_strike)*edgedens; sum_delta += w[i]*exp(x[i])/t*edgedens; } /* Call Price */ CTtK= exp(-r*t)*sum; /* Put Price from Parity*/ if(r==divid) PTtK=CTtK+pseudo_strike*exp(-r*t)-pseudo_stock*exp(-r*t); else PTtK=CTtK+pseudo_strike*exp(-r*t)-pseudo_stock*exp(-r*t)*(exp((r-divid)*t)-1.)/(t*(r-divid)); /*Delta for call option*/ Dlt=exp(-r*t)*sum_delta; /*Delta for put option*/ if(r==divid) Plt=Dlt-exp(-r*t); else Plt=Dlt-exp(-r*t)*(exp((r-divid)*t)-1.0)/(t*(r-divid)); /*Price*/ if ((po->Compute)==&Call_OverSpot2) *ptprice=CTtK; else *ptprice=PTtK; /*Delta */ if ((po->Compute)==&Call_OverSpot2) *ptdelta=Dlt; else *ptdelta=Plt; free(x); free(w); return OK; }