forked from aphearin/accelerating_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpairwise_sum_cython.py
52 lines (41 loc) · 1.47 KB
/
pairwise_sum_cython.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
""" Python function calling an underlying cython engine
calculating the pairwise sum of the elements of two arrays.
"""
try:
from pairwise_sum_cython_engine import pairwise_sum_cython_engine
except ImportError:
msg = ("The cython module must be compiled first via "
"``python setup.py build_ext --inplace``")
raise ImportError(msg)
__all__ = ('pairwise_sum_cython', )
def pairwise_sum_cython(arr1, arr2):
""" Function calculates the pairwise sum of all elements in arr1 and arr2.
Parameters
-----------
arr1 : array_like
1-d array storing *npts1* floats
arr2 : array_like
1-d array storing *npts2* floats
Returns
-------
result : array
1-d array storing *npts1 x npts2* floats determined by the
pairwise sum of the input ``arr1`` and ``arr2``.
Element *k* of ``result`` equals *arr1[i] + arr2[j]*, where
*i = k // npts2* and *j = k % npts2*.
"""
# Catch input errors within python layer to make exception handling easier
try:
npts1 = len(arr1)
npts2 = len(arr2)
assert npts1 > 1
assert npts2 > 1
except TypeError:
msg = "Input ``arr1`` and ``arr2`` must be arrays"
raise TypeError(msg)
except AssertionError:
msg = "Input ``arr1`` and ``arr2`` must have more than one element"
raise ValueError(msg)
# Call the underlying cython kernel
result = pairwise_sum_cython_engine(arr1, arr2)
return result