Пример #1
0
def test_lenore_example_1_symbols():
    _A = LazyArray(name='A', shape=('n', 'm'))
    _B = LazyArray(name='B', shape=('l', 3))
    python_source = compiler((_A + _B).T['i'].context)

    local_dict = {}
    exec(python_source, globals(), local_dict)

    A = Array((2, 3), (1, 2, 3, 4, 5, 6))
    B = Array((2, 3), (7, 8, 9, 10, 11, 12))
    i = Array((), (0))
    C = local_dict['f'](A=A, B=B, i=i)
    assert C.shape == (2,)
    assert C.value == [8, 14]
Пример #2
0
def test_lenore_example_1():
    _A = LazyArray(name='A', shape=(2, 3))
    _B = LazyArray(name='B', shape=(2, 3))
    python_source = compiler((_A + _B).T[0].context)
    print(python_source)

    local_dict = {}
    exec(python_source, globals(), local_dict)

    A = Array((2, 3), (1, 2, 3, 4, 5, 6))
    B = Array((2, 3), (7, 8, 9, 10, 11, 12))
    C = local_dict['f'](A, B)
    assert C.shape == (2,)
    assert C.value == [8, 14]
Пример #3
0
def test_array_complex_slice():
    _A = LazyArray(name='A', shape=(3, 4, 5))
    _B = LazyArray(name='B', shape=(3, 4, 5))

    expression = _A[0, 0:2:-1] + _B[:, 1:3, :][1]

    local_dict = {}
    exec(expression.compile(), globals(), local_dict)

    A = Array(shape=(3, 4, 5), value=tuple(range(1, 60)))
    B = Array(shape=(3, 4, 5), value=tuple(range(61, 121)))
    B = local_dict['f'](A=A)

    assert B.shape == (3, 5)
    assert B.value == [3, 7, 11]
Пример #4
0
def test_array_scalar_opperations_invalid():
    a = Array(shape=(1, 2), value=(1, 2))

    with pytest.raises(TypeError):
        a + 1

    with pytest.raises(TypeError):
        1 + a

    with pytest.raises(TypeError):
        a - 1

    with pytest.raises(TypeError):
        1 - a

    with pytest.raises(TypeError):
        a * 1

    with pytest.raises(TypeError):
        1 * a

    with pytest.raises(TypeError):
        a / 1

    with pytest.raises(TypeError):
        1 / a
Пример #5
0
def test_array_frontend_transpose_vector_outer_scalar_addition():
    _A = LazyArray(name='A', shape=(3, 2))
    _B = LazyArray(name='B', shape=(4, ))
    _C = LazyArray(name='C', shape=(3, 4))

    expression = (((_A.T)[0] - 1).outer('*', _B) + _C + 'n').transpose([1, 0])

    local_dict = {}
    exec(expression.compile(), globals(), local_dict)

    A = Array(shape=(3, 2), value=(1, 2, 3, 4, 5, 6))
    B = Array(shape=(4, ), value=(13, 14, 15, 16))
    C = Array(shape=(3, 4),
              value=(17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28))
    n = Array(shape=(), value=(4, ))

    D = local_dict['f'](A=A, B=B, C=C, n=n)

    assert D.shape == (4, 3)
    assert D.value == [21, 51, 81, 22, 54, 86, 23, 57, 91, 24, 60, 96]
Пример #6
0
def test_array_scalar_opperations_valid():
    a = Array(shape=(), value=(3, ))

    assert a + 1 == 4
    assert 1 + a == 4
    assert a - 1 == 2
    assert 1 - a == -2
    assert a * 1 == 3
    assert 1 * a == 3
    assert a / 1 == 3
    assert abs(1 / a - 0.3333333333333) < 1e-6
Пример #7
0
def test_array_reduction():
    _A = LazyArray(name='A', shape=(3, 2))

    expression = _A.reduce('+')

    local_dict = {}
    print(expression.compile())
    exec(expression.compile(), globals(), local_dict)

    A = Array(shape=(3, 2), value=tuple(range(1, 7)))
    B = local_dict['f'](A=A)

    assert B.shape == (2, )
    assert B.value == [9, 12]
Пример #8
0
def test_array_get_index():
    a = Array(shape=(1, 2, 3), value=(1, 2, 3, 4, 5, 6), fmt='row')
    assert a[0, 0, 0] == 1
    assert a[0, 0, 1] == 2
    assert a[0, 0, 2] == 3
    assert a[0, 1, 0] == 4
    assert a[0, 1, 2] == 6

    # partial index
    with pytest.raises(IndexError):
        a[1, 1]

    # out of bounds index
    with pytest.raises(IndexError):
        a[1, 1, 1] == 10
Пример #9
0
def test_array_scalar_comparison_valid():
    a = Array(shape=(), value=(3, ))

    assert (a > 1) == True
    assert (1 > a) == False
    assert (a >= 1) == True
    assert (1 >= a) == False
    assert (a < 1) == False
    assert (1 < a) == True
    assert (a <= 1) == False
    assert (1 <= a) == True
    assert (a == 1) == False
    assert (1 == a) == False
    assert (a != 1) == True
    assert (1 != a) == True
Пример #10
0
def test_array_scalar_comparison_invalid():
    a = Array(shape=(1, 2), value=(1, 2))

    with pytest.raises(TypeError):
        a > 1

    with pytest.raises(TypeError):
        1 > a

    with pytest.raises(TypeError):
        a >= 1

    with pytest.raises(TypeError):
        1 >= a

    with pytest.raises(TypeError):
        a < 1

    with pytest.raises(TypeError):
        1 < a

    with pytest.raises(TypeError):
        a <= 1

    with pytest.raises(TypeError):
        1 <= a

    with pytest.raises(TypeError):
        a == 1

    with pytest.raises(TypeError):
        1 == a

    with pytest.raises(TypeError):
        a != 1

    with pytest.raises(TypeError):
        1 != a
Пример #11
0
def test_array_invalid_shape():
    Array(shape=(2, 3, 2), value=(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12))

    with pytest.raises(ValueError):
        Array(shape=(1, 2, 3), value=(1, 2), fmt='row')
Пример #12
0
def test_array_set_index():
    a = Array(shape=(1, 2, 3), value=(1, 2, 3, 4, 5, 6), fmt='row')
    a[0, 1, 1] = 10
    a[0, 1, 1] == 10
Пример #13
0
def test_array_dimension():
    a = Array(shape=(1, 2, 3), value=(1, 2, 3, 4, 5, 6), fmt='row')

    assert len(a.shape) == 3