Functions

Header file function.h

Generic function class `Function'

A template class, generated on the classes of the argument and the result. Subclasses must provide data, input/output, evaluation and evaluation of the derivative. Inherits from `Classio'.

Example:

#include<function.h>
...
class my_function: public Function<double,double>
{
   public:

   // data
   double m,c;

   // input and output
   void Read(istream& stream) { stream >> m >> c; }
   void Write(ostream& stream) const { stream << m << ' ' << c; }

   // evaluation
   double evaluate(const double& x) { return m * x + c; }
};

The method `()' is defined to evaluate the function, e.g.

my_function f1; cin >> f1; // read parameters
double x; cin >> x; // read argument
double y = f1(x); cout << y << endl; // evaluate function

1D real function class: `Realfunction1d'

Inherits from Function<double,double>

A generic write-with-switch is provided for writing pointered polymorphic subclasses. The corresponding read-with-switch is provided for groups of functions in the library. To use, define a pointer to an instance of the appropriate class. Data in an input stream must first specify the class identifier (the name, with lowercase first character) then the data. When writing to an output stream, the identifier is written followed by the data. Example:

#include<function.h>
int main()
{
Realfunction1d *f; cin >> f;
double x; cin >> x;
cout << "f(x) = " << (*f)(x) << endl;
}

Constant

Data is a double, c. Evaluating the function returns c.

Artificial example: (one would tend to use `double'; this subclass is provided for use with polymorphic functions)

#include<function.h>
...
Constant p; cin >> p;
double x; cin >> x;
double y = p(x); cout << y << endl;

Parameter

Data is the parameter name, up to 80 characters. Evaluating the function returns the argument.

Polynomial

Data is an array of doubles, ci for i=0 to n-1. The function evaluated is c0+c1x+...+cn-1xn-1

Example:

#include<function.h>
...
Polynomial p; cin >> p;
double x; cin >> x;
double y = p(x); cout << y << endl;

Methods

Gaussian

Data is the centre (xc) and width (rho). The function evaluated is exp[-(x - xc)2 / rho2] / [sqrt(2 pi) rho].

Example:

#include<function.h>
...
Gaussian g; cin >> m;
double x; cin >> x;
double y = g(x); cout << y << endl;

Monomial

Data is the power of the argument (p) and the factor (c). The function evaluated is cxp.

Example:

#include<function.h>
...
Monomial m; cin >> m;
double x; cin >> x;
double y = m(x); cout << y << endl;

General_polynomial

Data is an array of monomials, mi for i=0 to n-1. The function evaluated is m0(x)+m1(x)+...+mn-1(x)

Example:

#include<function.h>
...
General_polynomial p; cin >> p;
double x; cin >> x;
double y = p(x); cout << y << endl;

Table1d

Data is an array of pairs of doubles modelling f(x) as piecewise linear. The function evaluated is the linear interpolation (or extrapolation).

Data

Functions

Example

#include<function.h>
...
Table1d t; cin >> t;
double x; cin >> x;
double y = t(x); cout << y << endl; // evaluate function
...
int n; cin >> n;
Array<double> tn = t[n]; // tn should be returned with 2 elements

Table1d_polyint

Data is an array of pairs of doubles modelling f(x) as piecewise polynomial. The function evaluated is the polynomial interpolation (or extrapolation). (The inverse interpolation is still linear.)

Data

As `Table1d', plus

Functions

As for `Table1d', except

Sum

Data is an array of Realfunction1ds. The function evaluated is the sum of the constituent functions.

Product

Data is an array of Realfunction1ds. The function evaluated is the product of the constituent functions.

Nested

Data is an array of Realfunction1ds. The function evaluated is the nest of the constituent functions, i.e. f1(f2(...(fn(x))...)).

Piecewise

A default function, and a set of functions each with a range of validity in x. Data:
f0		default function
number_of_ranged_functions
For each:
   x1 x2 f	range (min, max x) and function
f0 and each f are Realfunction1ds.

Spline

A set of functions fi with associated knots x0i, the spline function being
f(x) = sumi fi(x - x0i) theta(x - x0i)

where theta is the Heaviside step function. Functions must be specified in order of increasing x0. Data:
number_of__functions
For each:
   x0 f
Each f is a Realfunction1d.

Displaced function

A function f and an offset x0 to its argument, the displaced function being f(x-x0). Data:
x0 f
f is a Realfunction1d.

Transcendental

This class is used to contain transcendental functions with no parameters. Subclasses are exp, ln, log10,sin, cos, tan, sinh, cosh, tanh from the standard C++ maths library, and alegpoly, lnGamma, Gamma, Bessel_J0, Bessel_J1, Bessel_Y0, Bessel_Y1, elliptic_K, elliptic_E, Erfc, Erf, Fresnel_C, Fresnel_S from the transcendental functions part of this maths library.

Miscellaneous functions

modulus

double mod(const double a, const double b)
returns a mod b, i.e. a - int(a/b) * b