Skip to content

Commit

Permalink
Improve test coverage for sympy/tensor/array
Browse files Browse the repository at this point in the history
Drop untested and/or inaccessible code, add some
trivial tests.
  • Loading branch information
skirpichev committed Jul 17, 2016
1 parent 14e8188 commit f20cee8
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 36 deletions.
3 changes: 1 addition & 2 deletions sympy/printing/str.py
Expand Up @@ -505,8 +505,7 @@ def _print_MatPow(self, expr):
def _print_ImmutableDenseNDimArray(self, expr):
return str(expr)

def _print_ImmutableSparseNDimArray(self, expr):
return str(expr)
_print_ImmutableSparseNDimArray = _print_ImmutableDenseNDimArray

def _print_Integer(self, expr):
return str(expr.p)
Expand Down
5 changes: 1 addition & 4 deletions sympy/tensor/array/dense_ndim_array.py
Expand Up @@ -10,9 +10,6 @@

class DenseNDimArray(NDimArray):

def __new__(cls, *args, **kwargs):
return ImmutableDenseNDimArray(*args, **kwargs)

def __getitem__(self, index):
"""
Allows to get items from N-dim array.
Expand Down Expand Up @@ -104,7 +101,7 @@ def reshape(self, *newshape):
"""
new_total_size = functools.reduce(lambda x, y: x*y, newshape)
if new_total_size != self._loop_size:
raise ValueError("Invalid reshape parameters " + newshape)
raise ValueError("Invalid reshape parameters " + str(newshape))

# there is no `.func` as this class does not subtype `Basic`:
return type(self)(self._array, newshape)
Expand Down
20 changes: 0 additions & 20 deletions sympy/tensor/array/ndim_array.py
Expand Up @@ -55,9 +55,6 @@ class NDimArray(object):
[[-3, -3], [-3, -3]]
"""
def __new__(cls, *args, **kwargs):
from sympy.tensor.array import ImmutableDenseNDimArray
return ImmutableDenseNDimArray(*args, **kwargs)

def _parse_index(self, index):

Expand Down Expand Up @@ -207,23 +204,6 @@ def f(sh, shape_left, i, j):

return f(self._loop_size, self.shape, 0, self._loop_size)

out_str = ''

# forming output string
for i, el in enumerate(self):

out_str += str(el) + ' '
chidx = i+1
for sh in reversed(self.shape):
if chidx % sh == 0:
out_str += '\n'
chidx //= sh

return out_str

def __repr__(self):
return self.__str__()

def tolist(self):
"""
Conveting MutableDenseNDimArray to one-dim list
Expand Down
10 changes: 0 additions & 10 deletions sympy/tensor/array/sparse_ndim_array.py
Expand Up @@ -8,9 +8,6 @@

class SparseNDimArray(NDimArray):

def __new__(cls, *args, **kwargs):
return ImmutableSparseNDimArray(*args, **kwargs)

def __getitem__(self, index):
"""
Get an element from a sparse N-dim array.
Expand Down Expand Up @@ -77,13 +74,6 @@ def iterator():
yield self[i]
return iterator()

def reshape(self, *newshape):
new_total_size = functools.reduce(lambda x, y: x*y, newshape)
if new_total_size != self._loop_size:
raise ValueError("Invalid reshape parameters " + newshape)

return type(self)(*(newshape + (self._array,)))


class ImmutableSparseNDimArray(SparseNDimArray, Expr):

Expand Down
5 changes: 5 additions & 0 deletions sympy/tensor/array/tests/test_immutable_ndim_array.py
Expand Up @@ -69,6 +69,8 @@ def test_reshape():
assert array.rank() == 3
assert len(array) == 50

pytest.raises(ValueError, lambda: array.reshape(1, 1))


def test_iterator():
array = ImmutableDenseNDimArray(range(4), (2, 2))
Expand Down Expand Up @@ -145,6 +147,9 @@ def test_ndim_array_converting():
matrix = sparse_array.tomatrix()
assert(isinstance(matrix, SparseMatrix))

pytest.raises(ValueError,
lambda: ImmutableDenseNDimArray([1]*6, (2, 2, 2)).tomatrix())

for i in range(len(sparse_array)):
assert sparse_array[i] == matrix[i]
assert matrix.shape == sparse_array.shape
Expand Down
5 changes: 5 additions & 0 deletions sympy/tensor/array/tests/test_mutable_ndim_array.py
@@ -1,5 +1,7 @@
from copy import copy

import pytest

from sympy.tensor.array.dense_ndim_array import MutableDenseNDimArray
from sympy import Symbol, Rational, SparseMatrix
from sympy.matrices import Matrix
Expand Down Expand Up @@ -145,6 +147,9 @@ def test_ndim_array_converting():
matrix = sparse_array.tomatrix()
assert(isinstance(matrix, SparseMatrix))

pytest.raises(ValueError,
lambda: MutableSparseNDimArray([1]*6, (2, 2, 2)).tomatrix())

for i in range(len(sparse_array)):
assert sparse_array[i] == matrix[i]
assert matrix.shape == sparse_array.shape
Expand Down

0 comments on commit f20cee8

Please sign in to comment.