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.
  • Loading branch information
jishnub committed Aug 5, 2024
1 parent f4d1381 commit a163483
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 @@ -181,7 +181,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 @@ -471,7 +471,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 @@ -485,6 +485,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 a163483

Please sign in to comment.