Template Function thrust::inner_product(InputIterator1, InputIterator1, InputIterator2, OutputType)¶
Function Documentation¶
-
template<typename
InputIterator1
, typenameInputIterator2
, typenameOutputType
>
OutputTypethrust
::
inner_product
(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputType init) inner_product
calculates an inner product of the ranges[first1, last1)
and[first2, first2 + (last1 - first1))
.Specifically, this version of
inner_product
computes the suminit + (*first1 * *first2) + (*(first1+1) * *(first2+1)) + ...
Unlike the C++ Standard Template Library function
std::inner_product
, this version offers no guarantee on order of execution.The following code demonstrates how to use
inner_product
to compute the dot product of two vectors.- Return
The inner product of sequences
[first1, last1)
and[first2, last2)
plusinit
.- Parameters
first1
: The beginning of the first sequence.last1
: The end of the first sequence.first2
: The beginning of the second sequence.init
: Initial value of the result.
- Template Parameters
InputIterator1
: is a model of Input Iterator,InputIterator2
: is a model of Input Iterator,OutputType
: is a model of Assignable, and ifx
is an object of typeOutputType
, andy
is an object ofInputIterator1's
value_type
, andz
is an object ofInputIterator2's
value_type
, thenx + y * z
is defined and is convertible toOutputType
.
#include <thrust/inner_product.h> ... float vec1[3] = {1.0f, 2.0f, 5.0f}; float vec2[3] = {4.0f, 1.0f, 5.0f}; float result = thrust::inner_product(vec1, vec1 + 3, vec2, 0.0f); // result == 31.0f