Skip to content

Commit 712a177

Browse files
authored
Merge pull request #183 from JuliaDiff/ox/norandtan
deprecate rand_tangent
2 parents 57703c1 + 9882fd1 commit 712a177

5 files changed

Lines changed: 41 additions & 23 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "FiniteDifferences"
22
uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
3-
version = "0.12.15"
3+
version = "0.12.16"
44

55
[deps]
66
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

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: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
using FiniteDifferences: rand_tangent
1+
# Test struct for `rand_tangent` and `difference`.
2+
struct Foo
3+
a::Float64
4+
b::Int
5+
c::Any
6+
end
27

3-
@testset "generate_tangent" begin
8+
# to avoid deprecation spam (and actually test deprecations) we will define a wrapper `rand_tangent` function for testing
9+
rand_tangent(args...) = @test_deprecated FiniteDifferences.rand_tangent(args...)
10+
11+
@testset "rand_tangent" begin
412
rng = MersenneTwister(123456)
513

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

9098
@testset "erroring cases" begin
9199
# Ensure struct fallback errors for non-struct types.
92-
@test_throws ArgumentError invoke(rand_tangent, Tuple{AbstractRNG, Any}, rng, 5.0)
100+
@test_throws ArgumentError invoke(
101+
FiniteDifferences.rand_tangent, Tuple{AbstractRNG, Any}, rng, 5.0
102+
)
93103
end
94104

95105
@testset "compsition of addition" begin

test/runtests.jl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,11 @@ using Random
77
using StaticArrays
88
using Test
99

10-
# Test struct for `rand_tangent` and `difference`.
11-
struct Foo
12-
a::Float64
13-
b::Int
14-
c::Any
15-
end
1610

1711
Random.seed!(1)
1812

1913
@testset "FiniteDifferences" begin
20-
include("rand_tangent.jl")
14+
include("deprecated.jl")
2115
include("methods.jl")
2216
include("numerics.jl")
2317
include("to_vec.jl")

0 commit comments

Comments
 (0)