init commit
This commit is contained in:
commit
76fe7fb668
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
venv/
|
||||||
2
mathstream/__init__.py
Normal file
2
mathstream/__init__.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
from .engine import clear_logs, add, sub, mul, div
|
||||||
|
from .number import StreamNumber
|
||||||
45
mathstream/engine.py
Normal file
45
mathstream/engine.py
Normal file
@ -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
|
||||||
27
mathstream/number.py
Normal file
27
mathstream/number.py
Normal file
@ -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"<StreamNumber {self.path.name}>"
|
||||||
|
|
||||||
|
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
|
||||||
0
mathstream/utils.py
Normal file
0
mathstream/utils.py
Normal file
Loading…
x
Reference in New Issue
Block a user