Last active
November 26, 2015 01:22
-
-
Save pstoll/1a83da6bc02b32c79139 to your computer and use it in GitHub Desktop.
Sample showing the power of slices and letting callers specify the output array for NumPy like ufuncs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
primesieve-python pstoll$ python testnumpy.py | |
straight into array: [ 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71] | |
offset by 10: [ 0 0 0 0 0 0 0 0 0 0 2 3 5 7 11 13 17 19 23 29] | |
strided by 2: [ 2 0 3 0 5 0 7 0 11 0 13 0 17 0 19 0 23 0 29 0] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import primesieve as ps | |
import numpy as np | |
n = 10**6 | |
a = np.zeros( (2*n,), dtype=np.uint64) | |
# store the primes into the array a starting at position 0 | |
ps.generate_n_primes_numpy( n, out=a) | |
print("straight into array:", a[0:20]) | |
# store the primes in 10 spots offset into the array a | |
a = np.zeros( (2*n,), dtype=np.uint64) | |
b = ps.generate_n_primes_numpy( n, out=a[10::]) | |
print("offset by 10:", a[0:20]) | |
# store the primes into every other location of array a | |
a = np.zeros( (2*n,), dtype=np.uint64) | |
b = ps.generate_n_primes_numpy( n, out=a[::2]) | |
print("strided by 2:", a[0:20]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment