JuliaDiff / FiniteDifferences.jl

High accuracy derivatives, estimated via numerical finite differences (formerly FDM.jl)
MIT License
298 stars 26 forks source link

support empty tuples and named tuples #226

Closed piever closed 1 year ago

piever commented 1 year ago

On the main branch, to_vec(t::Tuple{}) fails as it attempts to call cumsum on an empty vector of type Union{} (obtained by collecting the empty tuple). It is a bit of a corner case, but it can cause test_rrule to fail when the output has an empty tuple or named tuple.

This PR specialcases empty tuples to fix that.

On main:

julia> using FiniteDifferences

julia> v, reconstruct = FiniteDifferences.to_vec(())
ERROR: MethodError: Base.ArithmeticStyle(::Type{Union{}}) is ambiguous.

With this PR:

julia> using FiniteDifferences
[ Info: Precompiling FiniteDifferences [26cc04aa-876d-5657-8c51-4c34ba976000]

julia> v, reconstruct = FiniteDifferences.to_vec(())
(Bool[], FiniteDifferences.var"#Tuple_from_vec#51"())

julia> reconstruct(v)
()