On Feb 27, 6:26 am, Jason Grout <jason-s...@creativetrax.com> wrote:
> How about the matrix constructor reads from an iterator and recognizes
> csv?  We could even use the numpy savetxt and loadtxt functions to more
> sophisticated parsing.

You can already do:

sage: import csv
sage: M=matrix(RR,list(csv.reader(open("m.csv"))))
sage: csv.writer(open("c.csv","w")).writerows(list(M^2))

I think it would be possible to accept matrix(R,<iterable>) as a
generalization of matrix(R,<list>), which would save 6 characters in
the second line above.

Personally, I think apart from the "list" call, the line
"M=matrix(RR,list(csv.reader(open("m.csv"))))" carries no extraneous
cognitive ballast. All steps are meaningful and leaving any of them
out requires "magic" functionality that tends to break in corner
cases. (csv files can be tricky beasts, so having an extra call that
tells how to interpret the file as a csv makes good sense; requiring
the ring also makes good sense, because it's difficult to see from a
string what ring exactly the elements should lie in). Another
advantage of this incantation is that it's easily discovered by
someone familiar with python and that the logic behind it is entirely
transferable. A novice will have to look up how to do it anyway, so if
we just use this as doc for how to read csv files into a matrix, we're
done.

The writer looks a little less logical to me. I don't have a good
alternative for that. Perhaps your "m.save(...,format='csv')" is a
reasonable alternative.

-- 
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

Reply via email to