是否有可能使用提升蓄能器引导? [英] Is it possible to use boost accumulators with vectors?

查看:202
本文介绍了是否有可能使用提升蓄能器引导?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用升压蓄电池来计算一个变量,是一个矢量的统计数据。有一个简单的方法来做到这一点。我认为这是不可能的使用最愚蠢的事情:

I wanted to use boost accumulators to calculate statistics of a variable that is a vector. Is there a simple way to do this. I think it's not possible to use the dumbest thing:

  using namespace boost::accumulators;
  //stuff...

  accumulator_set<vector<double>, stats<tag::mean> > acc;
  vector<double> some_vetor;
  //stuff 
  some_vector = doStuff();
  acc(some_vector);

也许这是显而易见的,但我想反正。 :P

maybe this is obvious, but I tried anyway. :P

我想要的是有一个累加器,将计算一个载体,其是许多矢量的分量的平均值。有一个简单的方法?

What I wanted was to have an accumulator that would calculate a vector which is the mean of the components of many vectors. Is there an easy way out?

编辑:

我不知道,如果我是彻底说清楚。我不希望这样的:

I don't know if I was thoroughly clear. I don't want this:

 for_each(vec.begin(), vec.end(),acc); 

此将计算给定矢量的项的平均值。我需要的是不同的。我有一个函数,将吐向量:

This would calculate the mean of the entries of a given vector. What I need is different. I have a function that will spit vectors:

 vector<double> doSomething(); 
 // this is a monte carlo simulation;

和我需要运行很多次,并计算出的矢量是指那些载体的:

And I need to run this many times and calculate the vectorial mean of those vectors:

  for(int i = 0; i < numberOfMCSteps; i++){
  vec = doSomething();
  acc(vec);
  }
  cout << mean(acc);

和我想的意思(ACC)是一个矢量本身,其条目由[i]将累积向量的项[I]的装置。

And I want mean(acc) to be a vector itself, whose entry [i] would be the means of the entries [i] of the accumulated vectors.

有专门关于这升压的文档暗示,但没有明确。而且我有点哑。 :P

Theres a hint about this in the docs of Boost, but nothing explicit. And I'm a bit dumb. :P

推荐答案

我看着你的问题了一下,似乎我已经Boost.Accumulators为的std ::支持向量。以下是我能够在<一个找到href=\"http://www.boost.org/doc/libs/1_45_0/doc/html/accumulators/user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework.extending_the_accumulators_framework.operators_ex\">a用户指南的部分:

I've looked into your question a bit, and it seems to me that Boost.Accumulators already provides support for std::vector. Here is what I could find in a section of the user's guide :

又如,其中数字
  运营商子库是很有用的是
  当类型不限定
  使用它所需的运算符重载
  对于一些统计计算。
  例如,的std ::矢量&lt;&GT; 不超载的算术运算符,但
  它可能是使用有用的std ::矢量&lt;&GT;
  作为样品或变动内容类型。该
  数值运算符子库定义
  必要的运算符重载的
  在提高::数字::运营商
  名字空间,它被带进范围
  通过用该累加器框架
  使用指令。

Another example where the Numeric Operators Sub-Library is useful is when a type does not define the operator overloads required to use it for some statistical calculations. For instance, std::vector<> does not overload any arithmetic operators, yet it may be useful to use std::vector<> as a sample or variate type. The Numeric Operators Sub-Library defines the necessary operator overloads in the boost::numeric::operators namespace, which is brought into scope by the Accumulators Framework with a using directive.

事实上,经过验证,文件升压/累加器/数字/功能/ vector.hpp 确实的包含必要的运营商的天真解决方案的工作。

Indeed, after verification, the file boost/accumulators/numeric/functional/vector.hpp does contain the necessary operators for the 'naive' solution to work.

我相信你应该尝试:


  • 既包括

    • 升压/累加器/数字/功能/ vector.hpp 之前的任何其他蓄能器头

    • 升压/累加器/数字/ functional.hpp 在定义 BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT

    • Including either
      • boost/accumulators/numeric/functional/vector.hpp before any other accumulators header
      • boost/accumulators/numeric/functional.hpp while defining BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT

      有只有一个最后的细节留给:执行将在运行时打破,因为最初的累积值是缺省构造的,并试图尺寸的 N 的向量添加到空载体时会产生断言。对于这一点,似乎应该初始化累加器(其中<青霉> N 的是在你的载体的元素数):

      There's only one last detail left : execution will break at runtime because the initial accumulated value is default-constructed, and an assertion will occur when trying to add a vector of size n to an empty vector. For this, it seems you should initialize the accumulator with (where n is the number of elements in your vector) :

      accumulator_set<std::vector<double>, stats<tag::mean> > acc(std::vector<double>(n));
      

      我尝试以下code,的意思是给我大小为2的的std ::矢量

      int main()
      {
          accumulator_set<std::vector<double>, stats<tag::mean> > acc(std::vector<double>(2));
      
          const std::vector<double> v1 = boost::assign::list_of(1.)(2.);
          const std::vector<double> v2 = boost::assign::list_of(2.)(3.);
          const std::vector<double> v3 = boost::assign::list_of(3.)(4.);
          acc(v1);
          acc(v2);
          acc(v3);
      
          const std::vector<double> &meanVector = mean(acc);
      }
      

      我相信这是你想要的吗?

      I believe this is what you wanted ?

      这篇关于是否有可能使用提升蓄能器引导?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