Created
December 26, 2012 00:29
-
-
Save crdueck/4376765 to your computer and use it in GitHub Desktop.
Parallel Mandlebrot set generator using Repa
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
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