Example #1
0
def test_binary_op(v):
    v2 = Vector.new_from_existing(v)
    v2.element[1] = 0
    w = v.ewise_mult(v2, BinaryOp.GT).new()
    result = Vector.new_from_values([1, 3, 4, 6], [True, False, False, False])
    assert w.dtype == 'BOOL'
    assert w == result
Example #2
0
def test_equal(v):
    assert v == v
    u = Vector.new_from_values([1], [1])
    assert u != v
    u2 = Vector.new_from_values([1], [1], size=7)
    assert u2 != v
    u3 = Vector.new_from_values([1, 3, 4, 6], [1., 1., 2., 0.])
    assert u3 != v, 'different datatypes are not equal'
Example #3
0
def test_resolve_ops_using_output_dtype():
    # C[:] = A.ewise_mult(B, BinaryOp.PLUS) <-- PLUS should use same dtype as C, not A or B
    u = Vector.new_from_values([0, 1, 3], [1, 2, 3], dtype=dtypes.INT64)
    v = Vector.new_from_values([0, 1, 3], [0.1, 0.1, 0.1], dtype='FP64')
    w = Vector.new_from_type('FP32', u.size)
    w[:] = u.ewise_mult(v, BinaryOp.PLUS)
    # Element[0] should be 1.1; check approximate equality
    assert abs(w.element[0] - 1.1) < 1e-6
Example #4
0
def test_assign(v):
    u = Vector.new_from_values([0, 2], [9, 8])
    result = Vector.new_from_values([0, 1, 3, 4, 6], [9, 1, 1, 8, 0])
    w = Vector.new_from_existing(v)
    w.assign[[0, 2, 4]] = u
    assert w == result
    w = Vector.new_from_existing(v)
    w.assign[:5:2] = u
    assert w == result
Example #5
0
def test_extract(v):
    w = Vector.new_from_type(v.dtype, 3)
    result = Vector.new_from_values([0, 1], [1, 1], size=3)
    w[:] = v.extract[[1, 3, 5]]
    assert w == result
    w[:] = v.extract[1::2]
    assert w == result
    w2 = v.extract[1::2].new()
    assert w2 == w
Example #6
0
def test_ewise_mult_change_dtype(v):
    # We want to divide by 2, converting ints to floats
    v2 = Vector.new_from_values([1, 3, 4, 6], [2, 2, 2, 2])
    assert v.dtype == dtypes.INT64
    assert v2.dtype == dtypes.INT64
    result = Vector.new_from_values([1, 3, 4, 6], [0.5, 0.5, 1.0, 0],
                                    dtype=dtypes.FP64)
    w = v.ewise_mult(v2, BinaryOp.DIV).new(dtype=dtypes.FP64)
    assert w == result
Example #7
0
def test_ewise_mult(v):
    # Binary, Monoid, and Semiring
    v2 = Vector.new_from_values([0, 3, 5, 6], [2, 3, 2, 1])
    result = Vector.new_from_values([3, 6], [3, 0])
    w = v.ewise_mult(v2, BinaryOp.TIMES).new()
    assert w == result
    w[:] = v.ewise_mult(v2, Monoid.TIMES)
    assert w == result
    w[:] = v.ewise_mult(v2, Semiring.PLUS_TIMES)
    assert w == result
Example #8
0
def test_ewise_add(v):
    # Binary, Monoid, and Semiring
    v2 = Vector.new_from_values([0, 3, 5, 6], [2, 3, 2, 1])
    result = Vector.new_from_values([0, 1, 3, 4, 5, 6], [2, 1, 3, 2, 2, 1])
    w = v.ewise_add(v2, BinaryOp.MAX).new()
    assert w == result
    w[:] = v.ewise_add(v2, Monoid.MAX)
    assert w == result
    w[:] = v.ewise_add(v2, Semiring.MAX_TIMES)
    assert w == result
Example #9
0
def test_new_from_values_dtype_resolving():
    data = [[0, 1, 2], [1, 2, 3]]
    u = Vector.new_from_values([0, 1, 2], [1, 2, 3], dtype=dtypes.INT32)
    assert u.dtype == 'INT32'
    u = Vector.new_from_values([0, 1, 2], [1, 2, 3], dtype='INT32')
    assert u.dtype == dtypes.INT32
    M = Matrix.new_from_values([0, 1, 2], [2, 0, 1], [0, 2, 3],
                               dtype=dtypes.UINT8)
    assert M.dtype == 'UINT8'
    M = Matrix.new_from_values([0, 1, 2], [2, 0, 1], [0, 2, 3], dtype=float)
    assert M.dtype == dtypes.FP64
