Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:32:53

0001 ///////////////////////////////////////////////////////////////////////////////
0002 // weighted_sum_kahan.hpp
0003 //
0004 //  Copyright 2011 Simon West. Distributed under the Boost
0005 //  Software License, Version 1.0. (See accompanying file
0006 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 
0008 #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SUM_KAHAN_HPP_EAN_11_05_2011
0009 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SUM_KAHAN_HPP_EAN_11_05_2011
0010 
0011 #include <boost/mpl/placeholders.hpp>
0012 #include <boost/accumulators/framework/accumulator_base.hpp>
0013 #include <boost/accumulators/framework/extractor.hpp>
0014 #include <boost/accumulators/numeric/functional.hpp>
0015 #include <boost/accumulators/framework/parameters/sample.hpp>
0016 #include <boost/accumulators/framework/parameters/weight.hpp>
0017 #include <boost/accumulators/framework/accumulators/external_accumulator.hpp>
0018 #include <boost/accumulators/framework/depends_on.hpp>
0019 #include <boost/accumulators/statistics_fwd.hpp>
0020 #include <boost/accumulators/statistics/weighted_sum.hpp>
0021 #include <boost/numeric/conversion/cast.hpp>
0022 
0023 namespace boost { namespace accumulators
0024 {
0025 
0026 namespace impl
0027 {
0028 #if _MSC_VER > 1400
0029 # pragma float_control(push)
0030 # pragma float_control(precise, on)
0031 #endif
0032 
0033     ///////////////////////////////////////////////////////////////////////////////
0034     // weighted_sum_kahan_impl
0035     template<typename Sample, typename Weight, typename Tag>
0036     struct weighted_sum_kahan_impl
0037       : accumulator_base
0038     {
0039         typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
0040 
0041         // for boost::result_of
0042         typedef weighted_sample result_type;
0043 
0044         template<typename Args>
0045         weighted_sum_kahan_impl(Args const &args)
0046           : weighted_sum_(
0047                 args[parameter::keyword<Tag>::get() | Sample()] * numeric::one<Weight>::value),
0048                 compensation(boost::numeric_cast<weighted_sample>(0.0))
0049         {
0050         }
0051 
0052         template<typename Args>
0053         void 
0054 #if BOOST_ACCUMULATORS_GCC_VERSION > 40305
0055         __attribute__((__optimize__("no-associative-math")))
0056 #endif
0057         operator ()(Args const &args)
0058         {
0059             const weighted_sample myTmp1 = args[parameter::keyword<Tag>::get()] * args[weight] - this->compensation;
0060             const weighted_sample myTmp2 = this->weighted_sum_ + myTmp1;
0061             this->compensation = (myTmp2 - this->weighted_sum_) - myTmp1;
0062             this->weighted_sum_ = myTmp2;
0063 
0064         }
0065 
0066         result_type result(dont_care) const
0067         {
0068             return this->weighted_sum_;
0069         }
0070 
0071         // make this accumulator serializeable
0072         template<class Archive>
0073         void serialize(Archive & ar, const unsigned int file_version)
0074         {
0075             ar & weighted_sum_;
0076             ar & compensation;
0077         }
0078 
0079     private:
0080         weighted_sample weighted_sum_;
0081         weighted_sample compensation;
0082     };
0083 
0084 #if _MSC_VER > 1400
0085 # pragma float_control(pop)
0086 #endif
0087 
0088 } // namespace impl
0089 
0090 ///////////////////////////////////////////////////////////////////////////////
0091 // tag::weighted_sum_kahan
0092 // tag::weighted_sum_of_variates_kahan
0093 //
0094 namespace tag
0095 {
0096     struct weighted_sum_kahan
0097       : depends_on<>
0098     {
0099         /// INTERNAL ONLY
0100         ///
0101         typedef accumulators::impl::weighted_sum_kahan_impl<mpl::_1, mpl::_2, tag::sample> impl;
0102     };
0103 
0104     template<typename VariateType, typename VariateTag>
0105     struct weighted_sum_of_variates_kahan
0106       : depends_on<>
0107     {
0108         /// INTERNAL ONLY
0109         ///
0110         typedef accumulators::impl::weighted_sum_kahan_impl<VariateType, mpl::_2, VariateTag> impl;
0111     };
0112 
0113 }
0114 
0115 ///////////////////////////////////////////////////////////////////////////////
0116 // extract::weighted_sum_kahan
0117 // extract::weighted_sum_of_variates_kahan
0118 //
0119 namespace extract
0120 {
0121     extractor<tag::weighted_sum_kahan> const weighted_sum_kahan = {};
0122     extractor<tag::abstract_weighted_sum_of_variates> const weighted_sum_of_variates_kahan = {};
0123 
0124     BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum_kahan)
0125     BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum_of_variates_kahan)
0126 }
0127 
0128 using extract::weighted_sum_kahan;
0129 using extract::weighted_sum_of_variates_kahan;
0130 
0131 // weighted_sum(kahan) -> weighted_sum_kahan
0132 template<>
0133 struct as_feature<tag::weighted_sum(kahan)>
0134 {
0135     typedef tag::weighted_sum_kahan type;
0136 };
0137 
0138 template<typename VariateType, typename VariateTag>
0139 struct feature_of<tag::weighted_sum_of_variates_kahan<VariateType, VariateTag> >
0140   : feature_of<tag::abstract_weighted_sum_of_variates>
0141 {
0142 };
0143 
0144 }} // namespace boost::accumulators
0145 
0146 #endif