On Sun, Mar 4, 2012 at 10:34 AM, Nils Bruin <[email protected]> wrote: > On Mar 4, 1:14 am, Robert Bradshaw <[email protected]> > wrote: >> 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 > > Indeed. It's not quit consistent with duck typing, but well ... > > I guess matrix(S) would do: > > if isinstance(S,basestring): > <do our best to parse S as a string rep of a matrix> > else: > try: > V=iter(S) > #figure out how to interpret the elements of V > #(are they rows or elements?) > #should we accept strings at this point? > #the CSV example above indicates that with a known base ring, > #allowing elements of V to be iterables of strings representing > elements > #seems a good idea. > except TypeError: > #do whatever we do with non-iterable matrix initializers
Yep, that's the basic idea, though I might test for the non-iterable matrix initializers first, so something like matrix(QQ['x'], 2, x^2) becomes [x^2, 0] [0, x^2] rather than using list(x^2), and an iterable of strings is always considered an iterator of elements, not an iterator of rows. - Robert -- 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/sage-support URL: http://www.sagemath.org
