fn

is_nonzero

bool
is_nonzero(x: Tensor)
source

Predicate: is a single-element tensor non-zero?

Reduces a scalar (one-element) tensor to a Python bool. Raises if the tensor has more than one element so that the caller is forced to pick an explicit reduction (e.g. .any() / .all()) for multi-element inputs.

Parameters

xTensor
Single-element tensor (i.e. x.numel() == 1).

Returns

bool

True iff the (sole) element is non-zero.

Raises

RuntimeError
If x.numel() != 1.

Notes

Equivalent to:

out=(x00)\text{out} = (x_0 \neq 0)

where x0x_0 is the single scalar value extracted via Tensor.item. The strict shape check prevents the silent bug where one would accidentally call bool(tensor) on an unintentionally multi-element tensor.

Examples

>>> import lucid
>>> lucid.is_nonzero(lucid.tensor(0.0))
False
>>> lucid.is_nonzero(lucid.tensor(3.5))
True