def remove_overlaps(feval, individuals, hives): if len(individuals) == 0: return [] idx = range(0, len(hives)) idx.sort(key=lambda i: individuals[i].fitness) nm = np.sqrt(len(individuals[0])) covs = [h.sigma*nm for h in hives] D = squareform(pdist(individuals)) uniques = set(idx[:]) for i, j in permut(idx, 2): if covs[i] > D[i,j]: if individuals[i].fitness > individuals[j].fitness: wps = dm.denorm(individuals[i], individuals[j], [0.5]) wpfit = np.array(map(feval, wps)) if all(wpfit >= individuals[j].fitness.wvalues[0]): D[j,...] = np.inf uniques.discard(j) return (i for i in uniques)
def permutations(string): _arr = [] _set = {} for item in permut(string, len(string)): _arr.append(''.join(item)) _set = set(_arr) _arr = list(_set) return _arr
def createnet(ncell=100): global ncells, cells, nclist # need global since want to set these ncells = ncell cells = [ HHCell(i) if useHHCell else Cell(i, i, 0, 0) for i in range(ncell) ] nclist = [ h.NetCon(x[0].soma(0.5)._ref_v, x[1].syn, sec=x[0].soma) if useHHCell else h.NetCon(x[0].pp, x[1].pp) for x in permut(cells, 2) ] for ce in cells: nc = h.NetCon(ce.soma(0.5)._ref_v, None, sec=ce.soma) if useHHCell else h.NetCon(ce.pp, None) nc.record(tvec, ind, ce.gid) nclist.append(nc)
def is_seq(*cards): '''checks whether cards are in sequence or not. Returns True if cards form a sequence. All cards must be different.''' tot_cards = len(cards) suits = set() for c in cards: suits.add(c.suit) if len(suits) != 1: return False if len(cards) == 2: if (cards[0] + 1) == cards[1] or (cards[0] - 1) == cards[1]: return True return False tot = permut(cards, len(cards)) for ss in tot: if is_seq(ss[0], ss[1]): if is_seq(*ss[1:]): return True return False
import sys from itertools import permutations as permut def is_prime(n): for x in xrange(2,int(n**0.5)+1): if n % x == 0: return False return True if __name__ == '__main__': for y in xrange(10,1,-1): for x in reversed(sorted(permut(xrange(1,y)))): int_x = int(''.join(str(z) for z in x)) if is_prime(int_x): print int_x sys.exit(0)
def construct_vibronic_model(multi_e, dvr): r""" Bi-linear vibronic coupling model for Pyrazine, 4 modes See: Raab, Worth, Meyer, Cederbaum. J.Chem.Phys. 110 (1999) 936 The parameters are from heidelberg mctdh package pyr4+.op """ # frequencies w10a = 0.1139 * ev2au w6a = 0.0739 * ev2au w1 = 0.1258 * ev2au w9a = 0.1525 * ev2au # energy-gap delta = 0.42300 * ev2au # linear, on-diagonal coupling coefficients # H(1,1) _6a_s1_s1 = 0.09806 * ev2au _1_s1_s1 = 0.05033 * ev2au _9a_s1_s1 = 0.14521 * ev2au # H(2,2) _6a_s2_s2 = -0.13545 * ev2au _1_s2_s2 = 0.17100 * ev2au _9a_s2_s2 = 0.03746 * ev2au # quadratic, on-diagonal coupling coefficients # H(1,1) _10a_10a_s1_s1 = -0.01159 * ev2au _6a_6a_s1_s1 = 0.00000 * ev2au _1_1_s1_s1 = 0.00000 * ev2au _9a_9a_s1_s1 = 0.00000 * ev2au # H(2,2) _10a_10a_s2_s2 = -0.01159 * ev2au _6a_6a_s2_s2 = 0.00000 * ev2au _1_1_s2_s2 = 0.00000 * ev2au _9a_9a_s2_s2 = 0.00000 * ev2au # bilinear, on-diagonal coupling coefficients # H(1,1) _6a_1_s1_s1 = 0.00108 * ev2au _1_9a_s1_s1 = -0.00474 * ev2au _6a_9a_s1_s1 = 0.00204 * ev2au # H(2,2) _6a_1_s2_s2 = -0.00298 * ev2au _1_9a_s2_s2 = -0.00155 * ev2au _6a_9a_s2_s2 = 0.00189 * ev2au # linear, off-diagonal coupling coefficients _10a_s1_s2 = 0.20804 * ev2au # bilinear, off-diagonal coupling coefficients # H(1,2) and H(2,1) _1_10a_s1_s2 = 0.00553 * ev2au _6a_10a_s1_s2 = 0.01000 * ev2au _9a_10a_s1_s2 = 0.00126 * ev2au ham_terms = [] e_list = ["s1", "s2"] v_list = ["10a", "6a", "9a", "1"] for (e_isymbol, e_jsymbol) in product(e_list, repeat=2): e_op = Op(r"a^\dagger a", [e_isymbol, e_jsymbol]) for (v_isymbol, v_jsymbol) in product(v_list, repeat=2): # linear if v_isymbol == v_jsymbol: # if one of the permutation is defined, then the `e_idx_tuple` term should # be defined as required by Hermitian Hamiltonian for eterm1, eterm2 in permut([e_isymbol, e_jsymbol], 2): factor = locals().get(f"_{v_isymbol}_{eterm1}_{eterm2}") if factor is not None: factor *= np.sqrt(eval(f"w{v_isymbol}")) ham_terms.append(e_op * Op("x", v_isymbol) * factor) logger.debug( f"term: {v_isymbol}_{e_isymbol}_{e_jsymbol}") break else: logger.debug( f"no term: {v_isymbol}_{e_isymbol}_{e_jsymbol}") # quadratic # use product to guarantee `break` breaks the whole loop it = product(permut([v_isymbol, v_jsymbol], 2), permut([e_isymbol, e_jsymbol], 2)) for (vterm1, vterm2), (eterm1, eterm2) in it: factor = locals().get(f"_{vterm1}_{vterm2}_{eterm1}_{eterm2}") if factor is not None: factor *= np.sqrt( eval(f"w{v_isymbol}") * eval(f"w{v_jsymbol}")) if vterm1 == vterm2: v_op = Op("x^2", vterm1) else: v_op = Op("x", vterm1) * Op("x", vterm2) ham_terms.append(e_op * v_op * factor) logger.debug( f"term: {v_isymbol}_{v_jsymbol}_{e_isymbol}_{e_jsymbol}" ) break else: logger.debug( f"no term: {v_isymbol}_{v_jsymbol}_{e_isymbol}_{e_jsymbol}" ) # electronic coupling ham_terms.append(Op(r"a^\dagger a", "s1", -delta, [0, 0])) ham_terms.append(Op(r"a^\dagger a", "s2", delta, [0, 0])) # vibrational kinetic and potential for v_isymbol in v_list: ham_terms.extend([ Op("p^2", v_isymbol, 0.5), Op("x^2", v_isymbol, 0.5 * eval("w" + v_isymbol)**2) ]) for term in ham_terms: logger.info(term) basis = [] if not multi_e: for e_isymbol in e_list: basis.append(ba.BasisSimpleElectron(e_isymbol)) else: basis.append(ba.BasisMultiElectron(e_list, [0, 0])) for v_isymbol in v_list: basis.append( ba.BasisSHO(v_isymbol, locals()[f"w{v_isymbol}"], 30, dvr=dvr)) logger.info(f"basis:{basis}") return basis, ham_terms
def makeSet(numbers): num_set = set() for i in range(1, len(numbers) + 1): for p in permut(list(numbers), i): num_set.add(int("".join(p))) return num_set
def construct_vibronic_model(multi_e): r""" Bi-linear vibronic coupling model for Pyrazine, 4 modes See: Raab, Worth, Meyer, Cederbaum. J.Chem.Phys. 110 (1999) 936 The parameters are from heidelberg mctdh package pyr4+.op """ # frequencies w10a = 0.1139 * ev2au w6a = 0.0739 * ev2au w1 = 0.1258 * ev2au w9a = 0.1525 * ev2au # energy-gap delta = 0.42300 * ev2au # linear, on-diagonal coupling coefficients # H(1,1) _6a_s1_s1 = 0.09806 * ev2au _1_s1_s1 = 0.05033 * ev2au _9a_s1_s1 = 0.14521 * ev2au # H(2,2) _6a_s2_s2 = -0.13545 * ev2au _1_s2_s2 = 0.17100 * ev2au _9a_s2_s2 = 0.03746 * ev2au # quadratic, on-diagonal coupling coefficients # H(1,1) _10a_10a_s1_s1 = -0.01159 * ev2au _6a_6a_s1_s1 = 0.00000 * ev2au _1_1_s1_s1 = 0.00000 * ev2au _9a_9a_s1_s1 = 0.00000 * ev2au # H(2,2) _10a_10a_s2_s2 = -0.01159 * ev2au _6a_6a_s2_s2 = 0.00000 * ev2au _1_1_s2_s2 = 0.00000 * ev2au _9a_9a_s2_s2 = 0.00000 * ev2au # bilinear, on-diagonal coupling coefficients # H(1,1) _6a_1_s1_s1 = 0.00108 * ev2au _1_9a_s1_s1 = -0.00474 * ev2au _6a_9a_s1_s1 = 0.00204 * ev2au # H(2,2) _6a_1_s2_s2 = -0.00298 * ev2au _1_9a_s2_s2 = -0.00155 * ev2au _6a_9a_s2_s2 = 0.00189 * ev2au # linear, off-diagonal coupling coefficients _10a_s1_s2 = 0.20804 * ev2au # bilinear, off-diagonal coupling coefficients # H(1,2) and H(2,1) _1_10a_s1_s2 = 0.00553 * ev2au _6a_10a_s1_s2 = 0.01000 * ev2au _9a_10a_s1_s2 = 0.00126 * ev2au model = {} e_list = ["s1", "s2"] v_list = ["10a", "6a", "9a", "1"] for e_idx, e_isymbol in enumerate(e_list): for e_jdx, e_jsymbol in enumerate(e_list): e_idx_tuple = (f"e_{e_idx}", f"e_{e_jdx}") model[e_idx_tuple] = defaultdict(list) for v_idx, v_isymbol in enumerate(v_list): for v_jdx, v_jsymbol in enumerate(v_list): if v_idx == v_jdx: v_idx_tuple = (f"v_{v_idx}", ) else: v_idx_tuple = (f"v_{v_idx}", f"v_{v_jdx}") # linear if v_idx == v_jdx: # if one of the permutation is defined, then the `e_idx_tuple` term should # be defined as required by Hermitian Hamiltonian for eterm1, eterm2 in permut( [f"{e_isymbol}", f"{e_jsymbol}"], 2): factor = locals().get( f"_{v_isymbol}_{eterm1}_{eterm2}") if factor is not None: factor *= np.sqrt(eval(f"w{v_isymbol}")) model[e_idx_tuple][v_idx_tuple].append( (Op("x", 0), factor)) logger.debug( f"term: {v_isymbol}_{e_isymbol}_{e_jsymbol}" ) break else: logger.debug( f"no term: {v_isymbol}_{e_isymbol}_{e_jsymbol}" ) # quadratic # use product to guarantee `break` breaks the whole loop it = product(permut([f"{v_isymbol}", f"{v_jsymbol}"], 2), permut([f"{e_isymbol}", f"{e_jsymbol}"], 2)) for (vterm1, vterm2), (eterm1, eterm2) in it: logger.info(f"_{vterm1}_{vterm2}_{eterm1}_{eterm2}") factor = locals().get( f"_{vterm1}_{vterm2}_{eterm1}_{eterm2}") if factor is not None: factor *= np.sqrt( eval(f"w{v_isymbol}") * eval(f"w{v_jsymbol}")) if v_idx == v_jdx: model_term = (Op("x^2", 0), factor) else: model_term = (Op("x", 0), Op("x", 0), factor) model[e_idx_tuple][v_idx_tuple].append(model_term) logger.debug( f"term: {v_isymbol}_{v_jsymbol}_{e_isymbol}_{e_jsymbol}" ) break else: logger.debug( f"no term: {v_isymbol}_{v_jsymbol}_{e_isymbol}_{e_jsymbol}" ) # electronic coupling model[("e_0", "e_0")]["J"] = -delta model[("e_1", "e_1")]["J"] = delta # vibrational kinetic and potential model["I"] = {} for v_idx, v_isymbol in enumerate(v_list): model["I"][(f"v_{v_idx}", )] = [(Op("p^2", 0), 0.5), (Op("x^2", 0), 0.5 * eval("w" + v_isymbol)**2)] for e_dof, value in model.items(): for v_dof, ops in value.items(): if v_dof == "J": logger.info(f"{e_dof},{v_dof},{ops}") else: for term in ops: logger.info(f"{e_dof},{v_dof},{term}") order = {} basis = [] if not multi_e: idx = 0 for e_idx, e_isymbol in enumerate(e_list): order[f"e_{e_idx}"] = idx basis.append(ba.BasisSimpleElectron()) idx += 1 else: order = {"e_0": 0, "e_1": 0} basis.append(ba.BasisMultiElectron(2, [0, 0])) idx = 1 for v_idx, v_isymbol in enumerate(v_list): order[f"v_{v_idx}"] = idx basis.append(ba.BasisSHO(locals()[f"w{v_isymbol}"], 30)) idx += 1 logger.info(f"order:{order}") logger.info(f"basis:{basis}") return order, basis, model