fn

subtract

Tensor
subtract(a: Tensor, b: Tensor | Scalar, alpha: float = ...)
source

Element-wise subtraction with an optional scalar multiplier.

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

Parameters

aTensor
Minuend.
bTensor | Scalar
Subtrahend. Python scalars are broadcast against a.
alphafloat
Scalar multiplier applied to b before subtraction. Defaults to 1.0 (plain subtraction).

Returns

Tensor

Element-wise difference of the broadcast shape of a and b.

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.])