Skip to content

Commit 2effe2e

Browse files
authored
Add a ForwardDiff extension for dimensionless quantities (#765)
1 parent 49aa440 commit 2effe2e

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Project.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1111

1212
[weakdeps]
1313
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
14+
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
1415
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
1516

1617
[extensions]
1718
ConstructionBaseUnitfulExt = "ConstructionBase"
19+
ForwardDiffExt = "ForwardDiff"
1820
InverseFunctionsUnitfulExt = "InverseFunctions"
1921

2022
[compat]
2123
Aqua = "0.8"
2224
ConstructionBase = "1"
2325
Dates = "<0.0.1, 1"
26+
ForwardDiff = "0.10, 1"
2427
InverseFunctions = "0.1"
2528
LinearAlgebra = "<0.0.1, 1"
2629
REPL = "<0.0.1, 1"
@@ -31,11 +34,12 @@ julia = "1"
3134
[extras]
3235
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
3336
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
37+
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
3438
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
3539
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
3640
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
3741
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
3842
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3943

4044
[targets]
41-
test = ["Aqua", "ConstructionBase", "InverseFunctions", "LinearAlgebra", "Test", "Random", "REPL"]
45+
test = ["Aqua", "ConstructionBase", "ForwardDiff", "InverseFunctions", "LinearAlgebra", "Test", "Random", "REPL"]

ext/ForwardDiffExt.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module ForwardDiffExt
2+
using Unitful
3+
using ForwardDiff
4+
5+
function Base.convert(d::Type{ForwardDiff.Dual{T, V, N}}, q::Quantity{T2, NoDims}) where {T, V, N, T2}
6+
return d(uconvert(NoUnits, q))
7+
end
8+
9+
end

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ using Test, LinearAlgebra, Random, ConstructionBase, InverseFunctions
33
import Unitful: DimensionError, AffineError
44
import Unitful: LogScaled, LogInfo, Level, Gain, MixedUnits, Decibel
55
import Unitful: FreeUnits, ContextUnits, FixedUnits, AffineUnits, AffineQuantity
6+
import ForwardDiff
67

78
import Unitful:
89
nm, μm, mm, cm, m, km, inch, ft, mi,
@@ -2206,6 +2207,12 @@ end
22062207
@test isa(TUM.fu^2, TUM.FakeDim212345Units)
22072208
end
22082209

2210+
if isdefined(Base, :get_extension)
2211+
@testset "ForwardDiff extension, solving Issue 682" begin
2212+
@test ForwardDiff.Dual(1.0)*u"cm/m" + ForwardDiff.Dual(1.0) == 1.01
2213+
@test ForwardDiff.Dual(1.0)*u"cm/m" == ForwardDiff.Dual(0.01)
2214+
end
2215+
end
22092216

22102217
struct Num <: Real
22112218
x::Float64

0 commit comments

Comments
 (0)