Exemplo n.º 1
0
    def test_matrix_vector_mul_NotEqualFields_ExpectedAssertionError(self):
        with self.assertRaises(AssertionError):
            N1 = Mat(
                ({1, 3, 5, 7}, {'a'}), {
                    (1, 'a'): -1,
                    (1, 'b'): 2,
                    (3, 'a'): 1,
                    (3, 'b'): 4,
                    (7, 'a'): 3,
                    (5, 'b'): -1
                })
            u1 = Vec({'a', 'b'}, {'a': 1, 'b': 2})
            N1 * u1

        with self.assertRaises(AssertionError):
            N1 = Mat(
                ({1, 3, 5, 7}, {'a', 'b'}), {
                    (1, 'a'): -1,
                    (1, 'b'): 2,
                    (3, 'a'): 1,
                    (3, 'b'): 4,
                    (7, 'a'): 3,
                    (5, 'b'): -1
                })
            u1 = Vec({'a'}, {'a': 1, 'b': 2})
            N1 * u1
Exemplo n.º 2
0
    def test_scalar_mul(self):
        zero = Vec({'x', 'y', 'z', 'w'}, {})
        u = Vec({'x', 'y', 'z', 'w'}, {'x': 1, 'y': 2, 'z': 3, 'w': 4})

        self.assertEqual(0 * u, zero)
        self.assertEqual(1 * u, u)
        self.assertEqual(0.5 * u, Vec({'x', 'y', 'z', 'w'}, {'x': 0.5, 'y': 1, 'z': 1.5, 'w': 2}))
        self.assertEqual(u, Vec({'x', 'y', 'z', 'w'}, {'x': 1, 'y': 2, 'z': 3, 'w': 4}))
Exemplo n.º 3
0
def vec_sum(veclist, D):
    sum_vec = Vec(D, {})
    for vec in veclist:
        sum_vec += vec
        # for d in D:
        #     sum_vec[d] += vec[d]
    return sum_vec
Exemplo n.º 4
0
def list2vec(L):
    """Given a list L of field elements, return a Vec with domain {0...len(L)-1}
    whose entry i is L[i]

    >>> list2vec([10, 20, 30])
    Vec({0, 1, 2},{0: 10, 1: 20, 2: 30})
    """
    return Vec(set(range(len(L))), {k: L[k] for k in range(len(L))})
Exemplo n.º 5
0
def button_vectors(n):
    D = {(i, j) for i in range(n) for j in range(n)}
    vecdict = {(i, j): Vec(
        D,
        dict([((x, j), one) for x in range(max(i - 1, 0), min(i + 2, n))] +
             [((i, y), one) for y in range(max(j - 1, 0), min(j + 2, n))]))
               for (i, j) in D}
    return vecdict
Exemplo n.º 6
0
    def test_dot(self):
        u1 = Vec({'a', 'b'}, {'a': 1, 'b': 2})
        u2 = Vec({'a', 'b'}, {'b': 2, 'a': 1})
        self.assertEqual(u1 * u2, 5)
        self.assertEqual(u1, Vec({'a', 'b'}, {'a': 1, 'b': 2}))
        self.assertEqual(u2, Vec({'a', 'b'}, {'b': 2, 'a': 1}))

        v1 = Vec({'p', 'q', 'r', 's'}, {'p': 2, 's': 3, 'q': -1, 'r': 0})
        v2 = Vec({'p', 'q', 'r', 's'}, {'p': -2, 'r': 5})
        self.assertEqual(v1 * v2, -4)

        w1 = Vec({'a', 'b', 'c'}, {'a': 2, 'b': 3, 'c': 4})
        w2 = Vec({'a', 'b', 'c'}, {'a': 12, 'b': 8, 'c': 6})
        self.assertEqual(w1 * w2, 72)

        v1 = Vec({1, 2}, {1: 3, 2: 6})
        v2 = Vec({1, 2}, {1: 2, 2: 1})
        self.assertEqual(v1 * v2, 12)
