在C ++中有类似numpy.logspace的东西吗? [英] Is there something like numpy.logspace in C++?
本文介绍了在C ++中有类似numpy.logspace的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
正如标题所说,我寻找一些生成日志空间值的函数,就像numpy.logspace,但是对于python。和想法?
As the title says, im looking for some function that generates log-spaces values, just like numpy.logspace does, but for python. And ideas?
推荐答案
标准库中没有这样的函数,但是,你可以轻松地编写自己的。由于C ++和Python的不同性质,函数不会是相同的。我建议使用generator-style函数对象:
There is no such function in the standard library, however, you can easily write your own. Due to the different nature of C++ and Python, the function isn't going to be identical. I'd recommend using generator-style function object:
template<typename T>
class Logspace {
private:
T curValue, base;
public:
Logspace(T first, T base) : curValue(first), base(base) {}
T operator()() {
T retval = curValue;
curValue *= base;
return retval;
}
};
使用示例(40个值,以2开始,以1开头):
Example usage (40 values with base of 2 starting with 1):
std::vector<double> vals;
std::generate_n(std::back_inserter(vals), 40, Logspace<double>(1,2));
要注释的示例解决方案:
Example solution to comment:
std::vector<double> pyLogspace(double start, double stop, int num = 50, double base = 10) {
double realStart = pow(base, start);
double realBase = pow(base, (stop-start)/num);
std::vector<double> retval;
retval.reserve(num);
std::generate_n(std::back_inserter(retval), num, Logspace<double>(realStart,realBase));
return retval;
}
generate_while的示例实现
template<typename Value, typename OutputIt, typename Condition, typename Generator>
void generate_while(OutputIt output, Condition cond, Generator g) {
Value val;
while(cond(val = g())) {
*output++ = val;
}
}
这篇关于在C ++中有类似numpy.logspace的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文