The R Project SVN R

Rev

Rev 11499 | Rev 29041 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 11499 Rev 13434
Line 1... Line 1...
1
/*
1
/*
2
 *  Mathlib : A C Library of Special Functions
2
 *  Mathlib : A C Library of Special Functions
3
 *  Copyright (C) 1998 Ross Ihaka
3
 *  Copyright (C) 1998 Ross Ihaka
-
 
4
 *  Copyright (C) 2000-2001 the R Development Core Team
4
 *
5
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  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
 *  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
8
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *  (at your option) any later version.
Line 46... Line 47...
46
 *
47
 *
47
 *    "dpsifn" computes a sequence of scaled derivatives of
48
 *    "dpsifn" computes a sequence of scaled derivatives of
48
 *    the psi function; i.e. for fixed x and m it computes
49
 *    the psi function; i.e. for fixed x and m it computes
49
 *    the m-member sequence
50
 *    the m-member sequence
50
 *
51
 *
51
 *		  ((-1)**(k+1)/gamma(k+1))*psi(k,x)
52
 *		  (-1)^(k+1) / gamma(k+1) * psi(k,x)
52
 *		     for k = n,...,n+m-1
53
 *		     for k = n,...,n+m-1
53
 *
54
 *
54
 *    where psi(k,x) is as defined above.   For kode=1, dpsifn
55
 *    where psi(k,x) is as defined above.   For kode=1, dpsifn
55
 *    returns the scaled derivatives as described.  kode=2 is
56
 *    returns the scaled derivatives as described.  kode=2 is
56
 *    operative only when k=0 and in that case dpsifn returns
57
 *    operative only when k=0 and in that case dpsifn returns
Line 66... Line 67...
66
 *
67
 *
67
 *	x     - argument, x > 0.
68
 *	x     - argument, x > 0.
68
 *
69
 *
69
 *	n     - first member of the sequence, 0 <= n <= 100
70
 *	n     - first member of the sequence, 0 <= n <= 100
70
 *		n == 0 gives ans(1) = -psi(x)	    for kode=1
71
 *		n == 0 gives ans(1) = -psi(x)	    for kode=1
71
 *					 -psi(x)+ln(x) for kode=2
72
 *				      -psi(x)+ln(x) for kode=2
72
 *
73
 *
73
 *	kode  - selection parameter
74
 *	kode  - selection parameter
74
 *		kode == 1 returns scaled derivatives of the
75
 *		kode == 1 returns scaled derivatives of the
75
 *		psi function.
76
 *		psi function.
76
 *		kode == 2 returns scaled derivatives of the
77
 *		kode == 2 returns scaled derivatives of the
Line 104... Line 105...
104
 *
105
 *
105
 *    The basic method of evaluation is the asymptotic expansion
106
 *    The basic method of evaluation is the asymptotic expansion
106
 *    for large x >= xmin followed by backward recursion on a two
107
 *    for large x >= xmin followed by backward recursion on a two
107
 *    term recursion relation
108
 *    term recursion relation
108
 *
109
 *
109
 *	     w(x+1) + x**(-n-1) = w(x).
110
 *	     w(x+1) + x^(-n-1) = w(x).
110
 *
111
 *
111
 *    this is supplemented by a series
112
 *    this is supplemented by a series
112
 *
113
 *
113
 *	     sum( (x+k)**(-n-1) , k=0,1,2,... )
114
 *	     sum( (x+k)^(-n-1) , k=0,1,2,... )
114
 *
115
 *
115
 *    which converges rapidly for large n. both xmin and the
116
 *    which converges rapidly for large n. both xmin and the
116
 *    number of terms of the series are calculated from the unit
117
 *    number of terms of the series are calculated from the unit
117
 *    roundoff of the machine environment.
118
 *    roundoff of the machine environment.
118
 *
119
 *
Line 135... Line 136...
135
 *    Routines called: d1mach, i1mach.
136
 *    Routines called: d1mach, i1mach.
136
 */
137
 */
137
 
138
 
138
#include "nmath.h"
139
#include "nmath.h"
139
 
140
 
140
	/* Bernoulli Numbers */
-
 
141
 
-
 
