/
bari
/
HPLSE-poster-code
Обзор
Документация
Войти
/
bari
/
HPLSE-poster-code
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
Репозиторий помещен в архив 16 июня 2026. Вся функциональность доступна только для чтения.
main
helpinn/utils.py
83 строки
2 KB
bari
Implement core helpinn library and solvers
12 апр 2026, 23:59
12 апр 2026, 23:59
d37f0de
Код
Авторство
О чём код?
"""Shared numerical and plotting helpers.""" from __future__ import annotations import os import tempfile import numpy as np _FALLBACK_STYLES = { "helpinn-science": { "axes.grid": True, "grid.alpha": 0.25, "axes.spines.top": False, "axes.spines.right": False, "figure.dpi": 120, "savefig.dpi": 300, "font.size": 11, }, "helpinn-no-latex": { "text.usetex": False, }, } def safe_divide(numerator: np.ndarray, denominator: np.ndarray, default: float | complex = 0.0) -> np.ndarray: """Divide two arrays and replace invalid values with a default.""" numerator = np.asarray(numerator) denominator = np.asarray(denominator) dtype = np.result_type(numerator, denominator, default) result = np.full(np.broadcast(numerator, denominator).shape, default, dtype=dtype) np.divide(numerator, denominator, out=result, where=denominator != 0) return result def as_complex_array(values: object) -> np.ndarray: """Convert values to a one-dimensional complex NumPy array.""" array = np.asarray(values, dtype=complex) if array.ndim != 1: raise ValueError("expected a one-dimensional array") return array def as_real_array(values: object) -> np.ndarray: """Convert values to a one-dimensional real NumPy array.""" array = np.asarray(values, dtype=float) if array.ndim != 1: raise ValueError("expected a one-dimensional array") return array def register_fallback_styles() -> None: """Register the local plotting styles with Matplotlib.""" os.environ.setdefault("MPLCONFIGDIR", tempfile.gettempdir()) from matplotlib import style as mpl_style mpl_style.library.update(_FALLBACK_STYLES) def use_science_style() -> None: """Apply a scientific plotting style.""" try: os.environ.setdefault("MPLCONFIGDIR", tempfile.gettempdir()) import matplotlib.pyplot as plt try: import scienceplots # noqa: F401 plt.style.use(["science", "no-latex"]) return except Exception: register_fallback_styles() plt.style.use(["helpinn-science", "helpinn-no-latex"]) except Exception: return def new_figure(): """Create a Matplotlib figure and axis.""" use_science_style() import matplotlib.pyplot as plt return plt.subplots()