Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

inner_product


Generalized Numeric Operation

Summary

Computes the inner product A X B of two ranges A and B.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

None

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:

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

Program Output

Warnings

If your compiler does not support default template parameters, then you always need to supply the Allocator template argument. For instance, you have to write:

list<int, allocator<int> > and vector<int, allocator<int> >

instead of

list<int> and vector<int>

If your compiler does not support namespaces, then you do not need the using declaration for std.



Previous fileTop of documentContentsIndexNext file
©Copyright 1998, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.
OEM Release, June 1998