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 xs:: ys[Double] =-> f[Double] rho1-> (tailDouble xs)-> whereDouble
thiele xs ys = f rho1 (tail xs)
f _ [] _ = 1
where
f r@(r0:r1:r2:rs) (x:xs) v = r2 - r0 + (v-x) / f (tail r) xs v
f _ [] _ = 1
f r@(r0:r1:r2:rs) (x:xs) v = r2 - r0 + (v - x) / f (tail r) xs v
rho1 = (map ((!! 1) . (++ [0])) <$> rho)
rho = [0,0 ..] : [0,0 ..] : ys : rnext (tail rho) xs (tail xs) where
where
rnext _ _ [] = []
rnext r@(r0:r1:rs) x xn = let z_ = zipWith in
let z_ = zipWith
in z_ (+) (tail r0) (z_ (/) (z_ (-) x xn) (z_ (-) r1 (tail r1))) :
: rnext (tail r) x (tail xn)
 
-- invertedInverted interpolation function of f
rho1 = (map ((!!1).(++[0])) rho)
invInterp :: (Double -> Double) -> [Double] -> Double -> Double
inv_interpinvInterp f xs = thiele (map f <$> xs) xs
 
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
(z_ (+) (tail r0)
main = do print $ [ 3.21 * inv_sin (sin (pi / 3.21))
(z_ (/) (z_ (-) x xn)
(z_, (-)pi / 1.2345 * r1inv_cos (tailcos r1)))1.2345)
, 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
print $ pi/1.2345 * inv_cos (cos (1.2345))
print $ 7 * inv_tandiv_pi (tand, n) = (* (pi / 7(d * n))) <$> [0 .. n]</lang>
where
inv_sin = inv_interp sin $ div_pi 2 31
inv_cos = inv_interp cos $ div_pi 2 100
inv_tan = inv_interp tan $ div_pi 4 1000 -- because we can
-- uniformly take n points from 0 to Pi/d
div_pi d n = map (* (pi / (d * n))) [0..n]</lang>
{{out}}
<pre>3.141592653589795
3.141592653589791
3.141592653589795
3.1415926535897905</pre>
3.1415926535897802
3.1415926535835275
</pre>
 
=={{header|J}}==
9,655

edits