Added approx, maybe that will fix tests on ci.

This commit is contained in:
2018-10-24 17:24:09 +02:00
parent 6ac0d0be78
commit c5c4abb95c
2 changed files with 54 additions and 11 deletions

View File

@@ -5,3 +5,6 @@ authors = ["Anders Olsson <anders.e.olsson@gmail.com>"]
[dependencies] [dependencies]
statrs = "0.10" statrs = "0.10"
[dev-dependencies]
approx = "0.3"

View File

@@ -1,5 +1,9 @@
extern crate statrs; extern crate statrs;
#[cfg(test)]
#[macro_use]
extern crate approx;
mod factor_graph; mod factor_graph;
mod gaussian; mod gaussian;
mod math; mod math;
@@ -278,6 +282,7 @@ where
let _ata = BETA.powi(2) * &rotated_a_matrix * &a_matrix; let _ata = BETA.powi(2) * &rotated_a_matrix * &a_matrix;
let _atsa = &rotated_a_matrix * &variance_matrix * &a_matrix; let _atsa = &rotated_a_matrix * &variance_matrix * &a_matrix;
let start = mean_matrix.transpose() * &a_matrix; let start = mean_matrix.transpose() * &a_matrix;
let middle = &_ata + &_atsa; let middle = &_ata + &_atsa;
let end = &rotated_a_matrix * &mean_matrix; let end = &rotated_a_matrix * &mean_matrix;
@@ -290,14 +295,48 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::f64;
use approx::{AbsDiffEq, RelativeEq};
use super::*; use super::*;
impl AbsDiffEq for Rating {
type Epsilon = f64;
#[inline]
fn default_epsilon() -> Self::Epsilon {
f64::default_epsilon()
}
#[inline]
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
self.mu.abs_diff_eq(&other.mu, epsilon) && self.sigma.abs_diff_eq(&other.sigma, epsilon)
}
}
impl RelativeEq for Rating {
fn default_max_relative() -> Self::Epsilon {
f64::default_max_relative()
}
fn relative_eq(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool {
self.mu.relative_eq(&other.mu, epsilon, max_relative)
&& self.sigma.relative_eq(&other.sigma, epsilon, max_relative)
}
}
#[test] #[test]
fn test_quality_1vs1() { fn test_quality_1vs1() {
let alice = Rating::new(MU, SIGMA); let alice = Rating::new(MU, SIGMA);
let bob = Rating::new(MU, SIGMA); let bob = Rating::new(MU, SIGMA);
assert_eq!(quality(&[&[alice], &[bob]]), 0.4472135954999579); assert_relative_eq!(quality(&[&[alice], &[bob]]), 0.4472135954999579);
} }
#[test] #[test]
@@ -307,16 +346,17 @@ mod tests {
let chris = Rating::new(MU, SIGMA); let chris = Rating::new(MU, SIGMA);
let darren = Rating::new(MU, SIGMA); let darren = Rating::new(MU, SIGMA);
let new_ratings = rate(&[&[alice], &[bob], &[chris], &[darren]]); let expected_ratings = vec![
assert_eq!(
new_ratings,
vec![
Rating::new(33.20778932559525, 6.347937214998893), Rating::new(33.20778932559525, 6.347937214998893),
Rating::new(27.401497882797486, 5.787057812482782), Rating::new(27.401497882797486, 5.787057812482782),
Rating::new(22.598576351652632, 5.7871159419307645), Rating::new(22.598576351652632, 5.7871159419307645),
Rating::new(16.79337409436942, 6.348053083319977), Rating::new(16.79337409436942, 6.348053083319977),
] ];
);
let ratings = rate(&[&[alice], &[bob], &[chris], &[darren]]);
for (rating, expected) in ratings.iter().zip(expected_ratings.iter()) {
assert_relative_eq!(rating, expected);
}
} }
} }