Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit d60311e

Browse files
committed
16671: handle more argument types; fix num. H(n,m); cosmetics
1 parent ca1a03d commit d60311e

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

src/sage/functions/log.py

+41-19
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ class Function_harmonic_number_generalized(BuiltinFunction):
790790
791791
H_{s}=\int_0^1\frac{1-x^s}{1-x}
792792
793-
H_{s,m}=\zeta(s)-\zeta(s,m-1)
793+
H_{s,m}=\zeta(m)-\zeta(m,s-1)
794794
795795
If called with a single argument, that argument is ``s`` and ``m`` is
796796
assumed to be 1 (the normal harmonic numbers ``H_m``).
@@ -813,15 +813,15 @@ class Function_harmonic_number_generalized(BuiltinFunction):
813813
251/216
814814
sage: harmonic_number(5/2)
815815
-2*log(2) + 46/15
816-
sage: harmonic_number(3,3.)
816+
sage: harmonic_number(3.,3)
817817
zeta(3) - 0.0400198661225573
818818
sage: harmonic_number(3.,3.)
819819
1.16203703703704
820820
sage: harmonic_number(3,3).n(200)
821821
1.16203703703703703703703...
822822
sage: harmonic_number(1+I,5)
823823
harmonic_number(I + 1, 5)
824-
sage: harmonic_number(1.+I,5)
824+
sage: harmonic_number(5,1.+I)
825825
1.57436810798989 - 1.06194728851357*I
826826
827827
Solutions to certain sums are returned in terms of harmonic numbers::
@@ -830,10 +830,10 @@ class Function_harmonic_number_generalized(BuiltinFunction):
830830
sage: sum(1/k^7,k,1,x)
831831
harmonic_number(x, 7)
832832
833-
Check the defining integral. at a random integer::
833+
Check the defining integral at a random integer::
834834
835835
sage: n=randint(10,100)
836-
sage: bool(integrate((1-x^n)/(1-x),x,0,1) == harmonic_number(n))
836+
sage: bool(SR(integrate((1-x^n)/(1-x),x,0,1)) == harmonic_number(n))
837837
True
838838
839839
There are several special values which are automatically simplified::
@@ -899,47 +899,59 @@ def _eval_(self, z, m):
899899
251/216
900900
sage: harmonic_number(3,3).n() # this goes from rational to float
901901
1.16203703703704
902-
sage: harmonic_number(3.,3) # the following uses zeta functions
902+
sage: harmonic_number(3,3.) # the following uses zeta functions
903903
1.16203703703704
904-
sage: harmonic_number(5,0.1)
904+
sage: harmonic_number(3.,3)
905+
zeta(3) - 0.0400198661225573
906+
sage: harmonic_number(0.1,5)
905907
zeta(5) - 0.650300133161038
906-
sage: harmonic_number(5,0.1).n()
908+
sage: harmonic_number(0.1,5).n()
907909
0.386627621982332
908-
sage: harmonic_number(Qp(5)(10))
909-
4*5^-1 + 2 + 2*5 + 4*5^2 + 3*5^3 + 5^4 + 4*5^5 + 4*5^6 + 5^7 + 4*5^8 + 3*5^9 + 5^10 + 4*5^11 + 4*5^12 + 5^13 + 4*5^14 + 3*5^15 + 5^16 + 4*5^17 + 4*5^18 + O(5^19)
910+
sage: harmonic_number(3,5/2)
911+
1/27*sqrt(3) + 1/8*sqrt(2) + 1
912+
sage: harmonic_number(Qp(5)(10),1)
913+
4*5^-1 + 2 + 2*5 + 4*5^2 + 3*5^3 + 5^4 + ...
914+
sage: harmonic_number(Qp(5)(10),2)
915+
4*5^-1 + 3 + 5 + 3*5^2 + 2*5^3 + 3*5^5 + ...
910916
"""
911917
if m == 0:
912918
return z
913919
elif m == 1:
914920
return harmonic_m1._eval_(z)
915-
elif isinstance(m, Integer):
916-
from sage.misc.misc import srange
921+
from sage.symbolic.ring import SR
922+
P = s_parent(z)
923+
if not hasattr(z, 'operator') and not self._is_numerical(z):
917924
try:
918-
_ = ZZ(z)
925+
z = ZZ(z)
919926
except TypeError:
920927
pass
921928
else:
922-
return sum(1/(k**m) for k in srange(1,z+1))
929+
if (isinstance(m, (Integer, int))):
930+
if P in (ZZ, int):
931+
P = QQ
932+
return SR(P(sum(1/(k**m) for k in range(1,z+1))))
933+
elif isinstance(m, Rational):
934+
return sum(1/(k**m) for k in range(1,z+1))
923935

924936
def _evalf_(self, z, m, parent=None, algorithm=None):
925937
"""
926938
EXAMPLES::
927939
928-
sage: harmonic_number(3,3.)
940+
sage: harmonic_number(3.,3)
929941
zeta(3) - 0.0400198661225573
930942
sage: harmonic_number(3.,3.)
931943
1.16203703703704
932944
sage: harmonic_number(3,3).n(200)
933945
1.16203703703703703703703...
934-
sage: harmonic_number(I,5).n()
946+
sage: harmonic_number(5,I).n()
935947
2.36889632899995 - 3.51181956521611*I
936948
"""
937949
if m == 0:
938950
return parent(z)
939951
elif m == 1:
940952
return harmonic_m1._evalf_(z, parent)
941953
from sage.functions.transcendental import zeta, hurwitz_zeta
942-
return zeta(z) - hurwitz_zeta(z,m+1)
954+
return zeta(m) - hurwitz_zeta(m,z+1)
943955

944956
def _maxima_init_evaled_(self, n, z):
945957
"""
@@ -1069,18 +1081,28 @@ def _eval_(self, z, **kwds):
10691081
-2*log(2) + 46/15
10701082
sage: harmonic_number(2*x)
10711083
harmonic_number(2*x)
1084+
sage: harmonic_number(Qp(5)(3))
1085+
1 + 5 + 4*5^2 + 4*5^4 + 4*5^6 + ...
10721086
"""
1087+
from sage.symbolic.ring import SR
1088+
P = s_parent(z)
1089+
if P in (ZZ, int):
1090+
P = QQ
1091+
if not hasattr(z, 'operator') and not self._is_numerical(z):
1092+
try:
1093+
z = ZZ(QQ(z))
1094+
except TypeError:
1095+
pass
10731096
if isinstance(z, Integer):
10741097
if z == 0:
10751098
return Integer(0)
10761099
elif z == 1:
10771100
return Integer(1)
10781101
else:
10791102
import sage.libs.flint.arith as flint_arith
1080-
return flint_arith.harmonic_number(z)
1103+
return SR(P(flint_arith.harmonic_number(z)))
10811104
elif isinstance(z, Rational):
10821105
from sage.calculus.calculus import symbolic_sum
1083-
from sage.symbolic.ring import SR
10841106
from sage.rings.infinity import infinity
10851107
x = SR.var('x')
10861108
return z*symbolic_sum(1/x/(z+x),x,1,infinity)

0 commit comments

Comments
 (0)