diff options
author | Julian T <julian@jtle.dk> | 2020-04-01 17:56:56 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-04-01 17:56:56 +0200 |
commit | a2e57f7df5aed74991002b4940c7f1152d83235b (patch) | |
tree | 843081fa369f9c47f4ae98fe7c35a56e52d658b0 | |
parent | a76d25f222615a7a211f737d32c0b6d79992a8a2 (diff) |
Added first to working mangelbrot implementations for hpp
-rw-r--r-- | sem4/hpp/miniproject/naive.py | 70 | ||||
-rw-r--r-- | sem4/hpp/miniproject/optimised.py | 60 |
2 files changed, 130 insertions, 0 deletions
diff --git a/sem4/hpp/miniproject/naive.py b/sem4/hpp/miniproject/naive.py new file mode 100644 index 0000000..765878a --- /dev/null +++ b/sem4/hpp/miniproject/naive.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +import numpy as np +import matplotlib.pyplot as plt +import time + +# c-mesh limits +limitre = ( -2, 1 ) +limitim = ( -1.5, 1.5 ) + +def lota(c, T, l): + """ + Implement the ι function used in mangelbrot + + :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 + + # If we did not find z, use l + return l + +def mangel(pre, pim, T, l): + """ + 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 + """ + + # Preallocate result array + rs = np.zeros((pre, pim)) + + # 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] = lota(c, T, l) / l + + return rs + + +start = time.time() +arr = mangel(500, 500, 2, 100) +end = time.time() + +plt.imshow(arr, cmap=plt.cm.hot, vmin=0, vmax=1) +plt.savefig("test.png") +plt.savefig("test.pdf") + +print(f"Took {end - start} seconds") diff --git a/sem4/hpp/miniproject/optimised.py b/sem4/hpp/miniproject/optimised.py new file mode 100644 index 0000000..64af72d --- /dev/null +++ b/sem4/hpp/miniproject/optimised.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 + +import numpy as np +import matplotlib.pyplot as plt +import time + +# c-mesh limits +limitre = ( -2, 1 ) +limitim = ( -1.5, 1.5 ) + +def lota(c, T, l): + z = 0 + for i in range(l): + nz = z*z + c + + # Check if we found or z + if np.abs(nz) > T: + break + + z = nz + else: + # If we did not find z, use l + return l + + return np.abs(z) + +def mangel(pre, pim, T, l): + # Preallocate result array and z array + rs = np.zeros((pre, pim)) + z = np.zeros((pre, pim)) + + # Calculate C matrix + re = np.linspace(limitre[0], limitre[1], pre) + im = np.linspace(limitim[0], limitim[1], pim) + + # Calculate C by multiplying the scalers in. Remember to move it to the beggining og the c-mesh limit + grid = np.add.outer(re, 1j * im) + + for i in range(l): + z = z*z + grid + + # Extract all the ones that are under the threshold + below = (np.abs(z) < T) + + rs += below + + rs[ rs==rs.max() ] = l + rs /= l + + return rs + +start = time.time() +arr = mangel(500, 500, 2, 100) +end = time.time() + +plt.imshow(arr, cmap=plt.cm.hot, vmin=0, vmax=1) +plt.savefig("test.png") +plt.savefig("test.pdf") + +print(f"Took {end - start} seconds") |