def test_kron(): n = Matrix.from_lists(list(range(3)), list(range(3)), list(range(3))) m = Matrix.from_lists(list(range(3)), list(range(3)), list(range(3))) o = n.kron(m) assert o == Matrix.from_lists([0, 1, 2, 3, 4, 5, 6, 7, 8], [0, 1, 2, 3, 4, 5, 6, 7, 8], [0, 0, 0, 0, 1, 2, 0, 2, 4])
def test_apply_lambda(): v = Matrix.from_lists([0, 1, 2], [0, 1, 2], [22, 33, 44]) w = v.apply(lambda x: mod(x, 10)) assert w == Matrix.from_lists([0, 1, 2], [0, 1, 2], [2, 3, 4]) w = v.apply(lambda x: mod(x, 7)) assert w == Matrix.from_lists([0, 1, 2], [0, 1, 2], [1, 5, 2])
def test_matrix_multiplication(): a = Matrix.from_lists([0, 0, 1, 1], [0, 1, 0, 1], [4, 1, 1, 2]) b = Matrix.from_lists([0, 0, 1, 1], [0, 1, 0, 1], [7, 1, 3, 1]) c = Matrix.from_lists([0, 0, 1, 1], [0, 1, 0, 1], [31, 5, 13, 3]) result = a @ b assert result.iseq(c)
def test_mxm(): m = Matrix.from_lists([0, 1, 2], [0, 1, 2], [1, 2, 3]) n = Matrix.from_lists([0, 1, 2], [0, 1, 2], [1, 2, 3]) actual_res = m @ n expected_res = Matrix.from_lists([0, 1, 2], [0, 1, 2], [1, 4, 9]) assert actual_res.iseq(expected_res), "Matrices are not equal"
def test_apply(): v = Matrix.from_lists( [0, 1, 2], [0, 1, 2], [2, 3, 4]) w = v.apply(unaryop.ainv_int64) assert w == Matrix.from_lists( [0, 1, 2], [0, 1, 2], [-2, -3, -4])
def test_matrix_eq(): v = Matrix.from_lists(list(range(10)), list(range(10)), list(range(10))) w = Matrix.from_lists(list(range(10)), list(range(10)), list(range(10))) x = Matrix.from_lists(list(range(1, 11)), list(range(1, 11)), list(range(1, 11)), ncols=11, nrows=11) assert v == w assert v != x
def test_matrix_transpose(): v = Matrix.from_lists( list(range(2, -1, -1)), list(range(3)), list(range(3))) w = v.transpose() assert w == Matrix.from_lists( [0, 1, 2], [2, 1, 0], [0, 1, 2] )
def test_matrix_ewise_add(): v = Matrix.from_lists(list(range(10)), list(range(10)), list(range(10))) w = Matrix.from_lists(list(range(10)), list(range(10)), list(range(10))) x = v.ewise_add(w) assert x == Matrix.from_lists(list(range(10)), list(range(10)), list(range(0, 20, 2))) z = v + w assert x == z v += w assert v == z
def test_vector_ewise_mult(): v = Matrix.from_lists(list(range(10)), list(range(10)), list(range(10))) w = Matrix.from_lists(list(range(10)), list(range(10)), list(range(10))) x = v.ewise_mult(w) assert x == Matrix.from_lists(list(range(10)), list(range(10)), list(map(lambda x: x * x, list(range(10))))) z = v * w assert x == z v *= w assert v == z
def test_check_mult(): row_inds = [0, 0, 1, 1] col_inds = [0, 1, 0, 1] matrix_1 = Matrix.from_lists(row_inds, col_inds, [1, 2, 0, 3]) matrix_2 = Matrix.from_lists(row_inds, col_inds, [0, 1, 3, 5]) matrix_res = matrix_1 @ matrix_2 expected = Matrix.from_lists(row_inds, col_inds, [6, 11, 9, 15]) assert expected.iseq(matrix_res), "Matrices not equal"
def test_select_ops(): I, J = tuple(map(list, zip(*product(range(3), repeat=2)))) V = list(range(9)) m = Matrix.from_lists(I, J, V, 3, 3) assert m.tril() == Matrix.from_lists( [0, 1, 1, 2, 2, 2], [0, 0, 1, 0, 1, 2], [0, 3, 4, 6, 7, 8]) assert m.triu() == Matrix.from_lists( [0, 0, 0, 1, 1, 2], [0, 1, 2, 1, 2, 2], [0, 1, 2, 4, 5, 8]) assert m.diag() == Matrix.from_lists( [0, 1, 2], [0, 1, 2], [0, 4, 8]) assert m.offdiag() == Matrix.from_lists( [0, 0, 1, 1, 2, 2], [1, 2, 0, 2, 0, 1], [1, 2, 3, 5, 6, 7]) assert m.nonzero() == Matrix.from_lists( [0, 0, 1, 1, 1, 2, 2, 2], [1, 2, 0, 1, 2, 0, 1, 2], [1, 2, 3, 4, 5, 6, 7, 8]) assert -m == Matrix.from_lists( [0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2], [0, -1, -2, -3, -4, -5, -6, -7, -8]) n = -m assert abs(m) == Matrix.from_lists( [0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2], [0, 1, 2, 3, 4, 5, 6, 7, 8]) m = Matrix.from_lists( [0, 1, 2], [0, 1, 2], [0.0, 1.0, 2.0], 3, 3) n = ~m assert n == Matrix.from_lists( [0, 1, 2], [0, 1, 2], [float('inf'), 1.0, 0.5])
def test_mxm(): m = Matrix.from_lists([0, 1, 2], [1, 2, 0], [1, 2, 3]) n = Matrix.from_lists([0, 1, 2], [1, 2, 0], [2, 3, 4]) o = m.mxm(n) assert o.nrows == 3 assert o.ncols == 3 assert o.nvals == 3 r = Matrix.from_lists([0, 1, 2], [2, 0, 1], [3, 8, 6]) assert o == r assert r == m @ n m @= n assert r == m o = m.mxm(n, semiring=semiring.lor_land_bool) assert o == Matrix.from_lists([0, 1, 2], [0, 1, 2], [1, 1, 1])
def test_matrix_prod(): A = Matrix.from_lists([0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2], [1, 2, 1, 2, 1, 2, 1, 2, 1]) B = Matrix.from_lists([0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2], [2, 1, 2, 1, 2, 1, 2, 1, 2]) A_times_B = Matrix.from_lists([0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2], [6, 6, 6, 9, 6, 9, 6, 6, 6]) assert A_times_B.iseq(A @ B)
def test_mxv(): m = Matrix.from_lists([0, 1, 2], [1, 2, 0], [1, 2, 3]) v = Vector.from_lists([0, 1, 2], [2, 3, 4]) o = m.mxv(v) assert o == Vector.from_lists([0, 1, 2], [3, 8, 6]) assert m @ v == o
def test_matrix_reduce_vector(): m = Matrix.from_lists( list(range(10)), list(range(10)), list(range(10))) v = m.reduce_vector() v == Vector.from_list(list(range(10)))
def test_select(): v = Matrix.from_lists( [0, 1, 2], [0, 1, 2], [0, 0, 3]) w = v.select(lib.GxB_NONZERO) assert w.to_lists() == [[2], [2], [3]]
def test_clear(): v = Matrix.from_lists(list(range(10)), list(range(10)), list(range(10))) assert v.nvals == 10 assert len(v) == 10 v.clear() assert v.nvals == 0 assert len(v) == 0
def test_matrix_pattern(): v = Matrix.from_lists(list(range(10)), list(range(10)), list(range(10))) p = v.pattern() assert p.gb_type == lib.GrB_BOOL assert p.nrows == 10 assert p.ncols == 10 assert p.nvals == 10
def __init__(self, edges: List[Edge], start_states: Optional[Set[int]] = None, final_states: Optional[Set[int]] = None): label_to_edges: Dict[Any, Tuple[Indices, Indices]] = {} label_to_bool_matrix: Dict[Any, Matrix] = {} self.vertices = set() for edge in edges: I, J = label_to_edges.setdefault(edge.label, ([], [])) I.append(edge.node_from) J.append(edge.node_to) self.vertices.add(edge.node_from) self.vertices.add(edge.node_to) max_size = 0 if not self.vertices else max(self.vertices) + 1 self.matrix_size = max_size for label, (I, J) in label_to_edges.items(): label_to_bool_matrix[label] = Matrix.from_lists(I=I, J=J, V=[True] * len(I), ncols=max_size, nrows=max_size, typ=types.BOOL) self.label_to_bool_matrix = label_to_bool_matrix if start_states is None: start_states = self.vertices self.start_states = start_states if final_states is None: final_states = self.vertices self.final_states = final_states
def test_matrix_mul(): matrix_1 = Matrix.from_lists([ 0, 1, 2], [1, 2, 0], [3, 5, 1] ) matrix_2 = Matrix.from_lists( [0, 1, 2], [1, 2, 0], [2, 1, 4] ) # matrix multiplication res = matrix_1.mxm(matrix_2) assert res.nrows == 3 assert res.ncols == 3 assert res.nvals == 3 exp_res = Matrix.from_lists( [0, 1, 2], [2, 0, 1], [3, 20, 2] ) assert res.iseq(exp_res)
def test_select_cmp(): I, J = tuple(map(list, zip(*product(range(3), repeat=2)))) V = list(range(9)) m = Matrix.from_lists(I, J, V, 3, 3) n = m.select('>', 5) assert n == Matrix.from_lists([2, 2, 2], [0, 1, 2], [6, 7, 8]) n = m.select('>=', 5) assert n == Matrix.from_lists([1, 2, 2, 2], [2, 0, 1, 2], [5, 6, 7, 8]) n = m.select('<', 5) assert n == Matrix.from_lists([0, 0, 0, 1, 1], [0, 1, 2, 0, 1], [0, 1, 2, 3, 4], 3, 3) n = m.select('<=', 5) assert n == Matrix.from_lists([0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2], [0, 1, 2, 3, 4, 5], 3, 3) n = m.select('==', 5) assert n == Matrix.from_lists([1], [2], [5], 3, 3) n = m.select('!=', 5) assert n == Matrix.from_lists([0, 0, 0, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 0, 1, 2], [0, 1, 2, 3, 4, 6, 7, 8], 3, 3)
def test_cmp(): I, J = tuple(map(list, zip(*product(range(3), repeat=2)))) V = list(range(9)) m = Matrix.from_lists(I, J, V, 3, 3) n = m > 5 assert n == Matrix.from_lists( [0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2], [False, False, False, False, False, False, True, True, True], ) n = m >= 5 assert n == Matrix.from_lists( [0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2], [False, False, False, False, False, True, True, True, True], ) n = m < 5 assert n == Matrix.from_lists([0, 0, 0, 1, 1], [0, 1, 2, 0, 1], [True, True, True, True, True], 3, 3) n = m <= 5 assert n == Matrix.from_lists([0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2], [True, True, True, True, True, True], 3, 3) n = m == 5 assert n == Matrix.from_lists([1], [2], [5], 3, 3) n = m != 5 assert n == Matrix.from_lists([0, 0, 0, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 0, 1, 2], [0, 1, 2, 3, 4, 6, 7, 8], 3, 3)
def test_matrix_assign(): m = Matrix.from_lists(list(range(3)), list(range(3)), list(range(3))) assert m.nvals == 3 m[2] = Vector.from_list(list(repeat(6, 3))) assert m.nvals == 5 assert m == Matrix.from_lists([0, 1, 2, 2, 2], [0, 1, 0, 1, 2], [0, 1, 6, 6, 6], 3, 3) m = Matrix.from_lists(list(range(3)), list(range(3)), list(range(3))) assert m.nvals == 3 m[2, :] = Vector.from_list(list(repeat(6, 3))) assert m.nvals == 5 assert m == Matrix.from_lists([0, 1, 2, 2, 2], [0, 1, 0, 1, 2], [0, 1, 6, 6, 6], 3, 3) m = Matrix.from_lists(list(range(3)), list(range(3)), list(range(3))) assert m.nvals == 3 m[:, 2] = Vector.from_list(list(repeat(6, 3))) assert m.nvals == 5 assert m == Matrix.from_lists([0, 1, 0, 1, 2], [0, 1, 2, 2, 2], [0, 1, 6, 6, 6], 3, 3) m = Matrix.from_type(int, 3, 3) assert m.nvals == 0 n = Matrix.from_lists([0, 1, 2], [0, 1, 2], [0, 1, 2]) m[:, :] = n assert m == n
def test_resize(): v = Matrix.from_lists(list(range(10)), list(range(10)), list(range(10))) assert v.nrows == 10 assert v.ncols == 10 assert v.nvals == 10 v.resize(20, 20) assert v.nrows == 20 assert v.ncols == 20 assert v.nvals == 10
def test_matrix_to_from_lists(): v = Matrix.from_lists(list(range(10)), list(range(10)), list(range(10))) assert v.nrows == 10 assert v.ncols == 10 assert v.nvals == 10 assert v.to_lists() == [ list(range(10)), list(range(10)), list(range(10)), ]
def test_transitive_closure(): g = Graph() g.from_file("input3.txt") reachability_matrix = g.transitive_closure() expected = Matrix.from_lists( [0, 0, 1, 3, 3, 3, 4, 4], [1, 2, 2, 1, 2, 4, 1, 2], [True, True, True, True, True, True, True, True] ) assert expected.iseq(reachability_matrix)
def test_vxm(): m = Matrix.from_lists([0, 1, 2], [1, 2, 0], [1, 2, 3]) v = Vector.from_lists([0, 1, 2], [2, 3, 4]) o = v.vxm(m) assert o == Vector.from_lists([0, 1, 2], [12, 2, 6]) w = Vector.dup(v) assert v @ m == o v @= m assert v == o
def test_matrix_multiplication(): matrix_1 = Matrix.from_lists( [0, 1, 2], [1, 2, 0], [1, 2, 3] ) matrix_2 = Matrix.from_lists( [0, 1, 2], [1, 2, 0], [4, 5, 6] ) result = matrix_1 @ matrix_2 expected = Matrix.from_lists( [0, 1, 2], [2, 0, 1], [5, 12, 12] ) assert result.iseq(expected)
def test_matrix_mm_read_write(tmp_path): mmf = tmp_path / 'mmwrite_test.mm' mmf.touch() m = Matrix.from_lists([0, 1, 2], [0, 1, 2], [2, 3, 4]) with mmf.open('w') as f: m.to_mm(f) with mmf.open() as f: assert f.readlines() == [ '%%MatrixMarket matrix coordinate integer symmetric\n', '%%GraphBLAS GrB_INT64\n', '3 3 3\n', '1 1 2\n', '2 2 3\n', '3 3 4\n' ] with mmf.open() as f: n = Matrix.from_mm(f) assert n == m
def graph_to_bin_matrix(self): graph = self.graph edges = graph.edges for v_from, key, v_to in edges: if key in self.M_dict: self.M_dict[key][0].append(int(v_from)) self.M_dict[key][1].append(int(v_to)) else: self.M_dict[key] = ([int(v_from)], [int(v_to)]) for key, nodes in self.M_dict.items(): num_edges = len(nodes[0]) self.M_bin[key] = Matrix.from_lists(nodes[0], nodes[1], [True] * num_edges, nrows=graph.vertices_num, ncols=graph.vertices_num)