Пример #1
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
Пример #2
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
Пример #3
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
Пример #4
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
Пример #5
0
def test_new_from_existing(A):
    C = Matrix.new_from_existing(A)
    assert C is not A
    assert C.dtype == A.dtype
    assert C.nvals == A.nvals
    assert C.nrows == A.nrows
    assert C.ncols == A.ncols
    # Ensure they are not the same backend object
    A.element[0, 0] = 1000
    assert C.element[0, 0] != 1000
Пример #6
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
Пример #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