Skip to content

Commit

Permalink
start addressing warnings from clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Radonirinaunimi committed Sep 30, 2024
1 parent 6d11bd1 commit 9fbb246
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 28 deletions.
12 changes: 6 additions & 6 deletions pineappl/src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,16 +806,16 @@ mod test {
]))
.unwrap();

assert_eq!(limits.left(), 0.0);
assert_eq!(limits.right(), 2.0);
assert!((limits.left() - 0.0).abs() < f64::EPSILON);
assert!((limits.right() - 2.0).abs() < f64::EPSILON);
assert_eq!(limits.bins(), 6);

let non_consecutive_bins = BinLimits::new(vec![3.0, 4.0]);

assert!(limits.merge(&non_consecutive_bins).is_err());

assert_eq!(limits.left(), 0.0);
assert_eq!(limits.right(), 2.0);
assert!((limits.left() - 0.0).abs() < f64::EPSILON);
assert!((limits.right() - 2.0).abs() < f64::EPSILON);
assert_eq!(limits.bins(), 6);

// left merge
Expand All @@ -828,8 +828,8 @@ mod test {
]))
.is_err());

assert_eq!(limits.left(), 0.0);
assert_eq!(limits.right(), 2.0);
assert!((limits.left() - 0.0).abs() < f64::EPSILON);
assert!((limits.right() - 2.0).abs() < f64::EPSILON);
assert_eq!(limits.bins(), 6);
}

