def MUL_PP_P(koefs_1, powers_1, koefs_2, powers_2): # Артамонов Артур, гр.0306 # Умножение многочленов new_koefs = [] new_powers = [] for i in range(len(koefs_1)): for j in range(len(koefs_2)): k = rat.MUL_QQ_Q(koefs_1[i], koefs_2[j]) # Умножаем фонтанчиком n = nat.ADD_NN_N(powers_1[i], powers_2[j]) # коэффициенты и степени new_koefs.append(k) new_powers.append(n) super_new_powers = [] super_new_koefs = [] deleted = set() t = [[0], [1]] # Складываем одночлены с одинаковыми степенями for i in range(len(new_powers)): x = new_powers[i] if new_powers.count(x) > 1: q = i for j in range(q + 1, len(new_powers)): if new_powers[j] == x: k = rat.ADD_QQ_Q(new_koefs[q], new_koefs[j]) t = rat.ADD_QQ_Q(t, k) if new_powers[i][0] not in deleted: super_new_koefs.append(t) super_new_powers.append(new_powers[i]) deleted.add(new_powers[i][0]) else: super_new_koefs.append(new_koefs[i]) super_new_powers.append(new_powers[i]) n = len(super_new_powers) # Сортировка пузырьком n = len(super_new_powers) for j in range(n - 1): for i in range(n - j - 1): a = int(''.join(map(str, super_new_powers[i]))) b = int(''.join(map(str, super_new_powers[i + 1]))) if a < b: super_new_powers[i], super_new_powers[ i + 1] = super_new_powers[i + 1], super_new_powers[i] super_new_koefs[i], super_new_koefs[i + 1] = super_new_koefs[ i + 1], super_new_koefs[i] return super_new_koefs, super_new_powers
def test_zeros(self): zero = [ [0, 1, [0]], [1, [1]], ] expect = zero result = rational.ADD_QQ_Q(zero, zero) self.assertEqual(result, expect)
def test_zero(self): number = [ [0, 3, [5, 6, 3]], [2, [3, 1]], ] zero = [ [0, 1, [0]], [1, [1]], ] expect = number result = rational.ADD_QQ_Q(number, zero) self.assertEqual(result, expect)
def on_btn_q_add_released(self): try: q1 = self.get_q_q(1) q2 = self.get_q_q(2) result = rational.ADD_QQ_Q(q1, q2) self.add_history_record('%s + %s = %s' % ( pretty.rational(q1), pretty.rational(q2), pretty.rational(result), )) except Exception as e: self.on_exception(e)
def ADD_PP_P(koefs_1, powers_1, koefs_2, powers_2): new_koefs = [] new_powers = [] if len(powers_2) > len(powers_1): koefs_1, koefs_2 = koefs_2, koefs_1 powers_1, powers_2 = powers_2, powers_1 for i in range(len(powers_1)): x = powers_1[i] c = koefs_1[i] q = 0 for j in range(len(powers_2)): if q != -1: q += 1 if powers_2[j] == x: q = -1 k = rat.ADD_QQ_Q(koefs_2[j], c) new_koefs.append(k) new_powers.append(powers_2[j]) if q == len(powers_2): new_koefs.append(c) new_powers.append(x) for i in range(len(powers_2)): x = powers_2[i] c = koefs_2[i] q = 0 for j in range(len(powers_1)): if q != -1: q += 1 if powers_1[j] == x: q = -1 if q == len(powers_1): new_koefs.append(c) new_powers.append(x) n = len(new_powers) for j in range(n - 1): for i in range(n - j - 1): a = int(''.join(map(str, new_powers[i]))) b = int(''.join(map(str, new_powers[i + 1]))) if a < b: new_powers[i], new_powers[i + 1] = new_powers[i + 1], new_powers[i] new_koefs[i], new_koefs[i + 1] = new_koefs[i + 1], new_koefs[i] return new_koefs, new_powers
def test_positive(self): number1 = [ [0, 1, [5]], [1, [3]], ] number2 = [ [0, 1, [2]], [1, [7]], ] expect = [ [0, 2, [1, 4]], [2, [1, 2]], ] result = rational.ADD_QQ_Q(number1, number2) self.assertEqual(result, expect)
def test_negative_second(self): number1 = [ [0, 1, [5]], [1, [3]], ] number2 = [ [1, 1, [2]], [1, [7]], ] expect = [ [0, 2, [9, 2]], [2, [1, 2]], ] result = rational.ADD_QQ_Q(number1, number2) self.assertEqual(result, expect)
def open_window_rat_sum(): layout = [[sg.Text('Enter two rationals')], [sg.Input(key='dig1')], [sg.Button('+', key='start')], [sg.Input(key='dig2')], [sg.Text(size=(400, 10), key='out')]] window = sg.Window('The sum of rational numbers', layout, size=(460, 260), resizable=True) while True: event, values = window.read() if event == "start": window['out'].update( RatToStr( rat.TRANS_Q_Z( rat.ADD_QQ_Q(StrToRat(values['dig1']), StrToRat(values['dig2']))))) if event == sg.WINDOW_CLOSED: break