Skip to content

Commit

Permalink
Merge pull request #110 from JuliaDiff/ox/views
Browse files Browse the repository at this point in the history
to_vec on SubArrays and ReshapedArrays
  • Loading branch information
oxinabox authored Oct 8, 2020
2 parents 8292355 + e2a6839 commit 0f812fa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
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

2 comments on commit 0f812fa

@oxinabox
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/22619

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.11.1 -m "<description of version>" 0f812fa6a4b28f2ea150e69e268fbfdd0b661228
git push origin v0.11.1

Please sign in to comment.