示例#1
0
def test_approximate_max_intensity():
    m1 = Matrix([
        [1.0, 0.0, 0.0],
        [1.0, 0.0, 0.0],
    ])
    m2 = Matrix([
        [0.0, 0.0, 1.0],
        [0.0, 1.0, 0.0],
    ])

    m1.approximate(m2, 1.0)
    expected = Matrix([
        [0.0, 0.0, 1.0],
        [0.0, 1.0, 0.0],
    ])
    assert matrixes_almost_equal(m1, expected)
示例#2
0
def test_approximate_nothing():
    m1 = Matrix([
        [1.0, 0.0, 0.0],
        [1.0, 0.0, 0.0],
    ])
    m2 = Matrix([
        [1.0, 0.0, 0.0],
        [1.0, 0.0, 0.0],
    ])

    m1.approximate(m2, 1.0)
    expected = Matrix([
        [1.0, 0.0, 0.0],
        [1.0, 0.0, 0.0],
    ])
    assert matrixes_almost_equal(m1, expected)
示例#3
0
def test_create_default():
    m = Matrix.create(2, 3)

    assert m.get(0, 0) == 0.0
    assert m.get(1, 2) == 0.0

    with pytest.raises(IndexError):
        m.get(2, 2)

    with pytest.raises(IndexError):
        m.get(1, 3)

    with pytest.raises(ValueError):
        Matrix.create(0, 3)

    with pytest.raises(ValueError):
        Matrix.create(2, 0)
示例#4
0
def test_average_similarity():
    m1 = Matrix([
        [0.1, 0.2, 0.3],
        [0.4, 0.5, 0.6],
    ])
    m2 = Matrix([
        [0.1, 0.1, 0.1],
        [0.4, 0.4, 0.4],
    ])

    # [0] sum min = 0.3, sum max = 0.6, limit = 0.3, res = 0.3 * 0.3 / 0.6 = 0.15
    # [1] sum min = 1.2, sum max = 1.5, limit = 0.6, res = 0.6 * 1.2 / 1.5 = 0.48
    # avg = 0.315

    sim = m1.average_similarity(m2)
    assert almost_equal(sim, 0.315)

    m3 = Matrix([
        [0.1, 0.2],
        [0.4, 0.5],
    ])
    with pytest.raises(ValueError):
        m1.average_similarity(m3)

    m4 = Matrix([
        [0.1, 0.1, 0.1],
        [0.4, 0.4, 0.4],
        [0.4, 0.4, 0.4],
    ])
    with pytest.raises(ValueError):
        m1.average_similarity(m4)
示例#5
0
def test_new_matrix():
    data = [
        [1.0, 2.0, 3.0],
        [4.0, 5.0, 6.0],
    ]
    m = Matrix(data)
    assert m.get_data() == data

    with pytest.raises(ValueError):
        Matrix([
            [1.0, 2.0, 3.0],
            [4.0, 5.0],
        ])

    with pytest.raises(ValueError):
        Matrix([])

    with pytest.raises(ValueError):
        Matrix([
            [],
            []
        ])
示例#6
0
def test_set():
    m = Matrix.create(2, 3)

    m.set(0, 0, 0.5)
    assert almost_equal(m.get(0, 0), 0.5)

    m.set(1, 2, 0.6)
    assert almost_equal(m.get(1, 2), 0.6)

    with pytest.raises(IndexError):
        m.set(2, 2, 1.0)

    with pytest.raises(IndexError):
        m.set(1, 3, 1.0)
示例#7
0
def test_approximate_some_intensity():
    m1 = Matrix([
        [1.0, 0.0, 0.0],
        [1.0, 0.0, 0.0],
    ])
    m2 = Matrix([
        [0.0, 0.0, 1.0],
        [0.0, 1.0, 0.0],
    ])

    m1.approximate(m2, 0.1)
    expected = Matrix([
        [0.9, 0.0, 0.1],
        [0.9, 0.1, 0.0],
    ])
    assert matrixes_almost_equal(m1, expected)

    m1.approximate(m2, 0.1)
    expected = Matrix([
        [0.819, 0.0, 0.19],
        [0.819, 0.19, 0.0],
    ])
    assert matrixes_almost_equal(m1, expected)
示例#8
0
def test_get_data():
    m = Matrix.create(2, 3, 1.0)

    expected_data = [
        [1.0, 1.0, 1.0],
        [1.0, 1.0, 1.0],
    ]
    assert m.get_data() == expected_data

    m.set(1, 1, 2.0)
    m.set(1, 2, 3.0)

    expected_data = [
        [1.0, 1.0, 1.0],
        [1.0, 2.0, 3.0],
    ]
    assert m.get_data() == expected_data
示例#9
0
def test_rows():
    m = Matrix.create(2, 3, 1.0)

    expected_data = [
        [1.0, 1.0, 1.0],
        [1.0, 1.0, 1.0],
    ]
    assert list(m.rows()) == expected_data

    m.set(1, 1, 2.0)
    m.set(1, 2, 3.0)

    expected_data = [
        [1.0, 1.0, 1.0],
        [1.0, 2.0, 3.0],
    ]
    assert list(m.rows()) == expected_data
示例#10
0
def test_create_with_initial_value():
    m = Matrix.create(2, 3, initial_value=0.5)

    assert m.get(0, 0) == 0.5
    assert m.get(1, 2) == 0.5