예제 #1
0
def rule_xx(group, word):
    global n_rules
    n_rules += 1
    x1, x2 = word[0], word[1]
    delta = AutPLoopAtom('p', mat24.ploop_cap(x1.pl, x2.pl), 0)
    x = xy_Atom(x1.tag, mat24.mul_ploop(x1.pl, x2.pl))
    return [delta, x]
예제 #2
0
 def __mul__(self, other):
     if isinstance(other, PLoop):
         return PLoop(mat24.mul_ploop(self.value, other.value))
     elif isinstance(other, Integral):
         if abs(other) == 1:
             return PLoop(self.value ^ ((other & 2) << 11))
         return GCode(self.value & -(other & 1))
     elif isinstance(other, AutPL):
         return PLoop(mat24.op_ploop_autpl(self.value, other.rep))
     else:
         return NotImplemented
예제 #3
0
 def __truediv__(self, other):
     if isinstance(other, PLoop):
         return PLoop(
             mat24.mul_ploop(self.value, mat24.pow_ploop(other.value, 3)))
     elif isinstance(other, Integral):
         if abs(other) == 1:
             return PLoop(self.value ^ ((other & 2) << 11))
         elif abs(other) == 2:
             return Parity(0)
         elif abs(other) == 4:
             return Parity(mat24.gcode_weight(self.value) & 1)
         else:
             raise TypeError(ERR_DIV4 % type(self))
     else:
         return NotImplemented
