Beispiel #1
0
  TEUCHOS_UNIT_TEST( Stokhos_NormalizedHermiteBasis, EvaluateBasesAndDerivatives ) {
    double x = 0.1234;
    Teuchos::Array<double> val1(setup.p+1), deriv1(setup.p+1), 
      val2(setup.p+1), deriv2(setup.p+1);
    setup.basis.evaluateBasesAndDerivatives(x, val1, deriv1);

    // evaluate bases and derivatives using formula:
    // He_0(x) = 1
    // He_1(x) = x
    // He_i(x) = (x*He_{i-1}(x) - sqrt(i-1)*He_{i-2}(x))/sqrt(i), i=2,3,...
    val2[0] = 1.0;
    if (setup.p >= 1)
      val2[1] = x;
    for (int i=2; i<=setup.p; i++)
      val2[i] = (x*val2[i-1] - std::sqrt(i-1.0)*val2[i-2])/std::sqrt(1.0*i);

    deriv2[0] = 0.0;
    if (setup.p >= 1)
      deriv2[1] = 1.0;
    for (int i=2; i<=setup.p; i++)
      deriv2[i] = (val2[i-1] + x*deriv2[i-1] - std::sqrt(i-1.0)*deriv2[i-2]) / 
	std::sqrt(1.0*i);
    success = Stokhos::compareArrays(val1, "val1", val2, "val2", 
  				     setup.rtol, setup.atol, out);
    success = success && 
      Stokhos::compareArrays(deriv1, "deriv1", deriv2, "deriv2", 
  			     setup.rtol, setup.atol, out);
  }
  TEUCHOS_UNIT_TEST( Stokhos_LegendreBasis, EvaluateBasesAndDerivatives ) {
    double x = 0.1234;
    Teuchos::Array<double> val1(setup.p+1), deriv1(setup.p+1), 
      val2(setup.p+1), deriv2(setup.p+1);
    setup.basis.evaluateBasesAndDerivatives(x, val1, deriv1);

    // evaluate bases and derivatives using formula:
    // P_0(x) = 1
    // P_1(x) = x
    // P_i(x) = (2*i-1)/i*x*P_{i-1}(x) - (i-1)/i*P_{i-2}(x), i=2,3,...
    // P_0'(x) = 0
    // P_1'(x) = 1
    // P_i'(x) = i*P_{i-1}(x) + x*P_{i-1}'(x), i=2,3,...
    val2[0] = 1.0;
    if (setup.p >= 1)
      val2[1] = x;
    for (int i=2; i<=setup.p; i++)
      val2[i] = (2.0*i-1.0)/i*x*val2[i-1] - (i-1.0)/i*val2[i-2];

    deriv2[0] = 0.0;
    if (setup.p >= 1)
      deriv2[1] = 1.0;
    for (int i=2; i<=setup.p; i++)
      deriv2[i] = i*val2[i-1] + x*deriv2[i-1];
    success = Stokhos::compareArrays(val1, "val1", val2, "val2", 
				     setup.rtol, setup.atol, out);
    success = success && 
      Stokhos::compareArrays(deriv1, "deriv1", deriv2, "deriv2", 
			     setup.rtol, setup.atol, out);

    
  }
