Issue #10903 has been updated by Lito Nicolai.


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.

Best,
Lito

----------------------------------------
Feature #10903: [PATCH] Matrix#zip returns a matrix
https://bugs.ruby-lang.org/issues/10903#change-53522

* Author: Lito Nicolai
* Status: Assigned
* Priority: Normal
* Assignee: Marc-Andre Lafortune
----------------------------------------
Currently:
    > x = Matrix.I 2
    > x.zip x
    => [[1, 1], [0, 0], [0, 0], [1, 1]] # It's an array!

With the patch:
    > x.zip x
    => Matrix[[1, 1], [0, 0], [0, 0], [1, 1]]

And also:
    > x.zip x, x  # and so on
    => Matrix[[1, 1, 1], [0, 0, 0], [0, 0, 0], [1, 1, 1]]


---Files--------------------------------
matrix_zip.patch (1.42 KB)


-- 
https://bugs.ruby-lang.org/