Check for Membership (Containment)#
import minterpy as mp
import numpy as np
This guide shows how to check the membership of a set of exponents in a MultiIndexSet
instance.
Motivating example#
As a motivating example, consider the following three multi-index sets of the same dimension and \(l_p\)-degree of \(2.0\):
mi_a = mp.MultiIndexSet(
np.array([[0, 0, 0], [1, 0, 0], [2, 0, 0], [0, 1, 0], [1, 1, 0]]),
lp_degree=2.0,
)
mi_b = mp.MultiIndexSet(
np.array([[0, 0, 0], [1, 0, 0]]),
lp_degree=2.0,
)
mi_c = mp.MultiIndexSet(
np.array(
[[0, 0, 0], [2, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 2]],
),
lp_degree=2.0,
)
Check for exponents membership#
The method contains_these_exponents()
is used to check whether a given set of exponents are contained in the set of exponents of a MultiIndexSet
instance. The method takes as input the set of exponents as an integer NumPy array. It returns True
if the set of exponents are contained in the MultiIndexSet
instance and False
otherwise.
For instance, to check if the exponents \(\{(0, 0, 0), (2, 0, 0)\}\) are contained in the multi-index set \(A\) (they are):
mi_a.contains_these_exponents(np.array([[0, 0, 0], [2, 0, 0]]))
True
Attention
Note that the dimension of the set exponents must be consistent with the dimension of the multi-index set; otherwise, an exception will be raised.
This behavior can be changed via the parameter expand_dim
(default is set to False
); if set to True
, the set whose the lower dimension is first expanded before the membership check is carried out.
As another example, the exponent \((2, 0, 0)\) is not contained in \(B\):
mi_b.contains_these_exponents(np.array([[2, 0, 0]]))
False
If a set of exponents are checked then it is either all of them are contained in a multi-index set or none of them; a set of exponents cannot be partially contained in a multi-index set.
For instance, the set of exponents \(\{ (0, 0, 0), (1, 0, 0), (2, 0, 0) \}\) is not contained in \(B\) because the element \((2, 0, 0)\) is not contained in \(B\):
mi_b.contains_these_exponents(
np.array([[0, 0, 0], [1, 0, 0], [2, 0, 0]])
)
False
in
Keyword#
A single index element may be checked for containment in a MultiIndexSet
instance via the in
keyword.
For instance, the element \((1, 1, 0)\) is in \(C\):
np.array([1, 1, 0]) in mi_c
True
One may also use list instead of an array:
[0, 0, 0] in mi_a
True
Note that the containment check via the in
keyword is forgiving in the sense that if the operation does not make any sense the statement simply returns False
instead of raising an exception.
For example:
"a" in mi_a
False
Different in spatial dimension#
The behavior of the contains_these_exponents()
method and the in
keyword differs with respect to how it handle item of different spatial dimension.
In the method call, if there is any difference in the spatial dimension, the method raises an exception.
This behavior can be changed via the expand_dim
parameter.
For example, the index element \((1, 1)\) when compared to a set whose spatial dimension is \(3\) is expanded to \((1, 1, 0)\) (zero padding) when expand_dim
is set to True
:
mi_a.contains_these_exponents(np.array([[1, 1]]), expand_dim=True)
True
When using the in
keyword, this dimension expansion happens automatically:
[1, 1] in mi_a
True