Skip to content

Commit 9b6e7f3

Browse files
Merge pull request #42 from JuliaDiffEq/issingular
add issingular
2 parents 6e7e7a8 + 023b75a commit 9b6e7f3

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

Project.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "2.5.1"
3+
version = "2.6.0"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
@@ -15,9 +15,10 @@ julia = "1.2"
1515
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
1616
BlockBandedMatrices = "ffab5731-97b5-5995-9138-79e8c1846df0"
1717
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
18+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1819
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1920
SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9"
2021
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2122

2223
[targets]
23-
test = ["Test", "LabelledArrays", "StaticArrays", "BandedMatrices", "BlockBandedMatrices", "SuiteSparse"]
24+
test = ["Test", "LabelledArrays", "StaticArrays", "BandedMatrices", "BlockBandedMatrices", "SuiteSparse", "Random"]

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ A `setindex!` which is always allowed.
6565
Return an instance of the LU factorization object with the correct type
6666
cheaply.
6767

68+
## issingular(A)
69+
70+
Return an instance of the LU factorization object with the correct type cheaply.
71+
6872
## List of things to add
6973

7074
- https://github.com/JuliaLang/julia/issues/22216

src/ArrayInterface.jl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ isstructured(::Diagonal) = true
9595
"""
9696
has_sparsestruct(x::AbstractArray)
9797
98-
determine whether `findstructralnz` accepts the parameter `x`
98+
Determine whether `findstructralnz` accepts the parameter `x`
9999
"""
100100
has_sparsestruct(x) = false
101101
has_sparsestruct(x::AbstractArray) = has_sparsestruct(typeof(x))
@@ -106,6 +106,24 @@ has_sparsestruct(x::Type{<:Bidiagonal}) = true
106106
has_sparsestruct(x::Type{<:Tridiagonal}) = true
107107
has_sparsestruct(x::Type{<:SymTridiagonal}) = true
108108

109+
"""
110+
issingular(A::AbstractMatrix)
111+
112+
Determine whether a given abstract matrix is singular.
113+
"""
114+
issingular(A::AbstractMatrix) = issingular(Matrix(A))
115+
issingular(A::AbstractSparseMatrix) = !issuccess(lu(A, check=false))
116+
issingular(A::Matrix) = !issuccess(lu(A, check=false))
117+
issingular(A::UniformScaling) = A.λ == 0
118+
issingular(A::Diagonal) = any(iszero,A.diag)
119+
issingular(B::Bidiagonal) = any(iszero, A.dv)
120+
issingular(S::SymTridiagonal) = diaganyzero(iszero, ldlt(S).data)
121+
issingular(T::Tridiagonal) = !issuccess(lu(A, check=false))
122+
issingular(A::Union{Hermitian,Symmetric}) = diaganyzero(bunchkaufman(A, check=false).LD)
123+
issingular(A::Union{LowerTriangular,UpperTriangular}) = diaganyzero(A.data)
124+
issingular(A::Union{UnitLowerTriangular,UnitUpperTriangular}) = false
125+
diaganyzero(A) = any(iszero, view(A, diagind(A)))
126+
109127
"""
110128
findstructralnz(x::AbstractArray)
111129

test/runtests.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,21 @@ using SuiteSparse
141141
end
142142
@test lu_instance(1) === 1
143143
end
144+
145+
using Random
146+
using ArrayInterface: issingular
147+
@testset "issingular" begin
148+
for T in [Float64, ComplexF64]
149+
R = randn(MersenneTwister(2), T, 5, 5)
150+
S = Symmetric(R)
151+
L = UpperTriangular(R)
152+
U = LowerTriangular(R)
153+
@test all(!issingular, [R, S, L, U])
154+
R[:, 2] .= 0
155+
@test all(issingular, [R, L, U])
156+
@test !issingular(S)
157+
R[2, :] .= 0
158+
@test issingular(S)
159+
@test all(!issingular, [UnitLowerTriangular(R), UnitUpperTriangular(R)])
160+
end
161+
end

0 commit comments

Comments
 (0)