Apache C++ Standard Library Reference Guide

## inner_product()

Library: Numerics

    Function

- Local Index
- Summary
- Synopsis
- Description
- Complexity
- Example
- Standards Conformance

### Local Index

No Entries

### Summary

A generalized numeric operation that computes the inner product `A X B` of two ranges `A` and `B`

### Synopsis

    #include <numeric>

    namespace std {
      template <class InputIterator1, class InputIterator2,
                class T>
      T inner_product(InputIterator1 start1,
                      InputIterator1 finish1,
                      InputIterator2 start2, T init);

      template <class InputIterator1, class InputIterator2,
                class T,
                class BinaryOperation1,
                class BinaryOperation2>
      T inner_product(InputIterator1 start1,
                      InputIterator1 finish1,
                      InputIterator2 start2, 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 initializes `acc` with `init` and then modifies it with:

`acc = acc + ((*i1) * (*i2))`

for every iterator `i1` in the range `[start1, finish1)` and iterator `i2` in the range `[start2, start2 + (finish1 - start1))`. 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 `[start1, finish1)` and iterator `i2` in the range `[start2, start2 + (finish1 - start1))`.

### Complexity

The `inner_product()` algorithm computes exactly `(finish1 - start1)` applications of either:

`acc + (*i1) * (*i2)`

or

`binary_op1(acc, binary_op2(*i1, *i2)).`

### Example

    //
    //  inr_prod.cpp
    //

    #include <algorithm>     // for copy
    #include <numeric>       // for inner_product
    #include <iostream>      // for cout, endl
    #include <list>          // for list
    #include <vector>        // for vector
    #include <functional>    // for plus, minus

    int main ()
    {
        typedef std::list<int, std::allocator<int> >   List;
        typedef std::vector<int, std::allocator<int> > Vector;
        typedef std::ostream_iterator<int, char,
                              std::char_traits<char> > os_iter;

        // Initialize a list and a vector using arrays of ints.
        int a1[] = { 6, -3, -2 };
        int a2[] = { -2, -3, -2 };

        List   l (a1, a1 + sizeof a1 / sizeof *a1);
        Vector v (a2, a2 + sizeof a2 / sizeof *a2);

        // Calculate the inner product of the two sets of values.
        List::value_type prod = std::inner_product (l.begin (),
                                          l.end (), v.begin (), 0);

        // Calculate a wacky inner product using the same values.
        List::value_type wacky =
            std::inner_product (l.begin (), l.end (), v.begin (),
                                0, std::plus<List::value_type>(),
                                std::minus<List::value_type>());
        // Print the output.
        std::cout << "For the sets of numbers: { ";
        std::copy (v.begin (), v.end (), os_iter (std::cout, " "));

        std::cout << "} and { ";
        std::copy (l.begin (), l.end (), os_iter (std::cout, " "));

        std::cout << "}\nThe inner product is: " << prod
                  << "\nThe wacky result is:  " << wacky
                  << std::endl;

        return 0;
    }

    Program Output:
    For the sets of numbers: { -2 -3 -2 } and { 6 -3 -2 }
    The inner product is: 1
    The wacky result is:  8

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 26.4.2