Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/inner product"

From cppreference.com
< cpp‎ | algorithm
m (Text replace - "{{eq fun cpp" to "{{eq fun")
(Parameters: remove straggling text from pre-P0452 revision)
 
(37 intermediate revisions by 14 users not shown)
Line 1: Line 1:
 
{{cpp/title|inner_product}}
 
{{cpp/title|inner_product}}
{{cpp/algorithm/navbar}}
+
{{cpp/algorithm/numeric/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | numeric}}
+
{{dcl header|numeric}}
{{ddcl list item | num=1 |
+
{{dcla|num=1|constexpr=c++20|
template< class InputIterator1, class InputIterator2, class T >
+
template< class InputIt1, class InputIt2, class T >
T inner_product( InputIterator1 first1, InputIterator1 last1,
+
T inner_product( InputIt1 first1, InputIt1 last1,
                 InputIterator2 first2, T value );
+
                 InputIt2 first2, T init );
 
}}
 
}}
{{ddcl list item | num=2 |
+
{{dcla|num=2|constexpr=c++20|
template<
+
template< class InputIt1, class InputIt2, class T,
    class InputIterator1,
+
          class BinaryOp1, class BinaryOp2 >
    class InputIterator2,
+
T inner_product( InputIt1 first1, InputIt1 last1,
    class T,
+
                InputIt2 first2, T init,
    class BinaryOperation1,
+
                BinaryOp1 op1, BinaryOp2 op2 );
    class BinaryOperation2
+
> T inner_product( InputIterator1 first1, InputIterator1 last1,
+
                  InputIterator2 first2, T value,
+
                  BinaryOperation1 op1,
+
                  BinaryOperation2 op2 );
+
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
Computes inner product (i.e. sum of products) of the range {{tt|[first1, last1)}} and another range beginning at {{tt|first2}}. The first version uses {{tt|operator*}} to compute product of the element pairs and {{tt|operator+}} to sum up the products, the second version uses {{tt|op2}} and {{tt|op1}} for these tasks respectively.
+
Computes inner product (i.e. sum of products) or performs ordered map/reduce operation on the range {{range|first1|last1}} and the range of {{c|std::distance(first1, last1)}} elements beginning at {{c|first2}}.  
 +
 
 +
@1@ Initializes the accumulator {{c|acc}} (of type {{tt|T}}) with the initial value {{c|init}} and then  modifies it with the expression {{rev inl|until=c++20|{{c|1=acc = acc + (*i1) * (*i2)}}}}{{rev inl|since=c++20|{{c|1=acc = std::move(acc) + (*i1) * (*i2)}}}} for every iterator {{c|i1}} in the range {{range|first1|last1}} in order and its corresponding iterator {{c|i2}} in the range beginning at {{c|first2}}. For built-in meaning of + and *, this computes inner product of the two ranges.
 +
 
 +
@2@ Initializes the accumulator {{c|acc}} (of type {{tt|T}}) with the initial value {{c|init}} and then  modifies it with the expression {{rev inl|until=c++20|{{c|1=acc = op1(acc, op2(*i1, *i2))}}}}{{rev inl|since=c++20|{{c|1=acc = op1(std::move(acc), op2(*i1, *i2))}}}} for every iterator {{c|i1}} in the range {{range|first1|last1}} in order and its corresponding iterator {{c|i2}} in the range beginning at {{c|first2}}.
 +
 
 +
Given {{c|last2}} as the {{c|std::distance(first1, last1)}}{{su|b=th}} next iterator of {{c|first2}}, if any of the following conditions is satisfied, the behavior is undefined:
 +
* {{tt|T}} is not {{named req|CopyConstructible}}.
 +
* {{tt|T}} is not {{named req|CopyAssignable}}.
 +
* {{c|op1}} or {{c|op2}} modifies any element of {{range|first1|last1}} or {{range|first2|last2}}.
 +
* {{c|op1}} or {{c|op2}} invalidates any iterator or subrange in {{closed range|first1|last1}} or {{closed range|first2|last2}}.
  
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | first1, last1 | the first range of elements}}
+
{{par|first1, last1|the first range of elements}}
{{param list item | first2 | the beginning of the second range of elements}}
+
{{par|first2|the beginning of the second range of elements}}
{{param list item | value | initial value of the sum of the products}}
+
{{par|init|initial value of the sum of the products}}
{{param list op2 | op1 | This function takes a value returned by op2 and the current value of the accumulator and produces a new value to be stored in the accumulator. | t1=T | t2=Type3 | rt=T}}
+
{{par op2|op1|This "sum" function takes a value returned by {{c|op2}} and the current value of the accumulator and produces a new value to be stored in the accumulator.|t1=T|t2=Type3|rt=T}}
{{param list op2 | op2 | This function takes one value from each range and produces a new value. | p1=InputIterator1 | p2=InputIterator2 | rt=Type3}}
+
{{par op2|op2|This "product" function takes one value from each range and produces a new value.|p1=InputIt1|p2=InputIt2|rt=Type3}}
{{param list end}}
+
{{par hreq}}
 +
{{par req named|InputIt1, InputIt2|InputIterator}}
 +
{{par end}}
  
 
===Return value===
 
===Return value===
The inner product of two ranges.
+
{{c|acc}} after all modifications.
  
 
===Possible implementation===
 
===Possible implementation===
{{eq fun
+
{{eq impl
| 1=
+
|title1=inner_product (1)|ver1=1|1=
template<class InputIterator1, class InputIterator2, class T>
+
template<class InputIt1, class InputIt2, class T>
T inner_product(InputIterator1 first1, InputIterator1 last1,
+
constexpr // since C++20
                InputIterator2 first2, T value)
+
T inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init)
 
{
 
{
     while (first1 != last1) {
+
     while (first1 != last1)
        value = value + *first1 * *first2;
+
    {
        ++first1;
+
        init = std::move(init) + (*first1) * (*first2); // std::move since C++20
        ++first2;
+
        ++first1;
 +
        ++first2;
 
     }
 
     }
     return value;
+
   
 +
     return init;
 
}
 
}
| 2=
+
|title2=inner_product (2)|ver2=2|2=
template<class InputIterator1, class InputIterator2,
+
template<class InputIt1, class InputIt2, class T,
        class T,
+
         class BinaryOp1, class BinaryOp2>
         class BinaryOperation1, class BinaryOperation2>
+
constexpr // since C++20
T inner_product(InputIterator1 first1, InputIterator1 last1,
+
T inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init,
                InputIterator2 first2, T value,
+
                 BinaryOp1 op1, BinaryOp2 op2)
                 BinaryOperation1 op1
+
                BinaryOperation2 op2)
+
 
{
 
{
     while (first1 != last1) {
+
     while (first1 != last1)
        value = op1(value, op2(*first1, *first2));
+
    {
        ++first1;
+
        init = op1(std::move(init), op2(*first1, *first2)); // std::move since C++20
        ++first2;
+
        ++first1;
 +
        ++first2;
 
     }
 
     }
     return value;
+
   
 +
     return init;
 
}
 
}
 
}}
 
}}
 +
 +
