我们可以这样写:
hermite :: (Enum n, Num n) => n -> [n]
hermite x = s
where s@(_:ts) = 1 : x : zipWith3 (\hn2 hn1 n1 -> x*hn1 - n1*hn2) s ts [1..]
其中1 : x : ...的第一个元素是hermite的第一个元素(可以填写其他值)。
对于下一个,我们压缩原始值s(以H0 开头)、s 的尾部ts(以H1 开头)和索引(以2, 3, ...) 并执行操作x*hn1 - x*hn2(nh1 代表 Hn-1,nh2 代表Hn-2),所以我们每次都计算下一个元素。
x = 0.75 的前 11 个值是:
Prelude> take 11 (hermite 0.75)
[1.0,0.75,-0.4375,-1.828125,-5.859375e-2,7.2685546875,5.744384765625,-39.30303955078125,-69.68797302246094,262.1583366394043,823.8105096817017]
所以第一个值是 1,第二个是 x,第三个是 x*x-2,第四个是 x*x*x-2*x-3*x,以此类推。
话虽如此,如果我没记错的话,Hermite 多项式的递归公式是:
Hn(x) = 2×x×Hn-1(x)-2×(n-1)Hn -2(x)
而不是问题中引用的那个。
在这种情况下,公式如下:
hermite :: (Enum n, Num n) => n -> [n]
hermite x = s
where s@(_:ts) = 1 : 2 * x : zipWith3 helper s ts [1..]
helper hn2 hn1 n1 = 2 * (x * hn1 - n1 * hn2)
那么前11个值是:
Prelude> take 11 (hermite 0.75)
[1.0,1.5,0.25,-5.625,-9.9375,30.09375,144.515625,-144.3515625,-2239.74609375,-1049.994140625,38740.4384765625]
根据Wolfram article,哪个是正确的:
H0 = 1
H1 = 2*x
H2 = 4˙x2 - 2
H3 = 8˙x3 - 4˙x
H4 = 16˙x4 - 48˙x2 + 12
这与我们获得的值完全一致:
Prelude> let x = 0.75 in [1,2*x,4*x*x-2,8*x*x*x-4*x,16*x*x*x*x-48*x*x+12]
[1.0,1.5,0.25,0.375,-9.9375]