Skip to content

Commit 094a78b

Browse files
authored
Merge pull request #158 from JuliaDiff/ox/simrandtan
make rand_tangent of struct with no perturbable fields return DoesNotExist
2 parents 22e1fbd + 786983c commit 094a78b

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/rand_tangent.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ function rand_tangent(rng::AbstractRNG, x::T) where {T}
3737
tangents = map(field_names) do field_name
3838
rand_tangent(rng, getfield(x, field_name))
3939
end
40-
return Composite{T}(; NamedTuple{field_names}(tangents)...)
40+
if all(tangent isa DoesNotExist for tangent in tangents)
41+
# if none of my fields can be perturbed then I can't be perturbed
42+
return DoesNotExist()
43+
else
44+
Composite{T}(; NamedTuple{field_names}(tangents)...)
45+
end
4146
else
4247
return NO_FIELDS
4348
end

test/rand_tangent.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ using FiniteDifferences: rand_tangent
3333
((a=5.0, b=1), Composite{NamedTuple{(:a, :b), Tuple{Float64, Int}}}),
3434

3535
# structs.
36-
(sin, typeof(NO_FIELDS)),
3736
(Foo(5.0, 4, rand(rng, 3)), Composite{Foo}),
3837
(Foo(4.0, 3, Foo(5.0, 2, 4)), Composite{Foo}),
38+
(sin, typeof(NO_FIELDS)),
39+
# all fields DoesNotExist implies DoesNotExist
40+
(Pair(:a, "b"), DoesNotExist),
41+
(1:10, DoesNotExist),
42+
(1:2:10, DoesNotExist),
3943

4044
# LinearAlgebra types (also just structs).
4145
(

0 commit comments

Comments
 (0)