Skip to content

Commit

Permalink
Create a copy while evaluating eigvals(::Diagonal) (#45048)
Browse files Browse the repository at this point in the history
(cherry picked from commit b9d8280)
  • Loading branch information
jishnub authored and KristofferC committed Dec 21, 2022
1 parent f68c919 commit 6ab6c96
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 @@ -659,7 +659,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 @@ -403,6 +403,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 6ab6c96

Please sign in to comment.