Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: longemen3000/ForwardDiffOverMeasurements.jl
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.1.3
Choose a base ref
...
head repository: longemen3000/ForwardDiffOverMeasurements.jl
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.1.4
Choose a head ref
  • 1 commit
  • 3 files changed
  • 1 contributor

Commits on Sep 24, 2023

  1. 2
    Copy the full SHA
    6787444 View commit details
Showing with 19 additions and 6 deletions.
  1. +3 −3 Project.toml
  2. +14 −1 src/ForwardDiffOverMeasurements.jl
  3. +2 −2 test/runtests.jl
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
name = "ForwardDiffOverMeasurements"
uuid = "9eb8ae02-809e-4b16-afbc-1cadb820c769"
authors = ["longemen3000 <longemen3000@gmail.com> and contributors"]
version = "0.1.3"
version = "0.1.4"

[deps]
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Measurements = "eff96d63-e80a-5855-80a2-b1b0885c5ab7"

[compat]
julia = "1"
ForwardDiff = "0.10"
Measurements = "2 - 2.8"
ForwardDiff = "0.10 - 0.10.36"
Measurements = "2"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
15 changes: 14 additions & 1 deletion src/ForwardDiffOverMeasurements.jl
Original file line number Diff line number Diff line change
@@ -3,9 +3,22 @@ module ForwardDiffOverMeasurements
using ForwardDiff: Dual, DiffRules, NaNMath, LogExpFunctions, SpecialFunctions,
using Measurements: Measurement
import Base: +,-,/,*,promote_rule
using ForwardDiff: AMBIGUOUS_TYPES, partials, values
using ForwardDiff: AMBIGUOUS_TYPES, partials, values, Partials, value
using ForwardDiff: ForwardDiff

#patch this until is fixed in ForwardDiff

#TIL you can actually dispatch on @generated functions
@generated function ForwardDiff.construct_seeds(::Type{Partials{N,V}}) where {N,V<:Measurement}
return Expr(:tuple, [:(single_seed(Partials{N,V}, Val{$i}())) for i in 1:N]...)
end

#needs redefinition here, because generated functions don't allow extra definitions
@generated function single_seed(::Type{Partials{N,V}}, ::Val{i}) where {N,V,i}
ex = Expr(:tuple, [ifelse(i === j, :(oneunit(V)), :(zero(V))) for j in 1:N]...)
return :(Partials($(ex)))
end

function promote_rule(::Type{Measurement{V}}, ::Type{Dual{T, V, N}}) where {T,V,N}
Dual{Measurement{T}, V, N}
end
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -7,11 +7,11 @@ f1(x,y) = x+y
f2(x,y) = x-y
f3(x,y) = x*y
f4(x,y) = x/y

f5(x,y) = muladd(x,y,1)
@testset "ForwardDiffOverMeasurements.jl" begin
x1 = 1.0 ± 0.1
y1 = 2.0 ± 0.001
for op in (:f1,:f2,:f3,:f4)
for op in (:f1,:f2,:f3,:f4,:f5)
@eval begin
@test ForwardDiff.derivative(x->$(op)(x,$y1),$x1) isa Measurement
@test ForwardDiff.derivative(y->$(op)($x1,y),$y1) isa Measurement