I often work with multidimensional arrays whose array indices are generated from a complicated user-specified set.
I'm looking for a library with classes for representing complicated sets with an arbitrary number of indices, and arbitrarily complicated predicates. Given a set description, the desired output would be a generator. This generator would in turn produce either dict
s or tuple
s which correspond to the multidimensional array indices.
Does such a library exist?
Example
Suppose we had the following user-specified set (in set-builder notation), which represents the indices of some array variable x[i][j]
:
{i in 1..100, j in 1..50: i >= 20, j >= 21, 2*(i + j) <= 100}
I'd like to put this into some sort of a lazy class (a generator expression perhaps) that will allow me to lazily evaluate the elements of the set to generate the indices for my array. Suppose this class were called lazyset
; this would be the desired behavior:
>>> S = lazyset("{i in 1..100, j in 1..50: i >= 20, j >= 21, 2*(i+j) <= 100}")
>>> S
<generator object <genexpr> at 0x1f3e7d0>
>>> next(S)
{'i': 20, 'j': 21}
>>> next(S)
{'i': 20, 'j': 22}
I'm thinking I could roll my own using generator expressions, but this almost seems like a solved problem. So I thought I'd asked if anyone's come across an established library that handles this (to some extent, at least). Does such a library exist?
Although I am not certain if this specifically (the set-builder notation) is supported by scipy. I think scipy is your best bet regardless.
There is support for sparse arrays/sets in scipy so you can easily let it handle the allocation of those without actually allocating the space :)
This looks more like a constraint-solver problem to me:
then if you do
you get