polylog/
li3.rs

1use num::complex::Complex;
2use crate::cln::CLn;
3
4/// Provides the 3rd order polylogarithm (trilogarithm) function
5/// `li3()` of a number of type `T`.
6pub trait Li3<T> {
7    fn li3(&self) -> T;
8}
9
10impl Li3<f64> for f64 {
11    /// Returns the real trilogarithm of a real number of type `f64`.
12    ///
13    /// Implemented as rational function approximations with a maximum
14    /// error of less than 2.050e-17 [[arXiv:2308.11619]].
15    ///
16    /// [arXiv:2308.11619]: https://arxiv.org/abs/2308.11619
17    ///
18    /// # Example:
19    /// ```
20    /// use polylog::Li3;
21    ///
22    /// assert!((1.0_f64.li3() - 1.2020569031595943_f64).abs() < std::f64::EPSILON);
23    /// ```
24    fn li3(&self) -> f64 {
25        let z2 = 1.6449340668482264;
26        let z3 = 1.2020569031595943;
27        let x = *self;
28
29        // transformation to [-1,0] and [0,1/2]
30        if x < -1.0 {
31            let l = (-x).ln();
32            li3_neg(1.0/x) - l*(z2 + 1.0/6.0*l*l)
33        } else if x == -1.0 {
34            -0.75*z3
35        } else if x < 0.0 {
36            li3_neg(x)
37        } else if x == 0.0 {
38            x
39        } else if x < 0.5 {
40            li3_pos(x)
41        } else if x == 0.5 {
42            0.53721319360804020
43        } else if x < 1.0 {
44            let l = x.ln();
45            -li3_neg(1.0 - x.recip()) - li3_pos(1.0 - x) + z3 + l*(z2 + l*(-0.5*(-x).ln_1p() + 1.0/6.0*l))
46        } else if x == 1.0 {
47            z3
48        } else if x < 2.0 {
49            let l = x.ln();
50            -li3_neg(1.0 - x) - li3_pos(1.0 - x.recip()) + z3 + l*(z2 + l*(-0.5*(x - 1.0).ln() + 1.0/6.0*l))
51        } else { // x >= 2.0
52            let l = x.ln();
53            li3_pos(x.recip()) + l*(2.0*z2 - 1.0/6.0*l*l)
54        }
55    }
56}
57
58// Li_3(x) for x in [-1,0]
59fn li3_neg(x: f64) -> f64 {
60    let cp = [
61        0.9999999999999999795e+0, -2.0281801754117129576e+0,
62        1.4364029887561718540e+0, -4.2240680435713030268e-1,
63        4.7296746450884096877e-2, -1.3453536579918419568e-3
64    ];
65    let cq = [
66        1.0000000000000000000e+0, -2.1531801754117049035e+0,
67        1.6685134736461140517e+0, -5.6684857464584544310e-1,
68        8.1999463370623961084e-2, -4.0756048502924149389e-3,
69        3.4316398489103212699e-5
70    ];
71
72    let x2 = x*x;
73    let x4 = x2*x2;
74    let p = cp[0] + x * cp[1] + x2 * (cp[2] + x * cp[3]) +
75            x4 * (cp[4] + x * cp[5]);
76    let q = cq[0] + x * cq[1] + x2 * (cq[2] + x * cq[3]) +
77            x4 * (cq[4] + x * cq[5] + x2 * cq[6]);
78
79    x*p/q
80}
81
82// Li_3(x) for x in [0,1/2]
83fn li3_pos(x: f64) -> f64 {
84    let cp = [
85        0.9999999999999999893e+0, -2.5224717303769789628e+0,
86        2.3204919140887894133e+0, -9.3980973288965037869e-1,
87        1.5728950200990509052e-1, -7.5485193983677071129e-3
88    ];
89    let cq = [
90        1.0000000000000000000e+0, -2.6474717303769836244e+0,
91        2.6143888433492184741e+0, -1.1841788297857667038e+0,
92        2.4184938524793651120e-1, -1.8220900115898156346e-2,
93        2.4927971540017376759e-4
94    ];
95
96    let x2 = x*x;
97    let x4 = x2*x2;
98    let p = cp[0] + x * cp[1] + x2 * (cp[2] + x * cp[3]) +
99            x4 * (cp[4] + x * cp[5]);
100    let q = cq[0] + x * cq[1] + x2 * (cq[2] + x * cq[3]) +
101            x4 * (cq[4] + x * cq[5] + x2 * cq[6]);
102
103    x*p/q
104}
105
106impl Li3<Complex<f64>> for Complex<f64> {
107    /// Returns the trilogarithm of a complex number of type `Complex<f64>`.
108    ///
109    /// # Example:
110    /// ```
111    /// use num::complex::Complex;
112    /// use polylog::Li3;
113    ///
114    /// assert!((Complex::new(1.0_f64, 1.0_f64).li3() - Complex::new(0.8711588834109380_f64, 1.2670834418889240_f64)).norm() < 2.0_f64*std::f64::EPSILON);
115    /// ```
116    fn li3(&self) -> Complex<f64> {
117        let pi  = std::f64::consts::PI;
118        let z2  = 1.6449340668482264;
119        let z3  = 1.2020569031595943;
120
121        if self.im == 0.0 {
122            if self.re <= 1.0 {
123                Complex::new(self.re.li3(), self.im)
124            } else { // rz > 1.0
125                let l = self.re.ln();
126                Complex::new(self.re.li3(), -0.5*pi*l*l)
127            }
128        } else {
129            let nz  = self.norm();
130            let pz  = self.arg();
131            let lnz = nz.ln();
132
133            if lnz*lnz + pz*pz < 1.0 { // |log(z)| < 1
134                let u  = Complex::new(lnz, pz);
135                let u2 = u*u;
136                let u4 = u2*u2;
137                let u8 = u4*u4;
138                let c0 = z3 + u*(z2 - u2/12.0);
139                let c1 = 0.25 * (3.0 - 2.0*(-u).cln());
140
141                let cs = [
142                    -3.4722222222222222e-03, 1.1574074074074074e-05,
143                    -9.8418997228521038e-08, 1.1482216343327454e-09,
144                    -1.5815724990809166e-11, 2.4195009792525152e-13,
145                    -3.9828977769894877e-15
146                ];
147
148                c0 +
149                c1*u2 +
150                u4*(cs[0] + u2*cs[1]) +
151                u8*(cs[2] + u2*cs[3] + u4*(cs[4] + u2*cs[5])) +
152                u8*u8*cs[6]
153            } else if nz <= 1.0 {
154                cli3_unit_circle(-(1.0 - self).cln())
155            } else { // nz > 1
156                let arg = if pz > 0.0 { pz - pi } else { pz + pi };
157                let lmz = Complex::new(lnz, arg); // (-self).cln()
158                cli3_unit_circle(-(1.0 - 1.0/self).cln()) - lmz*(lmz*lmz/6.0 + z2)
159            }
160        }
161    }
162}
163
164/// series approximation of Li3(z) for |z| <= 1
165/// in terms of x = -ln(1 - z)
166fn cli3_unit_circle(x: Complex<f64>) -> Complex<f64> {
167    let bf  = [
168        1.0, -3.0/8.0, 17.0/216.0, -5.0/576.0,
169        1.2962962962962963e-04,  8.1018518518518519e-05,
170       -3.4193571608537595e-06, -1.3286564625850340e-06,
171        8.6608717561098513e-08,  2.5260875955320400e-08,
172       -2.1446944683640648e-09, -5.1401106220129789e-10,
173        5.2495821146008294e-11,  1.0887754406636318e-11,
174       -1.2779396094493695e-12, -2.3698241773087452e-13,
175        3.1043578879654623e-14,  5.2617586299125061e-15,
176    ];
177    let x2 = x*x;
178    let x4 = x2*x2;
179    let x8 = x4*x4;
180
181    x*bf[0] +
182    x2*(bf[1] + x*bf[2]) +
183    x4*(bf[3] + x*bf[4] + x2*(bf[5] + x*bf[6])) +
184    x8*(bf[7] + x*bf[8] + x2*(bf[9] + x*bf[10]) +
185        x4*(bf[11] + x*bf[12] + x2*(bf[13] + x*bf[14]))) +
186    x8*x8*(bf[15] + x*bf[16] + x2*bf[17])
187}