Skip to content

Commit

Permalink
Merge pull request #620 from JuliaDiff/ox/zeromore
Browse files Browse the repository at this point in the history
iszero for Tangents
  • Loading branch information
oxinabox authored May 9, 2023
2 parents 79ba4ef + cc7b906 commit 7ccf8ed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/tangent_types/tangent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ function Base.show(io::IO, tangent::Tangent{P}) where {P}
end
end

Base.iszero(::Tangent{<:,NamedTuple{}}) = true
Base.iszero(::Tangent{<:,Tuple{}}) = true
Base.iszero(t::Tangent) = all(iszero, backing(t))

Base.first(tangent::Tangent{P,T}) where {P,T<:Union{Tuple,NamedTuple}} = first(backing(canonicalize(tangent)))
Base.last(tangent::Tangent{P,T}) where {P,T<:Union{Tuple,NamedTuple}} = last(backing(canonicalize(tangent)))

Expand Down
10 changes: 10 additions & 0 deletions test/tangent_types/tangent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,16 @@ end
@test_throws MethodError Tangent{Foo}(; y=1.5, x=2.5) * @thunk [1 2; 3 4]
end

@testset "iszero" begin
@test iszero(Tangent{Foo}())
@test iszero(Tangent{Tuple{}}())
@test iszero(Tangent{Foo}(; x=ZeroTangent()))
@test iszero(Tangent{Foo}(; y=0.0))
@test iszero(Tangent{Foo}(; x=Tangent{Tuple{}}(), y=0.0))

@test !iszero(Tangent{Foo}(; y=3.0))
end

@testset "show" begin
@test repr(Tangent{Foo}(; x=1)) == "Tangent{Foo}(x = 1,)"
# check for exact regex match not occurence( `^...$`)
Expand Down

0 comments on commit 7ccf8ed

Please sign in to comment.