142
static double bvalues[] = {
-
 
143
     1.00000000000000000e+00,
-
 
144
    -5.00000000000000000e-01,
-
 
145
     1.66666666666666667e-01,
-
 
146
    -3.33333333333333333e-02,
-
 
147
     2.38095238095238095e-02,
-
 
148
    -3.33333333333333333e-02,
-
 
149
     7.57575757575757576e-02,
-
 
150
    -2.53113553113553114e-01,
-
 
151
     1.16666666666666667e+00,
-
 
152
    -7.09215686274509804e+00,
-
 
153
     5.49711779448621554e+01,
-
 
154
    -5.29124242424242424e+02,
-
 
155
     6.19212318840579710e+03,
-
 
156
    -8.65802531135531136e+04,
-
 
157
     1.42551716666666667e+06,
-
 
158
    -2.72982310678160920e+07,
-
 
159
     6.01580873900642368e+08,
-
 
160
    -1.51163157670921569e+10,
-
 
161
     4.29614643061166667e+11,
-
 
162
    -1.37116552050883328e+13,
-
 
163
     4.88332318973593167e+14,
-
 
164
    -1.92965793419400681e+16
-
 
165
};
-
 
166
 
-
 
167
static double *b = (double *)&bvalues -1;
-
 
168
static int nmax = 100;
-
 
169
 
-
 
170
static
141
static
171
void dpsifn(double x, int n, int kode, int m, double *ans, int *nz, int *ierr)
142
void dpsifn(double x, int n, int kode, int m, double *ans, int *nz, int *ierr)
172
{
143
{
-
 
144
    const double bvalues[] = {	/* Bernoulli Numbers */
-
 
145
	 1.00000000000000000e+00,
-
 
146
	-5.00000000000000000e-01,
-
 
147
	 1.66666666666666667e-01,
-
 
148
	-3.33333333333333333e-02,
-
 
149
	 2.38095238095238095e-02,
-
 
150
	-3.33333333333333333e-02,
-
 
151
	 7.57575757575757576e-02,
-
 
152
	-2.53113553113553114e-01,
-
 
153
	 1.16666666666666667e+00,
-
 
154
	-7.09215686274509804e+00,
-
 
155
	 5.49711779448621554e+01,
-
 
156
	-5.29124242424242424e+02,
-
 
157
	 6.19212318840579710e+03,
-
 
158
	-8.65802531135531136e+04,
-
 
159
	 1.42551716666666667e+06,
-
 
160
	-2.72982310678160920e+07,
-
 
161
	 6.01580873900642368e+08,
-
 
162
	-1.51163157670921569e+10,
-
 
163
	 4.29614643061166667e+11,
-
 
164
	-1.37116552050883328e+13,
-
 
165
	 4.88332318973593167e+14,
-
 
166
	-1.92965793419400681e+16
-
 
167
    };
-
 
168
    const double *b = (double *)&bvalues -1; /* ==> b[1] = bvalues[0], etc */ 
-
 
169
    const int nmax = 100;
-
 
170
 
173
    int i, j, k, mm, mx, nn, np, nx, fn;
171
    int i, j, k, mm, mx, nn, np, nx, fn;
174
    double arg, den, elim, eps, fln, fx, rln, rxsq,
172
    double arg, den, elim, eps, fln, fx, rln, rxsq,
175
	r1m4, r1m5, s, slope, t, ta, tk, tol, tols, tss, tst,
173
	r1m4, r1m5, s, slope, t, ta, tk, tol, tols, tss, tst,
176
	tt, t1, t2, wdtol, xdmln, xdmy, xinc, xln, xm, xmin,
174
	tt, t1, t2, wdtol, xdmln, xdmy, xinc, xln, xm, xmin,
177
	xq, yint;
175
	xq, yint;
Line 213... Line 211...
213
	}
211
	}
214
	else {
212
	else {
215
	    if (x < wdtol) {
213
	    if (x < wdtol) {
216
		ans[1] = pow(x, -n-1.0);
214
		ans[1] = pow(x, -n-1.0);
217
		if (mm != 1) {
215
		if (mm != 1) {
218
		    k = 1;
-
 
219
		    for(i=2 ; i<=mm ; i++) {
216
		    for(i = 2, k = 1; i <= mm ; i++, k++)
220
			ans[k+1] = ans[k] / x;
217
			ans[k+1] = ans[k] / x;
221
			k = k+1;
-
 
222
		    }
-
 
223
		}
218
		}
224
		if (n == 0 && kode == 2)
219
		if (n == 0 && kode == 2)
225
		    ans[1] = ans[1] + xln;
220
		    ans[1] += xln;
226
		return;
221
		return;
227
	    }
222
	    }
228
 
223
 
229
	    /* compute xmin and the number of terms of the series,  fln+1 */
224
	    /* compute xmin and the number of terms of the series,  fln+1 */
230
 
225
 
Line 266... Line 261...
266
	    t2 = t + xdmln;
261
	    t2 = t + xdmln;
267
	    tk = fmax2(fabs(t), fmax2(fabs(t1), fabs(t2)));
262
	    tk = fmax2(fabs(t), fmax2(fabs(t1), fabs(t2)));
268
	    if (tk <= elim)
263
	    if (tk <= elim)
269
		goto L10;
264
		goto L10;
270
	}
265
	}
