2025-06-01 10:36:07 -04:00

85 lines
2.8 KiB
Python

import numpy as np
import sys
from decimal import Decimal
from time import time
from ..units import HighPrecisionMatrix
def calculate_distances(positions):
N = len(positions)
dists = HighPrecisionMatrix(N,N)
for i in range(N):
dists[i][i] = Decimal(0)
for j in range(i+1, N):
d = sum([x**2 for x in (positions[i]-positions[j])]).sqrt()
dists[i][j] = Decimal(d)
dists[j][i] = Decimal(d)
return dists
def calculate_distances_np(positions):
positions = np.array(positions)
diffs = positions[:, np.newaxis] - positions[np.newaxis, :]
dists = np.linalg.norm(diffs, axis=-1)
np.fill_diagonal(dists, 0)
return dists
def print_progress_bar(iteration, total, start_time, length=50):
"""Prints a progress bar to the console."""
percent = (iteration / total) * 100
filled_length = int(length * iteration // total)
bar = '#' * filled_length + '-' * (length - filled_length)
sys.stdout.write(f'\r[{bar}] {percent:.2f}% {int(iteration/(time()-start_time))} steps/s')
sys.stdout.flush()
def format_sig_figs(value, sig_figs):
"""Format a number to a specified number of significant figures for printing."""
if value == 0:
return "0"
return f"{value:.{sig_figs-1}e}"
def plot_points_terminal(vectors, stdscr, scale=500000, grid_width=30, grid_height=30):
"""Plots multiple points in the terminal, scaled and centered at (0,0)."""
stdscr.clear()
if not vectors:
stdscr.addstr(0, 0, "No vectors provided.")
stdscr.refresh()
return
# Apply scaling
scaled_vectors = {(round(vec[0] / scale), round(vec[1] / scale)) for vec in vectors}
# Find min and max coordinates
min_x = min(vec[0] for vec in scaled_vectors)
max_x = max(vec[0] for vec in scaled_vectors)
min_y = min(vec[1] for vec in scaled_vectors)
max_y = max(vec[1] for vec in scaled_vectors)
# Center offsets to keep (0,0) in middle
center_x = (grid_width // 2) - min_x
center_y = (grid_height // 2) - min_y
# Adjust coordinates for plotting
adjusted_vectors = {(vec[0] + center_x, vec[1] + center_y) for vec in scaled_vectors}
# Ensure grid boundaries
max_terminal_y, max_terminal_x = stdscr.getmaxyx()
max_x = min(grid_width, max_terminal_x - 5)
max_y = min(grid_height, max_terminal_y - 5)
# Draw grid with points
for i in range(grid_height, -1, -1):
row = f"{i - center_y:2} | "
for j in range(grid_width + 1):
row += "" if (j, i) in adjusted_vectors else ". "
stdscr.addstr(max_y - i, 0, row[:max_terminal_x - 1]) # Ensure no overflow
# Print X-axis labels
x_labels = " " + " ".join(f"{j - center_x:2}" for j in range(max_x + 1))
stdscr.addstr(max_y + 1, 0, x_labels[:max_terminal_x - 1]) # Avoid out-of-bounds error
stdscr.refresh()