Skip to content
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.11.0"
version = "0.11.1"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
16 changes: 15 additions & 1 deletion src/to_vec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand Down
3 changes: 3 additions & 0 deletions test/to_vec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down