@@ -344,10 +344,7 @@ this definition is equivalent to the Hurwitz zeta function
344
344
``\\ sum_{k=0}^\\ infty (k+z)^{-s}``. For ``z=1``, it yields
345
345
the Riemann zeta function ``\\ zeta(s)``.
346
346
"""
347
- zeta (s,z)
348
-
349
- function zeta (s:: Union{Int, ComplexOrReal{Float64}} ,
350
- z:: ComplexOrReal{Float64} )
347
+ function zeta (s:: ComplexOrReal{Float64} , z:: ComplexOrReal{Float64} )
351
348
ζ = zero (promote_type (typeof (s), typeof (z)))
352
349
353
350
(z == 1 || z == 0 ) && return oftype (ζ, zeta (s))
@@ -611,10 +608,11 @@ end
611
608
612
609
# Converting types that we can convert, and not ones we can not
613
610
# Float16, and Float32 and their Complex equivalents can be converted to Float64
614
- # and results converted back. Similar for BitIntegers (eg Int32), but no converting back.
611
+ # and results converted back.
615
612
# Otherwise, we need to make things use their own `float` converting methods
616
613
# and in those cases, we do not convert back either as we assume
617
614
# they also implement their own versions of the functions, with the correct return types.
615
+ # This is the case for BitIntegers (which become `Float64` when `float`ed).
618
616
# Otherwise, if they do not implement their version of the functions we
619
617
# manually throw a `MethodError`.
620
618
# This case occurs, when calling `float` on a type does not change its type,
@@ -627,22 +625,17 @@ end
627
625
# Float16 version, by using a precomputed table look-up.
628
626
629
627
630
-
631
- ofpromotedtype (as:: Tuple , c) = convert (promote_type (typeof .(as)... ), c)
632
- ComplexOrRealUnion (TS... ) = Union{(ComplexOrReal{T} for T in TS). .. }
633
-
634
- const types_le_Float64 = (Float16, Float32, Float64, Base. BitInteger. types... )
635
-
636
- for T in types_le_Float64
628
+ for T in (Float16, Float32, Float64)
637
629
@eval f64 (x:: Complex{$T} ) = Complex128 (x)
638
630
@eval f64 (x:: $T ) = Float64 (x)
639
631
end
640
632
641
633
642
634
for f in (:digamma , :trigamma , :zeta , :eta , :invdigamma )
643
635
@eval begin
644
- $ f (z:: ComplexOrRealUnion (Base. BitInteger. types... )) = $ f (f64 (z))
645
- $ f (z:: ComplexOrRealUnion (Float16,Float32)) = oftype (z, $ f (f64 (z)))
636
+ function $f (z:: Union{ComplexOrReal{Float16}, ComplexOrReal{Float32}} )
637
+ oftype (z, $ f (f64 (z)))
638
+ end
646
639
647
640
function $f (z:: Number )
648
641
x = float (z)
@@ -653,41 +646,19 @@ for f in (:digamma, :trigamma, :zeta, :eta, :invdigamma)
653
646
end
654
647
end
655
648
656
- function polygamma (m:: Integer , z:: ComplexOrRealUnion (Float16,Float32))
657
- oftype (z, polygamma (m, f64 (z)))
658
- end
659
649
660
- for T1 in types_le_Float64, T2 in types_le_Float64
661
- if (T1 == T2 == Float64) || (T1 == Int && T2 == Float64)
662
- continue # Avoid redefining base definition
663
- # However this skips `zeta(::Complex{Int}, ::ComplexOrReal{Float64})`
664
- # so that will need to be added back after
665
- end
666
-
667
- if T1<: Integer && T2<: Integer
668
- @eval function zeta (s:: ComplexOrReal{$T1} , z:: ComplexOrReal{$T2} )
669
- zeta (f64 (s), f64 (z)) # Do not promote down to Integers
670
- end
671
- else
672
- @eval function zeta (s:: ComplexOrReal{$T1} , z:: ComplexOrReal{$T2} )
673
- ofpromotedtype ((s, z), zeta (f64 (s), f64 (z)))
674
- end
675
- end
676
- end
650
+ for T1 in (Float16, Float32, Float64), T2 in (Float16, Float32, Float64)
651
+ (T1 == T2 == Float64) && continue # Avoid redefining base definition
677
652
678
- # this is the one definition that is skipped
679
- function zeta (s:: Complex{Int} , z:: ComplexOrReal{Float64} ):: Complex{Float64}
680
- zeta (f64 (s), f64 (z))
681
- end
653
+ @eval function zeta (s:: ComplexOrReal{$T1} , z:: ComplexOrReal{$T2} )
654
+ ζ = zeta (f64 (s), f64 (z))
682
655
683
- function zeta (s:: Integer , z:: Number )
684
- t = Int (s) # One could worry here about converting a BigInteger into a Int32/Int64
685
- x = float (z)
686
- if typeof (t) === typeof (s) && typeof (x) === typeof (z)
687
- # There is nothing to fallback to, since this didn't work
688
- throw (MethodError (zeta,(s,z)))
689
- end
690
- zeta (t, x)
656
+ if typeof (s) <: Complex || typeof (z) <: Complex
657
+ convert (Complex{$ T2}, ζ)
658
+ else
659
+ convert (typeof (z), ζ)
660
+ end
661
+ end
691
662
end
692
663
693
664
@@ -701,6 +672,18 @@ function zeta(s::Number, z::Number)
701
672
zeta (t, x)
702
673
end
703
674
675
+ # It is safe to convert `s` to a float as underflow will occur before precision issues
676
+ zeta (s:: Integer , z:: Number ) = zeta (oftype (z, s), z)
677
+ function zeta (s:: Integer , z:: Integer )
678
+ x= float (z)
679
+ zeta (oftype (x, s), x)
680
+ end
681
+
682
+
683
+ function polygamma (m:: Integer , z:: Union{ComplexOrReal{Float16}, ComplexOrReal{Float32}} )
684
+ oftype (z, polygamma (m, f64 (z)))
685
+ end
686
+
704
687
705
688
function polygamma (m:: Integer , z:: Number )
706
689
x = float (z)
0 commit comments