Skip to content
4 changes: 3 additions & 1 deletion stdlib/LinearAlgebra/src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,15 @@ ishermitian(D::Diagonal{<:Number}) = isreal(D.diag)
ishermitian(D::Diagonal) = all(ishermitian, D.diag)
issymmetric(D::Diagonal{<:Number}) = true
issymmetric(D::Diagonal) = all(issymmetric, D.diag)
isposdef(D::Diagonal) = all(x -> x > 0, D.diag)
isposdef(D::Diagonal) = all(isposdef, D.diag)

factorize(D::Diagonal) = D

real(D::Diagonal) = Diagonal(real(D.diag))
imag(D::Diagonal) = Diagonal(imag(D.diag))

isdiag(D::Diagonal) = all(isdiag, D.diag)
isdiag(D::Diagonal{<:Number}) = true
istriu(D::Diagonal) = true
istril(D::Diagonal) = true
function triu!(D::Diagonal,k::Integer=0)
Expand Down
2 changes: 2 additions & 0 deletions stdlib/LinearAlgebra/src/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ oneunit(J::UniformScaling{T}) where {T} = oneunit(UniformScaling{T})
zero(::Type{UniformScaling{T}}) where {T} = UniformScaling(zero(T))
zero(J::UniformScaling{T}) where {T} = zero(UniformScaling{T})

isdiag(::UniformScaling) = true
istriu(::UniformScaling) = true
istril(::UniformScaling) = true
issymmetric(::UniformScaling) = true
ishermitian(J::UniformScaling) = isreal(J.λ)
isposdef(J::UniformScaling) = isposdef(J.λ)

(+)(J::UniformScaling, x::Number) = J.λ + x
(+)(x::Number, J::UniformScaling) = x + J.λ
Expand Down
7 changes: 7 additions & 0 deletions stdlib/LinearAlgebra/test/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Random.seed!(1)
@test D[1,2] == 0

@test issymmetric(D)
@test isdiag(D)
@test isdiag(Diagonal([[1 0; 0 1], [1 0; 0 1]]))
@test !isdiag(Diagonal([[1 0; 0 1], [1 0; 1 1]]))
@test istriu(D)
@test istril(D)
if elty <: Real
Expand Down Expand Up @@ -298,6 +301,10 @@ end
@testset "isposdef" begin
@test isposdef(Diagonal(1.0 .+ rand(n)))
@test !isposdef(Diagonal(-1.0 * rand(n)))
@test isposdef(Diagonal(complex(1.0, 0.0) .+ rand(n)))
@test !isposdef(Diagonal(complex(1.0, 1.0) .+ rand(n)))
@test isposdef(Diagonal([[1 0; 0 1], [1 0; 0 1]]))
@test !isposdef(Diagonal([[1 0; 0 1], [1 0; 1 1]]))
end

@testset "getindex" begin
Expand Down
7 changes: 6 additions & 1 deletion stdlib/LinearAlgebra/test/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ end
@test conj(UniformScaling(1.0+1.0im))::UniformScaling{Complex{Float64}} == UniformScaling(1.0-1.0im)
end

@testset "istriu, istril, issymmetric, ishermitian, isapprox" begin
@testset "isdiag, istriu, istril, issymmetric, ishermitian, isposdef, isapprox" begin
@test isdiag(I)
@test istriu(I)
@test istril(I)
@test issymmetric(I)
@test issymmetric(UniformScaling(complex(1.0,1.0)))
@test ishermitian(I)
@test !ishermitian(UniformScaling(complex(1.0,1.0)))
@test isposdef(I)
@test !isposdef(-I)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe test these for complex-valued UniformScalings as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think that's what @mcognetta's suggestion above does, I believe. This is now included.

@test isposdef(UniformScaling(complex(1.0, 0.0)))
@test !isposdef(UniformScaling(complex(1.0, 1.0)))
@test UniformScaling(4.00000000000001) ≈ UniformScaling(4.0)
@test UniformScaling(4.32) ≈ UniformScaling(4.3) rtol=0.1 atol=0.01
@test UniformScaling(4.32) ≈ 4.3 * [1 0; 0 1] rtol=0.1 atol=0.01
Expand Down