Example #1
0
def test_unsorted_coordinates(format, indices, vals, permutation):
    data = [
        ((0, 0), 6),
        ((0, 2), 9),
        ((0, 3), 8),
        ((2, 0), 5),
        ((2, 3), 7),
    ]

    permutated_data = [data[i] for i in permutation]
    coordinates, values = zip(*permutated_data)

    A = Tensor.from_aos(coordinates, values, dimensions=(3, 4), format=format)

    assert A.taco_indices == indices
    assert A.taco_vals == vals

    A = Tensor.from_soa(zip(*coordinates),
                        values,
                        dimensions=(3, 4),
                        format=format)

    assert A.taco_indices == indices
    assert A.taco_vals == vals

    A = Tensor.from_dok(dict(permutated_data),
                        dimensions=(3, 4),
                        format=format)

    assert A.taco_indices == indices
    assert A.taco_vals == vals
Example #2
0
def test_to_dok():
    dok = {
        (2, 3): 2.0,
        (0, 1): 0.0,
        (1, 2): -1.0,
        (0, 3): 0.0,
    }
    dok_no_zeros = {key: value for key, value in dok.items() if value != 0}
    x = Tensor.from_dok(dok)

    assert x.to_dok() == dok_no_zeros
    assert x.to_dok(explicit_zeros=True) == dok
Example #3
0
def test_matrix_multiply_too_many_dimensions():
    a = Tensor.from_lol([3, 2, 5])
    b = Tensor.from_dok(
        {
            (0, 0, 0): 4.5,
            (1, 0, 1): 3.2,
            (1, 1, 2): -3.0,
            (0, 1, 1): 5.0,
        },
        dimensions=(3, 3, 3))

    with pytest.raises(ValueError):
        _ = a @ b
Example #4
0
def test_figure_2e(format, indices, vals):
    data = {
        (0, 0): 5,
        (0, 1): 1,
        (1, 0): 7,
        (1, 1): 3,
        (3, 0): 8,
        (3, 3): 4,
        (3, 4): 9,
    }
    a = Tensor.from_dok(data, dimensions=(4, 6), format=format)

    assert a.taco_indices == indices
    assert a.taco_vals == vals
Example #5
0
def test_figure_2m(format, indices, vals):
    data = {
        (0, 0, 0): 1,
        (0, 0, 1): 7,
        (0, 2, 1): 5,
        (2, 0, 1): 2,
        (2, 2, 0): 4,
        (2, 2, 1): 8,
        (2, 3, 0): 3,
        (2, 3, 1): 9,
    }
    a = Tensor.from_dok(data, dimensions=(3, 4, 2), format=format)

    assert a.taco_indices == indices
    assert a.taco_vals == vals
Example #6
0
def test_from_dok():
    data = {
        (2, 2): 2.0,
        (0, 2): -3.0,
        (1, 0): 2.0,
        (2, 3): 5.0,
    }
    format = Format((Mode.compressed, Mode.compressed), (0, 1))
    x = Tensor.from_dok(data, dimensions=(4, 5), format='ss')

    assert x.order == 2
    assert x.dimensions == (4, 5)
    assert x.modes == (Mode.compressed, Mode.compressed)
    assert x.mode_ordering == (0, 1)
    assert x.format == format
    assert x.to_dok() == data
Example #7
0
    with pytest.raises(ValueError):
        _ = a @ b


def test_equality():
    assert Tensor.from_lol([0, 2, 0],
                           format='d') == Tensor.from_lol([0, 2, 0],
                                                          format='s')
    assert Tensor.from_lol([0, 1, 0], format='d') != Tensor.from_lol(
        [0, 2, 0], format='s')
    assert Tensor.from_lol([0, 1, 0], format='d') != 1


@pytest.mark.parametrize('tensor', [
    Tensor.from_dok({}, dimensions=()),
    Tensor.from_dok({
        (2, 3): 2.0,
        (0, 1): 0.0,
        (1, 2): -1.0,
        (0, 3): 0.0
    },
                    dimensions=(2, 4)),
    Tensor.from_dok(
        {
            (0, 0, 0): 4.5,
            (1, 0, 1): 3.2,
            (1, 1, 2): -3.0,
            (0, 1, 1): 5.0
        },
        dimensions=(3, 3, 3)),