commit 76fe7fb668d8350ec9df586c764ce54a224efdee Author: Dominik Krenn Date: Wed Nov 5 07:52:44 2025 +0100 init commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f7275bb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +venv/ diff --git a/main.py b/main.py new file mode 100644 index 0000000..e69de29 diff --git a/mathstream/__init__.py b/mathstream/__init__.py new file mode 100644 index 0000000..339ac18 --- /dev/null +++ b/mathstream/__init__.py @@ -0,0 +1,2 @@ +from .engine import clear_logs, add, sub, mul, div +from .number import StreamNumber diff --git a/mathstream/engine.py b/mathstream/engine.py new file mode 100644 index 0000000..e4da028 --- /dev/null +++ b/mathstream/engine.py @@ -0,0 +1,45 @@ +from pathlib import Path +from .number import StreamNumber, LOG_DIR + +def clear_logs(): + if LOG_DIR.exists(): + for p in LOG_DIR.glob("*"): + p.unlink() + LOG_DIR.mkdir(parents=True, exist_ok=True) + +def add(num_a: StreamNumber, num_b: StreamNumber) -> StreamNumber: + """Digit-by-digit streamed addition.""" + out_file = LOG_DIR / f"{num_a.hash}_add_{num_b.hash}.bin" + + carry = 0 + a_buf = list(num_a.stream(1)) + b_buf = list(num_b.stream(1)) + + # align lengths + max_len = max(len(a_buf), len(b_buf)) + a_buf = ["0"] * (max_len - len(a_buf)) + a_buf + b_buf = ["0"] * (max_len - len(b_buf)) + b_buf + + with open(out_file, "wb") as out: + for i in range(max_len - 1, -1, -1): + s = int(a_buf[i]) + int(b_buf[i]) + carry + carry, digit = divmod(s, 10) + out.write(str(digit).encode()) + if carry: + out.write(str(carry).encode()) + return StreamNumber(out_file) + +def sub(num_a, num_b): + """Basic streamed subtraction (assumes a >= b).""" + # similar pattern with borrow propagation... + pass + +def mul(num_a, num_b): + """Chunked multiplication using repeated addition.""" + # create temporary stage files for partial sums + pass + +def div(num_a, num_b): + """Long division, streamed stage by stage.""" + # create multiple intermediate files: div_stage_1, div_stage_2, etc. + pass diff --git a/mathstream/number.py b/mathstream/number.py new file mode 100644 index 0000000..a3cc79e --- /dev/null +++ b/mathstream/number.py @@ -0,0 +1,27 @@ +import hashlib +from pathlib import Path + +LOG_DIR = Path("./instance/log") + +class StreamNumber: + def __init__(self, file_path): + self.path = Path(file_path) + if not self.path.exists(): + raise FileNotFoundError(self.path) + self.hash = hashlib.sha1(str(self.path).encode()).hexdigest()[:10] + + def __repr__(self): + return f"" + + def stream(self, chunk_size=4096): + """Yield chunks of digits as strings.""" + with open(self.path, "r", encoding="utf-8") as f: + while chunk := f.read(chunk_size): + yield chunk.strip().replace(",", ".") + + def write_stage(self, stage, data: str): + """Write intermediate stage result.""" + stage_file = LOG_DIR / f"{self.hash}_stage_{stage}.bin" + with open(stage_file, "wb") as f: + f.write(data.encode()) + return stage_file diff --git a/mathstream/utils.py b/mathstream/utils.py new file mode 100644 index 0000000..e69de29