# Revision history [back]

### Element-wise product of matrices

I am trying to calculate Hadamard product of two matrices.


def elementwise(M, N):
assert(M.parent() == N.parent())
nc, nr = M.ncols(), M.nrows()
A = copy(M.parent().zero_element())
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')
T = Matrix(ring, [
[1,-x,-x,0],
[-x,1,0,-x],
[0,-x,1,0],
[-x,0,0,1]]) R = Matrix(QQ, [
[1,1,0,1],
[1,1,1,0],
[1,0,0,1],
[0,1,1,0]]) B = ~T
elementwise(B,R)

I get a following mistake

Error in lines 21-21
Traceback (most recent call last):
File "/projects/sage/sage-7.5/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 982, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
File "", line 2, in elementwise
AssertionError

Although elementwise(B,R) does not work, the program calculates elementwise(B,B) and elementwise(R,R) without any mistakes. Probably that's because matrices B and R have different types (B is a dense matrix over general ring and R is a rational dense matrix). What should I do?