--- sympy/matrices/matrices.py | 12 ++++++++++++ sympy/matrices/tests/test_matrices.py | 13 ++++++++++++- 2 files changed, 24 insertions(+), 1 deletions(-)
diff --git a/sympy/matrices/matrices.py b/sympy/matrices/matrices.py index 14ada69..6d067a3 100644 --- a/sympy/matrices/matrices.py +++ b/sympy/matrices/matrices.py @@ -1076,6 +1076,10 @@ def dot(self, b): prod += self[i] * b[i] return prod + def hadamard(self, b): + """Return the elementwise product of A and B""" + return matrix_hadamard_multiply(self, b) + def norm(self): assert self.rows == 1 or self.cols == 1 out = sympify(0) @@ -1633,6 +1637,14 @@ def matrix_multiply(A, B): alst[i], blst[j]))) +def matrix_hadamard_multiply(A, B): + """Return the elementwise product of A and B""" + if A.shape != B.shape: + raise ShapeError() + shape = A.shape + return Matrix(shape[0], shape[1], + lambda i, j: A[i,j] * B[i, j]) + def matrix_add(A,B): """Return A+B""" if A.shape != B.shape: diff --git a/sympy/matrices/tests/test_matrices.py b/sympy/matrices/tests/test_matrices.py index f9e501e..fe275f3 100644 --- a/sympy/matrices/tests/test_matrices.py +++ b/sympy/matrices/tests/test_matrices.py @@ -1,7 +1,8 @@ from sympy import symbols, Matrix, eye, I, Symbol, Rational, wronskian, cos, \ sin, exp, hessian, sqrt, zeros, ones, randMatrix, Poly, S, pi, \ integrate, oo, raises, trigsimp, Integer, block_diag, N -from sympy.matrices.matrices import ShapeError, MatrixError +from sympy.matrices.matrices import ShapeError, MatrixError, \ + matrix_hadamard_multiply from sympy.printing import srepr from sympy.utilities.pytest import XFAIL @@ -37,6 +38,16 @@ def test_multiplication(): assert c[2,0]==18 assert c[2,1]==0 + h = matrix_hadamard_multiply(a, c) + assert h == a.hadamard(c) + assert h[0,0]==7 + assert h[0,1]==4 + assert h[1,0]==18 + assert h[1,1]==6 + assert h[2,0]==0 + assert h[2,1]==0 + raises(ShapeError, 'matrix_hadamard_multiply(a, b)') + x = Symbol("x") c = b * Symbol("x") -- 1.7.0.5 -- You received this message because you are subscribed to the Google Groups "sympy-patches" group. To post to this group, send email to sympy-patc...@googlegroups.com. To unsubscribe from this group, send email to sympy-patches+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sympy-patches?hl=en.