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 more non-perturbable types #208

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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.24"
version = "0.12.25"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
3 changes: 2 additions & 1 deletion src/to_vec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ end

# Base case -- if x is already a Vector{<:Real} there's no conversion necessary.
to_vec(x::Vector{<:Real}) = (x, identity)
to_vec(x::Vector{<:Bool}) = invoke(to_vec, Tuple{DenseVector}, x) # not for bool arrays

# get around the constructors and make the type directly
# Note this is moderately evil accessing julia's internals
Expand Down Expand Up @@ -260,7 +261,7 @@ function to_vec(d::Dict)
end

# non-perturbable types
for T in (:DataType, :CartesianIndex, :AbstractZero)
for T in (:DataType, :CartesianIndex, :AbstractZero, :Bool, :Nothing, :AbstractString, :Symbol)
T_from_vec = Symbol(T, :_from_vec)
@eval function FiniteDifferences.to_vec(x::$T)
function $T_from_vec(x_vec::Vector)
Expand Down
38 changes: 29 additions & 9 deletions test/to_vec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,32 @@ end
end
end

@testset "DataType" begin
test_to_vec(Float64; check_inferred=false) # isa DataType
test_to_vec(Vector; check_inferred=false) # isa UnionAll
end
@testset "Nondifferentiable types" begin
@testset "DataType" begin
test_to_vec(Float64; check_inferred=false) # isa DataType
test_to_vec(Vector; check_inferred=false) # isa UnionAll
end

@testset "CartesianIndex" begin
test_to_vec(CartesianIndex(1))
test_to_vec(CartesianIndex(1, 2))
@test to_vec(CartesianIndex(1))[1] == []
@test to_vec(CartesianIndex(1, 3))[1] == []
end

@testset "Bool" begin
test_to_vec(true)
@test to_vec(true)[1] == []

@testset "CartesianIndex" begin
test_to_vec(CartesianIndex(1))
test_to_vec(CartesianIndex(1, 2))
@test to_vec(CartesianIndex(1))[1] == []
@test to_vec(CartesianIndex(1, 3))[1] == []
test_to_vec([true, false])
@test to_vec([true, false])[1] == []
end

@testset "misc Base types" begin
test_to_vec(nothing)
test_to_vec("a")
test_to_vec(:b)
end
end

@testset "ChainRulesCore Differentials" begin
Expand Down Expand Up @@ -260,8 +276,12 @@ end
end

@testset "fallback" begin
test_to_vec(ThreeFields(nothing, 1.5, false), check_inferred=false)
@test to_vec(ThreeFields(nothing, 1.5, false))[1] == [1.5] # drops the two nonpertubable fields

nested = Nested(ThreeFields(1.0, 2.0, "Three"), Singleton())
test_to_vec(nested; check_inferred=false) # map

end

@testset "WrapperArray" begin
Expand Down