Skip to content

Commit b805b45

Browse files
committed
deprecate rand_tangent
1 parent 57703c1 commit b805b45

4 files changed

Lines changed: 33 additions & 16 deletions

File tree

src/FiniteDifferences.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ using StaticArrays
99

1010
export to_vec, grad, jacobian, jvp, j′vp
1111

12-
include("rand_tangent.jl")
12+
include("deprecated.jl")
1313
include("methods.jl")
1414
include("numerics.jl")
1515
include("to_vec.jl")
Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
function depwarn_rt()
2+
Base.depwarn(
3+
"FiniteDifferences.rand_tangent is deprecated, it has moved to ChainRulesTestUtils",
4+
:rand_tangent
5+
)
6+
end
7+
18
"""
29
rand_tangent([rng::AbstractRNG,] x)
310
@@ -7,27 +14,33 @@ Rather it is an arbitary value, that is generated using the `rng`.
714
"""
815
rand_tangent(x) = rand_tangent(Random.GLOBAL_RNG, x)
916

10-
rand_tangent(rng::AbstractRNG, x::Symbol) = NoTangent()
11-
rand_tangent(rng::AbstractRNG, x::AbstractChar) = NoTangent()
12-
rand_tangent(rng::AbstractRNG, x::AbstractString) = NoTangent()
17+
rand_tangent(rng::AbstractRNG, x::Symbol) = (depwarn_rt(); NoTangent())
18+
rand_tangent(rng::AbstractRNG, x::AbstractChar) = (depwarn_rt(); NoTangent())
19+
rand_tangent(rng::AbstractRNG, x::AbstractString) = (depwarn_rt(); NoTangent())
1320

14-
rand_tangent(rng::AbstractRNG, x::Integer) = NoTangent()
21+
rand_tangent(rng::AbstractRNG, x::Integer) = (depwarn_rt(); NoTangent())
1522

1623
# Try and make nice numbers with short decimal representations for good error messages
1724
# while also not biasing the sample space too much
1825
function rand_tangent(rng::AbstractRNG, x::T) where {T<:Number}
19-
# multiply by 9 to give a bigger range of values tested: no so tightly clustered around 0.
26+
depwarn_rt()
27+
# multiply by 9 to give a bigger range of values tested:
28+
# not so tightly clustered around 0.
2029
return round(9 * randn(rng, T), sigdigits=5, base=2)
2130
end
22-
rand_tangent(rng::AbstractRNG, x::Float64) = rand(rng, -9:0.01:9)
31+
rand_tangent(rng::AbstractRNG, x::Float64) = (depwarn_rt(); rand(rng, -9:0.01:9))
2332
function rand_tangent(rng::AbstractRNG, x::ComplexF64)
33+
depwarn_rt()
2434
return ComplexF64(rand(rng, -9:0.1:9), rand(rng, -9:0.1:9))
2535
end
2636

2737
#BigFloat/MPFR is finicky about short numbers, this doesn't always work as well as it should
28-
29-
# multiply by 9 to give a bigger range of values tested: no so tightly clustered around 0.
30-
rand_tangent(rng::AbstractRNG, ::BigFloat) = round(big(9 * randn(rng)), sigdigits=5, base=2)
38+
function rand_tangent(rng::AbstractRNG, ::BigFloat)
39+
depwarn_rt()
40+
# multiply by 9 to give a bigger range of values tested:
41+
# not so tightly clustered around 0.
42+
return round(big(9 * randn(rng)), sigdigits=5, base=2)
43+
end
3144

3245
rand_tangent(rng::AbstractRNG, x::StridedArray{T, 0}) where {T} = fill(rand_tangent(x[1]))
3346
rand_tangent(rng::AbstractRNG, x::StridedArray) = rand_tangent.(Ref(rng), x)
@@ -53,11 +66,12 @@ function rand_tangent(rng::AbstractRNG, x::T) where {T}
5366
end
5467
if all(tangent isa NoTangent for tangent in tangents)
5568
# if none of my fields can be perturbed then I can't be perturbed
69+
depwarn_rt()
5670
return NoTangent()
5771
else
5872
Tangent{T}(; NamedTuple{field_names}(tangents)...)
5973
end
6074
end
6175

62-
rand_tangent(rng::AbstractRNG, ::Type) = NoTangent()
63-
rand_tangent(rng::AbstractRNG, ::Module) = NoTangent()
76+
rand_tangent(rng::AbstractRNG, ::Type) = (depwarn_rt(); NoTangent())
77+
rand_tangent(rng::AbstractRNG, ::Module) = (depwarn_rt(); NoTangent())
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using FiniteDifferences: rand_tangent
21

3-
@testset "generate_tangent" begin
2+
rand_tangent(args...) = @test_deprecated FiniteDifferences.rand_tangent(args...)
3+
4+
@testset "rand_tangent" begin
45
rng = MersenneTwister(123456)
56

67
@testset "Primal: $(typeof(x)), Tangent: $T_tangent" for (x, T_tangent) in [
@@ -89,7 +90,9 @@ using FiniteDifferences: rand_tangent
8990

9091
@testset "erroring cases" begin
9192
# Ensure struct fallback errors for non-struct types.
92-
@test_throws ArgumentError invoke(rand_tangent, Tuple{AbstractRNG, Any}, rng, 5.0)
93+
@test_throws ArgumentError invoke(
94+
FiniteDifferences.rand_tangent, Tuple{AbstractRNG, Any}, rng, 5.0
95+
)
9396
end
9497

9598
@testset "compsition of addition" begin

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ end
1717
Random.seed!(1)
1818

1919
@testset "FiniteDifferences" begin
20-
include("rand_tangent.jl")
20+
include("deprecated.jl")
2121
include("methods.jl")
2222
include("numerics.jl")
2323
include("to_vec.jl")

0 commit comments

Comments
 (0)