Skip to content

Commit

Permalink
Add structural zero tangent code for higher order
Browse files Browse the repository at this point in the history
  • Loading branch information
oxinabox committed Jan 17, 2024
1 parent c93608e commit 4f5ebed
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/tangent_types/abstract_zero.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,15 @@ zero_tangent(x::Number) = zero(x)

zero_tangent(::Type) = NoTangent()

zero_tangent(x::Tangent) = ZeroTangent()
# TODO: zero_tangent(x::MutableTangent)
function zero_tangent(x::MutableTangent{P}) where P
zb = backing(zero_tangent(backing(x)))
return MutableTangent{P}(zb)
end

function zero_tangent(x::Tangent{P}) where P
zb = backing(zero_tangent(backing(x)))
return Tangent{P, typeof(zb)}(zb)
end

@generated function zero_tangent(primal)
fieldcount(primal) == 0 && return NoTangent() # no tangent space at all, no need for structural zero.
Expand Down
8 changes: 8 additions & 0 deletions test/tangent_types/abstract_zero.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ end
@test zero_tangent([[1.0, 2.0], [3.0]]) == [[0.0, 0.0], [0.0]]

@test zero_tangent((1.0, 2.0)) == Tangent{Tuple{Float64,Float64}}(0.0, 0.0)

# Higher order
# StructuralTangents are valid tangents for themselves (just like Numbers)
# and indeed we prefer that, otherwise higher order structural tangents are kinda
# nightmarishly complex types.
@test zero_tangent(zero_tangent(Demo(1.5))) == zero_tangent(Demo(1.5))
@test zero_tangent(zero_tangent((1.5, 2.5))) == Tangent{Tuple{Float64, Float64}}(0.0, 0.0)
@test zero_tangent(zero_tangent(MutDemo(1.5))) == zero_tangent(MutDemo(1.5))
end

@testset "Weird types" begin
Expand Down

0 comments on commit 4f5ebed

Please sign in to comment.