Marc-Andre Lafortune wrote:
Hi, sorry I missed your proposal until now, and thanks for the patch.
Could you explain in what kind of circumstances one would use this? I'm not sure I see what kind of mathematical operation this can correspond to.
No worries! Thanks for taking a look.
Matrix#zip is useful for simply implementing any binary (or n-ary) operation on matrices.
For instance, when I wrote this patch, I was working with matrices of binary numbers— if I wanted to elementwise AND the matrices, here are two ways to do it:
Pre-patch:
def & other
Matrix.build(self.row_count){|i, j| self[i, j] & other[i, j] }
end
With patch:
def & other
Matrix.zip(other).map{|a, b| a&b}
end
I like that this allows for a more Matrix-native coding style, much like Numpy matrices
or even APL arrays. Finding the elementwise max/min, the Hadamard product, and
(with a ‘pad-with-zeros’ function) Matrix convolution are all simpler in this notation.
Edit: I'm using this function as a zipWith
rather than a zip
, but I submitted zip
because its use is a bit broader and it kept the spirit of Matrix implementing Enumerable.
Best,
Lito