fn

isin

Tensor
isin(elements: Tensor | TensorLike, test_elements: Tensor | TensorLike, invert: bool = ...)
source

Per-element set-membership test.

For each entry of elements, checks whether the value appears anywhere in test_elements. Implemented as a broadcasted equality compare followed by an OR reduction.

Parameters

elementsTensor | TensorLike
Values to test. Reshaped to 1-D internally; the output is reshaped back to elements.shape.
test_elementsTensor | TensorLike
The set of values to test against. Flattened internally.
invertbool
If True, return the negation of the membership test. Defaults to False.

Returns

Tensor

Boolean tensor of the same shape as elements.

Notes

Mathematical definition (with E=elementsE = \text{elements} and T=test_elementsT = \text{test\_elements}):

outi=j(Ei=Tj).\text{out}_i = \bigvee_{j} (E_i = T_j).

Cost is O(ET)\mathcal{O}(|E| \cdot |T|). For large test_elements a sort-based implementation would be cheaper, but this composite form keeps the operation differentiable-by-default (the gradient is zero everywhere — the output dtype is boolean — but the structure is preserved).

Examples

>>> import lucid
>>> e = lucid.tensor([1, 2, 3, 4])
>>> t = lucid.tensor([2, 4])
>>> lucid.isin(e, t)
Tensor([False,  True, False,  True])