diff --git a/Project.toml b/Project.toml index 01356b3c..f6b6a238 100644 --- a/Project.toml +++ b/Project.toml @@ -11,16 +11,19 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" [weakdeps] ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9" +ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112" [extensions] ConstructionBaseUnitfulExt = "ConstructionBase" +ForwardDiffExt = "ForwardDiff" InverseFunctionsUnitfulExt = "InverseFunctions" [compat] Aqua = "0.8" ConstructionBase = "1" Dates = "<0.0.1, 1" +ForwardDiff = "0.10, 1" InverseFunctions = "0.1" LinearAlgebra = "<0.0.1, 1" REPL = "<0.0.1, 1" @@ -31,6 +34,7 @@ julia = "1" [extras] Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9" +ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" @@ -38,4 +42,4 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Aqua", "ConstructionBase", "InverseFunctions", "LinearAlgebra", "Test", "Random", "REPL"] +test = ["Aqua", "ConstructionBase", "ForwardDiff", "InverseFunctions", "LinearAlgebra", "Test", "Random", "REPL"] diff --git a/ext/ForwardDiffExt.jl b/ext/ForwardDiffExt.jl new file mode 100644 index 00000000..41990062 --- /dev/null +++ b/ext/ForwardDiffExt.jl @@ -0,0 +1,9 @@ +module ForwardDiffExt +using Unitful +using ForwardDiff + +function Base.convert(d::Type{ForwardDiff.Dual{T, V, N}}, q::Quantity{T2, NoDims}) where {T, V, N, T2} + return d(uconvert(NoUnits, q)) +end + +end diff --git a/test/runtests.jl b/test/runtests.jl index 913bb66a..3def0dfd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,6 +3,7 @@ using Test, LinearAlgebra, Random, ConstructionBase, InverseFunctions import Unitful: DimensionError, AffineError import Unitful: LogScaled, LogInfo, Level, Gain, MixedUnits, Decibel import Unitful: FreeUnits, ContextUnits, FixedUnits, AffineUnits, AffineQuantity +import ForwardDiff import Unitful: nm, μm, mm, cm, m, km, inch, ft, mi, @@ -2224,6 +2225,12 @@ end @test isa(TUM.fu^2, TUM.FakeDim212345Units) end +if isdefined(Base, :get_extension) + @testset "ForwardDiff extension, solving Issue 682" begin + @test ForwardDiff.Dual(1.0)*u"cm/m" + ForwardDiff.Dual(1.0) == 1.01 + @test ForwardDiff.Dual(1.0)*u"cm/m" == ForwardDiff.Dual(0.01) + end +end struct Num <: Real x::Float64