Exemplo n.º 7
0
def GF2_span(D, L):
    l = list()

    for pd in product([0, one], repeat=len(L)):
        sum_vec = Vec(D, {})
        for idx in range(len(L)):
            sum_vec += pd[idx] * L[idx]
        l.append(sum_vec)

    return l
Exemplo n.º 8
0
    def test_vector_matrix_mul_NotEqualFields_ExpectedAssertionError(self):
        with self.assertRaises(AssertionError):
            v1 = Vec({1, 2}, {1: 1, 2: 8})
            M1 = Mat(({1, 2, 3}, {'a', 'b', 'c'}), {
                (1, 'b'): 2,
                (2, 'a'): -1,
                (3, 'a'): 1,
                (3, 'c'): 7
            })
            v1 * M1

        with self.assertRaises(AssertionError):
            v1 = Vec({1, 2, 3}, {1: 1, 2: 8})
            M1 = Mat(({1, 2}, {'a', 'b', 'c'}), {
                (1, 'b'): 2,
                (2, 'a'): -1,
                (3, 'a'): 1,
                (3, 'c'): 7
            })
            v1 * M1
Exemplo n.º 9
0
def mat2coldict(A):
    """Given a matrix, return a dictionary mapping column labels of A to columns of A
           e.g.:
           >>> M = Mat(({0, 1, 2}, {0, 1}), {(0, 1): 1, (2, 0): 8, (1, 0): 4, (0, 0): 3, (2, 1): -2})
           >>> mat2coldict(M)
           {0: Vec({0, 1, 2},{0: 3, 1: 4, 2: 8}), 1: Vec({0, 1, 2},{0: 1, 1: 0, 2: -2})}
           >>> mat2coldict(Mat(({0,1},{0,1}),{}))
           {0: Vec({0, 1},{0: 0, 1: 0}), 1: Vec({0, 1},{0: 0, 1: 0})}
    """
    return {
        col: Vec(A.D[0], {row: A[row, col]
                          for row in A.D[0]})
        for col in A.D[1]
    }
Exemplo n.º 10
0
def mat2rowdict(A):
    """Given a matrix, return a dictionary mapping row labels of A to rows of A
           e.g.:

       >>> M = Mat(({0, 1, 2}, {0, 1}), {(0, 1): 1, (2, 0): 8, (1, 0): 4, (0, 0): 3, (2, 1): -2})
           >>> mat2rowdict(M)
           {0: Vec({0, 1},{0: 3, 1: 1}), 1: Vec({0, 1},{0: 4, 1: 0}), 2: Vec({0, 1},{0: 8, 1: -2})}
           >>> mat2rowdict(Mat(({0,1},{0,1}),{}))
           {0: Vec({0, 1},{0: 0, 1: 0}), 1: Vec({0, 1},{0: 0, 1: 0})}
           """
    return {
        row: Vec(A.D[1], {col: A[row, col]
                          for col in A.D[1]})
        for row in A.D[0]
    }
Exemplo n.º 11
0
def solve(a, b):
    mat_list = []
    vec_list = []
    D = list(a.D[1])

    for r in a.D[0]:
        row_list = []
        for c in a.D[1]:
            row_list.append(a[r, c])
        mat_list.append(row_list)
        vec_list.append(b[r])

    solution = _solve(mat_list, vec_list)[0]

    return Vec(set(D), {D[i]: solution[i] for i in range(len(D))})
