Ejemplo n.º 1
0
Archivo: stable.c Proyecto: cran/gnlm
/* density of a stable distribution */
void stable(int *n, double *y, double *beta, double *alpha, int *npt,
	    double *up, double *eps, int *type, int *err, double *ffy)
{
  int i, j;
  double h, s, *eta, *seta, *ceta, *sa;
  *err=0;
  eta=(double*)R_alloc((size_t)(*n),sizeof(double));
  seta=(double*)R_alloc((size_t)(*n),sizeof(double));
  ceta=(double*)R_alloc((size_t)(*n),sizeof(double));
  sa=(double*)R_alloc((size_t)(*n),sizeof(double));
  nn=*n;
  if(!eta||!seta||!ceta||!sa){
    *err=1;
    return;}
  for(i=0;i<*n;i++){
    ffy[i]=0.0;
    eta[i]=beta[i]*(1.0-fabs(1.0-alpha[i]))*M_PI/2.0;
    seta[i]=sin(eta[i]);
    ceta[i]=cos(eta[i]);}
  if(*type==1){
    *npt=*npt-*npt%2;
    h=*up/ *npt;
	   for(j=0;j<=*npt;j++){
	     s=(*npt-j)*h;
	     for(i=0;i<*n;i++){
	       sa[i]=pow(s,alpha[i]);
	       ffy[i]=ffy[i]+(4-2*(j%2==0)-(j==1||j==*npt))*cos(-y[i]*s+sa[i]*seta[i])*exp(-sa[i]*ceta[i]);}}
	   for(i=0;i<*n;i++)ffy[i]=ffy[i]*h/3.0/M_PI;}
  else {
    for(i=0;i<*n;i++){
      alphai=alpha[i];
      yi=y[i];
      setai=seta[i];
      cetai=ceta[i];
      ffy[i]=(romberg(fcn1, *eps)+romberg(fcn2, *eps))/M_PI;}}}
Ejemplo n.º 2
0
Archivo: stable.c Proyecto: cran/gnlm
/* cdf of a stable distribution */
void pstable(int *n, double *y, double *beta, double *alpha,
	     double *eps, int *err, double *ffy)
{
  int i;
  *err=0;
  nn=*n;
  for(i=0;i<*n;i++){
    ffy[i]=0.0;
    etai=beta[i]*(1.0-fabs(1.0-alpha[i]))*M_PI/2.0;
    setai=sin(etai);
    cetai=cos(etai);
    alphai=alpha[i];
    yyi=y[i];
    if(etai==0.&&yyi==0)
      ffy[i]=0.5;
    else ffy[i]=0.5+(romberg(fcn3, *eps)+romberg(fcn4, *eps))/M_PI;}}
