Skip to content

Adjoint/transpose for tridiagonal matrices preserve structure #1370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,13 @@ end
isreal(S::SymTridiagonal) = isreal(S.dv) && isreal(S.ev)

transpose(S::SymTridiagonal) = S
adjoint(S::SymTridiagonal{<:Number}) = SymTridiagonal(vec(adjoint(S.dv)), vec(adjoint(S.ev)))
adjoint(S::SymTridiagonal{<:Number, <:Base.ReshapedArray{<:Number,1,<:Adjoint}}) =
SymTridiagonal(adjoint(parent(S.dv)), adjoint(parent(S.ev)))
adjoint(S::SymTridiagonal) = SymTridiagonal(_vecadjoint(S.dv), _vecadjoint(S.ev))
Copy link
Member

@stevengj stevengj Jun 2, 2025

Choose a reason for hiding this comment

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

I think this is only correct for SymTridiagonal{<:Number}.

In particular, the off-diagonal elements should be adjoint.(transpose.(S.ev)) or equivalently conj(S.ev). The reason is that S.ev stores the upper diagonal, but when you take the adjoint, the new upper diagonal comes from the adjoint of the lower diagonal, but the lower diagonal of S is transpose.(S.ev).

(Why we allow SymTridiagonal for non-Number elements is beyond me.)


permutedims(S::SymTridiagonal) = S
function permutedims(S::SymTridiagonal, perm)
Base.checkdims_perm(axes(S), axes(S), perm)
NTuple{2}(perm) == (2, 1) ? permutedims(S) : S
end
Base.copy(S::Adjoint{<:Any,<:SymTridiagonal}) = SymTridiagonal(map(x -> copy.(adjoint.(x)), (S.parent.dv, S.parent.ev))...)

ishermitian(S::SymTridiagonal) = isreal(S.dv) && isreal(_evview(S))
issymmetric(S::SymTridiagonal) = true
Expand Down Expand Up @@ -683,23 +680,17 @@ for func in (:conj, :copy, :real, :imag)
end
isreal(T::Tridiagonal) = isreal(T.dl) && isreal(T.d) && isreal(T.du)

adjoint(S::Tridiagonal{<:Number}) = Tridiagonal(vec(adjoint(S.du)), vec(adjoint(S.d)), vec(adjoint(S.dl)))
adjoint(S::Tridiagonal{<:Number, <:Base.ReshapedArray{<:Number,1,<:Adjoint}}) =
Tridiagonal(adjoint(parent(S.du)), adjoint(parent(S.d)), adjoint(parent(S.dl)))
transpose(S::Tridiagonal{<:Number}) = Tridiagonal(S.du, S.d, S.dl)
adjoint(S::Tridiagonal) = Tridiagonal(_vecadjoint(S.du), _vecadjoint(S.d), _vecadjoint(S.dl))
transpose(S::Tridiagonal) = Tridiagonal(_vectranspose(S.du), _vectranspose(S.d), _vectranspose(S.dl))
permutedims(T::Tridiagonal) = Tridiagonal(T.du, T.d, T.dl)
function permutedims(T::Tridiagonal, perm)
Base.checkdims_perm(axes(T), axes(T), perm)
NTuple{2}(perm) == (2, 1) ? permutedims(T) : T
end
Base.copy(aS::Adjoint{<:Any,<:Tridiagonal}) = (S = aS.parent; Tridiagonal(map(x -> copy.(adjoint.(x)), (S.du, S.d, S.dl))...))
Base.copy(tS::Transpose{<:Any,<:Tridiagonal}) = (S = tS.parent; Tridiagonal(map(x -> copy.(transpose.(x)), (S.du, S.d, S.dl))...))

ishermitian(S::Tridiagonal) = all(ishermitian, S.d) && all(Iterators.map((x, y) -> x == y', S.du, S.dl))
issymmetric(S::Tridiagonal) = all(issymmetric, S.d) && all(Iterators.map((x, y) -> x == transpose(y), S.du, S.dl))

\(A::Adjoint{<:Any,<:Tridiagonal}, B::Adjoint{<:Any,<:AbstractVecOrMat}) = copy(A) \ B

function diag(M::Tridiagonal, n::Integer=0)
# every branch call similar(..., ::Int) to make sure the
# same vector type is returned independent of n
Expand Down
22 changes: 22 additions & 0 deletions test/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1207,4 +1207,26 @@ end
@test_throws BoundsError S[LinearAlgebra.BandIndex(0,size(S,1)+1)]
end

@testset "lazy adjtrans" begin
dv = fill([1 2; 3 4], 3)
ev = fill([5 6; 7 8], 2)
T = Tridiagonal(ev, dv, ev)
S = SymTridiagonal(dv, ev)
m = [2 4; 4 2]
for B in (copy(T), copy(S))
for op in (transpose, adjoint)
C = op(B)
el = op(m)
C[1,1] = el
@test B[1,1] == m
if B isa Tridiagonal
C[2,1] = el
@test B[1,2] == m
end
@test (@allocated op(B)) == 0
@test (@allocated op(op(B))) == 0
end
end
end

end # module TestTridiagonal