https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112012-08-10T21:16:36ZRuby Issue Tracking SystemRuby master - Feature #6852: [].transpose should behave speciallyhttps://redmine.ruby-lang.org/issues/6852?journal_id=287602012-08-10T21:16:36Zalexeymuranov (Alexey Muranov)
<ul></ul><p>I think it can be agreed that all of the following represent the same "empty matrix": <code>[]</code>, <code>[[]]</code>, <code>[[], [], []]</code>. Otherwise they all would need to be treated exceptionally (how to express the transposition of each of them?). After all, 0 x 3 = 0 x 2 = 3 x 0 = 0 x 0 = 0.</p> Ruby master - Feature #6852: [].transpose should behave speciallyhttps://redmine.ruby-lang.org/issues/6852?journal_id=287682012-08-11T01:57:31Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Category</strong> set to <i>core</i></li><li><strong>Target version</strong> set to <i>2.0.0</i></li></ul><p>Hi,</p>
<p>alexeymuranov (Alexey Muranov) wrote:</p>
<blockquote>
<p>I think it can be agreed that all of the following represent the same "empty matrix": <code>[]</code>, <code>[[]]</code>, <code>[[], [], []]</code>. Otherwise they all would need to be treated exceptionally (how to express the transposition of each of them?). After all, 0 x 3 = 0 x 2 = 3 x 0 = 0 x 0 = 0.</p>
</blockquote>
<p>Actually, no. Empty matrices are well defined: <a href="http://en.wikipedia.org/wiki/Matrix_(mathematics)#Empty_matrices" class="external">http://en.wikipedia.org/wiki/Matrix_(mathematics)#Empty_matrices</a></p>
<p>The matrix library handles then well too:</p>
<p>require 'matrix'<br>
Matrix[[], [], []] == Matrix[[]] # => false<br>
Matrix[[], [], []].transpose.column_size # => 3</p>
<p>boris_stitnicky (Boris Stitnicky) wrote:</p>
<blockquote>
<p>In other words, when [] is the receiver, ... it should raise,<br>
or warn, or complain</p>
</blockquote>
<p>Strictly speaking you are right.</p>
<p>I'm not sure it's that much more useful to raise, though. I think the current behavior might be more helpful.</p> Ruby master - Feature #6852: [].transpose should behave speciallyhttps://redmine.ruby-lang.org/issues/6852?journal_id=287692012-08-11T02:29:15Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>Hi,</p>
<blockquote>
<p>boris_stitnicky (Boris Stitnicky) wrote:</p>
<blockquote>
<p>In other words, when [] is the receiver, ... it should raise,<br>
or warn, or complain</p>
</blockquote>
<p>Strictly speaking you are right.</p>
</blockquote>
<p>Oups, I was not thinking straight. [] corresponds to a 0x0 matrix (i.e. Matrix[] or Matrix.empty(0,0)). It is its own transpose.</p>
<p>So the current behavior is correct.</p>
<p>What could be said is that <code>[[], [], []].transpose</code> is not completely accurate in returning [], but no other result is possible. For more accurate handling... use the matrix library.</p>
<p>I'll close this if there are no other objections.</p> Ruby master - Feature #6852: [].transpose should behave speciallyhttps://redmine.ruby-lang.org/issues/6852?journal_id=287722012-08-11T06:20:21Zalexeymuranov (Alexey Muranov)
<ul></ul><p>marcandre (Marc-Andre Lafortune) wrote:</p>
<blockquote>
<p>Oups, I was not thinking straight. [] corresponds to a 0x0 matrix (i.e. Matrix[] or Matrix.empty(0,0)). It is its own transpose.</p>
</blockquote>
<p>Then it is not possible to express in the same way a 3 x 0 matrix (or 0 x 3?) I think this was the reason for the original question.<br>
The result of <code>[[], [], []].transpose</code> is then wrong.</p>
<p>I think it is not completely wrong to allow oneself for certain purposes, like for addition and multiplication, to identify matrices of different dimensions which can be obtained from one another by inserting zeros, and also to identify all empty and all zero matrices. The exact dimension is probably needed only for the determinant and the inverse matrix.</p>
<p>It is just a possible explanation for the current behavior.</p> Ruby master - Feature #6852: [].transpose should behave speciallyhttps://redmine.ruby-lang.org/issues/6852?journal_id=288272012-08-13T21:12:39Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>Hi,</p>
<p>alexeymuranov (Alexey Muranov) wrote:</p>
<blockquote>
<p>Then it is not possible to express in the same way a 3 x 0 matrix (or 0 x 3?) I think this was the reason for the original question.</p>
</blockquote>
<p>A 3x0 matrix corresponds to [[], [], []], but there is no correspondence for a 0x3 matrix. So strictly speaking, <code>[[], [], []].transpose</code> has no valid answer, but returning <code>[]</code> is more useful than raising I believe.</p> Ruby master - Feature #6852: [].transpose should behave speciallyhttps://redmine.ruby-lang.org/issues/6852?journal_id=288282012-08-13T21:23:12ZAnonymous
<ul></ul><blockquote>
<p>A 3x0 matrix corresponds to [[], [], []], but there is no correspondence for a 0x3 matrix. So strictly speaking, <code>[[], [], []].transpose</code> has no valid answer, but returning <code>[]</code> is more useful than raising I believe.</p>
</blockquote>
<p>It would be interesting to see code that found returning [] to be<br>
useful. As in, code that expected to operate on the transposed result.</p>
<p>To me, [[], [], []].transpose smells like division by zero, and<br>
because I can't imagine enjoying the result, I'm fine with an<br>
exception.</p>
<p>Ciao,<br>
Sheldon.</p> Ruby master - Feature #6852: [].transpose should behave speciallyhttps://redmine.ruby-lang.org/issues/6852?journal_id=288292012-08-13T22:14:50Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>Anonymous wrote:</p>
<blockquote>
<blockquote>
<p>A 3x0 matrix corresponds to [[], [], []], but there is no correspondence for a 0x3 matrix. So strictly speaking, <code>[[], [], []].transpose</code> has no valid answer, but returning <code>[]</code> is more useful than raising I believe.</p>
</blockquote>
<p>It would be interesting to see code that found returning [] to be<br>
useful. As in, code that expected to operate on the transposed result.</p>
</blockquote>
<p>I can imagine code that wants to iterate on all elements, but going through columns first. E.g:</p>
<pre><code># Instead of
exams.each_with_index do |exam, i|
# do something with exam and grades[i]
end
# Using transpose (or zip) is nicer:
[exams, grades].transpose.each do |exam, grade|
# do the same with exam and grade
end
</code></pre>
<p>The later would fail for an empty set of exams and grades</p>
<p>Note that <code>[[],[],[]].transpose</code> is consistent with <code>[].zip([], [])</code></p> Ruby master - Feature #6852: [].transpose should behave speciallyhttps://redmine.ruby-lang.org/issues/6852?journal_id=288382012-08-14T03:23:21ZAnonymous
<ul></ul><p>On Mon, Aug 13, 2012 at 3:15 PM, marcandre (Marc-Andre Lafortune)<br>
<a href="mailto:ruby-core@marc-andre.ca" class="email">ruby-core@marc-andre.ca</a> wrote:</p>
<blockquote>
<blockquote>
<p>It would be interesting to see code that found returning [] to be<br>
useful. As in, code that expected to operate on the transposed result.</p>
</blockquote>
<p>I can imagine code that wants to iterate on all elements, but going through columns first. E.g:</p>
</blockquote>
<p>[...]</p>
<blockquote>
<pre><code># Using transpose (or zip) is nicer:
[exams, grades].transpose.each do |exam, grade|
# do the same with exam and grade
end
</code></pre>
</blockquote>
<p>So there are at least some situations when [] would be a useful<br>
result. And while there are situations where no result would be<br>
useful, it doesn't much matter in those situations whether you get an<br>
exception or a result that isn't useful.</p>
<p>I'm convinced. :-)</p>
<p>Ciao,<br>
Sheldon.</p> Ruby master - Feature #6852: [].transpose should behave speciallyhttps://redmine.ruby-lang.org/issues/6852?journal_id=288402012-08-14T03:48:08Zalexeymuranov (Alexey Muranov)
<ul></ul><p>marcandre (Marc-Andre Lafortune) wrote:</p>
<blockquote>
<p>The later would fail for an empty set of exams and grades</p>
</blockquote>
<p>I didn't understand this sentence.</p>
<p>Anyway, i just want to give another explanation why it is possible to assume that there is only one empty matrix. With this assumption the function of #transpose will become correct, as [] == [[]] == [[],[],[]] == ... (as matrices).</p>
<p>One of the ways to define a matrix is as an m x n rectangular table of numbers, more precisely a function from {0, ..., m-1} x {0, ..., n-1} to the numbers. If m or n is equal to 0, then the domain is the empty set, and so is the only function on this domain: {} x {} = {} x {0,1,2} = {0,1,2} x {} = {} = ∅, and the only function defined on ∅ is ∅. I used here set theoretic conventions.</p>
<p>Of course matrices can also be viewed as functions from R^m to R^n, then R^0 = {∅} is a one-element set, and "empty matrices" of different dimension will not be the same. But i think this view point is less standard.</p>
<p>Alexey.</p> Ruby master - Feature #6852: [].transpose should behave speciallyhttps://redmine.ruby-lang.org/issues/6852?journal_id=288812012-08-15T13:21:03Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>HI,</p>
<p>alexeymuranov (Alexey Muranov) wrote:</p>
<blockquote>
<p>marcandre (Marc-Andre Lafortune) wrote:</p>
<blockquote>
<p>The later would fail for an empty set of exams and grades</p>
</blockquote>
<p>I didn't understand this sentence.</p>
</blockquote>
<p>The example using <code>each_with_index</code> will work even for empty arrays. The "equivalent" example with transpose currently works with empty arrays but would not if <code>transpose</code> returned <code>nil</code> or raised an error on [[], []]. It would make "equivalent" code behave differently in that case, and I feel that it wouldn't be more useful.</p> Ruby master - Feature #6852: [].transpose should behave speciallyhttps://redmine.ruby-lang.org/issues/6852?journal_id=288852012-08-15T16:09:32Zalexeymuranov (Alexey Muranov)
<ul></ul><p>Ok, thanks, i've understood.</p> Ruby master - Feature #6852: [].transpose should behave speciallyhttps://redmine.ruby-lang.org/issues/6852?journal_id=289012012-08-16T07:57:12ZAnonymous
<ul></ul><p>I see, so as long as the exact behavior is provided by the matrix library, the [].transpose can afford to be pragmatic.</p> Ruby master - Feature #6852: [].transpose should behave speciallyhttps://redmine.ruby-lang.org/issues/6852?journal_id=1040802023-08-04T01:20:42Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/104080/diff?detail_id=65313">diff</a>)</li></ul>