<link rel="self" href="https://bugs.ruby-lang.org/issues/8223.atom"/>
<link rel="alternate" href="https://bugs.ruby-lang.org/"/>
<id>https://bugs.ruby-lang.org/</id>
<icon>https://bugs.ruby-lang.org/favicon.ico?1610773141</icon>
<updated>2013-04-07T04:30:40Z</updated>
<author>
<name>Ruby Issue Tracking System</name>
</author>
<entry>
<title>Ruby master - Feature #8223: Make Matrix more omnivorous.https://bugs.ruby-lang.org/issues/8223?journal_id=383142013-04-07T04:30:40ZAnonymous
<ul></ul><p>For starters, I propose changing the existing Matrix#* method to the following:</p>
<p>class Matrix<br>
# Matrix multiplication.<br>
#<br>
def * arg # arg is matrix or vector or number<br>
case arg<br>
when Numeric<br>
rows = @rows.map { |row| row.map { |e| e * arg } }<br>
return new_matrix rows, column_size<br>
when Vector<br>
arg = Matrix.column_vector arg<br>
result = self * arg<br>
return result.column 0<br>
when Matrix<br>
Matrix.Raise ErrDimensionMismatch if column_size != arg.row_size<br>
rows = Array.new row_size do |i|<br>
Array.new arg.column_size do |j|<br>
( 0...column_size ).map { |c| arg[c, j] * self[i, c] }.reduce :+<br>
end<br>
end<br>
return new_matrix( rows, arg.column_size )<br>
else<br>
compat_1, compat_2 = arg.coerce self<br>
return compat_1 * compat_2<br>
end<br>
end<br>
end</p>
<p>Provided that I didn't make a mistake, this version of matrix multiplication does not perform<br>
addition of 0 to the sum. I know that there is actually a reason why we should always write<br>
#reduce with starting value, but I now cannot remember what the reason was. And patching with<br>
this method makes the matrix multiplication of metres work...</p>
Ruby master - Feature #8223: Make Matrix more omnivorous.https://bugs.ruby-lang.org/issues/8223?journal_id=383152013-04-07T04:48:40ZAnonymous
<ul></ul><p>Ahh, I remembered now, initial zero has to be provided to make empty collections work.<br>
My suggested patch breaks empty matrix multiplication. I hope Marc-Andre will appear<br>
here soon.</p>
Ruby master - Feature #8223: Make Matrix more omnivorous.https://bugs.ruby-lang.org/issues/8223?journal_id=383162013-04-07T05:33:57ZAnonymous
<ul></ul><p>So with another apology, I will use this space to write down a few more remarks so that I do not forget about them. My line of thinking was as follows: The first step to the systematic solution of this problem would be to generalize zero. It means that the matrix elements would be required to be of a class that has #zero method defined. As for Matrix.zero and Matrix.empty, I think that there should be an option to tell them what this zero is (or tell them the class that has #zero defined). Or perhaps we could play the abstract algebra terminology and call it "additive_identity_element" instead of "zero". That would mean that Matrix would require its elements to comply with at least monoid definition for matrix addition and multiplication, and monoids necessarily need to have the additive identity element defined.</p>
Ruby master - Feature #8223: Make Matrix more omnivorous.https://bugs.ruby-lang.org/issues/8223?journal_id=383412013-04-08T05:53:16Zzzak (Zachary Scott)
<ul></ul><p>Hi Boris, I think it takes too long to read through all of your<br>
messages, so best to define a smaller proposal with exact details for<br>
each feature you want.</p>
<p>On Sat, Apr 6, 2013 at 4:33 PM, boris_stitnicky (Boris Stitnicky)<br>
<a href="mailto:boris@iis.sinica.edu.tw">boris@iis.sinica.edu.tw</a> wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: Make Matrix more omnivorous. (Open)" href="https://bugs.ruby-lang.org/issues/8223">#8223</a> has been updated by boris_stitnicky (Boris Stitnicky).</p>
<a name="So-with-another-apology-I-will-use-this-space-to-write-down-a-few-more-remarks-so-that-I-do-not-forget-about-them-My-line-of-thinking-was-as-follows-The-first-step-to-the-systematic-solution-of-this-problem-would-be-to-generalize-zero-It-means-that-the-matrix-elements-would-be-required-to-be-of-a-class-that-has-zero-method-defined-As-for-Matrixzero-and-Matrixempty-I-think-that-there-should-be-an-option-to-tell-them-what-this-zero-is-or-tell-them-the-class-that-has-zero-defined-Or-perhaps-we-could-play-the-abstract-algebra-terminology-and-call-it-additive_identity_element-instead-of-zero-That-would-mean-that-Matrix-would-require-its-elements-to-comply-with-at-least-monoid-definition-for-matrix-addition-and-multiplication-and-monoids-necessarily-need-to-have-the-additive-identity-element-defined"></a>
<h2 >So with another apology, I will use this space to write down a few more remarks so that I do not forget about them. My line of thinking was as follows: The first step to the systematic solution of this problem would be to generalize zero. It means that the matrix elements would be required to be of a class that has #zero method defined. As for Matrix.zero and Matrix.empty, I think that there should be an option to tell them what this zero is (or tell them the class that has #zero defined). Or perhaps we could play the abstract algebra terminology and call it "additive_identity_element" instead of "zero". That would mean that Matrix would require its elements to comply with at least monoid definition for matrix addition and multiplication, and monoids necessarily need to have the additive identity element defined.<a href="#So-with-another-apology-I-will-use-this-space-to-write-down-a-few-more-remarks-so-that-I-do-not-forget-about-them-My-line-of-thinking-was-as-follows-The-first-step-to-the-systematic-solution-of-this-problem-would-be-to-generalize-zero-It-means-that-the-matrix-elements-would-be-required-to-be-of-a-class-that-has-zero-method-defined-As-for-Matrixzero-and-Matrixempty-I-think-that-there-should-be-an-option-to-tell-them-what-this-zero-is-or-tell-them-the-class-that-has-zero-defined-Or-perhaps-we-could-play-the-abstract-algebra-terminology-and-call-it-additive_identity_element-instead-of-zero-That-would-mean-that-Matrix-would-require-its-elements-to-comply-with-at-least-monoid-definition-for-matrix-addition-and-multiplication-and-monoids-necessarily-need-to-have-the-additive-identity-element-defined" class="wiki-anchor">¶</a></h2>
<p>Feature <a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: Make Matrix more omnivorous. (Open)" href="https://bugs.ruby-lang.org/issues/8223">#8223</a>: Make Matrix more omnivorous.<br>
<a href="https://bugs.ruby-lang.org/issues/8223#change-38316">https://bugs.ruby-lang.org/issues/8223#change-38316</a></p>
<p>Author: boris_stitnicky (Boris Stitnicky)<br>
Status: Open<br>
Priority: Normal<br>
Assignee:<br>
Category:<br>
Target version:</p>
<p>Let's imagine a class Metre, whose instances represent physical magnitudes in metres.</p>
<pre>class Metre
attr_reader :magnitude
def initialize magnitude; @magnitude = magnitude end
def to_s; magnitude.to_s + ".m" end
end
</pre>
<p>Let's say that metres can be multiplied by a number:</p>
<pre>class Metre
def * multiplicand
case multiplicand
when Numeric then Metre.new( magnitude * multiplicand )
else
raise "Metres can only be multiplied by numbers, multiplication by #{multiplicand.class} attempted!"
end
end
end
</pre>
<p>And that they can be summed up with other magnitudes in metres, but, as a feature,<br>
not with numbers (apples, pears, seconds, kelvins...).</p>
<pre>class Metre
def + summand
case summand
when Metre then Metre.new( magnitude + summand.magnitude )
else
raise "Metres can only be summed with metres, summation with #{summand.class} attempted!"
end
end
end
</pre>
<p>Now with one more convenience constructor Numeric#m:</p>
<pre>class Numeric
def m; Metre.new self end
end
</pre>
<p>We can write expressions such as</p>
<pre>3.m + 5.m
#=> 8.m
3.m * 2
#=> 6.m
</pre>
<p>And with defined #coerce:</p>
<pre>class Metre
def coerce other; [ self, other ] end
end
</pre>
<p>Also this expression is valid:</p>
<pre>2 * 3.m
#=> 6.m
</pre>
<p>Before long, the user will want to make a matrix of magnitudes:</p>
<pre>require 'matrix'
mx = Matrix.build 2, 2 do 1.m end
#=> Matrix[[1.m, 1.m], [1.m, 1.m]]
</pre>
<p>It works, but the joy does not last long. The user will fail miserably if ze wants to perform matrix multiplication:</p>
<pre>cv = Matrix.column_vector [1, 1]
mx * cv
#=> RuntimeError: Metres can only be summed with metres, summation with Fixnum attempted!
# where 2.m would be expected
</pre>
<p>In theory, everything should be O.K., since Metre class has both metre summation and multiplication by a number defined. The failure happens due to the internal workings of the Matrix class, which assumes that the elements can be summed together with numeric 0. But it is a feature of metres, that they are picky and allow themselves to be summed only with other Metre instances.</p>
<p>In my real physical units library that I have written, I have solved this problem by<br>
defining an über zero object that produces the expected result, when summed with objects, that would otherwise not lend themselves to summation with ordinary numeric 0,<br>
and patching the Matrix class so that it uses this über zero instead of the ordinary one.</p>
<p>But this is not a very systematic solution. Actually, I think that the Matrix class would be more flexible, if, instead of simply using 0, it asked the elements of the matrix what their zero is, as in:</p>
<pre>class << Metre
def zero; new 0 end
end
</pre>
<p>But of course, that would also require that ordinary numeric classes can tell what their zero is, as in:</p>
<pre>def Integer.zero; 0 end
def Float.zero; 0.0 end
def Complex.zero; Complex 0.0, 0.0 end
# etc.
</pre>
<p>I think that this way of doing things (that is, having #zero methods in numeric classes and making Matrix actually require the class of the objects in it to have public class method #zero defined) would make everything more consistent and more algebra-like. I am having this problem for already almost half a year, but I only gathered courage today to encumber you guys with this proposal. Please don't judge me harshly for it. I have actually already seen something like this, in particular with bigdecimal's Jacobian (<a href="http://ruby-doc.org/stdlib-2.0/libdoc/bigdecimal/rdoc/Jacobian.html">http://ruby-doc.org/stdlib-2.0/libdoc/bigdecimal/rdoc/Jacobian.html</a>), which requires that the object from which the Jacobian is computed implements methods #zero, #one, #two etc. Sorry again.</p>
<p>--<br>
<a href="http://bugs.ruby-lang.org/">http://bugs.ruby-lang.org/</a></p>
</blockquote>
Ruby master - Feature #8223: Make Matrix more omnivorous.https://bugs.ruby-lang.org/issues/8223?journal_id=383432013-04-08T12:21:46Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Category</strong> set to <i>lib</i></li><li><strong>Assignee</strong> set to <i>marcandre (Marc-Andre Lafortune)</i></li><li><strong>Priority</strong> changed from <i>Normal</i> to <i>3</i></li></ul><p>Summary: I could consider injecting nothing instead of 0, but can not consider a generic <code>SomeClass.zero</code>. I don't feel your example is a good justification of a need of not injecting 0.</p>
<p>boris_stitnicky (Boris Stitnicky) wrote:</p>
<blockquote>
<p>Let's imagine a class Metre, whose instances represent physical magnitudes in metres.</p>
<pre>class Metre
attr_reader :magnitude
def initialize magnitude; @magnitude = magnitude end
def to_s; magnitude.to_s + ".m" end
end
</pre>
<p>Let's say that metres can be multiplied by a number:<br>
And that they can be summed up with other magnitudes in metres, but, as a feature,<br>
not with numbers (apples, pears, seconds, kelvins...).</p>
</blockquote>
<p>Shouldn't 0 be specially allowed, i.e 1.m + 0 == 1.m?</p>
<blockquote>
<p>And with defined #coerce:</p>
<pre>class Metre
def coerce other; [ self, other ] end
end
</pre></blockquote>
<p>This is not the way to define <code>coerce</code>. I'm sorry there are not precise specs for coercion, but the idea is to return [transform(other), transform2(self)] such that both elements are compatible. You are reversing the order of things! AFAIK, there is no guarantee this will continue to work as you want it to.</p>
<p>Check how the Matrix class does it (by using an intermediary Scalar class).</p>
<p>But I'm not convinced you have the right approach with the Metre class. Have you tried defining instead a class <code>MeasureWithPhysicalUnit</code> (feel free to shorten the name :-)). You'll need a class "PhysicalUnit" too.</p>
<p>In that case, it would be easy to coerce a numeric value:</p>
<pre>class Metre
def coerce other; [ MeasureWithPhysicalUnit.new(other, ''), self ] end
end
</pre>
<p>This probably will be a bit more complex. This should allow a more sensible way of working, and 2.m * 3.m would == 6.m<sup>2</sup></p>
<blockquote>
<p>It works, but the joy does not last long. The user will fail miserably if ze wants to perform matrix multiplication:</p>
</blockquote>
<p>Right. Just allow addition with 0, which you have zero reason not to implement.</p>
<blockquote>
<p>But this is not a very systematic solution. Actually, I think that the Matrix class would be more flexible, if, instead of simply using 0, it asked the elements of the matrix what their zero</p>
</blockquote>
<p>Definitely not necessary. As you realized in a later email, the 0 is injected in case of empty matrices. If this really was a problem for you, it would be easy to inject the first term in the summation instead (and handle the empty matrix case another way). In no case could a Metre.zero be used, because there is Meter to get in case of zero matrices...</p>
Ruby master - Feature #8223: Make Matrix more omnivorous.https://bugs.ruby-lang.org/issues/8223?journal_id=383582013-04-09T00:23:09ZAnonymous
<ul></ul><p>@Marc-Andre:</p>
<blockquote>
<p>Summary: I could consider injecting nothing instead of 0,<br>
but can not consider a generic <code>SomeClass.zero</code>. I don't feel<br>
your example is a good justification of a need of not injecting 0. <br>
...<br>
Shouldn't 0 be specially allowed, i.e 1.m + 0 == 1.m?</p>
</blockquote>
<p>I tried that in desperation long ago, but it fails: Due to 0 + 1.m, 0 has to<br>
be treated specially also in #coerce. But 1.m.coerce( 0 ) can't return<br>
[0.m, 1.m], or 0 * 1.m would return 0.m² (zero square metres) instead of<br>
expected 0.m. It is theoretically possible to make coerce return an object<br>
that distinguishes between operators, but that's a lot of work. I'd like to<br>
work on that with Ilya, <a href="http://bugs.ruby-lang.org/issues/7604">http://bugs.ruby-lang.org/issues/7604</a></p>
<blockquote>
<p>...<br>
This is not the way to define <code>coerce</code>. I'm sorry there are not precise specs for coercion...</p>
</blockquote>
<p>Obviously, simply reversing order will fail upon any noncommutative operation (eg. 0 / 1.m<br>
will fail instead of returning expected 0.m⁻¹). In the real gem, I have it figured, I'll<br>
put it on Github soon. I just wanted to give a simple example here.</p>
<blockquote>
<p>Check how the Matrix class does it (by using an intermediary Scalar class).</p>
</blockquote>
<p>I read it once, but it takes some time to get into everything. I definitely plan to read it<br>
more, I consider it one of the crucial libraries which I have to be fluent in.</p>
<blockquote>
<p>But I'm not convinced you have the right approach with the Metre class. Have you tried<br>
defining instead a class <code>MeasureWithPhysicalUnit</code> (feel free to shorten the name :-)).<br>
You'll need a class "PhysicalUnit" too.</p>
</blockquote>
<p>Again, this was a toy example. I wrote a big fat physical units gem for my simulator, I'll<br>
put it on Github soon. Dimension, Quantity, Magnitude, Unit, mixin for Numerics, dimensional<br>
analysis, everything works, with the distinction that I had to patch Matrix.</p>
<blockquote>
<p>...<br>
If this really was a problem for you, it would be easy to inject the first term in the<br>
summation instead (and handle the empty matrix case another way).</p>
</blockquote>
<p>I've been sucking it up for a long time. I didn't want to be a bother. but I gradually gained<br>
an impression, that it might be a concern for others. So could you make the solution you<br>
suggest official, pretty please? Not injecting 0 saves one addition per row for non-empty<br>
matrices, am I right?</p>
<blockquote>
<p>In no case could a Metre.zero be used, because there is Meter to get in case of zero matrices...</p>
</blockquote>
<p>One would have to specify of what - zero matrix of what does one want. Imagining:</p>
<pre>a = Matrix.empty 3, 0, over: Float # new syntax proposal, :over option
b = Matrix.empty 0, 1 # while regular syntax still works
a * b
#=> Matrix[[0.0], [0,0], [0,0]]
</pre>
<p>or</p>
<pre>a = Matrix.empty 3, 0, over: Metre
a * b
#=> Matrix[[0.m], [0.m], [0.m]]
</pre>
<p>On one hand, I as the user can implement this myself in a subclass, without needing<br>
to bother you the StdLib maintainer. On the other hand, I wanted to publicly discuss<br>
the fact, that for matrix multiplication, matrix need to be defined over an algebraic<br>
ring, and by definition, rings must have an additive identity element and a<br>
multiplicative identity element defined. Multiplicative identity would come into<br>
use in methods such as</p>
<pre>Matrix.identity( n, options )
if options[:over] then
Matrix.scalar( n, options[:over].multiplicative_identity )
else
Matrix.scalar( n, 1 )
end
end
# and then
Matrix.identity 3, over: Metre
Matrix[[1, 0.m, 0.m], [0.m, 1, 0.m], [0.m, 0.m, 1]]
</pre>
<p>I have an itch to try to write a subclass MatrixOverAlgebraicRing (or AlgebraicField?).<br>
If I get that far, I'll ask you what will you think about it.</p>
Ruby master - Feature #8223: Make Matrix more omnivorous.https://bugs.ruby-lang.org/issues/8223?journal_id=383592013-04-09T00:35:29Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>boris_stitnicky (Boris Stitnicky) wrote:</p>
<blockquote>
<p>@Marc-Andre:</p>
<blockquote>
<p>Summary: I could consider injecting nothing instead of 0,<br>
but can not consider a generic <code>SomeClass.zero</code>. I don't feel<br>
your example is a good justification of a need of not injecting 0. <br>
...<br>
Shouldn't 0 be specially allowed, i.e 1.m + 0 == 1.m?</p>
</blockquote>
<p>I tried that in desperation long ago, but it fails: Due to 0 + 1.m, 0 has to<br>
be treated specially also in #coerce. But 1.m.coerce( 0 ) can't return<br>
[0.m, 1.m], or 0 * 1.m would return 0.m² (zero square metres) instead of<br>
expected 0.m. It is theoretically possible to make coerce return an object<br>
that distinguishes between operators, but that's a lot of work. I'd like to<br>
work on that with Ilya, <a href="http://bugs.ruby-lang.org/issues/7604">http://bugs.ruby-lang.org/issues/7604</a></p>
</blockquote>
<p>If you stick with Metre class, like in this example, then coerce 0/1 to a homemade Scalar class (like Matrix does).<br>
If you go for the more generic MagnitudeWithUnit, then coerce it to MagnitudeWithUnit(0, SCALAR) or something.<br>
In both cases, it should work fine.</p>
<blockquote>
<p>So could you make the solution you suggest official</p>
</blockquote>
<p>I haven't looked at the code yet, nor have I rejected the issue.</p>
<p>Still, if you want to be serious about your library, 0 + 42.some_unit should work, and this is what you should focus on.</p>
Ruby master - Feature #8223: Make Matrix more omnivorous.https://bugs.ruby-lang.org/issues/8223?journal_id=383682013-04-09T03:42:02ZAnonymous
<ul></ul><p>marcandre (Marc-Andre Lafortune) wrote:</p>
<blockquote>
<p>If you stick with Metre class, like in this example, then coerce 0/1 to a homemade Scalar class (like Matrix does).<br>
If you go for the more generic MagnitudeWithUnit, then coerce it to MagnitudeWithUnit(0, SCALAR) or something.<br>
In both cases, it should work fine.</p>
</blockquote>
<p>I'll take a look at that Scalar, thanks.</p>
<blockquote>
<p>Still, if you want to be serious about your library, 0 + 42.some_unit should work, and this is what you should focus on.</p>
</blockquote>
<p>I want to be serious about my library. I want to make it the best of something like 5 other Ruby unit libraries out there.<br>
I must admit that pragmatically, it saves keystrokes in the interactive mode, if the user is allowed to type</p>
<p>7.m.s⁻¹ + 1 #=> 8.m⁻¹</p>
<p>rather than having to tediously type</p>
<p>7.m.s⁻¹ + 1.m.s⁻¹</p>
<p>It is a perilous feature, because the user needs to keep in mind...</p>
<p>7.km.h⁻¹ + 1 #=> 10.6.km.h⁻¹</p>
<p>...what the standard unit of speed is 1.m.s⁻¹, not 1.km.h⁻¹. I still want<br>
to have this feature, but keep it optional, only when the user explicitly<br>
turns it on. The biggest problem with this feature is the necessary coerce<br>
behavior, which will have to return an advanced object with operator-specific<br>
behavior (#+, #-, #<em>, #/, #</em>*, #== and whatnot) defined. I'll focus on this<br>
in the near future.</p>
<p>It would seem that with 0, there would be no such problems, because<br>
<code>something + 0</code> is always <code>something</code>. But unfortunately, this does not<br>
hold well with units that have offsets, such as Celsius degrees. So the<br>
user would still have to keep in mind that 0 in the context of temperatures<br>
means 0.K rather than 0.°C. (Of course, filling matrix with Celsius<br>
temperatures is out of question, because only kelvins support addition,<br>
celsius + kelvins give celsius and celsius + celsius raise QuantityError.)</p>