271
	nz = nz + 1;
266
	nz++;
272
	ans[mm] = 0.0;
267
	ans[mm] = 0.0;
273
	mm = mm - 1;
268
	mm--;
274
	if (mm == 0)
269
	if (mm == 0)
275
	    return;
270
	    return;
276
    }
271
    }
277
    nn = (int)fln + 1;
272
    nn = (int)fln + 1;
278
    np = n + 1;
273
    np = n + 1;
279
    t1 = (n + 1) * xln;
274
    t1 = (n + 1) * xln;
280
    t = exp(-t1);
275
    t = exp(-t1);
281
    s = t;
276
    s = t;
282
    den = x;
277
    den = x;
283
    for(i=1 ; i<=nn ; i++) {
278
    for(i=1 ; i<=nn ; i++) {
284
	den = den + 1.0;
279
	den += 1.;
285
	trm[i] = pow(den, (double)-np);
280
	trm[i] = pow(den, (double)-np);
286
	s = s + trm[i];
281
	s += trm[i];
287
    }
282
    }
288
    ans[1] = s;
283
    ans[1] = s;
289
    if (n == 0 && kode == 2)
284
    if (n == 0 && kode == 2)
290
	ans[1] = s + xln;
285
	ans[1] = s + xln;
291
 
286
 
292
    if (mm!=1) {
-
 
293
 
-
 
294
	/* generate higher derivatives,	 j > n */
287
    if (mm != 1) { /* generate higher derivatives, j > n */
295
 
288
 
296
	tol = wdtol / 5.0;
289
	tol = wdtol / 5.0;
297
	for(j=2 ; j<=mm ; j++) {
290
	for(j = 2; j <= mm; j++) {
298
	    t = t / x;
291
	    t = t / x;
299
	    s = t;
292
	    s = t;
300
	    tols = t * tol;
293
	    tols = t * tol;
301
	    den = x;
294
	    den = x;
302
	    for(i=1 ; i<=nn ; i++) {
295
	    for(i=1 ; i<=nn ; i++) {
303
		den = den + 1.0;
296
		den += 1.;
304
		trm[i] = trm[i] / den;
297
		trm[i] /= den;
305
		s = s + trm[i];
298
		s += trm[i];
306
		if (trm[i] < tols)
299
		if (trm[i] < tols)
307
		    break;
300
		    break;
308
	    }
301
	    }
309
	    ans[j] = s;
302
	    ans[j] = s;
310
	}
303
	}
Line 321... Line 314...
321
    ta = 0.5 * rxsq;
314
    ta = 0.5 * rxsq;
322
    t = (fn + 1) * ta;
315
    t = (fn + 1) * ta;
323
    s = t * b[3];
316
    s = t * b[3];
324
    if (fabs(s) >= tst) {
317
    if (fabs(s) >= tst) {
325
	tk = 2.0;
318
	tk = 2.0;
326
	for(k=4 ; k<=22 ; k++) {
319
	for(k = 4; k <= 22; k++) {
327
	    t = t * ((tk + fn + 1)/(tk + 1.0))*((tk + fn)/(tk + 2.0)) * rxsq;
320
	    t = t * ((tk + fn + 1)/(tk + 1.0))*((tk + fn)/(tk + 2.0)) * rxsq;
328
	    trm[k] = t * b[k];
321
	    trm[k] = t * b[k];
329
	    if (fabs(trm[k]) < tst)
322
	    if (fabs(trm[k]) < tst)
330
		break;
323
		break;
331
	    s = s + trm[k];
324
	    s += trm[k];
332
	    tk = tk + 2.0;
325
	    tk += 2.;
333
	}
326
	}
334
    }
327
    }
335
    s = (s + t1) * tss;
328
    s = (s + t1) * tss;
336
    if (xinc != 0.0) {
329
    if (xinc != 0.0) {
337
 
330
 
Line 349... Line 342...
349
		goto L20;
342
		goto L20;
350
	    xm = xinc - 1.0;
343
	    xm = xinc - 1.0;
351
	    fx = x + xm;
344
	    fx = x + xm;
352
 
345
 
353
	    /* this loop should not be changed. fx is accurate when x is small */
346
	    /* this loop should not be changed. fx is accurate when x is small */
354
 
-
 
355
	    for(i=1 ; i<=nx ; i++) {
347
	    for(i = 1; i <= nx; i++) {
356
		trmr[i] = pow(fx, (double)-np);
348
		trmr[i] = pow(fx, (double)-np);
357
		s = s + trmr[i];
349
		s += trmr[i];
358
		xm = xm - 1.0;
350
		xm -= 1.;
359
		fx = x + xm;
351
		fx = x + xm;
360
	    }
352
	    }
361
	}
353
	}
362
    }
354
    }
