<link rel="self" href="https://redmine.ruby-lang.org/issues/10855.atom"/>
<link rel="alternate" href="https://redmine.ruby-lang.org/"/>
<id>https://redmine.ruby-lang.org/</id>
<icon>https://redmine.ruby-lang.org/favicon.ico?1593313917</icon>
<updated>2015-02-15T20:15:34Z</updated>
<author>
<name>Ruby Issue Tracking System</name>
</author>
<entry>
<title>Ruby master - Bug #10855: [PATCH] Matrix#inverse returns matrix of integers whenever possiblehttps://redmine.ruby-lang.org/issues/10855?journal_id=515062015-02-15T20:15:34Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Assignee</strong> set to <i>marcandre (Marc-Andre Lafortune)</i></li></ul><p>Interesting.</p>
<p>I'm thinking it might be best to do the conversion even if some entries are not integral. Why do you feel it's best to have uniform types accross a matrix, in particular when would having an Integer instead of a Rational be a problem?</p>
Ruby master - Bug #10855: [PATCH] Matrix#inverse returns matrix of integers whenever possiblehttps://redmine.ruby-lang.org/issues/10855?journal_id=515072015-02-15T22:23:21ZLitoNico (Lito Nicolai)lito.nicolai@gmail.com
<ul></ul><p>Marc-Andre Lafortune wrote:</p>
<blockquote>
<p>Interesting.</p>
<p>I'm thinking it might be best to do the conversion even if some entries are not integral. Why do you feel it's best to have uniform types accross a matrix, in particular when would having an Integer instead of a Rational be a problem?</p>
</blockquote>
<p>In the Matrix class, scalar divison is implemented by using the usual <code>/</code><br>
operation, which loses precision on <code>Integer</code>s but not on <code>Rational</code>s. If<br>
the Matrix is a mix of the two, something like this will happen:</p>
<pre>> x = Matrix[[(3/1), 3, 3], [3, (3/1), 3], [3, 3, (3/1)]]
=> # as above
> x / 2
=> Matrix[[(3/2), 1, 1], [1, (3/2), 1], [1, 1, (3/2)]]
</pre>
<p>I would find this mixed precision <em>really</em> surprising when writing matrix code!<br>
Especially because the loss of precision could be hidden across a number<br>
of matrix, vector, and scalar multiplications.</p>
<p>Actually, that's a good argument for returning rationals in ordinary matrix<br>
scalar division (and changing this patch as you suggest), but that's out of <br>
line compared to what the rest of Ruby does with division.</p>
Ruby master - Bug #10855: [PATCH] Matrix#inverse returns matrix of integers whenever possiblehttps://redmine.ruby-lang.org/issues/10855?journal_id=515112015-02-16T10:42:16ZEregon (Benoit Daloze)
<ul></ul><p>Lito Nicolai wrote:</p>
<blockquote>
<p>Marc-Andre Lafortune wrote:</p>
<blockquote>
<p>Interesting.</p>
<p>I'm thinking it might be best to do the conversion even if some entries are not integral. Why do you feel it's best to have uniform types accross a matrix, in particular when would having an Integer instead of a Rational be a problem?</p>
</blockquote>
</blockquote>
<p>It means every operation that follows must go through rational arithmetic which is likely to be slower and more memory hungry, isn't it?<br>
But of course homogeneity also has its value and the code to lower explicitly Rational to Integer is not exactly nice.</p>
<blockquote>
<p>In the Matrix class, scalar divison is implemented by using the usual <code>/</code><br>
operation, which loses precision on <code>Integer</code>s but not on <code>Rational</code>s. If<br>
the Matrix is a mix of the two, something like this will happen:</p>
<pre>> x = Matrix[[(3/1), 3, 3], [3, (3/1), 3], [3, 3, (3/1)]]
=> # as above
> x / 2
=> Matrix[[(3/2), 1, 1], [1, (3/2), 1], [1, 1, (3/2)]]
</pre>
<p>I would find this mixed precision <em>really</em> surprising when writing matrix code!<br>
Especially because the loss of precision could be hidden across a number<br>
of matrix, vector, and scalar multiplications.</p>
</blockquote>
<p>I would think this is a bug. Matrix division by a scalar should be exact, no?</p>
Ruby master - Bug #10855: [PATCH] Matrix#inverse returns matrix of integers whenever possiblehttps://redmine.ruby-lang.org/issues/10855?journal_id=515922015-02-23T00:03:22ZLitoNico (Lito Nicolai)lito.nicolai@gmail.com
<ul></ul><p>Hello! Are there any further thoughts or consensus on which path to take with this?</p>
<p>Here are the options:</p>
<ol>
<li>When dividing matrices, if the resulting matrix has any rational numbers in it, it is entirely rational numbers--
even if they have a divisor of 1.</li>
<li>When dividing matrices, the result can have a mix of numeric types, even though this can result in a mix of
precise (rational) and imprecise (integral) division in the next operation.</li>
<li>Scalar division of a matrix by an integer is patched to return a rational if needed, removing the loss of
precision, but breaking with the rest of integral division in Ruby.</li>
</ol>
<p>I'm happy to write up a patch with any of these changes!</p>
<p>Best,<br>
L</p>
<p>Benoit Daloze wrote:</p>
<blockquote>
<p>Lito Nicolai wrote:</p>
<blockquote>
<p>Marc-Andre Lafortune wrote:</p>
<blockquote>
<p>Interesting.</p>
<p>I'm thinking it might be best to do the conversion even if some entries are not integral. Why do you feel it's best to have uniform types accross a matrix, in particular when would having an Integer instead of a Rational be a problem?</p>
</blockquote>
</blockquote>
<p>It means every operation that follows must go through rational arithmetic which is likely to be slower and more memory hungry, isn't it?<br>
But of course homogeneity also has its value and the code to lower explicitly Rational to Integer is not exactly nice.</p>
<blockquote>
<p>In the Matrix class, scalar divison is implemented by using the usual <code>/</code><br>
operation, which loses precision on <code>Integer</code>s but not on <code>Rational</code>s. If<br>
the Matrix is a mix of the two, something like this will happen:</p>
<pre>> x = Matrix[[(3/1), 3, 3], [3, (3/1), 3], [3, 3, (3/1)]]
=> # as above
> x / 2
=> Matrix[[(3/2), 1, 1], [1, (3/2), 1], [1, 1, (3/2)]]
</pre>
<p>I would find this mixed precision <em>really</em> surprising when writing matrix code!<br>
Especially because the loss of precision could be hidden across a number<br>
of matrix, vector, and scalar multiplications.</p>
</blockquote>
<p>I would think this is a bug. Matrix division by a scalar should be exact, no?</p>
</blockquote>
Ruby master - Bug #10855: [PATCH] Matrix#inverse returns matrix of integers whenever possiblehttps://redmine.ruby-lang.org/issues/10855?journal_id=516202015-02-23T16:38:54Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>TBH, I can't think of any legitimate use of <code>Matrix#/</code> with integer division. Anyone?</p>
<p>I never really thought of that, but it's a bit odd that there is no natural way to write <code>Matrix.I(3) / 2</code>, say. There's no <code>quo</code> method on <code>Matrix</code>, so one has to do <code>Matrix.I(3) / 2r</code>, <code>Matrix.I(3) * 0.5r</code> or <code>Matrix.diagonal([0.5r] * 3)</code>.</p>
<p>I'm very tempted to change <code>/</code> to act like <code>quo</code>.</p>