是否可以将 boost 累加器与向量一起使用?
我想使用 boost 累加器来计算向量变量的统计数据。有没有一种简单的方法可以做到这一点。我认为不可能使用最愚蠢的东西:
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
我想要的是有一个累加器来计算一个向量,该向量是许多向量分量的平均值。有简单的出路吗?
编辑:
我不知道我是否完全清楚。我不想要这个:
for_each(vec.begin(), vec.end(),acc);
这将计算给定向量的条目的平均值。我需要的是不同的。我有一个会吐出向量的函数:
vector<double> doSomething();
// this is a monte carlo simulation;
我需要运行多次并计算这些向量的向量平均值:
for(int i = 0; i < numberOfMCSteps; i++){
vec = doSomething();
acc(vec);
}
cout << mean(acc);
我希望mean(acc)本身就是一个向量,其条目[i ] 将是累积向量的条目 [i] 的平均值。
Boost 的文档中有对此的暗示,但没有明确的。而且我有点傻。 :P
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);
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?
EDIT:
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);
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.
Theres a hint about this in the docs of Boost, but nothing explicit. And I'm a bit dumb. :P
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我稍微研究了一下你的问题,在我看来,Boost.Accumulators 已经提供了对 std::vector 的支持。这是我可以在 用户指南的一部分:
验证,文件
boost/accumulators/numeric/functions/vector.hpp
确实包含“简单”解决方案所需的运算符去工作。我相信你应该尝试:
boost/accumulators/numeric/function/vector.hpp
在任何其他累加器标头之前BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT
时boost/accumulators/numeric/functional.hpp
using namespace boost::numeric::operators;
的范围。只剩下最后一个细节:执行将在运行时中断,因为初始累积值是默认构造的,并且当尝试将大小为 n 的向量添加到空向量时会发生断言。为此,似乎您应该使用以下方式初始化累加器(其中 n 是向量中的元素数量):
我尝试了以下代码,
mean
给了我一个std::vector
大小为 2:我相信这就是您想要的?
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 :Indeed, after verification, the file
boost/accumulators/numeric/functional/vector.hpp
does contain the necessary operators for the 'naive' solution to work.I believe you should try :
boost/accumulators/numeric/functional/vector.hpp
before any other accumulators headerboost/accumulators/numeric/functional.hpp
while definingBOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT
using namespace boost::numeric::operators;
.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) :
I tried the following code,
mean
gives me astd::vector
of size 2 :I believe this is what you wanted ?
我现在还没有设置它来尝试,但如果所有 boost::accumulators 需要的是正确定义的数学运算符,那么您可能可以使用不同的向量类型: http://www.boost.org/doc/libs/1_37_0/libs/numeric/ ublas/doc/vector.htm
I don't have it set up to try right now, but if all boost::accumulators need is properly defined mathematical operators, then you might be able to get away with a different vector type: http://www.boost.org/doc/libs/1_37_0/libs/numeric/ublas/doc/vector.htm
那么文档怎么样?
And what about the documentation?