Exemplo n.º 12
0
    def test_matrix_vector_mul(self):
        N1 = Mat(
            ({1, 3, 5, 7}, {'a', 'b'}), {
                (1, 'a'): -1,
                (1, 'b'): 2,
                (3, 'a'): 1,
                (3, 'b'): 4,
                (7, 'a'): 3,
                (5, 'b'): -1
            })
        u1 = Vec({'a', 'b'}, {'a': 1, 'b': 2})
        self.assertEqual(N1 * u1, Vec({1, 3, 5, 7}, {1: 3, 3: 9, 5: -2, 7: 3}))
        self.assertEqual(
            N1,
            Mat(
                ({1, 3, 5, 7}, {'a', 'b'}), {
                    (1, 'a'): -1,
                    (1, 'b'): 2,
                    (3, 'a'): 1,
                    (3, 'b'): 4,
                    (7, 'a'): 3,
                    (5, 'b'): -1
                }))
        self.assertEqual(u1, Vec({'a', 'b'}, {'a': 1, 'b': 2}))

        N2 = Mat(({('a', 'b'), ('c', 'd')}, {1, 2, 3, 5, 8}), {})
        u2 = Vec({1, 2, 3, 5, 8}, {})
        self.assertEqual(N2 * u2, Vec({('a', 'b'), ('c', 'd')}, {}))

        M3 = Mat(({0, 1}, {'a', 'b'}), {
            (0, 'a'): 1,
            (0, 'b'): 1,
            (1, 'a'): 1,
            (1, 'b'): 1
        })
        v3 = Vec({'a', 'b'}, {'a': 1, 'b': 1})
        self.assertEqual(M3 * v3, Vec({0, 1}, {0: 2, 1: 2}))
Exemplo n.º 13
0
    def test_vector_matrix_mul(self):
        v1 = Vec({1, 2, 3}, {1: 1, 2: 8})
        M1 = Mat(({1, 2, 3}, {'a', 'b', 'c'}), {
            (1, 'b'): 2,
            (2, 'a'): -1,
            (3, 'a'): 1,
            (3, 'c'): 7
        })
        self.assertEqual(v1 * M1,
                         Vec({'a', 'b', 'c'}, {
                             'a': -8,
                             'b': 2,
                             'c': 0
                         }))
        self.assertEqual(v1, Vec({1, 2, 3}, {1: 1, 2: 8}))
        self.assertEqual(
            M1,
            Mat(({1, 2, 3}, {'a', 'b', 'c'}), {
                (1, 'b'): 2,
                (2, 'a'): -1,
                (3, 'a'): 1,
                (3, 'c'): 7
            }))

        v2 = Vec({'a', 'b'}, {})
        M2 = Mat(({'a', 'b'}, {0, 2, 4, 6, 7}), {})
        self.assertEqual(v2 * M2, Vec({0, 2, 4, 6, 7}, {}))

        v3 = Vec({'a', 'b'}, {'a': 1, 'b': 1})
        M3 = Mat(({'a', 'b'}, {0, 1}), {
            ('a', 1): 1,
            ('b', 1): 1,
            ('a', 0): 1,
            ('b', 0): 1
        })
        self.assertEqual(v3 * M3, Vec({0, 1}, {0: 2, 1: 2}))
Exemplo n.º 14
0
def standard(D, one):
    return [Vec(D, {k: one}) for k in D]
Exemplo n.º 15
0
 def test_dot_NotEqualFields_ExpectedAssertionError(self):
     with self.assertRaises(AssertionError):
         Vec({'a', 'b', 'c'}, {'a': 0}) * Vec({'a', 'b'}, {'b': 0})
Exemplo n.º 16
0
 def setUp(self):
     self.v = Vec({'a', 'b', 'c', 'd'}, {'a': 2, 'c': 1, 'd': 3})
Exemplo n.º 17
0
def zero_vec(D):
    """Returns a zero vector with the given domain
    """
    return Vec(D, {})
Exemplo n.º 18
0
    def test_add(self):
        a = Vec({'a', 'e', 'i', 'o', 'u'}, {'a': 0, 'e': 1, 'i': 2})
        b = Vec({'a', 'e', 'i', 'o', 'u'}, {'o': 4, 'u': 7})
        c = Vec({'a', 'e', 'i', 'o', 'u'}, {'a': 0, 'e': 1, 'i': 2, 'o': 4, 'u': 7})
        self.assertEqual(a + b, c)

        a == Vec({'a', 'e', 'i', 'o', 'u'}, {'a': 0, 'e': 1, 'i': 2})
        b == Vec({'a', 'e', 'i', 'o', 'u'}, {'o': 4, 'u': 7})
        d = Vec({'x', 'y', 'z'}, {'x': 2, 'y': 1})
        e = Vec({'x', 'y', 'z'}, {'z': 4, 'y': -1})
        f = Vec({'x', 'y', 'z'}, {'x': 2, 'y': 0, 'z': 4})
        self.assertEqual(d + e, f)

        d == Vec({'x', 'y', 'z'}, {'x': 2, 'y': 1})
        e == Vec({'x', 'y', 'z'}, {'z': 4, 'y': -1})
        self.assertEqual(b + Vec({'a', 'e', 'i', 'o', 'u'}, {}), b)
