Skip to content
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

Add Tridiagonal construction rule #758

Merged
merged 10 commits into from
Jun 10, 2024
17 changes: 17 additions & 0 deletions src/rulesets/LinearAlgebra/structured.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,20 @@ function rrule(::typeof(logdet), X::Union{Diagonal, AbstractTriangular})
end
return y, logdet_pullback
end

#####
##### Tridiagonal
#####

function rrule(::Type{Tridiagonal}, dl, d, du)
y = Tridiagonal(dl, d, du)
@views function ∇Tridiagonal(∂y)
return (
NoTangent(),
diag(∂y[2:end, 1:(end - 1)]),
diag(∂y),
diag(∂y[1:(end - 1), 2:end]),
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
ChrisRackauckas marked this conversation as resolved.
Show resolved Hide resolved
)
end
return y, ∇Tridiagonal
end
5 changes: 5 additions & 0 deletions test/rulesets/LinearAlgebra/structured.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,9 @@
end
end
end

@testset "Tridiagonal" begin
res, pb = rrule(Tridiagonal, [1, 4], [2, 3, 4], [5, 3])
ChrisRackauckas marked this conversation as resolved.
Show resolved Hide resolved
@test pb(10*res) == (NoTangent(), [10, 40], [20, 30, 40], [50, 30])
ChrisRackauckas marked this conversation as resolved.
Show resolved Hide resolved
end
end
Loading