3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004 Gerard Jungman
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 /* Author: G. Jungman */
23 #include <gsl/gsl_math.h>
24 #include <gsl/gsl_errno.h>
25 #include <gsl/gsl_sf_clausen.h>
26 #include <gsl/gsl_sf_trig.h>
27 #include <gsl/gsl_sf_log.h>
28 #include <gsl/gsl_sf_dilog.h>
31 /* Evaluate series for real dilog(x)
32 * Sum[ x^k / k^2, {k,1,Infinity}]
34 * Converges rapidly for |x| < 1/2.
38 dilog_series_1(const double x, gsl_sf_result * result)
40 const int kmax = 1000;
44 for(k=2; k<kmax; k++) {
45 const double rk = (k-1.0)/k;
49 if(fabs(term/sum) < GSL_DBL_EPSILON) break;
53 result->err = 2.0 * fabs(term);
54 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
57 GSL_ERROR ("error", GSL_EMAXITER);
63 /* Compute the associated series
65 * sum_{k=1}{infty} r^k / (k^2 (k+1))
67 * This is a series which appears in the one-step accelerated
68 * method, which splits out one elementary function from the
69 * full definition of Li_2(x). See below.
72 series_2(double r, gsl_sf_result * result)
74 static const int kmax = 100;
82 ds = rk/(k*k*(k+1.0));
89 ds = rk/(k*k*(k+1.0));
91 if(fabs(ds/sum) < 0.5*GSL_DBL_EPSILON) break;
95 result->err = 2.0 * kmax * GSL_DBL_EPSILON * fabs(sum);
101 /* Compute Li_2(x) using the accelerated series representation.
103 * Li_2(x) = 1 + (1-x)ln(1-x)/x + series_2(x)
105 * assumes: -1 < x < 1
108 dilog_series_2(double x, gsl_sf_result * result)
110 const int stat_s3 = series_2(x, result);
113 t = (1.0 - x) * log(1.0-x) / x;
116 static const double c3 = 1.0/3.0;
117 static const double c4 = 1.0/4.0;
118 static const double c5 = 1.0/5.0;
119 static const double c6 = 1.0/6.0;
120 static const double c7 = 1.0/7.0;
121 static const double c8 = 1.0/8.0;
122 const double t68 = c6 + x*(c7 + x*c8);
123 const double t38 = c3 + x *(c4 + x *(c5 + x * t68));
124 t = (x - 1.0) * (1.0 + x*(0.5 + x*t38));
126 result->val += 1.0 + t;
127 result->err += 2.0 * GSL_DBL_EPSILON * fabs(t);
132 /* Calculates Li_2(x) for real x. Assumes x >= 0.0.
136 dilog_xge0(const double x, gsl_sf_result * result)
140 const int stat_ser = dilog_series_2(1.0/x, &ser);
141 const double log_x = log(x);
142 const double t1 = M_PI*M_PI/3.0;
143 const double t2 = ser.val;
144 const double t3 = 0.5*log_x*log_x;
145 result->val = t1 - t2 - t3;
146 result->err = GSL_DBL_EPSILON * fabs(log_x) + ser.err;
147 result->err += GSL_DBL_EPSILON * (fabs(t1) + fabs(t2) + fabs(t3));
148 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
153 const int stat_ser = dilog_series_2(1.0 - 1.0/x, &ser);
154 const double log_x = log(x);
155 const double log_term = log_x * (log(1.0-1.0/x) + 0.5*log_x);
156 const double t1 = M_PI*M_PI/6.0;
157 const double t2 = ser.val;
158 const double t3 = log_term;
159 result->val = t1 + t2 - t3;
160 result->err = GSL_DBL_EPSILON * fabs(log_x) + ser.err;
161 result->err += GSL_DBL_EPSILON * (fabs(t1) + fabs(t2) + fabs(t3));
162 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
166 /* series around x = 1.0 */
167 const double eps = x - 1.0;
168 const double lne = log(eps);
169 const double c0 = M_PI*M_PI/6.0;
170 const double c1 = 1.0 - lne;
171 const double c2 = -(1.0 - 2.0*lne)/4.0;
172 const double c3 = (1.0 - 3.0*lne)/9.0;
173 const double c4 = -(1.0 - 4.0*lne)/16.0;
174 const double c5 = (1.0 - 5.0*lne)/25.0;
175 const double c6 = -(1.0 - 6.0*lne)/36.0;
176 const double c7 = (1.0 - 7.0*lne)/49.0;
177 const double c8 = -(1.0 - 8.0*lne)/64.0;
178 result->val = c0+eps*(c1+eps*(c2+eps*(c3+eps*(c4+eps*(c5+eps*(c6+eps*(c7+eps*c8)))))));
179 result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
183 result->val = M_PI*M_PI/6.0;
184 result->err = 2.0 * GSL_DBL_EPSILON * M_PI*M_PI/6.0;
189 const int stat_ser = dilog_series_2(1.0-x, &ser);
190 const double log_x = log(x);
191 const double t1 = M_PI*M_PI/6.0;
192 const double t2 = ser.val;
193 const double t3 = log_x*log(1.0-x);
194 result->val = t1 - t2 - t3;
195 result->err = GSL_DBL_EPSILON * fabs(log_x) + ser.err;
196 result->err += GSL_DBL_EPSILON * (fabs(t1) + fabs(t2) + fabs(t3));
197 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
201 return dilog_series_2(x, result);
204 return dilog_series_1(x, result);
215 /* Evaluate the series representation for Li2(z):
217 * Li2(z) = Sum[ |z|^k / k^2 Exp[i k arg(z)], {k,1,Infinity}]
222 * It is used only for small r.
230 gsl_sf_result * real_result,
231 gsl_sf_result * imag_result
234 const double cos_theta = x/r;
235 const double sin_theta = y/r;
236 const double alpha = 1.0 - cos_theta;
237 const double beta = sin_theta;
238 double ck = cos_theta;
239 double sk = sin_theta;
241 double real_sum = r*ck;
242 double imag_sum = r*sk;
243 const int kmax = 50 + (int)(22.0/(-log(r))); /* tuned for double-precision */
245 for(k=2; k<kmax; k++) {
248 ck = ck - (alpha*ck + beta*sk);
249 sk = sk - (alpha*sk - beta*ck_tmp);
251 dr = rk/((double)k*k) * ck;
252 di = rk/((double)k*k) * sk;
255 if(fabs((dr*dr + di*di)/(real_sum*real_sum + imag_sum*imag_sum)) < GSL_DBL_EPSILON*GSL_DBL_EPSILON) break;
258 real_result->val = real_sum;
259 real_result->err = 2.0 * kmax * GSL_DBL_EPSILON * fabs(real_sum);
260 imag_result->val = imag_sum;
261 imag_result->err = 2.0 * kmax * GSL_DBL_EPSILON * fabs(imag_sum);
269 * sum_{k=1}{infty} z^k / (k^2 (k+1))
271 * This is a series which appears in the one-step accelerated
272 * method, which splits out one elementary function from the
273 * full definition of Li_2.
280 gsl_sf_result * sum_re,
281 gsl_sf_result * sum_im
284 const double cos_theta = x/r;
285 const double sin_theta = y/r;
286 const double alpha = 1.0 - cos_theta;
287 const double beta = sin_theta;
288 double ck = cos_theta;
289 double sk = sin_theta;
291 double real_sum = 0.5 * r*ck;
292 double imag_sum = 0.5 * r*sk;
293 const int kmax = 30 + (int)(18.0/(-log(r))); /* tuned for double-precision */
295 for(k=2; k<kmax; k++)
298 const double ck_tmp = ck;
299 ck = ck - (alpha*ck + beta*sk);
300 sk = sk - (alpha*sk - beta*ck_tmp);
302 dr = rk/((double)k*k*(k+1.0)) * ck;
303 di = rk/((double)k*k*(k+1.0)) * sk;
306 if(fabs((dr*dr + di*di)/(real_sum*real_sum + imag_sum*imag_sum)) < GSL_DBL_EPSILON*GSL_DBL_EPSILON) break;
309 sum_re->val = real_sum;
310 sum_re->err = 2.0 * kmax * GSL_DBL_EPSILON * fabs(real_sum);
311 sum_im->val = imag_sum;
312 sum_im->err = 2.0 * kmax * GSL_DBL_EPSILON * fabs(imag_sum);
318 /* Compute Li_2(z) using the one-step accelerated series.
320 * Li_2(z) = 1 + (1-z)ln(1-z)/z + series_2_c(z)
324 * assumes: r > epsilon, so that we take no special care with log(1-z)
332 gsl_sf_result * real_dl,
333 gsl_sf_result * imag_dl
346 gsl_sf_result sum_re;
347 gsl_sf_result sum_im;
348 const int stat_s3 = series_2_c(r, x, y, &sum_re, &sum_im);
351 gsl_sf_result ln_omz_r;
352 gsl_sf_result ln_omz_theta;
353 const int stat_log = gsl_sf_complex_log_e(1.0-x, -y, &ln_omz_r, &ln_omz_theta);
354 const double t_x = ( ln_omz_r.val * x + ln_omz_theta.val * y)/(r*r);
355 const double t_y = (-ln_omz_r.val * y + ln_omz_theta.val * x)/(r*r);
357 /* r = (1-z) ln(1-z)/z */
358 const double r_x = (1.0 - x) * t_x + y * t_y;
359 const double r_y = (1.0 - x) * t_y - y * t_x;
361 real_dl->val = sum_re.val + r_x + 1.0;
362 imag_dl->val = sum_im.val + r_y;
363 real_dl->err = sum_re.err + 2.0*GSL_DBL_EPSILON*(fabs(real_dl->val) + fabs(r_x));
364 imag_dl->err = sum_im.err + 2.0*GSL_DBL_EPSILON*(fabs(imag_dl->val) + fabs(r_y));
365 return GSL_ERROR_SELECT_2(stat_s3, stat_log);
370 /* Evaluate a series for Li_2(z) when |z| is near 1.
371 * This is uniformly good away from z=1.
373 * Li_2(z) = Sum[ a^n/n! H_n(theta), {n, 0, Infinity}]
376 * H_n(theta) = Sum[ e^(i m theta) m^n / m^2, {m, 1, Infinity}]
379 * H_0(t) = Gl_2(t) + i Cl_2(t)
380 * H_1(t) = 1/2 ln(2(1-c)) + I atan2(-s, 1-c)
381 * H_2(t) = -1/2 + I/2 s/(1-c)
382 * H_3(t) = -1/2 /(1-c)
383 * H_4(t) = -I/2 s/(1-c)^2
384 * H_5(t) = 1/2 (2 + c)/(1-c)^2
385 * H_6(t) = I/2 s/(1-c)^5 (8(1-c) - s^2 (3 + c))
393 gsl_sf_result * real_result,
394 gsl_sf_result * imag_result
397 const double theta = atan2(y, x);
398 const double cos_theta = x/r;
399 const double sin_theta = y/r;
400 const double a = log(r);
401 const double omc = 1.0 - cos_theta;
402 const double omc2 = omc*omc;
406 double sum_re, sum_im;
410 H_re[0] = M_PI*M_PI/6.0 + 0.25*(theta*theta - 2.0*M_PI*fabs(theta));
411 gsl_sf_clausen_e(theta, &Him0);
414 H_re[1] = -0.5*log(2.0*omc);
415 H_im[1] = -atan2(-sin_theta, omc);
418 H_im[2] = 0.5 * sin_theta/omc;
424 H_im[4] = -0.5*sin_theta/omc2;
426 H_re[5] = 0.5 * (2.0 + cos_theta)/omc2;
430 H_im[6] = 0.5 * sin_theta/(omc2*omc2*omc) * (8.0*omc - sin_theta*sin_theta*(3.0 + cos_theta));
436 for(n=1; n<=6; n++) {
441 sum_re += t * H_re[n];
442 sum_im += t * H_im[n];
445 real_result->val = sum_re;
446 real_result->err = 2.0 * 6.0 * GSL_DBL_EPSILON * fabs(sum_re) + fabs(an/nfact);
447 imag_result->val = sum_im;
448 imag_result->err = 2.0 * 6.0 * GSL_DBL_EPSILON * fabs(sum_im) + Him0.err + fabs(an/nfact);
454 /* Calculate complex dilogarithm Li_2(z) in the fundamental region,
455 * which we take to be the intersection of the unit disk with the
456 * half-space x < MAGIC_SPLIT_VALUE. It turns out that 0.732 is a
457 * nice choice for MAGIC_SPLIT_VALUE since then points mapped out
458 * of the x > MAGIC_SPLIT_VALUE region and into another part of the
459 * unit disk are bounded in radius by MAGIC_SPLIT_VALUE itself.
461 * If |z| < 0.98 we use a direct series summation. Otherwise z is very
462 * near the unit circle, and the series_2 expansion is used; see above.
463 * Because the fundamental region is bounded away from z = 1, this
468 dilogc_fundamental(double r, double x, double y, gsl_sf_result * real_dl, gsl_sf_result * imag_dl)
471 return dilogc_series_3(r, x, y, real_dl, imag_dl);
473 return dilogc_series_2(r, x, y, real_dl, imag_dl);
475 return dilogc_series_1(r, x, y, real_dl, imag_dl);
479 /* Compute Li_2(z) for z in the unit disk, |z| < 1. If z is outside
480 * the fundamental region, which means that it is too close to z = 1,
481 * then it is reflected into the fundamental region using the identity
483 * Li2(z) = -Li2(1-z) + zeta(2) - ln(z) ln(1-z).
487 dilogc_unitdisk(double x, double y, gsl_sf_result * real_dl, gsl_sf_result * imag_dl)
489 static const double MAGIC_SPLIT_VALUE = 0.732;
490 static const double zeta2 = M_PI*M_PI/6.0;
491 const double r = hypot(x, y);
493 if(x > MAGIC_SPLIT_VALUE)
495 /* Reflect away from z = 1 if we are too close. The magic value
496 * insures that the reflected value of the radius satisfies the
497 * related inequality r_tmp < MAGIC_SPLIT_VALUE.
499 const double x_tmp = 1.0 - x;
500 const double y_tmp = - y;
501 const double r_tmp = hypot(x_tmp, y_tmp);
502 /* const double cos_theta_tmp = x_tmp/r_tmp; */
503 /* const double sin_theta_tmp = y_tmp/r_tmp; */
505 gsl_sf_result result_re_tmp;
506 gsl_sf_result result_im_tmp;
508 const int stat_dilog = dilogc_fundamental(r_tmp, x_tmp, y_tmp, &result_re_tmp, &result_im_tmp);
510 const double lnz = log(r); /* log(|z|) */
511 const double lnomz = log(r_tmp); /* log(|1-z|) */
512 const double argz = atan2(y, x); /* arg(z) assuming principal branch */
513 const double argomz = atan2(y_tmp, x_tmp); /* arg(1-z) */
514 real_dl->val = -result_re_tmp.val + zeta2 - lnz*lnomz + argz*argomz;
515 real_dl->err = result_re_tmp.err;
516 real_dl->err += 2.0 * GSL_DBL_EPSILON * (zeta2 + fabs(lnz*lnomz) + fabs(argz*argomz));
517 imag_dl->val = -result_im_tmp.val - argz*lnomz - argomz*lnz;
518 imag_dl->err = result_im_tmp.err;
519 imag_dl->err += 2.0 * GSL_DBL_EPSILON * (fabs(argz*lnomz) + fabs(argomz*lnz));
525 return dilogc_fundamental(r, x, y, real_dl, imag_dl);
531 /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
535 gsl_sf_dilog_e(const double x, gsl_sf_result * result)
538 return dilog_xge0(x, result);
541 gsl_sf_result d1, d2;
542 int stat_d1 = dilog_xge0( -x, &d1);
543 int stat_d2 = dilog_xge0(x*x, &d2);
544 result->val = -d1.val + 0.5 * d2.val;
545 result->err = d1.err + 0.5 * d2.err;
546 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
547 return GSL_ERROR_SELECT_2(stat_d1, stat_d2);
553 gsl_sf_complex_dilog_xy_e(
556 gsl_sf_result * real_dl,
557 gsl_sf_result * imag_dl
560 const double zeta2 = M_PI*M_PI/6.0;
561 const double r2 = x*x + y*y;
567 imag_dl->val = -M_PI * log(x);
568 imag_dl->err = 2.0 * GSL_DBL_EPSILON * fabs(imag_dl->val);
575 return gsl_sf_dilog_e(x, real_dl);
577 else if(fabs(r2 - 1.0) < GSL_DBL_EPSILON)
579 /* Lewin A.2.4.1 and A.2.4.2 */
581 const double theta = atan2(y, x);
582 const double term1 = theta*theta/4.0;
583 const double term2 = M_PI*fabs(theta)/2.0;
584 real_dl->val = zeta2 + term1 - term2;
585 real_dl->err = 2.0 * GSL_DBL_EPSILON * (zeta2 + term1 + term2);
586 return gsl_sf_clausen_e(theta, imag_dl);
590 return dilogc_unitdisk(x, y, real_dl, imag_dl);
594 /* Reduce argument to unit disk. */
595 const double r = sqrt(r2);
596 const double x_tmp = x/r2;
597 const double y_tmp = -y/r2;
598 /* const double r_tmp = 1.0/r; */
599 gsl_sf_result result_re_tmp, result_im_tmp;
601 const int stat_dilog =
602 dilogc_unitdisk(x_tmp, y_tmp, &result_re_tmp, &result_im_tmp);
604 /* Unwind the inversion.
606 * Li_2(z) + Li_2(1/z) = -zeta(2) - 1/2 ln(-z)^2
608 const double theta = atan2(y, x);
609 const double theta_abs = fabs(theta);
610 const double theta_sgn = ( theta < 0.0 ? -1.0 : 1.0 );
611 const double ln_minusz_re = log(r);
612 const double ln_minusz_im = theta_sgn * (theta_abs - M_PI);
613 const double lmz2_re = ln_minusz_re*ln_minusz_re - ln_minusz_im*ln_minusz_im;
614 const double lmz2_im = 2.0*ln_minusz_re*ln_minusz_im;
615 real_dl->val = -result_re_tmp.val - 0.5 * lmz2_re - zeta2;
616 real_dl->err = result_re_tmp.err + 2.0*GSL_DBL_EPSILON*(0.5 * fabs(lmz2_re) + zeta2);
617 imag_dl->val = -result_im_tmp.val - 0.5 * lmz2_im;
618 imag_dl->err = result_im_tmp.err + 2.0*GSL_DBL_EPSILON*fabs(lmz2_im);
625 gsl_sf_complex_dilog_e(
628 gsl_sf_result * real_dl,
629 gsl_sf_result * imag_dl
632 const double cos_theta = cos(theta);
633 const double sin_theta = sin(theta);
634 const double x = r * cos_theta;
635 const double y = r * sin_theta;
636 return gsl_sf_complex_dilog_xy_e(x, y, real_dl, imag_dl);
641 gsl_sf_complex_spence_xy_e(
644 gsl_sf_result * real_sp,
645 gsl_sf_result * imag_sp
648 const double oms_x = 1.0 - x;
649 const double oms_y = - y;
650 return gsl_sf_complex_dilog_xy_e(oms_x, oms_y, real_sp, imag_sp);
655 /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
659 double gsl_sf_dilog(const double x)
661 EVAL_RESULT(gsl_sf_dilog_e(x, &result));