def quick_sort(values, first, last):
if first < last:
splitPoint = split(values, first, last)
quick_sort(values, first, splitPoint - 1)
quick_sort(values, splitPoint + 1, last)
return values
def split(values, first, last):
splitval = values[first]
savefirst = first
first += 1
while True:
onCollectSide = True
while onCollectSide:
if values[first] > splitval:
onCollectSide = False
else:
first += 1
onCollectSide = (first <= last)
onCollectSide = (first <= last)
while onCollectSide:
if values[last] <= splitval:
onCollectSide = False
else:
last -= 1
onCollectSide = (first <= last)
if first < last:
temp = values[first]
values[first] = values[last]
values[last] = temp
first += 1
last -= 1
if first <= last:
continue
break
splitPoint = last
temp1 = values[savefirst]
values[savefirst] = values[splitPoint]
values[splitPoint] = temp1
return splitPoint
from numpy import *
import numpy as np
def straseen (n, A, B, C):
threshold = 2
A11 = np.array([[A[rows][cols] for cols in range(int(n / 2))] for rows in range(int(n / 2))])
A12 = np.array([[A[rows][cols] for cols in range(int(n / 2), n)] for rows in range(int(n / 2))])
A21 = np.array([[A[rows][cols] for cols in range(int(n / 2))] for rows in range(int(n / 2), n)])
A22 = np.array([[A[rows][cols] for cols in range(int(n / 2), n)] for rows in range(int(n / 2), n)])
B11 = np.array([[B[rows][cols] for cols in range(int(n / 2))] for rows in range(int(n / 2))])
B12 = np.array([[B[rows][cols] for cols in range(int(n / 2), n)] for rows in range(int(n / 2))])
B21 = np.array([[B[rows][cols] for cols in range(int(n / 2))] for rows in range(int(n / 2), n)])
B22 = np.array([[B[rows][cols] for cols in range(int(n / 2), n)] for rows in range(int(n / 2), n)])
if n <= threshold:
C = np.array(A) @ np.array(B)
else:
M1 = M2 = M3 = M4 = M5 = M6 = M7 = np.array([])
M1 = straseen(int(n/2), (A11+A22), (B11+B22), M1)
M2 = straseen(int(n/2), (A21+A22), B11, M2)
M3 = straseen(int(n/2), A11, (B12-B22), M3)
M4 = straseen(int(n/2), A22, (B21 - B11), M4)
M5 = straseen(int(n/2), (A11+A12), B22, M5)
M6 = straseen(int(n/2), (A21-A11), (B11 + B12), M6)
M7 = straseen(int(n/2), (A12-A22), (B21 + B22), M7)
C = np.vstack( [np.hstack ( [M1+M4-M5+M7, M3+M5] ), np.hstack( [M2+M4, M1+M3-M2+M6] )] )
return C
단위연산: 곱셈하는 연산
단위연산: 덧셈/뺄셈하는 연산