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

Support Tridiagonal in to_vec #237

Merged
merged 1 commit into from
May 31, 2024
Merged
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FiniteDifferences"
uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
version = "0.12.31"
version = "0.12.32"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
16 changes: 14 additions & 2 deletions src/to_vec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,26 @@ function to_vec(x::T) where {T<:LinearAlgebra.HermOrSym}
return x_vec, HermOrSym_from_vec
end

function to_vec(X::Diagonal)
x_vec, back = to_vec(Matrix(X))
function to_vec(x::Diagonal)
x_vec, back = to_vec(Matrix(x))
function Diagonal_from_vec(x_vec)
return Diagonal(back(x_vec))
end
return x_vec, Diagonal_from_vec
end

function to_vec(x::Tridiagonal)
x_vec, back = to_vec((x.dl, x.d, x.du))
# Other field (du2) of a Tridiagonal is not part of its value and is really a kind of cache
function Tridiagonal_from_vec(x_vec)
return Tridiagonal(back(x_vec)...)
end
return x_vec, Tridiagonal_from_vec
end




function to_vec(X::Transpose)
x_vec, back = to_vec(Matrix(X))
function Transpose_from_vec(x_vec)
Expand Down
2 changes: 2 additions & 0 deletions test/to_vec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ end
test_to_vec(reshape([1.0, randn(T, 5, 4, 3), randn(T, 4, 3), 2.0], 2, 2); check_inferred=false)
test_to_vec(UpperTriangular(randn(T, 13, 13)))
test_to_vec(Diagonal(randn(T, 7)))
test_to_vec(Tridiagonal(randn(T, 3), randn(T, 4), randn(T, 3)))

test_to_vec(DummyType(randn(T, 2, 9)))
test_to_vec(SVector{2, T}(1.0, 2.0); check_inferred=false)
test_to_vec(SMatrix{2, 2, T}(1.0, 2.0, 3.0, 4.0); check_inferred=false)
Expand Down
Loading