The following assumes your data is a floating point. If you have integers with multiple dots, indicate your requirements.
The pure numerical way is, of course, to take the magazine first. In fact, partial works can easily be under / overflowed, even if the result is finite.
Idiomatic corresponding program in C ++:
#include <cmath> #include <functional> #include <numeric> double f(double x, double y) { return y * std::log(x); } template <typename I, typename J> double multi_exponentiation(I a0, I an, J b0) { return std::exp(std::inner_product(a0, an, b0, 0., std::plus<double>(), f)); } // Example program int main() { std::vector<double> a, b; ... double e = multi_exponentiation(a.begin(), a.end(), b.begin()); }
Using inner_product instead of writing a loop in itself has the advantage that as soon as you know that performance is a problem, you can replace the inner_product algorithm with the parallel_inner_product algorithm provided by a third-party library (or write one yourself).
source share