Skip to content
Oakfield Operator Calculus Function Reference Site

Reaction Operators

sim_add_remainder_operator(ctx, warped, reference, out, opts)

Measure analytic residues between a warped field and a reference field with optional nonlinear transformations. Useful for computing error metrics, loss functions, and field comparisons with configurable nonlinearities.

sim_add_remainder_operator(ctx, warped, reference, output, [options]) -> operator

Returns: Operator handle (userdata)

The operator computes:

outi=wf(warpedireferencei)+b\text{out}_i = w \cdot f(\text{warped}_i - \text{reference}_i) + b

where:

  • ww is the weight parameter
  • bb is the bias parameter
  • ff is the nonlinearity function

Nonlinearity options:

  • identity: f(x)=xf(x) = x (linear difference)
  • abs: f(x)=xf(x) = |x| (absolute error)
  • log_abs: f(x)=log(x+ε)f(x) = \log(|x| + \varepsilon) (log-scale error)
  • power: f(x)=xpsign(x)f(x) = |x|^p \cdot \text{sign}(x) (power-law error)
  • tanh: f(x)=tanh(x)f(x) = \tanh(x) (saturating error)

Complex mode handling:

  • component: Compare real and imaginary parts independently
  • polar: Compare magnitude and phase
ParameterTypeDefaultRangeDescription
weightdouble 1.0unboundedGain applied to residue
biasdouble 0.0unboundedAdditive bias after nonlinearity
exponentdouble 1.0unboundedPower for nonlinearity = "power"
epsilondouble 1e-4>0Guard for log/power modes
nonlinearityenum "identity"see belowAnalytic transform to apply
complex_modeenum "component"see belowComplex comparison mode
accumulateboolean falseAdd to output instead of overwriting
scale_by_dtboolean trueScale accumulated writes by dt

Nonlinearity options: identity, abs, log_abs, power, tanh

Complex mode options: component, polar

  • Operates elementwise; no boundary handling required
  • All three fields (warped, reference, output) must have compatible dimensions
  • For complex fields, complex_mode determines comparison semantics
  • identity/abs: Unconditionally stable
  • log_abs: Protected by epsilon; avoid very small epsilon values
  • power: Sign-preserving for odd-integer exponents; epsilon guards against |x|^p overflow
  • tanh: Saturates smoothly at ±1; useful for bounded error signals
  • Lightweight pointwise operation
  • log_abs and power require transcendental function evaluation
  • tanh uses fast approximation when available
  • complex_mode = "polar" requires magnitude/phase extraction
-- Simple difference (identity)
ooc.sim_add_remainder_operator(ctx, warped, ref, diff, {
nonlinearity = "identity"
})
-- Squared error (power = 2)
ooc.sim_add_remainder_operator(ctx, warped, ref, sq_error, {
nonlinearity = "power",
exponent = 2
})
-- Absolute error
ooc.sim_add_remainder_operator(ctx, warped, ref, abs_error, {
nonlinearity = "abs"
})
-- Log-scale error (for wide dynamic range)
ooc.sim_add_remainder_operator(ctx, warped, ref, log_error, {
nonlinearity = "log_abs",
epsilon = 1e-5
})
-- Saturating error (bounded output)
ooc.sim_add_remainder_operator(ctx, warped, ref, bounded_error, {
nonlinearity = "tanh",
weight = 2.0 -- scale before tanh
})
-- Weighted and biased residue
ooc.sim_add_remainder_operator(ctx, warped, ref, weighted, {
nonlinearity = "abs",
weight = 0.5,
bias = 0.1
})
-- Accumulate error over time
ooc.sim_add_remainder_operator(ctx, warped, ref, integrated_error, {
nonlinearity = "power",
exponent = 2,
accumulate = true,
scale_by_dt = true
})
-- Complex polar comparison
ooc.sim_add_remainder_operator(ctx, complex_warped, complex_ref, diff, {
nonlinearity = "abs",
complex_mode = "polar"
})
-- L1 loss with custom epsilon
ooc.sim_add_remainder_operator(ctx, prediction, target, l1_loss, {
nonlinearity = "abs",
epsilon = 1e-8
})