inner_product (3C++std) - Tru64 UNIX
Standard C++ LibraryCopyright 1996, Rogue Wave Software, Inc. NAME
inner_product - Computes the inner product A X B of two ranges A and B.
SYNOPSIS
#include <numeric>
template <class InputIterator1, class InputIterator2,
class T>
T inner_product (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, T init);
template <class InputIterator1, class InputIterator2,
class T,
class BinaryOperation1,
class BinaryOperation2>
T inner_product (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, T init,
BinaryOperation1 binary_op1,
BinaryOperation2 binary_op2);
DESCRIPTION
There are two versions of inner_product. The first computes an inner
product using the default multiplication and addition operators, while the
second allows you to specify binary operations to use in place of the
default operations.
The first version of the function computes its result by initializing the
accumulator acc with the initial value init and then modifying it with:
acc = acc + ((*i1) * (*i2))
for every iterator i1 in the range [first1, last1) and iterator i2 in the
range [first2, first2 + (last1 - first1)). The algorithm returns acc.
The second version of the function initializes acc with init, then computes
the result:
acc = binary_op1(acc, binary_op2(*i1, *i2))
for every iterator i1 in the range [first1, last1) and iterator i2 in the
range [first2, first2 + (last1 - first1)).
COMPLEXITY
The inner_product algorithm computes exactly (last1 - first1) applications
of either:
acc + (*i1) * (*i2)
or
binary_op1(acc, binary_op2(*i1, *i2)).
EXAMPLE
//
// inr_prod.cpp
//
#include <numeric> //For inner_product
#include <list> //For list
#include <vector> //For vectors
#include <functional> //For plus and minus
#include <iostream.h>
int main()
{
//Initialize a list and an int using arrays of ints
int a1[3] = {6, -3, -2};
int a2[3] = {-2, -3, -2};
list<int> l(a1, a1+3);
vector<int> v(a2, a2+3);
//Calculate the inner product of the two sets of values
int inner_prod =
inner_product(l.begin(), l.end(), v.begin(), 0);
//Calculate a wacky inner product using the same values
int wacky =
inner_product(l.begin(), l.end(), v.begin(), 0,
plus<int>(), minus<int>());
//Print the output
cout << "For the two sets of numbers: " << endl
<< " ";
copy(v.begin(),v.end(),ostream_iterator<int,char>(cout," "));
cout << endl << " and ";
copy(l.begin(),l.end(),ostream_iterator<int,char>(cout," "));
cout << "," << endl << endl;
cout << "The inner product is: " << inner_prod << endl;
cout << "The wacky result is: " << wacky << endl;
return 0;
}
Output :
For the two sets of numbers:
-2 -3 -2
and 6 -3 -2 ,
The inner product is: 1
The wacky result is: 8
WARNINGS
If your compiler does not support default template parameters, then you
need to always supply the Allocator template argument. For instance,
you'll have to write :
list<int, allocator<int> > and vector<int, allocator<int> >
instead of
list<int> and vector<int>
STANDARDS CONFORMANCE
ANSI X3J16/ISO WG21 Joint C++ Committee
|