Last active
July 13, 2016 15:08
-
-
Save jkachmar/e4fb7b3722c41050c526fc91e67ade5d to your computer and use it in GitHub Desktop.
Taylor Series Expansion of e^x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Power series expansion of the exponential function | |
-- i.e. e^x = 1 + x + (x^2)/2! + ... (x^n)/n! | |
-- 'Cute' version | |
exp :: Double -> Double | |
exp x = sum $ take 10 $ map go [0..] | |
where fac = product . flip take [1..] | |
go n = x ^ n / fac n | |
-- Explanatory version | |
exp' :: Double -> Double | |
exp' x = sum (take 10 (map go [0..])) | |
where fac :: Int -> Double | |
fac n = product (take n [1..]) | |
go :: Int -> Double | |
go n = (x ^ n) / (fac n) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment