Logical operations
Overview
Multik provides two element-wise logical operations: and and or. Both are infix functions that accept two arrays of the same shape and return an NDArray<Int, D> where each element is 1 (true) or 0 (false).
and
infix fun <T : Number, D : Dimension> MultiArray<T, D>.and(
other: MultiArray<T, D>
): NDArray<Int, D>
Returns an Int array where each element is the logical AND of the corresponding elements.
Truth rules
Type | False value | True value |
|---|---|---|
|
| Non-zero (bitwise AND) |
|
| Non-zero, non-NaN |
Example
val a = mk.ndarray(mk[1, 0, 3, 0])
val b = mk.ndarray(mk[1, 1, 0, 0])
val c = a and b // [1, 0, 0, 0]
or
infix fun <T : Number, D : Dimension> MultiArray<T, D>.or(
other: MultiArray<T, D>
): NDArray<Int, D>
Returns an Int array where each element is the logical OR of the corresponding elements.
Example
val a = mk.ndarray(mk[1, 0, 3, 0])
val b = mk.ndarray(mk[1, 1, 0, 0])
val c = a or b // [1, 1, 1, 0]
Pitfalls
Both arrays must have the same shape.
For integer types, bitwise AND/OR is used —
2 and 1yields0(bitwise), not1.Float/DoubleNaNis treated as false.The return type is always
NDArray<Int, D>, regardless of the input type.
28 February 2026