#include
<numeric>
T inner_product (In first, In last, In2 first2, T startwert) T inner_product (In first, In last, In2 first2, T startwert, Binary op1, Binary2 op2)
Berechnet das innere (Skalar-) Produkt
der Folgen [first, last) und [first2,…) zuzüglich einem startwert
.
startwert + x1*y2 + x2*y2 + x3*y3 ...
bzw. ein "verallgemeinertes" Skalarprodukt
startwert op1 (x1 op2 y1) op1 (x2 op2 y2) op1 (x3 op2 y3) ...
Die Verarbeitung der Werte erfolgt nach dem Rechenschema
x1 x2 x3 x4 ... xn | y1 | y2 | y3 | y4 ...| yn \ / \ / \ / \ / \ / * * * * * | | | | | v v v v v startwert-> + -> + -> + -> + -> ... + -> Ergebnis
first | Anfang eines Bereiches |
last | Ende eines Bereiches |
first2 | Anfang des zweiten Bereiches |
op1 | äußere Operation ("verallgemeinerte" Summe) |
op2 | innere Operation ("verallgemeinertes" Produkt) |
startwert | Anfangswert der Berechnung |
Rückgabewert: akkumulierter startwert
.
#include <numeric> #include <iostream> int main() { double a[] = { 2, 4, 6, 8 }; double b[] = { 2, 3, 5, 7 }; std::cout << "Skalarprodukt = " // 4+12+30+56 = 102 << std::inner_product(a, a+4, b, 0.0) << '\n'; std::cout << "Produkt der Summen = " // 4*7*11*15 = 462 << std::inner_product(a, a+4, b, 0.0, std::multiplies{}, std::plus{}) << '\n'; }