/** * Gauss-Legendre quadature. * computes knots and weights of a Gauss-Legendre quadrature formula. * \param a Left endpoint of interval * \param b Right endpoint of interval * \param _t array of abscissa * \param _wts array of corresponding wights */ void gauss_legendre( double a, double b, const dvector& _t, const dvector& _wts ) // // Purpose: // // computes knots and weights of a Gauss-Legendre quadrature formula. // // Discussion: // // The user may specify the interval (A,B). // // Only simple knots are produced. // // Use routine EIQFS to evaluate this quadrature formula. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // September 2010 by Derek Seiple // // Author: // // Original FORTRAN77 version by Sylvan Elhay, Jaroslav Kautsky. // C++ version by John Burkardt. // // Reference: // // Sylvan Elhay, Jaroslav Kautsky, // Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of // Interpolatory Quadrature, // ACM Transactions on Mathematical Software, // Volume 13, Number 4, December 1987, pages 399-415. // // Parameters: // // Input, double A, B, the interval endpoints, or // other parameters. // // Output, double T[NT], the knots. // // Output, double WTS[NT], the weights. // { dvector t=(dvector&) _t; dvector wts=(dvector&) _wts; if( t.indexmax()!=wts.indexmax() ) { cerr << "Incompatible sizes in void " "mygauss_legendre(double a, double b, const dvector& _t, const dvector& _wts)" << endl; ad_exit(-1); } t.shift(0); wts.shift(0); int nt = t.indexmax() + 1; int ub = nt-1; int i; int k; int l; double al; double ab; double abi; double abj; double be; double p; double shft; double slp; double temp; double tmp; double zemu; // Compute the Gauss quadrature formula for default values of A and B. dvector aj(0,ub); dvector bj(0,ub); ab = 0.0; zemu = 2.0 / ( ab + 1.0 ); for ( i = 0; i < nt; i++ ) { aj[i] = 0.0; } for ( i = 1; i <= nt; i++ ) { abi = i + ab * ( i % 2 ); abj = 2 * i + ab; bj[i-1] = sqrt ( abi * abi / ( abj * abj - 1.0 ) ); } // Compute the knots and weights. if ( zemu <= 0.0 ) // Exit if the zero-th moment is not positive. { cout << "\n"; cout << "Fatal error!\n"; cout << " ZEMU <= 0.\n"; exit ( 1 ); } // Set up vectors for IMTQLX. for ( i = 0; i < nt; i++ ) { t[i] = aj[i]; } wts[0] = sqrt ( zemu ); for ( i = 1; i < nt; i++ ) { wts[i] = 0.0; } // Diagonalize the Jacobi matrix. imtqlx (t, bj, wts ); for ( i = 0; i < nt; i++ ) { wts[i] = wts[i] * wts[i]; } // Prepare to scale the quadrature formula to other weight function with // valid A and B. ivector mlt(0,ub); for ( i = 0; i < nt; i++ ) { mlt[i] = 1; } ivector ndx(0,ub); for ( i = 0; i < nt; i++ ) { ndx[i] = i + 1; } dvector st(0,ub); dvector swts(0,ub); temp = 3.0e-14; al = 0.0; be = 0.0; if ( fabs ( b - a ) <= temp ) { cout << "\n"; cout << "Fatal error!\n"; cout << " |B - A| too small.\n"; exit ( 1 ); } shft = ( a + b ) / 2.0; slp = ( b - a ) / 2.0; p = pow ( slp, al + be + 1.0 ); for ( k = 0; k < nt; k++ ) { st[k] = shft + slp * t[k]; l = abs ( ndx[k] ); if ( l != 0 ) { tmp = p; for ( i = l - 1; i <= l - 1 + mlt[k] - 1; i++ ) { swts[i] = wts[i] * tmp; tmp = tmp * slp; } } } for(i=0;i<nt;i++) { t(i) = st(ub-i); wts(i) = swts(ub-i); } return; }
/** * Gauss-Hermite quadature. * Computes a Gauss-Hermite quadrature formula with simple knots. * \param _t array of abscissa * \param _wts array of corresponding wights */ void gauss_hermite (const dvector& _t,const dvector& _wts) // // Purpose: // // computes a Gauss quadrature formula with simple knots. // // Discussion: // // This routine computes all the knots and weights of a Gauss quadrature // formula with a classical weight function with default values for A and B, // and only simple knots. // // There are no moments checks and no printing is done. // // Use routine EIQFS to evaluate a quadrature computed by CGQFS. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // September 2010 by Derek Seiple // // Author: // // Original FORTRAN77 version by Sylvan Elhay, Jaroslav Kautsky. // C++ version by John Burkardt. // // Reference: // // Sylvan Elhay, Jaroslav Kautsky, // Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of // Interpolatory Quadrature, // ACM Transactions on Mathematical Software, // Volume 13, Number 4, December 1987, pages 399-415. // // Parameters: // // Output, double T[NT], the knots. // // Output, double WTS[NT], the weights. // { dvector t=(dvector&) _t; dvector wts=(dvector&) _wts; if( t.indexmax()!=wts.indexmax() ) { cerr << "Incompatible sizes in void " << "void gauss_hermite (const dvector& _t,const dvector& _wts)" << endl; ad_exit(-1); } int lb = t.indexmin(); int ub = t.indexmax(); dvector aj(lb,ub); dvector bj(lb,ub); double zemu; int i; // Get the Jacobi matrix and zero-th moment. zemu = 1.772453850905516; for ( i = lb; i <= ub; i++ ) { aj(i) = 0.0; } for ( i = lb; i <= ub; i++ ) { bj(i) = sqrt((i-lb+1)/2.0); } // Compute the knots and weights. if ( zemu <= 0.0 ) // Exit if the zero-th moment is not positive. { cout << "\n"; cout << "SGQF - Fatal error!\n"; cout << " ZEMU <= 0.\n"; exit ( 1 ); } // Set up vectors for IMTQLX. for ( i = lb; i <= ub; i++ ) { t(i) = aj(i); } wts(lb) = sqrt ( zemu ); for ( i = lb+1; i <= ub; i++ ) { wts(i) = 0.0; } // Diagonalize the Jacobi matrix. imtqlx ( t, bj, wts ); for ( i = lb; i <= ub; i++ ) { wts(i) = wts(i) * wts(i); } return; }
void sgqf ( int nt, double aj[], double bj[], double zemu, double t[], double wts[] ) /******************************************************************************/ /* Purpose: SGQF computes knots and weights of a Gauss Quadrature formula. Discussion: This routine computes all the knots and weights of a Gauss quadrature formula with simple knots from the Jacobi matrix and the zero-th moment of the weight function, using the Golub-Welsch technique. Licensing: This code is distributed under the GNU LGPL license. Modified: 11 January 2010 Author: Original FORTRAN77 version by Sylvan Elhay, Jaroslav Kautsky. C version by John Burkardt. Reference: Sylvan Elhay, Jaroslav Kautsky, Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of Interpolatory Quadrature, ACM Transactions on Mathematical Software, Volume 13, Number 4, December 1987, pages 399-415. Parameters: Input, int NT, the number of knots. Input, double AJ[NT], the diagonal of the Jacobi matrix. Input/output, double BJ[NT], the subdiagonal of the Jacobi matrix, in entries 1 through NT-1. On output, BJ has been overwritten. Input, double ZEMU, the zero-th moment of the weight function. Output, double T[NT], the knots. Output, double WTS[NT], the weights. */ { int i; /* Exit if the zero-th moment is not positive. */ if ( zemu <= 0.0 ) { printf ( "\n" ); printf ( "SGQF - Fatal error!\n" ); printf ( " ZEMU <= 0.\n" ); exit ( 1 ); } /* Set up vectors for IMTQLX. */ for ( i = 0; i < nt; i++ ) { t[i] = aj[i]; } wts[0] = sqrt ( zemu ); for ( i = 1; i < nt; i++ ) { wts[i] = 0.0; } /* Diagonalize the Jacobi matrix. */ imtqlx ( nt, t, bj, wts ); for ( i = 0; i < nt; i++ ) { wts[i] = wts[i] * wts[i]; } return; }