예제 #4
0
def test_Parker_loop():
    print("\nTesting operation on Parker loop")
    for i in range(200):
        # Test power map, inveriosn, sign, theta, and conversion to GCode
        n1 = randint(0, 0x1fff)
        p1 = PLoop(n1)
        assert p1.ord == n1 == p1.gcode + 0x1000 * (1 - p1.sign) / 2
        if (i < 2):
            print("Testing Parker loop element", p1, ", GCode = ", GCode(p1))
        assert len(p1) == mat24.gcode_weight(n1) << 2
        assert p1.theta() == Cocode(mat24.ploop_theta(n1))
        assert p1 / 4 == p1.theta(p1) == Parity(mat24.ploop_cocycle(n1, n1))
        assert p1**2 == PLoopZ(p1 / 4) == (-PLoopZ())**(p1 / 4)
        assert p1**(-5) == PLoopZ(p1 / 4) * p1 == (-1)**(p1 / 4) * p1 == 1 / p1
        assert (1 / p1).ord ^ n1 == (mat24.gcode_weight(n1) & 1) << 12
        assert -1 / p1 == -p1**3 == -(1 / p1)
        assert p1 * (-1 / p1) == PLoopZ(1) == -PLoopZ()
        assert abs(p1).ord == GCode(p1).ord == p1.ord & 0xfff
        assert p1 != GCode(p1)
        assert +p1 == 1 * p1 == p1 * 1 == p1
        assert p1 != -p1 and p1 != ~p1
        assert (-p1).ord == p1.ord ^ 0x1000
        assert (~p1).ord == p1.ord ^ 0x800
        s, o, p1_pos = p1.split()
        assert s == (p1.ord >> 12) & 1
        assert o == (p1.ord >> 11) & 1
        assert p1.sign == (-1)**s
        assert p1_pos.ord == p1.ord & 0x7ff
        assert PLoopZ(s, o) * p1_pos == p1
        assert PLoopZ(0, o) * p1_pos == abs(p1)
        assert PLoopZ(s + 1, o) * p1_pos == -p1
        assert -p1 == -1 * p1 == p1 * -1 == p1 / -1 == -1 / p1**-5
        assert PLoopZ(s, 1 + o) * p1_pos == ~p1
        assert PLoopZ(1 + s, 1 + o) * p1_pos == ~-p1 == -~p1 == ~p1 / -1
        assert 2 * p1 == GCode(0) == -2 * GCode(p1)
        assert -13 * p1 == GCode(p1) == 7 * GCode(p1)
        if len(p1) & 7 == 0:
            assert p1**Parity(1) == p1
            assert p1**Parity(0) == PLoop(0)
        else:
            with pytest.raises(ValueError):
                p1**Parity(randint(0, 1))
        assert p1.bit_list == mat24.gcode_to_bit_list(p1.value & 0xfff)
        assert p1.bit_list == GcVector(p1).bit_list
        assert p1.bit_list == GCode(p1).bit_list
        assert PLoop(GcVector(p1) + 0) == PLoop(GCode(p1) + 0) == abs(p1)
        assert p1 + 0 == 0 + p1 == GCode(p1) + 0 == 0 + GCode(p1)
        assert Cocode(GcVector(p1)) == Cocode(0)
        assert p1 / 2 == Parity(0)

        # Test Parker loop multiplication and commutator
        n2 = randint(0, 0x1fff)
        p2 = PLoop(n2)
        coc = Cocode(mat24.ploop_cap(n1, n2))
        if (i < 1):
            print("Intersection with", p2, "is", coc)
        p2inv = p2**-1
        assert p1 * p2 == PLoop(mat24.mul_ploop(p1.ord, p2.ord))
        assert p1 / p2 == p1 * p2**(-1)
        assert p1 + p2 == p1 - p2 == GCode(p1 * p2) == GCode(n1 ^ n2)
        assert (p1 * p2) / (p2 * p1) == PLoopZ((p1 & p2) / 2)
        assert p1 & p2 == coc
        assert p1.theta() == Cocode(mat24.ploop_theta(p1.ord))
        assert p1.theta(p2) == Parity(mat24.ploop_cocycle(p1.ord, p2.ord))
        assert (p1 & p2) / 2 == p1.theta(p2) + p2.theta(p1)
        assert p1 & p2 == p1.theta() + p2.theta() + (p1 + p2).theta()
        assert int((p1 & p2) / 2) == mat24.ploop_comm(p1.ord, p2.ord)
        assert GcVector(p1 & p2) == GcVector(p1) & GcVector(p2)
        assert ~GcVector(p1 & p2) == ~GcVector(p1) | ~GcVector(p2)
        assert Cocode(GcVector(p1 & p2)) == p1 & p2

        # Test associator
        n3 = randint(0, 0x1fff)
        p3 = PLoop(n3)
        assert p1 * p2 * p3 / (p1 * (p2 * p3)) == PLoopZ(p1 & p2 & p3)
        assert int(p1 & p2 & p3) == mat24.ploop_assoc(p1.ord, p2.ord, p3.ord)
        i = randint(-1000, 1000)
        par = Parity(i)
        s3 = ((p3 & p1) & p2) + par
        assert s3 == ((p3 & p1) & p2) + par
        assert s3 == i + (p1 & (p2 & p3))
        assert s3 == par + (p1 & (p2 & p3))

        # Test some operations leading to a TypeError
        with pytest.raises(TypeError):
            p1 & p2 & p3 & p1
        with pytest.raises(TypeError):
            coc & coc
        with pytest.raises(TypeError):
            GCode(p1) * GCode(p2)
        with pytest.raises(TypeError):
            1 / GCode(p2)
        with pytest.raises(ValueError):
            coc / 4
        with pytest.raises(TypeError):
            p1 * coc
        with pytest.raises(TypeError):
            coc * p1
        types = [GcVector, GCode, Cocode, PLoop]
        for type_ in types:
            with pytest.raises(TypeError):
                int(type_(0))

    print("Parker Loop test passed")
예제 #5
0
def mul_Zy(tag, d, i, g):
    e = g.pl
    de = m24.mul_ploop(d, e)
    s = m24.scalar_prod(e, m24.vect_to_cocode(1 << i))
    s ^= de >> 12
    return s, Z, de & 0x7ff, i
예제 #6
0
def mul_Xy(tag, d, i, g):
    e = g.pl
    de = m24.mul_ploop(d, e)
    s = de >> 12
    return s, X, de & 0x7ff, i
예제 #7
0
def mul_Zx(tag, d, i, g):
    e = g.pl
    ed = m24.mul_ploop(m24.pow_ploop(e, 3), d)
    s = m24.scalar_prod(e, m24.vect_to_cocode(1 << i))
    s = (ed >> 12)
    return s & 1, Z, ed & 0x7ff, i