ParticleListο
- class plasmapy.particles.particle_collections.ParticleList(particles: Iterable | None = None)[source]ο
Bases:
UserListA
listlike collection ofParticleandCustomParticleobjects.- Parameters:
particles (iterable of particle-like, optional) β An iterable that provides a sequence of particle-like objects. Objects that are not a
ParticleorCustomParticlewill be cast into aParticleorCustomParticle. If not provided, an emptyParticleListwill be created.- Raises:
InvalidParticleError β If an object supplied to
ParticleListis not particle-like.TypeError β If a
DimensionlessParticleis provided.
See also
Examples
A
ParticleListcan be created by calling it with an iterable like alistortuplethat provides particle-like objects.>>> from plasmapy.particles import ParticleList >>> particle_list = ParticleList(["e-", "e+"]) >>> particle_list[0] Particle("e-")
Attributes such as
massandchargewill return aQuantityarray containing the values of the corresponding attribute for each particle in theParticleList.>>> particle_list.mass <Quantity [9.1093...e-31, 9.1093...e-31] kg> >>> particle_list.charge <Quantity [-1.60217663e-19, 1.60217663e-19] C> >>> particle_list.symbols ['e-', 'e+']
ParticleListinstances can also be created through addition and multiplication withParticle,CustomParticle, andParticleListinstances.>>> from plasmapy.particles import Particle, CustomParticle >>> import astropy.units as u >>> proton = Particle("p+") >>> custom_particle = CustomParticle(mass=1e-26 * u.kg, charge=6e-19 * u.C) >>> 2 * proton + custom_particle ParticleList(['p+', 'p+', 'CustomParticle(mass=1e-26 kg, charge=6e-19 C)'])
These operations may also be performed using particle-like objects.
>>> particle_list + "deuteron" ParticleList(['e-', 'e+', 'D 1+'])
A
ParticleListcan also be created usingQuantityobjects.>>> import astropy.units as u >>> quantities = [2.3e-26 * u.kg, 4.8e-19 * u.C] >>> particle_list = ParticleList(quantities) >>> particle_list.mass <Quantity [2.3e-26, nan] kg> >>> particle_list.charge <Quantity [ nan, 4.8e-19] C>
Normal
listmethods may also be used onParticleListobjects. When a particle-like object is appended to aParticleList, that object will be cast into aParticleorCustomParticle.>>> noble_gases = ParticleList(["He", "Ar", "Kr", "Xe", "Rn"]) >>> noble_gases.append("Og") >>> noble_gases[-1] Particle("Og")
The
>operator may be used withParticleandParticleListinstances to access the nuclear reaction energy.>>> reactants = ParticleList(["deuterium", "tritium"]) >>> products = ParticleList(["alpha", "neutron"]) >>> energy = reactants > products >>> energy.to("MeV") <Quantity 17.58925... MeV>
Attributes Summary
The electric charges of the particles.
The charges of the particles in units of the elementary charge.
A
listcontaining the particles contained in theParticleListinstance.The half-lives of the particles.
The masses of the particles.
The mass energies of the particles.
A
listof the symbols of the particles.Methods Summary
append(particle)Append a particle to the end of the
ParticleList.average_particle([abundances,Β ...])Return a particle with the average mass and charge.
clear()Remove all items from the
ParticleList.copy()Return a shallow copy of the
ParticleList.count(item)Return the number of occurrences of
item.extend(iterable)Extend
ParticleListby casting particle-like items fromiterableinto particle objects.index(item,Β *args)Return first index of a particle-like value.
insert(index,Β particle)Insert a particle before an index.
Determine if the particles in the
ParticleListmeet categorization criteria.pop([i])Remove and return item at index (default last).
remove(item)Remove the first occurrence of a particle-like item.
reverse()Reverse the
ParticleListin place.sort([key,Β reverse])Sort the
ParticleListin-place.Attributes Documentation
- charge_numberο
The charges of the particles in units of the elementary charge.
- Return type:
- dataο
A
listcontaining the particles contained in theParticleListinstance.Important
This attribute should not be modified directly.
- Return type:
listofParticleorCustomParticle
- mass_energyο
The mass energies of the particles.
If the particle is an isotope or nuclide, return the mass energy of the nucleus only.
- Return type:
Methods Documentation
- append( ) None[source]ο
Append a particle to the end of the
ParticleList.
- average_particle( ) CustomParticle | Particle[source]ο
Return a particle with the average mass and charge.
By default, the mean will be used as the average. If the
abundancesare provided, then this method will return the weighted mean. Ifuse_rms_chargeoruse_rms_massisTrue, then this method will return the root-mean-square of the charge or mass, respectively. If all items in theParticleListare equal to each other, then this method will return the first item in theParticleList.- Parameters:
abundances (array_like, optional) β Real numbers representing relative abundances of the particles in the
ParticleList. Must have the same number of elements as theParticleList. This parameter gets passed tonumpy.averagevia that functionβsweightsparameter. If not provided, the particles contained in theParticleListare assumed to be equally abundant.use_rms_charge (
bool, keyword-only, default:False) β IfTrue, use the root-mean-square charge instead of the mean charge.use_rms_mass (
bool, keyword-only, default:False) β IfTrue, use the root-mean-square mass instead of the mean mass.
- Return type:
Examples
>>> reactants = ParticleList(["electron", "positron"]) >>> reactants.average_particle() CustomParticle(mass=9.109383...e-31 kg, charge=0.0 C) >>> reactants.average_particle(abundances=[1, 0.5]) CustomParticle(mass=9.109383...e-31 kg, charge=-5.34058...e-20 C) >>> reactants.average_particle(use_rms_charge=True) CustomParticle(mass=9.109383...e-31 kg, charge=1.6021766...-19 C) >>> protons = ParticleList(["p+", "p+", "p+"]) >>> protons.average_particle() Particle("p+")
- clear()ο
Remove all items from the
ParticleList.
- copy()ο
Return a shallow copy of the
ParticleList.
- count(item)ο
Return the number of occurrences of
item. Here,itemmust be particle-like.
- extend( ) None[source]ο
Extend
ParticleListby casting particle-like items fromiterableinto particle objects.
- index(item, *args)ο
Return first index of a particle-like value.
- Raises:
ValueError β If the value is not present.
- is_category(
- *category_tuple,
- require: str | Iterable[str] | None = None,
- any_of: str | Iterable[str] | None = None,
- exclude: str | Iterable[str] | None = None,
- particlewise: Literal[True] = False,
- is_category(
- *category_tuple,
- require: str | Iterable[str] | None = None,
- any_of: str | Iterable[str] | None = None,
- exclude: str | Iterable[str] | None = None,
- particlewise: Literal[False],
Determine if the particles in the
ParticleListmeet categorization criteria.Please refer to the documentation of
is_categoryfor information on the parameters and categories, as well as more extensive examples.- Parameters:
particlewise (
bool, default:False) β IfTrue, return alistofboolin which an element will beTrueif the corresponding particle is consistent with the categorization criteria, andFalseotherwise. IfFalse, return aboolwhich will beTrueif all particles are consistent with the categorization criteria andFalseotherwise.- Return type:
See also
Examples
>>> particles = ParticleList(["proton", "electron", "tau neutrino"]) >>> particles.is_category("lepton") False >>> particles.is_category("lepton", particlewise=True) [False, True, True] >>> particles.is_category(require="lepton", exclude="neutrino") False >>> particles.is_category( ... require="lepton", exclude="neutrino", particlewise=True ... ) [False, True, False] >>> particles.is_category(any_of=["lepton", "charged"]) True >>> particles.is_category(any_of=["lepton", "charged"], particlewise=True) [True, True, True]
- pop(i=-1)ο
Remove and return item at index (default last).
- Raises:
IndexError β If the
ParticleListis empty or the index is out of range.
- remove(item)ο
Remove the first occurrence of a particle-like item.
- Raises:
ValueError β If the value is not present.
- reverse()ο
Reverse the
ParticleListin place.
- sort(key: Callable | None = None, reverse: bool = False)[source]ο
Sort the
ParticleListin-place.- Parameters:
key (callable) β A function that accepts one argument that is used to extract a comparison key for each item in the
ParticleList.reverse (
bool, default:False) β IfTrue, the items in theParticleListare sorted as if each comparison were reversed.
- Raises:
TypeError β If
keyis not provided.
Examples
To sort a
ParticleListby atomic number, setkeytoatomic_number().>>> from plasmapy.particles import ParticleList, atomic_number >>> elements = ParticleList(["Li", "H", "He"]) >>> elements.sort(key=atomic_number) >>> print(elements) ParticleList(['H', 'He', 'Li'])
We can also create a function to pass to
key. In this example, we sort first by atomic number and second by mass number using different attributes ofParticle.>>> def sort_key(isotope): ... return isotope.atomic_number, isotope.mass_number >>> isotopes = ParticleList(["He-3", "T", "H-1", "He-4", "D"]) >>> isotopes.sort(key=sort_key) >>> print(isotopes) ParticleList(['H-1', 'D', 'T', 'He-3', 'He-4'])