Skip to content

Instantly share code, notes, and snippets.

@freifrauvonbleifrei
Created July 8, 2019 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freifrauvonbleifrei/d3774f00e12572d424ac15d2f6166a43 to your computer and use it in GitHub Desktop.
Save freifrauvonbleifrei/d3774f00e12572d424ac15d2f6166a43 to your computer and use it in GitHub Desktop.
Python numpy: get all combinations of d ints within certain bounds
import numpy as np
def bound_combinations(lmin, lmax):
"""
example: bound_combinations([1,1], [2,3]) == [[1,1], [1,2], [1,3], [2,1], [2,2], [2,3]]
"""
assert len(lmax) == len(lmin)
d = len(lmax)
# get all the ranges as slices
ranges = tuple(slice(lmin[i],lmax[i]+1, 1) for i in range(llen))
# get all combinations and reshape accordingly
return np.stack(np.mgrid[ranges], axis=llen).reshape((-1,llen))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment