18 template <
typename Real>
25 std::function<Real(Real)>
const& integrand);
31 std::function<Real(Real)>
const& integrand);
50 std::vector<Real>& coefficients);
53 std::vector<Real>
const & coefficients, Real a, Real b,
54 std::function<Real(Real)>
const& integrand);
58 template <
typename Real>
60 std::function<Real(Real)>
const& integrand)
62 Real
h = (b -
a) / (Real)(numSamples - 1);
63 Real
result = ((Real)0.5) * (integrand(a) + integrand(b));
64 for (
int i = 1; i <= numSamples - 2; ++i)
66 result += integrand(a + i*h);
72 template <
typename Real>
74 std::function<Real(Real)>
const& integrand)
76 Real
const half = (Real)0.5;
77 std::vector<std::array<Real, 2>> rom(order);
79 rom[0][0] = half * h * (integrand(a) + integrand(b));
80 for (
int i0 = 2, p0 = 1; i0 <=
order; ++i0, p0 *= 2, h *= half)
85 for (i1 = 1; i1 <= p0; ++i1)
87 sum += integrand(a + h * (i1 - half));
91 rom[0][1] = half * (rom[0][0] + h * sum);
92 for (
int i2 = 1, p2 = 4; i2 < i0; ++i2, p2 *= 4)
94 rom[i2][1] = (p2*rom[i2 - 1][1] - rom[i2 - 1][0]) / (p2 - 1);
97 for (i1 = 0; i1 < i0; ++i1)
99 rom[i1][0] = rom[i1][1];
103 Real
result = rom[order - 1][0];
107 template <
typename Real>
109 std::vector<Real>& roots, std::vector<Real>& coefficients)
111 Real
const zero = (Real)0;
112 Real
const one = (Real)1;
113 Real
const half = (Real)0.5;
115 std::vector<std::vector<Real>> poly(degree + 1);
124 for (
int n = 2;
n <= degree; ++
n)
126 Real mult0 = (Real)(
n - 1) / (Real)
n;
127 Real mult1 = (Real)(2 *
n - 1) / (Real)
n;
129 poly[
n].resize(
n + 1);
130 poly[
n][0] = -mult0 * poly[
n - 2][0];
131 for (
int i = 1; i <=
n - 2; ++i)
133 poly[
n][i] = mult1 * poly[
n - 1][i - 1] - mult0 * poly[
n - 2][i];
135 poly[
n][
n - 1] = mult1 * poly[
n - 1][
n - 2];
136 poly[
n][
n] = mult1 * poly[
n - 1][
n - 1];
139 roots.resize(degree);
142 coefficients.resize(roots.size());
143 size_t n = roots.size() - 1;
144 std::vector<Real> subroots(n);
145 for (
size_t i = 0; i < roots.size(); ++i)
148 for (
size_t j = 0, k = 0; j < roots.size(); ++j)
152 subroots[k++] = roots[j];
153 denominator *= roots[i] - roots[j];
157 std::array<Real, 2> delta =
159 -one - subroots.back(),
160 +one - subroots.back()
163 std::vector<std::array<Real, 2>>
weights(n);
164 weights[0][0] = half * delta[0] * delta[0];
165 weights[0][1] = half * delta[1] * delta[1];
166 for (
size_t k = 1; k <
n; ++k)
169 Real mult = -dk / (dk + (Real)2);
170 weights[k][0] = mult * delta[0] * weights[k - 1][0];
171 weights[k][1] = mult * delta[1] * weights[k - 1][1];
177 std::array<Real, 2> product;
180 int numElements = (1 <<
static_cast<unsigned int>(n - 1));
181 std::vector<Info> info(numElements);
183 info[0].product[0] = one;
184 info[0].product[1] = one;
185 for (
int ipow = 1,
r = 0; ipow < numElements; ipow <<= 1, ++
r)
187 info[ipow].numBits = 1;
188 info[ipow].product[0] = -one - subroots[
r];
189 info[ipow].product[1] = +one - subroots[
r];
190 for (
int m = 1, j = ipow + 1;
m < ipow; ++
m, ++j)
192 info[j].numBits = info[
m].numBits + 1;
194 info[ipow].product[0] * info[
m].product[0];
196 info[ipow].product[1] * info[
m].product[1];
200 std::vector<std::array<Real, 2>> sum(n);
201 std::array<Real, 2> zero2 = { zero, zero };
202 std::fill(sum.begin(), sum.end(), zero2);
203 for (
size_t k = 0; k < info.size(); ++k)
205 sum[info[k].numBits][0] += info[k].product[0];
206 sum[info[k].numBits][1] += info[k].product[1];
209 std::array<Real, 2> total = zero2;
210 for (
size_t k = 0; k <
n; ++k)
212 total[0] += weights[n - 1 - k][0] * sum[k][0];
213 total[1] += weights[n - 1 - k][1] * sum[k][1];
216 coefficients[i] = (total[1] - total[0]) / denominator;
220 template <
typename Real>
222 std::vector<Real>
const & coefficients, Real a, Real b,
223 std::function<Real(Real)>
const& integrand)
225 Real
const half = (Real)0.5;
226 Real radius = half * (b -
a);
227 Real center = half * (b +
a);
229 for (
size_t i = 0; i < roots.size(); ++i)
231 result += coefficients[i] * integrand(radius*roots[i] + center);
GLfixed GLfixed GLint GLint order
static Real GaussianQuadrature(std::vector< Real > const &roots, std::vector< Real >const &coefficients, Real a, Real b, std::function< Real(Real)> const &integrand)
GLboolean GLboolean GLboolean GLboolean a
static Real TrapezoidRule(int numSamples, Real a, Real b, std::function< Real(Real)> const &integrand)
static void ComputeQuadratureInfo(int degree, std::vector< Real > &roots, std::vector< Real > &coefficients)
GLboolean GLboolean GLboolean b
INT32 INT32 * denominator
GLfloat GLfloat GLfloat GLfloat h
static Real Romberg(int order, Real a, Real b, std::function< Real(Real)> const &integrand)
static int Find(int degree, Real const *c, unsigned int maxIterations, Real *roots)