innerProduct method
- Iterable<
T> other, - T init, {
- T op1(
- T,
- T
- T op2(
- T,
- T
Computes the inner product (dot product) of two ranges.
Similar to std::inner_product. The default sums the products of corresponding elements.
op1 accumulates the results (default +), and op2 computes the product (default *).
Implementation
T innerProduct(
Iterable<T> other,
T init, {
T Function(T, T)? op1,
T Function(T, T)? op2,
}) {
op1 ??= (T a, T b) => ((a as dynamic) + (b as dynamic)) as T;
op2 ??= (T a, T b) => ((a as dynamic) * (b as dynamic)) as T;
var it1 = iterator;
var it2 = other.iterator;
T result = init;
while (it1.moveNext() && it2.moveNext()) {
result = op1(result, op2(it1.current, it2.current));
}
return result;
}