Try using the built-in sparse matrix rank code instead of writing my own.