1 | initial version |
There is no such mechanism out of the box. For lists, you can use list comprehension:
sage: [M[i] + N[i] for i in xrange(len(M))]
For matrices, you can also make one by yourself as follows:
def elementwise(operator, 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] = operator(M[r,c], N[r,c])
return A
and then use it as follows:
sage: elementwise(operator.add, M, N)
sage: elementwise(operator.pow, M, N)
sage: elementwise(operator.mul, M, N)
sage: elementwise(operator.div, M, N)
and so on.
2 | No.2 Revision |
There is no such mechanism out of the box. For lists, you can use list comprehension:comprehension or the map
function:
sage: [M[i] + N[i] for i in xrange(len(M))]
sage: map(operator.add, N, M)
For matrices, there is no such mechanism out of the box, but you can also make one by yourself as follows:
def elementwise(operator, 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] = operator(M[r,c], N[r,c])
return A
and then use it as follows:
sage: elementwise(operator.add, M, N)
sage: elementwise(operator.pow, M, N)
sage: elementwise(operator.mul, M, N)
sage: elementwise(operator.div, M, N)
and so on.