Skip to content

Commit 7ccf8ed

Browse files
authored
Merge pull request #620 from JuliaDiff/ox/zeromore
iszero for Tangents
2 parents 79ba4ef + cc7b906 commit 7ccf8ed

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/tangent_types/tangent.jl

+4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ function Base.show(io::IO, tangent::Tangent{P}) where {P}
9696
end
9797
end
9898

99+
Base.iszero(::Tangent{<:,NamedTuple{}}) = true
100+
Base.iszero(::Tangent{<:,Tuple{}}) = true
101+
Base.iszero(t::Tangent) = all(iszero, backing(t))
102+
99103
Base.first(tangent::Tangent{P,T}) where {P,T<:Union{Tuple,NamedTuple}} = first(backing(canonicalize(tangent)))
100104
Base.last(tangent::Tangent{P,T}) where {P,T<:Union{Tuple,NamedTuple}} = last(backing(canonicalize(tangent)))
101105

test/tangent_types/tangent.jl

+10
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,16 @@ end
369369
@test_throws MethodError Tangent{Foo}(; y=1.5, x=2.5) * @thunk [1 2; 3 4]
370370
end
371371

372+
@testset "iszero" begin
373+
@test iszero(Tangent{Foo}())
374+
@test iszero(Tangent{Tuple{}}())
375+
@test iszero(Tangent{Foo}(; x=ZeroTangent()))
376+
@test iszero(Tangent{Foo}(; y=0.0))
377+
@test iszero(Tangent{Foo}(; x=Tangent{Tuple{}}(), y=0.0))
378+
379+
@test !iszero(Tangent{Foo}(; y=3.0))
380+
end
381+
372382
@testset "show" begin
373383
@test repr(Tangent{Foo}(; x=1)) == "Tangent{Foo}(x = 1,)"
374384
# check for exact regex match not occurence( `^...$`)

0 commit comments

Comments
 (0)