363
    ans[mm] = s;
355
    ans[mm] = s;
364
    if (fn == 0)
356
    if (fn == 0)
365
	goto L30;
357
	goto L30;
366
 
358
 
367
    /* generate lower derivatives,  j < n+mm-1 */
359
    /* generate lower derivatives,  j < n+mm-1 */
368
 
360
 
369
    if (mm == 1)
-
 
370
	return;
-
 
371
 
-
 
372
    for(j=2 ; j<=mm ; j++) {
361
    for(j = 2; j <= mm; j++) {
373
	fn = fn - 1;
362
	fn--;
374
	tss = tss * xdmy;
363
	tss *= xdmy;
375
	t1 = tt;
364
	t1 = tt;
376
	if (fn!=0)
365
	if (fn!=0)
377
	    t1 = tt + 1.0 / fn;
366
	    t1 = tt + 1.0 / fn;
378
	t = (fn + 1) * ta;
367
	t = (fn + 1) * ta;
379
	s = t * b[3];
368
	s = t * b[3];
Line 381... Line 370...
381
	    tk = 4 + fn;
370
	    tk = 4 + fn;
382
	    for(k=4 ; k<=22 ; k++) {
371
	    for(k=4 ; k<=22 ; k++) {
383
		trm[k] = trm[k] * (fn + 1) / tk;
372
		trm[k] = trm[k] * (fn + 1) / tk;
384
		if (fabs(trm[k]) < tst)
373
		if (fabs(trm[k]) < tst)
385
		    break;
374
		    break;
386
		s = s + trm[k];
375
		s += trm[k];
387
		tk = tk + 2.0;
376
		tk += 2.;
388
	    }
377
	    }
389
	}
378
	}
390
	s = (s + t1) * tss;
379
	s = (s + t1) * tss;
391
	if (xinc != 0.0) {
380
	if (xinc != 0.0) {
392
	    if (fn == 0)
381
	    if (fn == 0)
393
		goto L20;
382
		goto L20;
394
	    xm = xinc - 1.0;
383
	    xm = xinc - 1.0;
395
	    fx = x + xm;
384
	    fx = x + xm;
396
	    for(i=1 ; i<=nx ; i++) {
385
	    for(i=1 ; i<=nx ; i++) {
397
		trmr[i] = trmr[i] * fx;
386
		trmr[i] = trmr[i] * fx;
398
		s = s + trmr[i];
387
		s += trmr[i];
399
		xm = xm - 1.0;
388
		xm -= 1.;
400
		fx = x + xm;
389
		fx = x + xm;
401
	    }
390
	    }
402
	}
391
	}
403
	mx = mm - j + 1;
392
	mx = mm - j + 1;
404
	ans[mx] = s;
393
	ans[mx] = s;
405
	if (fn == 0)
394
	if (fn == 0)
406
	    goto L30;
395
	    goto L30;
407
    }
396
    }
408
    return;
397
    return;
409
 
398
 
-
 
399
  L20:	
410
  L20:	for(i=1 ; i<=nx ; i++)
400
    for(i = 1; i <= nx; i++)
411
      s = s + 1.0 / (x + nx - i);
401
        s += 1. / (x + nx - i);
412
 
402
    
-
 
403
  L30:
413
  L30:	if (kode!=2)
404
    if (kode!=2)
414
      ans[1] = s - xdmln;
405
	ans[1] = s - xdmln;
415
  else if (xdmy != x) {
406
    else if (xdmy != x) {
416
      xq = xdmy / x;
407
	xq = xdmy / x;
417
      ans[1] = s - log(xq);
408
	ans[1] = s - log(xq);
418
  }
409
    }
419
    return;
410
    return;
420
}
411
}
421
 
412
 
422
double digamma(double x)
413
double digamma(double x)
423
{
414
{