Skip to main content

uqa_fusion/
boolean.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Probabilistic Boolean operators in log space (Section 5, Paper 3).
8
9use uqa_scoring::{prob_and, prob_not, prob_or};
10
11pub struct ProbabilisticBoolean;
12
13impl ProbabilisticBoolean {
14    pub fn and(probs: &[f64]) -> f64 {
15        prob_and(probs)
16    }
17
18    pub fn prob_and(probs: &[f64]) -> f64 {
19        Self::and(probs)
20    }
21
22    pub fn or(probs: &[f64]) -> f64 {
23        prob_or(probs)
24    }
25
26    pub fn prob_or(probs: &[f64]) -> f64 {
27        Self::or(probs)
28    }
29
30    pub fn not(p: f64) -> f64 {
31        prob_not(p)
32    }
33
34    pub fn prob_not(p: f64) -> f64 {
35        Self::not(p)
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    fn approx_eq(a: f64, b: f64) {
44        assert!((a - b).abs() < 1e-9, "expected {a} ~ {b}");
45    }
46
47    #[test]
48    fn and_or_de_morgan_pair() {
49        let a = 0.7;
50        let b = 0.4;
51        // ~(a AND b) == ~a OR ~b  =>  not(and([a,b])) == or([not a, not b])
52        let lhs = ProbabilisticBoolean::not(ProbabilisticBoolean::and(&[a, b]));
53        let rhs =
54            ProbabilisticBoolean::or(&[ProbabilisticBoolean::not(a), ProbabilisticBoolean::not(b)]);
55        approx_eq(lhs, rhs);
56    }
57}