Skip to content

Commit

Permalink
Fix tr for block SymTridiagonal (#55371)
Browse files Browse the repository at this point in the history
This ensures that `tr` for a block `SymTridiagonal` symmetrizes the
diagonal elements.

(cherry picked from commit a163483)
  • Loading branch information
jishnub authored and KristofferC committed Aug 8, 2024
1 parent cb7a962 commit 8a4dd86
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Base.copy(S::Adjoint{<:Any,<:SymTridiagonal}) = SymTridiagonal(map(x -> copy.(ad
ishermitian(S::SymTridiagonal) = isreal(S.dv) && isreal(_evview(S))
issymmetric(S::SymTridiagonal) = true

tr(S::SymTridiagonal) = sum(S.dv)
tr(S::SymTridiagonal) = sum(symmetric, S.dv)

@noinline function throw_diag_outofboundserror(n, sz)
sz1, sz2 = sz
Expand Down
5 changes: 4 additions & 1 deletion stdlib/LinearAlgebra/test/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ end
end

@testset "SymTridiagonal/Tridiagonal block matrix" begin
M = [1 2; 2 4]
M = [1 2; 3 4]
n = 5
A = SymTridiagonal(fill(M, n), fill(M, n-1))
@test @inferred A[1,1] == Symmetric(M)
Expand All @@ -482,6 +482,9 @@ end
@test_throws ArgumentError diag(A, n+1)
@test_throws ArgumentError diag(A, -n-1)

@test tr(A) == sum(diag(A))
@test issymmetric(tr(A))

A = Tridiagonal(fill(M, n-1), fill(M, n), fill(M, n-1))
@test @inferred A[1,1] == M
@test @inferred A[1,2] == M
Expand Down

0 comments on commit 8a4dd86

Please sign in to comment.