def testFeedback(self):
        A, B, C, D, A1, A2 = get_symbols(3, 2, 1, 1, 1, 1)
        circuit_identity(1)

#        self.assertRaises(Exception, Feedback, ())
#        self.assertRaises(Exception, Feedback, (C,))
#        self.assertRaises(Exception, Feedback, (C + D,))
#        self.assertRaises(Exception, Feedback, (C << D,))
#        self.assertRaises(Exception, Feedback, (circuit_identity(n),))
#        self.assertRaises(Exception, Feedback.create, (circuit_identity(0)))
#        self.assertEquals(Feedback.create(circuit_identity(n)), circuit_identity(n-1))
        self.assertEquals(FB(A+B), A + FB(B))
        smq = map_signals_circuit({2:1}, 3) # == 'cid(1) + X'
        self.assertEquals(smq, smq.series_inverse())
        # import metapost as mp
        # mp.display_circuit(Feedback.apply_with_rules(smq.series_inverse() << (B + C) << smq))
        # mp.display_circuit(B.feedback() + C)

        self.assertEquals(( smq << (B + C)).feedback(out_index = 2, in_index = 1), B.feedback() + C)
        print((smq << (B + C) << smq).feedback())

        self.assertEquals(( smq << (B + C) << smq).feedback(), B.feedback() + C)

        self.assertEquals((B + C).feedback(1,1), B.feedback() + C)

        #check that feedback is resolved into series when possible
        self.assertEquals(B.feedback(1,0).substitute({B:(C+D)}), C << D)
        self.assertEquals((A << (B + cid(1))).feedback(),  A.feedback() << B)
        self.assertEquals((A << (B + cid(1)) << (cid(1) + P_sigma(1,0))).feedback(2,1),  A.feedback() << B)
        self.assertEquals((A << (cid(1) + P_sigma(1,0)) << (B + cid(1)) << (cid(1) + P_sigma(1,0))).feedback(1,1),  A.feedback(1,1) << B)
        self.assertEquals((B << (cid(1)  + C)).feedback(0,1).substitute({B: (A1 + A2)}), A2 << C << A1)
        self.assertEquals(((cid(1)  + C)<< P_sigma(1,0) << B).feedback(1,1).substitute({B: (A1 + A2)}), A2 << C << A1)
        self.assertEquals(((cid(1)  + C)<< P_sigma(1,0) << B << (cid(1) + D)).feedback(1,1).substitute({B: (A1 + A2)}), A2 << D<< C << A1)
    def testCPermutation(self):
        test_perm = (0,1,2,5,6,3,4)
        qtp = CPermutation.create(test_perm)
        self.assertRaises(Exception, CPermutation.create,(list(test_perm)))
        self.assertEqual(qtp.series_inverse(), CPermutation.create(invert_permutation(test_perm)))
        self.assertEqual(qtp.block_structure, (1,1,1,4))
        id1 = circuit_identity(1)
        self.assertEqual(qtp.get_blocks(), (id1, id1, id1, CPermutation.create((2,3,0,1))))

        self.assertEqual(CPermutation((1,0,3,2)).get_blocks(), (CPermutation((1,0)), CPermutation((1,0))))
        nt = len(test_perm)
        self.assertEqual(qtp << qtp.series_inverse(), circuit_identity(nt))
        self.assertEqual(permute(list(invert_permutation(test_perm)), test_perm), list(range(nt)))
    def testPermutation(self):
        n = 5

        self.assertEqual(CPermutation.create(()), circuit_identity(0))
        invalid_permutation = (1,1)
        self.assertRaises(Exception, CPermutation.create, (invalid_permutation,))
        p_id = tuple(range(n))
        self.assertEqual(CPermutation.create(p_id), circuit_identity(n))


        self.assertEqual(map_signals({0:1,1:0}, 2), (1,0))
        self.assertEqual(map_signals({0:5,1:0}, 6), (5,0,1,2,3,4))

        self.assertEqual(map_signals({0:5,1:0, 3:2}, 6), invert_permutation(map_signals({5:0,0:1, 2:3}, 6)))
 def testSeriesFilterIdentities(self):
     for n in (1,2,3, 10):
         A, B = get_symbol(n), get_symbol(n)
         idn = circuit_identity(n)
         self.assertEquals(A << idn, A)
         self.assertEquals(idn << A, A)
         self.assertEquals(SeriesProduct.create(idn, idn, A, idn, idn, B, idn, idn), A << B)
Exemple #5
0
 def testSeriesFilterIdentities(self):
     for n in (1, 2, 3, 10):
         A, B = get_symbol(n), get_symbol(n)
         idn = circuit_identity(n)
         self.assertEquals(A << idn, A)
         self.assertEquals(idn << A, A)
         self.assertEquals(
             SeriesProduct.create(idn, idn, A, idn, idn, B, idn, idn),
             A << B)
