Skip to content
fn

subtract

→Tensor
subtract(input: Tensor, other: Tensor | Scalar, alpha: Scalar = ...)

Element-wise subtraction with an optional scalar multiplier.

Computes a−α⋅ba - \alpha \cdot b element-wise, broadcasting input and other to a common shape and following the standard dtype-promotion rules.

Parameters

inputTensor
Minuend.
otherTensor | Scalar
Subtrahend. Python scalars are broadcast against input.
alphaScalar= ...
Scalar multiplier applied to other before subtraction. Defaults to 1 (plain subtraction).

Returns

Tensor

Element-wise difference of the broadcast shape of input and other.

Notes

Mathematical definition:

outi=ai−α⋅bi\text{out}_i = a_i - \alpha \cdot b_i

Dtype follows standard promotion: e.g. int64 - float32 → float32. Gradients flow as ∂/∂a=1\partial / \partial a = 1 and ∂/∂b=−α\partial / \partial b = -\alpha.

Examples

>>> import lucid
>>> a = lucid.tensor([4.0, 5.0])
>>> b = lucid.tensor([1.0, 2.0])
>>> lucid.subtract(a, b, alpha=2.0)
tensor([2., 1.])