diff --git a/Project.toml b/Project.toml index 5d55036..2410f48 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "FiniteDifferences" uuid = "26cc04aa-876d-5657-8c51-4c34ba976000" -version = "0.12.15" +version = "0.12.16" [deps] ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" diff --git a/src/FiniteDifferences.jl b/src/FiniteDifferences.jl index b943030..0a44f84 100644 --- a/src/FiniteDifferences.jl +++ b/src/FiniteDifferences.jl @@ -9,7 +9,7 @@ using StaticArrays export to_vec, grad, jacobian, jvp, j′vp -include("rand_tangent.jl") +include("deprecated.jl") include("methods.jl") include("numerics.jl") include("to_vec.jl") diff --git a/src/rand_tangent.jl b/src/deprecated.jl similarity index 64% rename from src/rand_tangent.jl rename to src/deprecated.jl index adb0ac8..cf10a91 100644 --- a/src/rand_tangent.jl +++ b/src/deprecated.jl @@ -1,3 +1,10 @@ +function depwarn_rt() + Base.depwarn( + "FiniteDifferences.rand_tangent is deprecated, it has moved to ChainRulesTestUtils", + :rand_tangent + ) +end + """ rand_tangent([rng::AbstractRNG,] x) @@ -7,27 +14,33 @@ Rather it is an arbitary value, that is generated using the `rng`. """ rand_tangent(x) = rand_tangent(Random.GLOBAL_RNG, x) -rand_tangent(rng::AbstractRNG, x::Symbol) = NoTangent() -rand_tangent(rng::AbstractRNG, x::AbstractChar) = NoTangent() -rand_tangent(rng::AbstractRNG, x::AbstractString) = NoTangent() +rand_tangent(rng::AbstractRNG, x::Symbol) = (depwarn_rt(); NoTangent()) +rand_tangent(rng::AbstractRNG, x::AbstractChar) = (depwarn_rt(); NoTangent()) +rand_tangent(rng::AbstractRNG, x::AbstractString) = (depwarn_rt(); NoTangent()) -rand_tangent(rng::AbstractRNG, x::Integer) = NoTangent() +rand_tangent(rng::AbstractRNG, x::Integer) = (depwarn_rt(); NoTangent()) # Try and make nice numbers with short decimal representations for good error messages # while also not biasing the sample space too much function rand_tangent(rng::AbstractRNG, x::T) where {T<:Number} - # multiply by 9 to give a bigger range of values tested: no so tightly clustered around 0. + depwarn_rt() + # multiply by 9 to give a bigger range of values tested: + # not so tightly clustered around 0. return round(9 * randn(rng, T), sigdigits=5, base=2) end -rand_tangent(rng::AbstractRNG, x::Float64) = rand(rng, -9:0.01:9) +rand_tangent(rng::AbstractRNG, x::Float64) = (depwarn_rt(); rand(rng, -9:0.01:9)) function rand_tangent(rng::AbstractRNG, x::ComplexF64) + depwarn_rt() return ComplexF64(rand(rng, -9:0.1:9), rand(rng, -9:0.1:9)) end #BigFloat/MPFR is finicky about short numbers, this doesn't always work as well as it should - -# multiply by 9 to give a bigger range of values tested: no so tightly clustered around 0. -rand_tangent(rng::AbstractRNG, ::BigFloat) = round(big(9 * randn(rng)), sigdigits=5, base=2) +function rand_tangent(rng::AbstractRNG, ::BigFloat) + depwarn_rt() + # multiply by 9 to give a bigger range of values tested: + # not so tightly clustered around 0. + return round(big(9 * randn(rng)), sigdigits=5, base=2) +end rand_tangent(rng::AbstractRNG, x::StridedArray{T, 0}) where {T} = fill(rand_tangent(x[1])) rand_tangent(rng::AbstractRNG, x::StridedArray) = rand_tangent.(Ref(rng), x) @@ -53,11 +66,12 @@ function rand_tangent(rng::AbstractRNG, x::T) where {T} end if all(tangent isa NoTangent for tangent in tangents) # if none of my fields can be perturbed then I can't be perturbed + depwarn_rt() return NoTangent() else Tangent{T}(; NamedTuple{field_names}(tangents)...) end end -rand_tangent(rng::AbstractRNG, ::Type) = NoTangent() -rand_tangent(rng::AbstractRNG, ::Module) = NoTangent() +rand_tangent(rng::AbstractRNG, ::Type) = (depwarn_rt(); NoTangent()) +rand_tangent(rng::AbstractRNG, ::Module) = (depwarn_rt(); NoTangent()) diff --git a/test/rand_tangent.jl b/test/deprecated.jl similarity index 88% rename from test/rand_tangent.jl rename to test/deprecated.jl index 6020286..7ca97e2 100644 --- a/test/rand_tangent.jl +++ b/test/deprecated.jl @@ -1,6 +1,14 @@ -using FiniteDifferences: rand_tangent +# Test struct for `rand_tangent` and `difference`. +struct Foo + a::Float64 + b::Int + c::Any + end -@testset "generate_tangent" begin +# to avoid deprecation spam (and actually test deprecations) we will define a wrapper `rand_tangent` function for testing +rand_tangent(args...) = @test_deprecated FiniteDifferences.rand_tangent(args...) + +@testset "rand_tangent" begin rng = MersenneTwister(123456) @testset "Primal: $(typeof(x)), Tangent: $T_tangent" for (x, T_tangent) in [ @@ -89,7 +97,9 @@ using FiniteDifferences: rand_tangent @testset "erroring cases" begin # Ensure struct fallback errors for non-struct types. - @test_throws ArgumentError invoke(rand_tangent, Tuple{AbstractRNG, Any}, rng, 5.0) + @test_throws ArgumentError invoke( + FiniteDifferences.rand_tangent, Tuple{AbstractRNG, Any}, rng, 5.0 + ) end @testset "compsition of addition" begin diff --git a/test/runtests.jl b/test/runtests.jl index eaec87c..e0b776a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -7,17 +7,11 @@ using Random using StaticArrays using Test -# Test struct for `rand_tangent` and `difference`. -struct Foo - a::Float64 - b::Int - c::Any -end Random.seed!(1) @testset "FiniteDifferences" begin - include("rand_tangent.jl") + include("deprecated.jl") include("methods.jl") include("numerics.jl") include("to_vec.jl")