Skip to content

Commit

Permalink
Broaden promote_rule for Dual to include AbstractIrrational (#687)
Browse files Browse the repository at this point in the history
* Broaden promote_rule for Dual to include AbstractIrrational

* add IrrationalConstants test dependency

* Add tests for AbstractIrrational promote_rule

* fix pkg and test bugs
  • Loading branch information
ElOceanografo authored Mar 13, 2024
1 parent cbb2733 commit 42b1554
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand All @@ -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"]
2 changes: 1 addition & 1 deletion src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
19 changes: 19 additions & 0 deletions test/MiscTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using Test
using ForwardDiff
using DiffTests
using SparseArrays: sparse
using IrrationalConstants

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

Expand Down Expand Up @@ -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 #
########
Expand Down

0 comments on commit 42b1554

Please sign in to comment.