## Continued Fractions

**numerics4net** provides the means to create and evaluate arbitrary
continued fractions. See Continued Fraction
for an in depth definition of continued fractions. To create a
continued fraction, simply create a new numerics4net.continuedfraction.ContinuedFraction
instance providing delegates for the a and b
coefficients, which return the continued fraction terms.

The ContinuedFraction class is quite flexible with regards to the variety of continued fractions it can represent. For example it can be used on very basic continued fractions, ones having all constant terms like the golden ratio constant:

double GoldenRatioCoefficient(uint n, double x) { return 1.0; } ContinuedFraction.Coefficient goldenRatioCoefficient = new ContinuedFraction.Coefficient(GoldenRatioCoefficient); ContinuedFraction goldenRatio = new ContinuedFraction(goldenRatioCoefficient, goldenRatioCoefficient); // returns 1.6180339887499... for all input values. double x = goldenRatio.Evaluate(0.0);

Also, the ContinuedFraction class can represent continued
fractions whose terms are functions of their indices. An example of
this type of continued fraction can be used to compute the constant
*e*:

double EA(uint n, double x) { return n + 1.0; } double EB(uint n, double x) { return n; } ContinuedFraction.Coefficient eaCoefficient = new ContinuedFraction.Coefficient(EA); ContinuedFraction.Coefficient ebCoefficient = new ContinuedFraction.Coefficient(EB); ContinuedFraction e = new ContinuedFraction(eaCoefficient, ebCoefficient); double x = 2.0 + 1.0 / e.Evaluate(0.0);

The most involved type of continued fractions are ones whose terms are not only functions of their indices, but also functions of an evaluation point. A lot of transcendental functions have continued fraction representations. As such, these functions can be evaluated numerically using the ContinuedFraction class. For example, the exponetial function can be evaulated with a continued fraction:

double ExponentialA(uint n, double x) { if (n == 0) { return 1.0; } else if (n % 2 == 0) { // even return 2.0; } else { // odd return n; } } double ExponentialB(uint n, double x) { if (n % 2 == 0) { // even return x; } else { // odd return -x; } } ContinuedFraction.Coefficient exponentialACoefficient = new ContinuedFraction.Coefficient(ExponentialA); ContinuedFraction.Coefficient exponentialBCoefficient = new ContinuedFraction.Coefficient(ExponentialB); ContinuedFraction e = new ContinuedFraction(exponentialACoefficient, exponentialBCoefficient); double x = exponential.Evaluate(2.0); // Math.Exp(2.0) double x = exponential.Evaluate(4.0); // Math.Exp(4.0)