cython

Форк
0
/
cpp_stl_numeric_ops.pyx 
147 строк · 4.0 Кб
1
# mode: run
2
# tag: cpp, werror, cpp11
3

4
from libcpp.numeric cimport inner_product, iota, accumulate, adjacent_difference, partial_sum
5
from libcpp.vector cimport vector
6
from libcpp cimport bool
7

8
# Subtracts two integers.
9
cdef int subtract_integers(int lhs, int rhs):
10
    return lhs - rhs
11

12
# Adds two integers.
13
cdef int add_integers(int lhs, int rhs):
14
    return lhs + rhs
15

16
# Multiplies two integers.
17
cdef int multiply_integers(int lhs, int rhs):
18
    return lhs * rhs
19

20
# Determines equality for two integers.
21
# If lhs == rhs, returns true. Returns false otherwise.
22
cdef bool is_equal(int lhs, int rhs):
23
    return lhs == rhs
24

25
def test_inner_product(vector[int] v1, vector[int] v2, int init):
26
    """
27
    Test inner_product with integer values.
28
    >>> test_inner_product([1, 2, 3], [1, 2, 3], 1)
29
    15
30
    """
31
    return inner_product(v1.begin(), v1.end(), v2.begin(), init)
32

33

34
def test_inner_product_with_zero(vector[int] v1, vector[int] v2, int init):
35
    """
36
    Test inner_product with a zero value in the container.
37
    >>> test_inner_product_with_zero([1, 2, 0], [1, 1, 1], 0)
38
    3
39
    """
40
    return inner_product(v1.begin(), v1.end(), v2.begin(), init)
41

42
def test_inner_product_with_bin_op(vector[int] v1, vector[int] v2, int init):
43
    """
44
    Test inner_product with two binary operations. In this case,
45
    Looks at number of pairwise matches between v1 and v2.
46
    [5, 1, 2, 3, 4]
47
    [5, 4, 2, 3, 1]
48
    There are 3 matches (5, 2, 3). So, 1 + 1 + 1 = 3.
49

50
    >>> test_inner_product_with_bin_op([5, 1, 2, 3, 4], [5, 4, 2, 3, 1], 0)
51
    3
52
    """
53
    return inner_product(v1.begin(), v1.end(), v2.begin(), init, add_integers, is_equal)
54

55
def test_iota(vector[int] v, int value):
56
    """
57
    Test iota with beginning value of 0.
58
    >>> test_iota(range(6), 0)
59
    [0, 1, 2, 3, 4, 5]
60
    """
61
    iota(v.begin(), v.end(), value)
62
    return v
63

64
def test_iota_negative_init(vector[int] v, int value):
65
    """
66
    Test iota with a negative beginning value.
67
    >>> test_iota_negative_init(range(7), -4)
68
    [-4, -3, -2, -1, 0, 1, 2]
69
    """
70
    iota(v.begin(), v.end(), value)
71
    return v
72

73
def test_accumulate(vector[int] v, int init):
74
    """
75
    Test accumulate.
76
     0 + 1 = 1
77
     1 + 2 = 3
78
     3 + 3 = 6
79
    >>> test_accumulate([1, 2, 3], 0)
80
    6
81
    """
82
    return accumulate(v.begin(), v.end(), init)
83

84
def test_accumulate_with_subtraction(vector[int] v, int init):
85
    """
86
    Test accumulate with subtraction. Note that accumulate is a fold-left operation.
87
     0 - 1 = -1
88
    -1 - 2 = -3
89
    -3 - 3 = -6
90
    >>> test_accumulate_with_subtraction([1, 2, 3], 0)
91
    -6
92
    """
93
    return accumulate(v.begin(), v.end(), init, subtract_integers)
94

95
def test_adjacent_difference(vector[int] v):
96
    """
97
    Test adjacent_difference with integer values.
98
    2 - 0,   -> 2
99
    4 - 2,   -> 2
100
    6 - 4,   -> 2
101
    8 - 6,   -> 2
102
    10 - 8,  -> 2
103
    12 - 10  -> 2
104
    >>> test_adjacent_difference([2, 4, 6, 8, 10, 12])
105
    [2, 2, 2, 2, 2, 2]
106
    """
107
    adjacent_difference(v.begin(), v.end(), v.begin())
108
    return v
109

110
def test_adjacent_difference_with_bin_op(vector[int] v):
111
    """
112
    Test adjacent_difference with a binary operation.
113
    0 + 1 -> 1
114
    1 + 2 -> 3
115
    2 + 4 -> 6
116
    4 + 5 -> 9
117
    5 + 6 -> 11
118
    >>> test_adjacent_difference_with_bin_op([1, 2, 4, 5, 6])
119
    [1, 3, 6, 9, 11]
120
    """
121
    adjacent_difference(v.begin(), v.end(), v.begin(), add_integers)
122
    return v
123

124
def test_partial_sum(vector[int] v):
125
    """
126
    Test partial_sum with integer values.
127
    2 + 0   -> 2
128
    2 + 2   -> 4
129
    4 + 2   -> 6
130
    6 + 2   -> 8
131
    8 + 2   -> 10
132
    10 + 2  -> 12
133
    >>> test_partial_sum([2, 2, 2, 2, 2, 2])
134
    [2, 4, 6, 8, 10, 12]
135
    """
136
    partial_sum(v.begin(), v.end(), v.begin())
137
    return v
138

139
def test_partial_sum_with_bin_op(vector[int] v):
140
    """
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])
144
    [2, 4, 8, 16, 32]
145
    """
146
    partial_sum(v.begin(), v.end(), v.begin(), multiply_integers)
147
    return v
148

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.