def initialize_x_values(): x = [] for symbol in constellation: if np.real(symbol) not in x: x.append(np.real(symbol)) x = sorting.merge(x) return x
def test_merge(self): items1 = ["Bananas", "Picadilly", "Picasso"] items2 = ["August", "Picadilly", "Rearing"] new_list = merge(items1, items2) print(new_list) assert new_list == [ "August", "Bananas", "Picadilly", "Picadilly", "Picasso", "Rearing" ]
def test_merge(self): """ Tests that the merge operation successfully merges two sorted lists into a single sorted list. """ lst1 = [random.random() for i in range(N_NUMBERS)] lst1.sort() lst2 = [random.random() for i in range(N_NUMBERS)] lst2.sort() merged = lst1 + lst2 merge(merged, 0, len(lst1) - 1, len(merged) - 1) expected = lst1 + lst2 expected.sort() self.assertListEqual(expected, merged)
def test_merge(self): # Note: merge works in O(n) time assuming that the two lists are SORTED num1 = [1,4,6,7,20] num2 = [2,5,9,23] orig1, orig2 = num1, num2 ans = [1,2,4,5,6,7,9,20,23] test = sorting.merge(num1, num2) self.assertEqual(test, ans) self.assertEqual(orig1, num1) self.assertEqual(orig2, num2)
def test_merge(self): # Note: merge works in O(n) time assuming that the two lists are SORTED num1 = [1, 4, 6, 7, 20] num2 = [2, 5, 9, 23] orig1, orig2 = num1, num2 ans = [1, 2, 4, 5, 6, 7, 9, 20, 23] test = sorting.merge(num1, num2) self.assertEqual(test, ans) self.assertEqual(orig1, num1) self.assertEqual(orig2, num2)
def realizar_test_chi_cuadrado(self, numeros_pseudoaleatorios, intervalos): # Ordeno lista de numeros pseudoaleatorios para facilitar el calculo de frecuencias por intervalo, optimizando # el procesamiento con un algoritmo de ordenamiento de O(n * log n) numeros_pseudoaleatorios = sorting.merge(numeros_pseudoaleatorios) # Calculo frecuencias observadas por intervalo cantidad_intervalos = len(intervalos) frecuencias_observadas_x_intervalo = [0] * cantidad_intervalos index = 0 for numero_pseudoaleatorio in numeros_pseudoaleatorios: if not (intervalos[index][0] <= numero_pseudoaleatorio < intervalos[index][1]): index += 1 frecuencias_observadas_x_intervalo[index] += 1 # Calculo frecuencias esperadas por intervalo fe = Decimal(len(numeros_pseudoaleatorios) / cantidad_intervalos) frecuencias_esperadas_x_intervalo = [fe] * cantidad_intervalos # Genero una lista de diccionarios con el calculo de la prueba de chi cuadrado por intervalo y guardo el final # en una variable chi_cuadrado_x_intervalo = [] fo_acum = 0 fe_acum = 0 c_acum = 0 for i in range(0, cantidad_intervalos): intervalo = (intervalos[i][0].quantize(TWOPLACES), intervalos[i][1].quantize(TWOPLACES)) fo = frecuencias_observadas_x_intervalo[i] fo_acum += fo fe = frecuencias_esperadas_x_intervalo[i] fe_acum += fe c = ((fo - fe) ** 2) / fe c_acum += c chi_cuadrado_x_intervalo.append({ "intervalo": intervalo, "fo": fo, "fo_acum": round(fo_acum, 4), "fe": fe.quantize(FOURPLACES), "fe_acum": round(fe_acum, 4), "c": c.quantize(FOURPLACES), "c_acum": round(c_acum, 4) }) chi_cuadrado = round(c_acum, 4) return chi_cuadrado_x_intervalo, chi_cuadrado
def test_merge(self): sa = self.assertEqual l = [2, 7, 3, 4] sl = sorted(l) sorting.merge(l, 0, 1, 2, 3) sa(sl, l) l = [7, 2] sl = sorted(l) sorting.merge(l, 0, 0, 1, 1) sa(sl, l) l = [-34, 4, 4, 5, 120, 200, 300, 400] sl = sorted(l) sorting.merge(l, 0, 4, 5, 7) sa(sl, l)
def test_merge(self): sa = self.assertEqual l = [2, 7, 3, 4] sl = sorted(l) sorting.merge(l, 0, 1, 2, 3) sa(sl, l) l = [7, 2] sl = sorted(l) sorting.merge(l, 0, 0, 1, 1) sa(sl, l) l = [-34, 4 ,4, 5, 120, 200, 300, 400] sl = sorted(l) sorting.merge(l, 0, 4, 5, 7) sa(sl, l)
def merge(self): S.merge() self.assertEqual(self.merge([1, 2, 3, 4], [2, 3, 5]), [1, 2, 3, 4, 5])
from sorting import merge listA = [1, 3, 5, 7, 8, 9] listB = [2, 4, 6] print(merge(listA, listB))
sorting.counting(data) end_time = timeit.default_timer() print('Counting Sort : ', '%.15f' % (end_time - start_time)) start_time = timeit.default_timer() sorting.maxheap(data) end_time = timeit.default_timer() print('Max Heap Sort : ', '%.15f' % (end_time - start_time)) start_time = timeit.default_timer() sorting.minheap(data) end_time = timeit.default_timer() print('Min Heap Sort : ', '%.15f' % (end_time - start_time)) start_time = timeit.default_timer() sorting.merge(data) end_time = timeit.default_timer() print('Merge Sort : ', '%.15f' % (end_time - start_time)) start_time = timeit.default_timer() sorting.quick(data) end_time = timeit.default_timer() print('Quick Sort : ', '%.15f' % (end_time - start_time)) start_time = timeit.default_timer() sorting.radix(data) end_time = timeit.default_timer() print('Radix Sort : ', '%.15f' % (end_time - start_time)) start_time = timeit.default_timer() sorting.selection(data)
def test_merge_descending(self): (lo, hi) = ([12, 9, 5], [15, 13, 11, 9]) self.assertEqual(merge(lo, hi, False), [15, 13, 12, 11, 9, 9, 5])
def ordenar_variables_aleatorias(self, variables_aleatorias): # Ordeno lista de variables aleatorias para facilitar la obtencion de mínimo y máximo y obtención de frecuencias # por intervalo, optimizando el procesamiento con un algoritmo de ordenamiento de O(n * log n) return sorting.merge(variables_aleatorias)
def test_merge(self): list_a = [3, 5, 7] list_b = [2, 4, 6, 8, 10] self.assertEqual(merge(list_a, list_b), [2, 3, 4, 5, 6, 7, 8, 10])
def hqam(M, nn_dist=2): # M is the order of constellation, nn_dist is the nearest neighbour dist # The idea behind this algo is that we will create an M-ary constellation with a center point at(0,-y_1) # and then we will shift the constellation backwards in the x-axis in order to achieve the symmetry # of the constellation around the origin for a regular HQAM # We have to parametrize the func in order to generate different types of HQAM # depending on M and nn_dist # First we check if M is the square of an even integer even = 0 # even number that is even^2=M or the first even number that is even^2>M for i in range(2, M, 2): if i * i == M: even = i break elif i * i > M: even = i break diff = even**2 - M symbols = np.array([]) # this works only for nn_dist = 2, something went wrong with listing comprehension below (check the bounds) # We can observe that we have two possible arrays for the x_axis values if diff == 0: pos_x_axis1 = np.array([ x for x in range(nn_dist // 2, even + 1) if x % 2 == 1 ]) # for 64-HQAM neg_x_axis1 = np.array( [-x for x in range(nn_dist // 2, even + 1) if x % 2 == 1]) pos_x_axis2 = np.array( [x for x in range(nn_dist // 2, even + 1) if x % 2 == 0]) neg_x_axis2 = np.array([-x for x in range(0, even) if x % 2 == 0]) # for 64-HQAM # We can observe that we have only one array for y_axis values y_unity = np.sqrt(3 * (nn_dist**2) / 4) # the height of the basic equilateral triangle pos_y_axis = np.array([ y_unity / 2 + y_unity * i for i in range(0, even // 2) ]) # for 64-HQAM not exactly xd neg_y_axis = np.array([ -y_unity / 2 - y_unity * (even // 2 - i - 1) for i in range(0, even // 2) ]) # build 1st quadrant cnt1 = 0 cnt2 = 0 for column in range(0, even, 1): if column % 2 == 0: temp = np.ones(np.ceil(even // 4).astype(int)) * pos_x_axis1[ cnt1] # the real part of the symbol temp = temp + 1j * np.array( [pos_y_axis[i] for i in range(0, even // 2) if i % 2 == 0]) symbols = np.concatenate((symbols, temp)) cnt1 += 1 else: temp = np.ones(np.ceil( even // 4).astype(int)) * pos_x_axis2[cnt2] temp = temp + 1j * np.array( [pos_y_axis[i] for i in range(0, even // 2) if i % 2 != 0]) symbols = np.concatenate((symbols, temp)) cnt2 += 1 # build 2nd quadrant cnt1 = 0 cnt2 = 0 for column in range(0, even, 1): if column % 2 == 0: temp = np.ones(np.ceil(even // 4).astype(int)) * neg_x_axis1[ cnt1] # the ceil and astype(int) are added # because of the posibility that we want to produce 32-HQAM and therefore even == 6 temp = temp + 1j * np.array( [pos_y_axis[i] for i in range(0, even // 2) if i % 2 == 0]) symbols = np.concatenate((symbols, temp)) cnt1 += 1 else: temp = np.ones(np.ceil( even // 4).astype(int)) * neg_x_axis2[cnt2] temp = temp + 1j * np.array( [pos_y_axis[i] for i in range(0, even // 2) if i % 2 != 0]) symbols = np.concatenate((symbols, temp)) cnt2 += 1 # build 3rd quadrant cnt1 = 0 cnt2 = 0 for column in range(0, even, 1): if column % 2 == 0: temp = np.ones(np.ceil( even // 4).astype(int)) * neg_x_axis1[cnt1] temp = temp + 1j * np.array( [neg_y_axis[i] for i in range(0, even // 2) if i % 2 == 0]) symbols = np.concatenate((symbols, temp)) cnt1 += 1 else: temp = np.ones(np.ceil( even // 4).astype(int)) * neg_x_axis2[cnt2] temp = temp + 1j * np.array( [neg_y_axis[i] for i in range(0, even // 2) if i % 2 != 0]) symbols = np.concatenate((symbols, temp)) cnt2 += 1 # build 4th quadrant cnt1 = 0 cnt2 = 0 for column in range(0, even, 1): if column % 2 == 0: temp = np.ones(np.ceil( even // 4).astype(int)) * pos_x_axis1[cnt1] temp = temp + 1j * np.array( [neg_y_axis[i] for i in range(0, even // 2) if i % 2 == 0]) symbols = np.concatenate((symbols, temp)) cnt1 += 1 else: temp = np.ones(np.ceil( even // 4).astype(int)) * pos_x_axis2[cnt2] temp = temp + 1j * np.array( [neg_y_axis[i] for i in range(0, even // 2) if i % 2 != 0]) symbols = np.concatenate((symbols, temp)) cnt2 += 1 # Now we will shift the constellation in x-axis direction shift = np.sqrt((nn_dist / 2)**2 - (y_unity / 2)**2) # calculated by pythagorean theorem for k in range(0, len(symbols)): symbols[k] -= shift # by this move we achieve the symmetry return symbols else: # diff !=0 # We will assume that we can create the constellation with even * even points and then we will erase # diff points from the constellation, we will erase them depending on their energy # First, we have created an even^2 constellation, # symbols is an already constructed np.array with points on the complex plane # Now , we are going to remove the diff points from the symbols pos_x_axis1 = np.array([ x for x in range(nn_dist // 2, even + 1) if x % 2 == 1 ]) # for 64-HQAM neg_x_axis1 = np.array( [-x for x in range(nn_dist // 2, even + 1) if x % 2 == 1]) pos_x_axis2 = np.array( [x for x in range(nn_dist // 2, even + 1) if x % 2 == 0]) neg_x_axis2 = np.array([-x for x in range(0, even) if x % 2 == 0]) # for 64-HQAM # We can observe that we have only one array for y_axis values y_unity = np.sqrt(3 * (nn_dist**2) / 4) # the height of the basic equilateral triangle pos_y_axis = np.array([ y_unity / 2 + y_unity * i for i in range(0, even // 2) ]) # for 64-HQAM not exactly xd neg_y_axis = np.array([ -y_unity / 2 - y_unity * (even // 2 - i - 1) for i in range(0, even // 2) ]) # build 1st quadrant cnt1 = 0 cnt2 = 0 for column in range(0, even, 1): if column % 2 == 0: temp = np.ones(np.ceil(even // 4).astype(int)) * pos_x_axis1[ cnt1] # the real part of the symbol temp = temp + 1j * np.array( [pos_y_axis[i] for i in range(0, even // 2) if i % 2 == 0]) symbols = np.concatenate((symbols, temp)) cnt1 += 1 else: temp = np.ones(np.ceil( even // 4).astype(int)) * pos_x_axis2[cnt2] temp = temp + 1j * np.array( [pos_y_axis[i] for i in range(0, even // 2) if i % 2 != 0]) symbols = np.concatenate((symbols, temp)) cnt2 += 1 # build 2nd quadrant cnt1 = 0 cnt2 = 0 for column in range(0, even, 1): if column % 2 == 0: temp = np.ones(np.ceil( even // 4).astype(int)) * neg_x_axis1[cnt1] temp = temp + 1j * np.array( [pos_y_axis[i] for i in range(0, even // 2) if i % 2 == 0]) symbols = np.concatenate((symbols, temp)) cnt1 += 1 else: temp = np.ones(np.ceil( even // 4).astype(int)) * neg_x_axis2[cnt2] temp = temp + 1j * np.array( [pos_y_axis[i] for i in range(0, even // 2) if i % 2 != 0]) symbols = np.concatenate((symbols, temp)) cnt2 += 1 # build 3rd quadrant cnt1 = 0 cnt2 = 0 for column in range(0, even, 1): if (even // 2) % 2 == 0: if column % 2 == 0: temp = np.ones(np.ceil( even // 4).astype(int)) * neg_x_axis1[cnt1] temp = temp + 1j * np.array([ neg_y_axis[i] for i in range(0, even // 2) if i % 2 == 0 ]) symbols = np.concatenate((symbols, temp)) cnt1 += 1 else: temp = np.ones(np.ceil( even // 4).astype(int)) * neg_x_axis2[cnt2] temp = temp + 1j * np.array([ neg_y_axis[i] for i in range(0, even // 2) if i % 2 != 0 ]) symbols = np.concatenate((symbols, temp)) cnt2 += 1 else: # exception when we have 32-HQAM even = 6 and even//2 = 3 if column % 2 == 0: temp = np.ones(np.ceil( even // 4).astype(int)) * neg_x_axis2[cnt1] temp = temp + 1j * np.array([ neg_y_axis[i] for i in range(0, even // 2) if i % 2 == 0 ]) symbols = np.concatenate((symbols, temp)) cnt1 += 1 else: temp = np.ones(np.ceil( even // 4).astype(int)) * neg_x_axis1[cnt2] temp = temp + 1j * np.array([ neg_y_axis[i] for i in range(0, even // 2) if i % 2 != 0 ]) symbols = np.concatenate((symbols, temp)) cnt2 += 1 # build 4th quadrant cnt1 = 0 cnt2 = 0 for column in range(0, even, 1): if (even // 2) % 2 == 0: if column % 2 == 0: temp = np.ones(np.ceil( even // 4).astype(int)) * pos_x_axis1[cnt1] temp = temp + 1j * np.array([ neg_y_axis[i] for i in range(0, even // 2) if i % 2 == 0 ]) symbols = np.concatenate((symbols, temp)) cnt1 += 1 else: temp = np.ones(np.ceil( even // 4).astype(int)) * pos_x_axis2[cnt2] temp = temp + 1j * np.array([ neg_y_axis[i] for i in range(0, even // 2) if i % 2 != 0 ]) symbols = np.concatenate((symbols, temp)) cnt2 += 1 else: # exception when we have 32-HQAM even = 6 and even//2 = 3 if column % 2 == 0: temp = np.ones(np.ceil( even // 4).astype(int)) * pos_x_axis2[cnt1] temp = temp + 1j * np.array([ neg_y_axis[i] for i in range(0, even // 2) if i % 2 == 0 ]) symbols = np.concatenate((symbols, temp)) cnt1 += 1 else: temp = np.ones(np.ceil( even // 4).astype(int)) * pos_x_axis1[cnt2] temp = temp + 1j * np.array([ neg_y_axis[i] for i in range(0, even // 2) if i % 2 != 0 ]) symbols = np.concatenate((symbols, temp)) cnt2 += 1 shift = np.sqrt((nn_dist / 2)**2 - (y_unity / 2)**2) for k in range(0, len(symbols)): symbols[k] -= shift # by this move we achieve the symmetry energy = [ np.real(symbol)**2 + np.imag(symbol)**2 for symbol in symbols ] energy = sorting.merge( energy ) # sorted in ascending order O(n*log n) algorithm complexity erased = [] removed = 0 for k in range(0, len(symbols)): symbol_energy = np.real(symbols[k])**2 + np.imag( symbols[k] )**2 # calculate energy per symbol while traversing symbols array # compare it to the diff-biggest elements of the energy array and if it is inside delete it for j in range(len(energy) - 1, len(energy) - diff, -1): if energy[j] == symbol_energy: removed += 1 erased.append(k) break if removed == diff: break print(len(erased)) symbols = np.delete(symbols, erased) print(len(symbols)) return symbols
import sorting filename = "/home/vampy/data/test1" fp =open(filename, "r") N = int(fp.readline()) names = [] for i in range(N): names.append(fp.readline().strip()) fp.close() sorting.merge(names) fp=open(open_filename,"w") fp.write("{0}\n".format(N)) for i n range(N) fp.write("{0}\n".format(names[i])) fp.close()
def test_merge_ascending(self): (lo, hi) = ([5, 9, 12], [9, 11, 13, 15]) self.assertEqual(merge(lo, hi, True), [5, 9, 9, 11, 12, 13, 15])