File: legnomial.c

package info (click to toggle)
fudgit 2.42-6
  • links: PTS
  • area: non-free
  • in suites: potato, woody
  • size: 2,468 kB
  • ctags: 2,375
  • sloc: ansic: 27,729; makefile: 793; yacc: 724; lex: 102; asm: 29; fortran: 15
file content (59 lines) | stat: -rw-r--r-- 1,025 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <math.h>
#include "fudgit.h"

/* returns the legendre nomial of degree n */
/* Uses the recurrence relation
 * P(i+1) = [(2i + 1) x P(i) - i P(i-1)] / (i + 1)
 * P(i) = [(2i - 1) x P(i-1) - (i-1) P(i-2) / i
 *		{ oddx	}		 prev_di		  di
 */

double leg(expr , expr );

  /* for a whole vector */
void vleg(VEC x, VEC y, expr data, expr order)
{
	int n = (int) *data;
	int i;

	for (i=1;i<=n;i++) {
		y[i] = leg(x+i, order);
	}
}

	/* for one element */
double leg(expr x, expr order)
{
	int inl = (int)*order;

	if (inl < 0) {
		Ft_matherror("%s: Order cannot be negative (%d).", "leg", inl);
	}
	else if (inl == 0) {
		return(1.0);
	}
	else if (inl == 1) {
		return(*x);
	}
	else {
		double p0 = 1.0;
		double p1 = *x;
		double di = 1.0;
		double twox = 2.0 * *x;
		double oddx = *x;
		double prev_di, pnow;
		int j;

		for (j=2;j<=inl;j++) {
			prev_di = di;
			di += 1.0;
			oddx += twox;
			/* recurrence relation */
			pnow = (oddx * p1 - prev_di * p0) / di;
			p0 = p1;
			p1 = pnow;
		}
		return (pnow);
	}
}