Difference between revisions of "CPP/Boost/Histogram"
From ProgrammingExamples
< CPP
Daviddoria (Talk | contribs) (Created page with '==Histogram.cpp== <source lang="cpp"> #include <vector> #include <algorithm> #include <iostream> #include <boost/accumulators/accumulators.hpp> #include <boost/accumulators/stat…') |
Daviddoria (Talk | contribs) |
||
Line 1: | Line 1: | ||
+ | Warning: this example has not been checked. | ||
+ | |||
==Histogram.cpp== | ==Histogram.cpp== | ||
<source lang="cpp"> | <source lang="cpp"> | ||
Line 54: | Line 56: | ||
return 0; | return 0; | ||
} | } | ||
+ | |||
+ | </source> | ||
+ | |||
+ | ==CMakeLists.txt== | ||
+ | <source lang="cmake"> | ||
+ | cmake_minimum_required(VERSION 2.6) | ||
+ | |||
+ | Project(Histogram) | ||
+ | |||
+ | #FIND_PACKAGE(Boost 1.38 COMPONENTS program_options required) | ||
+ | |||
+ | #INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${Boost_INCLUDE_DIRS}) | ||
+ | #LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS}) | ||
+ | |||
+ | ADD_EXECUTABLE(Histogram Histogram.cpp) | ||
</source> | </source> |
Latest revision as of 11:36, 23 January 2011
Warning: this example has not been checked.
Histogram.cpp
#include <vector> #include <algorithm> #include <iostream> #include <boost/accumulators/accumulators.hpp> #include <boost/accumulators/statistics/density.hpp> #include <boost/accumulators/statistics/stats.hpp> using namespace boost; using namespace boost::accumulators; typedef accumulator_set<double, features<tag::density> > acc; typedef iterator_range<std::vector<std::pair<double, double> >::iterator > histogram_type; template <typename T> class data_filler { public: data_filler(){} T operator()() { return rand()/(T)RAND_MAX; } }; int main(int argc, char** argv) { //create some random data std::vector<double> data(100); std::generate(data.begin(), data.end(), data_filler<double>()); int c = data.size();//cache size for histogramm. //create an accumulator acc myAccumulator( tag::density::num_bins = 20, tag::density::cache_size = 10); //fill accumulator for (int j = 0; j < c; ++j) { myAccumulator(data[j]); } histogram_type hist = density(myAccumulator); double total = 0.0; for( int i = 0; i < hist.size(); i++ ) { std::cout << "Bin lower bound: " << hist[i].first << ", Value: " << hist[i].second << std::endl; total += hist[i].second; } std::cout << "Total: " << total << std::endl; //should be 1 (and it is) return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) Project(Histogram) #FIND_PACKAGE(Boost 1.38 COMPONENTS program_options required) #INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${Boost_INCLUDE_DIRS}) #LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS}) ADD_EXECUTABLE(Histogram Histogram.cpp)