Skip to content

Xor (Exactly One) ​

XorFunction returns one if and only if exactly one input expression is nonzero. For two inputs this is ordinary XOR; for three or more inputs it is an exactly-one predicate, not odd parity.

Mathematical definition ​

For nonzero indicators ai∈{0,1} and result y∈{0,1},

y=1⟺∑iai=1.

Both Kotlin and Rust use the same exact linear encoding:

y≤∑iai,y≥ai−∑j≠iaj∀i,y+ai+aj≤2∀i<j.

The first row forces zero when every input is zero. The second family forces one when exactly one indicator is active. The pair rows force zero as soon as two or more indicators are active.

Nonzero indicators ​

Each input is connected to an indicator and a sign-side helper by the shared nonzero Big-M formulation. Both implementations use a default zero-band tolerance of 1e-10 and a strict nonzero boundary of about 1.6e-9. Kotlin exposes bigM, tolerance, and strictBoundary on the direct constructor; Rust exposes the matching with_big_m, with_tolerance, with_strict_boundary, and with_parameters builders. Values with abs(input) <= tolerance are zero. The open transition interval tolerance < abs(input) < strictBoundary is undefined (null/None) because the solver deliberately rejects it; values at or beyond the strict boundary are nonzero.

Kotlin/Rust example ​

kotlin
val exactlyOne = XorFunction(
    polynomials = listOf(a, b, c),
    converter = IntoValue.Identity,
    name = "exactly-one"
)
check(exactlyOne.evaluate(valuesWithOnlyA) == Flt64.one)
check(exactlyOne.evaluate(valuesWithAAndB) == Flt64.zero)
rust
let exactly_one = XorFunction::new(1, "exactly_one", vec![a, b, c]);
assert_eq!(exactly_one.calculate_value(&only_a_tokens, false), Some(1.0));
assert_eq!(exactly_one.calculate_value(&a_and_b_tokens, false), Some(0.0));

To configure all numeric policy values in Rust at once:

rust
let configured = XorFunction::new(2, "configured_xor", vec![a, b])
    .with_parameters(Some(100.0), 1e-8, 1e-6);

Tests and references ​