Module Tolk_frontend.Elementwise
Element-wise operations.
Binary operations broadcast their operands to a common shape and promote them to a common dtype before applying. Broadcasting follows the standard rule: shapes are aligned from the last axis, and an axis of size 1 is stretched to match. Promotion picks the least dtype that both operands cast into without loss where possible. Use Tensor.i, Tensor.f, and Tensor.b to lift scalar operands.
Broadcasting and promotion themselves are Tensor.broadcasted, whose implementation lives in Op.
contiguous t forces t to be materialised into a contiguous buffer, breaking a chain of lazy views. It is a value-preserving layout hint.
Arithmetic
div a b is element-wise true division. Integer operands are promoted to float, so the result is always floating point.
floordiv a b rounds the quotient toward negative infinity.
mod_ a b is the remainder with the sign of the divisor (floor modulo), matching floordiv.
Comparisons
Each returns a boolean tensor.
Bitwise
Operands must be integer or boolean.
threefry x key mixes the 64-bit counters x with the 64-bit key using the Threefry-2x32 block cipher, producing uniformly distributed bits. Both operands must be uint64 tensors of the same shape. This is the primitive underlying the Rand generators.
Selection
where cond x y selects from x where cond is true and from y elsewhere. cond is cast to boolean and all three are broadcast together.
masked_fill t mask value replaces the elements of t where mask is true with value, leaving the rest unchanged.
inverse t is the additive inverse neg t for floating-point t and the bitwise complement for integer t. It is the reflection used to turn a maximum into a minimum.
usum t ts folds t and ts together, adding them when t is numeric and taking their logical or when t is boolean.
uprod t ts folds t and ts together, multiplying them when t is numeric and taking their logical and when t is boolean.
Unary math
Transcendental functions promote integer inputs to float.
Activations
Powers, shifts, and truncating division
pow a b is a raised to the power b, element-wise. When a is integer and b floating point the result is rounded and cast back to a's dtype.
cdiv a b divides a by b rounding the quotient toward zero (C-style), as opposed to floordiv which rounds toward negative infinity.
fmod a b is the remainder of truncating division, taking the sign of the dividend a, as opposed to mod_ which takes the sign of the divisor.
lshift a b shifts a left by b bits. Operands must be integer.
rshift a b shifts a right by b bits, arithmetically. Operands must be integer.
Sign and interpolation
copysign a b has the magnitude of a and the sign of b, element-wise.
logaddexp a b is log (exp a + exp b), computed stably by factoring out the element-wise maximum.
lerp t end_ weight linearly interpolates from t to end_ by weight, i.e. t + (end_ - t) * weight.
Rounding and clamping
clamp ~min ~max t bounds t below by min and above by max. Either bound may be omitted.
More unary math
erf t is the Gauss error function, approximated by a rational polynomial.
Floating-point classification
Each returns a boolean tensor.
isinf t is true where t is an infinity. detect_positive and detect_negative (both default true) restrict which sign counts.
isclose t other is true where t and other agree within atol plus rtol times |other|. Matching infinities compare equal; NaNs compare equal only when equal_nan is set.