Skip to content

Commit da5f1cc

Browse files
dkarraschStefanKarpinski
authored andcommitted
[LinearAlgebra] Add isdiag/isposdef for Diagonal and UniformScaling (#29638)
1 parent ca5fa12 commit da5f1cc

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

stdlib/LinearAlgebra/src/diagonal.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,15 @@ ishermitian(D::Diagonal{<:Number}) = isreal(D.diag)
116116
ishermitian(D::Diagonal) = all(ishermitian, D.diag)
117117
issymmetric(D::Diagonal{<:Number}) = true
118118
issymmetric(D::Diagonal) = all(issymmetric, D.diag)
119-
isposdef(D::Diagonal) = all(x -> x > 0, D.diag)
119+
isposdef(D::Diagonal) = all(isposdef, D.diag)
120120

121121
factorize(D::Diagonal) = D
122122

123123
real(D::Diagonal) = Diagonal(real(D.diag))
124124
imag(D::Diagonal) = Diagonal(imag(D.diag))
125125

126+
isdiag(D::Diagonal) = all(isdiag, D.diag)
127+
isdiag(D::Diagonal{<:Number}) = true
126128
istriu(D::Diagonal) = true
127129
istril(D::Diagonal) = true
128130
function triu!(D::Diagonal,k::Integer=0)

stdlib/LinearAlgebra/src/uniformscaling.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ oneunit(J::UniformScaling{T}) where {T} = oneunit(UniformScaling{T})
7474
zero(::Type{UniformScaling{T}}) where {T} = UniformScaling(zero(T))
7575
zero(J::UniformScaling{T}) where {T} = zero(UniformScaling{T})
7676

77+
isdiag(::UniformScaling) = true
7778
istriu(::UniformScaling) = true
7879
istril(::UniformScaling) = true
7980
issymmetric(::UniformScaling) = true
8081
ishermitian(J::UniformScaling) = isreal(J.λ)
82+
isposdef(J::UniformScaling) = isposdef(J.λ)
8183

8284
(+)(J::UniformScaling, x::Number) = J.λ + x
8385
(+)(x::Number, J::UniformScaling) = x + J.λ

stdlib/LinearAlgebra/test/diagonal.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ Random.seed!(1)
5050
@test D[1,2] == 0
5151

5252
@test issymmetric(D)
53+
@test isdiag(D)
54+
@test isdiag(Diagonal([[1 0; 0 1], [1 0; 0 1]]))
55+
@test !isdiag(Diagonal([[1 0; 0 1], [1 0; 1 1]]))
5356
@test istriu(D)
5457
@test istril(D)
5558
if elty <: Real
@@ -298,6 +301,10 @@ end
298301
@testset "isposdef" begin
299302
@test isposdef(Diagonal(1.0 .+ rand(n)))
300303
@test !isposdef(Diagonal(-1.0 * rand(n)))
304+
@test isposdef(Diagonal(complex(1.0, 0.0) .+ rand(n)))
305+
@test !isposdef(Diagonal(complex(1.0, 1.0) .+ rand(n)))
306+
@test isposdef(Diagonal([[1 0; 0 1], [1 0; 0 1]]))
307+
@test !isposdef(Diagonal([[1 0; 0 1], [1 0; 1 1]]))
301308
end
302309

303310
@testset "getindex" begin

stdlib/LinearAlgebra/test/uniformscaling.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@ end
3030
@test conj(UniformScaling(1.0+1.0im))::UniformScaling{Complex{Float64}} == UniformScaling(1.0-1.0im)
3131
end
3232

33-
@testset "istriu, istril, issymmetric, ishermitian, isapprox" begin
33+
@testset "isdiag, istriu, istril, issymmetric, ishermitian, isposdef, isapprox" begin
34+
@test isdiag(I)
3435
@test istriu(I)
3536
@test istril(I)
3637
@test issymmetric(I)
3738
@test issymmetric(UniformScaling(complex(1.0,1.0)))
3839
@test ishermitian(I)
3940
@test !ishermitian(UniformScaling(complex(1.0,1.0)))
41+
@test isposdef(I)
42+
@test !isposdef(-I)
43+
@test isposdef(UniformScaling(complex(1.0, 0.0)))
44+
@test !isposdef(UniformScaling(complex(1.0, 1.0)))
4045
@test UniformScaling(4.00000000000001) UniformScaling(4.0)
4146
@test UniformScaling(4.32) UniformScaling(4.3) rtol=0.1 atol=0.01
4247
@test UniformScaling(4.32) 4.3 * [1 0; 0 1] rtol=0.1 atol=0.01

0 commit comments

Comments
 (0)