# Revision history [back]

The assertion error comes from the line

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


because:

sage: B.parent()
Full MatrixSpace of 4 by 4 dense matrices over Fraction Field of Multivariate Polynomial Ring in x over Rational Field
sage: R.parent()
Full MatrixSpace of 4 by 4 dense matrices over Rational Field


Just remove that line and you will get:

sage: elementwise(B,R)    # with the assertion removed
[        (-1)/(x^4 + 2*x^3 + x^2 - 1)   (-x^2 - x)/(x^4 + 2*x^3 + x^2 - 1)                                    0 (-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)]
[  (-x^2 - x)/(x^4 + 2*x^3 + x^2 - 1)         (-1)/(x^4 + 2*x^3 + x^2 - 1) (-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)                                    0]
[(-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)                                    0                                    0       (-x^2)/(x^4 + 2*x^3 + x^2 - 1)]
[                                   0 (-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)       (-x^2)/(x^4 + 2*x^3 + x^2 - 1)                                    0]


Alternatively, define both matrices on the same ring:

sage: elementwise(B,R.change_ring(B.base_ring()))   # without removing the assertion
[        (-1)/(x^4 + 2*x^3 + x^2 - 1)   (-x^2 - x)/(x^4 + 2*x^3 + x^2 - 1)                                    0 (-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)]
[  (-x^2 - x)/(x^4 + 2*x^3 + x^2 - 1)         (-1)/(x^4 + 2*x^3 + x^2 - 1) (-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)                                    0]
[(-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)                                    0                                    0       (-x^2)/(x^4 + 2*x^3 + x^2 - 1)]
[                                   0 (-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)       (-x^2)/(x^4 + 2*x^3 + x^2 - 1)                                    0]


The assertion error comes from the line

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


because:

sage: B.parent()
Full MatrixSpace of 4 by 4 dense matrices over Fraction Field of Multivariate Polynomial Ring in x over Rational Field
sage: R.parent()
Full MatrixSpace of 4 by 4 dense matrices over Rational Field


Just remove that line and you will get:

sage: elementwise(B,R)    # with the assertion removed
[        (-1)/(x^4 + 2*x^3 + x^2 - 1)   (-x^2 - x)/(x^4 + 2*x^3 + x^2 - 1)                                    0 (-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)]
[  (-x^2 - x)/(x^4 + 2*x^3 + x^2 - 1)         (-1)/(x^4 + 2*x^3 + x^2 - 1) (-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)                                    0]
[(-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)                                    0                                    0       (-x^2)/(x^4 + 2*x^3 + x^2 - 1)]
[                                   0 (-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)       (-x^2)/(x^4 + 2*x^3 + x^2 - 1)                                    0]


Alternatively, if you want to keep your function intact, you can define both matrices on the same ring:

sage: elementwise(B,R.change_ring(B.base_ring()))   # without removing the assertion
[        (-1)/(x^4 + 2*x^3 + x^2 - 1)   (-x^2 - x)/(x^4 + 2*x^3 + x^2 - 1)                                    0 (-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)]
[  (-x^2 - x)/(x^4 + 2*x^3 + x^2 - 1)         (-1)/(x^4 + 2*x^3 + x^2 - 1) (-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)                                    0]
[(-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)                                    0                                    0       (-x^2)/(x^4 + 2*x^3 + x^2 - 1)]
[                                   0 (-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)       (-x^2)/(x^4 + 2*x^3 + x^2 - 1)                                    0]


Alternatively, the elementwise_product method doesthe job, see R.elementwise_product? for the documentation:

sage: R.elementwise_product(B)
[        (-1)/(x^4 + 2*x^3 + x^2 - 1)   (-x^2 - x)/(x^4 + 2*x^3 + x^2 - 1)                                    0 (-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)]
[  (-x^2 - x)/(x^4 + 2*x^3 + x^2 - 1)         (-1)/(x^4 + 2*x^3 + x^2 - 1) (-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)                                    0]
[(-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)                                    0                                    0       (-x^2)/(x^4 + 2*x^3 + x^2 - 1)]
[                                   0 (-x^3 - x^2)/(x^4 + 2*x^3 + x^2 - 1)       (-x^2)/(x^4 + 2*x^3 + x^2 - 1)                                    0]