KUMI v3.1.0
Exquisite Epidote
Loading...
Searching...
No Matches

◆ inner_product()

template<concepts::product_type S1, concepts::sized_product_type< size_v< S1 > > S2, typename T, typename Sum, typename Prod>
requires (concepts::compatible_product_types<S1, S2>)
auto kumi::inner_product ( S1 && s1,
S2 && s2,
T init,
Sum sum,
Prod prod )
inlinenodiscardconstexprnoexcept

Computes inner product (i.e. sum of products).

Computes the generalized sum of products of the elements of two product types. By default, + and * is used.

On record types, operates by pair of fields sharing the same label.

Note
Does not participate in overload resolution if product types' size are not equal or if any of the binary operations can't be applied on the product types' elements. Similarily, doesn't participate in overload resolution if the two inputs are not compatible.
See also
concepts::compatible_product_types.
Parameters
s1First product type to operate on
s2Second product type to operate on
initInitial value
sumBinary callable function to use as the sum operations
prodBinary callable function to use as the product operations
Returns
The inner product of s1 and s2 using the provided binary operations.

Helper type

namespace kumi::result
{
template<product_type S1, sized_product_type<size_v<S1>> S2, typename T>
struct inner_product;
template<product_type S1, sized_product_type<size_v<S1>> S2, typename T
, typename Sum, typename Prod
>
struct inner_product;
template<product_type S1, sized_product_type<size_v<T1>> S2, typename T>
using inner_product_t = typename inner_product<S1,S2,T>::type;
template< product_type S1, sized_product_type<size_v<T1>> S2, typename T
, typename Sum, typename Prod
>
using inner_product_t = typename inner_product<S1,S2,T,Sum,Prod>::type;
}
constexpr auto inner_product(S1 &&s1, S2 &&s2, T init, Sum sum, Prod prod) noexcept
Computes inner product (i.e. sum of products).
Definition inner_product.hpp:93

Computes the return type of a call to kumi::inner_product

Examples:

Tuple:

#include <kumi/kumi.hpp>
#include <iostream>
int main()
{
auto bits = kumi::tuple{ 1, 0,1,0,0,1};
auto base = kumi::tuple{32,16,8,4,2,1};
std::cout << 0b101001L << "\n";
std::cout << kumi::inner_product(bits, base, 0) << "\n";
}
Fixed-size collection of heterogeneous values.
Definition tuple.hpp:33

Record:

#include <kumi/kumi.hpp>
#include <iostream>
#include <vector>
int main()
{
using namespace kumi::literals;
auto bits = kumi::record{"a"_id = 1, "b"_id = 0, "c"_id = 1, "d"_id = 0,"e"_id = 0,"f"_id = 1};
auto base = kumi::record{"f"_id = 32,"e"_id = 16,"d"_id = 8,"c"_id = 4,"b"_id = 2,"a"_id = 1};
std::cout << 0b101001L << "\n";
std::cout << kumi::inner_product(bits, base, 0) << "\n";
}
Fixed-size collection of heterogeneous tagged fields, tags are unique.
Definition record.hpp:36