Ejemplo n.º 3
0
/* P(Z_j\in (a_j,b_j)): deriv wrt rho */
void r_emvndrh(int *m, double *w, double *x, double *rh, double *eps, 
  double *deriv)
{ double r_grh(double),der,tem,sum;
  double romberg(double (*)(double), double, double, double);
  double pnorms(double),phi(double);
  int i,k;
  double *t;
  extern int mm;
  extern double *ww,*xx,rs,r1,r32;
  mm=*m; rs=sqrt(*rh); r1=sqrt(1.-(*rh)); r32=r1*(1.-(*rh));
  xx=(double *) malloc(mm * sizeof(double));
  ww=(double *) malloc(mm * sizeof(double));
  t=(double *) malloc(mm * sizeof(double));
  for(i=0;i<mm;i++) { ww[i]=w[i]; xx[i]=x[i]; }
  if((*rh)>=0.) der=romberg(r_grh,-UB,UB,*eps);
  else /* rho=0 */
  { for(i=0;i<mm;i++) t[i]=pnorms(x[i])-pnorms(w[i]);
    for(k=0,sum=0.;k<mm;k++)
    { for(i=0,tem=1.;i<mm;i++)
      { if(i!=k) { tem*=t[i]; }
        else tem*=(x[i]*phi(x[i])-w[i]*phi(w[i]));
        // maybe check if x>10 or w<-10?
      }
      sum+=tem;
    }
    der=.5*sum;
  }
  free(xx); free(ww); free(t);
  *deriv=der;
}
Ejemplo n.º 4
0
double romberg(double f(double), double a, double b, int n, int m) {
	if((n == 0) && (m == 0)) {
		return (double)(1/2) * (b - a) * (f(a) + f(b));
	}
	else if(m == 0) {
		double coefficient = (b - a)/pow(2, n);
		double sum = 0;
		int k;
		for(k = 0; k < (pow(2, n-1)+1); k++) {
			sum += f(a + (2*k - 1)*coefficient);
		}
		return 0.5 * romberg(f, a, b, n-1, 0) + coefficient*sum;
	}
	double r1 = romberg(f, a, b, n, m-1);
	double r2 = romberg(f, a, b, n-1, m-1);

	double coefficient = 1/(pow(4,(double)m) - 1);
	return coefficient * (pow(4,m)*r1 - r2);
}
Ejemplo n.º 5
0
//this method is here for 1-d integration (a convenience method)
//the integration limits are placed in arrays of size 1, and other methods are called
double RecursiveIntegration::romberg(MultiVarFunction* f, double a, double b, int n)
{
   double* aa = new double[1];
   aa[0] = a;
   double* bb = new double[1];
   bb[0] = b;
   double result = romberg(f, aa, bb, n, 1);
   delete[] aa;
   delete[] bb;
   return result;
}
Ejemplo n.º 6
0
/* P(Z_j\in (a_j,b_j)): deriv wrt a_k or b_k,
   ks=-1 for a_k, ks=1 for b_k
*/
void r_emvnd(int *m, double *w, double *x, double *rh, int *k, int *ks, 
  double *eps, double *deriv)
{ double r_gd(double),der;
  double romberg(double (*)(double), double, double, double);
  int i;
  extern int mm,kk,ksign;
  extern double *ww,*xx,rs,r1;
  mm=*m; kk=(*k-1); rs=sqrt(*rh); r1=sqrt(1.-(*rh)); ksign=*ks;
  xx=(double *) malloc(mm * sizeof(double));
  ww=(double *) malloc(mm * sizeof(double));
  for(i=0;i<mm;i++) { ww[i]=w[i]; xx[i]=x[i]; }
  der=romberg(r_gd,-UB,UB,*eps);
  free(xx); free(ww);
  *deriv= ksign*der;
}
Ejemplo n.º 7
0
QString Integration::logInfo()
{
	ApplicationWindow *app = (ApplicationWindow *)parent();
    QLocale locale = app->locale();
    int prec = app->d_decimal_digits;

	QString logInfo = "[" + QDateTime::currentDateTime().toString(Qt::LocalDate);
	if (d_integrand == AnalyticalFunction){
		logInfo += "\n" + tr("Numerical integration of") + " f(" + d_variable + ") = " + d_formula + " ";
		logInfo += tr("using a %1 order method").arg(d_method) + "\n";
		logInfo += tr("From") + " x = " + locale.toString(d_from, 'g', prec) + " ";
		logInfo += tr("to") + " x = " + locale.toString(d_to, 'g', prec) + "\n";
		logInfo += tr("Tolerance") + " = " + locale.toString(d_tolerance, 'g', prec) + "\n";
		logInfo += tr("Iterations") + ": " + QString::number(romberg()) + "\n";
	} else if (d_integrand == DataSet){
		if (d_graph)
			logInfo += tr("\tPlot")+ ": ''" + d_graph->multiLayer()->objectName() + "'']\n";
		else
			logInfo += "\n";
		QString dataSet;
		if (d_curve)
			dataSet = d_curve->title().text();
		else
			dataSet = d_y_col_name;
		logInfo += "\n" + tr("Numerical integration of") + ": " + dataSet + " ";
		logInfo += tr("using the Trapezoidal Rule") + "\n";
		logInfo += tr("Points") + ": " + QString::number(d_n) + " " + tr("from") + " x = " + locale.toString(d_from, 'g', prec) + " ";
    	logInfo += tr("to") + " x = " + locale.toString(d_to, 'g', prec) + "\n";

		// use GSL to find maximum value of data set
		gsl_vector *aux = gsl_vector_alloc(d_n);
		for(int i=0; i < d_n; i++)
			gsl_vector_set (aux, i, fabs(d_y[i]));
		int maxID = gsl_vector_max_index (aux);
		gsl_vector_free(aux);

    	logInfo += tr("Peak at") + " x = " + locale.toString(d_x[maxID], 'g', prec)+"\t";
		logInfo += "y = " + locale.toString(d_y[maxID], 'g', prec)+"\n";
		d_area = trapez();
	}

	logInfo += tr("Area") + "=" + locale.toString(d_area, 'g', prec);
	logInfo += "\n-------------------------------------------------------------\n";
    return logInfo;
}
Ejemplo n.º 8
0
main() {

	double EPS = 1.e-12;
	double ans = 10000000;
	double prevans = 1;
	int n = 1;
	int m = 2;
	double a = 0.0;
	double b = 1.0;

	printf("    m      n       ans         (anser - prev answer) \n");
	while(fabs(ans - prevans) > EPS) {
		prevans = ans;
		ans = romberg(f, a, b, n, m);
		printf(" %d %d %12.8f %12.8f \n", m, n, ans, ans - prevans);
		n = n*2;
	}
}
Ejemplo n.º 9
0
int main(int narg, char* argc[])
{
    std::cout.setf( std::ios::fixed, std:: ios::floatfield );
    std::cout.precision(8);
    
    timestamp_t inicio, fim;
	inicio = get_timestamp();
	
		std::vector<Function*> functions;
	
		functions.push_back( new Function1() );
		functions.push_back( new Function2() );

		RombergIntegration romberg(argc[1], functions, atoi(argc[2]));

		std::cout << "Integral: " << romberg.calculateIntegral() << "\n";

    fim = get_timestamp();	
	
	std::cout << "tempo: " << (fim - inicio)/1000.0L << " milissegundos.\n";

	return 0;
}
Ejemplo n.º 10
0
int main(){
	printf("%lf\n",romberg(test,0,1));
	printf("%lf\n",simpson(test,0,1,(int)1e6));
}