Skip to content
fn

div

→Tensor
div(input: Tensor, other: Tensor | Scalar, rounding_mode: str | None = ...)

Implementing kernel

C++ engine symbols that back this Python API.

Element-wise division with broadcasting and dtype promotion, rounded as asked.

Computes input / other element-wise. With rounding_mode=None (true division) integer operands are promoted to a floating-point dtype, and division by zero produces +/-inf or NaN according to IEEE 754. rounding_mode="trunc" rounds the quotient toward zero and "floor" toward negative infinity (lucid.floor_divide); both keep an integer dtype for integer operands.

Parameters

inputTensor
Left operand.
otherTensor or scalar
Right operand. Broadcasts against input following the standard broadcasting rules; Python scalars are promoted to a tensor of matching dtype.
rounding_mode(None, trunc, floor)= None
How the quotient is rounded. Default None: not at all.

Returns

Tensor

Element-wise result with shape broadcast(input.shape, other.shape) and dtype determined by the usual type-promotion rules.

Raises

RuntimeError
An integer zero divisor with a rounding_mode, on the CPU. Metal gives -1 there without a device sync.
NotImplementedError
A bool or complex operand with a rounding_mode.
lucid._C.engine.InvalidArgument
Any other rounding_mode.

Notes

Mathematical definition:

outi=inputiotheri,trunc⁡(inputiotheri),⌊inputiotheri⌋\text{out}_i = \frac{\text{input}_i}{\text{other}_i}, \quad \operatorname{trunc}\left(\frac{\text{input}_i}{\text{other}_i}\right), \quad \left\lfloor \frac{\text{input}_i}{\text{other}_i} \right\rfloor

for None, "trunc" and "floor". "floor" is the exact floor of the real quotient; "trunc" truncates the rounded float quotient, so 1.0 / 0.1 truncates to 10 while it floors to 9, as in the reference. An integer quotient is exact in both modes.

Autograd: ∂out/∂input=1/other\partial \text{out}/\partial \text{input} = 1/\text{other}, ∂out/∂other=−input/other2\partial \text{out}/\partial \text{other} = -\text{input}/\text{other}^{2}; a rounded quotient has a zero gradient.

Examples

>>> import lucid
>>> a = lucid.tensor([1.0, 2.0, 3.0])
>>> b = lucid.tensor([4.0, 5.0, 6.0])
>>> lucid.div(a, b)
Tensor([...])
>>> lucid.div(lucid.tensor([7, -7]), 2, rounding_mode="trunc")
Tensor([...])
>>> lucid.div(lucid.tensor([7, -7]), 2, rounding_mode="floor")
Tensor([...])