Skip to content

Commit

Permalink
MAINT: simplify Fraction construction and notation
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Nov 18, 2024
1 parent 3e5ed8c commit c9ad862
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/qrules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def check_reaction_violations( # noqa: C901, PLR0917
mass_conservation_factor: float | None = 3.0,
particle_db: ParticleCollection | None = None,
max_angular_momentum: int = 1,
max_spin_magnitude: float | Fraction = Fraction(2, 1),
max_spin_magnitude: float | Fraction = 2,
) -> set[frozenset[str]]:
"""Determine violated interaction rules for a given particle reaction.
Expand Down Expand Up @@ -213,7 +213,7 @@ def check_edge_qn_conservation() -> set[frozenset[str]]:
InteractionProperties(l_magnitude=l_magnitude, s_magnitude=s_magnitude)
for l_magnitude, s_magnitude in product(
_int_domain(0, max_angular_momentum),
_halves_domain(Fraction(0, 1), Fraction(max_spin_magnitude)),
_halves_domain(Fraction(0), Fraction(max_spin_magnitude)),
)
]

Expand Down Expand Up @@ -278,7 +278,7 @@ def generate_transitions( # noqa: PLR0917
particle_db: ParticleCollection | None = None,
mass_conservation_factor: float | None = 3.0,
max_angular_momentum: int = 2,
max_spin_magnitude: float | Fraction = Fraction(2, 1),
max_spin_magnitude: float | Fraction = 2,
topology_building: str = "isobar",
number_of_threads: int | None = None,
) -> ReactionInfo:
Expand Down Expand Up @@ -366,7 +366,7 @@ def generate_transitions( # noqa: PLR0917
formalism=formalism,
mass_conservation_factor=mass_conservation_factor,
max_angular_momentum=max_angular_momentum,
max_spin_magnitude=Fraction(max_spin_magnitude),
max_spin_magnitude=max_spin_magnitude,
topology_building=topology_building,
number_of_threads=number_of_threads,
)
Expand Down
4 changes: 2 additions & 2 deletions src/qrules/combinatorics.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ def fill_spin_projections(state: StateDefinition) -> StateWithSpins:
particle_name = state
particle = particle_db[particle_name]
spin_projections = set(arange(-particle.spin, particle.spin + 1))
if particle.mass == 0.0 and Fraction(0, 1) in spin_projections:
spin_projections.remove(Fraction(0, 1))
if particle.mass == 0.0 and Fraction(0) in spin_projections:
spin_projections.remove(Fraction(0))
return particle_name, sorted(spin_projections)
return state

Expand Down
2 changes: 1 addition & 1 deletion src/qrules/conservation_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ def calculate_hypercharge(
or edge_qns.tau_lepton_number
):
return True
isospin_3 = Fraction(0, 1)
isospin_3 = Fraction(0)
if edge_qns.isospin_projection:
isospin_3 = edge_qns.isospin_projection
return float(edge_qns.charge) == isospin_3 + 0.5 * calculate_hypercharge(edge_qns)
Expand Down
4 changes: 2 additions & 2 deletions src/qrules/particle.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ def _validate_fraction_for_spin(
attribute: Attribute, # noqa: ARG001
value: Fraction, # noqa: ARG001
) -> Any:
if instance.magnitude % Fraction(1, 2) != Fraction(0, 1):
if instance.magnitude % Fraction(1, 2) != Fraction(0):
msg = f"Spin magnitude {instance.magnitude} has to be a multitude of 0.5"
raise ValueError(msg)
if abs(instance.projection) > instance.magnitude:
if instance.magnitude < Fraction(0, 1):
if instance.magnitude < Fraction(0):
msg = f"Spin magnitude has to be positive, but is {instance.magnitude}"
raise ValueError(msg)
msg = (
Expand Down
2 changes: 1 addition & 1 deletion src/qrules/quantum_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class InteractionProperties:


def arange(
x_1: Fraction, x_2: Fraction, delta: Fraction = Fraction(1, 1)
x_1: Fraction, x_2: Fraction, delta: Fraction = Fraction(1)
) -> Generator[Fraction, None, None]:
current = Fraction(x_1)
delta = Fraction(delta)
Expand Down
8 changes: 4 additions & 4 deletions src/qrules/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def create_interaction_settings( # noqa: PLR0917
nbody_topology: bool = False,
mass_conservation_factor: float | None = 3.0,
max_angular_momentum: int = 2,
max_spin_magnitude: float | Fraction = Fraction(2, 1),
max_spin_magnitude: float | Fraction = 2,
) -> dict[InteractionType, tuple[EdgeSettings, NodeSettings]]:
"""Create a container that holds the settings for `.InteractionType`."""
formalism_edge_settings = EdgeSettings(
Expand Down Expand Up @@ -246,8 +246,8 @@ def __get_spin_magnitudes(
is_nbody: bool, max_spin_magnitude: Fraction
) -> list[Fraction]:
if is_nbody:
return [Fraction(0, 1)]
return _halves_domain(Fraction(0, 1), max_spin_magnitude)
return [Fraction(0)]
return _halves_domain(Fraction(0), max_spin_magnitude)


def _create_domains(particle_db: ParticleCollection) -> dict[Any, list]:
Expand Down Expand Up @@ -308,7 +308,7 @@ def __positive_halves_domain(
particle_db: ParticleCollection, attr_getter: Callable[[Particle], Any]
) -> list[Fraction]:
values = set(map(attr_getter, particle_db))
return _halves_domain(Fraction(0, 1), max(values))
return _halves_domain(Fraction(0), max(values))


def __positive_int_domain(
Expand Down
2 changes: 1 addition & 1 deletion src/qrules/transition.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def __init__( # noqa: C901, PLR0912, PLR0917
reload_pdg: bool = False,
mass_conservation_factor: float | None = 3.0,
max_angular_momentum: int = 1,
max_spin_magnitude: float | Fraction = Fraction(2, 1),
max_spin_magnitude: float | Fraction = 2,
number_of_threads: int | None = None,
) -> None:
if number_of_threads is not None:
Expand Down

0 comments on commit c9ad862

Please sign in to comment.