Expand Down
8 changes: 4 additions & 4 deletions pineappl/src/convolutions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ impl<'a> LumiCache<'a> {
.map(|Mu2 { ren, .. }| {
self.mur2_grid
.iter()
.position(|&mur2| mur2 == xir * xir * ren)
.position(|&mur2| (mur2 - xir * xir * ren).abs() < f64::EPSILON)
.unwrap_or_else(|| unreachable!())
})
.collect();
Expand All @@ -316,7 +316,7 @@ impl<'a> LumiCache<'a> {
.map(|Mu2 { fac, .. }| {
self.muf2_grid
.iter()
.position(|&muf2| muf2 == xif * xif * fac)
.position(|&muf2| (muf2 - xif * xif * fac).abs() < f64::EPSILON)
.unwrap_or_else(|| unreachable!())
})
.collect();
Expand All @@ -325,7 +325,7 @@ impl<'a> LumiCache<'a> {
.map(|x1| {
self.x_grid
.iter()
.position(|x| x1 == x)
.position(|x| (x1 - x).abs() < f64::EPSILON)
.unwrap_or_else(|| unreachable!())
})
.collect();
Expand All @@ -335,7 +335,7 @@ impl<'a> LumiCache<'a> {
.map(|x2| {
self.x_grid
.iter()
.position(|x| x2 == x)
.position(|x| (x2 - x).abs() < f64::EPSILON)
.unwrap_or_else(|| unreachable!())
})
.collect();
Expand Down
2 changes: 1 addition & 1 deletion pineappl/src/empty_subgrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ mod tests {
#[test]
fn create_empty() {
let mut subgrid = EmptySubgridV1;
assert_eq!(subgrid.convolve(&[], &[], &[], &mut |_, _, _| 0.0), 0.0,);
assert!((subgrid.convolve(&[], &[], &[], &mut |_, _, _| 0.0) - 0.0).abs() < f64::EPSILON);
assert!(subgrid.is_empty());
subgrid.merge(&mut EmptySubgridV1.into(), false);
subgrid.scale(2.0);
Expand Down
4 changes: 2 additions & 2 deletions pineappl/src/fk_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,15 @@ impl TryFrom<Grid> for FkTable {

if muf2 < 0.0 {
muf2 = mu2_grid[0].fac;
} else if muf2 != mu2_grid[0].fac {
} else if (muf2 - mu2_grid[0].fac).abs() > f64::EPSILON {
return Err(TryFromGridError::MultipleScales);
}
}

for channel in grid.channels() {
let entry = channel.entry();

if entry.len() != 1 || entry[0].2 != 1.0 {
if entry.len() != 1 || (entry[0].2 - 1.0).abs() > f64::EPSILON {
return Err(TryFromGridError::InvalidChannel);
}
}
Expand Down
18 changes: 17 additions & 1 deletion pineappl/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ bitflags! {

/// Main data structure of `PineAPPL`. This structure contains a `Subgrid` for each `LumiEntry`,
/// bin, and coupling order it was created with.
#[allow(clippy::unsafe_derive_deserialize)]
#[derive(Clone, Deserialize, Serialize)]
pub struct Grid {
subgrids: Array3<SubgridEnum>,
Expand Down Expand Up @@ -257,6 +258,7 @@ impl Grid {
/// # Errors
///
/// If `subgrid_type` is none of the values listed above, an error is returned.
#[allow(clippy::needless_pass_by_value)]
pub fn with_subgrid_type(
channels: Vec<Channel>,
orders: Vec<Order>,
Expand Down Expand Up @@ -356,7 +358,9 @@ impl Grid {
for ((ord, bin, chan), subgrid) in self.subgrids.indexed_iter() {
let order = &self.orders[ord];

if ((order.logxir > 0) && (xir == 1.0)) || ((order.logxif > 0) && (xif == 1.0)) {
if ((order.logxir > 0) && (xir - 1.0).abs() < f64::EPSILON)
|| ((order.logxif > 0) && (xif - 1.0).abs() < f64::EPSILON)
{
continue;
}

Expand Down Expand Up @@ -841,6 +845,7 @@ impl Grid {
}

/// Set the convolution type for this grid for the corresponding `index`.
#[allow(clippy::needless_pass_by_value)]
pub fn set_convolution(&mut self, index: usize, convolution: Convolution) {
// remove outdated metadata
self.key_values_mut()
Expand Down Expand Up @@ -1391,6 +1396,11 @@ impl Grid {
/// Returns a [`GridError::EvolutionFailure`] if either the `operator` or its `info` is
/// incompatible with this `Grid`. Returns a [`GridError::Other`] if the iterator from `slices`
/// return an error.
///
/// # Panics
///
/// This function will panic if the dimension of the operators do not match the operator
/// information.
pub fn evolve_with_slice_iter<'a, E: Into<anyhow::Error>>(
&self,
slices: impl IntoIterator<Item = Result<(OperatorSliceInfo, CowArray<'a, f64, Ix4>), E>>,
Expand Down Expand Up @@ -1513,6 +1523,11 @@ impl Grid {
/// Returns a [`GridError::EvolutionFailure`] if either the `operator` or its `info` is
/// incompatible with this `Grid`. Returns a [`GridError::Other`] if the iterator from `slices`
/// return an error.
///
/// # Panics
///
/// This function will panic if the dimension of the operators do not match the operator
/// information.
pub fn evolve_with_slice_iter2<'a, E: Into<anyhow::Error>>(
&self,
slices_a: impl IntoIterator<Item = Result<(OperatorSliceInfo, CowArray<'a, f64, Ix4>), E>>,
Expand Down Expand Up @@ -1661,6 +1676,7 @@ impl Grid {

/// Deletes bins with the corresponding `bin_indices`. Repeated indices and indices larger or
/// equal the bin length are ignored.
#[allow(clippy::range_plus_one)]
pub fn delete_bins(&mut self, bin_indices: &[usize]) {
let mut bin_indices: Vec<_> = bin_indices
.iter()
Expand Down
10 changes: 5 additions & 5 deletions pineappl/src/import_only_subgrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Subgrid for ImportOnlySubgridV1 {

for (other_index, mu2) in other_grid.mu2_grid().iter().enumerate() {
// the following should always be the case
assert_eq!(mu2.ren, mu2.fac);
assert!((mu2.ren - mu2.fac).abs() < f64::EPSILON);
let q2 = &mu2.ren;

let index = match self
Expand Down Expand Up @@ -276,11 +276,11 @@ impl Subgrid for ImportOnlySubgridV2 {
for ((i, j, k), value) in self.array.indexed_iter() {
let target_j = x1_grid
.iter()
.position(|&x| x == self.x1_grid[j])
.position(|&x| (x - self.x1_grid[j]).abs() < f64::EPSILON)
.unwrap_or_else(|| unreachable!());
let target_k = x2_grid
.iter()
.position(|&x| x == self.x2_grid[k])
.position(|&x| (x - self.x2_grid[k]).abs() < f64::EPSILON)
.unwrap_or_else(|| unreachable!());

array[[i, target_j, target_k]] = value;
Expand Down Expand Up @@ -313,12 +313,12 @@ impl Subgrid for ImportOnlySubgridV2 {
let target_j = self
.x1_grid
.iter()
.position(|&x| x == rhs_x1[j])
.position(|&x| (x - rhs_x1[j]).abs() < f64::EPSILON)
.unwrap_or_else(|| unreachable!());
let target_k = self
.x2_grid
.iter()
.position(|&x| x == rhs_x2[k])
.position(|&x| (x - rhs_x2[k]).abs() < f64::EPSILON)
.unwrap_or_else(|| unreachable!());

self.array[[index, target_j, target_k]] += value;
Expand Down
20 changes: 12 additions & 8 deletions pineappl/src/lagrange_subgrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ use std::iter;
use std::mem;

fn weightfun(x: f64) -> f64 {
(x.sqrt() / (1.0 - 0.99 * x)).powi(3)
(x.sqrt() / 0.99f64.mul_add(-x, 1.0)).powi(3)
}

fn fx(y: f64) -> f64 {
let mut yp = y;

for _ in 0..100 {
let x = (-yp).exp();
let delta = y - yp - 5.0 * (1.0 - x);
let delta = 5.0f64.mul_add(-(1.0 - x), y - yp);
if (delta).abs() < 1e-12 {
return x;
}
let deriv = -1.0 - 5.0 * x;
let deriv = 5.0f64.mul_add(-x, -1.0);
yp -= delta / deriv;
}

Expand Down Expand Up @@ -451,7 +451,7 @@ impl LagrangeSubgridV2 {
}

fn gety1(&self, iy: usize) -> f64 {
if self.y1min == self.y1max {
if (self.y1min - self.y1max).abs() < f64::EPSILON {
debug_assert_eq!(iy, 0);
self.y1min
} else {
Expand All @@ -460,7 +460,7 @@ impl LagrangeSubgridV2 {
}

fn gety2(&self, iy: usize) -> f64 {
if self.y2min == self.y2max {
if (self.y2min - self.y2max).abs() < f64::EPSILON {
debug_assert_eq!(iy, 0);
self.y2min
} else {
Expand All @@ -469,7 +469,7 @@ impl LagrangeSubgridV2 {
}

fn gettau(&self, iy: usize) -> f64 {
if self.taumin == self.taumax {
if (self.taumin - self.taumax).abs() < f64::EPSILON {
debug_assert_eq!(iy, 0);
self.taumin
} else {
Expand Down Expand Up @@ -532,7 +532,9 @@ impl Subgrid for LagrangeSubgridV2 {

if self.static_q2 == 0.0 {
self.static_q2 = ntuple.q2;
} else if (self.static_q2 != -1.0) && (self.static_q2 != ntuple.q2) {
} else if ((self.static_q2 + 1.0).abs() > f64::EPSILON)
&& ((self.static_q2 - ntuple.q2).abs() > f64::EPSILON)
{
self.static_q2 = -1.0;
}

Expand Down Expand Up @@ -651,7 +653,9 @@ impl Subgrid for LagrangeSubgridV2 {
self.increase_tau(new_itaumin, new_itaumax);
}

if (other_grid.static_q2 == -1.0) || (self.static_q2 != other_grid.static_q2) {
if ((other_grid.static_q2 + 1.0).abs() > f64::EPSILON)
|| ((self.static_q2 - other_grid.static_q2).abs() > f64::EPSILON)
{
self.static_q2 = -1.0;
}

Expand Down
2 changes: 1 addition & 1 deletion pineappl/src/pids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ pub fn pdg_mc_ids_to_evol(tuples: &[(i32, f64)]) -> Option<i32> {
.collect();

if let &[(pid, factor)] = non_zero.as_slice() {
if factor == 1.0 {
if (factor - 1.0).abs() < f64::EPSILON {
return Some(pid);
}
}
Expand Down
1 change: 1 addition & 0 deletions pineappl/src/sparse_array3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ impl<T: Clone + Default + PartialEq> SparseArray3<T> {
}

/// Return an iterator over the elements, including zero elements.
#[allow(clippy::iter_without_into_iter)]
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
self.entries.iter_mut()
}
Expand Down
1 change: 1 addition & 0 deletions pineappl/tests/drell_yan_lo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn int_photo(s: f64, t: f64, u: f64) -> f64 {
}

// Eq. (2.12) - quark-antiquark contribution to DY lepton pair production
#[allow(clippy::suboptimal_flops)]
fn int_quark(s: f64, t: f64, u: f64, qq: f64, i3_wq: f64) -> f64 {
let alphagf: f64 = 1.0 / 132.30818655547878;
let mw = 80.35198454966643;
Expand Down

0 comments on commit 9fbb246

Please sign in to comment.