|
| |
C# managed code classes to integrate
functions:
-
along lines,
-
over areas, and
-
through out volumes
based on the genius of Carl F Gauss.
One of my favorite explanations of the
theory of quadrature can be found
here.
Contents
Using this new, managed-code version of our
popular Quadrature Toolkit is as easy as 1-2-3. It gives you the classes you
need to make numerical integration part of your own apps:
- Derive your class from the
IIntegrableIn1D, IIntegrableIn2D, or IIntegrableIn3D interface
- Implement the two methods Evaluate() and
Accumulate() in your class.
- Invoke the Integrator with your class.
You get the source code (more than 1,100
lines of managed C#), which includes test programs that exercise all the class methods, so you can
see clearly how they work. All solution, project, and other supporting files are
also included.
And of course, the code is guaranteed; cf
Policies.
Quadrature.NET is .NET 2.0 compatible.
top
|
Quadrature.cs |
includes IIntegrableXX interfaces and
IntegratorXD C# classes |
|
|
|
|
Program.cs |
console program
driver for tests |
|
Test1D.cs |
tests of 1D quadrature |
|
Test2D.cs |
tests of 2D quadrature |
|
Test3D.cs |
tests of 3D quadrature |
top
Interfaces
|
IIntegrableIn1D |
signatures for 1D quadrature |
|
IIntegrableIn2D |
signatures for 2D quadrature |
|
IIntegrableIn3D |
signatures for 3D quadrature |
Classes
|
Integrator1D |
quadrature for
implementers of IIntegrable1D |
|
Integrator2D |
quadrature for
implementers of IIntegrable2D |
|
Integrator3D |
quadrature for
implementers of IIntegrable3D |
|
Internal classes providing weights and sampling point
locations |
top
The interfaces are:
public interface
IIntegrableIn1D {
void Evaluate(double
r, double rWeight);
void Accumulate();
}
public interface
IIntegrableIn2D {
void Evaluate(double
r, double rWeight,
double s,
double sWeight);
void Accumulate();
}
public interface
IIntegrableIn3D {
void Evaluate(double
r, double rWeight,
double s,
double sWeight,
double t,
double tWeight);
void Accumulate();
}
where
-
r, s
and
t
are
“sampling point” coordinates in the range -1 .. +1.
-
rWeight, sWeight,
and
tWeight are Gaussian “weights” associated with each
sampling point.
-
All sampling points and weights are provided by the Integrator
classes (see below).
top
A simple way to explain these classes is by
example. Suppose we wish to integrate the function y = x2 + 1 over
the range -1 to +1 (the exact integral of this function is 8/3).
The equation x2 + 1 is of order q
= 2.
The order of Gaussian quadrature 'n'
necessary to the exact solution is given by 2n-1 = q. Solving for n in this
case, we obtain n = 3/2. Rounding up suggests that Quadrature using n = 2
sampling points will produce the exact solution.
Hence we write the class to evaluate the
integral:
public class
Test1DIntegrator :
IIntegrableIn1D {
private
double valueAtPoint; // result of Evaluate
private double
accumulatedValue; // 'sum of' all evaluations
public
Test1DIntegrator() {
// ... other initializing
accumulatedValue = 0.0;
}
public double
Value() { // return result of quadrature
return accumulatedValue;
}
public void
Accumulate() { // 'sum'
accumulatedValue += valueAtPoint;
}
public void
Evaluate(double r,
double rWt){
valueAtPoint = rWt * ( r * r + 1 );
}
}
Notes:
- The constructor initializes the accumulated value and any
others that the function may require.
- Value() is a convenience to return the result of the quadrature. This could just as easily be a property.
- Accumulate() assembles the quadrature by accumulating the
results of function evaluation at each sampling point.
- The contents of Evaluate() are the most intricate. You
must provide code to evaluate your function at each of the sampling point
coordinates r, and multiply the function result by the weight at that
sampling point rWt. Both the coordinate and the weight are provided
by the Integrator, which calls your Evaluate(), and then calls your
Accumulate() (see below).
The Integrators
All 0f the integrators follow the same
pattern, illustrated here in a method of some object:
public void
test1Dintegrator() {
Test1DIntegrator integrand =
new Test1DIntegrator();
Integrator1D q =
new Integrator1D(
2 ); // 2 sample points
q.Integrate(integrand);
// calls Evaluate(), Accumulate()
Console.WriteLine("Value:
{0}", integrand.Value());
}
Notes:
- Instantiate
your object as
Test1DIntegrator
integrand.
- Instantiate an
integrator.
- Pass your
integrand to the integrator.
- Display the
result. In this case, the output is (as expected: 8/3)
Value:
2.66666666666667.
If one were to "integrate" this function in Excel, 21 function
evaluations produces the value 2.67, as shown on this chart

top
Pricing US$99
Delivery by email
|