public int rank ( ) { if ( rows == 0 || columns == 0 ) { return 0 ; } // TODO: // handle small (1x1, 1xn, nx1, 2x2, 2xn, nx2, 3x3, 3xn, nx3) // matrices without SVD MatrixDecompositor decompositor = withDecompositor ( LinearAlgebra . SVD ) ; Matrix [ ] usv = decompositor . decompose ( ) ; // TODO: Where is my pattern matching? Matrix s = usv [ 1 ] ; double tolerance = Math . max ( rows , columns ) * s . get ( 0 , 0 ) * Matrices . EPS ; int result = 0 ; for ( int i = 0 ; i < s . rows ( ) ; i ++ ) { if ( s . get ( i , i ) > tolerance ) { result ++ ; } } return result ; }
Calculates the rank of this matrix .