Energy to Beta
The pyamtrack.converters.beta_from_energy function calculates the relativistic speed (beta) of a particle based on its energy per nucleon.
Function Purpose
The pyamtrack.converters.beta_from_energy function computes the relative speed of a particle (beta = v/c), where v is the particle's velocity and c is the speed of light.
Input Parameters
input: The energy of the particle per nucleon in MeV/u. The function accepts the following types:- A single value as a
floatorint. - A
numpy.ndarrayof energy values. - A Python
listof energy values.
- A single value as a
Output
- The function returns the calculated beta value(s) as:
- A
floatfor a single input value. - A
numpy.ndarrayfor a NumPy array input. - A Python
listfor a list input.
- A
Notes
- The input energy must be non-negative. Negative energy values are invalid and will cause
np.nanto be returned. - The function supports both scalar and vectorized operations, making it efficient for batch calculations.
Example Usage
Single Value Input
import pyamtrack
energy = 150.0 # MeV/u
beta = pyamtrack.converters.beta_from_energy(energy)
print(f"Beta: {beta}")
NumPy Array Input
import pyamtrack
import numpy as np
energies = np.array([10.0, 50.0, 100.0, 500.0]) # MeV/u
betas = pyamtrack.converters.beta_from_energy(energies)
print(f"Betas: {betas}")
Python List Input
import pyamtrack
energies = [10.0, 50.0, 100.0, 500.0] # MeV/u
betas = pyamtrack.converters.beta_from_energy(energies)
print(f"Betas: {betas}")
Edge Cases
- Zero Energy:
beta = pyamtrack.converters.beta_from_energy(0.0)
print(f"Beta for zero energy: {beta}") # Output: 0.0 - Empty Input:
betas = pyamtrack.converters.beta_from_energy([])
print(f"Beta for empty input: {betas}") # Output: []
Error Handling
- If the input is not a valid type (
float,int,numpy.ndarray, orlist), the function raises aValueErrororRuntimeError. - Example:
try:
beta = pyamtrack.converters.beta_from_energy("invalid")
except ValueError as e:
print(f"Error: {e}")