From 9ce1087a076fabe0cb05071a8f81bb343cd7f35e Mon Sep 17 00:00:00 2001 From: hackprot0 <91518094+hackprot0@users.noreply.github.com> Date: Sat, 2 Oct 2021 22:31:29 +0530 Subject: [PATCH 1/2] Python code for linear gauss deletion --- cellular_automata/gauss_deletion.py | 83 +++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 cellular_automata/gauss_deletion.py diff --git a/cellular_automata/gauss_deletion.py b/cellular_automata/gauss_deletion.py new file mode 100644 index 000000000000..2dada4fbf9b1 --- /dev/null +++ b/cellular_automata/gauss_deletion.py @@ -0,0 +1,83 @@ +""" +Gaussian elimination method for solving a system of linear equations. +Gaussian elimination - https://en.wikipedia.org/wiki/Gaussian_elimination +""" + + +import numpy as np + + +def retroactive_resolution(coefficients: np.matrix, vector: np.ndarray) -> np.ndarray: + """ + This function performs a retroactive linear system resolution + for triangular matrix + + Examples: + 2x1 + 2x2 - 1x3 = 5 2x1 + 2x2 = -1 + 0x1 - 2x2 - 1x3 = -7 0x1 - 2x2 = -1 + 0x1 + 0x2 + 5x3 = 15 + >>> gaussian_elimination([[2, 2, -1], [0, -2, -1], [0, 0, 5]], [[5], [-7], [15]]) + array([[2.], + [2.], + [3.]]) + >>> gaussian_elimination([[2, 2], [0, -2]], [[-1], [-1]]) + array([[-1. ], + [ 0.5]]) + """ + + rows, columns = np.shape(coefficients) + + x = np.zeros((rows, 1), dtype=float) + for row in reversed(range(rows)): + sum = 0 + for col in range(row + 1, columns): + sum += coefficients[row, col] * x[col] + + x[row, 0] = (vector[row] - sum) / coefficients[row, row] + + return x + + +def gaussian_elimination(coefficients: np.matrix, vector: np.ndarray) -> np.ndarray: + """ + This function performs Gaussian elimination method + + Examples: + 1x1 - 4x2 - 2x3 = -2 1x1 + 2x2 = 5 + 5x1 + 2x2 - 2x3 = -3 5x1 + 2x2 = 5 + 1x1 - 1x2 + 0x3 = 4 + >>> gaussian_elimination([[1, -4, -2], [5, 2, -2], [1, -1, 0]], [[-2], [-3], [4]]) + array([[ 2.3 ], + [-1.7 ], + [ 5.55]]) + >>> gaussian_elimination([[1, 2], [5, 2]], [[5], [5]]) + array([[0. ], + [2.5]]) + """ + # coefficients must to be a square matrix so we need to check first + rows, columns = np.shape(coefficients) + if rows != columns: + return np.array((), dtype=float) + + # augmented matrix + augmented_mat = np.concatenate((coefficients, vector), axis=1) + augmented_mat = augmented_mat.astype("float64") + + # scale the matrix leaving it triangular + for row in range(rows - 1): + pivot = augmented_mat[row, row] + for col in range(row + 1, columns): + factor = augmented_mat[col, row] / pivot + augmented_mat[col, :] -= factor * augmented_mat[row, :] + + x = retroactive_resolution( + augmented_mat[:, 0:columns], augmented_mat[:, columns : columns + 1] + ) + + return x + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 3ba0ca59d2732924afdad32d6846ced343e91369 Mon Sep 17 00:00:00 2001 From: hackprot0 <91518094+hackprot0@users.noreply.github.com> Date: Sat, 2 Oct 2021 22:52:43 +0530 Subject: [PATCH 2/2] =?UTF-8?q?Most=20common=20implementation=20of=20Lempe?= =?UTF-8?q?l=E2=80=93Ziv=E2=80=93Welch=20decompression=20algorithm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LZW_decompress.py | 111 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 LZW_decompress.py diff --git a/LZW_decompress.py b/LZW_decompress.py new file mode 100644 index 000000000000..4d3c2c0d2cf3 --- /dev/null +++ b/LZW_decompress.py @@ -0,0 +1,111 @@ +""" + One of the several implementations of Lempel–Ziv–Welch decompression algorithm + https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch +""" + +import math +import sys + + +def read_file_binary(file_path: str) -> str: + """ + Reads given file as bytes and returns them as a long string + """ + result = "" + try: + with open(file_path, "rb") as binary_file: + data = binary_file.read() + for dat in data: + curr_byte = f"{dat:08b}" + result += curr_byte + return result + except OSError: + print("File not accessible") + sys.exit() + + +def decompress_data(data_bits: str) -> str: + """ + Decompresses given data_bits using Lempel–Ziv–Welch compression algorithm + and returns the result as a string + """ + lexicon = {"0": "0", "1": "1"} + result, curr_string = "", "" + index = len(lexicon) + + for i in range(len(data_bits)): + curr_string += data_bits[i] + if curr_string not in lexicon: + continue + + last_match_id = lexicon[curr_string] + result += last_match_id + lexicon[curr_string] = last_match_id + "0" + + if math.log2(index).is_integer(): + newLex = {} + for curr_key in list(lexicon): + newLex["0" + curr_key] = lexicon.pop(curr_key) + lexicon = newLex + + lexicon[bin(index)[2:]] = last_match_id + "1" + index += 1 + curr_string = "" + return result + + +def write_file_binary(file_path: str, to_write: str) -> None: + """ + Writes given to_write string (should only consist of 0's and 1's) as bytes in the + file + """ + byte_length = 8 + try: + with open(file_path, "wb") as opened_file: + result_byte_array = [ + to_write[i : i + byte_length] + for i in range(0, len(to_write), byte_length) + ] + + if len(result_byte_array[-1]) % byte_length == 0: + result_byte_array.append("10000000") + else: + result_byte_array[-1] += "1" + "0" * ( + byte_length - len(result_byte_array[-1]) - 1 + ) + + for elem in result_byte_array[:-1]: + opened_file.write(int(elem, 2).to_bytes(1, byteorder="big")) + except OSError: + print("File not accessible") + sys.exit() + + +def remove_prefix(data_bits: str) -> str: + """ + Removes size prefix, that compressed file should have + Returns the result + """ + counter = 0 + for letter in data_bits: + if letter == "1": + break + counter += 1 + + data_bits = data_bits[counter:] + data_bits = data_bits[counter + 1 :] + return data_bits + + +def compress(source_path: str, destination_path: str) -> None: + """ + Reads source file, decompresses it and writes the result in destination file + """ + data_bits = read_file_binary(source_path) + data_bits = remove_prefix(data_bits) + decompressed = decompress_data(data_bits) + write_file_binary(destination_path, decompressed) + + +if __name__ == "__main__": + compress(sys.argv[1], sys.argv[2])