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

Implement to_vec for PermutedDimsArray #108

Merged
merged 3 commits into from
Sep 23, 2020
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.10.8"
version = "0.10.9"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
9 changes: 9 additions & 0 deletions src/to_vec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ function to_vec(X::Adjoint)
return x_vec, Adjoint_from_vec
end

function to_vec(X::PermutedDimsArray{T, D, P, Pinv} where {T, D}) where {P, Pinv}
x_vec, back = to_vec(collect(X))
function PermutedDimsArray_from_vec(x_vec)
X_parent = collect(PermutedDimsArray(back(x_vec), Pinv))
return PermutedDimsArray(X_parent, P)
end
return x_vec, PermutedDimsArray_from_vec
end

# Non-array data structures

function to_vec(x::Tuple)
Expand Down
10 changes: 10 additions & 0 deletions test/to_vec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ end
test_to_vec(Op(randn(T, 2, 5)))
end

@testset "PermutedDimsArray" begin
test_to_vec(PermutedDimsArray(randn(T, 3, 1), (2, 1)))
test_to_vec(PermutedDimsArray(randn(T, 4, 2, 3), (3, 1, 2)))
test_to_vec(
PermutedDimsArray(
[randn(T, 3) for _ in 1:3, _ in 1:2, _ in 1:4], (2, 1, 3),
),
)
end

@testset "Tuples" begin
test_to_vec((5, 4))
test_to_vec((5, randn(T, 5)))
Expand Down