def free_alg(spark_ctx): """Initialize the environment for a free algebra.""" dr = Drudge(spark_ctx) r = Range('R') dumms = sympify('i, j, k, l, m, n') dr.set_dumms(r, dumms) s = Range('S') s_dumms = symbols('alpha beta') dr.set_dumms(s, s_dumms) dr.add_resolver_for_dumms() # For testing the Einstein over multiple ranges. a1, a2 = symbols('a1 a2') dr.add_resolver({a1: (r, s), a2: (r, s)}) dr.set_name(a1, a2) v = Vec('v') dr.set_name(v) m = IndexedBase('m') dr.set_symm(m, Perm([1, 0], NEG)) h = IndexedBase('h') dr.set_symm(h, Perm([1, 0], NEG | CONJ)) rho = IndexedBase('rho') dr.set_symm(rho, Perm([1, 0, 3, 2]), valence=4) dr.set_tensor_method('get_one', lambda x: 1) return dr
def test_canonicalization_of_vectors_w_symm(free_alg): """Test the canonicalization when vectors are given (anti-)symmetries. """ dr = free_alg p = dr.names x = IndexedBase('x') r = p.R i, j = p.i, p.j vs = Vec('vs') dr.set_symm(vs, Perm([1, 0]), valence=2) tensor = dr.sum((i, r), (j, r), x[i, j] * vs[j, i]) res = tensor.simplify() assert res.n_terms == 1 term = res.local_terms[0] assert term.sums == ((i, r), (j, r)) assert term.amp == x[i, j] assert term.vecs == (vs[i, j], ) va = Vec('va') dr.set_symm(va, Perm([1, 0], NEG), valence=2) tensor = dr.sum((i, r), (j, r), x[i, j] * va[j, i]) res = tensor.simplify() assert res.n_terms == 1 term = res.local_terms[0] assert term.sums == ((i, r), (j, r)) assert term.amp == -x[i, j] assert term.vecs == (va[i, j], )
def test_perm_reports_error(): """Tests perm class reports error for invalid inputs.""" with pytest.raises(ValueError): Perm([1, 1, 1]) with pytest.raises(ValueError): Perm([1, 2]) with pytest.raises(TypeError): Perm('aaa') with pytest.raises(TypeError): Perm([0, 1], 0.5)
def test_s3_group_correct_and_serializable(): """Test the S3 group. Since the permutation group objects are mostly opaque, here it is tested by its new arguments, which is also further verified to work with pickle. """ cycle = Perm([1, 2, 0]) transp = Perm([1, 0, 2], 1) group = Group([cycle, transp]) args = group.__getnewargs__() assert len(args) == 1 transvs = args[0] assert len(transvs) == 2 top_level = transvs[0] lower_level = transvs[1] assert len(top_level) == 2 target = top_level[0] transv = top_level[1] assert target == 0 assert len(transv) == 2 assert set(i[0][0] for i in transv) == {1, 2} assert len(lower_level) == 2 target = lower_level[0] transv = lower_level[1] assert target == 1 assert len(transv) == 1 perm = transv[0] # This permutation fixes 0, but is not identity. It must be the # transposition of 1 and 2 assert perm[0][0] == 0 assert perm[0][1] == 2 assert perm[0][2] == 1 assert perm[1] == 1 # Args should not change from the group reconstructed from pickle. pickled = pickle.dumps(group) new_group = pickle.loads(pickled) assert new_group.__getnewargs__() == args # Assert that Group type should be considered true, since it implements # neither __bool__ nor __len__. assert group
def test_eldag_can_be_canonicalized(): """Tests the Eldag canonicalization facility. Note that this test more focuses on better coverage in the canonpy interface to libcanon, rather than on the correctness of canonicalization algorithm, which should be already tested within libcanon. In this test, we have two bivalent nodes in the Eldag, one without symmetry, one with symmetry. They are both connected to two terminal nodes with the same colour. In this graph, the connection to the non-symmetric node determines the resulted permutations. """ transp = Perm([1, 0], 1) symms = [None, Group([transp]), None, None] colours = [0, 1, 1, 1] # We force the non-symmetric node to come earlier. for if_same in [True, False]: # If the non-symmetric node is connected to the two terminal nodes in # order. The symmetric node always connect to them in order. edges = [2, 3, 2, 3] if if_same else [3, 2, 2, 3] ia = [0, 2, 4, 4, 4] node_order, perms = canon_eldag(edges, ia, symms, colours) # Assertions applicable to both cases. assert node_order[0] == 0 assert node_order[1] == 1 for i in [0, 2, 3]: assert perms[i] is None continue # The ordering of the two terminals. if if_same: assert node_order[2:] == [2, 3] else: assert node_order[2:] == [3, 2] # The permutation of the symmetric node. perm = perms[1] if if_same: assert perm[0] == 0 assert perm[1] == 1 assert perm.acc == 0 else: assert perm[0] == 1 assert perm[1] == 0 assert perm.acc == 1 continue return
def test_simple_terms_can_be_canonicalized(): """Test the canonicalization of very simple terms. In this test, all the terms has very simple appearance. So rather than testing the canonicalization really canonicalizes all the equivalent forms, here we check if the canonicalized form is the most intuitive form that we expect. """ l = Range('L') x = IndexedBase('x') i, j = sympify('i, j') # A term without the vector part, canonicalization without symmetry. term = sum_term([(j, l), (i, l)], x[i, j])[0] res = term.canon() expected = sum_term([(i, l), (j, l)], x[i, j])[0] assert res == expected # A term without the vector part, canonicalization with symmetry. m = Range('M') term = sum_term([(j, m), (i, l)], x[j, i])[0] for neg, conj in itertools.product([IDENT, NEG], [IDENT, CONJ]): acc = neg | conj group = Group([Perm([1, 0], acc)]) res = term.canon(symms={x: group}) expected_amp = x[i, j] if neg == NEG: expected_amp *= -1 if conj == CONJ: expected_amp = conjugate(expected_amp) expected = sum_term([(i, l), (j, m)], expected_amp)[0] assert res == expected continue # In the absence of symmetry, the two indices should not be permuted. res = term.canon() expected = sum_term([(i, l), (j, m)], x[j, i])[0] assert res == expected # Now we add vectors to the terms. v = Vec('v') term = sum_term([(i, l), (j, l)], v[i] * v[j])[0] # Without anything added, it should already be in the canonical form. assert term.canon() == term # When we flip the colour of the vectors, we should get something different. res = term.canon(vec_colour=lambda idx, vec, term: -idx) expected = sum_term([(j, l), (i, l)], v[i] * v[j])[0] assert res == expected
def test_perm_pickles(): """Tests perms can be correctly pickled.""" pre_images = [1, 2, 0] perm = Perm(pre_images, 1) new_args = perm.__getnewargs__() assert (len(new_args) == 2) assert (new_args[0] == pre_images) assert (new_args[1] == 1) new_perm = Perm(*new_args) pickled = pickle.dumps(perm) unpickled_perm = pickle.loads(pickled) for form in [new_perm, unpickled_perm]: # Maybe equality comparison should be added to Perms. assert (len(form) == len(perm)) for i in range(len(perm)): assert (form[i] == perm[i]) assert (form.acc == perm.acc) return
def test_tensors_w_functions_can_be_canonicalized(free_alg): """Test canonicalization facility on general functions.""" dr = free_alg dr.set_symm(SymmFunc, Perm([1, 0], NEG), valence=2, set_base_name=False) p = dr.names i, j, k = p.R_dumms[:3] r = p.R v = p.v # General anti-symmetric real matrix. tensor = dr.sum((i, r), (j, r), SymmFunc(k, i, j) * SymmFunc(i, j) * v[i] * v[j]) + dr.sum( (i, r), (j, r), SymmFunc(k, i, j) * SymmFunc(j, i) * v[i] * v[j]) assert tensor.n_terms == 2 assert tensor.simplify() == 0
def test_perm_has_basic_functionality(): """Test the basic facility for Perms. Here a simple cyclic permutation of three points is used to test various facilities of Perm. """ pre_images = [1, 2, 0] perm = Perm(pre_images, 1) assert (len(perm) == len(pre_images)) for i, v in enumerate(pre_images): assert (perm[i] == v) assert (perm.acc == 1) return
ctx = SparkContext() raise_ = ReducedBCSDrudge.DEFAULT_RAISE lower = ReducedBCSDrudge.DEFAULT_LOWER dr = ReducedBCSDrudge(ctx, interact=InvariantIndexable(Symbol('G')), specials={(raise_, lower): 2 * raise_ * lower - 1}) #==================================== # AGP expected values: #==================================== # case: z00 Z00 = IndexedBase('Z00') # case: z02 Z02 = IndexedBase('Z02') dr.set_symm(Z02, Perm([1, 0], IDENT), valence=2) # case: z04 Z04 = IndexedBase('Z04') dr.set_symm(Z04, Perm([1, 0, 2, 3], IDENT), Perm([0, 1, 3, 2], IDENT), Perm([2, 3, 0, 1], IDENT), valence=4) # case: z06 Z06 = IndexedBase('Z06') dr.set_symm(Z06, Perm([1, 0, 2, 3, 4, 5], IDENT), Perm([0, 2, 1, 3, 4, 5], IDENT), Perm([0, 1, 2, 4, 3, 5], IDENT),