# Revision history [back]

The following works:

def elementwise( M, N ):
print "M parent is: %s" % M.parent()
print "N parent is: %s" % N.parent()

assert( M.parent() == N.parent() )

nc, nr = M.ncols(), M.nrows()
A = copy( M.parent().zero() )

for r in xrange(nr):
for c in xrange(nc):
A[r,c] = M[r,c] * N[r,c]
return A

ring  = PolynomialRing( QQ, 1, 'x' )
field = ring.fraction_field()

T = Matrix( ring, [
[ 1, -x, -x,  0] ,
[-x,  1,  0, -x] ,
[ 0, -x,  1,  0] ,
[-x,  0,  0,  1] ] )

R = Matrix( field, [
[ 1, 1, 0, 1 ] ,
[ 1, 1, 1, 0 ] ,
[ 1, 0, 0, 1 ] ,
[ 0, 1, 1, 0 ] ] )

B = ~T

elementwise( B, R )


(I only took care that the assertion does not fail. So i replaced the definition ring / field QQ from R, made it the field of fraction of the polynomial ring ring. While computing B, its parent is no longer the ring, as in T, but the field.)