Skip to content

Commit

Permalink
allow package to work with latest Measurements.jl release, fix typos
Browse files Browse the repository at this point in the history
  • Loading branch information
longemen3000 committed Sep 24, 2023
1 parent 6cdc9e6 commit 6787444
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
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 <[email protected]> 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"
Expand Down
15 changes: 14 additions & 1 deletion src/ForwardDiffOverMeasurements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

2 comments on commit 6787444

@longemen3000
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/92078

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.4 -m "<description of version>" 6787444c3b87ff0ec687735817dcbbeb8e872a7d
git push origin v0.1.4

Please sign in to comment.