Skip to content

Commit

Permalink
Merge pull request Bhupesh-V#464 from kbodurri/inner_product
Browse files Browse the repository at this point in the history
Showing the usage of inner_product()
  • Loading branch information
Bhupesh-V authored Oct 3, 2020
2 parents 5e5d582 + e1b11b6 commit c14fe7b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions algorithm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
:heavy_check_mark: [is_permutation](is_permutation.md)
:heavy_check_mark: [is_sorted](is_sorted.md)
:heavy_check_mark: [iter_swap](iter_swap.md)
:heavy_check_mark: [inner_product](inner_product.md)
:heavy_check_mark: [lexicographical_compare](lexicographical_compare.md)
:heavy_check_mark: [linear_search](linear_search.md)
:heavy_check_mark: [lower_bound](lower_bound.md)
Expand Down
45 changes: 45 additions & 0 deletions algorithm/inner_product.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# inner_product

**Description :** The function is used to perform the inner product of two ranges (i.e, vectors). The function takes 4 arguments. The initial position of the first range, the final position of the first range, the initial position of the second range all as iterators, and an initial value for the accumulator (usually is 0). The implementation of the inner_product() is based on an accumulator and a product function which add and multiply two numbers respectively.

Another usage of inner_product is to change the default behaviour of the accumulator (a+b) and the product (a*b). We can do that by calling the inner_product() with two extra arguments.

**Example** :
```cpp
/* Custom accumulator behaviour for inner_product.
* In this example the custom accumulator function happens to have the same
* behaviour with the default accumulator function of inner_product. */
int my_accumulator(int a, int b) {
return a + b;
}

/* Custom product behaviour for inner_product.
The default product function behaviour is to calculate the product of two
numbers (a*b). Here we change that behaviour to calculate the difference. */
int my_product(int a, int b) {
return a - b;
}

int main() {
std::vector<int> x = {1, 5, 3, 10};
std::vector<int> y = {8, 0, 2, 6};
int init_value = 0;

/* First case of inner_product: Calculate the inner product of x and y */
int default_inner_product_result = std::inner_product(x.begin(), x.end(),
y.begin(), init_value);

std::cout << "The default inner_product result between x and y is: " <<
default_inner_product_result << std::endl;

/* Second case of inner_product: Calculate the accumulative difference
between x and y. */
int custom_inner_product_result = std::inner_product(x.begin(), x.end(),
y.begin(), init_value, my_accumulator, my_product);

std::cout << "The custom inner_product result between x and y is: " <<
custom_inner_product_result << std::endl;
return 0;
}
```
**[Run Code](https://rextester.com/DBTRP29560)**
46 changes: 46 additions & 0 deletions snippets/algorithm/inner_product.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Author : Klajdi Bodurri
Date : 05/01/2020
Time : 16:07 PM
Description : Shows two different usages of inner_product().
*/

#include <iostream>
#include <vector>
#include <numeric>

/* Custom accumultor behaviour for inner_product.
* In this example the custom accumulator function happens to have the same
* behaviour with the default accumulator function of inner_product. */
int my_accumulator(int a, int b) {
return a + b;
}

/* Custom product behaviour for inner_product.
The default product function behaviour is to calculate the product of two
numbers (a*b). Here we change that behaviour to calculate the difference. */
int my_product(int a, int b) {
return a - b;
}

int main() {
std::vector<int> x = {1, 5, 3, 10};
std::vector<int> y = {8, 0, 2, 6};
int init_value = 0;

/* First case of inner_product: Calculate the inner product of x and y */
int default_inner_product_result = std::inner_product(x.begin(), x.end(),
y.begin(), init_value);

std::cout << "The default inner_product result between x and y is: " <<
default_inner_product_result << std::endl;

/* Second case of inner_product: Calculate the accumulative difference
between x and y. */
int custom_inner_product_result = std::inner_product(x.begin(), x.end(),
y.begin(), init_value, my_accumulator, my_product);

std::cout << "The custom inner_product result between x and y is: " <<
custom_inner_product_result << std::endl;
return 0;
}

0 comments on commit c14fe7b

Please sign in to comment.