2
# tag: cpp, werror, cpp11
4
from libcpp.numeric cimport inner_product, iota, accumulate, adjacent_difference, partial_sum
5
from libcpp.vector cimport vector
6
from libcpp cimport bool
8
# Subtracts two integers.
9
cdef int subtract_integers(int lhs, int rhs):
13
cdef int add_integers(int lhs, int rhs):
16
# Multiplies two integers.
17
cdef int multiply_integers(int lhs, int rhs):
20
# Determines equality for two integers.
21
# If lhs == rhs, returns true. Returns false otherwise.
22
cdef bool is_equal(int lhs, int rhs):
25
def test_inner_product(vector[int] v1, vector[int] v2, int init):
27
Test inner_product with integer values.
28
>>> test_inner_product([1, 2, 3], [1, 2, 3], 1)
31
return inner_product(v1.begin(), v1.end(), v2.begin(), init)
34
def test_inner_product_with_zero(vector[int] v1, vector[int] v2, int init):
36
Test inner_product with a zero value in the container.
37
>>> test_inner_product_with_zero([1, 2, 0], [1, 1, 1], 0)
40
return inner_product(v1.begin(), v1.end(), v2.begin(), init)
42
def test_inner_product_with_bin_op(vector[int] v1, vector[int] v2, int init):
44
Test inner_product with two binary operations. In this case,
45
Looks at number of pairwise matches between v1 and v2.
48
There are 3 matches (5, 2, 3). So, 1 + 1 + 1 = 3.
50
>>> test_inner_product_with_bin_op([5, 1, 2, 3, 4], [5, 4, 2, 3, 1], 0)
53
return inner_product(v1.begin(), v1.end(), v2.begin(), init, add_integers, is_equal)
55
def test_iota(vector[int] v, int value):
57
Test iota with beginning value of 0.
58
>>> test_iota(range(6), 0)
61
iota(v.begin(), v.end(), value)
64
def test_iota_negative_init(vector[int] v, int value):
66
Test iota with a negative beginning value.
67
>>> test_iota_negative_init(range(7), -4)
68
[-4, -3, -2, -1, 0, 1, 2]
70
iota(v.begin(), v.end(), value)
73
def test_accumulate(vector[int] v, int init):
79
>>> test_accumulate([1, 2, 3], 0)
82
return accumulate(v.begin(), v.end(), init)
84
def test_accumulate_with_subtraction(vector[int] v, int init):
86
Test accumulate with subtraction. Note that accumulate is a fold-left operation.
90
>>> test_accumulate_with_subtraction([1, 2, 3], 0)
93
return accumulate(v.begin(), v.end(), init, subtract_integers)
95
def test_adjacent_difference(vector[int] v):
97
Test adjacent_difference with integer values.
104
>>> test_adjacent_difference([2, 4, 6, 8, 10, 12])
107
adjacent_difference(v.begin(), v.end(), v.begin())
110
def test_adjacent_difference_with_bin_op(vector[int] v):
112
Test adjacent_difference with a binary operation.
118
>>> test_adjacent_difference_with_bin_op([1, 2, 4, 5, 6])
121
adjacent_difference(v.begin(), v.end(), v.begin(), add_integers)
124
def test_partial_sum(vector[int] v):
126
Test partial_sum with integer values.
133
>>> test_partial_sum([2, 2, 2, 2, 2, 2])
136
partial_sum(v.begin(), v.end(), v.begin())
139
def test_partial_sum_with_bin_op(vector[int] v):
141
Test partial_sum with a binary operation.
142
Using multiply_integers, partial_sum will calculate the first 5 powers of 2.
143
>>> test_partial_sum_with_bin_op([2, 2, 2, 2, 2])
146
partial_sum(v.begin(), v.end(), v.begin(), multiply_integers)