===Notes===
 +
The parallelizable version of this algorithm, {{lc|std::transform_reduce}}, requires {{c|op1}} and {{c|op2}} to be commutative and associative, but {{tt|std::inner_product}} makes no such requirement, and always performs the operations in the order given.
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|
| code=
+
|code=
#include <numeric>
+
#include <functional>
 
#include <iostream>
 
#include <iostream>
 +
#include <numeric>
 
#include <vector>
 
#include <vector>
#include <functional>
+
 
 
int main()
 
int main()
 
{
 
{
 
     std::vector<int> a{0, 1, 2, 3, 4};
 
     std::vector<int> a{0, 1, 2, 3, 4};
 
     std::vector<int> b{5, 4, 2, 3, 1};
 
     std::vector<int> b{5, 4, 2, 3, 1};
 
+
   
 
     int r1 = std::inner_product(a.begin(), a.end(), b.begin(), 0);
 
     int r1 = std::inner_product(a.begin(), a.end(), b.begin(), 0);
 
     std::cout << "Inner product of a and b: " << r1 << '\n';
 
     std::cout << "Inner product of a and b: " << r1 << '\n';
 
+
   
 
     int r2 = std::inner_product(a.begin(), a.end(), b.begin(), 0,
 
     int r2 = std::inner_product(a.begin(), a.end(), b.begin(), 0,
                                 std::plus<int>(), std::equal_to<int>());
+
                                 std::plus<>(), std::equal_to<>());
 
     std::cout << "Number of pairwise matches between a and b: " <<  r2 << '\n';
 
     std::cout << "Number of pairwise matches between a and b: " <<  r2 << '\n';
 
}
 
}
| output=
+
|output=
 
Inner product of a and b: 21
 
Inner product of a and b: 21
 
Number of pairwise matches between a and b: 2
 
Number of pairwise matches between a and b: 2
 
}}
 
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=242|std=C++98|before={{c|op1}} and {{c|op2}} could not have side effects|after=they cannot modify the ranges involved}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/algorithm/dcl list accumulate}}
+
{{dsc inc|cpp/algorithm/dsc transform_reduce}}
{{dcl list template | cpp/algorithm/dcl list partial_sum}}
+
{{dsc inc|cpp/algorithm/dsc accumulate}}
{{dcl list end}}
+
{{dsc inc|cpp/algorithm/dsc partial_sum}}
 +
{{dsc end}}
  
