示例#1
0
def test_assign_scalar(A):
    # Test block
    result_block = Matrix.new_from_values(
        [3, 0, 6, 0, 6, 6, 2, 4, 1, 1, 3, 5, 1, 3, 5],
        [0, 1, 2, 3, 3, 4, 5, 5, 6, 2, 2, 2, 4, 4, 4],
        [3, 2, 5, 3, 7, 3, 1, 7, 4, 0, 0, 0, 0, 0, 0])
    C = Matrix.new_from_existing(A)
    C.assign[[1, 3, 5], [2, 4]] = 0
    assert C == result_block
    C = Matrix.new_from_existing(A)
    C.assign[1::2, 2:5:2] = 0
    assert C == result_block
    # Test row
    result_row = Matrix.new_from_values(
        [3, 0, 6, 0, 6, 6, 2, 4, 1, 3, 5, 1, 1],
        [0, 1, 2, 3, 3, 4, 5, 5, 6, 2, 2, 2, 4],
        [3, 2, 5, 3, 7, 3, 1, 7, 4, 3, 1, 0, 0])
    C = Matrix.new_from_existing(A)
    C.assign[1, [2, 4]] = 0
    assert C == result_row
    C = Matrix.new_from_existing(A)
    C.assign[1, 2:5:2] = 0
    assert C == result_row
    # Test column
    result_column = Matrix.new_from_values(
        [3, 0, 6, 0, 6, 6, 2, 4, 1, 1, 1, 3, 5],
        [0, 1, 2, 3, 3, 4, 5, 5, 6, 4, 2, 2, 2],
        [3, 2, 5, 3, 7, 3, 1, 7, 4, 8, 0, 0, 0])
    C = Matrix.new_from_existing(A)
    C.assign[[1, 3, 5], 2] = 0
    assert C == result_column
    C = Matrix.new_from_existing(A)
    C.assign[1::2, 2] = 0
    assert C == result_column
示例#2
0
def test_mxm_mask(A):
    mask = Matrix.new_from_values([0, 3, 4], [2, 3, 2], [True, True, True],
                                  nrows=7,
                                  ncols=7)
    C = Matrix.new_from_existing(A)
    C[mask] = A.mxm(A, Semiring.PLUS_TIMES)
    result = Matrix.new_from_values(
        [0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 6, 6, 6],
        [1, 2, 3, 4, 6, 5, 0, 2, 3, 2, 5, 2, 2, 3, 4],
        [2, 9, 3, 8, 4, 1, 3, 3, 9, 7, 7, 1, 5, 7, 3])
    assert C == result
    C = Matrix.new_from_existing(A)
    C[~mask] = A.mxm(A, Semiring.PLUS_TIMES)
    result2 = Matrix.new_from_values(
        [0, 0, 0, 1, 1, 1, 1, 2, 3, 3, 5, 6, 6, 6],
        [0, 4, 6, 2, 3, 4, 5, 2, 1, 5, 5, 0, 2, 5],
        [9, 16, 8, 20, 28, 12, 56, 1, 6, 3, 1, 21, 21, 26])
    assert C == result2
    C = Matrix.new_from_existing(A)
    C[mask, REPLACE] = A.mxm(A, Semiring.PLUS_TIMES)
    result3 = Matrix.new_from_values([0, 3, 4], [2, 3, 2], [9, 9, 7],
                                     nrows=7,
                                     ncols=7)
    assert C == result3
    C2 = A.mxm(A, Semiring.PLUS_TIMES).new(mask=mask)
    assert C2 == result3
示例#3
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
示例#4
0
def test_assign(A):
    B = Matrix.new_from_values([0, 0, 1], [0, 1, 0], [9, 8, 7])
    result = Matrix.new_from_values([0, 0, 2, 3, 0, 3, 5, 6, 0, 6, 1, 6, 4, 1],
                                    [0, 5, 0, 0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6],
                                    [9, 8, 7, 3, 2, 3, 1, 5, 3, 7, 8, 3, 7, 4])
    C = Matrix.new_from_existing(A)
    C.assign[[0, 2], [0, 5]] = B
    assert C == result
    C = Matrix.new_from_existing(A)
    C.assign[:3:2, :6:5] = B
    assert C == result
示例#5
0
def test_ewise_mult(A):
    # Binary, Monoid, and Semiring
    B = Matrix.new_from_values([0, 0, 5], [1, 2, 2], [5, 4, 8],
                               nrows=7,
                               ncols=7)
    result = Matrix.new_from_values([0, 5], [1, 2], [10, 8], nrows=7, ncols=7)
    C = A.ewise_mult(B, BinaryOp.TIMES).new()
    assert C == result
    C[:] = A.ewise_mult(B, Monoid.TIMES)
    assert C == result
    C[:] = A.ewise_mult(B, Semiring.PLUS_TIMES)
    assert C == result
