diff options
author | gabrieldemarmiesse <gabriel.demarmiesse@teraki.com> | 2018-07-02 17:55:59 +0200 |
---|---|---|
committer | gabrieldemarmiesse <gabriel.demarmiesse@teraki.com> | 2018-07-03 10:58:42 +0200 |
commit | 2054772371408ca1fcd3f879635e1af0faa9196b (patch) | |
tree | f34902fb291fc91d8c7c8f295b871dc816e3c79c /docs/examples/userguide/numpy_tutorial/compute_py.py | |
parent | 084a25f55d0b4cf8b4c3cd496ec57bb3e2f57f71 (diff) | |
download | cython-2054772371408ca1fcd3f879635e1af0faa9196b.tar.gz |
Changed the numpy tutorial to make is faster to understand. Added prange example.
Diffstat (limited to 'docs/examples/userguide/numpy_tutorial/compute_py.py')
-rw-r--r-- | docs/examples/userguide/numpy_tutorial/compute_py.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/docs/examples/userguide/numpy_tutorial/compute_py.py b/docs/examples/userguide/numpy_tutorial/compute_py.py new file mode 100644 index 000000000..00bcf244c --- /dev/null +++ b/docs/examples/userguide/numpy_tutorial/compute_py.py @@ -0,0 +1,28 @@ +import numpy as np
+
+
+def clip(a, min_value, max_value):
+ return min(max(a, min_value), max_value)
+
+
+def compute(array_1, array_2, a, b, c):
+ """
+ This function must implement the formula
+ np.clip(array_1, 2, 10) * a + array_2 * b + c
+
+ array_1 and array_2 are 2D.
+ """
+ x_max = array_1.shape[0]
+ y_max = array_1.shape[1]
+
+ assert array_1.shape == array_2.shape
+
+ result = np.zeros((x_max, y_max), dtype=array_1.dtype)
+
+ for x in range(x_max):
+ for y in range(y_max):
+ tmp = clip(array_1[x, y], 2, 10)
+ tmp = tmp * a + array_2[x, y] * b
+ result[x, y] = tmp + c
+
+ return result
|