1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Continued Fraction Evaluation</title> 5<link rel="stylesheet" href="../../math.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../index.html" title="Math Toolkit 2.12.0"> 8<link rel="up" href="../internals.html" title="Internal tools"> 9<link rel="prev" href="series_evaluation.html" title="Series Evaluation"> 10<link rel="next" href="recurrence.html" title="Tools For 3-Term Recurrence Relations"> 11</head> 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> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="series_evaluation.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../internals.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="recurrence.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="math_toolkit.internals.cf"></a><a class="link" href="cf.html" title="Continued Fraction Evaluation">Continued Fraction Evaluation</a> 28</h3></div></div></div> 29<h5> 30<a name="math_toolkit.internals.cf.h0"></a> 31 <span class="phrase"><a name="math_toolkit.internals.cf.synopsis"></a></span><a class="link" href="cf.html#math_toolkit.internals.cf.synopsis">Synopsis</a> 32 </h5> 33<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</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><span class="identifier">fraction</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 34</pre> 35<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">tools</span><span class="special">{</span> 36 37<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Gen</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">U</span><span class="special">></span> 38<span class="keyword">typename</span> <span class="identifier">detail</span><span class="special">::</span><span class="identifier">fraction_traits</span><span class="special"><</span><span class="identifier">Gen</span><span class="special">>::</span><span class="identifier">result_type</span> 39 <span class="identifier">continued_fraction_b</span><span class="special">(</span><span class="identifier">Gen</span><span class="special">&</span> <span class="identifier">g</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">U</span><span class="special">&</span> <span class="identifier">tolerance</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">uintmax_t</span><span class="special">&</span> <span class="identifier">max_terms</span><span class="special">)</span> 40 41<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Gen</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">U</span><span class="special">></span> 42<span class="keyword">typename</span> <span class="identifier">detail</span><span class="special">::</span><span class="identifier">fraction_traits</span><span class="special"><</span><span class="identifier">Gen</span><span class="special">>::</span><span class="identifier">result_type</span> 43 <span class="identifier">continued_fraction_b</span><span class="special">(</span><span class="identifier">Gen</span><span class="special">&</span> <span class="identifier">g</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">U</span><span class="special">&</span> <span class="identifier">tolerance</span><span class="special">)</span> 44 45<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Gen</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">U</span><span class="special">></span> 46<span class="keyword">typename</span> <span class="identifier">detail</span><span class="special">::</span><span class="identifier">fraction_traits</span><span class="special"><</span><span class="identifier">Gen</span><span class="special">>::</span><span class="identifier">result_type</span> 47 <span class="identifier">continued_fraction_a</span><span class="special">(</span><span class="identifier">Gen</span><span class="special">&</span> <span class="identifier">g</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">U</span><span class="special">&</span> <span class="identifier">tolerance</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">uintmax_t</span><span class="special">&</span> <span class="identifier">max_terms</span><span class="special">)</span> 48 49<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Gen</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">U</span><span class="special">></span> 50<span class="keyword">typename</span> <span class="identifier">detail</span><span class="special">::</span><span class="identifier">fraction_traits</span><span class="special"><</span><span class="identifier">Gen</span><span class="special">>::</span><span class="identifier">result_type</span> 51 <span class="identifier">continued_fraction_a</span><span class="special">(</span><span class="identifier">Gen</span><span class="special">&</span> <span class="identifier">g</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">U</span><span class="special">&</span> <span class="identifier">tolerance</span><span class="special">)</span> 52 53<span class="comment">//</span> 54<span class="comment">// These interfaces are present for legacy reasons, and are now deprecated:</span> 55<span class="comment">//</span> 56<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Gen</span><span class="special">></span> 57<span class="keyword">typename</span> <span class="identifier">detail</span><span class="special">::</span><span class="identifier">fraction_traits</span><span class="special"><</span><span class="identifier">Gen</span><span class="special">>::</span><span class="identifier">result_type</span> 58 <span class="identifier">continued_fraction_b</span><span class="special">(</span><span class="identifier">Gen</span><span class="special">&</span> <span class="identifier">g</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">bits</span><span class="special">);</span> 59 60<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Gen</span><span class="special">></span> 61<span class="keyword">typename</span> <span class="identifier">detail</span><span class="special">::</span><span class="identifier">fraction_traits</span><span class="special"><</span><span class="identifier">Gen</span><span class="special">>::</span><span class="identifier">result_type</span> 62 <span class="identifier">continued_fraction_b</span><span class="special">(</span><span class="identifier">Gen</span><span class="special">&</span> <span class="identifier">g</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">bits</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">uintmax_t</span><span class="special">&</span> <span class="identifier">max_terms</span><span class="special">);</span> 63 64<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Gen</span><span class="special">></span> 65<span class="keyword">typename</span> <span class="identifier">detail</span><span class="special">::</span><span class="identifier">fraction_traits</span><span class="special"><</span><span class="identifier">Gen</span><span class="special">>::</span><span class="identifier">result_type</span> 66 <span class="identifier">continued_fraction_a</span><span class="special">(</span><span class="identifier">Gen</span><span class="special">&</span> <span class="identifier">g</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">bits</span><span class="special">);</span> 67 68<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Gen</span><span class="special">></span> 69<span class="keyword">typename</span> <span class="identifier">detail</span><span class="special">::</span><span class="identifier">fraction_traits</span><span class="special"><</span><span class="identifier">Gen</span><span class="special">>::</span><span class="identifier">result_type</span> 70 <span class="identifier">continued_fraction_a</span><span class="special">(</span><span class="identifier">Gen</span><span class="special">&</span> <span class="identifier">g</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">bits</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">uintmax_t</span><span class="special">&</span> <span class="identifier">max_terms</span><span class="special">);</span> 71 72<span class="special">}}}</span> <span class="comment">// namespaces</span> 73</pre> 74<h5> 75<a name="math_toolkit.internals.cf.h1"></a> 76 <span class="phrase"><a name="math_toolkit.internals.cf.description"></a></span><a class="link" href="cf.html#math_toolkit.internals.cf.description">Description</a> 77 </h5> 78<p> 79 <a href="http://en.wikipedia.org/wiki/Continued_fraction" target="_top">Continued fractions 80 are a common method of approximation. </a> These functions all evaluate 81 the continued fraction described by the <span class="emphasis"><em>generator</em></span> type 82 argument. The functions with an "_a" suffix evaluate the fraction: 83 </p> 84<div class="blockquote"><blockquote class="blockquote"><p> 85 <span class="inlinemediaobject"><img src="../../../equations/fraction2.svg"></span> 86 87 </p></blockquote></div> 88<p> 89 and those with a "_b" suffix evaluate the fraction: 90 </p> 91<div class="blockquote"><blockquote class="blockquote"><p> 92 <span class="inlinemediaobject"><img src="../../../equations/fraction1.svg"></span> 93 94 </p></blockquote></div> 95<p> 96 This latter form is somewhat more natural in that it corresponds with the 97 usual definition of a continued fraction, but note that the first <span class="emphasis"><em>a</em></span> 98 value returned by the generator is discarded. Further, often the first <span class="emphasis"><em>a</em></span> 99 and <span class="emphasis"><em>b</em></span> values in a continued fraction have different 100 defining equations to the remaining terms, which may make the "_a" 101 suffixed form more appropriate. 102 </p> 103<p> 104 The generator type should be a function object which supports the following 105 operations: 106 </p> 107<div class="informaltable"><table class="table"> 108<colgroup> 109<col> 110<col> 111</colgroup> 112<thead><tr> 113<th> 114 <p> 115 Expression 116 </p> 117 </th> 118<th> 119 <p> 120 Description 121 </p> 122 </th> 123</tr></thead> 124<tbody> 125<tr> 126<td> 127 <p> 128 Gen::result_type 129 </p> 130 </td> 131<td> 132 <p> 133 The type that is the result of invoking operator(). This can be 134 either an arithmetic or complex type, or a std::pair<> of 135 arithmetic or complex types. 136 </p> 137 </td> 138</tr> 139<tr> 140<td> 141 <p> 142 g() 143 </p> 144 </td> 145<td> 146 <p> 147 Returns an object of type Gen::result_type. 148 </p> 149 <p> 150 Each time this operator is called then the next pair of <span class="emphasis"><em>a</em></span> 151 and <span class="emphasis"><em>b</em></span> values is returned. Or, if result_type 152 is an arithmetic type, then the next <span class="emphasis"><em>b</em></span> value 153 is returned and all the <span class="emphasis"><em>a</em></span> values are assumed 154 to 1. 155 </p> 156 </td> 157</tr> 158</tbody> 159</table></div> 160<p> 161 In all the continued fraction evaluation functions the <span class="emphasis"><em>tolerance</em></span> 162 parameter is the precision desired in the result, evaluation of the fraction 163 will continue until the last term evaluated leaves the relative error in 164 the result less than <span class="emphasis"><em>tolerance</em></span>. The deprecated interfaces 165 take a number of digits precision here, internally they just convert this 166 to a tolerance and forward call. 167 </p> 168<p> 169 If the optional <span class="emphasis"><em>max_terms</em></span> parameter is specified then 170 no more than <span class="emphasis"><em>max_terms</em></span> calls to the generator will be 171 made, and on output, <span class="emphasis"><em>max_terms</em></span> will be set to actual 172 number of calls made. This facility is particularly useful when profiling 173 a continued fraction for convergence. 174 </p> 175<h5> 176<a name="math_toolkit.internals.cf.h2"></a> 177 <span class="phrase"><a name="math_toolkit.internals.cf.implementation"></a></span><a class="link" href="cf.html#math_toolkit.internals.cf.implementation">Implementation</a> 178 </h5> 179<p> 180 Internally these algorithms all use the modified Lentz algorithm: refer to 181 Numeric Recipes in C++, W. H. Press et all, chapter 5, (especially 5.2 Evaluation 182 of continued fractions, p 175 - 179) for more information, also Lentz, W.J. 183 1976, Applied Optics, vol. 15, pp. 668-671. 184 </p> 185<h5> 186<a name="math_toolkit.internals.cf.h3"></a> 187 <span class="phrase"><a name="math_toolkit.internals.cf.examples"></a></span><a class="link" href="cf.html#math_toolkit.internals.cf.examples">Examples</a> 188 </h5> 189<p> 190 All of these examples are in <a href="../../../../example/continued_fractions.cpp" target="_top">continued_fractions.cpp</a>. 191 </p> 192<p> 193 The <a href="http://en.wikipedia.org/wiki/Golden_ratio" target="_top">golden ratio phi 194 = 1.618033989...</a> can be computed from the simplest continued fraction 195 of all: 196 </p> 197<div class="blockquote"><blockquote class="blockquote"><p> 198 <span class="inlinemediaobject"><img src="../../../equations/fraction3.svg"></span> 199 200 </p></blockquote></div> 201<p> 202 We begin by defining a generator function: 203 </p> 204<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> 205<span class="keyword">struct</span> <span class="identifier">golden_ratio_fraction</span> 206<span class="special">{</span> 207 <span class="keyword">typedef</span> <span class="identifier">T</span> <span class="identifier">result_type</span><span class="special">;</span> 208 209 <span class="identifier">result_type</span> <span class="keyword">operator</span><span class="special">()()</span> 210 <span class="special">{</span> 211 <span class="keyword">return</span> <span class="number">1</span><span class="special">;</span> 212 <span class="special">}</span> 213<span class="special">};</span> 214</pre> 215<p> 216 The golden ratio can then be computed to double precision using: 217 </p> 218<pre class="programlisting"><span class="identifier">golden_ratio_fraction</span><span class="special"><</span><span class="keyword">double</span><span class="special">></span> <span class="identifier">func</span><span class="special">;</span> 219<span class="keyword">double</span> <span class="identifier">gr</span> <span class="special">=</span> <span class="identifier">continued_fraction_a</span><span class="special">(</span> 220 <span class="identifier">func</span><span class="special">,</span> 221 <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special"><</span><span class="keyword">double</span><span class="special">>::</span><span class="identifier">epsilon</span><span class="special">());</span> 222<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"The golden ratio is: "</span> <span class="special"><<</span> <span class="identifier">gr</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 223</pre> 224<p> 225 It's more usual though to have to define both the <span class="emphasis"><em>a</em></span>'s 226 and the <span class="emphasis"><em>b</em></span>'s when evaluating special functions by continued 227 fractions, for example the tan function is defined by: 228 </p> 229<div class="blockquote"><blockquote class="blockquote"><p> 230 <span class="inlinemediaobject"><img src="../../../equations/fraction4.svg"></span> 231 232 </p></blockquote></div> 233<p> 234 So its generator object would look like: 235 </p> 236<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> 237<span class="keyword">struct</span> <span class="identifier">tan_fraction</span> 238<span class="special">{</span> 239<span class="keyword">private</span><span class="special">:</span> 240 <span class="identifier">T</span> <span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">;</span> 241<span class="keyword">public</span><span class="special">:</span> 242 <span class="identifier">tan_fraction</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">v</span><span class="special">)</span> 243 <span class="special">:</span> <span class="identifier">a</span><span class="special">(-</span><span class="identifier">v</span> <span class="special">*</span> <span class="identifier">v</span><span class="special">),</span> <span class="identifier">b</span><span class="special">(-</span><span class="number">1</span><span class="special">)</span> 244 <span class="special">{}</span> 245 246 <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">T</span><span class="special">></span> <span class="identifier">result_type</span><span class="special">;</span> 247 248 <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">T</span><span class="special">></span> <span class="keyword">operator</span><span class="special">()()</span> 249 <span class="special">{</span> 250 <span class="identifier">b</span> <span class="special">+=</span> <span class="number">2</span><span class="special">;</span> 251 <span class="keyword">return</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_pair</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">b</span><span class="special">);</span> 252 <span class="special">}</span> 253<span class="special">};</span> 254</pre> 255<p> 256 Notice that if the continuant is subtracted from the <span class="emphasis"><em>b</em></span> 257 terms, as is the case here, then all the <span class="emphasis"><em>a</em></span> terms returned 258 by the generator will be negative. The tangent function can now be evaluated 259 using: 260 </p> 261<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> 262<span class="identifier">T</span> <span class="identifier">tan</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">a</span><span class="special">)</span> 263<span class="special">{</span> 264 <span class="identifier">tan_fraction</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span> <span class="identifier">fract</span><span class="special">(</span><span class="identifier">a</span><span class="special">);</span> 265 <span class="keyword">return</span> <span class="identifier">a</span> <span class="special">/</span> <span class="identifier">continued_fraction_b</span><span class="special">(</span><span class="identifier">fract</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">epsilon</span><span class="special">());</span> 266<span class="special">}</span> 267</pre> 268<p> 269 Notice that this time we're using the "_b" suffixed version to 270 evaluate the fraction: we're removing the leading <span class="emphasis"><em>a</em></span> 271 term during fraction evaluation as it's different from all the others. 272 </p> 273<p> 274 Now we'll look at a couple of complex number examples, starting with the 275 exponential integral which can be calculated via: 276 </p> 277<div class="blockquote"><blockquote class="blockquote"><p> 278 <span class="inlinemediaobject"><img src="../../../equations/expint_n_3.svg"></span> 279 280 </p></blockquote></div> 281<p> 282 So our functor looks like this: 283 </p> 284<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> 285<span class="keyword">struct</span> <span class="identifier">expint_fraction</span> 286<span class="special">{</span> 287 <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">T</span><span class="special">></span> <span class="identifier">result_type</span><span class="special">;</span> 288 <span class="identifier">expint_fraction</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">n_</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">z_</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">b</span><span class="special">(</span><span class="identifier">z_</span> <span class="special">+</span> <span class="identifier">T</span><span class="special">(</span><span class="identifier">n_</span><span class="special">)),</span> <span class="identifier">i</span><span class="special">(-</span><span class="number">1</span><span class="special">),</span> <span class="identifier">n</span><span class="special">(</span><span class="identifier">n_</span><span class="special">)</span> <span class="special">{}</span> 289 <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">T</span><span class="special">></span> <span class="keyword">operator</span><span class="special">()()</span> 290 <span class="special">{</span> 291 <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">T</span><span class="special">></span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_pair</span><span class="special">(-</span><span class="keyword">static_cast</span><span class="special"><</span><span class="identifier">T</span><span class="special">>((</span><span class="identifier">i</span> <span class="special">+</span> <span class="number">1</span><span class="special">)</span> <span class="special">*</span> <span class="special">(</span><span class="identifier">n</span> <span class="special">+</span> <span class="identifier">i</span><span class="special">)),</span> <span class="identifier">b</span><span class="special">);</span> 292 <span class="identifier">b</span> <span class="special">+=</span> <span class="number">2</span><span class="special">;</span> 293 <span class="special">++</span><span class="identifier">i</span><span class="special">;</span> 294 <span class="keyword">return</span> <span class="identifier">result</span><span class="special">;</span> 295 <span class="special">}</span> 296<span class="keyword">private</span><span class="special">:</span> 297 <span class="identifier">T</span> <span class="identifier">b</span><span class="special">;</span> 298 <span class="keyword">int</span> <span class="identifier">i</span><span class="special">;</span> 299 <span class="keyword">unsigned</span> <span class="identifier">n</span><span class="special">;</span> 300<span class="special">};</span> 301</pre> 302<p> 303 We can finish the example by wrapping everything up in a function: 304 </p> 305<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> 306<span class="keyword">inline</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span> <span class="identifier">expint_as_fraction</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">n</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">z</span><span class="special">)</span> 307<span class="special">{</span> 308 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">uintmax_t</span> <span class="identifier">max_iter</span> <span class="special">=</span> <span class="number">1000</span><span class="special">;</span> 309 <span class="identifier">expint_fraction</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span> <span class="special">></span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">n</span><span class="special">,</span> <span class="identifier">z</span><span class="special">);</span> 310 <span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span> <span class="identifier">result</span> <span class="special">=</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><span class="identifier">continued_fraction_b</span><span class="special">(</span> 311 <span class="identifier">f</span><span class="special">,</span> 312 <span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special"><</span><span class="identifier">T</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">epsilon</span><span class="special">()),</span> 313 <span class="identifier">max_iter</span><span class="special">);</span> 314 <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">exp</span><span class="special">(-</span><span class="identifier">z</span><span class="special">)</span> <span class="special">/</span> <span class="identifier">result</span><span class="special">;</span> 315 <span class="keyword">return</span> <span class="identifier">result</span><span class="special">;</span> 316<span class="special">}</span> 317</pre> 318<p> 319 Notice how the termination condition is still expressed as a complex number, 320 albeit one with zero imaginary part. 321 </p> 322<p> 323 Our final example will use <code class="literal">continued_fraction_a</code>, in fact 324 there is only one special function in our code which uses that variant, and 325 it's the upper incomplete gamma function (Q), which can be calculated via: 326 </p> 327<div class="blockquote"><blockquote class="blockquote"><p> 328 <span class="inlinemediaobject"><img src="../../../equations/igamma9.svg"></span> 329 330 </p></blockquote></div> 331<p> 332 In this case the first couple of terms are different from the rest, so our 333 fraction will start with the first "regular" a term: 334 </p> 335<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> 336<span class="keyword">struct</span> <span class="identifier">upper_incomplete_gamma_fract</span> 337<span class="special">{</span> 338<span class="keyword">private</span><span class="special">:</span> 339 <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">T</span><span class="special">::</span><span class="identifier">value_type</span> <span class="identifier">scalar_type</span><span class="special">;</span> 340 <span class="identifier">T</span> <span class="identifier">z</span><span class="special">,</span> <span class="identifier">a</span><span class="special">;</span> 341 <span class="keyword">int</span> <span class="identifier">k</span><span class="special">;</span> 342<span class="keyword">public</span><span class="special">:</span> 343 <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">T</span><span class="special">></span> <span class="identifier">result_type</span><span class="special">;</span> 344 345 <span class="identifier">upper_incomplete_gamma_fract</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">a1</span><span class="special">,</span> <span class="identifier">T</span> <span class="identifier">z1</span><span class="special">)</span> 346 <span class="special">:</span> <span class="identifier">z</span><span class="special">(</span><span class="identifier">z1</span> <span class="special">-</span> <span class="identifier">a1</span> <span class="special">+</span> <span class="identifier">scalar_type</span><span class="special">(</span><span class="number">1</span><span class="special">)),</span> <span class="identifier">a</span><span class="special">(</span><span class="identifier">a1</span><span class="special">),</span> <span class="identifier">k</span><span class="special">(</span><span class="number">0</span><span class="special">)</span> 347 <span class="special">{</span> 348 <span class="special">}</span> 349 350 <span class="identifier">result_type</span> <span class="keyword">operator</span><span class="special">()()</span> 351 <span class="special">{</span> 352 <span class="special">++</span><span class="identifier">k</span><span class="special">;</span> 353 <span class="identifier">z</span> <span class="special">+=</span> <span class="identifier">scalar_type</span><span class="special">(</span><span class="number">2</span><span class="special">);</span> 354 <span class="keyword">return</span> <span class="identifier">result_type</span><span class="special">(</span><span class="identifier">scalar_type</span><span class="special">(</span><span class="identifier">k</span><span class="special">)</span> <span class="special">*</span> <span class="special">(</span><span class="identifier">a</span> <span class="special">-</span> <span class="identifier">scalar_type</span><span class="special">(</span><span class="identifier">k</span><span class="special">)),</span> <span class="identifier">z</span><span class="special">);</span> 355 <span class="special">}</span> 356<span class="special">};</span> 357</pre> 358<p> 359 So now we can implement Q, this time using <code class="literal">continued_fraction_a</code>: 360 </p> 361<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> 362<span class="keyword">inline</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span> <span class="identifier">gamma_Q_as_fraction</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special"><</span><span class="identifier">T</span><span class="special">>&</span> <span class="identifier">a</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special"><</span><span class="identifier">T</span><span class="special">>&</span> <span class="identifier">z</span><span class="special">)</span> 363<span class="special">{</span> 364 <span class="identifier">upper_incomplete_gamma_fract</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span> <span class="special">></span> <span class="identifier">f</span><span class="special">(</span><span class="identifier">a</span><span class="special">,</span> <span class="identifier">z</span><span class="special">);</span> 365 <span class="identifier">std</span><span class="special">::</span><span class="identifier">complex</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span> <span class="identifier">eps</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">epsilon</span><span class="special">());</span> 366 <span class="keyword">return</span> <span class="identifier">pow</span><span class="special">(</span><span class="identifier">z</span><span class="special">,</span> <span class="identifier">a</span><span class="special">)</span> <span class="special">/</span> <span class="special">(</span><span class="identifier">exp</span><span class="special">(</span><span class="identifier">z</span><span class="special">)</span> <span class="special">*(</span><span class="identifier">z</span> <span class="special">-</span> <span class="identifier">a</span> <span class="special">+</span> <span class="identifier">T</span><span class="special">(</span><span class="number">1</span><span class="special">)</span> <span class="special">+</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><span class="identifier">continued_fraction_a</span><span class="special">(</span><span class="identifier">f</span><span class="special">,</span> <span class="identifier">eps</span><span class="special">)));</span> 367<span class="special">}</span> 368</pre> 369</div> 370<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 371<td align="left"></td> 372<td align="right"><div class="copyright-footer">Copyright © 2006-2019 Nikhar 373 Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, 374 Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan 375 Råde, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, 376 Daryle Walker and Xiaogang Zhang<p> 377 Distributed under the Boost Software License, Version 1.0. (See accompanying 378 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>) 379 </p> 380</div></td> 381</tr></table> 382<hr> 383<div class="spirit-nav"> 384<a accesskey="p" href="series_evaluation.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../internals.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="recurrence.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 385</div> 386</body> 387</html> 388