Example #10
0
def test_extract_row(A):
    w = Vector.new_from_type(A.dtype, 3)
    result = Vector.new_from_values([1, 2], [5, 3], size=3)
    w[:] = A.extract[6, [0, 2, 4]]
    assert w == result
    w[:] = A.extract[6, :5:2]
    assert w == result
    w[:] = A.T.extract[[0, 2, 4], 6]
    assert w == result
    w2 = A.extract[6, [0, 2, 4]].new()
    assert w2 == result
Example #11
0
def test_extract_column(A):
    w = Vector.new_from_type(A.dtype, 3)
    result = Vector.new_from_values([1, 2], [3, 1], size=3)
    w[:] = A.extract[[1, 3, 5], 2]
    assert w == result
    w[:] = A.extract[1:6:2, 2]
    assert w == result
    w[:] = A.T.extract[2, [1, 3, 5]]
    assert w == result
    w2 = A.extract[1:6:2, 2].new()
    assert w2 == result
Example #12
0
def test_vxm_nonsquare(v):
    A = Matrix.new_from_values([0, 3], [0, 1], [10, 20], nrows=7, ncols=2)
    u = Vector.new_from_type(v.dtype, size=2)
    u[:] = v.vxm(A, Semiring.MIN_PLUS)
    result = Vector.new_from_values([1], [21])
    assert u == result
    w1 = v.vxm(A, Semiring.MIN_PLUS).new()
    assert w1 == u
    # Test the transpose case
    v2 = Vector.new_from_values([0, 1], [1, 2])
    w2 = v2.vxm(A.T, Semiring.MIN_PLUS).new()
    assert w2.size == 7
Example #13
0
def test_assign_scalar(v):
    result = Vector.new_from_values([1, 3, 4, 5, 6], [9, 9, 2, 9, 0])
    w = Vector.new_from_existing(v)
    w.assign[[1, 3, 5]] = 9
    assert w == result
    w = Vector.new_from_existing(v)
    w.assign[1::2] = 9
    assert w == result
    w = Vector.new_from_values([0, 1, 2], [1, 1, 1])
    s = Scalar.new_from_value(9)
    w.assign[:] = s
    assert w == Vector.new_from_values([0, 1, 2], [9, 9, 9])
Example #14
0
def test_graphblas():
    GrblasNodeMap.Type.assert_equal(
        GrblasNodeMap(Vector.from_values([0, 1, 3, 4], [1, 2, 3, 4])),
        GrblasNodeMap(Vector.from_values([0, 1, 3, 4], [1, 2, 3, 4])),
        {},
        {},
        {},
        {},
    )
    GrblasNodeMap.Type.assert_equal(
        GrblasNodeMap(
            Vector.from_values([0, 1, 3, 4], [1.0, 2.0, 3.333333333333333333, 4.0])
        ),
        GrblasNodeMap(
            Vector.from_values([0, 1, 3, 4], [1.0, 2.0, 3.333333333333333334, 4 + 1e-9])
        ),
        {},
        {},
        {},
        {},
    )
    with pytest.raises(AssertionError):
        GrblasNodeMap.Type.assert_equal(
            GrblasNodeMap(Vector.from_values([0, 1, 3, 4], [1, 2, 3, 4])),
            GrblasNodeMap(Vector.from_values([0, 1, 2, 4], [1, 2, 3, 4])),
            {},
            {},
            {},
            {},
        )
    with pytest.raises(AssertionError):
        GrblasNodeMap.Type.assert_equal(
            GrblasNodeMap(Vector.from_values([0, 1, 2], [1, 2, 3])),
            GrblasNodeMap(Vector.from_values([0, 1, 2, 3], [1, 2, 3, 4])),
            {},
            {},
            {},
            {},
        )

    # Exercise GrblasNodeSet
    x = GrblasNodeSet(Vector.from_values([0, 1, 3], [1, 1, 1]))
    assert len(x) == 3
    assert 3 in x
    assert 2 not in x

    # Exercise GrblasNodeMap
    y = GrblasNodeMap(Vector.from_values([0, 1, 3], [1.1, 2.2, 3.3]))
    assert len(y) == 3
    assert 3 in y
    assert 2 not in y
    assert y[3] == 3.3
