From 42b1554493f04a7e3d2606f328e2a0e365bf8f69 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 13 Mar 2024 16:26:02 -0700 Subject: [PATCH] Broaden promote_rule for Dual to include AbstractIrrational (#687) * Broaden promote_rule for Dual to include AbstractIrrational * add IrrationalConstants test dependency * Add tests for AbstractIrrational promote_rule * fix pkg and test bugs --- Project.toml | 6 ++++-- src/dual.jl | 2 +- test/MiscTest.jl | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 0527e2da..cb88b6cb 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "ForwardDiff" uuid = "f6369f11-7733-5829-9624-2563aa707210" -version = "0.11-DEV" +version = "0.11.0-DEV" [deps] CommonSubexpressions = "bbf7d656-a473-5ed7-a52c-81e309532950" @@ -27,6 +27,7 @@ CommonSubexpressions = "0.3" DiffResults = "1.1" DiffRules = "1.4" DiffTests = "0.1" +IrrationalConstants = "0.1, 0.2" LogExpFunctions = "0.3" NaNMath = "1" Preferences = "1" @@ -38,9 +39,10 @@ julia = "1.6" Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9" DiffTests = "de460e47-3fe3-5279-bb4a-814414816d5d" InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" +IrrationalConstants = "92d709cd-6900-40b7-9082-c6be49f344b6" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Calculus", "DiffTests", "SparseArrays", "StaticArrays", "Test", "InteractiveUtils"] +test = ["Calculus", "DiffTests", "IrrationalConstants", "SparseArrays", "StaticArrays", "Test", "InteractiveUtils"] diff --git a/src/dual.jl b/src/dual.jl index 2ca4683f..2ff5366f 100644 --- a/src/dual.jl +++ b/src/dual.jl @@ -434,7 +434,7 @@ function Base.promote_rule(::Type{Dual{T,A,N}}, return Dual{T,promote_type(A, B),N} end -for R in (Irrational, Real, BigFloat, Bool) +for R in (AbstractIrrational, Real, BigFloat, Bool) if isconcretetype(R) # issue #322 @eval begin Base.promote_rule(::Type{$R}, ::Type{Dual{T,V,N}}) where {T,V,N} = Dual{T,promote_type($R, V),N} diff --git a/test/MiscTest.jl b/test/MiscTest.jl index 0ed8039a..66b71da5 100644 --- a/test/MiscTest.jl +++ b/test/MiscTest.jl @@ -6,6 +6,7 @@ using Test using ForwardDiff using DiffTests using SparseArrays: sparse +using IrrationalConstants include(joinpath(dirname(@__FILE__), "utils.jl")) @@ -141,6 +142,24 @@ let i, j end end +# AbstractIrrational numbers # +#----------------------------# +struct TestTag end +intrand(V) = V == Int ? rand(2:10) : rand(V) + +for N in (0,3), V in (Int, Float32), I in (Irrational, AbstractIrrational) + PARTIALS = ForwardDiff.Partials{N,V}(ntuple(n -> intrand(V), N)) + PRIMAL = intrand(V) + FDNUM = ForwardDiff.Dual{TestTag()}(PRIMAL, PARTIALS) + + @test promote_rule(typeof(FDNUM), I) == promote_rule(I, typeof(FDNUM)) + # π::Irrational, twoπ::AbstractIrrational + for IRR in (π, twoπ) + val_dual, val_irr = promote(FDNUM, IRR) + @test (val_irr, val_dual) == promote(IRR, FDNUM) + end +end + ######## # misc # ########