From c1a0a09d2b7fb920640857d4ed4b48761b94fb86 Mon Sep 17 00:00:00 2001 From: marcandre Date: Mon, 20 Nov 2017 02:18:43 +0000 Subject: lib/matrix: Add hadamard_product/entrywise_product. Based on a patch by Charley Hutchison. [GH-674] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/matrix.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'lib') diff --git a/lib/matrix.rb b/lib/matrix.rb index 2f2d0f371a..d242665ef9 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -979,6 +979,17 @@ class Matrix end end + # + # Hadamard product + # Matrix[[1,2], [3,4]].hadamard_product(Matrix[[1,2], [3,2]]) + # => 1 4 + # 9 8 + # + def hadamard_product(m) + combine(m){|a, b| a * b} + end + alias_method :entrywise_product, :hadamard_product + # # Returns the inverse of the matrix. # Matrix[[-1, -1], [0, -1]].inverse -- cgit v1.2.3