Exemple #6
0
    def testFeedback(self):
        A, B, C, D, A1, A2 = get_symbols(3, 2, 1, 1, 1, 1)
        circuit_identity(1)

        #        self.assertRaises(Exception, Feedback, ())
        #        self.assertRaises(Exception, Feedback, (C,))
        #        self.assertRaises(Exception, Feedback, (C + D,))
        #        self.assertRaises(Exception, Feedback, (C << D,))
        #        self.assertRaises(Exception, Feedback, (circuit_identity(n),))
        #        self.assertRaises(Exception, Feedback.create, (circuit_identity(0)))
        #        self.assertEquals(Feedback.create(circuit_identity(n)), circuit_identity(n-1))
        self.assertEquals(FB(A + B), A + FB(B))
        smq = map_signals_circuit({2: 1}, 3)  # == 'cid(1) + X'
        self.assertEquals(smq, smq.series_inverse())
        # import metapost as mp
        # mp.display_circuit(Feedback.apply_with_rules(smq.series_inverse() << (B + C) << smq))
        # mp.display_circuit(B.feedback() + C)

        self.assertEquals((smq << (B + C)).feedback(out_index=2, in_index=1),
                          B.feedback() + C)
        print((smq << (B + C) << smq).feedback())

        self.assertEquals((smq << (B + C) << smq).feedback(), B.feedback() + C)

        self.assertEquals((B + C).feedback(1, 1), B.feedback() + C)

        #check that feedback is resolved into series when possible
        self.assertEquals(B.feedback(1, 0).substitute({B: (C + D)}), C << D)
        self.assertEquals((A << (B + cid(1))).feedback(), A.feedback() << B)
        self.assertEquals((A << (B + cid(1)) <<
                           (cid(1) + P_sigma(1, 0))).feedback(2, 1),
                          A.feedback() << B)
        self.assertEquals((A << (cid(1) + P_sigma(1, 0)) << (B + cid(1)) <<
                           (cid(1) + P_sigma(1, 0))).feedback(1, 1),
                          A.feedback(1, 1) << B)
        self.assertEquals(
            (B << (cid(1) + C)).feedback(0, 1).substitute({B: (A1 + A2)}),
            A2 << C << A1)
        self.assertEquals(((cid(1) + C) << P_sigma(1, 0) << B).feedback(
            1, 1).substitute({B: (A1 + A2)}), A2 << C << A1)
        self.assertEquals(
            ((cid(1) + C) << P_sigma(1, 0) << B <<
             (cid(1) + D)).feedback(1, 1).substitute({B: (A1 + A2)}),
            A2 << D << C << A1)
Exemple #7
0
    def testCPermutation(self):
        test_perm = (0, 1, 2, 5, 6, 3, 4)
        qtp = CPermutation.create(test_perm)
        self.assertRaises(Exception, CPermutation.create, (list(test_perm)))
        self.assertEqual(qtp.series_inverse(),
                         CPermutation.create(invert_permutation(test_perm)))
        self.assertEqual(qtp.block_structure, (1, 1, 1, 4))
        id1 = circuit_identity(1)
        self.assertEqual(qtp.get_blocks(),
                         (id1, id1, id1, CPermutation.create((2, 3, 0, 1))))

        self.assertEqual(
            CPermutation((1, 0, 3, 2)).get_blocks(), (CPermutation(
                (1, 0)), CPermutation((1, 0))))
        nt = len(test_perm)
        self.assertEqual(qtp << qtp.series_inverse(), circuit_identity(nt))
        self.assertEqual(
            permute(list(invert_permutation(test_perm)), test_perm),
            list(range(nt)))
Exemple #8
0
    def testPermutation(self):
        n = 5

        self.assertEqual(CPermutation.create(()), circuit_identity(0))
        invalid_permutation = (1, 1)
        self.assertRaises(Exception, CPermutation.create,
                          (invalid_permutation, ))
        p_id = tuple(range(n))
        self.assertEqual(CPermutation.create(p_id), circuit_identity(n))

        self.assertEqual(map_signals({0: 1, 1: 0}, 2), (1, 0))
        self.assertEqual(map_signals({0: 5, 1: 0}, 6), (5, 0, 1, 2, 3, 4))

        self.assertEqual(
            map_signals({
                0: 5,
                1: 0,
                3: 2
            }, 6), invert_permutation(map_signals({
                5: 0,
                0: 1,
                2: 3
            }, 6)))
    def testConcatenation(self):
        n = 4
        A, B = get_symbol(n), get_symbol(n)
        id0 = circuit_identity(0)
        self.assertEquals(A+B, Concatenation(A,B))
        self.assertEquals(A+B, Concatenation.create(A,B))
        self.assertEquals(id0 + id0 + A + id0 + id0 + B + id0 + id0, A + B)
#        self.assertRaises(Exception, Concatenation, ())
#        self.assertRaises(Exception, Concatenation, (A,))

        self.assertEquals((A+B).block_structure, (n,n))

        self.assertEquals((A+B).get_blocks((n,n)), (A,B))
        #test index_in_block()
        self.assertEquals((A+B).index_in_block(0), (0,0))
        self.assertEquals((A+B).index_in_block(1), (1,0))
        self.assertEquals((A+B).index_in_block(2), (2,0))
        self.assertEquals((A+B).index_in_block(3), (3,0))
        self.assertEquals((A+B).index_in_block(4), (0,1))
        self.assertEquals((A+B).index_in_block(5), (1,1))
        self.assertEquals((A+B).index_in_block(7), (3,1))
Exemple #10
0
    def testConcatenation(self):
        n = 4
        A, B = get_symbol(n), get_symbol(n)
        id0 = circuit_identity(0)
        self.assertEquals(A + B, Concatenation(A, B))
        self.assertEquals(A + B, Concatenation.create(A, B))
        self.assertEquals(id0 + id0 + A + id0 + id0 + B + id0 + id0, A + B)
        #        self.assertRaises(Exception, Concatenation, ())
        #        self.assertRaises(Exception, Concatenation, (A,))

        self.assertEquals((A + B).block_structure, (n, n))

        self.assertEquals((A + B).get_blocks((n, n)), (A, B))
        #test index_in_block()
        self.assertEquals((A + B).index_in_block(0), (0, 0))
        self.assertEquals((A + B).index_in_block(1), (1, 0))
        self.assertEquals((A + B).index_in_block(2), (2, 0))
        self.assertEquals((A + B).index_in_block(3), (3, 0))
        self.assertEquals((A + B).index_in_block(4), (0, 1))
        self.assertEquals((A + B).index_in_block(5), (1, 1))
        self.assertEquals((A + B).index_in_block(7), (3, 1))