WOLFRAM NOTEBOOK

In[]:=
deploy
Thu 8 Jul 2021 15:33:56

Inverting
1
H
n
n
i=1
s
1-
1
i
1
i

Summarizing results of https://math.stackexchange.com/questions/4180259/behavior-of-inverse-of-fs-frac1h-n-sum-i-1n-left1-frac1i-right

Forward approximations

tldr : inverting skbmoore version is off by factor of >3, the rest are close to true result

Approximating sum with an integral

In[]:=
sum[s_,n_]:=
1
HarmonicNumber[n]
Sum
s
1-
1
i
1
i
,{i,1,n};(*Assuming{n>1,s>1},Integrate
s
1-
1
i
1
i
,{i,1,n}HarmonicNumber[n]*)integral[s_,n_]:=
-HarmonicNumber[s]+
sHypergeometricPFQ{1,1,1-s},{2,2},
1
n
n
+Log[n]
HarmonicNumber[n]
;With[{n=2000,p=1},DiscretePlot[sum[s,n]-integral[s,n],{s,1.,100},PlotLabel"Error of integral approximation"]]
Out[]=

Approximating sum with gary/skbmoore answers

In[]:=
With{n=2000,p=1},sum[s_,n_]:=
1
HarmonicNumber[n]
Sum
s
1-
1
i
1
i
,{i,1,n};(*Assuming{n>1,s>1},Integrate
s
1-
1
i
1
i
,{i,1,n}HarmonicNumber[n]*)integral[s_,n_]:=
-HarmonicNumber[s]+
sHypergeometricPFQ{1,1,1-s},{2,2},
1
n
n
+Log[n]
HarmonicNumber[n]
;skbmoore[s_,n_]:=1-
Log[s]+EulerGamma
Log[n]
;gary[s_,n_]:=
1
HarmonicNumber[n]
E1
s
n
;DiscretePlot[{f[s,n],integral[s,n],skbmoore[s,n],gary[s,n]},{s,1.,100},PlotLabelff[s,1000],AxesLabel{"n","f"},PlotLegends{"true","integral","skbmoore","gary"}]With[{n=2000},DiscretePlot[{integral[s,n]-sum[s,n],gary[s,n]-sum[s,n]},{s,1.,1000},PlotLabel"error of "ff[s,1000],AxesLabel{"s","error"},PlotLegends{"integral","gary"}]]
Out[]=
true
integral
skbmoore
gary
Out[]=
integral
gary

Backward approximations

TLDR: integral version is 100 x faster and off by 1/30000, exponential integral is 1000 x faster and off by .5/30000, exp small approximation is off by 178/30000 and same as exponential integral speed

Exponential integrals and inverses

In[]:=
E1[z_]:=-ExpIntegralEi[-z];(*exponentialintegral,https://dlmf.nist.gov/6.2#E1*)E1inv[y_]:=x/.FindRoot[E1[x]y,{x,2}];(*smallxinverse*)E1invSmall[x_]:=-Log[x]-Log[-Log[x]]-
Log[-Log[x]]-1
Log[x]
;(*+O
2
Log[-Log[x]]
2
Log[x]
*)(*largexinverse*)E1invLarge[x_]:=Exp[-x-EulerGamma]+Exp[-2x-2EulerGamma]+
5
4
Exp[-3x-3EulerGamma];DiscretePlot[{Abs[E1invSmall[y]-E1inv[y]],Abs[E1invLarge[y]-E1inv[y]]},{y,0,.9,.01},PlotLegends{"small","large"},PlotLabel"discrepancy"]
Out[]=
small
large

Compare inverses

In[]:=
solveSum[n_,eps_]:=formula[s_]=
1
HarmonicNumber[n]
Sum
s
1-
1
i
1
i
,{i,1,n};invert[f_]:=FindRoot[f[s]eps,{s,2},WorkingPrecisionMachinePrecision+Log[n]];s/.invert[formula];solveIntegral[n_,eps_]:=formula[s_]:=
-HarmonicNumber[s]+
sHypergeometricPFQ{1,1,1-s},{2,2},
1
n
n
+Log[n]
HarmonicNumber[n]
;invert[f_]:=FindRoot[f[s]eps,{s,2},WorkingPrecisionMachinePrecision+Log[n]];s/.invert[formula];harmonic[i_]:=Log[i]+EulerGamma+
1
2i
;solveGary[n_,eps_]:=nLambertW
1
epsHarmonicNumber[n]
;solveE1[n_,eps_]:=n*E1inv[eps*HarmonicNumber[n]];solveE1Log[n_,eps_]:=n*E1inv[eps*harmonic[n]];solveE1SmallLog[n_,eps_]:=n*E1invSmall[eps*harmonic[n]];solveE1Large[n_,eps_]:=n*E1invLarge[eps*HarmonicNumber[n]];solveE1LargeLog[n_,eps_]:=n*E1invLarge[eps*harmonic[n]];(*inverseforxcloseto0*)With[{n=10000,eps=
-3
10
},Print[eps*HarmonicNumber[n]//N];(*Print["exact: ",solveSum[n,eps]//Timing];(*exact:4.00,32275.58*)*)(*hardcoretheresultforn=10000,eps=
-3
10
*)exact=32275.58126454601799322175523564433222149345`25.164930142167186;Print["integral: ",solveIntegral[n,eps]-exact//Timing];Print["gary: ",solveGary[n,eps]-exact//N//Timing];Print["gary exp: ",solveE1[n,eps]-exact//N//Timing];Print["gary exp log: ",solveE1Log[n,eps]-exact//N//Timing];Print["gary exp small: ",solveE1Small[n,eps]-exact//N//Timing];Print["gary exp small log: ",solveE1SmallLog[n,eps]-exact//N//Timing];Print["gary exp large: ",solveE1Large[n,eps]-exact//N//Timing];]

Small vs large approximation

Wolfram Cloud

You are using a browser not supported by the Wolfram Cloud

Supported browsers include recent versions of Chrome, Edge, Firefox and Safari.


I understand and wish to continue anyway »

You are using a browser not supported by the Wolfram Cloud. Supported browsers include recent versions of Chrome, Edge, Firefox and Safari.