-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
100 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ jobs: | |
matrix: | ||
version: | ||
- '1' | ||
- '1.6' | ||
- '1.9' | ||
os: | ||
- ubuntu-latest | ||
arch: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module ThickNumbersForwardDiffExt | ||
|
||
using ThickNumbers | ||
using ForwardDiff: ForwardDiff, Dual, Partials, Tag | ||
|
||
function ForwardDiff.derivative(f::F, x::TN) where {F,TN<:ThickNumber} | ||
T = typeof(Tag(f, TN)) | ||
return ForwardDiff.extract_derivative(T, f(Dual{T}(x, one(x)))) | ||
end | ||
|
||
ForwardDiff.can_dual(::Type{<:ThickNumber}) = true | ||
|
||
function ForwardDiff.dual_definition_retval(::Val{T}, val::ThickNumber, deriv::ThickNumber, partial::Partials) where {T} | ||
return Dual{T}(val, deriv * partial) | ||
end | ||
function ForwardDiff.dual_definition_retval(::Val{T}, val::ThickNumber, deriv1::ThickNumber, partial1::Partials, deriv2::ThickNumber, partial2::Partials) where {T} | ||
return Dual{T}(val, ForwardDiff._mul_partials(partial1, partial2, deriv1, deriv2)) | ||
end | ||
|
||
Base.:*(x::ThickNumber, partials::Partials) = partials * x | ||
function Base.:*(partials::Partials, x::ThickNumber) | ||
return Partials(ForwardDiff.scale_tuple(partials.values, x)) | ||
end | ||
|
||
Base.promote_rule(::Type{TN}, ::Type{Dual{T,V,N}}) where {TN<:ThickNumber,T,V<:Number,N} = Dual{T, promote_dual(TN, V),N} | ||
|
||
promote_dual(::Type{TN}, ::Type{V}) where {TN<:ThickNumber,V} = promote_type(TN, V) | ||
promote_dual(::Type{TN}, ::Type{Dual{T,V,N}}) where {TN<:ThickNumber,T,V,N} = Dual{T, promote_dual(TN, V), N} | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using ThickNumbers | ||
using ForwardDiff | ||
using Test | ||
|
||
include(joinpath(dirname(@__DIR__), "setpath.jl")) | ||
|
||
using IntervalArith | ||
|
||
@testset "ForwardDiff extension" begin | ||
@test isempty(detect_ambiguities(ThickNumbers)) | ||
@test isempty(detect_ambiguities(IntervalArith)) | ||
|
||
a, b = Interval(1, 2), Interval(0, 0.1) | ||
f1(t) = a + t*b | ||
f2(x) = a + abs2(x)/2 | ||
|
||
df1(t) = ForwardDiff.derivative(f1, t) | ||
df2(x) = ForwardDiff.derivative(f2, x) | ||
@test df1(0.5) ≐ b | ||
@test df2(b) ⩪ b | ||
ddf2(x) = ForwardDiff.derivative(df2, x) | ||
@test ddf2(b) ≐ 1 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include("forwarddiff.jl") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
if !isdefined(@__MODULE__, :testpackagesdir) | ||
const interfacetestsdir = abspath(joinpath(dirname(@__DIR__), "ThickNumbersInterfaceTests")) | ||
const testpackagesdir = joinpath(interfacetestsdir, "test", "testpackages") | ||
|
||
if testpackagesdir ∉ LOAD_PATH | ||
push!(LOAD_PATH, testpackagesdir) | ||
end | ||
|
||
function cleanup() | ||
filter!(LOAD_PATH) do path | ||
path != testpackagesdir && path != interfacetestsdir | ||
end | ||
return nothing | ||
end | ||
end |