-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhermite.cpp
146 lines (110 loc) · 1.89 KB
/
hermite.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//-----------------------------------------------------------------------------
//
// Hermite polynomial
//
// Input: tos-2 x (can be a symbol or expr)
//
// tos-1 n
//
// Output: Result on stack
//
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include "defs.h"
void
hermite(void)
{
save();
yyhermite();
restore();
}
// uses the recurrence relation H(x,n+1)=2*x*H(x,n)-2*n*H(x,n-1)
#define X p1
#define N p2
#define Y p3
#define Y1 p4
#define Y0 p5
void
yyhermite(void)
{
int n;
N = pop();
X = pop();
push(N);
n = pop_integer();
if (n < 0) {
push_symbol(HERMITE);
push(X);
push(N);
list(3);
return;
}
if (issymbol(X))
yyhermite2(n);
else {
Y = X; // do this when X is an expr
X = symbol(SECRETX);
yyhermite2(n);
X = Y;
push(symbol(SECRETX));
push(X);
subst();
eval();
}
}
void
yyhermite2(int n)
{
int i;
push_integer(1);
push_integer(0);
Y1 = pop();
for (i = 0; i < n; i++) {
Y0 = Y1;
Y1 = pop();
push(X);
push(Y1);
multiply();
push_integer(i);
push(Y0);
multiply();
subtract();
push_integer(2);
multiply();
}
}
#if SELFTEST
static const char *s[] = {
"hermite(x,n)",
"hermite(x,n)",
"hermite(x,0)-1",
"0",
"hermite(x,1)-2*x",
"0",
"hermite(x,2)-(4*x^2-2)",
"0",
"hermite(x,3)-(8*x^3-12*x)",
"0",
"hermite(x,4)-(16*x^4-48*x^2+12)",
"0",
"hermite(x,5)-(32*x^5-160*x^3+120*x)",
"0",
"hermite(x,6)-(64*x^6-480*x^4+720*x^2-120)",
"0",
"hermite(x,7)-(128*x^7-1344*x^5+3360*x^3-1680*x)",
"0",
"hermite(x,8)-(256*x^8-3584*x^6+13440*x^4-13440*x^2+1680)",
"0",
"hermite(x,9)-(512*x^9-9216*x^7+48384*x^5-80640*x^3+30240*x)",
"0",
"hermite(x,10)-(1024*x^10-23040*x^8+161280*x^6-403200*x^4+302400*x^2-30240)",
"0",
"hermite(a-b,10)-eval(subst(a-b,x,hermite(x,10)))",
"0",
};
void
test_hermite(void)
{
test(__FILE__, s, sizeof s / sizeof (char *));
}
#endif