Beispiel #3
0
int main(void)
{
    int i = 0, j = 0;
    int maxdeg;
    
    while (maxdeg >= 0)
    {
        printf("Please enter the maximum degree of the polynomial: ");
        
        scanf("%d",&maxdeg);
        if (maxdeg < 0) {
            break;
        }
        printf("Please enter the coefficients: ");
        int *k;
        k = malloc( sizeof(int)*(maxdeg+1));
        free(k);
        int *coeff = (int*) coeff[*k, sizeof(int)];
        if (coeff != NULL) {
            
        for (j = 0; j <= *k; j++) {
            scanf("%d",&coeff[j]);
        }
        PofX1(i, *k, coeff, maxdeg);
        
        deriv1(i, *k, coeff, maxdeg);
        
        printf("\n\n");
    }
    }
}
Beispiel #4
0
inline double newton (double extr_a, double extr_b, double (*function) (double x), double (*deriv1) (double x), double (*deriv2) (double x), double precision) { // Require absolute

	double zero; // Zero
	double funz_a; // function(extr_a)
	double X[MAX_INTER]; // X vector
	double funz[MAX_INTER]; // Y vector
	double der[MAX_INTER]; // Deriv1 vector
	int inter = 0; // Iteration Number

	funz_a = function (extr_a); // function(extr_a)

	if ( funz_a*deriv2(extr_a) < 0 ) { // f(a)*DDf(x) < 0  seed = b

		X[0] = extr_b;

	}

	if ( funz_a*deriv2(extr_b) > 0 ) { // f(a)*DDf(x) > 0  seed = a

		X[0] = extr_a;

	}

	funz[0] = function (X[0]); // f(seed)

	der[0] = deriv1 (X[0]); // deriv1(seed)


	do {

		inter ++; // Increase Interaction

		X[inter] = X[inter-1] - (funz[inter-1] / der[inter-1] );  // New Point

		funz[inter] = function (X[inter]); // function[X[i]

		der[inter] = deriv1 (X[inter]);  // deriv1(X[i]

	} while (abs(X[inter - 1] - X[inter]) > precision || inter < MAX_INTER);

	zero = X[inter];

	return zero;

}
Beispiel #5
0
int main(int argc, char** argv)
{
    if(argc<1) {
        printf("usage: %s <>\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    std::vector<double> X(5), Y(5);
    X[0]=0.1;
    X[1]=0.4;
    X[2]=1.2;
    X[3]=1.8;
    X[4]=2.0;
    Y[0]=0.1;
    Y[1]=0.7;
    Y[2]=0.6;
    Y[3]=1.1;
    Y[4]=0.9;

    tk::spline s;
    // set_boundary() is optional and if omitted, natural boundary condition,
    // f''(a)=f''(b)=0, will be used
    // note, if natural boundary conditions are not used then extrapolation
    // will be a quadratic function, unless the last argument is set to true,
    // which forces linear extrapolation, but this will violate second order
    // differentiability at the endpoint
    s.set_boundary(tk::spline::second_deriv, 0.0,
                   tk::spline::first_deriv, -2.0, false);
    s.set_points(X,Y);

    for(size_t i=0; i<X.size(); i++) {
        printf("%f %f\n", X[i], Y[i]);
    }
    printf("\n");
    for(int i=-50; i<250; i++) {
        double x=0.01*i;
        printf("%f %f %f %f %f\n", x, s(x),
               s.deriv(1,x), s.deriv(2,x), s.deriv(3,x));
        // checking analytic derivatives and finite differences are close
        assert(fabs(s.deriv(1,x)-deriv1(s,x)) < 1e-8);
        assert(fabs(s.deriv(2,x)-deriv2(s,x)) < 1e-8);
    }
    printf("\n");

    return EXIT_SUCCESS;
}
Beispiel #6
0
/******************************************************************************
* Syntax:
*	int QRSFilter(int datum, int init) ;
* Description:
*	QRSFilter() takes samples of an ECG signal as input and returns a sample of
*	a signal that is an estimate of the local energy in the QRS bandwidth.  In
*	other words, the signal has a lump in it whenever a QRS complex, or QRS
*	complex like artifact occurs.  The filters were originally designed for data
*  sampled at 200 samples per second, but they work nearly as well at sample
*	frequencies from 150 to 250 samples per second.
*
*	The filter buffers and static variables are reset if a value other than
*	0 is passed to QRSFilter through init.
*******************************************************************************/
int QRSFilter(int datum, int init)
{
	if (init) {
		hpfilt(0, 1);		// Initialize filters.
		lpfilt(0, 1);
		mvwint(0, 1);
		deriv1(0, 1);
		deriv2(0, 1);
	}

	datum = lpfilt(datum, 0);	// Low pass filter data.
	datum = hpfilt(datum, 0);	// High pass filter data.
	datum = deriv2(datum, 0);	// Take the derivative.
	datum = abs(datum);				// Take the absolute value.
	datum = mvwint(datum, 0);	// Average over an 80 ms window .

	return datum;
}
  TEUCHOS_UNIT_TEST( Stokhos_NormalizedLegendreBasis, EvaluateBasesAndDerivatives ) {
    double x = 0.1234;
    Teuchos::Array<double> val1(setup.p+1), deriv1(setup.p+1), 
      val2(setup.p+1), deriv2(setup.p+1);
    setup.basis.evaluateBasesAndDerivatives(x, val1, deriv1);

    // evaluate bases and derivatives using formula:
    // P_0(x) = 1
    // P_1(x) = sqrt(3)*x
    // P_i(x) = (x*P_{i-1}(x) - b[i-1]*P_{i-2}(x))/b[i], i=2,3,...
    // where b[i] = std::sqrt((i*i)/((2.0*i+1.0)*(2.0*i-1.0)))
    // P_0'(x) = 0
    // P_1'(x) = sqrt(3)
    // P_i'(x) = (P_{i-1}(x) + x*P_{i-1}'(x) - b[i-1]*P_{i-2}')/b[i], i=2,3,...
    double b1, b2;
    b2 = std::sqrt(1.0/3.0);
    b1 = b2;
    val2[0] = 1.0;
    if (setup.p >= 1)
      val2[1] = x/b1;
    for (int i=2; i<=setup.p; i++) {
      b1 = std::sqrt((i*i)/((2.0*i+1.0)*(2.0*i-1.0)));
      val2[i] = (x*val2[i-1] - b2*val2[i-2])/b1;
      b2 = b1;
    }

    b2 = std::sqrt(1.0/3.0);
    b1 = b2;
    deriv2[0] = 0.0;
    if (setup.p >= 1)
      deriv2[1] = 1.0/b1;
    for (int i=2; i<=setup.p; i++) {
      b1 = std::sqrt((i*i)/((2.0*i+1.0)*(2.0*i-1.0)));
      deriv2[i] = (val2[i-1] + x*deriv2[i-1] - b2*deriv2[i-2])/b1;
      b2 = b1;
    }
    success = Stokhos::compareArrays(val1, "val1", val2, "val2", 
  				     setup.rtol, setup.atol, out);
    success = success && 
      Stokhos::compareArrays(deriv1, "deriv1", deriv2, "deriv2", 
  			     setup.rtol, setup.atol, out);

    
  }
Beispiel #8
0
int qrsfilter( int datum, int init )
{
	int fdatum ;

	//初始化滤波器
	if(init)
	{
	    hpfilt( 0, 1 ) ;
		lpfilt( 0, 1 ) ;
		mvwint( 0, 1 ) ;
		deriv1( 0, 1 ) ;
		deriv2( 0, 1 ) ;
	}

	fdatum = lpfilt( datum, 0 );    //低通滤波
	fdatum = hpfilt( fdatum, 0 );    //高通滤波
	fdatum = deriv2( fdatum, 0 );    //差分滤波
	fdatum = abs( fdatum );    //取绝对值
	fdatum = mvwint( fdatum, 0 );    //积分窗求和

	return(fdatum);
}