Skip to content

Commit

Permalink
Add promotion rules for ZeroTangent and NoTangent
Browse files Browse the repository at this point in the history
  • Loading branch information
BioTurboNick committed Sep 17, 2024
1 parent 9627bd6 commit 5369090
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/tangent_types/abstract_zero.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Base.:/(z::AbstractZero, ::Any) = z

Base.convert(::Type{T}, x::AbstractZero) where {T<:Number} = zero(T)
# (::Type{T})(::AbstractZero, ::AbstractZero...) where {T<:Number} = zero(T)
Base.promote_rule(T::Type{<:Number}, S::Type{<:AbstractZero}) = T

(::Type{Complex})(x::AbstractZero, y::Real) = Complex(false, y)
(::Type{Complex})(x::Real, y::AbstractZero) = Complex(x, false)
Expand Down
18 changes: 18 additions & 0 deletions test/tangent_types/abstract_zero.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@
@test convert(Float32, ZeroTangent()) === 0.0f0
@test convert(ComplexF64, ZeroTangent()) === 0.0 + 0.0im

@test promote_type(ZeroTangent, Bool) == Bool
@test promote_type(Bool, ZeroTangent) == Bool
@test promote_type(ZeroTangent, Int64) == Int64
@test promote_type(Int64, ZeroTangent) == Int64
@test promote_type(ZeroTangent, Float32) == Float32
@test promote_type(Float32, ZeroTangent) == Float32
@test promote_type(ZeroTangent, ComplexF64) == ComplexF64
@test promote_type(ComplexF64, ZeroTangent) == ComplexF64

@test z[1] === z
@test z[1:3] === z
@test z[1, 2] === z
Expand Down Expand Up @@ -110,6 +119,15 @@
@test dot(dne, 17.2) == dne
@test dot(11.9, dne) == dne

@test promote_type(NoTangent, Bool) == Bool
@test promote_type(Bool, NoTangent) == Bool
@test promote_type(NoTangent, Int64) == Int64
@test promote_type(Int64, NoTangent) == Int64
@test promote_type(NoTangent, Float32) == Float32
@test promote_type(Float32, NoTangent) == Float32
@test promote_type(NoTangent, ComplexF64) == ComplexF64
@test promote_type(ComplexF64, NoTangent) == ComplexF64

@test ZeroTangent() + dne == dne
@test dne + ZeroTangent() == dne
@test ZeroTangent() - dne == dne
Expand Down

0 comments on commit 5369090

Please sign in to comment.