from itertools import combinations, product import sympy as sp import pytest from noulith import TwistedAlgebra, bits, partitions, ideal_generators, equal_polynomial_ideals from noulith.geometry import * def test_refinement_identity_symbolic_four_channels(): pairs=list(combinations(range(4),2)); params=sp.symbols('q0:6') alg=TwistedAlgebra(4,dict(zip(pairs,params))); sh=alg.shuffle_table() for mask in range(1,16): for a in range(1,mask.bit_count()): total=0 for sub in range(1,16): if sub & mask == sub and sub.bit_count()==a: other=mask^sub _,weight=alg.monomial_product(sub,other) total+=weight*sh[sub]*sh[other] assert sp.expand(total-sh[mask])==0 def test_two_deficit_generator_formula(): a,b,c,d,e,f=sp.symbols('a b c d e f') pairs=list(combinations(range(4),2)) alg=TwistedAlgebra(4,{ij:-1+x for ij,x in zip(pairs,[a,b,c,d,e,f])}) sh=alg.shuffle_table() full=15 motifs=[sh[mask] for mask in range(16) if mask.bit_count()==3] motifs += [a*f,b*e,c*d] actual=ideal_generators(sh,full,2) assert equal_polynomial_ideals(actual,motifs,[a,b,c,d,e,f]) @pytest.mark.parametrize('m',range(2,9)) @pytest.mark.parametrize('seed',[1,2,3]) def test_pfaffian_tangent_theorem_exact(m,seed): t=sp.Symbol('t'); pairs=list(combinations(range(m),2)) edges={pair:sp.Integer(((i+seed)*7)%13-6) for i,pair in enumerate(pairs)} alg=TwistedAlgebra(m,{pair:-1+t*v for pair,v in edges.items()}) polynomial=sp.Poly(alg.shuffle_table()[-1],t) k=m//2 assert all(polynomial.coeff_monomial(t**j)==0 for j in range(k)) matrix=sp.zeros(m) for (i,j),v in edges.items(): matrix[i,j]=v;matrix[j,i]=-v assert sp.expand(polynomial.coeff_monomial(t**k)-leading_shuffle_form(matrix))==0 def test_formal_group_axioms_and_differential(): x,y,z=sp.symbols('x y z');F=resonance_group assert sp.cancel(F(F(x,y),z)-F(x,F(y,z)))==0 assert F(x,0)==x and sp.cancel(F(x,resonance_inverse(x)))==0 assert sp.cancel(sp.diff(F(x,y),y).subs(y,0)-(1-x+x*x))==0 def test_flat_triangle_identity_symbolic(): a,b,c=sp.symbols('a b c') A=flat_edge(a,b);B=flat_edge(a,c);C=flat_edge(b,c) assert sp.cancel(A*B*C-A*C+A-B+C)==0 @pytest.mark.parametrize('N',range(3,7)) def test_line_arrangement_generators_and_hilbert_function(N): z=sp.symbols(f'z1:{N+1}'); all_z=(sp.S.Zero,)+z motifs=[] edges=list(combinations(range(N+1),2)) for a,e in enumerate(edges): for f in edges[a+1:]: if not set(e)&set(f): motifs.append((all_z[e[1]]-all_z[e[0]])*(all_z[f[1]]-all_z[f[0]])) generators=line_arrangement_generators(z) assert equal_polynomial_ideals(motifs,generators,z) groebner=sp.groebner(generators,*z) leading=[poly.LM(order=groebner.order).exponents for poly in groebner.polys] for degree in range(6): count=0 for exponents in product(range(degree+1),repeat=N): if sum(exponents)==degree and not any(all(a>=b for a,b in zip(exponents,lead)) for lead in leading): count+=1 assert count==([1,N][degree] if degree<2 else N+1) @pytest.mark.parametrize('outlier',range(5)) def test_all_five_exact_resonance_branches(outlier): t=sp.Symbol('t');h=[0]*5;h[outlier]=t f={ij:flat_edge(h[ij[0]],h[ij[1]]) for ij in combinations(range(5),2)} for i,j,k in combinations(range(5),3): A,B,C=f[i,j],f[i,k],f[j,k] assert sp.cancel(A*B*C-A*C+A-B+C)==0 for e,g in combinations(f,2): if not set(e)&set(g): assert sp.cancel(f[e]*f[g])==0 def test_triangle_flatness_alone_is_insufficient(): t=sp.Symbol('t');h=[0,t,2*t,3*t] f={ij:flat_edge(h[ij[0]],h[ij[1]]) for ij in combinations(range(4),2)} for i,j,k in combinations(range(4),3): A,B,C=f[i,j],f[i,k],f[j,k] assert sp.cancel(A*B*C-A*C+A-B+C)==0 assert sp.cancel(f[0,1]*f[2,3])!=0 @pytest.mark.parametrize('m',[4,5,6]) @pytest.mark.parametrize('stratum,defect',[ ('generic',0),('single_star',1),('outlier_branch',2),('two_stars',2),('origin',None)]) def test_second_deficit_full_rank_strata(m,stratum,defect): from noulith.protocols import normalized_measurement_matrix f={pair:sp.S.Zero for pair in combinations(range(m),2)} if stratum=='generic': f={pair:sp.Rational(1,10) for pair in f} elif stratum=='single_star': for j in range(1,m): f[(0,j)]=sp.Rational(j,10*m) elif stratum=='outlier_branch': for j in range(1,m): f[(0,j)]=sp.Rational(1,10) elif stratum=='two_stars': f[(0,1)]=sp.Rational(1,10) algebra=TwistedAlgebra(m,{pair:-1+value for pair,value in f.items()}) matrix=normalized_measurement_matrix(algebra.shuffle_table(),m-2) # The frame has disjoint one-coordinate row supports; column nonzeroness is # therefore its exact rank, without floating-point thresholding. rank=sum(any(value!=0 for value in matrix[:,i]) for i in range(2**m)) expected=2**m-(m+1 if stratum=='origin' else defect) assert rank==expected if m==4: assert matrix.rank()==expected @pytest.mark.parametrize('N',range(3,9)) def test_branch_carriers_and_normalization_conductor(N): z=sp.symbols(f'z0:{N}') t=sp.symbols('t') carriers=branch_carriers(z) branches=[{v:(t if j==i else 0) for j,v in enumerate(z)} for i in range(N)] branches.append({v:t for v in z}) evaluations=sp.Matrix([[sp.expand(g.subs(branch)) for branch in branches] for g in carriers]) assert evaluations==t*t*sp.eye(N+1) @pytest.mark.parametrize('N',range(3,7)) def test_perfect_artinian_slice_pairing(N): z=sp.symbols(f'z0:{N}') G=sp.groebner(line_arrangement_generators(z)+[sum(z)],*z) h=z[0]*z[1] assert G.reduce(h)[1]!=0 pairing=perfect_slice_matrix(N) for i in range(N-1): for j in range(N-1): assert G.reduce(z[i]*z[j]-pairing[i,j]*h)[1]==0 assert sp.factor(pairing.det())==-(-N)**(N-2)