Example #15
0
def test_order_of_resolve_params_does_not_matter():
    # C[mask, accum, REPLACE] = ...
    # C[accum, REPLACE, mask] = ...
    # C[REPLACE, accum, mask] = ...
    # etc.
    from itertools import permutations
    u = Vector.new_from_values([0, 1, 3], [1, 2, 3])
    mask = Vector.new_from_values([0, 3], [True, True])
    result = Vector.new_from_values([0, 3], [5, 10])
    for params in permutations([REPLACE, mask, BinaryOp.PLUS], 3):
        v = Vector.new_from_values([0, 1, 2, 3], [4, 3, 2, 1])
        v[params] = u.ewise_mult(u, BinaryOp.TIMES)
        assert v == result
Example #16
0
def test_unaryop_udf():
    def plus_one(x):
        return x + 1

    UnaryOp.register_new('plus_one', plus_one)
    assert hasattr(UnaryOp, 'plus_one')
    assert UnaryOp.plus_one.types == {
        'INT8', 'INT16', 'INT32', 'INT64', 'UINT8', 'UINT16', 'UINT32',
        'UINT64', 'FP32', 'FP64'
    }
    v = Vector.new_from_values([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32)
    v[:] = v.apply(UnaryOp.plus_one)
    result = Vector.new_from_values([0, 1, 3], [2, 3, -3], dtype=dtypes.INT32)
    assert v == result
Example #17
0
def test_semiring_udf():
    def plus_plus_two(x, y):
        return x + y + 2

    BinaryOp.register_new('plus_plus_two', plus_plus_two)
    Semiring.register_new('extra_twos', Monoid.PLUS, BinaryOp.plus_plus_two)
    v = Vector.new_from_values([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32)
    A = Matrix.new_from_values([0, 0, 0, 0, 3, 3, 3, 3],
                               [0, 1, 2, 3, 0, 1, 2, 3],
                               [2, 3, 4, 5, 6, 7, 8, 9],
                               dtype=dtypes.INT32)
    w = v.vxm(A, Semiring.extra_twos).new()
    result = Vector.new_from_values([0, 1, 2, 3], [9, 11, 13, 15],
                                    dtype=dtypes.INT32)
    assert w == result
Example #18
0
def test_binaryop_udf():
    def times_minus_sum(x, y):
        return x * y - (x + y)

    BinaryOp.register_new('bin_test_func', times_minus_sum)
    assert hasattr(BinaryOp, 'bin_test_func')
    assert BinaryOp.bin_test_func.types == {
        'INT8', 'INT16', 'INT32', 'INT64', 'UINT8', 'UINT16', 'UINT32',
        'UINT64', 'FP32', 'FP64'
    }
    v1 = Vector.new_from_values([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32)
    v2 = Vector.new_from_values([0, 2, 3], [2, 3, 7], dtype=dtypes.INT32)
    w = v1.ewise_add(v2, BinaryOp.bin_test_func).new()
    result = Vector.new_from_values([0, 1, 2, 3], [-1, 2, 3, -31],
                                    dtype=dtypes.INT32)
    assert w == result
Example #19
0
def test_monoid_udf():
    def plus_plus_one(x, y):
        return x + y + 1

    BinaryOp.register_new('plus_plus_one', plus_plus_one)
    Monoid.register_new('plus_plus_one', BinaryOp.plus_plus_one, -1)
    assert hasattr(Monoid, 'plus_plus_one')
    assert Monoid.plus_plus_one.types == {
        'INT8', 'INT16', 'INT32', 'INT64', 'UINT8', 'UINT16', 'UINT32',
        'UINT64', 'FP32', 'FP64'
    }
    v1 = Vector.new_from_values([0, 1, 3], [1, 2, -4], dtype=dtypes.INT32)
    v2 = Vector.new_from_values([0, 2, 3], [2, 3, 7], dtype=dtypes.INT32)
    w = v1.ewise_add(v2, Monoid.plus_plus_one).new()
    result = Vector.new_from_values([0, 1, 2, 3], [4, 2, 3, 4],
                                    dtype=dtypes.INT32)
    assert w == result
Example #20
0
def test_new_from_existing(v):
    u = Vector.new_from_existing(v)
    assert u is not v
    assert u.dtype == v.dtype
    assert u.nvals == v.nvals
    assert u.size == v.size
    # Ensure they are not the same backend object
    v.element[0] = 1000
    assert u.element[0] != 1000
Example #21
0
def test_new_from_values():
    u = Vector.new_from_values([0, 1, 3], [True, False, True])
    assert u.size == 4
    assert u.nvals == 3
    assert u.dtype == bool
    u2 = Vector.new_from_values([0, 1, 3], [12.3, 12.4, 12.5], size=17)
    assert u2.size == 17
    assert u2.nvals == 3
    assert u2.dtype == float
    u3 = Vector.new_from_values([0, 1, 1], [1, 2, 3],
                                size=10,
                                dup_op=BinaryOp.TIMES)
    assert u3.size == 10
    assert u3.nvals == 2  # duplicates were combined
    assert u3.dtype == int
    assert u3.element[1] == 6  # 2*3
    with pytest.raises(ValueError):
        # Duplicate indices requires a dup_op
        Vector.new_from_values([0, 1, 1], [True, True, True])
Example #22
0
def test_vxm_mask(v, A):
    mask = Vector.new_from_values([0, 3, 4], [True, True, True], size=7)
    u = Vector.new_from_existing(v)
    u[mask] = v.vxm(A, Semiring.PLUS_TIMES)
    result = Vector.new_from_values([0, 1, 3, 4, 6], [3, 1, 0, 8, 0], size=7)
    assert u == result
    u = Vector.new_from_existing(v)
    u[~mask] = v.vxm(A, Semiring.PLUS_TIMES)
    result2 = Vector.new_from_values([2, 3, 4, 5, 6], [3, 1, 2, 14, 4], size=7)
    assert u == result2
    u = Vector.new_from_existing(v)
    u[mask, REPLACE] = v.vxm(A, Semiring.PLUS_TIMES)
    result3 = Vector.new_from_values([0, 3, 4], [3, 0, 8], size=7)
    assert u == result3
    w = v.vxm(A, Semiring.PLUS_TIMES).new(mask=mask)
    assert w == result3
Example #23
0
def test_graphblas():
    GrblasNodeMap.Type.assert_equal(
        GrblasNodeMap(Vector.from_values([0, 1, 3, 4], [1, 2, 3, 4])),
        GrblasNodeMap(Vector.from_values([0, 1, 3, 4], [1, 2, 3, 4])),
        {},
        {},
        {},
        {},
    )
    GrblasNodeMap.Type.assert_equal(
        GrblasNodeMap(
            Vector.from_values([0, 1, 3, 4], [1.0, 2.0, 3.333333333333333333, 4.0])
        ),
        GrblasNodeMap(
            Vector.from_values([0, 1, 3, 4], [1.0, 2.0, 3.333333333333333334, 4 + 1e-9])
        ),
        {},
        {},
        {},
        {},
    )
    with pytest.raises(AssertionError):
        GrblasNodeMap.Type.assert_equal(
            GrblasNodeMap(Vector.from_values([0, 1, 3, 4], [1, 2, 3, 4])),
            GrblasNodeMap(Vector.from_values([0, 1, 2, 4], [1, 2, 3, 4])),
            {},
            {},
            {},
            {},
        )
    with pytest.raises(AssertionError):
        GrblasNodeMap.Type.assert_equal(
            GrblasNodeMap(Vector.from_values([0, 1, 2], [1, 2, 3])),
            GrblasNodeMap(Vector.from_values([0, 1, 2, 3], [1, 2, 3, 4])),
            {},
            {},
            {},
            {},
        )
Example #24
0
def test_mxv(A, v):
    w = A.mxv(v, Semiring.PLUS_TIMES).new()
    result = Vector.new_from_values([0, 1, 6], [5, 16, 13])
    assert w == result
Example #25
0
def v():
    data = [[1, 3, 4, 6], [1, 1, 2, 0]]
    return Vector.new_from_values(*data)
Example #26
0
def test_simple_assignment(v):
    # w[:] = v
    w = Vector.new_from_type(v.dtype, v.size)
    w[:] = v
    assert w == v
Example #27
0
def test_apply(v):
    result = Vector.new_from_values([1, 3, 4, 6], [-1, -1, -2, 0])
    w = v.apply(UnaryOp.AINV).new()
    assert w == result
Example #28
0
def test_reduce_row(A):
    result = Vector.new_from_values([0, 1, 2, 3, 4, 5, 6],
                                    [5, 12, 1, 6, 7, 1, 15])
    w = A.reduce_rows(Monoid.PLUS).new()
    assert w == result
Example #29
0
def test_reduce_column(A):
    result = Vector.new_from_values([0, 1, 2, 3, 4, 5, 6],
                                    [3, 2, 9, 10, 11, 8, 4])
    w = A.reduce_columns(Monoid.PLUS).new()
    assert w == result
Example #30
0
def test_new_from_type():
    u = Vector.new_from_type(dtypes.INT8, 17)
    assert u.dtype == 'INT8'
    assert u.nvals == 0
    assert u.size == 17