use oxilean_kernel::{BinderInfo, Declaration, Environment, Expr, Level, Name};
use super::types::{
AbstractIntegral, BochnerIntegralApprox, Capacity, CaratheodoryExtension,
ConditionalExpectation, ConvergenceTheoremType, DiscreteMeasure, DiscreteMeasureTyped,
DiscreteProbabilitySpace, EgoroffData, FiniteSigmaAlgebra, FubiniData, HaarMeasureOnZpN,
HausdorffDimensionEstimator, Independence, LebesgueMeasureEstimator, LpNormComputer, LusinData,
Martingale, MeasureDisintExt, MeasureDisintegration, MonteCarloMeasureEstimator,
RadonNikodymData, SignedMeasure,
};
pub fn app(f: Expr, a: Expr) -> Expr {
Expr::App(Box::new(f), Box::new(a))
}
pub fn app2(f: Expr, a: Expr, b: Expr) -> Expr {
app(app(f, a), b)
}
pub fn cst(s: &str) -> Expr {
Expr::Const(Name::str(s), vec![])
}
pub fn prop() -> Expr {
Expr::Sort(Level::zero())
}
pub fn type0() -> Expr {
Expr::Sort(Level::succ(Level::zero()))
}
pub fn pi(bi: BinderInfo, name: &str, dom: Expr, body: Expr) -> Expr {
Expr::Pi(bi, Name::str(name), Box::new(dom), Box::new(body))
}
pub fn arrow(a: Expr, b: Expr) -> Expr {
pi(BinderInfo::Default, "_", a, b)
}
pub fn nat_ty() -> Expr {
cst("Nat")
}
pub fn real_ty() -> Expr {
cst("Real")
}
pub fn app3(f: Expr, a: Expr, b: Expr, c: Expr) -> Expr {
app(app2(f, a, b), c)
}
pub fn bvar(n: u32) -> Expr {
Expr::BVar(n)
}
pub fn list_ty(elem: Expr) -> Expr {
app(cst("List"), elem)
}
pub fn complex_ty() -> Expr {
cst("Complex")
}
pub fn int_ty() -> Expr {
cst("Int")
}
pub fn sigma_algebra_ty() -> Expr {
arrow(type0(), type0())
}
pub fn measure_ty() -> Expr {
pi(
BinderInfo::Default,
"Omega",
type0(),
arrow(app(cst("SigmaAlgebra"), cst("Omega_bvar")), type0()),
)
}
pub fn probability_space_ty() -> Expr {
arrow(type0(), prop())
}
pub fn measurable_function_ty() -> Expr {
pi(
BinderInfo::Default,
"Omega",
type0(),
arrow(type0(), type0()),
)
}
pub fn random_variable_ty() -> Expr {
arrow(type0(), type0())
}
pub fn lebesgue_integral_ty() -> Expr {
arrow(arrow(type0(), real_ty()), real_ty())
}
pub fn monotone_convergence_ty() -> Expr {
prop()
}
pub fn dominated_convergence_ty() -> Expr {
prop()
}
pub fn fubini_tonelli_ty() -> Expr {
prop()
}
pub fn radon_nikodym_ty() -> Expr {
prop()
}
pub fn borel_cantelli_ty() -> Expr {
arrow(arrow(nat_ty(), real_ty()), prop())
}
pub fn generated_sigma_algebra_ty() -> Expr {
pi(
BinderInfo::Default,
"Omega",
type0(),
arrow(arrow(type0(), prop()), app(cst("SigmaAlgebra"), type0())),
)
}
pub fn borel_sigma_algebra_ty() -> Expr {
arrow(type0(), app(cst("SigmaAlgebra"), type0()))
}
pub fn sigma_additive_ty() -> Expr {
arrow(type0(), prop())
}
pub fn outer_measure_ty() -> Expr {
arrow(type0(), type0())
}
pub fn caratheodory_extension_ty() -> Expr {
prop()
}
pub fn lebesgue_translation_invariant_ty() -> Expr {
prop()
}
pub fn lebesgue_regularity_ty() -> Expr {
prop()
}
pub fn fatous_lemma_ty() -> Expr {
arrow(arrow(nat_ty(), arrow(type0(), real_ty())), prop())
}
pub fn product_measure_ty() -> Expr {
pi(
BinderInfo::Default,
"Omega1",
type0(),
arrow(type0(), type0()),
)
}
pub fn signed_measure_ty() -> Expr {
arrow(type0(), type0())
}
pub fn jordan_decomposition_ty() -> Expr {
arrow(arrow(type0(), real_ty()), prop())
}
pub fn hahn_decomposition_ty() -> Expr {
arrow(type0(), prop())
}
pub fn absolutely_continuous_of_ty() -> Expr {
arrow(type0(), arrow(type0(), prop()))
}
pub fn lp_space_ty() -> Expr {
pi(BinderInfo::Default, "p", real_ty(), arrow(type0(), type0()))
}
pub fn lp_completeness_ty() -> Expr {
arrow(real_ty(), prop())
}
pub fn holders_inequality_ty() -> Expr {
arrow(real_ty(), arrow(real_ty(), prop()))
}
pub fn minkowskis_inequality_ty() -> Expr {
arrow(real_ty(), prop())
}
pub fn riesz_representation_measure_ty() -> Expr {
arrow(type0(), prop())
}
pub fn weak_convergence_measures_ty() -> Expr {
arrow(arrow(nat_ty(), type0()), arrow(type0(), prop()))
}
pub fn tight_sequence_ty() -> Expr {
arrow(arrow(nat_ty(), type0()), prop())
}
pub fn prokhorov_theorem_ty() -> Expr {
prop()
}
pub fn regular_conditional_probability_ty() -> Expr {
pi(
BinderInfo::Default,
"Omega",
type0(),
arrow(type0(), type0()),
)
}
pub fn disintegration_of_measure_ty() -> Expr {
arrow(type0(), arrow(type0(), prop()))
}
pub fn haar_measure_ty() -> Expr {
arrow(type0(), type0())
}
pub fn haar_measure_unique_ty() -> Expr {
arrow(type0(), prop())
}
pub fn ergodic_measure_ty() -> Expr {
pi(
BinderInfo::Default,
"Omega",
type0(),
arrow(arrow(type0(), type0()), prop()),
)
}
pub fn birkhoff_ergodic_theorem_ty() -> Expr {
prop()
}
pub fn optional_stopping_theorem_ty() -> Expr {
prop()
}
pub fn conditional_expectation_ty() -> Expr {
pi(
BinderInfo::Default,
"Omega",
type0(),
arrow(
app(cst("SigmaAlgebra"), type0()),
arrow(arrow(type0(), real_ty()), arrow(type0(), real_ty())),
),
)
}
pub fn gaussian_measure_ty() -> Expr {
arrow(type0(), type0())
}
pub fn wiener_measure_ty() -> Expr {
type0()
}
pub fn hausdorff_measure_ty() -> Expr {
pi(BinderInfo::Default, "d", real_ty(), arrow(type0(), type0()))
}
pub fn hausdorff_dimension_ty() -> Expr {
arrow(type0(), real_ty())
}
pub fn martingale_ty() -> Expr {
pi(
BinderInfo::Default,
"Omega",
type0(),
arrow(nat_ty(), type0()),
)
}
pub fn stopping_time_ty() -> Expr {
arrow(type0(), arrow(nat_ty(), prop()))
}
pub fn doob_upcrossing_inequality_ty() -> Expr {
prop()
}
pub fn martingale_convergence_ty() -> Expr {
prop()
}
pub fn caratheodory_extension_full_ty() -> Expr {
prop()
}
pub fn hahn_decomposition_full_ty() -> Expr {
prop()
}
pub fn jordan_decomposition_full_ty() -> Expr {
prop()
}
pub fn total_variation_ty() -> Expr {
arrow(arrow(type0(), real_ty()), real_ty())
}
pub fn banach_lattice_measures_ty() -> Expr {
arrow(type0(), type0())
}
pub fn bochner_integral_ty() -> Expr {
pi(
BinderInfo::Default,
"Omega",
type0(),
arrow(type0(), type0()),
)
}
pub fn bochner_integrable_ty() -> Expr {
arrow(arrow(type0(), type0()), prop())
}
pub fn pettis_integral_ty() -> Expr {
pi(
BinderInfo::Default,
"Omega",
type0(),
arrow(type0(), type0()),
)
}
pub fn dunford_integral_ty() -> Expr {
pi(
BinderInfo::Default,
"Omega",
type0(),
arrow(type0(), type0()),
)
}
pub fn lp_duality_ty() -> Expr {
arrow(real_ty(), arrow(real_ty(), prop()))
}
pub fn characteristic_function_ty() -> Expr {
arrow(type0(), arrow(real_ty(), complex_ty()))
}
pub fn levy_continuity_theorem_ty() -> Expr {
prop()
}
pub fn prokhorov_metric_ty() -> Expr {
arrow(type0(), arrow(type0(), real_ty()))
}
pub fn self_similar_measure_ty() -> Expr {
arrow(real_ty(), type0())
}
pub fn moran_theorem_ty() -> Expr {
prop()
}
pub fn covering_dimension_ty() -> Expr {
arrow(type0(), nat_ty())
}
pub fn rectifiable_set_ty() -> Expr {
arrow(nat_ty(), arrow(nat_ty(), type0()))
}
pub fn varifold_ty() -> Expr {
arrow(nat_ty(), arrow(nat_ty(), type0()))
}
pub fn current_ty() -> Expr {
arrow(nat_ty(), arrow(nat_ty(), type0()))
}
pub fn federer_fleming_closure_ty() -> Expr {
prop()
}
pub fn invariant_measure_existence_ty() -> Expr {
arrow(arrow(type0(), type0()), prop())
}
pub fn ergodic_decomposition_theorem_ty() -> Expr {
prop()
}
pub fn poincare_recurrence_theorem_ty() -> Expr {
prop()
}
pub fn povm_ty() -> Expr {
arrow(type0(), type0())
}
pub fn quantum_state_ty() -> Expr {
arrow(type0(), type0())
}
pub fn born_rule_ty() -> Expr {
arrow(type0(), arrow(type0(), prop()))
}
pub fn choquet_simplex_ty() -> Expr {
arrow(type0(), prop())
}
pub fn choquet_integral_representation_ty() -> Expr {
arrow(type0(), prop())
}
pub fn rokhlin_disintegration_ty() -> Expr {
arrow(type0(), arrow(type0(), prop()))
}
pub fn state_on_cstar_algebra_ty() -> Expr {
arrow(type0(), type0())
}
pub fn gns_construction_ty() -> Expr {
arrow(type0(), arrow(type0(), prop()))
}
pub fn von_neumann_algebra_ty() -> Expr {
arrow(type0(), type0())
}
pub fn murray_von_neumann_equiv_ty() -> Expr {
arrow(type0(), prop())
}
pub fn build_measure_theory_env(env: &mut Environment) {
let axioms: &[(&str, Expr)] = &[
("SigmaAlgebra", sigma_algebra_ty()),
("Measure", measure_ty()),
("ProbabilitySpace", probability_space_ty()),
("MeasurableFunction", measurable_function_ty()),
("RandomVariable", random_variable_ty()),
("LebesgueIntegral", lebesgue_integral_ty()),
("monotone_convergence", monotone_convergence_ty()),
("dominated_convergence", dominated_convergence_ty()),
("fubini_tonelli", fubini_tonelli_ty()),
("radon_nikodym", radon_nikodym_ty()),
("borel_cantelli", borel_cantelli_ty()),
("SigmaFinite", arrow(type0(), prop())),
(
"AbsolutelyContinuous",
arrow(type0(), arrow(type0(), prop())),
),
("AlmostEverywhere", arrow(arrow(type0(), prop()), prop())),
("Integrable", arrow(arrow(type0(), real_ty()), prop())),
("Expectation", arrow(arrow(type0(), real_ty()), real_ty())),
("Variance", arrow(arrow(type0(), real_ty()), real_ty())),
("SigmaAlgebraMem", arrow(list_ty(nat_ty()), prop())),
("GeneratedSigmaAlgebra", generated_sigma_algebra_ty()),
("BorelSigmaAlgebra", borel_sigma_algebra_ty()),
("SigmaAdditive", sigma_additive_ty()),
("OuterMeasure", outer_measure_ty()),
("caratheodory_extension", caratheodory_extension_ty()),
(
"lebesgue_translation_invariant",
lebesgue_translation_invariant_ty(),
),
("lebesgue_regularity", lebesgue_regularity_ty()),
("fatous_lemma", fatous_lemma_ty()),
("ProductMeasure", product_measure_ty()),
("SignedMeasure", signed_measure_ty()),
("jordan_decomposition", jordan_decomposition_ty()),
("hahn_decomposition", hahn_decomposition_ty()),
("AbsolutelyContinuousOf", absolutely_continuous_of_ty()),
("LpSpace", lp_space_ty()),
("lp_completeness", lp_completeness_ty()),
("holders_inequality", holders_inequality_ty()),
("minkowskis_inequality", minkowskis_inequality_ty()),
(
"riesz_representation_measure",
riesz_representation_measure_ty(),
),
("WeakConvergenceMeasures", weak_convergence_measures_ty()),
("TightSequence", tight_sequence_ty()),
("prokhorov_theorem", prokhorov_theorem_ty()),
(
"RegularConditionalProbability",
regular_conditional_probability_ty(),
),
("disintegration_of_measure", disintegration_of_measure_ty()),
("HaarMeasure", haar_measure_ty()),
("haar_measure_unique", haar_measure_unique_ty()),
("ErgodicMeasure", ergodic_measure_ty()),
("birkhoff_ergodic_theorem", birkhoff_ergodic_theorem_ty()),
("Martingale", martingale_ty()),
("StoppingTime", stopping_time_ty()),
("optional_stopping_theorem", optional_stopping_theorem_ty()),
(
"doob_upcrossing_inequality",
doob_upcrossing_inequality_ty(),
),
("martingale_convergence", martingale_convergence_ty()),
("ConditionalExpectation", conditional_expectation_ty()),
("GaussianMeasure", gaussian_measure_ty()),
("WienerMeasure", wiener_measure_ty()),
("HausdorffMeasure", hausdorff_measure_ty()),
("HausdorffDimension", hausdorff_dimension_ty()),
(
"caratheodory_extension_full",
caratheodory_extension_full_ty(),
),
("hahn_decomposition_full", hahn_decomposition_full_ty()),
("jordan_decomposition_full", jordan_decomposition_full_ty()),
("TotalVariation", total_variation_ty()),
("BanachLatticeMeasures", banach_lattice_measures_ty()),
("BochnerIntegral", bochner_integral_ty()),
("BochnerIntegrable", bochner_integrable_ty()),
("PettisIntegral", pettis_integral_ty()),
("DunfordIntegral", dunford_integral_ty()),
("lp_duality", lp_duality_ty()),
("CharacteristicFunction", characteristic_function_ty()),
("levy_continuity_theorem", levy_continuity_theorem_ty()),
("ProkhorovMetric", prokhorov_metric_ty()),
("SelfSimilarMeasure", self_similar_measure_ty()),
("moran_theorem", moran_theorem_ty()),
("CoveringDimension", covering_dimension_ty()),
("RectifiableSet", rectifiable_set_ty()),
("Varifold", varifold_ty()),
("Current", current_ty()),
("federer_fleming_closure", federer_fleming_closure_ty()),
(
"invariant_measure_existence",
invariant_measure_existence_ty(),
),
(
"ergodic_decomposition_theorem",
ergodic_decomposition_theorem_ty(),
),
(
"poincare_recurrence_theorem",
poincare_recurrence_theorem_ty(),
),
("POVM", povm_ty()),
("QuantumState", quantum_state_ty()),
("born_rule", born_rule_ty()),
("ChoquetSimplex", choquet_simplex_ty()),
(
"choquet_integral_representation",
choquet_integral_representation_ty(),
),
("rokhlin_disintegration", rokhlin_disintegration_ty()),
("StateOnCStarAlgebra", state_on_cstar_algebra_ty()),
("gns_construction", gns_construction_ty()),
("VonNeumannAlgebra", von_neumann_algebra_ty()),
("murray_von_neumann_equiv", murray_von_neumann_equiv_ty()),
];
for (name, ty) in axioms {
env.add(Declaration::Axiom {
name: Name::str(*name),
univ_params: vec![],
ty: ty.clone(),
})
.ok();
}
}
pub fn discrete_integral(f: &[f64], measure: &DiscreteMeasure) -> f64 {
f.iter()
.zip(measure.weights.iter())
.map(|(fi, wi)| fi * wi)
.sum()
}
pub fn expectation(f: &dyn Fn(&str) -> f64, space: &DiscreteProbabilitySpace) -> f64 {
space
.outcomes
.iter()
.zip(space.probabilities.iter())
.map(|(o, p)| f(o.as_str()) * p)
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_discrete_prob_space_uniform() {
let space = DiscreteProbabilitySpace::uniform(vec!["H".to_string(), "T".to_string()]);
assert_eq!(space.n_outcomes(), 2);
assert!(space.is_valid());
assert!((space.prob("H") - 0.5).abs() < 1e-10);
assert!((space.prob("T") - 0.5).abs() < 1e-10);
}
#[test]
fn test_discrete_prob_space_prob() {
let space = DiscreteProbabilitySpace::new(
vec!["a".to_string(), "b".to_string(), "c".to_string()],
vec![0.2, 0.5, 0.3],
)
.expect("should be valid");
assert!((space.prob("a") - 0.2).abs() < 1e-10);
assert!((space.prob("b") - 0.5).abs() < 1e-10);
assert!((space.prob("c") - 0.3).abs() < 1e-10);
assert_eq!(space.prob("d"), 0.0);
}
#[test]
fn test_discrete_prob_invalid() {
let result =
DiscreteProbabilitySpace::new(vec!["a".to_string(), "b".to_string()], vec![0.3, 0.3]);
assert!(result.is_none(), "Should be None when probs don't sum to 1");
}
#[test]
fn test_finite_sigma_algebra_trivial() {
let sa = FiniteSigmaAlgebra::trivial(4);
assert_eq!(sa.sets.len(), 2);
assert!(sa.is_sigma_algebra());
}
#[test]
fn test_finite_sigma_algebra_power_set_is_valid() {
let sa = FiniteSigmaAlgebra::power_set(3);
assert_eq!(sa.sets.len(), 8);
assert!(sa.is_sigma_algebra());
}
#[test]
fn test_discrete_measure_counting() {
let m = DiscreteMeasure::counting_measure(5);
assert_eq!(m.n_elements, 5);
assert!((m.measure_of(&[0, 2, 4]) - 3.0).abs() < 1e-10);
assert!(!m.is_probability());
}
#[test]
fn test_discrete_integral() {
let m = DiscreteMeasure::new(vec![0.5, 0.3, 0.2]);
let f = vec![1.0, 2.0, 3.0];
let result = discrete_integral(&f, &m);
assert!((result - 1.7).abs() < 1e-10, "Expected 1.7, got {}", result);
}
#[test]
fn test_expectation() {
let space = DiscreteProbabilitySpace::uniform(vec!["H".to_string(), "T".to_string()]);
let e = expectation(&|o| if o == "H" { 1.0 } else { 0.0 }, &space);
assert!((e - 0.5).abs() < 1e-10, "Expected 0.5, got {}", e);
}
#[test]
fn test_build_measure_theory_env() {
let mut env = Environment::new();
build_measure_theory_env(&mut env);
assert!(!env.is_empty());
}
#[test]
fn test_discrete_measure_typed_integrate() {
let m = DiscreteMeasureTyped::new(vec![1u32, 2, 3], vec![0.5, 0.3, 0.2]);
let result = m.integrate(|&x| x as f64);
assert!((result - 1.7).abs() < 1e-10);
}
#[test]
fn test_discrete_measure_typed_normalize() {
let m = DiscreteMeasureTyped::new(vec!["a", "b"], vec![2.0, 3.0]);
let nm = m.normalize().expect("non-zero total");
assert!((nm.total_mass() - 1.0).abs() < 1e-10);
assert!((nm.atom_weight(&"a").expect("atom_weight should succeed") - 0.4).abs() < 1e-10);
}
#[test]
fn test_discrete_measure_typed_push_forward() {
let m = DiscreteMeasureTyped::new(vec![0usize, 1, 2, 3], vec![0.25, 0.25, 0.25, 0.25]);
let pf = m.push_forward(|&x| if x % 2 == 0 { "even" } else { "odd" });
let even_w = pf.atom_weight(&"even").expect("atom_weight should succeed");
let odd_w = pf.atom_weight(&"odd").expect("atom_weight should succeed");
assert!((even_w - 0.5).abs() < 1e-10);
assert!((odd_w - 0.5).abs() < 1e-10);
}
#[test]
fn test_lebesgue_estimator_full_interval() {
let est = LebesgueMeasureEstimator::new(0.0, 1.0, 10_000);
let m = est.estimate(&[(0.0, 1.0)]);
assert!((m - 1.0).abs() < 1e-3);
}
#[test]
fn test_lebesgue_estimator_half() {
let est = LebesgueMeasureEstimator::new(0.0, 1.0, 100_000);
let m = est.estimate(&[(0.0, 0.5)]);
assert!((m - 0.5).abs() < 1e-3);
}
#[test]
fn test_lp_norm_l2() {
let lp = LpNormComputer::uniform(4);
let f = vec![1.0, 2.0, 3.0, 4.0];
let expected = (7.5_f64).sqrt();
let got = lp.lp_norm(&f, 2.0);
assert!((got - expected).abs() < 1e-10);
}
#[test]
fn test_holder_inequality() {
let lp = LpNormComputer::uniform(4);
let f = vec![1.0, 0.0, 2.0, 1.0];
let g = vec![2.0, 3.0, 0.0, 1.0];
let (lhs, rhs, holds) = lp.verify_holder(&f, &g, 2.0);
assert!(holds, "Hölder failed: lhs={} rhs={}", lhs, rhs);
}
#[test]
fn test_minkowski_inequality() {
let lp = LpNormComputer::uniform(4);
let f = vec![1.0, 2.0, 0.0, 1.0];
let g = vec![0.0, 1.0, 3.0, 1.0];
let (lhs, rhs, holds) = lp.verify_minkowski(&f, &g, 2.0);
assert!(holds, "Minkowski failed: lhs={} rhs={}", lhs, rhs);
}
#[test]
fn test_monte_carlo_estimator_circle() {
let est = MonteCarloMeasureEstimator::new(10_000, 2);
let area = est.estimate_unit_cube(|pt| {
let x = pt[0];
let y = pt[1];
x * x + y * y <= 1.0
});
let pi_over_4 = std::f64::consts::PI / 4.0;
assert!((area - pi_over_4).abs() < 0.02, "got {}", area);
}
#[test]
fn test_haar_measure_zn_atom_weight() {
let h = HaarMeasureOnZpN::new(6);
assert!((h.atom_weight() - 1.0 / 6.0).abs() < 1e-12);
}
#[test]
fn test_haar_measure_zn_translation_invariance() {
let h = HaarMeasureOnZpN::new(8);
let set = vec![0usize, 2, 4, 6];
assert!(h.verify_translation_invariance(&set));
}
#[test]
fn test_haar_measure_zn_integrate_constant() {
let h = HaarMeasureOnZpN::new(5);
let result = h.integrate(|_| 1.0);
assert!((result - 1.0).abs() < 1e-12);
}
#[test]
fn test_haar_measure_zn_convolve_delta() {
let n = 4usize;
let h = HaarMeasureOnZpN::new(n);
let f = vec![1.0, 2.0, 3.0, 4.0];
let delta = [1.0, 0.0, 0.0, 0.0];
let conv = h.convolve(&f, &delta);
for i in 0..n {
assert!((conv[i] - f[i] / n as f64).abs() < 1e-12);
}
}
#[test]
fn test_build_env_has_new_axioms() {
let mut env = Environment::new();
build_measure_theory_env(&mut env);
for name in &[
"GeneratedSigmaAlgebra",
"BorelSigmaAlgebra",
"OuterMeasure",
"SignedMeasure",
"LpSpace",
"HaarMeasure",
"ErgodicMeasure",
"Martingale",
"ConditionalExpectation",
"WienerMeasure",
"HausdorffMeasure",
] {
assert!(env.contains(&Name::str(*name)), "Missing axiom: {}", name);
}
}
#[test]
fn test_caratheodory_extension_total_mass() {
let ext = CaratheodoryExtension::new(vec![0.25, 0.25, 0.25, 0.25]);
assert!((ext.total_mass() - 1.0).abs() < 1e-12);
}
#[test]
fn test_caratheodory_extension_additive() {
let ext = CaratheodoryExtension::new(vec![0.1, 0.2, 0.3, 0.4]);
assert!(ext.is_finitely_additive(&[0, 1], &[2, 3]));
assert!(!ext.is_finitely_additive(&[0, 1], &[1, 2]));
}
#[test]
fn test_caratheodory_extension_measurability() {
let ext = CaratheodoryExtension::new(vec![0.1, 0.2, 0.3, 0.4]);
assert!(ext.is_caratheodory_measurable(&[0]));
assert!(ext.is_caratheodory_measurable(&[0, 1, 2, 3]));
}
#[test]
fn test_bochner_integral_constant() {
let approx = BochnerIntegralApprox::new(vec![0.5, 0.5], 2);
let values = vec![vec![1.0, 2.0], vec![1.0, 2.0]];
let result = approx.integrate(&values);
assert!((result[0] - 1.0).abs() < 1e-12);
assert!((result[1] - 2.0).abs() < 1e-12);
}
#[test]
fn test_bochner_integral_jensen() {
let approx = BochnerIntegralApprox::new(vec![0.3, 0.7], 2);
let values = vec![vec![1.0, 0.0], vec![0.0, 1.0]];
assert!(approx.verify_jensen(&values));
}
#[test]
fn test_measure_disintegration_marginal() {
let disint = MeasureDisintegration::new(vec![0.2, 0.3, 0.1, 0.4], vec![0, 0, 1, 1], 2);
let nu = disint.marginal();
assert!((nu[0] - 0.5).abs() < 1e-12);
assert!((nu[1] - 0.5).abs() < 1e-12);
}
#[test]
fn test_measure_disintegration_identity() {
let disint = MeasureDisintegration::new(vec![0.2, 0.3, 0.1, 0.4], vec![0, 0, 1, 1], 2);
let f = vec![1.0, 2.0, 3.0, 4.0];
let (lhs, rhs, holds) = disint.verify_disintegration(&f);
assert!(holds, "Disintegration: lhs={} rhs={}", lhs, rhs);
}
#[test]
fn test_hausdorff_box_count_line() {
let pts = vec![(0.0, 0.0), (0.25, 0.0), (0.5, 0.0), (0.75, 0.0)];
let est = HausdorffDimensionEstimator::new(pts);
assert_eq!(est.box_count(0.5), 2);
assert_eq!(est.box_count(0.25), 4);
}
#[test]
fn test_hausdorff_dimension_full_square() {
let n = 64;
let pts: Vec<(f64, f64)> = (0..n)
.flat_map(|i| {
(0..n).map(move |j| (i as f64 / (n - 1) as f64, j as f64 / (n - 1) as f64))
})
.collect();
let est = HausdorffDimensionEstimator::new(pts);
let dim = est.estimate_dimension(5);
assert!(dim > 1.5 && dim < 2.5, "Expected dim ≈ 2, got {}", dim);
}
#[test]
fn test_build_env_has_extended_axioms() {
let mut env = Environment::new();
build_measure_theory_env(&mut env);
for name in &[
"BochnerIntegral",
"POVM",
"QuantumState",
"VonNeumannAlgebra",
"ChoquetSimplex",
"Varifold",
"Current",
"RectifiableSet",
"SelfSimilarMeasure",
] {
assert!(
env.contains(&Name::str(*name)),
"Missing extended axiom: {}",
name
);
}
}
}
#[cfg(test)]
mod extended_measure_tests {
use super::*;
#[test]
fn test_signed_measure() {
let sm = SignedMeasure::jordan("P", "N", 3.0);
assert!(sm.is_positive());
assert!(sm.hahn_decomposition().contains("P=P"));
}
#[test]
fn test_abstract_integral() {
let leb = AbstractIntegral::lebesgue("R");
assert!(leb.is_lebesgue);
assert!(leb.description().contains("Lebesgue"));
}
#[test]
fn test_martingale() {
let mg = Martingale::true_martingale("F_t");
assert!(!mg.is_sub);
assert!(mg.optional_stopping_applies(true));
assert!(!mg.optional_stopping_applies(false));
}
#[test]
fn test_fubini() {
let f = FubiniData::fubini("X", "Y");
assert!(f.can_interchange());
}
#[test]
fn test_convergence_theorems() {
let dct = ConvergenceTheoremType::DominatedConvergence;
assert_eq!(dct.name(), "DCT");
let mct = ConvergenceTheoremType::MonotoneConvergence;
assert_eq!(mct.name(), "MCT");
}
#[test]
fn test_radon_nikodym() {
let rn = RadonNikodymData::new("nu", "mu");
assert!(rn.derivative_exists());
assert!(rn.density_name.contains("dnu"));
}
#[test]
fn test_disintegration() {
let d = MeasureDisintExt::standard("X", "Y");
assert!(d.rokhlin_applies());
}
}
#[cfg(test)]
mod extended_measure_tests2 {
use super::*;
#[test]
fn test_capacity() {
let cap = Capacity::newtonian();
assert!(cap.is_submodular);
assert!(cap.is_measure());
assert!(cap.choquet_integral_description().contains("Choquet"));
}
#[test]
fn test_lusin() {
let l = LusinData::new("R", "f", 0.01);
let desc = l.compact_set_description();
assert!(desc.contains("μ(K)"));
}
#[test]
fn test_egoroff() {
let e = EgoroffData::new("f_n", "f", 1.0);
assert!(e.description().contains("Egoroff"));
}
#[test]
fn test_independence() {
let ind = Independence::new(
vec!["A".to_string(), "B".to_string(), "C".to_string()],
true,
);
assert!(ind.mutually_independent_implies_pairwise());
assert_eq!(ind.count(), 3);
}
#[test]
fn test_conditional_expectation() {
let ce = ConditionalExpectation::new("X", "G");
assert!(ce.is_tower_property);
assert!(ce.tower_description().contains("tower") || ce.tower_description().contains("E["));
}
}