CompiledFunction.h

Classes

CompiledFunction -- Form a linear combination of function objects. (full description)

template <class T> class CompiledFunction : public CompiledParam<T>

Interface

Public Members
CompiledFunction() : CompiledParam<T>()
CompiledFunction(const CompiledFunction<T> &other) : other<T>(other)
template <class W> CompiledFunction(const CompiledFunction<W> &other) : CompiledParam<T>(other)
CompiledFunction<T> &operator=(const CompiledFunction<T> &other)
virtual ~CompiledFunction()
virtual T eval(typename Function<T>::FunctionArg x) const
virtual Function<T> *clone() const
virtual Function<typename FunctionTraits<T>::DiffType> *cloneAD() const
virtual Function<typename FunctionTraits<T>::BaseType> *cloneNonAD() const

Description

Review Status

Programs:
Tests:

Prerequisite

Synopsis

Given a string describing an expression (see FuncExpression class for details of the expression), the CompiledFunctionclass wraps this expression as a Function (see Function class) which can be used in all places where functions can be used (e.g. see Fitting).

The CompiledParam class takes care of the parameter interface.

Example

In the following example a Gaussian profile with three parameters (height, center and halfwidth) is specified and its value and derivatives with respect to the parameters are calculated at x=[1.9,2,2.1].
    // the Gaussian
    CompiledFunction<Double> prof;
    prof.setFunction("p0*exp(-((x-p1)/p2)^2)");
    prof[0] = 2;				// the height
    prof[1] = 1.5;			// the center
    prof[2] = 1;				// the width
    Vector<Double> x(3);
    x[0] = 1.9; x[1] = 2.0; x[2] = 2.1;
    for (uInt i=0; i<3; ++i) {
      cout << "Gaussian at x=" << x[i] << ": " << prof(x[i]) << endl;
    };
    // Calculate automatic derivatives of same function:
    CompiledFunction<AutoDiff<Double> > profad;
    profad.setFunction("p0*exp(-((x-p1)/p2)^2)");
    // Set the parameters (note the specification of the number of
    // derivatives and which derivative the parameter is)
    profad[0] = AutoDiff<Double>(2,  3,0);	// the height
    profad[1] = AutoDiff<Double>(1.5,3,1);	// the center
    profad[2] = AutoDiff<Double>(1,  3,2);      	// the width
    for (uInt i=0; i<3; ++i) {
      cout << "Gaussian at x=" << x[i] << ": " << profad(x[i]) << endl;
    };
    cout << "Value (x=2): " << profad(x[1]).value() << endl;
    cout << "Derivatives: " << profad(x[1]).derivatives() << endl;
    cout << "Derivative1: " << profad(x[1]).derivatives()[1] << endl;
    
will produce the output:
	Gaussian at x=1.9: 1.70429
	Gaussian at x=2: 1.5576
	Gaussian at x=2.1: 1.39535
	Gaussian at x=1.9: (1.70429, [0.852144, 1.36343, 0.545372])
	Gaussian at x=2: (1.5576, [0.778801, 1.5576, 0.778801])
	Gaussian at x=2.1: (1.39535, [0.697676, 1.67442, 1.00465])
	Value (x=2): 1.5576
	Derivatives: [0.778801, 1.5576, 0.778801]
	Derivative1: 1.5576

Template Type Argument Requirements (T)

Thrown Exceptions

Motivation

This class was created to allow specialization of the function evaluation in a simple way.

To Do

Member Description

CompiledFunction() : CompiledParam<T>()

The default constructor -- no functions, no parameters, nothing, the function operator returns a 0.

CompiledFunction(const CompiledFunction<T> &other) : other<T>(other)
template <class W> CompiledFunction(const CompiledFunction<W> &other) : CompiledParam<T>(other)

Make this object a (deep) copy of other.

CompiledFunction<T> &operator=(const CompiledFunction<T> &other)

Make this object a (deep) copy of other.

virtual ~CompiledFunction()

Destructor

virtual T eval(typename Function<T>::FunctionArg x) const

Evaluate the function at x.

virtual Function<T> *clone() const
virtual Function<typename FunctionTraits<T>::DiffType> *cloneAD() const
virtual Function<typename FunctionTraits<T>::BaseType> *cloneNonAD() const

Return a copy of this object from the heap. The caller is responsible for deleting the pointer.