Advertisement
wandrake

Untitled

May 12th, 2012
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.32 KB | None | 0 0
  1. open System
  2. open System.Diagnostics
  3.  
  4. type complex = C of float*float
  5.  
  6. let check (n: int) = true
  7.  
  8. let sum (a: complex) (b: complex) =
  9.     match a, b with
  10.         | C(x1, y1), C(x2, y2) -> C(x1+x2, y1+y2)
  11.  
  12. let mult (a: complex) (b: complex) =
  13.     match a, b with
  14.         | C(x1, y1), C(x2, y2) -> C(x1*x2-y1*y2, x1*y2+x2*y1)
  15.    
  16. let exp (e: float) =
  17.     C(cos e, -sin e)
  18.  
  19. let opp (c: complex) =
  20.     match c with
  21.         | C(a, b) -> C(-a, -b)
  22.  
  23. let rec transform (x: complex array) =
  24.     if not (check x.Length) then failwith "Error, input size invalid"
  25.    
  26.     if x.Length = 1 then x
  27.     else
  28.         let even = Array.init (x.Length/2) (fun i -> x.[2*i])
  29.         let odd = Array.init (x.Length/2) (fun i -> x.[2*i+1])
  30.         let es: complex array = transform even
  31.         let os: complex array = transform odd
  32.         let ret: complex array = Array.zeroCreate x.Length
  33.        
  34.         for i in 0..(x.Length/2)-1 do
  35.             let e = 2. * Math.PI * float i / float x.Length
  36.             ret.[i] <- sum (es.[i]) (mult (exp e) (os.[i]))
  37.             ret.[i+(x.Length/2)] <- sum (es.[i]) (mult (opp (exp e)) (os.[i]))
  38.            
  39.         ret
  40.  
  41. let t = new Stopwatch()
  42. t.Start()
  43. let x = transform [|C(1., 0.); C(1., 0.); C(1., 0.); C(1., 0.) |]
  44. printfn "tempo di esecuzione: %i" t.ElapsedMilliseconds
  45. printfn "%A" x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement