def main(): n = 12 g1 = Perm.trans(1, 2, n) g2 = Perm.cycle(1, n, n) k1 = np.kron(g1.mat(), g1.mat()) k2 = np.kron(g2.mat(), g2.mat()) irrep_mults = { (n - 2, 1, 1): 1, (n - 2, 2): 1, (n - 1, 1): 3, (n, ): 2, } irreps = {i: SnIrrep(i) for i in irrep_mults.keys()} intws = {} _st = time.time() for part, mult in irrep_mults.items(): st = time.time() rho = irreps[part] mult = irrep_mults[part] c = intertwine(k1, k2, rho(g1), rho(g2), mult) intws[part] = c end = time.time() print('Done with {} | Part elapsed: {:.2f} | Total elapsed: {:.2f}s'. format(part, end - st, time.time() - _st)) pdb.set_trace()
def test_cycle_decomp(self): g = Perm.cycle(1, 3, 10) h = Perm.trans(5, 7, 10) f = Perm.trans(4, 6, 10) ghf = g * h * f self.assertEqual(ghf.cycle_decomposition, [[1, 2, 3], [4, 6], [5, 7]])
def rand_perm(n, steps=100): gens = [Perm.trans(i, i + 1, n) for i in range(1, n)] gens.append(Perm.cycle(1, n, n)) p = Perm.eye(n) for i in range(steps): p = random.choice(gens) * p return p
def coset_reps_2(n): reps = [] for i in range(1, n + 1): pi = Perm.cycle(i, n, n) for j in range(1, n): pj = Perm.cycle(j, n - 1, n) pij = pi * pj reps.append(pij) return reps
def sn_minus2_coset(n): reps = [] for i in range(1, n + 1): pi = Perm.cycle(i, n, n) for j in range(1, n): pj = Perm.cycle(j, n - 1, n) reps.append(pi * pj) return reps
def cos_reps_minus2_pairs(n): reps_i = [] reps_j = [] for i in range(1, n + 1): pi = Perm.cycle(i, n, n) reps_i.append(pi) for j in range(1, n): pj = Perm.cycle(j, n - 1, n) reps_j.append(pj) return reps_i, reps_j
def cos_reps_minus2(n): ''' n: int Returns: list of coset representatives for S_n / S_{n-2} ''' reps = [] for i in range(1, n + 1): pi = Perm.cycle(i, n, n) for j in range(1, n): pj = Perm.cycle(j, n - 1, n) reps.append(pi * pj) return reps
def test_s3(self): partition = (3, 1) rho = SnIrrep(partition, fmt='dense') p12 = np.array([[-1, 0, 0], [0, 1, 0], [0, 0, 1]]) p23 = np.array([[0.5, np.sqrt(3) / 2., 0], [np.sqrt(3) / 2., -0.5, 0], [0, 0, 1]]) p34 = np.array([[1, 0, 0], [0, 1. / 3., np.sqrt(8) / 3], [0, np.sqrt(8) / 3, -1. / 3.]]) self.assertTrue(np.allclose(p12, rho(Perm.trans(1, 2, 4)))) self.assertTrue(np.allclose(p23, rho(Perm.trans(2, 3, 4)))) self.assertTrue(np.allclose(p34, rho(Perm.trans(3, 4, 4))))
def get_fhats_sp(fname, tup): st = time.time() mat = scipy.io.loadmat(fname) A = mat['A'] B = mat['B'] n = A.shape[0] popt = Perm(tup) fhats = {} opt_reps = {} tot = 0 print(f'Load time: {time.time()-st:.4f}s') # coset rep group elements reps_i, reps_j = cos_reps_minus2_pairs(n) reps = [i * j for i in reps_i for j in reps_j] for irrep in [(n - 2, 1, 1), (n - 2, 2), (n - 1, 1), (n, )][::-1]: st = time.time() sp_rho = SnIrrep(irrep, 'sparse') mats_i = [sp_rho(i) for i in reps_i] mats_j = [sp_rho(j) for j in reps_j] rep_mats = [mi @ mj for mi in mats_i for mj in mats_j] fh = fhat_AB(A, B, irrep, reps, rep_mats) rep = sp_rho(popt) tot += hook_length(irrep) * (rep.toarray() * fh).sum() / math.factorial(n) fhats[irrep] = fh opt_reps[irrep] = rep print(f'Time: {time.time() - st:.2f}s | Irrep: {irrep} | {type(fh)}') print(f'Total time: {time.time() - st:.2f}s') print(tot) return fhats, opt_reps
def intertwine(p1, p2, n, verbose=True): irreps = kron_irreps(p1, p2, n) g1 = Perm.trans(1, 2, n) g2 = Perm.cycle(1, n, n) rho1 = SnIrrep(p1) rho2 = SnIrrep(p2) g1_p1xp2 = np.kron(rho1(g1), rho2(g1)) g2_p1xp2 = np.kron(rho1(g2), rho2(g2)) g1_blocked = rho1(g1) #block_irrep(g1, irreps) g2_blocked = rho2(g2) #block_irrep(g2, irreps) mult = 1 d = g1_p1xp2.shape[0] dnu = g1_blocked.shape[0] // mult eye = np.eye(g1_p1xp2.shape[0]) eye_zd = np.eye(g1_blocked.shape[0] * mult) k1 = np.kron(eye, g1_blocked) - np.kron(g1_p1xp2, eye_zd) k2 = np.kron(eye, g2_blocked) - np.kron(g2_p1xp2, eye_zd) k = np.concatenate([k1, k2], axis=0) rand_null = random_null_vec(k, mult * mult) print('random vec shape', rand_null.shape) R = np.reshape(rand_null, (mult * dnu, d), 'F') # this reshapes columnwise RRT = np.matmul(R, R.T) M = kron_factor_id(RRT, dnu) _, evecs = np.linalg.eig(M) S = np.kron(evecs, np.eye(dnu)) output = normalize_rows(np.matmul(S.T, R)) print('output shape', output.shape) if verbose: print('rand null in null', in_null(k, rand_null)) print('R intertwines g1?', np.allclose(np.matmul(R, g1_p1xp2), np.matmul(g1_blocked, R))) print('R intertwines g2?', np.allclose(np.matmul(R, g2_p1xp2), np.matmul(g2_blocked, R))) print('rrt is commutant g1 block?', np.allclose(np.matmul(RRT, g1_blocked), np.matmul(g1_blocked, RRT))) print('rrt is commutant g2 block?', np.allclose(np.matmul(RRT, g2_blocked), np.matmul(g2_blocked, RRT))) return output
def test_inv(self): g = Perm((3, 2, 4, 1, 5)) ginv = Perm((4, 2, 1, 3, 5)) self.assertEqual(g.inv().tup, ginv.tup)
def test_mul(self): g = Perm.trans(1, 2, 6) h = Perm.trans(3, 4, 6) gh = g * h self.assertEqual(gh.tup, (2, 1, 4, 3, 5, 6))
def test_cycle(self): g = Perm.cycle(1, 5, 5) self.assertEqual(g.tup, (2, 3, 4, 5, 1))
def test_trans(self): g = Perm.trans(1, 4, 6) self.assertEqual(g.tup, (4, 2, 3, 1, 5, 6))
perm_tup = c_lambdas_inv[(n-2, 1, 1)] @ block_diags[(n - 2, 1, 1)].value @c_lambdas[(n-2, 1, 1)] res = ((perm @ amat @ perm.T)@ bmat).sum() print('Perm tup sums:', perm_tup.sum(axis=1), perm_tup.sum(axis=0), perm_tup.sum(), perm_tup.shape) print(f'Lower bound: {res} | result: {result}') pdb.set_trace() if __name__ == '__main__': #fname = '../data/rou12.mat' #mat = scipy.io.loadmat(fname) np.random.seed(0) n = 6 mat = {} mat['A'] = np.random.randint(0, 10, (n,n)) mat['B'] = np.random.randint(0, 100, (n,n)) best = (mat['A'] * mat['B']).sum() best_p = Perm.eye(n) avg = 0 for p in sn(mat['A'].shape[0]): obj = ((p.mat() @ mat['A'] @ p.mat().T) @ mat['B']).sum() avg += obj if obj > best: best = obj best_p = p avg = avg / math.factorial(n) print('Opt value: {} | Avg value: {:.2f} | Opt perm: {}'.format(best, avg, best_p)) start = time.time() main(mat) print('Elapsed: {:.2f}s'.format(time.time() - start))
def test_eye(self): n = 10 g = Perm.eye(n) self.assertEqual(g.tup, tuple(range(1, n + 1))) for i in range(1, n + 1): self.assertEqual(g[i], i)
def coset_reps_1(n): reps = [Perm.cycle(i, n, n) for i in range(1, n + 1)] return reps