This project is closed and read-only.
Bug #1515
closed[PATCH] Teach Matrix#square? About Non-Square Matrices; Fix Crasher
Description
=begin
The .square? method of the Matrix class regards a matrix as square if it contains the same number of rows as it does columns in the first row. Thus, Matrix[ [1,2],[1] ].square? returns true. Not only is this wrong, as admitted in the source code, but it also breaks other methods that rely upon it. For example:
irb(main):003:0> Matrix[ [1,2],[1] ].determinant
NoMethodError: undefined method -' for nil:NilClass from /usr/lib/ruby/1.9.0/matrix.rb:715:in block (2 levels) in determinant'
from /usr/lib/ruby/1.9.0/matrix.rb:713:in upto' from /usr/lib/ruby/1.9.0/matrix.rb:713:in block in determinant'
from /usr/lib/ruby/1.9.0/matrix.rb:710:in upto' from /usr/lib/ruby/1.9.0/matrix.rb:710:in determinant'
from (irb):3
from /usr/bin/irb1.9:12:in `
This occurs on the following versions:
- ruby 1.9.0 (2008-06-20 revision 17482) [i486-linux]
- ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
And it's been confirmed on 1.9.1, too.
There are a host of issues with this class, some of which I'll report separately, but this can be fixed with a trivial change to the .square? method: "@rows.all?{|r| r.size == @rows.size}". With this patch:
Matrix[ [1,2],[1] ].determinant
=> 0
Matrix[ [1,2],[1] ].square?
=> false
This is consistent with the documentation which states that non-square matrices will have a determinant of 0.
=end
Files