Status: Valid
Owner: ----
Labels: Type-Defect Priority-Medium Matrices

New issue 3401 by [email protected]: Slicing an empty matrix is broken
http://code.google.com/p/sympy/issues/detail?id=3401

This used to work:

In [4]: Matrix(0, 3, [])[:, 0]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-4-4596a324e212> in <module>()
----> 1 Matrix(0, 3, [])[:, 0]

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/matrices/matrices.py in __getitem__(self, key)
    286             i, j = key
    287             if type(i) is slice or type(j) is slice:
--> 288                 return self.submatrix(key)
    289
    290             else:

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/matrices/matrices.py in submatrix(self, keys)
   1022         extract
   1023         """
-> 1024         rlo, rhi, clo, chi = self.key2bounds(keys)
   1025         outLines, outCols = rhi-rlo, chi-clo
   1026         outMat = [0]*outLines*outCols

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/matrices/matrices.py in key2bounds(self, keys)
   1097             clo, chi = self.slice2bounds(keys[1], self.cols)
   1098         else:
-> 1099             _, clo = self.slice2bounds((0, keys[1]), self.cols)
   1100             chi = clo + 1
   1101         if not ( 0<=rlo<=rhi and 0<=clo<=chi ):

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/matrices/matrices.py in slice2bounds(self, key, defmax)
   1138         elif isinstance(key, tuple):
   1139             key = [defmax - 1 if i == -1 else i for i in key]
-> 1140             return self.key2ij(key)
   1141         else:
   1142             raise IndexError("Improper index type")

/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/matrices/matrices.py in key2ij(self, key) 1120 i, j = [(k + n) if k < 0 else k for k, n in zip(key, (self.rows, self.cols))]
   1121         if not (i>=0 and i<self.rows and j>=0 and j < self.cols):
-> 1122             raise IndexError("Index out of range: a[%s]"%repr(key))
   1123         return i,j
   1124

IndexError: Index out of range: a[[0, 0]]

It was broken by the commit

commit d8e586b4debff9ac51a9fd7ea59b298ce6bfad40
Author: Chris Smith <[email protected]>
Date:   Wed Nov 30 14:55:40 2011 +0545

    key2ij is used rather than slice to resolve int indices

The key2ij routine is also moved closer to the functions that use it.

        Factor out key2bounds code in matrices, too.

The problem is that the internal routines convert the first : to a range of 0 to 0, which is wrong (it should have no range). It then tests 0 <= 0 < 0, which gives False.

It's not clear how this should be fixed, though. There are four functions in matrices.py involved here: __getitem__, key2bounds, key2ij, and slice2bounds. It's not clear in which of these functions the change should go, or what it should look like. They all seem to operate on a core assumption that any slice can be represented by a range, which is not true in this case as I said.

This is causing a test failure in pull request 1530.

--
You received this message because you are subscribed to the Google Groups 
"sympy-issues" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy-issues?hl=en.

Reply via email to