Thiele's interpolation formula: Difference between revisions
→{{header|Haskell}}: hlint, hindent + a couple of reductions
(Added Kotlin) |
(→{{header|Haskell}}: hlint, hindent + a couple of reductions) |
||
Line 507:
=={{header|Haskell}}==
Caching of rho is automatic due to lazy lists.
<lang haskell>thiele
thiele xs ys = f rho1 (tail xs)
f _ [] _ = 1▼
f r@(r0:r1:r2:rs) (x:xs) v = r2 - r0 + (v-x) / f (tail r) xs v▼
where
let z_ = zipWith
in z_ (+) (tail r0) (z_ (/) (z_ (-) x xn) (z_ (-) r1 (tail r1))) :
▲ rho1 = (map ((!!1).(++[0])) rho)
invInterp :: (Double -> Double) -> [Double] -> Double -> Double
main :: IO ()
▲ rho = [0,0..] : [0,0..] : ys : rnext (tail rho) xs (tail xs) where
main =
▲ rnext _ _ [] = []
mapM_
▲ rnext r@(r0:r1:rs) x xn = let z_ = zipWith in
print
, 7 * inv_tan (tan (pi / 7))
▲ : rnext (tail r) x (tail xn)
]
where
▲-- inverted interpolation function of f
[inv_sin, inv_cos, inv_tan] =
▲inv_interp f xs = thiele (map f xs) xs
uncurry ((. div_pi) . invInterp) <$>
[(sin, (2, 31)), (cos, (2, 100)), (tan, (4, 100))]
▲main = do print $ 3.21 * inv_sin (sin (pi / 3.21))
-- N points taken uniformly from 0 to Pi/d
▲ where
{{out}}
<pre>3.141592653589795
3.141592653589791
3.1415926535897905</pre>
=={{header|J}}==
|