Opened 5 years ago

Closed 4 years ago

#23336 closed defect (wontfix)

matrix creation: iterator exhausts

Reported by: dkrenn Owned by:
Priority: major Milestone: sage-duplicate/invalid/wontfix
Component: linear algebra Keywords:
Cc: Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: #24742 Stopgaps:

Status badges

Description (last modified by jdemeyer)

sage: Matrix(ZZ, 2, 2, iter(srange(6)))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-d499495bdf36> in <module>()
----> 1 Matrix(ZZ, Integer(2), Integer(2), iter(srange(Integer(6))))

/usr/local/src/sage-git/src/sage/matrix/constructor.pyx in sage.matrix.constructor.MatrixFactory.__call__ (build/cythonized/sage/matrix/constructor.c:7025)()
    784             ncols = nrows
    785 
--> 786         return MatrixSpace(ring, nrows, ncols, sparse=sparse)(entries)
    787 
    788 Matrix = matrix = MatrixFactory()

/usr/local/src/sage-git/local/lib/python2.7/site-packages/sage/matrix/matrix_space.pyc in __call__(self, entries, coerce, copy, sparse)
    873             [t]
    874         """
--> 875         return self.matrix(entries, coerce, copy)
    876 
    877     def change_ring(self, R):

/usr/local/src/sage-git/local/lib/python2.7/site-packages/sage/matrix/matrix_space.pyc in matrix(self, x, coerce, copy)
   1886                 x = list_to_dict(x, m, n)
   1887                 copy = False
-> 1888         return MC(self, x, copy=copy, coerce=coerce)
   1889 
   1890     def matrix_space(self, nrows=None, ncols=None, sparse=False):

/usr/local/src/sage-git/src/sage/matrix/matrix_integer_dense.pyx in sage.matrix.matrix_integer_dense.Matrix_integer_dense.__init__ (build/cythonized/sage/matrix/matrix_integer_dense.c:7039)()
    341             # Create the matrix whose entries are in the given entry list.
    342             if len(entries_list) != self._nrows * self._ncols:
--> 343                 raise TypeError("entries has the wrong length")
    344             if coerce:
    345                 k = 0

TypeError: entries has the wrong length

The problem is that it reads all the values from the given iterator and not just as many as needed.

Change History (8)

comment:1 Changed 4 years ago by jdemeyer

  • Description modified (diff)

comment:2 follow-up: Changed 4 years ago by jdemeyer

Why do you consider this a bug? You are creating a 2 x 2 matrix while giving 6 entries. This works:

sage: Matrix(ZZ, 2, 2, iter(srange(4)))
[0 1]
[2 3]

comment:3 in reply to: ↑ 2 Changed 4 years ago by dkrenn

Replying to jdemeyer:

Why do you consider this a bug? You are creating a 2 x 2 matrix while giving 6 entries.

The answer was in the original desciption (before the modification):

error [...] because it reads all the values from the given iterable and not only the as many as needed. And from a command which allows as input an iterator, it would be pleasant if it only reads as many elements as needed and not more.

comment:4 Changed 4 years ago by jdemeyer

OK, but then what do you do with

sage: Matrix(ZZ, 2, 2, range(6))

Is that an error or should it just "truncate" the range?

The main problem here is that Matrix allows many different kinds of input in a rather loose way. If you can up with a good specification of how iterables should be handled and what should be an error and what not, I'm ready to listen. But lacking that, I consider it a feature that Matrix(ZZ, 2, 2, iter(srange(6))) is an error.

comment:5 Changed 4 years ago by jdemeyer

  • Description modified (diff)

comment:6 Changed 4 years ago by jdemeyer

Do you have an actual reasonable use case for this, or is it just "it feels wrong".

comment:7 Changed 4 years ago by jdemeyer

  • Dependencies set to #24742
  • Milestone changed from sage-8.0 to sage-8.2

I have no plans to "fix" this but if somebody does, it should be using #24742.

comment:8 Changed 4 years ago by jdemeyer

  • Milestone changed from sage-8.2 to sage-duplicate/invalid/wontfix
  • Resolution set to wontfix
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.