Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(numpy): #87 upgrade #92

Merged
merged 4 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions hamilflow/maths/trigonometrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def acos_with_shift(
shift: "Collection[float] | npt.ArrayLike | None" = None,
) -> "npt.ArrayLike":
"""Arccos with shift."""
x = np.array(x, copy=False)
x = np.asarray(x)
value = np.arccos(x)
shift = np.array(shift, copy=False)
shift = np.asarray(shift)
period_shift = (div := np.floor(shift)) * 2 * np.pi
remainder = shift - div
value = np.where(remainder <= 0.5, value, 2 * np.pi - value)
Expand Down
2 changes: 1 addition & 1 deletion hamilflow/models/brownian_motion.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def __init__(
@property
def dim(self) -> int:
"""Dimension of the Brownian motion."""
return np.array(self.initial_condition.x0, copy=False).size
return np.asarray(self.initial_condition.x0).size

@property
def _axis_names(self) -> list[str]:
Expand Down
6 changes: 3 additions & 3 deletions hamilflow/models/free_particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ def definition(self) -> dict[str, dict[str, float | list[float]]]:
return dict(initial_condition=self.initial_condition.model_dump())

def _x(self, t: "Sequence[float] | npt.ArrayLike") -> "npt.NDArray[np.float64]":
t = np.array(t, copy=False)
v0 = np.array(self.initial_condition.v0, copy=False)
x0 = np.array(self.initial_condition.x0, copy=False)
t = np.asarray(t)
v0 = np.asarray(self.initial_condition.v0)
x0 = np.asarray(self.initial_condition.x0)
return np.outer(t, v0) + x0

def __call__(self, t: "Sequence[float] | npt.ArrayLike") -> pd.DataFrame:
Expand Down
14 changes: 7 additions & 7 deletions hamilflow/models/harmonic_oscillator.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def _x(self, t: "Sequence[float] | npt.ArrayLike") -> np.ndarray:
$$
"""
return self.initial_condition.x0 * np.cos(
self.system.omega * np.array(t, copy=False) + self.initial_condition.phi,
self.system.omega * np.asarray(t) + self.initial_condition.phi,
)


Expand Down Expand Up @@ -248,7 +248,7 @@ def _x_under_damped(self, t: "Sequence[float] | npt.ArrayLike") -> npt.ArrayLike
\Omega = \omega\sqrt{ 1 - \zeta^2}.
$$
"""
t = np.array(t, copy=False)
t = np.asarray(t)
omega_damp = self.system.omega * np.sqrt(1 - self.system.zeta)
return (
self.initial_condition.x0 * np.cos(omega_damp * t)
Expand All @@ -274,7 +274,7 @@ def _x_critical_damped(self, t: "Sequence[float] | npt.ArrayLike") -> npt.ArrayL
\Omega = \omega\sqrt{ 1 - \zeta^2}.
$$
"""
t = np.array(t, copy=False)
t = np.asarray(t)
return self.initial_condition.x0 * np.exp(
-self.system.zeta * self.system.omega * t,
)
Expand All @@ -293,7 +293,7 @@ def _x_over_damped(self, t: "Sequence[float] | npt.ArrayLike") -> npt.ArrayLike:
\Gamma = \omega\sqrt{ \zeta^2 - 1 }.
$$
"""
t = np.array(t, copy=False)
t = np.asarray(t)
gamma_damp = self.system.omega * np.sqrt(self.system.zeta - 1)

return (
Expand All @@ -308,7 +308,7 @@ def _x_over_damped(self, t: "Sequence[float] | npt.ArrayLike") -> npt.ArrayLike:

def _x(self, t: "Sequence[float] | npt.ArrayLike") -> npt.ArrayLike:
r"""Solution to damped harmonic oscillators."""
t = np.array(t, copy=False)
t = np.asarray(t)
if self.system.type == "under_damped":
x = self._x_under_damped(t)
elif self.system.type == "over_damped":
Expand Down Expand Up @@ -372,7 +372,7 @@ def _z(self, t: "Sequence[float] | npt.ArrayLike") -> npt.ArrayLike:
x(t) = x_+ \exp(-\mathbb{i} (\omega t + \phi_+)) + x_- \exp(+\mathbb{i} (\omega t + \phi_-)).
$$
"""
t = np.array(t, copy=False)
t = np.asarray(t)
omega = self.system.omega
x0, phi = self.initial_condition.x0, self.initial_condition.phi
phases = -omega * t - phi[0], omega * t + phi[1]
Expand All @@ -385,7 +385,7 @@ def __call__(self, t: "Sequence[float] | npt.ArrayLike") -> pd.DataFrame:

:param t: time(s).
"""
t = np.array(t, copy=False)
t = np.asarray(t)
data = self._z(t)

return pd.DataFrame({"t": t, "z": data})
7 changes: 3 additions & 4 deletions hamilflow/models/harmonic_oscillator_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,12 @@ def _z(
self,
t: "Sequence[float] | npt.ArrayLike",
) -> "tuple[npt.NDArray[np.complex64], npt.NDArray[np.complex64]]":
t = np.array(t, copy=False).reshape(-1)
t = np.asarray(t).reshape(-1)
all_travelling_waves = [self.free_mode._x(t).reshape(1, -1)]

if self.independent_csho_modes:
independent_cshos = np.array(
independent_cshos = np.asarray(
[o._z(t) for o in self.independent_csho_modes],
copy=False,
)
all_travelling_waves.extend(
(
Expand Down Expand Up @@ -135,7 +134,7 @@ def __call__(self, t: "Sequence[float] | npt.ArrayLike") -> pd.DataFrame:

:param t: time.
"""
t = np.array(t, copy=False)
t = np.asarray(t)
original_xs, travelling_waves = self._x(t)
data = { # type: ignore [var-annotated]
f"{name}{i}": cast(
Expand Down
12 changes: 6 additions & 6 deletions hamilflow/models/kepler_problem/dynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ def tau_of_u_parabolic(ecc: float, u: "npt.ArrayLike") -> "npt.NDArray[np.float6
:param u: convenient radial inverse
:return: scaled time tau
"""
u = np.array(u, copy=False)
u = np.asarray(u)
return np.sqrt(1 - u) * (2 + u) / 3 / (1 + u) ** 1.5


def _tau_of_u_exact_hyperbolic(
ecc: float,
u: "npt.ArrayLike",
) -> "npt.NDArray[np.float64]":
u = np.array(u, copy=False)
u = np.asarray(u)
cosqr, eusqrt = ecc**2 - 1, np.sqrt(ecc**2 - u**2)
trig_numer = np.arctanh(np.sqrt(cosqr) * eusqrt / (ecc**2 + u))
return eusqrt / cosqr / (1 + u) - trig_numer / cosqr**1.5
Expand Down Expand Up @@ -117,7 +117,7 @@ def tau_of_u_prime(ecc: float, u: "npt.ArrayLike") -> "npt.NDArray[np.float64]":
:param u: convenient radial inverse
:return: the first derivative scaled time tau with respect to u
"""
u = np.array(u, copy=False)
u = np.asarray(u)
return -1 / (1 + u) ** 2 / np.sqrt(ecc**2 - u**2)


Expand All @@ -128,7 +128,7 @@ def tau_of_u_prime2(ecc: float, u: "npt.ArrayLike") -> "npt.NDArray[np.float64]"
:param u: convenient radial inverse
:return: the second derivative scaled time tau with respect to u
"""
u = np.array(u, copy=False)
u = np.asarray(u)
u2 = u**2
return (2 * ecc**2 - u - 3 * u2) / (1 + u) ** 3 / (ecc**2 - u2) ** 1.5

Expand All @@ -143,7 +143,7 @@ def esolve_u_from_tau_parabolic(
:param tau: scaled time
:return: convenient radial inverse u
"""
tau = np.array(tau, copy=False)
tau = np.asarray(tau)
tau_3 = 3 * tau
term = 1 + tau_3**2 # 1 + 9 * tau**2
term1_5 = term**1.5 # (1 + 9 * tau**2)**1.5
Expand All @@ -159,7 +159,7 @@ def _approximate_at_termina(
left: "Callable[[float, npt.NDArray[np.float64]], npt.NDArray[np.float64]]",
right: "Callable[[float, npt.NDArray[np.float64]], npt.NDArray[np.float64]]",
):
u = np.array(u, copy=False)
u = np.asarray(u)
u_s = u.reshape(-1)
res = exact(ecc, u_s)
mask = np.isnan(res)
Expand Down
8 changes: 4 additions & 4 deletions hamilflow/models/kepler_problem/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ def t_to_tau_factor(self) -> float:

def tau(self, t: "Collection[float] | npt.ArrayLike") -> "npt.ArrayLike":
"""Give the scaled time tau from t."""
return (np.array(t, copy=False) - self.t0) * self.t_to_tau_factor
return (np.asarray(t) - self.t0) * self.t_to_tau_factor

def u_of_tau(self, tau: "Collection[float] | npt.ArrayLike") -> "npt.ArrayLike":
"""Give the convenient radial inverse u from tau."""
tau = np.array(tau, copy=False)
tau = np.asarray(tau)
if self.ecc == 0:
return np.zeros(tau.shape)
else:
Expand All @@ -226,15 +226,15 @@ def u_of_tau(self, tau: "Collection[float] | npt.ArrayLike") -> "npt.ArrayLike":

def r_of_u(self, u: "Collection[float] | npt.ArrayLike") -> "npt.ArrayLike":
"""Give the radial r from u."""
return self.parameter / (np.array(u, copy=False) + 1)
return self.parameter / (np.asarray(u) + 1)

def phi_of_u_tau(
self,
u: "Collection[float] | npt.ArrayLike",
tau: "Collection[float] | npt.ArrayLike",
) -> "npt.ArrayLike":
"""Give the angular phi from u and tau."""
u, tau = np.array(u, copy=False), np.array(tau, copy=False)
u, tau = np.asarray(u), np.asarray(tau)
if self.ecc == 0:
phi = 2 * math.pi * tau / self.period_in_tau
else:
Expand Down
6 changes: 3 additions & 3 deletions hamilflow/models/kepler_problem/numerics.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def nsolve_u_from_tau_newton(ecc: float, tau: "npt.ArrayLike") -> OptimizeResult
:raises ValueError: when `ecc` is invalid
:return: numeric OptimizeResult from scipy
"""
tau = np.array(tau, copy=False)
tau = np.asarray(tau)
if 0 < ecc < 1:
tau_of_u = tau_of_u_elliptic
u0 = _u0_elliptic(ecc, tau)
Expand Down Expand Up @@ -72,7 +72,7 @@ def nsolve_u_from_tau_bisect(ecc: float, tau: "npt.ArrayLike") -> list[OptimizeR
:param tau: scaled time
:return: numeric OptimizeResult from scipy
"""
tau_s = np.array(tau, copy=False).reshape(-1)
tau_s = np.asarray(tau).reshape(-1)
if 0 < ecc < 1:
tau_of_u = tau_of_u_elliptic
elif ecc > 1:
Expand Down Expand Up @@ -102,7 +102,7 @@ def u_of_tau(
:raises ValueError: when `ecc` is invalid
:return: convenient radial inverse u
"""
tau = np.array(tau, copy=False)
tau = np.asarray(tau)
if ecc == 0:
return np.zeros(tau.shape)
elif ecc == 1:
Expand Down
2 changes: 1 addition & 1 deletion hamilflow/models/pendulum.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def _math_u(
self,
t: "Sequence[float] | npt.ArrayLike",
) -> "npt.NDArray[np.float64]":
return self.omega0 * np.array(t, copy=False)
return self.omega0 * np.asarray(t)

def u(self, t: "Sequence[float] | npt.ArrayLike") -> "npt.NDArray[np.float64]":
r"""Give the convenient generalised coordinate $u$, $\sin u \coloneqq \frac{\sin\frac{\theta}{2}}{\sin\frac{\theta_0}{2}}$.
Expand Down
Loading