Skip to content

Instantly share code, notes, and snippets.

@dpiponi
Created January 31, 2017 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpiponi/aa7b713da7c822c04193095397b70bb4 to your computer and use it in GitHub Desktop.
Save dpiponi/aa7b713da7c822c04193095397b70bb4 to your computer and use it in GitHub Desktop.
Compute formal power series for functional square root of sin function
import Data.Ratio
(^+) a b = zipWith (+) a b
(^-) a b = zipWith (-) a b
(a : as) `convolve` (b : bs) = (a * b) :
((map (a *) bs) ^+ (as `convolve` (b : bs)))
compose (f : fs) (0 : gs) = f : (gs `convolve` (compose fs (0 : gs)))
integrate x = 0 : zipWith (/) x (map fromInteger [1..])
sin' = integrate cos'
cos' = let _ : cos'' = map negate (integrate sin') in 1 : cos''
delta (g : gs) h = let g' = delta gs h
in (0 : ((1 : h) `convolve` g')) ^+ gs
fsqrt (0 : 1 : fs) =
let gs = map (/ 2) $ fs ^- (0 : gs `convolve`
((0 : delta gs gs) ^+
((2 : gs) `convolve` (gs `compose` g))))
g = 0 : 1 : gs
in g
type Q = Ratio Integer
type R = Double
main = mapM_ print $ zip [0..] (fsqrt sin' :: [Q])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment