helpers¶
The helpers module provides a collection of utility functions for physics calculations, particularly related to relativistic particle dynamics and root finding.
This module includes functions for calculating:
The Lorentz factor \(\gamma\) (calc_lorentz_factor).
The momentum of a particle (calc_momentum).
Filtering real and positive roots from a set of roots (get_real_and_positive_roots).
These functions are intended to be used within other modules of the package.
- piran.helpers.calc_lorentz_factor(
- E: Annotated[Quantity, Unit('J')],
- m: Annotated[Quantity, Unit('kg')],
Calculate the Lorentz factor (\(\gamma\)) from the relativistic kinetic energy (\(E_k\)) and rest mass (\(m_0\)).
The Lorentz factor is calculated as:
\[\gamma = 1 + \frac{E_k}{m_0 c^2}\]where \(c\) is the speed of light.
This function calculates the Lorentz factor using the relativistic kinetic energy, which is defined as the difference between the total relativistic energy and the rest mass energy:
\[E_k = E_t - E_0 = (\gamma - 1)m_0 c^2\]- Parameters:
- Eastropy.units.Quantity[u.Joule]
The relativistic kinetic energy of the particle. Must have units convertible to Joules.
- mastropy.units.Quantity[u.kg]
The rest mass of the particle. Must have units convertible to kilograms.
- Returns:
- astropy.units.Quantity[u.dimensionless_unscaled]
The Lorentz factor \(\gamma\), a dimensionless quantity.
See also
plasmapy.formulary.relativity.Lorentz_factor
The standard Lorentz factor calculation using velocity.
Notes
This calculation differs from the “standard” calculation of the Lorentz factor using velocity \(v\):
\[\gamma = \frac{1}{\sqrt{1 - v^2/c^2}}\]as implemented in packages like plasmapy. This function is specifically designed for cases where the relativistic kinetic energy is known.
Examples
>>> import astropy.units as u >>> energy = 1.0 * u.MeV >>> mass = 9.1093837015e-31 * u.kg >>> gamma = calc_lorentz_factor(energy, mass)
- piran.helpers.calc_momentum(
- gamma: Annotated[Quantity, Unit(dimensionless)],
- mass: Annotated[Quantity, Unit('kg')],
Calculate the relativistic momentum of a particle given its Lorentz factor (\(\gamma\)) and rest mass (\(m_0\)).
The relativistic momentum (\(p\)) is given by:
\[p = m_0c\sqrt{\gamma^2 - 1}\]where \(c\) is the speed of light, as shown in Glauert & Horne (2005), Equation 18.
- Parameters:
- gammaastropy.units.Quantity[u.dimensionless_unscaled]
The Lorentz factor, a dimensionless quantity.
- massastropy.units.Quantity[u.kg]
The rest mass of the particle. Must have units convertible to kilograms.
- Returns:
- astropy.units.Quantity[u.kg * u.m / u.s]
The relativistic momentum of the particle, with units of kg m/s.
Examples
>>> import astropy.units as u >>> gamma = 2.9569511835738735 >>> mass = 9.1093837015e-31 << u.kg >>> momentum = calc_momentum(gamma, mass)
- piran.helpers.get_real_and_positive_roots(values: ndarray, tol=1e-08) ndarray ¶
Filter a sequence of values (real or complex), returning only the real and positive values that are greater than the specified tolerance.
Complex numbers with imaginary parts close to zero are treated as real numbers, and their real parts are included in the result if they meet the other criteria (positive and greater than tol).
- Parameters:
- valuesnumpy.ndarray
A NumPy array containing the values to be filtered.
- tolfloat, optional
The tolerance below which values are considered zero and thus excluded. We compare only the real part if the input is a complex number. Defaults to 1e-8.
- Returns:
- numpy.ndarray
A NumPy array containing only the real and positive values from the input that are greater than tol. Returns an empty NumPy array if no values meet the criteria.
Notes
This function uses numpy.isclose with the default tolerances to determine if a value (the imaginary part) is close to zero. See the documentation of numpy.isclose for details on how the comparison is performed.
Examples
>>> values = np.array([-1 + 0j, 1.1 + 0.00000001j, 100 + 2j]) >>> valid_roots = get_real_and_positive_roots(values) >>> print(valid_roots) [1.1]