Forum: Ruby Bugs in Matrix#rank

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]
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.