The R Project SVN R

Rev

Rev 25962 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 25962 Rev 31434
Line 329... Line 329...
329
void spline_coef(int *method, int *n, double *x, double *y,
329
void spline_coef(int *method, int *n, double *x, double *y,
330
		 double *b, double *c, double *d, double *e)
330
		 double *b, double *c, double *d, double *e)
331
{
331
{
332
    switch(*method) {
332
    switch(*method) {
333
    case 1:
333
    case 1:
334
	periodic_spline(*n, x, y, b, c, d, e);
334
	periodic_spline(*n, x, y, b, c, d, e);	break;
335
	break;
-
 
336
 
335
 
337
    case 2:
336
    case 2:
338
	natural_spline(*n, x, y, b, c, d);
337
	natural_spline(*n, x, y, b, c, d);	break;
339
	break;
-
 
340
 
338
 
341
    case 3:
339
    case 3:
342
	fmm_spline(*n, x, y, b, c, d);
340
	fmm_spline(*n, x, y, b, c, d);	break;
343
	break;
-
 
344
    }
341
    }
345
}
342
}
346
 
343
 
347
void spline_eval(int *method, int *nu, double *u, double *v,
344
void spline_eval(int *method, int *nu, double *u, double *v,
348
		 int *n, double *x, double *y, double *b, double *c, double *d)
345
		 int *n, double *x, double *y, double *b, double *c, double *d)
349
{
346
{
-
 
347
/* Evaluate  v[l] := spline(u[l], ...),  l = 1,..,nu */
-
 
348
 
350
    int i, j, k, l;
349
    int i, j, k, l;
351
    double ul, dx, tmp;
350
    double ul, dx, tmp;
352
 
351
 
353
    u--; v--;
-
 
354
    x--; y--;
-
 
355
    b--; c--; d--;
-
 
356
 
-
 
357
    if(*method == 1) {
352
    if(*method == 1 && *n > 1) {
358
	dx = x[*n] - x[1];
353
	dx = x[*n - 1] - x[0];
359
	for( l=1 ; l<=*nu ; l++) {
354
	for(l = 0; l < *nu; l++) {
360
	    v[l] = fmod(u[l]-x[1], dx);
355
	    v[l] = fmod(u[l]-x[0], dx);
361
	    if(v[l] < 0.0) v[l] += dx;
356
	    if(v[l] < 0.0) v[l] += dx;
362
	    v[l] = v[l] + x[1];
357
	    v[l] += x[0];
363
	}
358
	}
364
    }
359
    }
365
    else {
360
    else {
366
	for( l=1 ; l<=*nu ; l++)
361
	for(l =0; l < *nu; l++)
367
	    v[l] = u[l];
362
	    v[l] = u[l];
368
    }
363
    }
369
 
364
 
370
    i = 1;
365
    i = 0;
371
    for( l=1 ; l<=*nu ; l++) {
366
    for(l = 0; l < *nu; l++) {
372
	ul = v[l];
367
	ul = v[l];
373
	if(ul < x[i] || x[i+1] < ul) {
368
	if(ul < x[i] || x[i+1] < ul) {
374
	    i = 1;
369
	    i = 0;
375
	    j = *n + 1;
370
	    j = *n;
376
	    do {
371
	    do {
377
		k = (i+j)/2;
372
		k = (i+j)/2;
378
		if(ul < x[k]) j = k;
373
		if(ul < x[k]) j = k;
379
		else i = k;
374
		else i = k;
380
	    }
375
	    }
381
	    while(j > i+1);
376
	    while(j > i+1);
382
	}
377
	}
383
	dx = ul - x[i];
378
	dx = ul - x[i];
384
	/* for natural splines extrapolate linearly left */
379
	/* for natural splines extrapolate linearly left */
385
	tmp = d[i];
-
 
386
	if(*method == 2 && ul < x[1]) tmp = 0.0;
380
	tmp = (*method == 2 && ul < x[0]) ? 0.0 : d[i];
-
 
381
 
387
	v[l] = y[i] + dx*(b[i] + dx*(c[i] + dx*tmp));
382
	v[l] = y[i] + dx*(b[i] + dx*(c[i] + dx*tmp));
388
    }
383
    }
389
}
384
}