Changeset 328

Show
Ignore:
Timestamp:
04/28/09 16:15:04 (15 years ago)
Author:
dedecius
Message:

Implementation of the digamma function.

Location:
bdm
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • bdm/itpp_ext.cpp

    r278 r328  
    298298                return std::string(tmp); 
    299299        }; 
     300 
     301// digamma 
     302// copied from C. Bonds' source 
     303#include <math.h> 
     304#define el 0.5772156649015329 
     305 
     306double psi(double x) { 
     307    double s,ps,xa,x2; 
     308    int n,k; 
     309    static double a[] = { 
     310        -0.8333333333333e-01, 
     311         0.83333333333333333e-02, 
     312        -0.39682539682539683e-02, 
     313         0.41666666666666667e-02, 
     314        -0.75757575757575758e-02, 
     315         0.21092796092796093e-01, 
     316        -0.83333333333333333e-01, 
     317         0.4432598039215686}; 
     318 
     319    xa = fabs(x); 
     320    s = 0.0; 
     321    if ((x == (int)x) && (x <= 0.0)) { 
     322        ps = 1e308; 
     323        return ps; 
     324    } 
     325    if (xa == (int)xa) { 
     326        n = xa; 
     327        for (k=1;k<n;k++) { 
     328            s += 1.0/k; 
     329        } 
     330        ps =  s-el; 
     331    } 
     332    else if ((xa+0.5) == ((int)(xa+0.5))) { 
     333        n = xa-0.5; 
     334        for (k=1;k<=n;k++) { 
     335            s += 1.0/(2.0*k-1.0); 
     336        } 
     337        ps = 2.0*s-el-1.386294361119891; 
     338    } 
     339    else { 
     340        if (xa < 10.0) { 
     341            n = 10-(int)xa; 
     342            for (k=0;k<n;k++) { 
     343                s += 1.0/(xa+k); 
     344            } 
     345            xa += n; 
     346        } 
     347        x2 = 1.0/(xa*xa); 
     348        ps = log(xa)-0.5/xa+x2*(((((((a[7]*x2+a[6])*x2+a[5])*x2+ 
     349            a[4])*x2+a[3])*x2+a[2])*x2+a[1])*x2+a[0]); 
     350        ps -= s; 
     351    } 
     352    if (x < 0.0) 
     353        ps = ps - M_PI*std::cos(M_PI*x)/std::sin(M_PI*x)-1.0/x; 
     354        return ps; 
    300355} 
     356 
     357} 
  • bdm/itpp_ext.h

    r276 r328  
    8080        //! reimplementation of matlabs num2str 
    8181        std::string num2str(int i); 
     82 
     83        //! implementation of digamma (psi) function 
     84        double psi(double); 
    8285} 
    8386