Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iszero for Tangents #620

Merged
merged 2 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
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))
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
end

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