Skip to content

Commit 0feb64c

Browse files
ElOceanografodevmotion
authored andcommitted
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
1 parent a6e56f7 commit 0feb64c

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

Project.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ CommonSubexpressions = "0.3"
2121
DiffResults = "0.0.1, 0.0.2, 0.0.3, 0.0.4, 1.0.1"
2222
DiffRules = "1.4.0"
2323
DiffTests = "0.0.1, 0.1"
24+
IrrationalConstants = "0.1, 0.2"
2425
LogExpFunctions = "0.3"
2526
NaNMath = "0.2.2, 0.3, 1"
2627
Preferences = "1"
@@ -35,12 +36,13 @@ ForwardDiffStaticArraysExt = "StaticArrays"
3536
Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
3637
DiffTests = "de460e47-3fe3-5279-bb4a-814414816d5d"
3738
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
39+
IrrationalConstants = "92d709cd-6900-40b7-9082-c6be49f344b6"
3840
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
3941
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
4042
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4143

4244
[targets]
43-
test = ["Calculus", "DiffTests", "SparseArrays", "Test", "InteractiveUtils", "StaticArrays"]
45+
test = ["Calculus", "DiffTests", "IrrationalConstants", "SparseArrays", "Test", "InteractiveUtils", "StaticArrays"]
4446

4547
[weakdeps]
46-
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
48+
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

src/dual.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ function Base.promote_rule(::Type{Dual{T,A,N}},
415415
return Dual{T,promote_type(A, B),N}
416416
end
417417

418-
for R in (Irrational, Real, BigFloat, Bool)
418+
for R in (AbstractIrrational, Real, BigFloat, Bool)
419419
if isconcretetype(R) # issue #322
420420
@eval begin
421421
Base.promote_rule(::Type{$R}, ::Type{Dual{T,V,N}}) where {T,V,N} = Dual{T,promote_type($R, V),N}

test/MiscTest.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ using Test
66
using ForwardDiff
77
using DiffTests
88
using SparseArrays: sparse
9+
using IrrationalConstants
910

1011
include(joinpath(dirname(@__FILE__), "utils.jl"))
1112

@@ -141,6 +142,24 @@ let i, j
141142
end
142143
end
143144

145+
# AbstractIrrational numbers #
146+
#----------------------------#
147+
struct TestTag end
148+
intrand(V) = V == Int ? rand(2:10) : rand(V)
149+
150+
for N in (0,3), V in (Int, Float32), I in (Irrational, AbstractIrrational)
151+
PARTIALS = ForwardDiff.Partials{N,V}(ntuple(n -> intrand(V), N))
152+
PRIMAL = intrand(V)
153+
FDNUM = ForwardDiff.Dual{TestTag()}(PRIMAL, PARTIALS)
154+
155+
@test promote_rule(typeof(FDNUM), I) == promote_rule(I, typeof(FDNUM))
156+
# π::Irrational, twoπ::AbstractIrrational
157+
for IRR in (π, twoπ)
158+
val_dual, val_irr = promote(FDNUM, IRR)
159+
@test (val_irr, val_dual) == promote(IRR, FDNUM)
160+
end
161+
end
162+
144163
########
145164
# misc #
146165
########

0 commit comments

Comments
 (0)