tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
inner_product.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3#ifndef TETL_NUMERIC_INNER_PRODUCT_HPP
4#define TETL_NUMERIC_INNER_PRODUCT_HPP
5
6#include <etl/_utility/move.hpp>
7
8namespace etl {
9
10/// \brief Computes inner product (i.e. sum of products) or performs ordered
11/// map/reduce operation on the range [first1, last1) and the range beginning at
12/// first2.
13/// \ingroup numeric
14template <typename InputIt1, typename InputIt2, typename T>
15[[nodiscard]] constexpr auto inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init) -> T
16{
17 for (; first1 != last1; ++first1, ++first2) {
18 init = etl::move(init) + *first1 * *first2;
19 }
20 return init;
21}
22
23/// \ingroup numeric
24template <typename InputIt1, typename InputIt2, typename T, typename BinaryOperation1, typename BinaryOperation2>
25[[nodiscard]] constexpr auto
26inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init, BinaryOperation1 op1, BinaryOperation2 op2) -> T
27{
28 for (; first1 != last1; ++first1, ++first2) {
29 init = op1(etl::move(init), op2(*first1, *first2));
30 }
31 return init;
32}
33
34} // namespace etl
35
36#endif // TETL_NUMERIC_INNER_PRODUCT_HPP
constexpr auto inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init, BinaryOperation1 op1, BinaryOperation2 op2) -> T
Definition inner_product.hpp:26
constexpr auto inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init) -> T
Computes inner product (i.e. sum of products) or performs ordered map/reduce operation on the range [...
Definition inner_product.hpp:15
Definition adjacent_find.hpp:9