home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / specfunc / lambert.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-18  |  6.2 KB  |  231 lines

  1. /* specfunc/lambert.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 Gerard Jungman
  4.  *
  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 2 of the License, or (at
  8.  * your option) any later version.
  9.  *
  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.
  14.  *
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. /* Author:  G. Jungman */
  21.  
  22. #include <config.h>
  23. #include <math.h>
  24. #include <gsl/gsl_math.h>
  25. #include <gsl/gsl_errno.h>
  26. #include <gsl/gsl_sf_lambert.h>
  27.  
  28. /* Started with code donated by K. Briggs; added
  29.  * error estimates, GSL foo, and minor tweaks.
  30.  * Some Lambert-ology from
  31.  *  [Corless, Gonnet, Hare, and Jeffrey, "On Lambert's W Function".]
  32.  */
  33.  
  34.  
  35. /* Halley iteration (eqn. 5.12, Corless et al) */
  36. static int
  37. halley_iteration(
  38.   double x,
  39.   double w_initial,
  40.   unsigned int max_iters,
  41.   gsl_sf_result * result
  42.   )
  43. {
  44.   double w = w_initial;
  45.   unsigned int i;
  46.  
  47.   for(i=0; i<max_iters; i++) {
  48.     double tol;
  49.     const double e = exp(w);
  50.     const double p = w + 1.0;
  51.     double t = w*e - x;
  52.     /* printf("FOO: %20.16g  %20.16g\n", w, t); */
  53.  
  54.     if (w > 0) {
  55.       t = (t/p)/e;  /* Newton iteration */
  56.     } else {
  57.       t /= e*p - 0.5*(p + 1.0)*t/p;  /* Halley iteration */
  58.     };
  59.  
  60.     w -= t;
  61.  
  62.     tol = GSL_DBL_EPSILON * GSL_MAX_DBL(fabs(w), 1.0/(fabs(p)*e));
  63.  
  64.     if(fabs(t) < tol)
  65.     {
  66.       result->val = w;
  67.       result->err = 2.0*tol;
  68.       return GSL_SUCCESS;
  69.     }
  70.   }
  71.  
  72.   /* should never get here */
  73.   result->val = w;
  74.   result->err = fabs(w);
  75.   return GSL_EMAXITER;
  76. }
  77.  
  78.  
  79. /* series which appears for q near zero;
  80.  * only the argument is different for the different branches
  81.  */
  82. static double
  83. series_eval(double r)
  84. {
  85.   static const double c[12] = {
  86.     -1.0,
  87.      2.331643981597124203363536062168,
  88.     -1.812187885639363490240191647568,
  89.      1.936631114492359755363277457668,
  90.     -2.353551201881614516821543561516,
  91.      3.066858901050631912893148922704,
  92.     -4.175335600258177138854984177460,
  93.      5.858023729874774148815053846119,
  94.     -8.401032217523977370984161688514,
  95.      12.250753501314460424,
  96.     -18.100697012472442755,
  97.      27.029044799010561650
  98.   };
  99.   const double t_8 = c[8] + r*(c[9] + r*(c[10] + r*c[11]));
  100.   const double t_5 = c[5] + r*(c[6] + r*(c[7]  + r*t_8));
  101.   const double t_1 = c[1] + r*(c[2] + r*(c[3]  + r*(c[4] + r*t_5)));
  102.   return c[0] + r*t_1;
  103. }
  104.  
  105.  
  106. /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
  107.  
  108. int
  109. gsl_sf_lambert_W0_e(double x, gsl_sf_result * result)
  110. {
  111.   const double one_over_E = 1.0/M_E;
  112.   const double q = x + one_over_E;
  113.  
  114.   if(x == 0.0) {
  115.     result->val = 0.0;
  116.     result->err = 0.0;
  117.     return GSL_SUCCESS;
  118.   }
  119.   else if(q < 0.0) {
  120.     /* Strictly speaking this is an error. But because of the
  121.      * arithmetic operation connecting x and q, I am a little
  122.      * lenient in case of some epsilon overshoot. The following
  123.      * answer is quite accurate in that case. Anyway, we have
  124.      * to return GSL_EDOM.
  125.      */
  126.     result->val = -1.0;
  127.     result->err =  sqrt(-q);
  128.     return GSL_EDOM;
  129.   }
  130.   else if(q == 0.0) {
  131.     result->val = -1.0;
  132.     result->err =  GSL_DBL_EPSILON; /* cannot error is zero, maybe q == 0 by "accident" */
  133.     return GSL_SUCCESS;
  134.   }
  135.   else if(q < 1.0e-03) {
  136.     /* series near -1/E in sqrt(q) */
  137.     const double r = sqrt(q);
  138.     result->val = series_eval(r);
  139.     result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  140.     return GSL_SUCCESS;
  141.   }
  142.   else {
  143.     static const unsigned int MAX_ITERS = 10;
  144.     double w;
  145.  
  146.     if (x < 1.0) {
  147.       /* obtain initial approximation from series near x=0;
  148.        * no need for extra care, since the Halley iteration
  149.        * converges nicely on this branch
  150.        */
  151.       const double p = sqrt(2.0 * M_E * q);
  152.       w = -1.0 + p*(1.0 + p*(-1.0/3.0 + p*11.0/72.0)); 
  153.     }
  154.     else {
  155.       /* obtain initial approximation from rough asymptotic */
  156.       w = log(x);
  157.       if(x > 3.0) w -= log(w);
  158.     }
  159.  
  160.     return halley_iteration(x, w, MAX_ITERS, result);
  161.   }
  162. }
  163.  
  164.  
  165. int
  166. gsl_sf_lambert_Wm1_e(double x, gsl_sf_result * result)
  167. {
  168.   if(x > 0.0) {
  169.     return gsl_sf_lambert_W0_e(x, result);
  170.   }
  171.   else if(x == 0.0) {
  172.     result->val = 0.0;
  173.     result->err = 0.0;
  174.     return GSL_SUCCESS;
  175.   }
  176.   else {
  177.     static const unsigned int MAX_ITERS = 32;
  178.     const double one_over_E = 1.0/M_E;
  179.     const double q = x + one_over_E;
  180.     double w;
  181.  
  182.     if (q < 0.0) {
  183.       /* As in the W0 branch above, return some reasonable answer anyway. */
  184.       result->val = -1.0; 
  185.       result->err =  sqrt(-q);
  186.       return GSL_EDOM;
  187.     }
  188.  
  189.     if(x < -1.0e-6) {
  190.       /* Obtain initial approximation from series about q = 0,
  191.        * as long as we're not very close to x = 0.
  192.        * Use full series and try to bail out if q is too small,
  193.        * since the Halley iteration has bad convergence properties
  194.        * in finite arithmetic for q very small, because the
  195.        * increment alternates and p is near zero.
  196.        */
  197.       const double r = -sqrt(q);
  198.       w = series_eval(r);
  199.       if(q < 3.0e-3) {
  200.         /* this approximation is good enough */
  201.         result->val = w;
  202.         result->err = 5.0 * GSL_DBL_EPSILON * fabs(w);
  203.         return GSL_SUCCESS;
  204.       }
  205.     }
  206.     else {
  207.       /* Obtain initial approximation from asymptotic near zero. */
  208.       const double L_1 = log(-x);
  209.       const double L_2 = log(-L_1);
  210.       w = L_1 - L_2 + L_2/L_1;
  211.     }
  212.  
  213.     return halley_iteration(x, w, MAX_ITERS, result);
  214.   }
  215. }
  216.  
  217.  
  218. /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
  219.  
  220. #include "eval.h"
  221.  
  222. double gsl_sf_lambert_W0(double x)
  223. {
  224.   EVAL_RESULT(gsl_sf_lambert_W0_e(x, &result));
  225. }
  226.  
  227. double gsl_sf_lambert_Wm1(double x)
  228. {
  229.   EVAL_RESULT(gsl_sf_lambert_Wm1_e(x, &result));
  230. }
  231.