Add support for a single element containment check for MultiIndexSet
For a more convenient checking whether a single element of multi-indices is contained by an instance of MultiIndexSet
,
the dunder method __contains__()
should be implemented in the class.
When operating on the MultiIndexSet
level, we often want to check if the set contains a single particular element.
While the method MultiIndexSet.contains_these_exponents()
would also work for a single element, it is not very Pythonic as the convention for checking containment in a container (which MultiIndexSet
is) is to use the in
keyword.
I expect after such implementation, the behavior is as follows:
>>> import numpy as np
>>> import minterpy as mp
>>> mi = mp.MultiIndexSet.from_degree(3, 2, 1)
>>> mi
MultiIndexSet
[[0 0 0]
[1 0 0]
[2 0 0]
[0 1 0]
[1 1 0]
[0 2 0]
[0 0 1]
[1 0 1]
[0 1 1]
[0 0 2]]
>>> [1, 0, 1] in mi # a list
True
>>> [0, 0, 3] in mi
False
>>> np.array([0, 1, 0]) in # a NumPy array
True
>>> np.array([1, 2, 3]) in mi # a NumPy array
False
>>> np.array([[1, 1, 0]]) in mi # inconsistent dim., but can be made consistent
True
>>> np.array([[0, 0, 0], [1, 0, 0]]) in mi # inconsistent dim., but can't be made consistent
False
>>> "a" in mi # inconsistent type
False
>>> np.array([1, 2, 3]) not in mi
True
The main assumption of the containment checking is an element of a multi-index set (i.e., the exponents) is a one-dimensional integer array with the spatial dimension of the set as the length.
A two-dimensional array would still work if we can squeeze it to one-dimensional.
Containment checking is quite forgiving, it seldom raises an exception and simply return False
either if the element is not in the set or the checking cannot be made.
This issue is related to the refactoring of MultiIndexSet
(Issue #99 (closed)).