Functions are passed to numerical routines using template-based function classes, sometimes called "functors". O2scl classes which accept functions as parameters generally default to types built upon std::function
. If the user would like to use Boost function objects instead, these may also be used, simply by specifying the Boost function type in the template parameter.
Some template aliases are defined to save typing of the function types, e.g.
n
functions of n
variables (used in solver classes)n
derivatives as a function of n
function values and the value of the independent variableThe example below demonstrates how C++11 function objects can be used with the o2scl::root_brent_gsl solver.
The C++ standard library functors employ copy construction at various types, so one must be careful about the types involved in creating the functor. Generally, all classes should have constructors and structs should be avoided because they can cause difficulties with default copy construction.
There is a small overhead associated with the indirection: a "user class" accesses the function class which then calls function which was specified in the constructor of the function class. In many problems, the overhead associated with the indirection is small. Some of this overhead can always be avoided by inheriting directly from the function class and thus the user class will make a direct virtual function call. To eliminate the overhead entirely, one can specify a new type for the template parameter in the user class.
This example shows how to provide functions to O2scl classes by solving the equation
Where and
. The parameter
is stored as member data for the class, and the parameter
is an argument to the member function.
The image below shows how the solver progresses to the solution of the example function.
Documentation generated with Doxygen. Provided under the
GNU Free Documentation License (see License Information).