Skip to content

Commit 42b1554

Browse files
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 cbb2733 commit 42b1554

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
@@ -1,6 +1,6 @@
11
name = "ForwardDiff"
22
uuid = "f6369f11-7733-5829-9624-2563aa707210"
3-
version = "0.11-DEV"
3+
version = "0.11.0-DEV"
44

55
[deps]
66
CommonSubexpressions = "bbf7d656-a473-5ed7-a52c-81e309532950"
@@ -27,6 +27,7 @@ CommonSubexpressions = "0.3"
2727
DiffResults = "1.1"
2828
DiffRules = "1.4"
2929
DiffTests = "0.1"
30+
IrrationalConstants = "0.1, 0.2"
3031
LogExpFunctions = "0.3"
3132
NaNMath = "1"
3233
Preferences = "1"
@@ -38,9 +39,10 @@ julia = "1.6"
3839
Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
3940
DiffTests = "de460e47-3fe3-5279-bb4a-814414816d5d"
4041
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
42+
IrrationalConstants = "92d709cd-6900-40b7-9082-c6be49f344b6"
4143
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
4244
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
4345
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4446

4547
[targets]
46-
test = ["Calculus", "DiffTests", "SparseArrays", "StaticArrays", "Test", "InteractiveUtils"]
48+
test = ["Calculus", "DiffTests", "IrrationalConstants", "SparseArrays", "StaticArrays", "Test", "InteractiveUtils"]

src/dual.jl

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

437-
for R in (Irrational, Real, BigFloat, Bool)
437+
for R in (AbstractIrrational, Real, BigFloat, Bool)
438438
if isconcretetype(R) # issue #322
439439
@eval begin
440440
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)