Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Simplex Optimization


Function Objects

Function Objects

I started writing this template after reading Qiang Liu's C/C++ Users Journal article [2] on the use of function objects for solving a Newton-Raphson problem. Function objects are just classes that implement a "call operator" [ operator ( ) (. . .)]. For Liu's problem, simple function objects that are exactly drop-in replacements for C's function pointers elegantly solved his problem: how to have different function signatures for different problems without hardcoding them. Function objects also provide a convenient method for holding data specific to the problem being solved. So you get several benefits from using function objects: no hardcoded function signatures, a convenient place to store problem-specific data, and compatibility with pointers to functions (for a C-like solution to function evaluations).

Modeling my solution after Liu's, I wrote a template for the simplex method that used a function object for function evaluation. Unlike Liu, I needed at least three different kinds of information from the parameterized class: the function value evaluated at some point, a starting point for the simplex, and the number of variables in the simplex. I solved this by making function object's class more complex. I implemented two different call operators, using the overloading ability of C++.

double operator ( ) ( const T t );

T operator ( ) ( );

So the function evaluation at a point is returned if a point is supplied. If no parameters are supplied, then a vector of the parameters is returned. In my case, I returned the starting point so that the simplex could be told where to start. Since I used std::valarray to hold the parameter values, the same call to the call operator that returns the starting point to the simplex template can use:

m_nVar = t.size( );
to determine how many parameters there are in the problem. The simplex template needs that value to compose m_nVar+1 points that make up the simplex and to assign the size of each of those points (stored in valarrays). Any program that uses this simplex template will need to implement both of these call operators.

Why function object? Originally, the idea was as a simple replacement for pointers to functions. An important result of using function objects is that the source code (template in this case) is not dependent on a particular set of function signatures.

— L.A.

Back to Article


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.