Skip to content

Commit

Permalink
Replace all methods of promote_type by promote_rule (#70)
Browse files Browse the repository at this point in the history
* Replace all methods of `promote_type` by `promote_rule`

* Add some tests
  • Loading branch information
lgoettgens authored Sep 13, 2024
1 parent 8ad37c2 commit 3a4803f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/ArbNumerics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export ArbNumber,
determinant, inverse, fastmul, # \timesbar

import Base: IEEEFloat,
hash, promote_rule, promote_type, convert,
hash, promote_rule, convert,
copy, deepcopy, string, show,

zero, one,
Expand Down
49 changes: 9 additions & 40 deletions src/libarb/promote.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
for T in (:Int8, :Int16, :Int32, :Int64, :Int128, :Float16, :Float32, :Float64)
@eval begin
promote_type(::Type{ArbComplex}, ::Type{$T}) = ArbComplex
promote_type(::Type{ArbReal}, ::Type{$T}) = ArbReal
promote_type(::Type{ArbFloat}, ::Type{$T}) = ArbFloat

promote_type(::Type{ArbComplex{P}}, ::Type{$T}) where {P} = ArbComplex{P}
promote_type(::Type{ArbReal{P}}, ::Type{$T}) where {P} = ArbReal{P}
promote_type(::Type{ArbFloat{P}}, ::Type{$T}) where {P} = ArbFloat{P}

promote_rule(::Type{ArbComplex}, ::Type{$T}) = ArbComplex
promote_rule(::Type{ArbReal}, ::Type{$T}) = ArbReal
promote_rule(::Type{ArbFloat}, ::Type{$T}) = ArbFloat
Expand All @@ -18,18 +10,10 @@ for T in (:Int8, :Int16, :Int32, :Int64, :Int128, :Float16, :Float32, :Float64)
end
end

promote_type(::Type{ArbComplex}, ::Type{ArbReal}) = ArbComplex
promote_type(::Type{ArbComplex}, ::Type{ArbFloat}) = ArbComplex
promote_type(::Type{ArbReal}, ::Type{ArbFloat}) = ArbReal

promote_rule(::Type{ArbComplex}, ::Type{ArbReal}) = ArbComplex
promote_rule(::Type{ArbComplex}, ::Type{ArbFloat}) = ArbComplex
promote_rule(::Type{ArbReal}, ::Type{ArbFloat}) = ArbReal

promote_type(::Type{ArbComplex{P}}, ::Type{ArbReal{P}}) where {P} = ArbComplex{P}
promote_type(::Type{ArbComplex{P}}, ::Type{ArbFloat{P}}) where {P} = ArbComplex{P}
promote_type(::Type{ArbReal{P}}, ::Type{ArbFloat{P}}) where {P} = ArbReal{P}

promote_rule(::Type{ArbComplex{P}}, ::Type{ArbReal{P}}) where {P} = ArbComplex{P}
promote_rule(::Type{ArbComplex{P}}, ::Type{ArbFloat{P}}) where {P} = ArbComplex{P}
promote_rule(::Type{ArbReal{P}}, ::Type{ArbFloat{P}}) where {P} = ArbReal{P}
Expand All @@ -38,32 +22,17 @@ promote_rule(::Type{ArbComplex{P}}, ::Type{Mag}) where {P} = ArbComplex{P}
promote_rule(::Type{ArbReal{P}}, ::Type{Mag}) where {P} = ArbReal{P}
promote_rule(::Type{ArbFloat{P}}, ::Type{Mag}) where {P} = ArbFloat{P}

promote_type(::Type{ArbFloat{P}}, ::Type{BigInt}) where {P} = ArbFloat{P}
promote_type(::Type{ArbReal{P}}, ::Type{BigInt}) where {P} = ArbReal{P}
promote_type(::Type{ArbComplex{P}}, ::Type{BigInt}) where {P} = ArbComplex{P}

promote_type(::Type{ArbFloat{P}}, ::Type{Rational{I}}) where {P,I} = ArbFloat{P}
promote_type(::Type{ArbReal{P}}, ::Type{Rational{I}}) where {P,I} = ArbReal{P}
promote_type(::Type{ArbComplex{P}}, ::Type{Rational{I}}) where {P,I} = ArbComplex{P}

promote_rule(::Type{ArbFloat{P}}, ::Type{BigInt}) where {P} = promote_type(ArbFloat{P}, BigInt)
promote_rule(::Type{ArbReal{P}}, ::Type{BigInt}) where {P} = promote_type(ArbReal{P}, BigInt)
promote_rule(::Type{ArbComplex{P}}, ::Type{BigInt}) where {P} = promote_type(ArbComplex{P}, BigInt)

promote_rule(::Type{ArbFloat{P}}, ::Type{Rational{I}}) where {P,I} = promote_type(ArbFloat{P}, Rational{I})
promote_rule(::Type{ArbReal{P}}, ::Type{Rational{I}}) where {P,I} = promote_type(ArbReal{P}, Rational{I})
promote_rule(::Type{ArbComplex{P}}, ::Type{Rational{I}}) where {P,I} = promote_type(ArbComplex{P}, Rational{I})
promote_rule(::Type{ArbFloat{P}}, ::Type{BigInt}) where {P} = ArbFloat{P}
promote_rule(::Type{ArbReal{P}}, ::Type{BigInt}) where {P} = ArbReal{P}
promote_rule(::Type{ArbComplex{P}}, ::Type{BigInt}) where {P} = ArbComplex{P}

promote_type(::Type{BigFloat},::Type{ArbFloat{P}}) where {P} = ArbFloat{P}
promote_type(::Type{BigFloat},::Type{ArbReal{P}}) where {P} = ArbReal{P}
promote_type(::Type{BigFloat},::Type{ArbComplex{P}}) where {P} = ArbComplex{P}
promote_type(::Type{ArbFloat{P}}, ::Type{BigFloat}) where {P} = ArbFloat{P}
promote_type(::Type{ArbReal{P}}, ::Type{BigFloat}) where {P} = ArbReal{P}
promote_type(::Type{ArbComplex{P}}, ::Type{BigFloat}) where {P} = ArbComplex{P}
promote_rule(::Type{ArbFloat{P}}, ::Type{Rational{I}}) where {P,I} = ArbFloat{P}
promote_rule(::Type{ArbReal{P}}, ::Type{Rational{I}}) where {P,I} = ArbReal{P}
promote_rule(::Type{ArbComplex{P}}, ::Type{Rational{I}}) where {P,I} = ArbComplex{P}

promote_rule(::Type{ArbFloat{P}}, ::Type{BigFloat}) where {P} = promote_type(ArbFloat{P}, BigFloat)
promote_rule(::Type{ArbReal{P}}, ::Type{BigFloat}) where {P} = promote_type(ArbReal{P}, BigFloat)
promote_rule(::Type{ArbComplex{P}}, ::Type{BigFloat}) where {P} = promote_type(ArbComplex{P}, BigFloat)
promote_rule(::Type{ArbFloat{P}}, ::Type{BigFloat}) where {P} = ArbFloat{P}
promote_rule(::Type{ArbReal{P}}, ::Type{BigFloat}) where {P} = ArbReal{P}
promote_rule(::Type{ArbComplex{P}}, ::Type{BigFloat}) where {P} = ArbComplex{P}

promote_rule(::Type{ArbFloat{P}}, ::Type{I}) where {P, I<:Integer} = ArbFloat{P}
promote_rule(::Type{ArbReal{P}}, ::Type{I}) where {P, I<:Integer} = ArbReal{P}
Expand Down
4 changes: 4 additions & 0 deletions test/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ acb = inv(ArbComplex(3))
@test Float64(arf / arf) == 1.0
@test Float64(arb / arb) == 1.0
@test Float64(acb / acb) == 1.0

@test arb + 1 isa ArbReal
@test 1.0 - arb isa ArbReal
@test arb * acb isa ArbComplex
end

0 comments on commit 3a4803f

Please sign in to comment.