The solutions I have found so far are (1) a double-for loop and (2)
converting to numpy arrays, divide (which is element-wise by default in
NumPy), then convert back to Matrix. Neither of these solutions are
particularly appealing to me. Is there a better way?
Thanks!
---
Ryan Hinton
iobass, domain email.com
Element divide is very unnatural from an algebraic point of view, which
is why we never even thought to implement it. Here's an example
of a 1 liner that does implement it, along with example usage.
sage: def matrix_eltdiv(A, B):
... return A.parent()([a/b for a, b in zip(A.list(), B.list())])
sage: A = matrix(QQ,3,5,[1..15]); B = matrix(QQ,3,5,[5..19])
sage: print A
sage: print B
[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
sage: matrix_eltdiv(A,B)
[ 1/5 1/3 3/7 1/2 5/9]
[ 3/5 7/11 2/3 9/13 5/7]
[11/15 3/4 13/17 7/9 15/19]
If some other sage developers think it would be a good thing
for sage matrices to have this built in (I think it would be),
they should create a trac ticket for it. It would certainly be
faster if it were built in.
William
It looks like there was extensive discussion in python about
element-wise operators a few years ago. See PEP 225:
http://www.python.org/dev/peps/pep-0225/
For some discussion, see the threads pointed out in the PEP:
http://mail.python.org/pipermail/python-list/2000-July/thread.html#44442
http://mail.python.org/pipermail/python-list/2000-August/thread.html#46674
for example.
Jason