11"""
22 rand_tangent([rng::AbstractRNG,] x)
33
4- Returns a randomly generated tangent vector appropriate for the primal value `x`.
4+ Returns a arbitary tangent vector _appropriate_ for the primal value `x`.
5+ Note that despite the name, no promises on the statistical randomness are made.
6+ Rather it is an arbitary value, that is generated using the `rng`.
57"""
68rand_tangent (x) = rand_tangent (Random. GLOBAL_RNG, x)
79
@@ -11,11 +13,21 @@ rand_tangent(rng::AbstractRNG, x::AbstractString) = NoTangent()
1113
1214rand_tangent (rng:: AbstractRNG , x:: Integer ) = NoTangent ()
1315
14- rand_tangent (rng:: AbstractRNG , x:: T ) where {T<: Number } = randn (rng, T)
16+ # Try and make nice numbers with short decimal representations for good error messages
17+ # while also not biasing the sample space too much
18+ 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.
20+ return round (9 * randn (rng, T), sigdigits= 5 , base= 2 )
21+ end
22+ rand_tangent (rng:: AbstractRNG , x:: Float64 ) = rand (rng, - 9 : 0.01 : 9 )
23+ function rand_tangent (rng:: AbstractRNG , x:: ComplexF64 )
24+ return ComplexF64 (rand (rng, - 9 : 0.1 : 9 ), rand (rng, - 9 : 0.1 : 9 ))
25+ end
26+
27+ # BigFloat/MPFR is finicky about short numbers, this doesn't always work as well as it should
1528
16- # TODO : right now Julia don't allow `randn(rng, BigFloat)`
17- # see: https://github.com/JuliaLang/julia/issues/17629
18- rand_tangent (rng:: AbstractRNG , :: BigFloat ) = big (randn (rng))
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 )
1931
2032rand_tangent (rng:: AbstractRNG , x:: StridedArray ) = rand_tangent .(Ref (rng), x)
2133rand_tangent (rng:: AbstractRNG , x:: Adjoint ) = adjoint (rand_tangent (rng, parent (x)))
0 commit comments