示例#6
0
def test_equal(A, v):
    assert A == A
    assert A != v
    C = Matrix.new_from_values([1], [1], [1])
    assert C != A
    C2 = Matrix.new_from_values([1], [1], [1], nrows=7, ncols=7)
    assert C2 != A
    C3 = Matrix.new_from_values(
        [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1],
        [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6],
        [3., 2., 3., 1., 5., 3., 7., 8., 3., 1., 7., 4.])
    assert C3 != A, 'different datatypes are not equal'
示例#7
0
def test_mxm_transpose(A):
    C = Matrix.new_from_existing(A)
    C[:] = A.mxm(A.T, Semiring.PLUS_TIMES)
    result = Matrix.new_from_values(
        [0, 0, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6],
        [0, 6, 1, 6, 2, 4, 3, 5, 6, 2, 4, 3, 5, 6, 0, 1, 3, 5, 6],
        [13, 21, 80, 24, 1, 7, 18, 3, 15, 7, 49, 3, 1, 5, 21, 24, 15, 5, 83])
    assert C == result
    C[:] = A.T.mxm(A, Semiring.PLUS_TIMES)
    result2 = Matrix.new_from_values(
        [0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 6, 6],
        [0, 2, 1, 3, 0, 2, 3, 4, 1, 2, 3, 4, 2, 3, 4, 6, 5, 4, 6],
        [9, 9, 4, 6, 9, 35, 35, 15, 6, 35, 58, 21, 15, 21, 73, 32, 50, 32, 16])
    assert C == result2
示例#8
0
def test_mxm_nonsquare():
    A = Matrix.new_from_values([0, 0, 0], [0, 2, 4], [1, 2, 3],
                               nrows=1,
                               ncols=5)
    B = Matrix.new_from_values([0, 2, 4], [0, 0, 0], [10, 20, 30],
                               nrows=5,
                               ncols=1)
    C = Matrix.new_from_type(A.dtype, nrows=1, ncols=1)
    C[:] = A.mxm(B, Semiring.MAX_PLUS)
    assert C.element[0, 0] == 33
    C1 = A.mxm(B, Semiring.MAX_PLUS).new()
    assert C1 == C
    C2 = A.T.mxm(B.T, Semiring.MAX_PLUS).new()
    assert C2.nrows == 5
    assert C2.ncols == 5
示例#9
0
def test_ewise_add(A):
    # Binary, Monoid, and Semiring
    B = Matrix.new_from_values([0, 0, 5], [1, 2, 2], [5, 4, 8],
                               nrows=7,
                               ncols=7)
    result = Matrix.new_from_values([0, 3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1],
                                    [2, 0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6],
                                    [4, 3, 5, 3, 8, 5, 3, 7, 8, 3, 1, 7, 4])
    C = A.ewise_add(B, BinaryOp.SECOND).new(
    )  # possibly surprising, but SECOND(x, empty) == x
    assert C == result
    C[:] = A.ewise_add(B, Monoid.MAX)
    assert C == result
    C[:] = A.ewise_add(B, Semiring.MAX_MINUS)
    assert C == result
示例#10
0
def test_assign_row(A, v):
    result = Matrix.new_from_values([3, 3, 5, 6, 6, 1, 6, 2, 4, 1, 0, 0, 0, 0],
                                    [0, 2, 2, 2, 3, 4, 4, 5, 5, 6, 1, 3, 4, 6],
                                    [3, 3, 1, 5, 7, 8, 3, 1, 7, 4, 1, 1, 2, 0])
    C = Matrix.new_from_existing(A)
    C.assign[0, :] = v
    assert C == result
示例#11
0
def test_mxm(A):
    C = A.mxm(A, Semiring.PLUS_TIMES).new()
    result = Matrix.new_from_values(
        [0, 0, 0, 0, 1, 1, 1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6],
        [0, 2, 4, 6, 2, 3, 4, 5, 2, 1, 3, 5, 2, 5, 0, 2, 5],
        [9, 9, 16, 8, 20, 28, 12, 56, 1, 6, 9, 3, 7, 1, 21, 21, 26])
    assert C == result
示例#12
0
def test_apply(A):
    result = Matrix.new_from_values(
        [3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1],
        [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6],
        [-3, -2, -3, -1, -5, -3, -7, -8, -3, -1, -7, -4])
    C = A.apply(UnaryOp.AINV).new()
    assert C == result
