https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112015-05-21T08:12:44ZRuby Issue Tracking SystemRuby master - Feature #10903: [PATCH] Matrix#zip returns a matrixhttps://bugs.ruby-lang.org/issues/10903?journal_id=525562015-05-21T08:12:44Zhsbt (Hiroshi SHIBATA)hsbt@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>marcandre (Marc-Andre Lafortune)</i></li></ul> Ruby master - Feature #10903: [PATCH] Matrix#zip returns a matrixhttps://bugs.ruby-lang.org/issues/10903?journal_id=525822015-05-22T09:45:30Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Tracker</strong> changed from <i>Bug</i> to <i>Feature</i></li></ul><p>Hi, sorry I missed your proposal until now, and thanks for the patch.</p>
<p>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.</p> Ruby master - Feature #10903: [PATCH] Matrix#zip returns a matrixhttps://bugs.ruby-lang.org/issues/10903?journal_id=535222015-07-23T07:38:48ZLitoNico (Lito Nicolai)lito.nicolai@gmail.com
<ul></ul><p>Marc-Andre Lafortune wrote:</p>
<blockquote>
<p>Hi, sorry I missed your proposal until now, and thanks for the patch.</p>
<p>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.</p>
</blockquote>
<p>No worries! Thanks for taking a look.</p>
<p>Matrix#zip is useful for simply implementing any binary (or n-ary) operation on matrices.</p>
<p>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:</p>
<p>Pre-patch:<br>
def & other<br>
Matrix.build(self.row_count){|i, j| self[i, j] & other[i, j] }<br>
end</p>
<p>With patch:<br>
def & other<br>
Matrix.zip(other).map{|a, b| a&b}<br>
end</p>
<p>I like that this allows for a more Matrix-native coding style, much like Numpy matrices<br>
or even APL arrays. Finding the elementwise max/min, the Hadamard product, and<br>
(with a ‘pad-with-zeros’ function) Matrix convolution are all simpler in this notation.</p>
<p>Edit: I'm using this function as a <code>zipWith</code> rather than a <code>zip</code>, but I submitted <code>zip</code> because its use is a bit broader and it kept the spirit of Matrix implementing Enumerable.</p>
<p>Best,<br>
Lito</p> Ruby master - Feature #10903: [PATCH] Matrix#zip returns a matrixhttps://bugs.ruby-lang.org/issues/10903?journal_id=677322017-11-09T22:24:47Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>Wow, time flies. Sorry for not addressing this earlier.</p>
<p>I have some commits ready to address this: <a href="https://github.com/ruby/ruby/pull/1746" class="external">https://github.com/ruby/ruby/pull/1746</a></p>
<p>Naming: The issue I have with <code>zip</code> is not really the potential incompatibility, but that the builtin <code>zip</code> (at least in my head) does a kind of transposition, while it wouldn't for matrices (or it would do two, if you want to see it this way). Also, <code>Array#zip</code> is lax on sizes (you can zip arrays of different sizes). Finally, I would imagine that the result of <code>zip</code> for Matrices will <em>always</em> be chained with <code>map</code>, while <code>Array#zip</code> has other uses (<code>keys.zip(values).to_h</code>).</p>
<p>So I'm proposing <code>combine</code> instead, that takes a block (or returns an Enumerable).</p>
<p>What do you think?</p> Ruby master - Feature #10903: [PATCH] Matrix#zip returns a matrixhttps://bugs.ruby-lang.org/issues/10903?journal_id=682522017-12-10T19:42:11Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Closed</i></li></ul>