예제 #1
0
def test_sparse():
    sparse_array = MutableSparseNDimArray([0, 0, 0, 1], (2, 2))
    assert len(sparse_array) == 2 * 2
    # dictionary where all data is, only non-zero entries are actually stored:
    assert len(sparse_array._sparse_array) == 1

    assert list(sparse_array) == [0, 0, 0, 1]

    for i, j in zip(sparse_array, [0, 0, 0, 1]):
        assert i == j

    sparse_array[0, 0] = 123
    assert len(sparse_array._sparse_array) == 2
    assert sparse_array[0, 0] == 123

    # when element in sparse array become zero it will disappear from
    # dictionary
    sparse_array[0, 0] = 0
    assert len(sparse_array._sparse_array) == 1
    sparse_array[1, 1] = 0
    assert len(sparse_array._sparse_array) == 0
    assert sparse_array[0, 0] == 0

    # test for large scale sparse array
    a = MutableSparseNDimArray.zeros(100000, 200000)
    b = MutableSparseNDimArray.zeros(100000, 200000)
    assert a == b
    a[1, 1] = 1
    b[1, 1] = 2
    assert a != b
예제 #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
    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
    raises(ValueError, lambda: arr_with_one_element[5])

    vector = MutableSparseNDimArray.zeros(number5)
    assert len(vector) == number5
    assert vector.shape == (number5,)
    assert 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
    raises(ValueError, lambda: n_dim_array[0, 0, 0, 3])
    raises(ValueError, lambda: n_dim_array[3, 0, 0, 0])
    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
예제 #3
0
def test_sparse():
    sparse_array = MutableSparseNDimArray([0, 0, 0, 1], (2, 2))
    assert len(sparse_array) == 2 * 2
    # dictionary where all data is, only non-zero entries are actually stored:
    assert len(sparse_array._sparse_array) == 1

    assert sparse_array.tolist() == [[0, 0], [0, 1]]

    for i, j in zip(sparse_array, [[0, 0], [0, 1]]):
        assert i == MutableSparseNDimArray(j)

    sparse_array[0, 0] = 123
    assert len(sparse_array._sparse_array) == 2
    assert sparse_array[0, 0] == 123
    assert sparse_array / 0 == MutableSparseNDimArray(
        [[S.ComplexInfinity, S.NaN], [S.NaN, S.ComplexInfinity]], (2, 2))

    # when element in sparse array become zero it will disappear from
    # dictionary
    sparse_array[0, 0] = 0
    assert len(sparse_array._sparse_array) == 1
    sparse_array[1, 1] = 0
    assert len(sparse_array._sparse_array) == 0
    assert sparse_array[0, 0] == 0

    # test for large scale sparse array
    # equality test
    a = MutableSparseNDimArray.zeros(100000, 200000)
    b = MutableSparseNDimArray.zeros(100000, 200000)
    assert a == b
    a[1, 1] = 1
    b[1, 1] = 2
    assert a != b

    # __mul__ and __rmul__
    assert a * 3 == MutableSparseNDimArray({200001: 3}, (100000, 200000))
    assert 3 * a == MutableSparseNDimArray({200001: 3}, (100000, 200000))
    assert a * 0 == MutableSparseNDimArray({}, (100000, 200000))
    assert 0 * a == MutableSparseNDimArray({}, (100000, 200000))

    # __div__
    assert a / 3 == MutableSparseNDimArray({200001: Rational(1, 3)},
                                           (100000, 200000))

    # __neg__
    assert -a == MutableSparseNDimArray({200001: -1}, (100000, 200000))
예제 #4
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
    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
    raises(ValueError, lambda: arr_with_one_element[5])

    vector = MutableSparseNDimArray.zeros(number5)
    assert len(vector) == number5
    assert vector.shape == (number5, )
    assert 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
    raises(ValueError, lambda: n_dim_array[0, 0, 0, 3])
    raises(ValueError, lambda: n_dim_array[3, 0, 0, 0])
    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

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

    vector_with_long_shape = MutableDenseNDimArray(range(5), long(5))
    assert len(vector_with_long_shape) == 5
    assert vector_with_long_shape.shape == (long(5), )
    assert vector_with_long_shape.rank() == 1
    raises(ValueError, lambda: vector_with_long_shape[long(5)])

    from sympy.abc import x
    for ArrayType in [MutableDenseNDimArray, MutableSparseNDimArray]:
        rank_zero_array = ArrayType(x)
        assert len(rank_zero_array) == 1
        assert rank_zero_array.shape == ()
        assert rank_zero_array.rank() == 0
        assert rank_zero_array[()] == x
        raises(ValueError, lambda: rank_zero_array[0])