Thiele's interpolation formula: Difference between revisions
Content added Content deleted
(Added Kotlin) |
(→{{header|Haskell}}: hlint, hindent + a couple of reductions) |
||
Line 507: | Line 507: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Caching of rho is automatic due to lazy lists. |
Caching of rho is automatic due to lazy lists. |
||
<lang haskell>thiele |
<lang haskell>thiele :: [Double] -> [Double] -> Double -> Double |
||
thiele xs ys = f rho1 (tail xs) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
where |
|||
⚫ | |||
⚫ | |||
let z_ = zipWith |
|||
in z_ (+) (tail r0) (z_ (/) (z_ (-) x xn) (z_ (-) r1 (tail r1))) : |
|||
⚫ | |||
⚫ | |||
⚫ | |||
invInterp :: (Double -> Double) -> [Double] -> Double -> Double |
|||
⚫ | |||
main :: IO () |
|||
⚫ | |||
main = |
|||
⚫ | |||
mapM_ |
|||
⚫ | |||
print |
|||
(z_ (+) (tail r0) |
|||
⚫ | |||
(z_ (/) (z_ (-) x xn) |
|||
, pi / 1.2345 * inv_cos (cos 1.2345) |
|||
, 7 * inv_tan (tan (pi / 7)) |
|||
⚫ | |||
] |
|||
where |
|||
⚫ | |||
[inv_sin, inv_cos, inv_tan] = |
|||
⚫ | |||
uncurry ((. div_pi) . invInterp) <$> |
|||
[(sin, (2, 31)), (cos, (2, 100)), (tan, (4, 100))] |
|||
⚫ | |||
-- N points taken uniformly from 0 to Pi/d |
|||
print $ pi/1.2345 * inv_cos (cos (1.2345)) |
|||
div_pi (d, n) = (* (pi / (d * n))) <$> [0 .. n]</lang> |
|||
⚫ | |||
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}} |
{{out}} |
||
<pre> |
<pre>3.141592653589795 |
||
3.141592653589791 |
|||
3.141592653589795 |
|||
3.1415926535897905</pre> |
|||
3.1415926535897802 |
|||
3.1415926535835275 |
|||
</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |