1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use num::complex::Complex;
use crate::cln::CLn;
trait Li2Approx<T> {
fn approx(&self) -> T;
}
impl Li2Approx<f32> for f32 {
/// rational function approximation of Re[Li2(x)] for x in [0, 1/2]
fn approx(&self) -> f32 {
let cp = [ 1.00000020_f32, -0.780790946_f32, 0.0648256871_f32 ];
let cq = [ 1.00000000_f32, -1.03077545_f32, 0.211216710_f32 ];
let x = *self;
let p = cp[0] + x*(cp[1] + x*cp[2]);
let q = cq[0] + x*(cq[1] + x*cq[2]);
x*p/q
}
}
impl Li2Approx<f64> for f64 {
/// rational function approximation of Re[Li2(x)] for x in [0, 1/2]
fn approx(&self) -> f64 {
let cp = [
0.9999999999999999502e+0_f64,
-2.6883926818565423430e+0_f64,
2.6477222699473109692e+0_f64,
-1.1538559607887416355e+0_f64,
2.0886077795020607837e-1_f64,
-1.0859777134152463084e-2_f64
];
let cq = [
1.0000000000000000000e+0_f64,
-2.9383926818565635485e+0_f64,
3.2712093293018635389e+0_f64,
-1.7076702173954289421e+0_f64,
4.1596017228400603836e-1_f64,
-3.9801343754084482956e-2_f64,
8.2743668974466659035e-4_f64
];
let x = *self;
let x2 = x*x;
let x4 = x2*x2;
let p = cp[0] + x*cp[1] + x2*(cp[2] + x*cp[3]) +
x4*(cp[4] + x*cp[5]);
let q = cq[0] + x*cq[1] + x2*(cq[2] + x*cq[3]) +
x4*(cq[4] + x*cq[5] + x2*cq[6]);
x*p/q
}
}
impl Li2Approx<Complex<f32>> for Complex<f32> {
/// series approximation of Li2(z) for Re(z) <= 1/2 and |z| <= 1
/// in terms of self = -ln(1 - z)
fn approx(&self) -> Complex<f32> {
// bf[1..N-1] are the even Bernoulli numbers / (2 n + 1)!
// generated by: Table[BernoulliB[2 n]/(2 n + 1)!, {n, 1, 19}]
let bf = [
-1.0_f32/4.0_f32,
1.0_f32/36.0_f32,
-1.0_f32/3600.0_f32,
1.0_f32/211680.0_f32,
];
let x = *self;
let x2 = x*x;
x + x2*(bf[0] + x*(bf[1] + x2*(bf[2] + x2*bf[3])))
}
}
impl Li2Approx<Complex<f64>> for Complex<f64> {
/// series approximation of Li2(z) for Re(z) <= 1/2 and |z| <= 1
/// in terms of self = -ln(1 - z)
fn approx(&self) -> Complex<f64> {
// bf[1..N-1] are the even Bernoulli numbers / (2 n + 1)!
// generated by: Table[BernoulliB[2 n]/(2 n + 1)!, {n, 1, 19}]
let bf = [
-1.0_f64/4.0_f64,
1.0_f64/36.0_f64,
-1.0_f64/3600.0_f64,
1.0_f64/211680.0_f64,
-1.0_f64/10886400.0_f64,
1.0_f64/526901760.0_f64,
-4.0647616451442255e-11_f64,
8.9216910204564526e-13_f64,
-1.9939295860721076e-14_f64,
4.5189800296199182e-16_f64,
];
let x = *self;
let x2 = x*x;
let x4 = x2*x2;
x + x2*(bf[0] +
x*(bf[1] +
x2*(
bf[2] +
x2*bf[3] +
x4*(bf[4] + x2*bf[5]) +
x4*x4*(bf[6] + x2*bf[7] + x4*(bf[8] + x2*bf[9]))
)
)
)
}
}
/// Provides the 2nd order polylogarithm (dilogarithm) function
/// `li2()` of a number of type `T`.
pub trait Li2<T> {
fn li2(&self) -> T;
}
impl Li2<f32> for f32 {
/// Returns the real dilogarithm of a real number of type `f32`.
///
/// Implemented as rational function approximation.
///
/// # Example:
/// ```
/// use polylog::Li2;
///
/// assert!((1.0_f32.li2() - 1.64493407_f32).abs() < 2.0_f32*std::f32::EPSILON);
/// ```
fn li2(&self) -> f32 {
let z2 = std::f32::consts::PI*std::f32::consts::PI/6.0_f32;
let x = *self;
// transform to [0, 1/2]
if x < -1.0_f32 {
let l = (1.0_f32 - x).ln();
(1.0_f32/(1.0_f32 - x)).approx() - z2 + l*(0.5_f32*l - (-x).ln())
} else if x == -1.0_f32 {
-0.5_f32*z2
} else if x < 0.0_f32 {
let l = (-x).ln_1p();
-(x/(x - 1.0_f32)).approx() - 0.5_f32*l*l
} else if x == 0.0_f32 {
x
} else if x < 0.5_f32 {
x.approx()
} else if x < 1.0_f32 {
-(1.0_f32 - x).approx() + z2 - x.ln()*(-x).ln_1p()
} else if x == 1.0_f32 {
z2
} else if x < 2.0_f32 {
let l = x.ln();
(1.0_f32 - 1.0_f32/x).approx() + z2 - l*((1.0_f32 - 1.0_f32/x).ln() + 0.5_f32*l)
} else {
let l = x.ln();
-(1.0_f32/x).approx() + 2.0_f32*z2 - 0.5_f32*l*l
}
}
}
impl Li2<f64> for f64 {
/// Returns the real dilogarithm of a real number of type `f64`.
///
/// Implemented as rational function approximation with a maximum
/// error of 5e-17 [[arXiv:2201.01678]].
///
/// [arXiv:2201.01678]: https://arxiv.org/abs/2201.01678
///
/// # Example:
/// ```
/// use polylog::Li2;
///
/// assert!((1.0_f64.li2() - 1.6449340668482264_f64).abs() < 2.0_f64*std::f64::EPSILON);
/// ```
fn li2(&self) -> f64 {
let z2 = std::f64::consts::PI*std::f64::consts::PI/6.0_f64;
let x = *self;
// transform to [0, 1/2]
if x < -1.0_f64 {
let l = (1.0_f64 - x).ln();
(1.0_f64/(1.0_f64 - x)).approx() - z2 + l*(0.5_f64*l - (-x).ln())
} else if x == -1.0_f64 {
-0.5_f64*z2
} else if x < 0.0_f64 {
let l = (-x).ln_1p();
-(x/(x - 1.0_f64)).approx() - 0.5_f64*l*l
} else if x == 0.0_f64 {
x
} else if x < 0.5_f64 {
x.approx()
} else if x < 1.0_f64 {
-(1.0_f64 - x).approx() + z2 - x.ln()*(-x).ln_1p()
} else if x == 1.0_f64 {
z2
} else if x < 2.0_f64 {
let l = x.ln();
(1.0_f64 - 1.0_f64/x).approx() + z2 - l*((1.0_f64 - 1.0_f64/x).ln() + 0.5_f64*l)
} else {
let l = x.ln();
-(1.0_f64/x).approx() + 2.0_f64*z2 - 0.5_f64*l*l
}
}
}
impl Li2<Complex<f32>> for Complex<f32> {
/// Returns the dilogarithm of a complex number of type
/// `Complex<f32>`.
///
/// # Example:
/// ```
/// use num::complex::Complex;
/// use polylog::Li2;
///
/// assert!((Complex::new(1.0_f32, 1.0_f32).li2() - Complex::new(0.61685028_f32, 1.46036212_f32)).norm() < std::f32::EPSILON);
/// ```
fn li2(&self) -> Complex<f32> {
let pi = std::f32::consts::PI;
let rz = self.re;
let iz = self.im;
if iz == 0.0_f32 {
if rz <= 1.0_f32 {
Complex::new(rz.li2(), iz)
} else { // rz > 1
Complex::new(rz.li2(), -pi*rz.ln())
}
} else {
let nz = self.norm_sqr();
if nz < std::f32::EPSILON {
self*(1.0_f32 + 0.25_f32*self)
} else if rz <= 0.5_f32 {
if nz > 1.0_f32 {
let l = (-self).cln();
-(-(1.0_f32 - 1.0_f32/self).cln()).approx() - 0.5_f32*l*l - pi*pi/6.0_f32
} else { // nz <= 1
(-(1.0_f32 - self).cln()).approx()
}
} else { // rz > 0.5
if nz <= 2.0_f32*rz {
let l = -(self).cln();
-l.approx() + l*(1.0_f32 - self).cln() + pi*pi/6.0_f32
} else { // nz > 2*rz
let l = (-self).cln();
-(-(1.0_f32 - 1.0_f32/self).cln()).approx() - 0.5_f32*l*l - pi*pi/6.0_f32
}
}
}
}
}
impl Li2<Complex<f64>> for Complex<f64> {
/// Returns the dilogarithm of a complex number of type
/// `Complex<f64>`.
///
/// This function has been translated from the
/// [SPheno](https://spheno.hepforge.org/) package.
///
/// # Example:
/// ```
/// use num::complex::Complex;
/// use polylog::Li2;
///
/// assert!((Complex::new(1.0_f64, 1.0_f64).li2() - Complex::new(0.6168502750680849_f64, 1.4603621167531195_f64)).norm() < 2.0_f64*std::f64::EPSILON);
/// ```
fn li2(&self) -> Complex<f64> {
let pi = std::f64::consts::PI;
let rz = self.re;
let iz = self.im;
if iz == 0.0_f64 {
if rz <= 1.0_f64 {
Complex::new(rz.li2(), iz)
} else { // rz > 1
Complex::new(rz.li2(), -pi*rz.ln())
}
} else {
let nz = self.norm_sqr();
if nz < std::f64::EPSILON {
self*(1.0_f64 + 0.25_f64*self)
} else if rz <= 0.5_f64 {
if nz > 1.0_f64 {
let l = (-self).cln();
-(-(1.0_f64 - 1.0_f64/self).cln()).approx() - 0.5_f64*l*l - pi*pi/6.0_f64
} else { // nz <= 1
(-(1.0_f64 - self).cln()).approx()
}
} else { // rz > 0.5
if nz <= 2.0_f64*rz {
let l = -(self).cln();
-l.approx() + l*(1.0_f64 - self).cln() + pi*pi/6.0_f64
} else { // nz > 2*rz
let l = (-self).cln();
-(-(1.0_f64 - 1.0_f64/self).cln()).approx() - 0.5_f64*l*l - pi*pi/6.0_f64
}
}
}
}
}