Compound operations perform multiple arithmetic steps in a single call for performance. Use these instead of chaining separate operations.
Formula reference:
| Method | Formula | Instead of |
|---|---|---|
AddAndMul(X, Y, Z) | self = (X + Y) * Z | Add(X,Y) then Mul(Z) |
SubAndMul(X, Y, Z) | self = (X - Y) * Z | Sub(X,Y) then Mul(Z) |
MulAndAdd(X, Y, Z) | self = X * Y + Z | Mul(X,Y) then Add(Z) |
MulAndSub(X, Y, Z) | self = X * Y - Z | Mul(X,Y) then Sub(Z) |
DivAndAdd(X, Y, Z) | self = X / Y + Z | Div(X,Y) then Add(Z) |
DivAndSub(X, Y, Z) | self = X / Y - Z | Div(X,Y) then Sub(Z) |
AddScaled(Y, yScale) | self = self + Y * yScale | Mul then Add |
SubScaled(Y, yScale) | self = self - Y * yScale | Mul then Sub |
SqrAddScaled(X, Y, yScale) | self = sqr(X) + sqr(Y) * yScale | Sqr twice, Mul, Add |
Scale variants: Most compound methods have scale factor overloads:
AddAndMul(X, Y, Z, zScale)→ self = (X + Y) Z zScaleMulAndAdd(X, Y, Z, zScale)→ self = X Y + Z zScale
Performance: Compound operations are 2-3x faster than separate calls because:
- Single pass through memory (cache-friendly)
- No temporary arrays allocated
- Vectorized (SIMD) implementation
When to use: Whenever your formula matches one of the patterns above. The AI should recognize expressions like (A + B) * C and suggest AddAndMul.