25constexpr long kMaxSumRange = 1L << 24;
45 unsigned resolved = 0;
50 std::vector<gate_t> cmps;
51 for (std::size_t i = 0; i < nb; ++i) {
52 auto g =
static_cast<gate_t>(i);
56 if (cmps.empty())
return 0;
79 for (
long m : match.
ms) {
80 if (m < 0) lo += m;
else hi += m;
82 const long range = hi - lo + 1;
83 if (range <= 0 || range > kMaxSumRange)
continue;
86 if (ref[
static_cast<std::size_t
>(match.
agg)] != 1)
continue;
88 std::vector<double> p;
89 p.reserve(match.
ks.size());
90 for (std::size_t i = 0; i < match.
ks.size(); ++i) {
91 if (ref[
static_cast<std::size_t
>(match.
semimods[i])] != 1) { sound =
false;
break; }
100 std::vector<double> dp(
static_cast<std::size_t
>(range), 0.0);
101 dp[
static_cast<std::size_t
>(-lo)] = 1.0;
102 for (std::size_t i = 0; i < p.size(); ++i) {
103 const long m = match.
ms[i];
104 const double pi = p[i];
105 const double qi = 1.0 - pi;
112 std::vector<double> ndp(
static_cast<std::size_t
>(range), 0.0);
113 for (
long s = lo; s <= hi; ++s) {
114 const double cur = dp[
static_cast<std::size_t
>(s - lo)];
115 if (cur == 0.0)
continue;
117 ndp[
static_cast<std::size_t
>(s - lo)] += cur * qi;
120 ndp[
static_cast<std::size_t
>(s + m - lo)] += cur * pi;
127 for (
long s = lo; s <= hi; ++s) {
128 if (sumSatisfies(s, match.
op, match.
C))
129 pr += dp[
static_cast<std::size_t
>(s - lo)];
131 if (sumSatisfies(0, match.
op, match.
C)) {
132 double prEmpty = 1.0;
133 for (
double pi : p) prEmpty *= (1.0 - pi);
138 if (pr < 0.0) pr = 0.0;
139 if (pr > 1.0) pr = 1.0;
Typed aggregation value, operator, and aggregator abstractions.
@ SUM
SUM → integer or float.
ComparisonOperator
SQL comparison operators used in gate_cmp circuit gates.
@ LE
Less than or equal (<=).
@ GE
Greater than or equal (>=).
gate_t
Strongly-typed gate identifier.
Shared machinery for the closed-form HAVING gate_cmp probability evaluators (Poisson-binomial COUNT,...
Closed-form probability resolution for HAVING SUM(a) op C gate_cmps via a weighted-sum DP.
gateType getGateType(gate_t g) const
Return the type of gate g.
std::vector< gate_t >::size_type getNbGates() const
Return the total number of gates in the circuit.
In-memory provenance circuit with semiring-generic evaluation.
void resolveCmpToBernoulli(gate_t g, double p)
Replace a gate_cmp by a constant Boolean leaf (gate_one for p == 1, gate_zero for p == 0) or by a Ber...
std::vector< unsigned > computeRefCounts(const GenericCircuit &gc)
Reference count of every gate as a wire-target across the whole circuit.
bool matchAggCmp(GenericCircuit &gc, gate_t cmp, AggCmpMatch &out)
Try to match cmp against gate_cmp(gate_agg(α, semimod_i(K_i, m_i)*), gate_value(C)).
double contributorProb(const GenericCircuit &gc, gate_t g, const std::vector< unsigned > &ref, bool &ok)
Read-once marginal probability of a count/aggregate contributor (the K side of a semimod).
unsigned runSumCmpEvaluator(GenericCircuit &gc)
Run the weighted-sum DP pre-pass over gc.
Result of matching a gate_cmp against the canonical HAVING aggregate-comparison shape.
gate_t agg
the gate_agg operand of the cmp
long C
the constant threshold, on the same integer grid as ms
std::vector< gate_t > ks
the K side of each semimod (contributor root)
std::vector< gate_t > semimods
the per-child gate_semimod parents
std::vector< long > ms
the M side of each semimod (per-row value), scaled to a common integer grid (numeric / decimal-float ...
AggregationOperator agg_kind
effective aggregate (SUM-of-1s remapped to COUNT)
ComparisonOperator op
comparator, flipped if the agg sits on the right