I Used NumPy to Impress My Crush- Solved a Math Question

The math required 169 different calculations!

The given data set of the math. credit: author

Code:

import numpy as np

# loading files
a = np.array([[10,9,20,25,26,34,35,11,20,25,31,32,35,36,28],[76,34,11,15,41,42,77,14,30,24,22,11,16,69,70],[47,25,13,11,10,55,41,39,42,65,27,79,18,20,35],[42,19,15,19,22,52,71,56,34,35,41,50,55,11,38],[34,20,16,60,27,85,35,17,25,16,10,31,52,13,40],[25,30,29,23,18,49,38,78,19,29,18,26,85,12,41],[30,42,20,13,57,23,73,74,60,20,57,63,82,19,71],[19,60,45,28,40,11,79,32,48,45,51,72,42,21,36],[64,36,55,58,38,13,80,28,64,55,61,44,49,32,37],[36,70,31,62,15,15,70,58,76,66,40,13,23,42,72],[70,68,22,14,75,19,36,62,47,59,38,71,11,50,34],[68,43,27,39,54,60,37,52,36,80,15,53,19,51,72],[43,48,41,56,46,16,72,21,70,23,75,33,60,59,73],[60,64,10,17,51,18,34,49,68,67,54,37,13,75,79],[48,76,18,78,61,82,72,69,43,73,46,12,15,25,80]])
q = f'''given dataset:
{a}'''
print(q)
b = np.array([[1,-2,1],[-2,5,-2],[1,-2,1]])
final_result =[]


print("Calculation: ")

u= 0

for m,n in zip(range(0,13),range(3,16)):
for i,j in zip(range(0,13),range(3,16)):
l = a[m:n,i:j] * b
w = np.sum(l)
u= u+1
final_result.append(w)
c = a[m:n,i:j]
e = f'''{u}th DN value change operation:
'''
print(e)


t = f'''= [({c[0,0]} × {b[0,0]}) + ({c[0,1]} × {b[0,1]}) + ({c[0,2]} × {b[0,2]}) + ({c[1,0]} × {b[1,0]}) + ({c[1,1]} × {b[1,1]}) + ({c[1,2]} × {b[1,2]}) + ({c[2,0]} × {b[2,0]}) + ({c[2,1]} × {b[2,1]}) + ({c[2,2]} × {b[2,2]})]

= ({l[0,0]}) + ({l[0,1]}) + ({l[0,2]}) + ({l[1,0]}) + ({l[1,1]}) + ({l[1,2]}) + ({l[2,0]}) + ({l[2,1]}) + ({l[2,2]})

= {w}
'''
print(t)


final_array = np.array(final_result)

s = final_array.reshape((13,13))
k = f'''final 13*13 result:
{s}'''
print(k)

Output:

169th number calculation is shown here. Check the Github link to see all.

Python Programmer, Online Marketer. Teacher, Udemy. Searching for a balanced life.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store