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

[WIP] handle higher order binary ops of mixed types #272

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions src/higher_fwd_rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ end
# TODO: It's a bit embarassing that we need to write these out, but currently the
# compiler is not strong enough to automatically lift the frule. Let's hope we
# can delete these in the near future.
function (∂☆ₙ::∂☆{N})(fb::AbstractZeroBundle{N, typeof(+)}, a::TaylorBundle{N}, b::TaylorBundle{N}) where {N}
function (∂☆ₙ::∂☆{N})(fb::AbstractZeroBundle{N, typeof(+)}, a::TaylorBundle{N, T}, b::TaylorBundle{N, T}) where {N, T}
TaylorBundle{N}(primal(a) + primal(b),
map(+, a.tangent.coeffs, b.tangent.coeffs))
end

function (∂☆ₙ::∂☆{N})(fb::AbstractZeroBundle{N, typeof(+)}, a::TaylorBundle{N}, b::AbstractZeroBundle{N}) where {N}
function (∂☆ₙ::∂☆{N})(fb::AbstractZeroBundle{N, typeof(+)}, a::TaylorBundle{N, T}, b::AbstractZeroBundle{N, T}) where {N, T}
TaylorBundle{N}(primal(a) + primal(b), a.tangent.coeffs)
end

function (∂☆ₙ::∂☆{N})(fb::AbstractZeroBundle{N, typeof(-)}, a::TaylorBundle{N}, b::TaylorBundle{N}) where {N}
function (∂☆ₙ::∂☆{N})(fb::AbstractZeroBundle{N, typeof(-)}, a::TaylorBundle{N, T}, b::TaylorBundle{N, T}) where {N, T}
TaylorBundle{N}(primal(a) - primal(b),
map(-, a.tangent.coeffs, b.tangent.coeffs))
end
Expand Down
21 changes: 21 additions & 0 deletions test/forward.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,27 @@ end
end
end

@testset "binops of mixed number types" begin
# We have had issues with mixed number types before
struct StoreHalfed <: Number
val::Float64
StoreHalfed(x) = new(x/2)
end
Base.:-(x::StoreHalfed, y::Number) = 2*x.val - y
Base.:+(x::StoreHalfed, y::Number) = 2*x.val + y

sub_sh(a) = StoreHalfed(a) - 10*a
add_sh(a) = StoreHalfed(a) + 10*a
let var"'" = Diffractor.PrimeDerivativeFwd
@test add_sh'(100.0) == 11.0
@test add_sh''(100.0) == 0.0

@test sub_sh'(100.0) == -9.0
@test sub_sh''(100.0) == 0.0
end
end



@testset "taylor_compatible" begin
taylor_compatible = Diffractor.taylor_compatible
Expand Down
Loading