diff --git a/Project.toml b/Project.toml index 11971be4..76b516fd 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "FiniteDifferences" uuid = "26cc04aa-876d-5657-8c51-4c34ba976000" -version = "0.11.0" +version = "0.11.1" [deps] ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" diff --git a/src/to_vec.jl b/src/to_vec.jl index 7af8b74f..4f74822f 100644 --- a/src/to_vec.jl +++ b/src/to_vec.jl @@ -33,7 +33,6 @@ function to_vec(x::AbstractVector) end function to_vec(x::AbstractArray) - x_vec, from_vec = to_vec(vec(x)) function Array_from_vec(x_vec) @@ -43,7 +42,22 @@ function to_vec(x::AbstractArray) return x_vec, Array_from_vec end + # Some specific subtypes of AbstractArray. +function to_vec(x::Base.ReshapedArray{<:Any, 1}) + x_vec, from_vec = to_vec(parent(x)) + function ReshapedArray_from_vec(x_vec) + p = from_vec(x_vec) + return Base.ReshapedArray(p, x.dims, x.mi) + end + + return x_vec, ReshapedArray_from_vec +end + +# To return a SubArray we would endup needing to copy the `parent` of `x` in `from_vec` +# which doesn't seem particularly useful. So we just convert the view into a copy. +# we might be able to do something more performant but this seems good for now. +to_vec(x::Base.SubArray) = to_vec(copy(x)) function to_vec(x::T) where {T<:LinearAlgebra.AbstractTriangular} x_vec, back = to_vec(Matrix(x)) diff --git a/test/to_vec.jl b/test/to_vec.jl index 48ba4b17..b105881f 100644 --- a/test/to_vec.jl +++ b/test/to_vec.jl @@ -50,6 +50,9 @@ end test_to_vec(DummyType(randn(T, 2, 9))) test_to_vec(SVector{2, T}(1.0, 2.0)) test_to_vec(SMatrix{2, 2, T}(1.0, 2.0, 3.0, 4.0)) + test_to_vec(@view randn(T, 10)[1:4]) # SubArray -- Vector + test_to_vec(@view randn(T, 10, 2)[1:4, :]) # SubArray -- Matrix + test_to_vec(Base.ReshapedArray(rand(T, 3, 3), (9,), ())) @testset "$Op" for Op in (Symmetric, Hermitian) test_to_vec(Op(randn(T, 11, 11)))