Skip to content

Instantly share code, notes, and snippets.

@pstoll
Last active November 26, 2015 01:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pstoll/1a83da6bc02b32c79139 to your computer and use it in GitHub Desktop.
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
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]
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