Skip to content

Instantly share code, notes, and snippets.

@crdueck
Created December 26, 2012 00:29
Show Gist options
  • Save crdueck/4376765 to your computer and use it in GitHub Desktop.
Save crdueck/4376765 to your computer and use it in GitHub Desktop.
Parallel Mandlebrot set generator using Repa
import Data.Array.Repa
data Complex a = Complex { re, im :: !a }
instance Num a => Num (Complex a) where
(Complex a b) + (Complex c d) = Complex (a + c) (b + d)
(Complex a b) - (Complex c d) = Complex (a - c) (b - d)
(Complex a b) * (Complex c d) = Complex (a*c - b*d) (a*d + b*c)
abs _ = undefined
signum _ = undefined
fromInteger i = Complex i 0
mandlebrot :: Num a => Complex a -> Maybe Integer
mandlebrot c = go c 0
where go z@(Complex a b) iter
| iter >= maxIter = Nothing
| abs a > 2 || abs b > 2 = Just iter
| otherwise = go (z^2 + c) (iter + 1)
maxIter = 255
genMandlebrot :: Array D DIM2 Integer
genMandlebrot = fromFunction DIM2 (mandlebrot . func)
where func sh = case listOfShape sh of [a, b] -> Complex a b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment