Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "SundanceGauss1D.hpp"
00044 #ifdef _MSC_VER
00045 # include "winmath.h"
00046 #endif
00047
00048 using namespace Sundance;
00049 using namespace Sundance;
00050 using namespace Sundance;
00051 using namespace Sundance;
00052 using namespace Sundance;
00053 using namespace Teuchos;
00054
00055 Gauss1D::Gauss1D(int n)
00056 : nodes_(n), weights_(n)
00057 {
00058 computeWeights(n, -1.0, 1.0);
00059 }
00060
00061 Gauss1D::Gauss1D(int n, double a, double b)
00062 : nodes_(n), weights_(n)
00063 {
00064 computeWeights(n, a, b);
00065 }
00066
00067
00068
00069 void Gauss1D::computeWeights(int n, double a, double b)
00070 {
00071 int m = (n+1)/2;
00072
00073 double xMid = (b+a)/2.0;
00074 double halfWidth = (b-a)/2.0;
00075
00076 for (int i=0; i<m; i++)
00077 {
00078
00079 double z = cos(M_PI*(i+0.75)/(n+0.5));
00080 double dP;
00081 double zOld;
00082 double tol = 1.0e-14;
00083
00084 do
00085 {
00086 double p1 = 1.0;
00087 double p2 = 0.0;
00088 for (int j=1; j<=n; j++)
00089 {
00090 double p3 = p2;
00091 p2 = p1;
00092 p1 = ((2.0*j-1.0)*z*p2 - (j-1.0)*p3)/j;
00093 }
00094 dP = n*(z*p1-p2)/(z*z-1.0);
00095 zOld =z;
00096 z = zOld - p1/dP;
00097 }
00098 while ( fabs(z-zOld) > tol );
00099 nodes_[i] = xMid - halfWidth*z;
00100 nodes_[n-i-1] = xMid + halfWidth*z;
00101 weights_[i] = 2.0*halfWidth/((1.0-z*z)*dP*dP);
00102 weights_[n-i-1] = weights_[i];
00103 }
00104 }
00105
00106
00107 bool Gauss1D::unitTest()
00108 {
00109 std::cerr << "------------------ Gauss1D unit test ----------------------"
00110 << std::endl;
00111
00112 Gauss1D q(20, 0.0, M_PI);
00113
00114 double sum = 0.0;
00115 for (int i=0; i<q.nPoints(); i++)
00116 {
00117 sum += q.weights()[i]*sin(q.nodes()[i]);
00118 }
00119 std::cerr << "integral of sin(x) over [0, pi] = " << sum << std::endl;
00120 double sumErr = fabs(sum - 2.0);
00121 bool sumPass = sumErr < 1.0e-10;
00122 std::cerr << "error = " << sumErr << std::endl;
00123 if (sumPass) std::cerr << "Gauss1D sine test PASSED" << std::endl;
00124 else std::cerr << "Gauss1D sine test FAILED" << std::endl;
00125 std::cerr << "------------------ End Gauss1D unit test ----------------------"
00126 << std::endl;
00127 return sumPass;
00128 }
00129
00130