#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BrownInvariants {
pub beta: u128,
pub rank: usize,
pub radical_dim: usize,
pub radical_anisotropic: bool,
}
impl BrownInvariants {
pub fn display(&self) -> String {
self.to_string()
}
}
impl std::fmt::Display for BrownInvariants {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let rad = if self.radical_dim > 0 {
let aniso = if self.radical_anisotropic {
" aniso"
} else {
""
};
format!(" ⊗̂ Λ({}^{}{})", "F", self.radical_dim, aniso)
} else {
String::new()
};
write!(f, "β={} rank={}{}", self.beta, self.rank, rad)
}
}
fn above(i: usize) -> u128 {
if i >= 127 {
0
} else {
(!0u128) << (i + 1)
}
}
fn q_of4(x: u128, q4: &[u128], bmat: &[u128]) -> u128 {
let mut lin = 0u128;
let mut cross = 0u32; let mut vv = x;
while vv != 0 {
let i = vv.trailing_zeros() as usize;
vv &= vv - 1;
lin += q4[i] % 4;
let inter = bmat[i] & x & above(i);
cross ^= inter.count_ones() & 1;
}
(lin + 2 * cross as u128) % 4
}
fn b_pair(x: u128, y: u128, full_b: &[u128]) -> bool {
let mut parity = 0u32;
let mut xx = x;
while xx != 0 {
let i = xx.trailing_zeros() as usize;
xx &= xx - 1;
parity ^= (full_b[i] & y).count_ones() & 1;
}
parity == 1
}
fn xor_insert(basis: &mut [Option<u128>; 128], mut v: u128) -> bool {
while v != 0 {
let c = v.trailing_zeros() as usize;
match basis[c] {
None => {
basis[c] = Some(v);
return true;
}
Some(b) => v ^= b,
}
}
false
}
fn radical_basis(full_b: &[u128], n: usize) -> Vec<u128> {
let mut piv: [Option<(u128, u128)>; 128] = [None; 128]; let mut null = Vec::new();
for i in 0..n {
let mut row = full_b[i];
let mut prov = 1u128 << i;
loop {
if row == 0 {
null.push(prov);
break;
}
let c = row.trailing_zeros() as usize;
match piv[c] {
None => {
piv[c] = Some((row, prov));
break;
}
Some((r, p)) => {
row ^= r;
prov ^= p;
}
}
}
}
null
}
fn core_complement_basis(radical: &[u128], n: usize) -> Vec<u128> {
let mut lin: [Option<u128>; 128] = [None; 128];
for &x in radical {
xor_insert(&mut lin, x);
}
let mut basis = Vec::new();
for i in 0..n {
if xor_insert(&mut lin, 1u128 << i) {
basis.push(1u128 << i);
}
}
basis
}
fn reduce_brown_core(mut basis: Vec<u128>, q4: &[u128], bmat: &[u128], full_b: &[u128]) -> u128 {
let mut beta = 0u128;
while !basis.is_empty() {
if let Some(p) = basis.iter().position(|&v| b_pair(v, v, full_b)) {
let v = basis.swap_remove(p);
let qv = q_of4(v, q4, bmat);
debug_assert!(qv == 1 || qv == 3);
beta = (beta + if qv == 1 { 1 } else { 7 }) % 8;
for w in &mut basis {
if b_pair(*w, v, full_b) {
*w ^= v;
}
}
continue;
}
let v = basis
.pop()
.expect("nonempty basis already checked before even-plane reduction");
let p = basis
.iter()
.position(|&w| b_pair(v, w, full_b))
.expect("a nonsingular alternating core has a symplectic partner");
let w = basis.swap_remove(p);
let qv = q_of4(v, q4, bmat);
let qw = q_of4(w, q4, bmat);
debug_assert!(qv == 0 || qv == 2);
debug_assert!(qw == 0 || qw == 2);
if qv == 2 && qw == 2 {
beta = (beta + 4) % 8;
}
for x in &mut basis {
let xv = b_pair(*x, v, full_b);
let xw = b_pair(*x, w, full_b);
if xw {
*x ^= v;
}
if xv {
*x ^= w;
}
}
}
beta
}
pub(crate) fn beta_from_gauss(re: i128, im: i128) -> Option<u128> {
match (re.signum(), im.signum()) {
(0, 0) => None, (s, 0) => Some(if s > 0 { 0 } else { 4 }),
(0, s) => Some(if s > 0 { 2 } else { 6 }),
(sr, si) => {
if re.unsigned_abs() != im.unsigned_abs() {
return None;
}
Some(match (sr, si) {
(1, 1) => 1,
(-1, 1) => 3,
(-1, -1) => 5,
(1, -1) => 7,
_ => unreachable!("nonzero signums already matched"),
})
}
}
}
pub fn brown_f2(n: usize, q4: &[u128], bmat: &[u128]) -> BrownInvariants {
assert!(
n <= 128,
"brown_f2 uses u128 bitmasks, so n must be at most 128"
);
assert!(
q4.len() >= n && bmat.len() >= n,
"brown_f2 needs q4 and bmat entries for every basis vector"
);
let full_b: Vec<u128> = (0..n)
.map(|i| {
let off = bmat[i] & !(1u128 << i);
off | (if q4[i] % 2 == 1 { 1u128 << i } else { 0 })
})
.collect();
let radical = radical_basis(&full_b, n);
let radical_dim = radical.len();
let radical_anisotropic = radical.iter().any(|&x| q_of4(x, q4, bmat) == 2);
let core = core_complement_basis(&radical, n);
let rank = core.len(); let beta = reduce_brown_core(core, q4, bmat, &full_b);
BrownInvariants {
beta,
rank,
radical_dim,
radical_anisotropic,
}
}
pub fn double_f2(qd: &[bool], bmat: &[u128]) -> BrownInvariants {
let q4: Vec<u128> = qd.iter().map(|&b| if b { 2 } else { 0 }).collect();
brown_f2(qd.len(), &q4, bmat)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::forms::arf_f2;
fn brown_f2_by_enumeration(n: usize, q4: &[u128], bmat: &[u128]) -> BrownInvariants {
let full_b: Vec<u128> = (0..n)
.map(|i| {
let off = bmat[i] & !(1u128 << i);
off | (if q4[i] % 2 == 1 { 1u128 << i } else { 0 })
})
.collect();
let radical = radical_basis(&full_b, n);
let radical_dim = radical.len();
let radical_anisotropic = radical.iter().any(|&x| q_of4(x, q4, bmat) == 2);
let core = core_complement_basis(&radical, n);
let rank = core.len();
assert!(
rank < 128,
"test-only enumeration needs a representable 2^rank loop bound"
);
let mut counts = [0i128; 4];
for mask in 0u128..(1u128 << rank) {
let mut x = 0u128;
for (b, &v) in core.iter().enumerate() {
if (mask >> b) & 1 == 1 {
x ^= v;
}
}
counts[q_of4(x, q4, bmat) as usize] += 1;
}
let re = counts[0] - counts[2];
let im = counts[1] - counts[3];
BrownInvariants {
beta: beta_from_gauss(re, im).expect("a nonsingular core has an eighth-root Gauss sum"),
rank,
radical_dim,
radical_anisotropic,
}
}
fn ortho_sum(
(n1, q1, b1): (usize, &[u128], &[u128]),
(n2, q2, b2): (usize, &[u128], &[u128]),
) -> (usize, Vec<u128>, Vec<u128>) {
let n = n1 + n2;
let mut q4 = q1.to_vec();
q4.extend_from_slice(q2);
let mut bmat = vec![0u128; n];
bmat[..n1].copy_from_slice(b1);
for i in 0..n2 {
bmat[n1 + i] = b2[i] << n1; }
(n, q4, bmat)
}
#[test]
fn one_dimensional_generators() {
assert_eq!(brown_f2(1, &[1], &[0]).beta, 1);
assert_eq!(brown_f2(1, &[3], &[0]).beta, 7);
}
#[test]
fn order_eight_relation() {
let r = brown_f2(8, &[1; 8], &[0; 8]);
assert_eq!(r.beta, 0);
assert_eq!((r.rank, r.radical_dim), (8, 0));
}
#[test]
fn split_objects_vanish() {
assert_eq!(brown_f2(2, &[1, 3], &[0, 0]).beta, 0);
assert_eq!(brown_f2(2, &[0, 0], &[0b10, 0b01]).beta, 0);
}
#[test]
#[allow(clippy::type_complexity)] fn double_is_four_times_arf() {
let cases: &[(&[bool], &[(usize, usize)])] = &[
(&[false, false], &[(0, 1)]), (&[true, true], &[(0, 1)]), (&[false, false, false, false], &[(0, 1), (2, 3)]), (&[false, false, true, true], &[(0, 1), (2, 3)]), (&[true, true, true, true], &[(0, 1), (2, 3)]), ];
for (qd, pairs) in cases {
let n = qd.len();
let mut bmat = vec![0u128; n];
for &(i, j) in *pairs {
bmat[i] |= 1 << j;
bmat[j] |= 1 << i;
}
let arf = arf_f2(n, qd, &bmat).arf;
let beta = double_f2(qd, &bmat).beta;
assert_eq!(beta, 4 * arf, "β(2q′) ≠ 4·Arf for q={qd:?}");
assert!(beta == 0 || beta == 4);
}
}
#[test]
fn beta_is_additive_under_orthogonal_sum() {
let comps: &[(usize, &[u128], &[u128])] = &[
(1, &[1], &[0]), (1, &[3], &[0]), (1, &[2], &[0]), (2, &[0, 0], &[0b10, 0b01]), (2, &[1, 1], &[0b10, 0b01]), ];
for a in comps {
for b in comps {
let ba = brown_f2(a.0, a.1, a.2).beta;
let bb = brown_f2(b.0, b.1, b.2).beta;
let (n, q4, bmat) = ortho_sum(*a, *b);
let bab = brown_f2(n, &q4, &bmat).beta;
assert_eq!(bab, (ba + bb) % 8, "additivity failed for {a:?} ⊥ {b:?}");
}
}
}
#[test]
fn anisotropic_radical_is_detected() {
let r = brown_f2(3, &[0, 0, 2], &[0b10, 0b01, 0]);
assert_eq!(
(r.beta, r.rank, r.radical_dim, r.radical_anisotropic),
(0, 2, 1, true)
);
let r0 = brown_f2(3, &[0, 0, 0], &[0b10, 0b01, 0]);
assert_eq!(
(r0.beta, r0.rank, r0.radical_dim, r0.radical_anisotropic),
(0, 2, 1, false)
);
}
#[test]
fn reduction_matches_enumeration_on_all_four_dimensional_inputs() {
for qmask in 0u128..(1u128 << 8) {
let q4: Vec<u128> = (0..4).map(|i| (qmask >> (2 * i)) & 0b11).collect();
for edges in 0u128..(1u128 << 6) {
let mut bmat = vec![0u128; 4];
let mut bit = 0;
for i in 0..4 {
for j in (i + 1)..4 {
if (edges >> bit) & 1 == 1 {
bmat[i] |= 1u128 << j;
bmat[j] |= 1u128 << i;
}
bit += 1;
}
}
assert_eq!(
brown_f2(4, &q4, &bmat),
brown_f2_by_enumeration(4, &q4, &bmat),
"reduction/enumeration mismatch for q={q4:?}, b={bmat:?}"
);
}
}
}
#[test]
fn reduction_matches_old_budget_edge() {
let q4 = vec![1u128; 26];
let bmat = vec![0u128; 26];
assert_eq!(
brown_f2(26, &q4, &bmat),
brown_f2_by_enumeration(26, &q4, &bmat)
);
}
#[test]
fn brown_f2_reduces_past_the_old_enumeration_budget() {
let q4 = vec![1u128; 40];
let bmat = vec![0u128; 40];
let r = brown_f2(40, &q4, &bmat);
assert_eq!(
(r.beta, r.rank, r.radical_dim, r.radical_anisotropic),
(0, 40, 0, false)
);
}
}