示例#13
0
def test_assign_column(A, v):
    result = Matrix.new_from_values(
        [3, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1, 1, 3, 4, 6],
        [0, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 1, 1, 1, 1],
        [3, 3, 1, 5, 3, 7, 8, 3, 1, 7, 4, 1, 1, 2, 0])
    C = Matrix.new_from_existing(A)
    C.assign[:, 1] = v
    assert C == result
示例#14
0
def test_new_from_values():
    C = Matrix.new_from_values([0, 1, 3], [1, 1, 2], [True, False, True])
    assert C.nrows == 4
    assert C.ncols == 3
    assert C.nvals == 3
    assert C.dtype == bool
    C2 = Matrix.new_from_values([0, 1, 3], [1, 1, 2], [12.3, 12.4, 12.5],
                                nrows=17,
                                ncols=3)
    assert C2.nrows == 17
    assert C2.ncols == 3
    assert C2.nvals == 3
    assert C2.dtype == float
    C3 = Matrix.new_from_values([0, 1, 1], [2, 1, 1], [1, 2, 3],
                                nrows=10,
                                dup_op=BinaryOp.TIMES)
    assert C3.nrows == 10
    assert C3.ncols == 3
    assert C3.nvals == 2  # duplicates were combined
    assert C3.dtype == int
    assert C3.element[1, 1] == 6  # 2*3
    with pytest.raises(ValueError):
        # Duplicate indices requires a dup_op
        Matrix.new_from_values([0, 1, 1], [2, 1, 1], [True, True, True])
    with pytest.raises(IndexOutOfBound):
        # Specified ncols can't hold provided indexes
        Matrix.new_from_values([0, 1, 3], [1, 1, 2], [12.3, 12.4, 12.5],
                               nrows=17,
                               ncols=2)
示例#15
0
def test_transpose(A):
    # C[:] = A.T
    rows, cols, vals = A.to_values()
    result = Matrix.new_from_values(cols, rows, vals)
    C = Matrix.new_from_type(A.dtype, A.ncols, A.nrows)
    C[:] = A.T
    assert C == result
    C2 = A.T.new()
    assert C2 == result
示例#16
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
示例#17
0
def test_mxm_accum(A):
    A[BinaryOp.PLUS] = A.mxm(A, Semiring.PLUS_TIMES)
    result = Matrix.new_from_values([
        0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6,
        6, 6, 6
    ], [
        0, 1, 2, 3, 4, 6, 2, 3, 4, 5, 6, 2, 5, 0, 1, 2, 3, 5, 2, 5, 2, 5, 0, 2,
        3, 4, 5
    ], [
        9, 2, 9, 3, 16, 8, 20, 28, 20, 56, 4, 1, 1, 3, 6, 3, 9, 3, 7, 7, 1, 1,
        21, 26, 7, 3, 26
    ])
    assert A == result
示例#18
0
def test_extract(A):
    C = Matrix.new_from_type(A.dtype, 3, 4)
    result = Matrix.new_from_values([0, 0, 1, 2, 2, 2], [0, 2, 1, 1, 2, 3],
                                    [2, 3, 3, 5, 7, 3],
                                    nrows=3,
                                    ncols=4)
    C[:] = A.extract[[0, 3, 6], [1, 2, 3, 4]]
    assert C == result
    C[:] = A.extract[0::3, 1:5]
    assert C == result
    C[:] = A.extract[[0, 3, 6], 1:5:1]
    assert C == result
    C2 = A.extract[[0, 3, 6], [1, 2, 3, 4]].new()
    assert C2 == result
示例#19
0
文件: test_op.py 项目: eriknw/grblas
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
示例#20
0
def test_new_from_values_invalid_dtype():
    with pytest.raises(OverflowError):
        Matrix.new_from_values([0, 1, 2], [2, 0, 1], [0, 2, 3],
                               dtype=dtypes.BOOL)
示例#21
0
def A():
    data = [[3, 0, 3, 5, 6, 0, 6, 1, 6, 2, 4, 1],
            [0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6],
            [3, 2, 3, 1, 5, 3, 7, 8, 3, 1, 7, 4]]
    return Matrix.new_from_values(*data)
示例#22
0
def test_assign_wrong_dims(A):
    B = Matrix.new_from_values([0, 0, 1], [0, 1, 0], [9, 8, 7])
    with pytest.raises(DimensionMismatch):
        A.assign[[0, 2, 4], [0, 5]] = B