1 ///////////////////////////////////////////////////////////////////////////////
2 // weighted_sum_kahan.hpp
4 // Copyright 2011 Simon West. Distributed under the Boost
5 // Software License, Version 1.0. (See accompanying file
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SUM_KAHAN_HPP_EAN_11_05_2011
9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SUM_KAHAN_HPP_EAN_11_05_2011
11 #include <boost/mpl/placeholders.hpp>
12 #include <boost/accumulators/framework/accumulator_base.hpp>
13 #include <boost/accumulators/framework/extractor.hpp>
14 #include <boost/accumulators/numeric/functional.hpp>
15 #include <boost/accumulators/framework/parameters/sample.hpp>
16 #include <boost/accumulators/framework/parameters/weight.hpp>
17 #include <boost/accumulators/framework/accumulators/external_accumulator.hpp>
18 #include <boost/accumulators/framework/depends_on.hpp>
19 #include <boost/accumulators/statistics_fwd.hpp>
20 #include <boost/accumulators/statistics/weighted_sum.hpp>
21 #include <boost/numeric/conversion/cast.hpp>
23 namespace boost { namespace accumulators
29 # pragma float_control(push)
30 # pragma float_control(precise, on)
33 ///////////////////////////////////////////////////////////////////////////////
34 // weighted_sum_kahan_impl
35 template<typename Sample, typename Weight, typename Tag>
36 struct weighted_sum_kahan_impl
39 typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
41 // for boost::result_of
42 typedef weighted_sample result_type;
44 template<typename Args>
45 weighted_sum_kahan_impl(Args const &args)
47 args[parameter::keyword<Tag>::get() | Sample()] * numeric::one<Weight>::value),
48 compensation(boost::numeric_cast<weighted_sample>(0.0))
52 template<typename Args>
54 #if BOOST_ACCUMULATORS_GCC_VERSION > 40305
55 __attribute__((__optimize__("no-associative-math")))
57 operator ()(Args const &args)
59 const weighted_sample myTmp1 = args[parameter::keyword<Tag>::get()] * args[weight] - this->compensation;
60 const weighted_sample myTmp2 = this->weighted_sum_ + myTmp1;
61 this->compensation = (myTmp2 - this->weighted_sum_) - myTmp1;
62 this->weighted_sum_ = myTmp2;
66 result_type result(dont_care) const
68 return this->weighted_sum_;
72 weighted_sample weighted_sum_;
73 weighted_sample compensation;
77 # pragma float_control(pop)
82 ///////////////////////////////////////////////////////////////////////////////
83 // tag::weighted_sum_kahan
84 // tag::weighted_sum_of_variates_kahan
88 struct weighted_sum_kahan
93 typedef accumulators::impl::weighted_sum_kahan_impl<mpl::_1, mpl::_2, tag::sample> impl;
96 template<typename VariateType, typename VariateTag>
97 struct weighted_sum_of_variates_kahan
102 typedef accumulators::impl::weighted_sum_kahan_impl<VariateType, mpl::_2, VariateTag> impl;
107 ///////////////////////////////////////////////////////////////////////////////
108 // extract::weighted_sum_kahan
109 // extract::weighted_sum_of_variates_kahan
113 extractor<tag::weighted_sum_kahan> const weighted_sum_kahan = {};
114 extractor<tag::abstract_weighted_sum_of_variates> const weighted_sum_of_variates_kahan = {};
116 BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum_kahan)
117 BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum_of_variates_kahan)
120 using extract::weighted_sum_kahan;
121 using extract::weighted_sum_of_variates_kahan;
123 // weighted_sum(kahan) -> weighted_sum_kahan
125 struct as_feature<tag::weighted_sum(kahan)>
127 typedef tag::weighted_sum_kahan type;
130 template<typename VariateType, typename VariateTag>
131 struct feature_of<tag::weighted_sum_of_variates_kahan<VariateType, VariateTag> >
132 : feature_of<tag::abstract_weighted_sum_of_variates>
136 }} // namespace boost::accumulators