• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Function template sum</title>
5<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../../index.html" title="Chapter 1. Boost.Histogram">
8<link rel="up" href="../../../histogram/reference.html#header.boost.histogram.algorithm.sum_hpp" title="Header &lt;boost/histogram/algorithm/sum.hpp&gt;">
9<link rel="prev" href="slice_an_idm45503931040864.html" title="Function slice_and_rebin">
10<link rel="next" href="../axis/boolean.html" title="Class template boolean">
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="../../../../../../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="slice_an_idm45503931040864.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../../histogram/reference.html#header.boost.histogram.algorithm.sum_hpp"><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="../axis/boolean.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="refentry">
26<a name="boost.histogram.algorithm.sum"></a><div class="titlepage"></div>
27<div class="refnamediv">
28<h2><span class="refentrytitle">Function template sum</span></h2>
29<p>boost::histogram::algorithm::sum — Compute the sum over all histogram cells (underflow/overflow included by default). </p>
30</div>
31<h2 xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv-title">Synopsis</h2>
32<div xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv"><pre class="synopsis"><span class="comment">// In header: &lt;<a class="link" href="../../../histogram/reference.html#header.boost.histogram.algorithm.sum_hpp" title="Header &lt;boost/histogram/algorithm/sum.hpp&gt;">boost/histogram/algorithm/sum.hpp</a>&gt;
33
34</span>
35<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> A<span class="special">,</span> <span class="keyword">typename</span> S<span class="special">&gt;</span>
36  <span class="keyword">auto</span> <span class="identifier">sum</span><span class="special">(</span><span class="keyword">const</span> <a class="link" href="../histogram.html" title="Class template histogram">histogram</a><span class="special">&lt;</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">S</span> <span class="special">&gt;</span> <span class="special">&amp;</span> hist<span class="special">,</span> <span class="keyword">const</span> <span class="identifier">coverage</span> cov <span class="special">=</span> <span class="identifier">coverage</span><span class="special">::</span><span class="identifier">all</span><span class="special">)</span><span class="special">;</span></pre></div>
37<div class="refsect1">
38<a name="idm45113194757408"></a><h2>Description</h2>
39<p>The implementation favors accuracy and protection against overflow over speed. If the value type of the histogram is an integral or floating point type, accumulators::sum&lt;double&gt; is used to compute the sum, else the original value type is used. Compilation fails, if the value type does not support operator+=. The return type is double if the value type of the histogram is integral or floating point, and the original value type otherwise.</p>
40<p>If you need a different trade-off, you can write your own loop or use <code class="computeroutput">std::accumulate</code>:</p>
41<pre class="programlisting"><span class="comment">// iterate over all bins</span>
42<span class="keyword">auto</span> <span class="identifier">sum_all</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">accumulate</span><span class="special">(</span><span class="identifier">hist</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(</span><span class="special">)</span><span class="special">,</span> <span class="identifier">hist</span><span class="special">.</span><span class="identifier">end</span><span class="special">(</span><span class="special">)</span><span class="special">,</span> <span class="number">0</span><span class="special">.</span><span class="number">0</span><span class="special">)</span><span class="special">;</span>
43
44<span class="comment">// skip underflow/overflow bins</span>
45<span class="keyword">double</span> <span class="identifier">sum</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
46<span class="keyword">for</span> <span class="special">(</span><span class="keyword">auto</span><span class="special">&amp;&amp;</span> <span class="identifier">x</span> <span class="special">:</span> <span class="identifier">indexed</span><span class="special">(</span><span class="identifier">hist</span><span class="special">)</span><span class="special">)</span>
47  <span class="identifier">sum</span> <span class="special">+=</span> <span class="special">*</span><span class="identifier">x</span><span class="special">;</span> <span class="comment">// dereference accessor</span>
48
49<span class="comment">// or:</span>
50<span class="comment">// auto ind = boost::histogram::indexed(hist);</span>
51<span class="comment">// auto sum = std::accumulate(ind.begin(), ind.end(), 0.0);</span>
52</pre>
53<p>
54
55</p>
56<div class="variablelist"><table border="0" class="variablelist compact">
57<colgroup>
58<col align="left" valign="top">
59<col>
60</colgroup>
61<tbody>
62<tr>
63<td><p><span class="term">Parameters:</span></p></td>
64<td><div class="variablelist"><table border="0" class="variablelist compact">
65<colgroup>
66<col align="left" valign="top">
67<col>
68</colgroup>
69<tbody>
70<tr>
71<td><p><span class="term"><code class="computeroutput">cov</code></span></p></td>
72<td><p>Iterate over all or only inner bins (optional, default: all). </p></td>
73</tr>
74<tr>
75<td><p><span class="term"><code class="computeroutput">hist</code></span></p></td>
76<td><p>Const reference to the histogram. </p></td>
77</tr>
78</tbody>
79</table></div></td>
80</tr>
81<tr>
82<td><p><span class="term">Returns:</span></p></td>
83<td><p>accumulator type or double</p></td>
84</tr>
85</tbody>
86</table></div>
87</div>
88</div>
89<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
90<td align="left"></td>
91<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Hans
92      Dembinski<p>
93        Distributed under the Boost Software License, Version 1.0. (See accompanying
94        file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
95      </p>
96</div></td>
97</tr></table>
98<hr>
99<div class="spirit-nav">
100<a accesskey="p" href="slice_an_idm45503931040864.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../../histogram/reference.html#header.boost.histogram.algorithm.sum_hpp"><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="../axis/boolean.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
101</div>
102</body>
103</html>
104