add multiplication logging, include prettytable library and update input_handler menu structure

This commit is contained in:
dymik739 2023-06-28 12:36:02 +03:00
parent 5a26802c34
commit 4e0e30383d
3 changed files with 2619 additions and 12 deletions

View File

@ -1,5 +1,6 @@
from collections import deque
from typing_extensions import Self
from lib.prettytable import PrettyTable
class BasicRegister:
@ -13,14 +14,17 @@ class BasicRegister:
self.memory: deque[bool] = deque(memory)
def __repr__(self) -> str:
return f"Memory: {[int(value) for value in self.memory]}"
return "".join([str(int(value)) for value in self.memory])
def __str__(self) -> str:
return f"Memory: {[int(value) for value in self.memory]}"
return "".join([str(int(value)) for value in self.memory])
def __len__(self) -> int:
return len(self.memory)
def compact_str(self) -> str:
return "".join([str(int(value)) for value in self.memory])
def adjusted_by_size(self, resulting_size: int) -> Self:
"""
Adjusts a register to a given size.
@ -81,14 +85,17 @@ class Counter:
self.memory: deque[bool] = deque([i == "1" for i in bin(value)[2:]])
def __repr__(self) -> str:
return f"Memory: {[int(value) for value in self.memory]}"
return "".join([str(int(value)) for value in self.memory])
def __str__(self) -> str:
return f"Memory: {[int(value) for value in self.memory]}"
return "".join([str(int(value)) for value in self.memory])
def __len__(self) -> int:
return len(self.memory)
def compact_str(self) -> str:
return "".join([str(int(value)) for value in self.memory])
def decrement(self):
self.memory = binary_subtraction(self, BasicRegister([False] * (len(self.memory) - 1) + [True])).memory
@ -206,6 +213,18 @@ def align_registers(*registers: BasicRegister) -> tuple[BasicRegister, ...]:
return tuple(reg.adjusted_by_size(required_size) for reg in registers)
def format_device_state_table(table) -> str:
pt = PrettyTable()
pt.field_names = table[0]
for block in table[1:]:
for line in block[:-1]:
pt.add_row(line)
pt.add_row(block[-1], divider = True)
return pt.get_string()
def binary_multiplication_method_1(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
"""
Multiplies two terms containing binary numbers using first method.
@ -224,15 +243,29 @@ def binary_multiplication_method_1(first_term: BasicRegister, second_term: Basic
rg3 = BasicRegister(second_term.memory)
ct = Counter(n)
data_table = [["iter", "RG1", "RG2", "RG3", "CT", "MicroOperations"]]
i = 0
data_table.append([])
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, ct, "-"])))
while ct.non_zero():
i += 1
data_table.append([])
if rg2.memory[n-1]:
rg1 = binary_sum(rg1, rg3)
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, ct, "RG1 := RG1 + RG3"])))
rg2.right_shift(rg1.memory[n-1])
rg1.right_shift()
ct.decrement()
return BasicRegister(list(rg1.memory) + list(rg2.memory))
print(ct.memory)
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, ct, "RG2 := RG1[1].r(RG2)\nRG1 := 0.r(RG1)\nCT := CT - 1"])))
return BasicRegister(list(rg1.memory) + list(rg2.memory)), data_table
def binary_multiplication_method_2(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
"""
@ -251,14 +284,26 @@ def binary_multiplication_method_2(first_term: BasicRegister, second_term: Basic
rg2 = BasicRegister(first_term.memory)
rg3 = BasicRegister([False] * n + list(second_term.memory))
i = 0
data_table = [["iter", "RG1", "RG2", "RG3", "MicroOperations"]]
data_table.append([])
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, "-"])))
while any(rg2.memory):
i += 1
data_table.append([])
if rg2.memory[n-1]:
rg1 = binary_sum(rg1, rg3)
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, "RG1 := RG1 + RG3"])))
rg2.right_shift()
rg3.left_shift()
return rg1
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, "RG2 := 0.r(RG2)\nRG3 := l(RG3).0"])))
return rg1, data_table
def binary_multiplication_method_3(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
"""
@ -273,22 +318,34 @@ def binary_multiplication_method_3(first_term: BasicRegister, second_term: Basic
first_term, second_term = align_registers(first_term, second_term)
n: int = len(first_term)
data_table = [["iter", "RG2", "RG1", "RG3", "CT", "MicroOperations"]]
rg1 = BasicRegister([False] * n)
rg2 = BasicRegister(list(first_term.memory) + [False])
rg3 = BasicRegister([False] * (n+1) + list(second_term.memory))
ct = Counter(n)
i = 0
data_table.append([])
data_table[-1].append(list(map(str, [i, rg2, rg1, rg3, ct, "-"])))
while ct.non_zero():
i += 1
data_table.append([])
if rg2.memory[0]:
result: list[bool] = list(binary_sum(BasicRegister(rg2.memory + rg1.memory), rg3).memory)
rg2 = BasicRegister(result[:n+1])
rg1 = BasicRegister(result[n+1:])
data_table[-1].append(list(map(str, [i, rg2, rg1, rg3, ct, "RG2.RG1 := RG2.RG1 + RG3"])))
rg2.left_shift(rg1.memory[0])
rg1.left_shift()
ct.decrement()
return BasicRegister((list(rg2.memory) + list(rg1.memory))[:-1])
data_table[-1].append(list(map(str, [i, rg2, rg1, rg3, ct, "RG2.RG1 := l(RG2.RG1).0\nCT := CT - 1"])))
return BasicRegister((list(rg2.memory) + list(rg1.memory))[:-1]), data_table
def binary_multiplication_method_4(first_term: BasicRegister, second_term: BasicRegister) -> BasicRegister:
"""
@ -307,11 +364,23 @@ def binary_multiplication_method_4(first_term: BasicRegister, second_term: Basic
rg2 = BasicRegister(first_term.memory)
rg3 = BasicRegister([False] + list(second_term.memory) + [False] * n)
data_table = [["iter", "RG1", "RG2", "RG3", "MicroOperations"]]
i = 0
data_table.append([])
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, "-"])))
while any(rg2.memory):
i += 1
data_table.append([])
if rg2.memory[0]:
rg1 = binary_sum(rg1, rg3)
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, "RG1 := RG1 + RG3"])))
rg2.left_shift()
rg3.right_shift()
return BasicRegister(list(rg1.memory)[:-1])
data_table[-1].append(list(map(str, [i, rg1, rg2, rg3, "RG2 := l(RG2).0\nRG3 := 0.r(RG3)"])))
return BasicRegister(list(rg1.memory)[:-1]), data_table

2534
lib/prettytable.py Normal file

File diff suppressed because it is too large Load Diff

12
main.py
View File

@ -18,13 +18,17 @@ def input_handler(first_register: bu.BasicRegister, second_register: bu.BasicReg
case "m":
match input("Choose method to use (1-4):\n>>> "):
case "1":
print(f"Multiplication: {bu.binary_multiplication_method_1(first_register, second_register)}")
result, data_table = bu.binary_multiplication_method_1(first_register, second_register)
print(f"Multiplication:\n{bu.format_device_state_table(data_table)}\nResult: {result.compact_str()}")
case "2":
print(f"Multiplication: {bu.binary_multiplication_method_2(first_register, second_register)}")
result, data_table = bu.binary_multiplication_method_2(first_register, second_register)
print(f"Multiplication:\n{bu.format_device_state_table(data_table)}\nResult: {result.compact_str()}")
case "3":
print(f"Multiplication: {bu.binary_multiplication_method_3(first_register, second_register)}")
result, data_table = bu.binary_multiplication_method_3(first_register, second_register)
print(f"Multiplication:\n{bu.format_device_state_table(data_table)}\nResult: {result.compact_str()}")
case "4":
print(f"Multiplication: {bu.binary_multiplication_method_4(first_register, second_register)}")
result, data_table = bu.binary_multiplication_method_4(first_register, second_register)
print(f"Multiplication:\n{bu.format_device_state_table(data_table)}\nResult: {result.compact_str()}")
case _:
print("Such method does not exist, try again.")
case "d":