我有一个大矩阵,其数值在数量级上变化很大.为了尽可能精确地计算总和,我的方法是将ndarray重新整形为一维数组,对其进行排序,然后将其添加起来,从最小的条目开始.有没有更好/更有效的方法来做到这一点?
我想编写一个'评估'函数,它接受一个带有整数的未指定返回类型的函数作为输入,并使用一个整数来调用该函数.
我想出的是以下内容:
#include <functional>
template<typename T>
T eval(function<T(int)> f, int x) {
return f(x);
}
Run Code Online (Sandbox Code Playgroud)
假设我有一个auto func = [] (int x) -> long { return (long)x * x; }我想用上面的函数评估的东西.之前我使用模板函数的方式是简单地调用它,就像我任何其他函数一样,让编译器推断出类型.
但是,这不适用于此eval功能.eval<long>(func, 5)编译并正常工作,但eval(func, 5)不会:
Aufgaben10.5.cpp:23:25: error: no matching function for call to 'eval(main()::__lambda0&, int)'
cout << eval(func, 5) << endl;
^
Aufgaben10.5.cpp:23:25: note: candidate is:
Aufgaben10.5.cpp:8:3: note: template<class T> T eval(std::function<T(int)>, int)
T eval(function<T(int)> f, int x) {
^
Aufgaben10.5.cpp:8:3: note: template argument deduction/substitution …Run Code Online (Sandbox Code Playgroud)