AplK/test_battery.py
2024-08-22 02:12:40 +02:00

291 lines
10 KiB
Python

from sage.all import *
import itertools
load('aplK.py')
load("minimalModel_parallel.py")
#S = simplicial_complexes.K3Surface()
#S.set_immutable()
#cochainComplex = S.chain_complex(base_ring=QQ).dual()
#A = AlgebraAplK(S)
#smodel = load("sullivan_model_K3_deg3.sobj")
#smorphism = load("model_morphism_K3_deg3.sobj")
######### TEST INTEGRAND/INTEGRAL APLn
"""
N=10
for dim in range(10):
print("Start test at dimension ", dim, flush=True)
s = SimplicialAlgebraplN(dim)
for i in range(N):
value = randint(-20,20);
p = s.integrand(value);
#print(p);
equal = p.integral() == value
if not equal:
raise Exception("The integral is not equal")
else:
None
#print("ok", flush=True)
print("Test finished")
"""
######### TEST SOLVE PRIMITIVE WITH RANDOM COCHAINS
"""
S = simplicial_complexes.Sphere(4)
#S = simplicial_complexes.K3Surface()
S.set_immutable()
A = AlgebraAplK(S)
cochainComplex = S.chain_complex(base_ring=QQ).dual()
N = 4
for dim in range(1,4):
for r in range(N):
print("Working on dimension ", dim, flush=True)
v = random_vector(QQ, len(S.n_cells(dim)))
print("Lifting random cochain", flush=True)
a = A.lift_cochain(A.vector_to_cochain(v,dim))
print("Checking Apl(K) structure", flush=True)
a.check()
#if a.check():
# print("lift ok", flush=True)
da = a.differential()
print("Checking Apl(K) structure of the differential", flush=True)
da.check()
#if da.check():
# print("da ok", flush=True)
#p_basis = da._get_primitive_basis()
print("Solving primitive", flush=True)
b = da.solve_primitive()
print("Checking Apl(K) structure of the solution found", flush=True)
b.check()
#if b.check():
# print("solve_primitive ok", flush=True)
print(b.differential()==da)
print("Test INTEGRAL/DIFFERENTIAL COMMUTATION", flush=True)
Diff = cochainComplex.differential(dim)
cb = A.cochain_to_vector(b.integral(), dim)
cda = A.cochain_to_vector(da.integral(), dim + 1)
equal = Diff*cb == cda
if not equal:
raise ValueError("the cochains are not equal at degree " + str(degree))
else:
print("OK", flush=True)
"""
def zip_with_constants(a, b, c, l):
return [(a, b, c, i) for i in l]
@parallel(ncpus=Integer(24))
def check_solve_primitive(A, dim, Diff, v):
#print("Lift random cochain to Apl(K)", flush=True)
a = A.lift_cochain(A.vector_to_cochain(v,dim))
#print("Checking Apl(K) structure of the random element ", flush=True)
a.check()
#print(a.check(), flush=True)
da = a.differential()
b = da.solve_primitive()
print("Checking Apl(K) structure of the primitive element ", flush=True)
b.check()
print("Checking b.differential()==da ", flush=True)
equal = b.differential()==da
if not equal:
raise ValueError("the differential is not equal")
return -1
cb = A.cochain_to_vector(b.integral(), dim)
cda = A.cochain_to_vector(da.integral(), dim + 1)
equal = Diff*cb == cda
if not equal:
raise ValueError("the cochains are not equal at degree " + str(dim))
return 0
#S = simplicial_complexes.Sphere(3)
S = simplicial_complexes.K3Surface()
S.set_immutable()
A = AlgebraAplK(S)
cochainComplex = S.chain_complex(base_ring=QQ).dual()
N = 96
for dim in range(1,4):
Diff = cochainComplex.differential(dim)
random_cochains = [random_vector(QQ, len(S.n_cells(dim))) for i in range(N)]
data = zip_with_constants(A, dim, Diff, random_cochains)
print("Start parallel tests at dimension " + str(dim), flush=True)
results = [i[1] for i in check_solve_primitive(data)]
print("Test finished", flush=True)
######### RUN MINIMAL MODEL ALGORITHM
"""
[sullivan_model, model_morphism, cohomology_correspondence] = minimal_model(S, max_depth=12000, max_dim=3)
print(sullivan_model, flush=True)
save(sullivan_model, "sullivan_model_K3_deg3")
save(model_morphism,"model_morphism_K3_deg3")
save(cohomology_correspondence,"cohomology_correspondence_K3_deg3")
"""
######### TEST APLK STRUCTURE
"""
@parallel(ncpus=Integer(24))
def check(gen, aplk):
print("Checking the aplk element " + str(gen), flush=True)
aplk.check()
return 0
for k in smorphism.keys():
elements = list(smorphism[k].items())
print("Start parallel check", flush=True)
results = [i[1] for i in check(elements)]
"""
########## TEST DIFFERENTIAL/PHI COMMUTING
"""
for degree in range(2,4):
for g in smodel.basis(degree):
phi_g = smorphism[degree][str(g)]
d_phi_g = phi_g.differential()
dg = g.differential()
phi_d_g = phi_polinomial(dg, smorphism, A)
equal = phi_d_g == d_phi_g
if not equal:
raise ValueError("(phi o dif)(" + str(g) + ") != (dif o phi)(" + str(g) + ")")
else:
print("(phi o dif)(" + str(g) + ") == (dif o phi)(" + str(g) + ")", flush=True)
"""
######### TEST LIFTING/INTEGRAL RANDOM COCHAINS
"""
def zip_with_constants(a, b, l):
return [(a, b, i) for i in l]
@parallel(ncpus=Integer(24))
def check_lifting(A, dim, v):
print("Lift random cochain to Apl(K)", flush=True)
a = A.lift_cochain(A.vector_to_cochain(v,dim))
print("Checking Apl(K) structure of the random element ", flush=True)
a.check()
print("Checking if the integral is equal to the original cochain", flush=True)
equal = A.cochain_to_vector(a.integral(), dim) == v
if not equal:
raise ValueError("the cochains are not equal")
return -1
else:
print("OK", flush=True)
return 0
N = 100
for dim in range(5):
random_cochains = [random_vector(QQ, len(S.n_cells(dim))) for i in range(N)]
data = zip_with_constants(A, dim, random_cochains)
print("Start parallel lifting at dimension " + str(dim), flush=True)
results = [i[1] for i in check_lifting(data)]
print("Finished battery test", flush=True)
"""
######### TEST DIFFERENTIAL/INTEGRAL COMMUTATION APLn
"""
def random_pol(apln):
n = apln.n
coef = randint(1,10)
exp = tuple([randint(0,5) for i in range(n)] + [0]*n)
gens = apln.gens()
return apln.get_term(apln, coef, exp, gens)
n = 5
s = SimplicialAlgebraplN(n)
s.inject_variables()
#cofaces = [x for x in list(powerset(range(n+1)) if len(x) == n]
dp = random_pol(s)*prod(s.gens()[s.n+2:])
print("dp: ",dp, flush=True)
p = dp.solve_primitive().reduce()
fd = sum([((-1)**f)*p.face(f).integral() for f in range(n+1)])
print("sum([((-1)**f)*p.face(f).integral() for f in range(n+1)])== dp.integral(): ", fd == dp.integral(), flush=True)
for f in range(n+1):
coface = list(range(n+2))
coface.remove(f)
q = dp.propagate(coface,n+1)
dq = q.differential().reduce()
#p = dp.solve_primitive().reduce()
#print("p.differential().reduce() == dp.reduce()", p.differential().reduce() == dp.reduce(), flush=True)
#print("q.face("+str(f)+") == p", flush=True)
#print(q.face(f).reduce() == p, flush=True)
print("(-1)**"+str(f)+")*p.face().integral() == dq.integral(): ", ((-1)**f)*dp.integral() == dq.integral(), flush=True)
#print(p.integral() == dq.integral(), flush=True)
"""
######### TEST DIFFERENTIAL/INTEGRAL COMMUTATION APL(K)
"""
def zip_with_constants(a,s,c, l):
return [(a,s,c, i) for i in l]
@parallel(ncpus=Integer(24))
def check_dif_int(A, S, cochainComplex, v):
#print("Lifting random cochain dim ", dim, flush=True)
a = A.lift_cochain(A.vector_to_cochain(v,dim))
a.check() # the method raises an error if the aplk element is not properly built (i.e, the faces do not match)
#print("Check Apl(K) structure of the lifted element: ",a.check(), flush=True)
da = a.differential()
Diff = cochainComplex.differential(dim)
cda = A.cochain_to_vector(da.integral(),dim+1)
equal = Diff*v == cda
#print("Diff*v == cda.integral()", equal, flush=True)
if not equal:
raise Exception("the integrals are not equal ", flush=True)
return -1
for cell in S.n_cells(dim+1):
d = da.elements[cell.tuple()]
#s = (-1)**(dim*(dim-1)//2) # [FHT, RHT, p.129]
di = sum([((-1)**(f))*a.elements[cell.face(f).tuple()].integral() for f in range(cell.dimension()+1)])
equal = di == d.integral()
if equal:
None
#print("di == d.integral(): ", equal, flush=True)
else:
raise Exception("the integrals are not equal ", flush=True)
return -1
return 0
S = simplicial_complexes.Sphere(5)
#S = simplicial_complexes.K3Surface()
S.set_immutable()
A = AlgebraAplK(S)
cochainComplex = S.chain_complex(base_ring=QQ).dual()
N = 1
for dim in range(7):
l = [random_vector(QQ, len(S.n_cells(dim))) for i in range(N)]
data = zip_with_constants(A, S, cochainComplex, l)
print("Start parallel test at dimension " + str(dim), flush=True)
results = [i[1] for i in check_dif_int(data)]
print("Test finished", flush=True)
"""
######### TEST DIFFERENTIAL/INTEGRAL COMMUTATION WITH RANDOM COCHAINS
"""
def zip_with_constants(a, b, c, l):
return [(a, b, c, i) for i in l]
@parallel(ncpus=Integer(24))
def check(A, Diff, degree, cochain):
#PARAMS:
# - Diff: Differential at dimension "dim"
# - f: tuple (str,Apl(K))
# -
f = A.lift_cochain(A.vector_to_cochain(cochain,degree))
#print("Checking (DoI)f == (IoD)f ", flush=True)
cf = A.cochain_to_vector(f.integral(), degree)
df = f.differential()
cdf = A.cochain_to_vector(df.integral(), degree + 1)
equal = Diff*cf == cdf
if not equal:
#raise ValueError("the cochains are not equal at degree " + str(degree))
print("the cochains are not equal at degree " + str(degree))
return -1
#else:
# print("OK", flush=True)
return 0
N = 10
for degree in range(5):
random_cochains = [random_vector(QQ, len(S.n_cells(degree))) for i in range(N)]
Diff = cochainComplex.differential(degree)
data = zip_with_constants(A, Diff, degree, random_cochains)
print("Start parallel DIFF/INTEGRAL at degree " + str(degree), flush=True)
results = [i[1] for i in check(data)]
print("Finished battery test", flush=True)
"""