Skip to content

Commit

Permalink
Create a copy while evaluating eigvals(::Diagonal) (#45048)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed May 2, 2022
1 parent ef1c9ce commit b9d8280
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ function pinv(D::Diagonal{T}, tol::Real) where T
end

#Eigensystem
eigvals(D::Diagonal{<:Number}; permute::Bool=true, scale::Bool=true) = D.diag
eigvals(D::Diagonal{<:Number}; permute::Bool=true, scale::Bool=true) = copy(D.diag)
eigvals(D::Diagonal; permute::Bool=true, scale::Bool=true) =
[eigvals(x) for x in D.diag] #For block matrices, etc.
eigvecs(D::Diagonal) = Matrix{eltype(D)}(I, size(D))
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 @@ -465,6 +465,13 @@ end
@test sort([eigvals(D)...;], by=LinearAlgebra.eigsortby) eigvals([D.diag[1] zeros(3,2); zeros(2,3) D.diag[2]])
end

@testset "eigvals should return a copy of the diagonal" begin
D = Diagonal([1, 2, 3])
lam = eigvals(D)
D[3,3] = 4 # should not affect lam
@test lam == [1, 2, 3]
end

@testset "eigmin (#27847)" begin
for _ in 1:100
d = randn(rand(1:10))
Expand Down

0 comments on commit b9d8280

Please sign in to comment.