Forum: Ruby Bugs in Matrix#rank

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
38d738b4c7b89ab44ad6c0f7d56fb2e4?d=identicon&s=25 Sander Land (Guest)
on 2007-02-02 22:06
(Received via mailing list)
I think I found some bugs in Matrix#rank

>ruby -rmatrix -e'p Matrix[[7,0], [1,0], [2,0]].rank'
.../matrix.rb:761:in `/': nil can't be coerced into Fixnum  (TypeError)

>ruby -rmatrix -e'p Matrix[[7,0], [1,0], [2,1]].rank'
.../matrix.rb:761:in `/': nil can't be coerced into Fixnum  (TypeError)

Fix (?):
change
        exists = true
        begin
          if (i += 1) > a_column_size - 1      # line 731
            exists = false
            break
          end
        end while a[i][k] == 0

to
        exists = true
        begin
          if (i += 1) > a_row_size - 1
            exists = false
            break
          end
        end while a[i][k] == 0

and

          exists = true
          begin
            if (i += 1) > a_row_size - 1  # line 743
              exists = false
              break
            end
          end while a[k][i] == 0

to

          exists = true
          begin
            if (i += 1) > a_column_size - 1
              exists = false
              break
            end
          end while a[k][i] == 0



The error seems to be caused by using loop indices up to row/column
size for column/row indices, which causes some swaps with nil values.

version:
>ruby -v
ruby 1.8.5 (2006-08-25) [i386-mingw32]
This topic is locked and can not be replied to.