Mandelbrot set: Difference between revisions

Content added Content deleted
m (→‎Icon and Unicon: header simplification)
(change base cases of iterations to 0, for simplicity and consistency)
Line 16: Line 16:
# Compute the length of an orbit. #
# Compute the length of an orbit. #
PROC iterate = (COMPL z0) INT:
PROC iterate = (COMPL z0) INT:
BEGIN COMPL z := z0, INT iter := 1;
BEGIN COMPL z := 0, INT iter := 1;
WHILE (iter +:= 1) < max iter # not converged # AND ABS z < 2 # not diverged #
WHILE (iter +:= 1) < max iter # not converged # AND ABS z < 2 # not diverged #
DO z := z * z + z0
DO z := z * z + z0
Line 220: Line 220:
cmplx c = x0 + (y0-d*y)*I
cmplx c = x0 + (y0-d*y)*I
repeat(w):
repeat(w):
cmplx w = c
cmplx w = 0
for i=0; i < max_i && cabs(w) < outside; ++i
for i=0; i < max_i && cabs(w) < outside; ++i
w = w*w + c
w = w*w + c
Line 609: Line 609:
(defn mandelbrot? [z]
(defn mandelbrot? [z]
(loop [c 1
(loop [c 1
m (iterate #(+ z (* % %)) z)]
m (iterate #(+ z (* % %)) 0)]
(if (and (> 20 c)
(if (and (> 20 c)
(< (abs (first m)) 2) )
(< (abs (first m)) 2) )
Line 820: Line 820:
set pm3d map
set pm3d map
set size square
set size square
splot [-2 : 2] [-2 : 2] mandelbrot (complex (x, y), complex (x, y), 0) notitle</lang>
splot [-2 : 2] [-2 : 2] mandelbrot (complex (0, 0), complex (x, y), 0) notitle</lang>
Output:
Output:


Line 829: Line 829:
<lang haskell>import Data.Complex
<lang haskell>import Data.Complex


mandelbrot a = iterate (\z -> z^2 + a) a !! 50
mandelbrot a = iterate (\z -> z^2 + a) 0 !! 50


main = mapM_ putStrLn [[if magnitude (mandelbrot (x :+ y)) < 2 then '*' else ' '
main = mapM_ putStrLn [[if magnitude (mandelbrot (x :+ y)) < 2 then '*' else ' '
Line 1,805: Line 1,805:




def mandelbrot(a): return reduce(lambda z, _: z*z + a, range(50), a)
def mandelbrot(a): return reduce(lambda z, _: z*z + a, range(50), 0)
def step(start, step, iterations): return (start + (i * step) for i in range(iterations))
def step(start, step, iterations): return (start + (i * step) for i in range(iterations))


Line 1,847: Line 1,847:


def mandelbrot(a)
def mandelbrot(a)
Array.new(50).inject(a) { |z,c| z*z + a }
Array.new(50).inject(0) { |z,c| z*z + a }
end
end


Line 1,915: Line 1,915:
(or (= n 0)
(or (= n 0)
(*inside? z-0 (+ (* z z) z-0) (- n 1)))))
(*inside? z-0 (+ (* z z) z-0) (- n 1)))))
(*inside? z z n))
(*inside? z 0 n))


(define (boolean->integer b)
(define (boolean->integer b)