예제 #1
0
def test_reshape():
    array = MutableDenseNDimArray(range(50), 50)
    assert array.shape == (50,)
    assert array.rank() == 1

    array = array.reshape(5, 5, 2)
    assert array.shape == (5, 5, 2)
    assert array.rank() == 3
    assert len(array) == 50
예제 #2
0
def test_ndim_array_initiation():
    arr_with_one_element = MutableDenseNDimArray([23])
    assert len(arr_with_one_element) == 1
    assert arr_with_one_element[0] == 23
    assert arr_with_one_element.rank() == 1
    pytest.raises(ValueError, lambda: arr_with_one_element[1])

    arr_with_symbol_element = MutableDenseNDimArray([Symbol('x')])
    assert len(arr_with_symbol_element) == 1
    assert arr_with_symbol_element[0] == Symbol('x')
    assert arr_with_symbol_element.rank() == 1

    number5 = 5
    vector = MutableDenseNDimArray.zeros(number5)
    assert len(vector) == number5
    assert vector.shape == (number5,)
    assert vector.rank() == 1
    pytest.raises(ValueError, lambda: arr_with_one_element[5])

    vector = MutableSparseNDimArray.zeros(number5)
    assert len(vector) == number5
    assert vector.shape == (number5,)
    assert not vector._sparse_array
    assert vector.rank() == 1

    n_dim_array = MutableDenseNDimArray(range(3**4), (3, 3, 3, 3,))
    assert len(n_dim_array) == 3 * 3 * 3 * 3
    assert n_dim_array.shape == (3, 3, 3, 3)
    assert n_dim_array.rank() == 4
    pytest.raises(ValueError, lambda: n_dim_array[0, 0, 0, 3])
    pytest.raises(ValueError, lambda: n_dim_array[3, 0, 0, 0])
    pytest.raises(ValueError, lambda: n_dim_array[3**4])

    array_shape = (3, 3, 3, 3)
    sparse_array = MutableSparseNDimArray.zeros(*array_shape)
    assert len(sparse_array._sparse_array) == 0
    assert len(sparse_array) == 3 * 3 * 3 * 3
    assert n_dim_array.shape == array_shape
    assert n_dim_array.rank() == 4

    one_dim_array = MutableDenseNDimArray([2, 3, 1])
    assert len(one_dim_array) == 3
    assert one_dim_array.shape == (3,)
    assert one_dim_array.rank() == 1
    assert one_dim_array.tolist() == [2, 3, 1]

    shape = (3, 3)
    array_with_many_args = MutableSparseNDimArray.zeros(*shape)
    assert len(array_with_many_args) == 3 * 3
    assert array_with_many_args.shape == shape
    assert array_with_many_args[0, 0] == 0
    assert array_with_many_args.rank() == 2

    pytest.raises(TypeError, lambda: MutableDenseNDimArray(1))
    pytest.raises(TypeError, lambda: MutableDenseNDimArray([1, 2], shape=(1.2, 3)))
    pytest.raises(ValueError, lambda: MutableDenseNDimArray([[1, 2], [3]]))

    b = MutableDenseNDimArray([1, 2, 3, 4], shape=(2, 2))
    c = MutableDenseNDimArray(b)
    assert c.shape == (2, 2)

    a2 = MutableDenseNDimArray([[2, 3], [4, 5]])
    pytest.raises(ValueError, lambda: a2[10])
    pytest.raises(ValueError, lambda: a2[1, 1, 1])
    pytest.raises(ValueError, lambda: a2[3, 3])