3 <meta http-equiv=
"Content-Type" content=
"text/html; charset=US-ASCII">
4 <title>Using C++
11 Lambda's
</title>
5 <link rel=
"stylesheet" href=
"../../../math.css" type=
"text/css">
6 <meta name=
"generator" content=
"DocBook XSL Stylesheets V1.77.1">
7 <link rel=
"home" href=
"../../../index.html" title=
"Math Toolkit 2.5.1">
8 <link rel=
"up" href=
"../root_finding_examples.html" title=
"Examples of Root-Finding (with and without derivatives)">
9 <link rel=
"prev" href=
"cbrt_eg.html" title=
"Finding the Cubed Root With and Without Derivatives">
10 <link rel=
"next" href=
"5th_root_eg.html" title=
"Computing the Fifth Root">
12 <body bgcolor=
"white" text=
"black" link=
"#0000FF" vlink=
"#840084" alink=
"#0000FF">
13 <table cellpadding=
"2" width=
"100%"><tr>
14 <td valign=
"top"><img alt=
"Boost C++ Libraries" width=
"277" height=
"86" src=
"../../../../../../../boost.png"></td>
15 <td align=
"center"><a href=
"../../../../../../../index.html">Home
</a></td>
16 <td align=
"center"><a href=
"../../../../../../../libs/libraries.htm">Libraries
</a></td>
17 <td align=
"center"><a href=
"http://www.boost.org/users/people.html">People
</a></td>
18 <td align=
"center"><a href=
"http://www.boost.org/users/faq.html">FAQ
</a></td>
19 <td align=
"center"><a href=
"../../../../../../../more/index.htm">More
</a></td>
22 <div class=
"spirit-nav">
23 <a accesskey=
"p" href=
"cbrt_eg.html"><img src=
"../../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../root_finding_examples.html"><img src=
"../../../../../../../doc/src/images/up.png" alt=
"Up"></a><a accesskey=
"h" href=
"../../../index.html"><img src=
"../../../../../../../doc/src/images/home.png" alt=
"Home"></a><a accesskey=
"n" href=
"5th_root_eg.html"><img src=
"../../../../../../../doc/src/images/next.png" alt=
"Next"></a>
26 <div class=
"titlepage"><div><div><h4 class=
"title">
27 <a name=
"math_toolkit.roots.root_finding_examples.lambda"></a><a class=
"link" href=
"lambda.html" title=
"Using C++11 Lambda's">Using
29 </h4></div></div></div>
31 Since all the root finding functions accept a function-object, they can
32 be made to work (often in a lot less code) with C++
11 lambda's. Here's
33 the much reduced code for our
"toy" cube root function:
35 <pre class=
"programlisting"><span class=
"keyword">template
</span> <span class=
"special"><</span><span class=
"keyword">class
</span> <span class=
"identifier">T
</span><span class=
"special">></span>
36 <span class=
"identifier">T
</span> <span class=
"identifier">cbrt_2deriv_lambda
</span><span class=
"special">(
</span><span class=
"identifier">T
</span> <span class=
"identifier">x
</span><span class=
"special">)
</span>
37 <span class=
"special">{
</span>
38 <span class=
"comment">// return cube root of x using
1st and
2nd derivatives and Halley.
</span>
39 <span class=
"comment">//using namespace std; // Help ADL of std functions.
</span>
40 <span class=
"keyword">using
</span> <span class=
"keyword">namespace
</span> <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">math
</span><span class=
"special">::
</span><span class=
"identifier">tools
</span><span class=
"special">;
</span>
41 <span class=
"keyword">int
</span> <span class=
"identifier">exponent
</span><span class=
"special">;
</span>
42 <span class=
"identifier">frexp
</span><span class=
"special">(
</span><span class=
"identifier">x
</span><span class=
"special">,
</span> <span class=
"special">&</span><span class=
"identifier">exponent
</span><span class=
"special">);
</span> <span class=
"comment">// Get exponent of z (ignore mantissa).
</span>
43 <span class=
"identifier">T
</span> <span class=
"identifier">guess
</span> <span class=
"special">=
</span> <span class=
"identifier">ldexp
</span><span class=
"special">(
</span><span class=
"number">1.
</span><span class=
"special">,
</span> <span class=
"identifier">exponent
</span> <span class=
"special">/
</span> <span class=
"number">3</span><span class=
"special">);
</span> <span class=
"comment">// Rough guess is to divide the exponent by three.
</span>
44 <span class=
"identifier">T
</span> <span class=
"identifier">min
</span> <span class=
"special">=
</span> <span class=
"identifier">ldexp
</span><span class=
"special">(
</span><span class=
"number">0.5</span><span class=
"special">,
</span> <span class=
"identifier">exponent
</span> <span class=
"special">/
</span> <span class=
"number">3</span><span class=
"special">);
</span> <span class=
"comment">// Minimum possible value is half our guess.
</span>
45 <span class=
"identifier">T
</span> <span class=
"identifier">max
</span> <span class=
"special">=
</span> <span class=
"identifier">ldexp
</span><span class=
"special">(
</span><span class=
"number">2.
</span><span class=
"special">,
</span> <span class=
"identifier">exponent
</span> <span class=
"special">/
</span> <span class=
"number">3</span><span class=
"special">);
</span> <span class=
"comment">// Maximum possible value is twice our guess.
</span>
46 <span class=
"keyword">const
</span> <span class=
"keyword">int
</span> <span class=
"identifier">digits
</span> <span class=
"special">=
</span> <span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">numeric_limits
</span><span class=
"special"><</span><span class=
"identifier">T
</span><span class=
"special">>::
</span><span class=
"identifier">digits
</span><span class=
"special">;
</span> <span class=
"comment">// Maximum possible binary digits accuracy for type T.
</span>
47 <span class=
"comment">// digits used to control how accurate to try to make the result.
</span>
48 <span class=
"keyword">int
</span> <span class=
"identifier">get_digits
</span> <span class=
"special">=
</span> <span class=
"keyword">static_cast
</span><span class=
"special"><</span><span class=
"keyword">int
</span><span class=
"special">>(
</span><span class=
"identifier">digits
</span> <span class=
"special">*
</span> <span class=
"number">0.4</span><span class=
"special">);
</span> <span class=
"comment">// Accuracy triples with each step, so stop when just
</span>
49 <span class=
"comment">// over one third of the digits are correct.
</span>
50 <span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">uintmax_t
</span> <span class=
"identifier">maxit
</span> <span class=
"special">=
</span> <span class=
"number">20</span><span class=
"special">;
</span>
51 <span class=
"identifier">T
</span> <span class=
"identifier">result
</span> <span class=
"special">=
</span> <span class=
"identifier">halley_iterate
</span><span class=
"special">(
</span>
52 <span class=
"comment">// lambda function:
</span>
53 <span class=
"special">[
</span><span class=
"identifier">x
</span><span class=
"special">](
</span><span class=
"keyword">const
</span> <span class=
"identifier">T
</span><span class=
"special">&</span> <span class=
"identifier">g
</span><span class=
"special">){
</span> <span class=
"keyword">return
</span> <span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">make_tuple
</span><span class=
"special">(
</span><span class=
"identifier">g
</span> <span class=
"special">*
</span> <span class=
"identifier">g
</span> <span class=
"special">*
</span> <span class=
"identifier">g
</span> <span class=
"special">-
</span> <span class=
"identifier">x
</span><span class=
"special">,
</span> <span class=
"number">3</span> <span class=
"special">*
</span> <span class=
"identifier">g
</span> <span class=
"special">*
</span> <span class=
"identifier">g
</span><span class=
"special">,
</span> <span class=
"number">6</span> <span class=
"special">*
</span> <span class=
"identifier">g
</span><span class=
"special">);
</span> <span class=
"special">},
</span>
54 <span class=
"identifier">guess
</span><span class=
"special">,
</span> <span class=
"identifier">min
</span><span class=
"special">,
</span> <span class=
"identifier">max
</span><span class=
"special">,
</span> <span class=
"identifier">get_digits
</span><span class=
"special">,
</span> <span class=
"identifier">maxit
</span><span class=
"special">);
</span>
55 <span class=
"keyword">return
</span> <span class=
"identifier">result
</span><span class=
"special">;
</span>
56 <span class=
"special">}
</span>
59 Full code of this example is at
<a href=
"../../../../../example/root_finding_example.cpp" target=
"_top">root_finding_example.cpp
</a>,
62 <table xmlns:
rev=
"http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width=
"100%"><tr>
63 <td align=
"left"></td>
64 <td align=
"right"><div class=
"copyright-footer">Copyright
© 2006-
2010,
2012-
2014 Nikhar Agrawal,
65 Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert
66 Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan R
åde, Gautam Sewani,
67 Benjamin Sobotta, Thijs van den Berg, Daryle Walker and Xiaogang Zhang
<p>
68 Distributed under the Boost Software License, Version
1.0. (See accompanying
69 file LICENSE_1_0.txt or copy at
<a href=
"http://www.boost.org/LICENSE_1_0.txt" target=
"_top">http://www.boost.org/LICENSE_1_0.txt
</a>)
74 <div class=
"spirit-nav">
75 <a accesskey=
"p" href=
"cbrt_eg.html"><img src=
"../../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../root_finding_examples.html"><img src=
"../../../../../../../doc/src/images/up.png" alt=
"Up"></a><a accesskey=
"h" href=
"../../../index.html"><img src=
"../../../../../../../doc/src/images/home.png" alt=
"Home"></a><a accesskey=
"n" href=
"5th_root_eg.html"><img src=
"../../../../../../../doc/src/images/next.png" alt=
"Next"></a>