예제 #1
0
def test_from_scalar():
    x = Tensor.from_scalar(-2.0)

    assert x.order == 0
    assert x.dimensions == ()
    assert x.modes == ()
    assert x.mode_ordering == ()
    assert x.format == Format((), ())
    assert x.to_dok() == {(): -2.0}
예제 #2
0
def test_from_dense_lil_scalar():
    format = Format((), ())
    x = Tensor.from_lol(2.0, dimensions=(), format=format)

    assert x.order == 0
    assert x.dimensions == ()
    assert x.modes == ()
    assert x.mode_ordering == ()
    assert x.format == format
    assert x.to_dok() == {(): 2.0}
예제 #3
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
예제 #4
0
def test_from_dense_lil():
    format = Format((Mode.dense, Mode.dense), (0, 1))
    x = Tensor.from_lol(
        [[0, -4.0, 4.5], [0, -3.5, 2.5]],
        dimensions=(2, 3),
        format=format,
    )

    assert x.order == 2
    assert x.dimensions == (2, 3)
    assert x.modes == (Mode.dense, Mode.dense)
    assert x.mode_ordering == (0, 1)
    assert x.format == format
    assert x.to_dok() == {
        (0, 1): -4.0,
        (0, 2): 4.5,
        (1, 1): -3.5,
        (1, 2): 2.5,
    }
예제 #5
0
def test_from_soa():
    format = Format((Mode.dense, Mode.compressed, Mode.compressed), (0, 1, 2))
    x = Tensor.from_soa(
        ([0, 1, 1, 0], [0, 0, 1, 1], [0, 1, 2, 1]),
        [4.5, 3.2, -3.0, 5.0],
        dimensions=(2, 3, 3),
        format=format,
    )

    assert x.order == 3
    assert x.dimensions == (2, 3, 3)
    assert x.modes == (Mode.dense, Mode.compressed, Mode.compressed)
    assert x.mode_ordering == (0, 1, 2)
    assert x.format == format
    assert x.to_dok() == {
        (0, 0, 0): 4.5,
        (1, 0, 1): 3.2,
        (1, 1, 2): -3.0,
        (0, 1, 1): 5.0,
    }
예제 #6
0
def test_from_aos():
    format = Format((Mode.dense, Mode.compressed), (1, 0))
    x = Tensor.from_aos(
        [(1, 0), (1, 1), (2, 1), (3, 1)],
        [4.5, 3.2, -3.0, 5.0],
        dimensions=(4, 3),
        format=format,
    )

    assert x.order == 2
    assert x.dimensions == (4, 3)
    assert x.modes == (Mode.dense, Mode.compressed)
    assert x.mode_ordering == (1, 0)
    assert x.format == format
    assert x.to_dok() == {
        (1, 0): 4.5,
        (1, 1): 3.2,
        (2, 1): -3.0,
        (3, 1): 5.0,
    }
예제 #7
0
def test_differing_sizes():
    with pytest.raises(ValueError):
        _ = Format((Mode.dense, ), (0, 1))
예제 #8
0
def test_format_attributes():
    format = Format((Mode.dense, Mode.compressed), (1, 0))

    assert format.order == 2
    assert format.modes[0] == Mode.dense
예제 #9
0
import pytest
from parsita import Failure

from tensora import Format, Mode
from tensora.format import parse_format

format_strings = [
    ('', Format((), ())),
    ('d', Format((Mode.dense, ), (0, ))),
    ('s', Format((Mode.compressed, ), (0, ))),
    ('ds', Format((Mode.dense, Mode.compressed), (0, 1))),
    ('sd', Format((Mode.compressed, Mode.dense), (0, 1))),
    ('d1s0', Format((Mode.dense, Mode.compressed), (1, 0))),
    ('d1s0s2', Format((Mode.dense, Mode.compressed, Mode.compressed),
                      (1, 0, 2))),
]


@pytest.mark.parametrize('string,format', format_strings)
def test_parse_format(string, format):
    actual = parse_format(string).or_die()
    assert actual == format


@pytest.mark.parametrize('string,format', format_strings)
def test_deparse_format(string, format):
    actual = format.deparse()
    assert actual == string


@pytest.mark.parametrize('string', ['df', '1d0s', 'd0s', 'd0s1s1', 'd1s2s3'])