aboutsummaryrefslogtreecommitdiff
path: root/sem4/hpp/miniproject/naive.py
blob: 24e366de61c8b79e65da1b5a9553e23a3c495b05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import numpy as np
import matplotlib.pyplot as plt
import time

# c-mesh limits
limitre = ( -2, 1 )
limitim = ( -1.5, 1.5 )

def iota(c, T, l):
    """
    Implement the ι function used in mangelbrot
    Also devides by l

    :param c: Complex number from the c-mesh
    :param T: Mangelbrot threshold
    :param l: Iterations
    """

    z = 0
    for i in range(l):
        z = z*z + c

        # Check if we found or z
        if np.abs(z) > T:
            return (i / l, z)

    # If we did not find z, use l
    return (l / l, z)

def mangel(pre, pim, T, l, savez):
    """
    Calculate the mangelbrot image
    (pre, pim) discribes the image size. Use T and l to tune the mangelbrot
    This function uses the global variables limitre and limitim to determine
    the c-mesh range.

    :param pre: Number of real numbers used
    :param pim: Number of imaginary numbers
    :param T: Mangelbrot threshold
    :param l: Iterations
    :param savez: Return z as the second element of returned tuple
    """
    
    # Preallocate result array
    rs = np.zeros((pre, pim))
    z = np.empty((pre, pim), dtype=complex)

    # Calculate scaling variables
    sre = ( limitre[1] - limitre[0] ) / (pre-1)
    sim = ( limitim[1] - limitim[0] ) / (pim-1)

    # Loop all pixels
    for re in range(pre):
        for im in range(pim):
            # Calculate the complex number using the scalers
            c = limitre[0] + limitim[0] * 1j + sre * re + 1j * sim * im
            
            # Calculate the ι
            (rs[re,im], z[re, im]) = iota(c, T, l)

    if savez:
        return (rs, z)
    else:
        return (rs, None)