[[ru:cpp/algorithm/inner product]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 14:31, 26 October 2024

 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
(C++11)
inner_product
Operations on uninitialized memory
 
 
Defined in header <numeric>
template< class InputIt1, class InputIt2, class T >

T inner_product( InputIt1 first1, InputIt1 last1,

                 InputIt2 first2, T init );
(1) (constexpr since C++20)
template< class InputIt1, class InputIt2, class T,

          class BinaryOp1, class BinaryOp2 >
T inner_product( InputIt1 first1, InputIt1 last1,
                 InputIt2 first2, T init,

                 BinaryOp1 op1, BinaryOp2 op2 );
(2) (constexpr since C++20)

Computes inner product (i.e. sum of products) or performs ordered map/reduce operation on the range [first1last1) and the range of std::distance(first1, last1) elements beginning at first2.

1) Initializes the accumulator acc (of type T) with the initial value init and then modifies it with the expression acc = acc + (*i1) * (*i2)(until C++20)acc = std::move(acc) + (*i1) * (*i2)(since C++20) for every iterator i1 in the range [first1last1) in order and its corresponding iterator i2 in the range beginning at first2. For built-in meaning of + and *, this computes inner product of the two ranges.
2) Initializes the accumulator acc (of type T) with the initial value init and then modifies it with the expression acc = op1(acc, op2(*i1, *i2))(until C++20)acc = op1(std::move(acc), op2(*i1, *i2))(since C++20) for every iterator i1 in the range [first1last1) in order and its corresponding iterator i2 in the range beginning at first2.

Given last2 as the std::distance(first1, last1)th next iterator of first2, if any of the following conditions is satisfied, the behavior is undefined:

  • T is not CopyConstructible.
  • T is not CopyAssignable.
  • op1 or op2 modifies any element of [first1last1) or [first2last2).
  • op1 or op2 invalidates any iterator or subrange in [first1last1] or [first2last2].

Contents

[edit] Parameters

first1, last1 - the first range of elements
first2 - the beginning of the second range of elements
init - initial value of the sum of the products
op1 - binary operation function object that will be applied. This "sum" function takes a value returned by op2 and the current value of the accumulator and produces a new value to be stored in the accumulator.

The signature of the function should be equivalent to the following:

 Ret fun(const Type1 &a, const Type2 &b);

The signature does not need to have const &.
The types  Type1 and  Type2 must be such that objects of types T and Type3 can be implicitly converted to  Type1 and  Type2 respectively. The type Ret must be such that an object of type T can be assigned a value of type Ret. ​

op2 - binary operation function object that will be applied. This "product" function takes one value from each range and produces a new value.

The signature of the function should be equivalent to the following:

 Ret fun(const Type1 &a, const Type2 &b);

The signature does not need to have const &.
The types  Type1 and  Type2 must be such that objects of types InputIt1 and InputIt2 can be dereferenced and then implicitly converted to  Type1 and  Type2 respectively. The type Ret must be such that an object of type Type3 can be assigned a value of type Ret. ​

Type requirements
-
InputIt1, InputIt2 must meet the requirements of LegacyInputIterator.

[edit] Return value

acc after all modifications.

[edit] Possible implementation

inner_product (1)
template<class InputIt1, class InputIt2, class T>
constexpr // since C++20
T inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init)
{
    while (first1 != last1)
    {
        init = std::move(init) + (*first1) * (*first2); // std::move since C++20
        ++first1;
        ++first2;
    }
 
    return init;
}
inner_product (2)
template<class InputIt1, class InputIt2, class T,
         class BinaryOp1, class BinaryOp2>
constexpr // since C++20
T inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init,
                BinaryOp1 op1, BinaryOp2 op2)
{
    while (first1 != last1)
    {
        init = op1(std::move(init), op2(*first1, *first2)); // std::move since C++20
        ++first1;
        ++first2;
    }
 
    return init;
}

[edit] Notes

The parallelizable version of this algorithm, std::transform_reduce, requires op1 and op2 to be commutative and associative, but std::inner_product makes no such requirement, and always performs the operations in the order given.

[edit] Example

#include <functional>
#include <iostream>
#include <numeric>
#include <vector>
 
int main()
{
    std::vector<int> a{0, 1, 2, 3, 4};
    std::vector<int> b{5, 4, 2, 3, 1};
 
    int r1 = std::inner_product(a.begin(), a.end(), b.begin(), 0);
    std::cout << "Inner product of a and b: " << r1 << '\n';
 
    int r2 = std::inner_product(a.begin(), a.end(), b.begin(), 0,
                                std::plus<>(), std::equal_to<>());
    std::cout << "Number of pairwise matches between a and b: " <<  r2 << '\n';
}

Output:

Inner product of a and b: 21
Number of pairwise matches between a and b: 2

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 242 C++98 op1 and op2 could not have side effects they cannot modify the ranges involved

[edit] See also

applies an invocable, then reduces out of order
(function template) [edit]
sums up or folds a range of elements
(function template) [edit]
computes the partial sum of a range of elements
(function template) [edit]