<s> from copperhead import * <EOL> @ cu <EOL> def z_square ( z ) : <EOL> real , imag = z <EOL> return real * real - imag * imag , <NUM_LIT:2> * real * imag <EOL> @ cu <EOL> def z_magnitude ( z ) : <EOL> real , imag = z <EOL> return sqrt ( real * real + imag * imag ) <EOL> @ cu <EOL> def z_add ( ( z0r , z0i ) , ( z1r , z1i ) ) : <EOL> return z0r + z1r , z0i + z1i <EOL> @ cu <EOL> def mandelbrot_iteration ( z0 , z , i , m , t ) : <EOL> z = z_add ( z_square ( z ) , z0 ) <EOL> escaped = z_magnitude ( z ) > m <EOL> converged = i > t <EOL> done = escaped or converged <EOL> if not done : <EOL> return mandelbrot_iteration ( z0 , z , i + <NUM_LIT:1> , m , t ) <EOL> else : <EOL> return i <EOL> @ cu <EOL> def mandelbrot ( lb , scale , ( x , y ) , m , t ) : <EOL> def mandelbrot_el ( zi ) : <EOL> return mandelbrot_iteration ( zi , zi , <NUM_LIT:0> , m , t ) <EOL> def index ( i ) : <EOL> scale_x , scale_y = scale <EOL> lb_x , lb_y = lb <EOL> return float32 ( i % x ) * scale_x + lb_x , float32 ( i / x ) * scale_y + lb_y <EOL> two_d_points = map ( index , range ( x * y ) ) <EOL> return map ( mandelbrot_el , two_d_points ) <EOL> lb = ( np . float32 ( - <NUM_LIT> ) , np . float32 ( - <NUM_LIT> ) ) <EOL> ub = ( np . float32 ( <NUM_LIT> ) , np . float32 ( <NUM_LIT> ) ) <EOL> x , y = <NUM_LIT:1000> , <NUM_LIT:1000> <EOL> scale = ( ( ub [ <NUM_LIT:0> ] - lb [ <NUM_LIT:0> ] ) / np . float32 ( x ) , ( ub [ <NUM_LIT:0> ] - lb [ <NUM_LIT:0> ] ) / np . float32 ( y ) ) <EOL> max_iterations = <NUM_LIT:100> <EOL> diverge_threshold = np . float32 ( <NUM_LIT> ) <EOL> print ( "<STR_LIT>" ) <EOL> result = mandelbrot ( lb , scale , ( x , y ) , diverge_threshold , max_iterations ) <EOL> print ( "<STR_LIT>" ) <EOL> import matplotlib . pyplot as plt <EOL> im_result = to_numpy ( result ) . reshape ( [ x , y ] ) <EOL> plt . imshow ( im_result ) <EOL> plt . show ( ) </s>
