File: group__utilities.html

package info (click to toggle)
quantlib-refman-html 0.9.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 60,592 kB
  • ctags: 7,595
  • sloc: makefile: 30
file content (112 lines) | stat: -rw-r--r-- 7,297 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="robots" content="none">
<title>QuantLib: Utilities</title>
<link rel="stylesheet" href="quantlib.css" type="text/css">
<link rel="stylesheet" href="print.css" type="text/css" media="print">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="icon" href="favicon.ico" type="image/x-icon">
</head>
<body>

<div id="container">
<div id="header">
<img class="titleimage"
 src="QL-title.jpg" width="212" height="47" border="0"
 alt="QuantLib">
<br>
<h3 class="subtitle">A free/open-source library for quantitative finance</h3>
</div>
<div id="menu">

<h3 class="navbartitle">Version 0.9.0</h3>

<hr>

<h3 class="navbartitle">Getting started</h3>
<ul class="navbarlist">
<li class="navlink"><a href="index.html">Introduction</a></li>
<li class="navlink"><a href="overview.html">Project overview</a></li>
<li class="navlink"><a href="where.html">Where to get QuantLib</a></li>
<li class="navlink"><a href="install.html">Installation</a></li>
<li class="navlink"><a href="config.html">Configuration</a></li>
<li class="navlink"><a href="usage.html">Usage</a></li>
<li class="navlink"><a href="history.html">Version history</a></li>
<li class="navlink"><a href="resources.html">Additional resources</a></li>
<li class="navlink"><a href="group.html">The QuantLib group</a></li>
<li class="navlink"><a href="license.html">Copyright and license</a></li>
</ul>

<hr>

<h3 class="navbartitle">Reference manual</h3>
<ul class="navbarlist">
<li class="navlink"><a href="modules.html">Modules</a></li>
<li class="navlink"><a href="hierarchy.html">Class Hierarchy</a></li>
<li class="navlink"><a href="annotated.html">Compound List</a></li>
<li class="navlink"><a href="files.html">File List</a></li>
<li class="navlink"><a href="functions.html">Compound Members</a></li>
<li class="navlink"><a href="globals.html">File Members</a></li>
<li class="navlink"><a href="bug.html">Known Bugs</a></li>
<li class="navlink"><a href="caveats.html">Caveats</a></li>
<li class="navlink"><a href="test.html">Test Suite</a></li>
<li class="navlink"><a href="examples.html">Examples</a></li>
</ul>
</div>

<div id="content">
<!--Doxygen-generated content-->

<!-- Generated by Doxygen 1.5.4 -->
<h1>Utilities</h1>Iterators are meant to build a sequence on the fly from one or more other sequences, without having to allocate place for storing it. A couple of examples: suppose we have a function which calculates the average of a sequence, and that for genericity we have implemented it as a template function which takes the beginning and the end of the sequence, so that its declaration is:<p>
<div class="fragment"><pre class="fragment">    <span class="keyword">template</span> &lt;<span class="keyword">class</span> Iterator&gt;
    <span class="keyword">typename</span> Iterator::value_type
    average(<span class="keyword">const</span> Iterator&amp; begin, <span class="keyword">const</span> Iterator&amp; end)
</pre></div><p>
This kind of genericity allows one to use the same function to calculate the average of a std::vector, a std::list, a QuantLib::History, any other container, of a subset of any of the former.<p>
Now let's say we have two sequences of numbers, and we want to calculate the average of their products. One approach could be to store the products in another sequence, and to calculate the average of the latter, as in:<p>
<div class="fragment"><pre class="fragment">    <span class="comment">// we have sequence1 and sequence2 and assume equal size:</span>
    <span class="comment">// first we store their product in a vector...</span>
    std::vector&lt;double&gt; products;
    std::transform(sequence1.begin(),sequence1.end(), <span class="comment">// first sequence</span>
                   sequence2.begin(),                 <span class="comment">// second sequence</span>
                   std::back_inserter(products),      <span class="comment">// output</span>
                   std::multiplies&lt;double&gt;());        <span class="comment">// operation to perform</span>
    <span class="comment">// ...then we calculate the average</span>
    <span class="keywordtype">double</span> result = average(products.begin(),products.end());
</pre></div><p>
The above works, however, it might be not particularly efficient since we have to allocate the product vector, quite possibly just to throw it away when the calculation is done.<p>
QuantLib::coupling_iterator allows us to do the same thing without allocating the extra vector: what we do is simply:<p>
<div class="fragment"><pre class="fragment">    <span class="comment">// we have sequence1 and sequence2 and assume equal size:</span>
    <span class="keywordtype">double</span> result = average(
        make_coupling_iterator(sequence1.begin(),
                               sequence2.begin(),
                               std::multiplies&lt;double()),
        make_coupling_iterator(sequence1.end(),
                               sequence2.end(),
                               std::multiplies&lt;double()));
</pre></div><p>
The call to make_coupling_iterator creates an iterator which is really a reference to the two iterators and the operation we passed. Dereferencing such iterator returns the result of applying such operation to the values pointed to by the two contained iterators. Advancing the coupling iterator advances the two underlying ones. One can see how iterating on such iterator generates the products one by one so that they can be processed by <code>average()</code>, but does not need allocating memory for storing the results. The product sequence is generated on the fly.<p>
The other iterators share the same principle but have different functionalities:<ul>
<li>combining_iterator is the same as coupling_iterator, but works on <img class="formulaInl" alt="$ N $" src="form_36.png"> sequences while the latter works on 2;</li><li>filtering_iterator generates the elements of a given sequence which satisfy a given predicate, i.e., it takes a sequence <img class="formulaInl" alt="$ [x_0,x_1,\dots] $" src="form_37.png"> and a predicate <img class="formulaInl" alt="$ p $" src="form_38.png"> and generates the sequence of those <img class="formulaInl" alt="$ x_i $" src="form_39.png"> for which <img class="formulaInl" alt="$ p(x_i) $" src="form_40.png"> returns <code>true</code>;</li><li>processing_iterator takes a sequence <img class="formulaInl" alt="$ [x_0,x_1,\dots] $" src="form_37.png"> and a function <img class="formulaInl" alt="$ f $" src="form_41.png"> and generates the sequence <img class="formulaInl" alt="$ [f(x_0),f(x_1),\dots] $" src="form_42.png">;</li><li>stepping_iterator takes a sequence <img class="formulaInl" alt="$ [x_0,x_1,\dots] $" src="form_37.png"> and a step <img class="formulaInl" alt="$ m $" src="form_43.png"> and generates the sequence <img class="formulaInl" alt="$ [x_0,x_m,x_{2m},\dots] $" src="form_44.png"> </li></ul>

<p>
<table border="0" cellpadding="0" cellspacing="0">
<tr><td></td></tr>
</table>

</div>

<div class="footer">
<div class="endmatter">
Documentation generated by
<a href="http://www.doxygen.org">Doxygen</a> 1.5.4
</div>
</div>

</div>

</body>
</html>