"""Determines if the score (zeta(w)_Delta in the article) of every Weyl
group element is positive or not, for every distinguished rho of E8.

Example usage (this will take a long time):
>> python3 pos-check-parallel-E8.py
>> nohup python3 pos-check-parallel-E8.py > E8.txt &

Authors: Alexander Bertoloni Meli, Teruhisa Koshikawa, Jonathan Leake 
Date: August 2024
"""

from sage.all import *
from multiprocessing import Pool
import time
import sys

# Determine the weight of a given root based on rho
def eval_rho(rho, root):
  return sum(coeff * rho[delta-1] for (delta, coeff) in root)

# Generator function for V_k
def gen_V(k, rho, roots):
  for r in roots:
    if eval_rho(rho, r) == k:
      yield r

# Absolute value of a given root
def root_abs(root):
  return root.map_coefficients(abs)

def compute_score(V2, V0, w):
  score = 0

  for p in V2:
    score += root_abs(w.action(p))

  for p in V0:
    score -= 2 * root_abs(w.action(p))

  return score

def is_pos(score):
  if any(x <= 0 for x in score.dense_coefficient_list()):
    return False
  else:
    return True
  
def has_pos_score(V2, V0, w):
  return is_pos(compute_score(V2, V0, w))

# Distinguished rho for E8
rholist = [
  (0,0,0,0,2,0,0,0),
  (0,0,0,2,0,0,0,2),
  (0,0,0,2,0,0,2,0),
  (0,0,0,2,0,0,2,2),
  (2,0,0,2,0,0,2,0),
  (2,0,0,2,0,0,2,2),
  (2,0,0,2,0,2,0,2),
  (2,0,0,2,0,2,2,2),
  (2,2,2,0,2,0,2,2),
  (2,2,2,0,2,2,2,2),
  (2,2,2,2,2,2,2,2)
]

print("Initializing...")
sys.stdout.flush()

starttime = time.time()

# This code is specific to E8, since it takes so much longer.
sys_type = "E"
n = 8

R = RootSystem([sys_type, n])
L = R.root_lattice()
W = L.weyl_group()
P = L.positive_roots()
Delta = L.simple_roots()

V2_list = [list(gen_V(2, rho, P)) for rho in rholist]
V0_list = [list(gen_V(0, rho, P)) for rho in rholist]

# In this case we check all distinguished rhos at once, and thus this function
# returns a list of booleans corresponding to each distinguished rho.
def pos_helper(w):
  out = [False] * len(rholist)

  for rho_idx in range(len(rholist)):
    out[rho_idx] = has_pos_score(V2_list[rho_idx], V0_list[rho_idx], w)

  return out

pos = True
counter = 0

# Parallelize the computation to make it run faster
with Pool(maxtasksperchild=1) as pool:
  # This means we apply the function pos_helper to every element of W
  # in a parallel way, and store the output in reslist.
  reslist = pool.imap_unordered(pos_helper, W, chunksize=1000000)
  pool.close()

  print("DONE, time elapsed (sec): " + str(time.time()-starttime))
  sys.stdout.flush()

  for result in reslist:
    counter += 1

    # Print out progress statement for every million Weyl group elements
    if counter % 1000000 == 0:
      print(str(counter) + " Weyl elements checked, time elapsed (sec): " + str(time.time()-starttime))
      sys.stdout.flush()

    # If some element of result is False, then it means some Weyl element has
    # non-positive score for some rho in rholist; in this case we terminate
    # since we expect this never to happen for the distinguished rhos
    if not all(result):
      pool.terminate()
      bad_rho = rholist[result.index(False)]
      pos = False
      break

  #pool.join()

if pos:
  for rho in rholist:
    print(str(rho) + " YES")
else:
  print(str(bad_rho) + " NO")

endtime = time.time()

print("Total time elapsed (sec): " + str(endtime-starttime))
