Skip to content

Add a ForwardDiff extension for dimensionless quantities #765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -31,11 +34,12 @@ 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"
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"]
9 changes: 9 additions & 0 deletions ext/ForwardDiffExt.jl
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Loading