On Fri, Mar 2, 2012 at 6:19 PM, Nils Bruin <nbr...@sfu.ca> wrote: > On Mar 2, 12:28 am, Robert Bradshaw <rober...@math.washington.edu> > wrote: >> The difficulty with accepting an iterator (of strings) is that it is >> unclear if each item corresponds to a row or an element. But I would >> be in favor of rather liberal string parsing, so one could do > > Why is it easier to decide if the items correspond to rows or elements > in > > matrix( [ c for c in iterator ]) > > than it is in > > matrix( ( c for c in iterator ) )
That's not the difference. > I agree that this problem arises if we would allow the c's themselves > to just be generic iterators, which would include strings. Yep. I don't like matrix(["1 2", "3 4"]) because that's impossible to distinguish (using types alone) from matrix(["1", "2", "3", "4"]) (or the same replacing lists with iterators). Iterating over open("file")) iterates over the lines which would typically be entire rows. > (although you could argue that if no dimensions are given, matrix > should insist on [ [...], [...] , ... , [...]] rather than > interpreting [1,2,3] as [[1,2,3]]). > > Of course, accepting iterators would be at odds with accepting strings > straight away, as in matrix("1,2,3\n4,5,6"), so we'd have to make a > choice between those two. Currently strings nor iterators are accepted > by a bare matrix. However, for related objects the choice has already > been made in favor of iterators: > > sage: M22=MatrixSpace(Integers(),2,2) > sage: M22([1,2,3,4]) > [1 2] > [3 4] > sage: M22( i for i in [1,2,3,4] ) > [1 2] > [3 4] > sage: M22("1 2 3 4") > TypeError: entries has the wrong length > sage: M22("1234") > [1 2] > [3 4] > sage: matrix(QQ,2,2, (i for i in [1,2,3,4]) ) > [1 2] > [3 4] > > Especially the last example makes me hesitant about letting matrix > parse strings. Interpreting strings as a 1-digit decimal list seems more broken to me sage: MatrixSpace(SR, 2, 2)("1234") [1234 0] [ 0 1234] sage: MatrixSpace(ZZ, 2, 2)("1234") [1 2] [3 4] I think it's fair to test for strings first, trying to parse, before testing if it's an iterator. This is consistant with many other objects that try to "parse" their string representations. sage: ZZ['x']([1,2,3]) 3*x^2 + 2*x + 1 sage: ZZ['x']("123") 123 sage: ZZ['x']("x^2 + 5") x^2 + 5 - Robert -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org