Exemplo n.º 19
0
    def tes_equal(self):
        self.assertTrue(Vec({'a', 'b', 'c'}, {'a': 0}) == Vec({'a', 'b', 'c'}, {'b': 0}))
        self.assertTrue(Vec({'a', 'b', 'c'}, {'a': 0}) == Vec({'a', 'b', 'c'}, {}))
        self.assertTrue(Vec({'a', 'b', 'c'}, {}) == Vec({'a', 'b', 'c'}, {'a': 0}))
        self.assertTrue(Vec({'x', 'y', 'z'}, {'y': 1, 'x': 2}) == Vec({'x', 'y', 'z'}, {'y': 1, 'x': 2, 'z': 0}))

        self.assertFalse(Vec({'x', 'y', 'z'}, {'y': 1, 'x': 2}) == Vec({'x', 'y', 'z'}, {'y': 1, 'z': 0}))
        self.assertFalse(Vec({'a', 'b', 'c'}, {'a': 0, 'c': 1}) == Vec({'a', 'b', 'c'}, {'a': 0, 'c': 1, 'b': 4}))
        self.assertFalse(Vec({'a', 'b', 'c'}, {'a': 0, 'c': 1, 'b': 4}) == Vec({'a', 'b', 'c'}, {'a': 0, 'c': 1}))
        self.assertFalse(Vec({'a', 'b'}, {'a': 1}) == Vec({'a', 'b'}, {'b': 1}))
        self.assertFalse(Vec({'a', 'b'}, {'a': 1}) == Vec({'a', 'b'}, {'a': 2}))
Exemplo n.º 20
0
 def test_neg(self):
     u = Vec({1, 3, 5, 7}, {1: 1, 3: 2, 5: 3, 7: 4})
     self.assertEqual(-u, Vec({1, 3, 5, 7}, {1: -1, 3: -2, 5: -3, 7: -4}))
     self.assertEqual(u, Vec({1, 3, 5, 7}, {1: 1, 3: 2, 5: 3, 7: 4}))
     self.assertEqual(-Vec({'a', 'b', 'c'}, {'a': 1}), Vec({'a', 'b', 'c'}, {'a': -1}))
Exemplo n.º 21
0

# Quiz 5.10.21
def diag(D, entries):
    return Mat((D, D), {(d, d): entries[d] for d in D})


if __name__ == '__main__':
    # Quiz 5.1.1
    print([[0 for j in range(4)] for i in range(3)])

    # Quiz 5.1.2
    print([[i - j for i in range(3)] for j in range(4)])

    # Quiz 5.1.4
    print(Vec({'a', 'b'}, {'a': 3, 'b': 30}))

    # Quiz 5.1.5
    print({
        '@': Vec({'a', 'b'}, {
            'a': 1,
            'b': 10
        }),
        '#': Vec({'a', 'b'}, {
            'a': 2,
            'b': 20
        }),
        '?': Vec({'a', 'b'}, {
            'a': 3,
            'b': 30
        })
Exemplo n.º 22
0
def mat2vec(M):
    return Vec({(r, c) for r in M.D[0] for c in M.D[1]}, M.f)
Exemplo n.º 23
0
def mat2coldict(A):
    return {c: Vec(A.D[0], {r: A.f[(r, c)] for r in A.D[0]}) for c in A.D[1]}
Exemplo n.º 24
0
def mat2rowdict(A):
    return {r: Vec(A.D[1], {c: A.f[(r, c)] for c in A.D[1]}) for r in A.D[0]}