Ejemplo n.º 1
0
    def test_z4_z2_compute_flat(self):
        nblks = 30
        ndim = 5
        sign_array = np.random.randint(0, 2, ndim)
        sign_map = {0: "+", 1: "-"}
        pattern = "".join([sign_map[ix] for ix in sign_array])

        na = np.random.randint(0, 10, nblks * ndim).reshape(nblks, ndim)

        z4lsts = [[Z4(na[nb, nd]) for nd in range(ndim)]
                  for nb in range(nblks)]
        z4arrs = [[z4lsts[nb][nd].to_flat() for nd in range(ndim)]
                  for nb in range(nblks)]
        z4arrs = np.asarray(z4arrs, dtype=int)
        z4out = Z4._compute(pattern, z4arrs)
        z4neg = Z4._compute(pattern, z4arrs, offset=("+", Z4(3)), neg=True)

        z2lsts = [[Z2(na[nb, nd]) for nd in range(ndim)]
                  for nb in range(nblks)]
        z2arrs = [[z2lsts[nb][nd].to_flat() for nd in range(ndim)]
                  for nb in range(nblks)]
        z2arrs = np.asarray(z2arrs, dtype=int)
        z2out = Z2._compute(pattern, z2arrs)
        z2neg = Z2._compute(pattern, z2arrs, offset=("-", Z2(1)), neg=True)

        u1lsts = [[U1(na[nb, nd]) for nd in range(ndim)]
                  for nb in range(nblks)]
        u1arrs = [[u1lsts[nb][nd].to_flat() for nd in range(ndim)]
                  for nb in range(nblks)]
        u1arrs = np.asarray(u1arrs, dtype=int)
        u1out = U1._compute(pattern, u1arrs)
        u1neg = U1._compute(pattern, u1arrs, offset=("-", U1(1)), neg=True)

        for i in range(nblks):
            z4outa = Z4._compute(pattern, z4lsts[i])
            z4outb = Z4.from_flat(z4out[i])
            assert z4outa == z4outb
            assert -(z4outa + Z4(3)) == Z4.from_flat(z4neg[i])

            z2outa = Z2._compute(pattern, z2lsts[i])
            z2outb = Z2.from_flat(z2out[i])
            assert z2outa == z2outb
            assert -(z2outa - Z2(1)) == Z2.from_flat(z2neg[i])

            u1outa = U1._compute(pattern, u1lsts[i])
            u1outb = U1.from_flat(u1out[i])
            assert u1outa == u1outb
            assert -(u1outa - U1(1)) == U1.from_flat(u1neg[i])
Ejemplo n.º 2
0
    def test_z4_z2_u1_compute(self):
        ndim = 10
        sign_array = np.random.randint(0, 2, ndim)
        sign_map = {0: "+", 1: "-"}
        pattern = "".join([sign_map[ix] for ix in sign_array])
        na = np.random.randint(0, 10, ndim)
        z4arrs = [Z4(n) for n in na]
        z4out = Z4._compute(pattern, z4arrs)
        z2arrs = [Z2(n) for n in na]
        z2out = Z2._compute(pattern, z2arrs)
        u1arrs = [U1(n) for n in na]
        u1out = U1._compute(pattern, u1arrs)

        z4outn = 0
        z2outn = 0
        u1outn = 0

        for p, n in zip(sign_array, na):
            if p == 0:
                z4outn += n
                z2outn += n
                u1outn += n
            else:
                z4outn -= n
                z2outn -= n
                u1outn -= n
        assert z4out.n == z4outn % 4
        assert z2out.n == z2outn % 2
        assert u1out.n == u1outn