@@ -712,13 +712,15 @@ end
712
712
# ##########################################################################################
713
713
714
714
"""
715
- rank(A[, tol::Real])
715
+ rank(A::AbstractMatrix; atol::Real=0, rtol::Real=atol>0 ? 0 : n*ϵ)
716
+ rank(A::AbstractMatrix, rtol::Real) = rank(A; rtol=rtol) # to be deprecated in Julia 2.0
716
717
717
718
Compute the rank of a matrix by counting how many singular
718
- values of `A` have magnitude greater than `tol*σ₁` where `σ₁` is
719
- `A`'s largest singular values. By default, the value of `tol` is the smallest
720
- dimension of `A` multiplied by the [`eps`](@ref)
721
- of the [`eltype`](@ref) of `A`.
719
+ values of `A` have magnitude greater than `max(atol, rtol*σ₁)` where `σ₁` is
720
+ `A`'s largest singular value. `atol` and `rtol` are the absolute and relative
721
+ tolerances, respectively. The default relative tolerance is `n*ϵ`, where `n`
722
+ is the size of the smallest dimension of `A`, and `ϵ` is the [`eps`](@ref) of
723
+ the element type of `A`.
722
724
723
725
# Examples
724
726
```jldoctest
@@ -728,16 +730,21 @@ julia> rank(Matrix(I, 3, 3))
728
730
julia> rank(diagm(0 => [1, 0, 2]))
729
731
2
730
732
731
- julia> rank(diagm(0 => [1, 0.001, 2]), 0.1)
733
+ julia> rank(diagm(0 => [1, 0.001, 2]), rtol= 0.1)
732
734
2
733
735
734
- julia> rank(diagm(0 => [1, 0.001, 2]), 0.00001)
736
+ julia> rank(diagm(0 => [1, 0.001, 2]), rtol= 0.00001)
735
737
3
738
+
739
+ julia> rank(diagm(0 => [1, 0.001, 2]), atol=1.5)
740
+ 1
736
741
```
737
742
"""
738
- function rank (A:: AbstractMatrix , tol:: Real = min (size (A)... )* eps (real (float (one (eltype (A))))))
743
+ function rank (A:: AbstractMatrix ; atol:: Real = 0.0 , rtol:: Real = (min (size (A)... )* eps (real (float (one (eltype (A))))))* iszero (atol))
744
+ isempty (A) && return 0 # 0-dimensional case
739
745
s = svdvals (A)
740
- count (x -> x > tol* s[1 ], s)
746
+ tol = max (atol, rtol* s[1 ])
747
+ count (x -> x > tol, s)
741
748
end
742
749
rank (x:: Number ) = x == 0 ? 0 : 1
743
750
0 commit comments