For the application of a function on a linear expression, the function as well as its derivative is required. For this reason, the following functional structure is required in order to apply a function on the linear expression:
class sine_on_linear_expression
{
double operator()(double x) const {return sin(x);}
class derivative
{
double operator()(double x) const {return cos(x);}
}
};
It can be seen that the class is written as function object which yields the respective function value and which additionally comprises a nested class called derivative. This nested class again is a function object which implements the operator(). The application of this operator yields the derivative of the function.
The application of the function to a linear expression can be treated as follows:
template<typename Func, typename NumericT>
apply_func(linearized_expression<NumericT> & expr)
{
linearized_expression<NumericT> result;
Func f;
Func::derivative f_;
double deriv = f_(expr.RHS);
result.RHS = f(expr.RHS);
map_iter_t iter = coefficients.begin();
while (iter != coefficients.end())
{
expr.coefficients[(*iter).first] =
(*iter).second * deriv;
}
}
Using further beautifications, typically object generators [88], explicit function objects can be constructed from the sine_on_linear_expression class so that the application of the expression can be written as follows
func<sine_on_linear_expression> Sin; linearized_expression<double> expr1; linearized_expression<double> expr2; ... expr2 = Sin(expr1);