Skip to content

Commit

Permalink
Fix some clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
atomflunder committed Oct 9, 2024
1 parent cb1c78c commit 5256e1b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
2 changes: 2 additions & 0 deletions src/glicko2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ fn new_volatility(
(delta_squared - deviation_squared - v).ln()
} else {
let mut k: f64 = 1.0;
#[allow(clippy::while_float)]
while f_value(
k.mul_add(-tau, a),
delta_squared,
Expand All @@ -685,6 +686,7 @@ fn new_volatility(
let mut fb = f_value(b, delta_squared, deviation_squared, v, old_volatility, tau);

// 0.000001 is the convergence tolerance suggested by Mark Glickman.
#[allow(clippy::while_float)]
while (b - a).abs() > convergence_tolerance {
let c = a + ((a - b) * fa / (fb - fa));
let fc = f_value(c, delta_squared, deviation_squared, v, old_volatility, tau);
Expand Down
24 changes: 11 additions & 13 deletions src/trueskill/factor_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl PriorFactor {
}
}

pub fn down(&mut self) -> f64 {
pub fn down(&self) -> f64 {
let sigma = self.val.sigma().hypot(self.dynamic);
let value = Gaussian::with_mu_sigma(self.val.mu(), sigma);
self.variable.borrow_mut().update_value(self.id, value)
Expand Down Expand Up @@ -104,7 +104,7 @@ impl LikelihoodFactor {
}
}

pub fn down(&mut self) -> f64 {
pub fn down(&self) -> f64 {
let msg = {
let mean = self.mean.borrow();
mean.gaussian / mean.messages[&self.id]
Expand All @@ -115,7 +115,7 @@ impl LikelihoodFactor {
.update_message(self.id, Gaussian::with_pi_tau(a * msg.pi, a * msg.tau))
}

pub fn up(&mut self) -> f64 {
pub fn up(&self) -> f64 {
let msg = {
let value = self.value.borrow();
value.gaussian / value.messages[&self.id]
Expand Down Expand Up @@ -158,7 +158,7 @@ impl SumFactor {
}
}

pub fn down(&mut self) -> f64 {
pub fn down(&self) -> f64 {
let msgs: Vec<Gaussian> = self
.terms
.iter()
Expand All @@ -167,7 +167,7 @@ impl SumFactor {
self.update(&self.sum, &self.terms, &msgs, &self.coeffs)
}

pub fn up(&mut self, index: usize) -> f64 {
pub fn up(&self, index: usize) -> f64 {
let coeff = self.coeffs[index];
let mut coeffs = Vec::new();
for (x, c) in self.coeffs.iter().enumerate() {
Expand Down Expand Up @@ -254,7 +254,7 @@ impl TruncateFactor {
}
}

pub fn up(&mut self) -> f64 {
pub fn up(&self) -> f64 {
let div = {
let variable = self.variable.borrow();
variable.gaussian / variable.messages[&self.id]
Expand All @@ -277,15 +277,13 @@ impl TruncateFactor {

#[cfg(test)]
mod tests {
use std::f64::INFINITY;

use super::*;

#[test]
fn test_delta_inf() {
let mut v1 = Variable::new();

v1.set(Gaussian::with_pi_tau(INFINITY, 1.0));
v1.set(Gaussian::with_pi_tau(f64::INFINITY, 1.0));

assert!(v1.delta(Gaussian::with_pi_tau(0.0, 0.0)) < f64::EPSILON);
}
Expand All @@ -295,10 +293,10 @@ mod tests {
let mut v1 = Variable::new();
let mut v2 = Variable::new();

v1.set(Gaussian::with_pi_tau(INFINITY, 1.0));
v1.set(Gaussian::with_pi_tau(f64::INFINITY, 1.0));
v2.set(Gaussian::with_pi_tau(0.0, 1.0));

let mut sm1 = SumFactor::new(
let sm1 = SumFactor::new(
0,
Rc::new(RefCell::new(v1.clone())),
vec![Rc::new(RefCell::new(v2.clone()))],
Expand All @@ -317,10 +315,10 @@ mod tests {
let mut v1 = Variable::new();
let mut v2 = Variable::new();

v1.set(Gaussian::with_pi_tau(INFINITY, 1.0));
v1.set(Gaussian::with_pi_tau(f64::INFINITY, 1.0));
v2.set(Gaussian::with_pi_tau(0.0, 1.0));

let mut sm1 = SumFactor::new(
let sm1 = SumFactor::new(
0,
Rc::new(RefCell::new(v1.clone())),
vec![Rc::new(RefCell::new(v2.clone()))],
Expand Down
4 changes: 1 addition & 3 deletions src/trueskill/gaussian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,13 @@ impl PartialOrd for Gaussian {

#[cfg(test)]
mod tests {
use std::f64::INFINITY;

use super::*;

#[test]
fn test_gaussian_edge_cases() {
let g1 = Gaussian::with_pi_tau(0.0, 0.0);

assert!(g1.sigma() == INFINITY);
assert!(g1.sigma() == f64::INFINITY);
assert!(g1.mu() == 0.0);
}

Expand Down
27 changes: 13 additions & 14 deletions src/trueskill/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1663,11 +1663,11 @@ fn run_schedule(
factor.down();
}

let mut team_diff_layer = build_team_diff_layer(team_diff_vars, team_perf_vars, id);
let team_diff_layer = build_team_diff_layer(team_diff_vars, team_perf_vars, id);
let team_diff_len = team_diff_layer.len();
id += team_diff_len;

let mut trunc_layer = build_trunc_layer(
let trunc_layer = build_trunc_layer(
team_diff_vars,
sorted_teams_and_ranks,
draw_probability,
Expand Down Expand Up @@ -1906,7 +1906,6 @@ mod tests {
use crate::MultiTeamOutcome;

use super::*;
use std::f64::{INFINITY, NEG_INFINITY};

#[test]
/// This example is taken from this presentation (Page 20):
Expand Down Expand Up @@ -2483,11 +2482,11 @@ mod tests {

assert!((dist - 0.096_800).abs() < 0.000_001);

let dist_inf = cdf(INFINITY, 0.0, 1.0);
let dist_inf = cdf(f64::INFINITY, 0.0, 1.0);

assert!((dist_inf - 1.0).abs() < f64::EPSILON);

let dist_neg_inf = cdf(NEG_INFINITY, 0.0, 1.0);
let dist_neg_inf = cdf(f64::NEG_INFINITY, 0.0, 1.0);

assert!((dist_neg_inf - 0.0).abs() < f64::EPSILON);
}
Expand All @@ -2508,29 +2507,29 @@ mod tests {

#[test]
fn test_wv_edge_cases() {
let w = w_non_draw(NEG_INFINITY, 0.0, 1.0);
let w = w_non_draw(f64::NEG_INFINITY, 0.0, 1.0);

assert!((w - 1.0).abs() < f64::EPSILON);

let v = v_non_draw(NEG_INFINITY, 0.0, 1.0);
let v = v_non_draw(f64::NEG_INFINITY, 0.0, 1.0);

assert!(v == INFINITY);
assert!(v == f64::INFINITY);

let w2 = w_draw(NEG_INFINITY, 0.0, 1.0);
let w2 = w_draw(f64::NEG_INFINITY, 0.0, 1.0);

assert!((w2 - 1.0).abs() < f64::EPSILON);

let v2 = v_draw(NEG_INFINITY, 0.0, 1.0);
let v2 = v_draw(f64::NEG_INFINITY, 0.0, 1.0);

assert!(v2 == INFINITY);
assert!(v2 == f64::INFINITY);

let w3 = w_non_draw(1.0, INFINITY, 1.0);
let w3 = w_non_draw(1.0, f64::INFINITY, 1.0);

assert!((w3 - 0.0).abs() < f64::EPSILON);

let v3 = v_draw(INFINITY, f64::MAX, 1.0);
let v3 = v_draw(f64::INFINITY, f64::MAX, 1.0);

assert!(v3 == NEG_INFINITY);
assert!(v3 == f64::NEG_INFINITY);
}

#[test]
Expand Down

0 comments on commit 5256e1b

Please sign in to comment.