From 5239d8bda1772c78662470dc44830fdb942d96fb Mon Sep 17 00:00:00 2001 From: Felix Hekhorn Date: Tue, 10 Sep 2024 12:59:24 +0300 Subject: [PATCH] rust: Use cmplx eq in tests --- .../unpolarized/spacelike/as1.rs | 31 ++--- .../unpolarized/spacelike/as2.rs | 124 +++++++++++------- .../unpolarized/spacelike/as3.rs | 41 ++++-- .../unpolarized/spacelike/as1.rs | 11 +- crates/ekore/src/util.rs | 4 + 5 files changed, 138 insertions(+), 73 deletions(-) diff --git a/crates/ekore/src/anomalous_dimensions/unpolarized/spacelike/as1.rs b/crates/ekore/src/anomalous_dimensions/unpolarized/spacelike/as1.rs index 5b5fc6ee6..e35a87472 100644 --- a/crates/ekore/src/anomalous_dimensions/unpolarized/spacelike/as1.rs +++ b/crates/ekore/src/anomalous_dimensions/unpolarized/spacelike/as1.rs @@ -54,10 +54,11 @@ pub fn gamma_singlet(c: &mut Cache, nf: u8) -> [[Complex; 2]; 2] { #[cfg(test)] mod tests { - use crate::cmplx; - use crate::{anomalous_dimensions::unpolarized::spacelike::as1::*, harmonics::cache::Cache}; - use float_cmp::assert_approx_eq; + use super::*; + use crate::harmonics::cache::Cache; + use crate::{assert_approx_eq_cmplx, cmplx}; use num::complex::Complex; + use num::Zero; const NF: u8 = 5; #[test] @@ -65,8 +66,7 @@ mod tests { const N: Complex = cmplx![1., 0.]; let mut c = Cache::new(N); let me = gamma_ns(&mut c, NF); - assert_approx_eq!(f64, me.re, 0., epsilon = 1e-12); - assert_approx_eq!(f64, me.im, 0., epsilon = 1e-12); + assert_approx_eq_cmplx!(f64, me, Complex::zero(), epsilon = 1e-12); } #[test] @@ -74,8 +74,7 @@ mod tests { const N: Complex = cmplx![2., 0.]; let mut c = Cache::new(N); let me = gamma_ns(&mut c, NF) + gamma_gq(&mut c, NF); - assert_approx_eq!(f64, me.re, 0., epsilon = 1e-12); - assert_approx_eq!(f64, me.im, 0., epsilon = 1e-12); + assert_approx_eq_cmplx!(f64, me, Complex::zero(), epsilon = 1e-12); } #[test] @@ -83,8 +82,7 @@ mod tests { const N: Complex = cmplx![2., 0.]; let mut c = Cache::new(N); let me = gamma_qg(&mut c, NF) + gamma_gg(&mut c, NF); - assert_approx_eq!(f64, me.re, 0., epsilon = 1e-12); - assert_approx_eq!(f64, me.im, 0., epsilon = 1e-12); + assert_approx_eq_cmplx!(f64, me, Complex::zero(), epsilon = 1e-12); } #[test] @@ -92,8 +90,7 @@ mod tests { const N: Complex = cmplx![1., 0.]; let mut c = Cache::new(N); let me = gamma_qg(&mut c, NF); - assert_approx_eq!(f64, me.re, -20. / 3.0, ulps = 32); - assert_approx_eq!(f64, me.im, 0., epsilon = 1e-12); + assert_approx_eq_cmplx!(f64, me, cmplx!(-20. / 3., 0.), ulps = 32, epsilon = 1e-12); } #[test] @@ -101,8 +98,7 @@ mod tests { const N: Complex = cmplx![0., 1.]; let mut c = Cache::new(N); let me = gamma_gq(&mut c, NF); - assert_approx_eq!(f64, me.re, 4. / 3.0, ulps = 32); - assert_approx_eq!(f64, me.im, -4. / 3.0, ulps = 32); + assert_approx_eq_cmplx!(f64, me, cmplx!(4. / 3.0, -4. / 3.0), ulps = 32); } #[test] @@ -110,7 +106,12 @@ mod tests { const N: Complex = cmplx![0., 1.]; let mut c = Cache::new(N); let me = gamma_gg(&mut c, NF); - assert_approx_eq!(f64, me.re, 5.195725159621, ulps = 32, epsilon = 1e-11); - assert_approx_eq!(f64, me.im, 10.52008856962, ulps = 32, epsilon = 1e-11); + assert_approx_eq_cmplx!( + f64, + me, + cmplx!(5.195725159621, 10.52008856962), + ulps = 32, + epsilon = 1e-11 + ); } } diff --git a/crates/ekore/src/anomalous_dimensions/unpolarized/spacelike/as2.rs b/crates/ekore/src/anomalous_dimensions/unpolarized/spacelike/as2.rs index 857034ab8..f1d0266d5 100644 --- a/crates/ekore/src/anomalous_dimensions/unpolarized/spacelike/as2.rs +++ b/crates/ekore/src/anomalous_dimensions/unpolarized/spacelike/as2.rs @@ -1,6 +1,6 @@ //! |NLO| |QCD|. -use ::num::complex::Complex; +use num::complex::Complex; use std::f64::consts::LN_2; use crate::constants::{CA, CF, TR, ZETA2, ZETA3}; @@ -216,11 +216,12 @@ pub fn gamma_singlet(c: &mut Cache, nf: u8) -> [[Complex; 2]; 2] { #[cfg(test)] mod tests { - use crate::cmplx; - use crate::{anomalous_dimensions::unpolarized::spacelike::as2::*, harmonics::cache::Cache}; - use float_cmp::assert_approx_eq; + use super::*; + use crate::harmonics::cache::Cache; + use crate::{assert_approx_eq_cmplx, cmplx}; use num::complex::Complex; use num::traits::Pow; + use num::Zero; use std::f64::consts::PI; const NF: u8 = 5; @@ -228,29 +229,42 @@ mod tests { #[test] fn physical_constraints() { // number conservation - let mut c = Cache::new(cmplx![1., 0.]); - assert_approx_eq!(f64, gamma_nsm(&mut c, NF).re, 0.0, epsilon = 2e-6); + let mut c = Cache::new(cmplx!(1., 0.)); + assert_approx_eq_cmplx!(f64, gamma_nsm(&mut c, NF), Complex::zero(), epsilon = 2e-6); // momentum conservation - let mut c = Cache::new(cmplx![2., 0.]); + let mut c = Cache::new(cmplx!(2., 0.)); let gS1 = gamma_singlet(&mut c, NF); // gluon momentum conservation - assert_approx_eq!(f64, (gS1[0][1] + gS1[1][1]).re, 0.0, epsilon = 4e-5); + assert_approx_eq_cmplx!( + f64, + (gS1[0][1] + gS1[1][1]), + Complex::zero(), + epsilon = 4e-5 + ); // quark momentum conservation - assert_approx_eq!(f64, (gS1[0][0] + gS1[1][0]).re, 0.0, epsilon = 2e-6); + assert_approx_eq_cmplx!( + f64, + (gS1[0][0] + gS1[1][0]), + Complex::zero(), + epsilon = 2e-6 + ); } #[test] fn N2() { // reference values are obtained from MMa - let mut c = Cache::new(cmplx![2., 0.]); + let mut c = Cache::new(cmplx!(2., 0.)); // ns+ - assert_approx_eq!( + assert_approx_eq_cmplx!( f64, - gamma_nsp(&mut c, NF).re, - (-112.0 * CF + 376.0 * CA - 64.0 * (NF as f64)) * CF / 27.0, + gamma_nsp(&mut c, NF), + cmplx!( + (-112.0 * CF + 376.0 * CA - 64.0 * (NF as f64)) * CF / 27.0, + 0. + ), epsilon = 2e-6 ); @@ -259,80 +273,102 @@ mod tests { + (373.0 / 9.0 - 34.0 * PI.pow(2) / 9.0 + 8.0 * ZETA3) * CA - 64.0 * (NF as f64) / 27.0) * CF; - assert_approx_eq!(f64, gamma_nsm(&mut c, NF).re, check, epsilon = 2e-6); + assert_approx_eq_cmplx!( + f64, + gamma_nsm(&mut c, NF), + cmplx!(check, 0.), + epsilon = 2e-6 + ); // singlet sector let gS1 = gamma_singlet(&mut c, NF); // ps - assert_approx_eq!( + assert_approx_eq_cmplx!( f64, - gamma_ps(&mut c, NF).re, - -40.0 * CF * (NF as f64) / 27.0 + gamma_ps(&mut c, NF), + cmplx!(-40.0 * CF * (NF as f64) / 27.0, 0.) ); // qg - assert_approx_eq!( + assert_approx_eq_cmplx!( f64, - gS1[0][1].re, - (-74.0 * CF - 35.0 * CA) * (NF as f64) / 27.0 + gS1[0][1], + cmplx!((-74.0 * CF - 35.0 * CA) * (NF as f64) / 27.0, 0.) ); // gq - assert_approx_eq!( + assert_approx_eq_cmplx!( f64, - gS1[1][0].re, - (112.0 * CF - 376.0 * CA + 104.0 * (NF as f64)) * CF / 27.0, + gS1[1][0], + cmplx!( + (112.0 * CF - 376.0 * CA + 104.0 * (NF as f64)) * CF / 27.0, + 0. + ), epsilon = 1e-13 ); } #[test] fn N3() { - let mut c = Cache::new(cmplx![3., 0.]); + let mut c = Cache::new(cmplx!(3., 0.)); // ns+ let check = ((-34487.0 / 432.0 + 86.0 * PI.pow(2) / 9.0 - 16.0 * ZETA3) * CF + (459.0 / 8.0 - 43.0 * PI.pow(2) / 9.0 + 8.0 * ZETA3) * CA - 415.0 * (NF as f64) / 108.0) * CF; - assert_approx_eq!(f64, gamma_nsp(&mut c, NF).re, check, epsilon = 2e-6); + assert_approx_eq_cmplx!( + f64, + gamma_nsp(&mut c, NF), + cmplx!(check, 0.), + epsilon = 2e-6 + ); // singlet sector let gS1 = gamma_singlet(&mut c, NF); // ps - assert_approx_eq!( + assert_approx_eq_cmplx!( f64, - gamma_ps(&mut c, NF).re, - -1391.0 * CF * (NF as f64) / 5400.0 + gamma_ps(&mut c, NF), + cmplx!(-1391.0 * CF * (NF as f64) / 5400.0, 0.) ); // gq - assert_approx_eq!( + assert_approx_eq_cmplx!( f64, - gS1[1][0].re, - (973.0 / 432.0 * CF - + (2801.0 / 5400.0 - 7.0 * PI.pow(2) / 9.0) * CA - + 61.0 / 54.0 * (NF as f64)) - * CF + gS1[1][0], + cmplx!( + (973.0 / 432.0 * CF + + (2801.0 / 5400.0 - 7.0 * PI.pow(2) / 9.0) * CA + + 61.0 / 54.0 * (NF as f64)) + * CF, + 0. + ) ); // gg - assert_approx_eq!( + assert_approx_eq_cmplx!( f64, - gS1[1][1].re, - (-79909.0 / 3375.0 + 194.0 * PI.pow(2) / 45.0 - 8.0 * ZETA3) * CA.pow(2) - - 967.0 / 270.0 * CA * (NF as f64) - + 541.0 / 216.0 * CF * (NF as f64), + gS1[1][1], + cmplx!( + (-79909.0 / 3375.0 + 194.0 * PI.pow(2) / 45.0 - 8.0 * ZETA3) * CA.pow(2) + - 967.0 / 270.0 * CA * (NF as f64) + + 541.0 / 216.0 * CF * (NF as f64), + 0. + ), epsilon = 3e-5 ); } #[test] fn N4() { - let mut c = Cache::new(cmplx![4., 0.]); + let mut c = Cache::new(cmplx!(4., 0.)); // singlet sector let gS1 = gamma_singlet(&mut c, NF); // qg - assert_approx_eq!( + assert_approx_eq_cmplx!( f64, - gS1[0][1].re, - (-56317.0 / 18000.0 * CF + 16387.0 / 9000.0 * CA) * (NF as f64), + gS1[0][1], + cmplx!( + (-56317.0 / 18000.0 * CF + 16387.0 / 9000.0 * CA) * (NF as f64), + 0. + ), epsilon = 1e-14 - ) + ); } } diff --git a/crates/ekore/src/anomalous_dimensions/unpolarized/spacelike/as3.rs b/crates/ekore/src/anomalous_dimensions/unpolarized/spacelike/as3.rs index d491db0e9..223d9406a 100644 --- a/crates/ekore/src/anomalous_dimensions/unpolarized/spacelike/as3.rs +++ b/crates/ekore/src/anomalous_dimensions/unpolarized/spacelike/as3.rs @@ -425,9 +425,9 @@ pub fn gamma_singlet(c: &mut Cache, nf: u8) -> [[Complex; 2]; 2] { #[cfg(test)] mod tests { - use crate::cmplx; - use crate::{anomalous_dimensions::unpolarized::spacelike::as3::*, harmonics::cache::Cache}; - use float_cmp::assert_approx_eq; + use super::*; + use crate::harmonics::cache::Cache; + use crate::{assert_approx_eq_cmplx, cmplx}; use num::complex::Complex; const NF: u8 = 5; @@ -436,20 +436,45 @@ mod tests { fn physical_constraints() { // number conservation let mut c = Cache::new(cmplx![1., 0.]); - assert_approx_eq!(f64, gamma_nsv(&mut c, NF).re, -0.000960586, epsilon = 3e-7); - assert_approx_eq!(f64, gamma_nsm(&mut c, NF).re, 0.000594225, epsilon = 6e-7); + assert_approx_eq_cmplx!( + f64, + gamma_nsv(&mut c, NF), + cmplx!(-0.000960586, 0.), + epsilon = 3e-7 + ); + assert_approx_eq_cmplx!( + f64, + gamma_nsm(&mut c, NF), + cmplx!(0.000594225, 0.), + epsilon = 6e-7 + ); let mut c = Cache::new(cmplx![2., 0.]); let gS2 = gamma_singlet(&mut c, NF); // gluon momentum conservation - assert_approx_eq!(f64, (gS2[0][1] + gS2[1][1]).re, -0.00388726, epsilon = 2e-6); + assert_approx_eq_cmplx!( + f64, + (gS2[0][1] + gS2[1][1]), + cmplx!(-0.00388726, 0.), + epsilon = 2e-6 + ); // quark momentum conservation - assert_approx_eq!(f64, (gS2[0][0] + gS2[1][0]).re, 0.00169375, epsilon = 2e-6); + assert_approx_eq_cmplx!( + f64, + (gS2[0][0] + gS2[1][0]), + cmplx!(0.00169375, 0.), + epsilon = 2e-6 + ); } #[test] fn N2() { let mut c = Cache::new(cmplx![2., 0.]); - assert_approx_eq!(f64, gamma_nsv(&mut c, NF).re, 188.325593, epsilon = 3e-7); + assert_approx_eq_cmplx!( + f64, + gamma_nsv(&mut c, NF), + cmplx!(188.325593, 0.), + epsilon = 3e-7 + ); } } diff --git a/crates/ekore/src/operator_matrix_elements/unpolarized/spacelike/as1.rs b/crates/ekore/src/operator_matrix_elements/unpolarized/spacelike/as1.rs index dd9b1e791..898d3d0c4 100644 --- a/crates/ekore/src/operator_matrix_elements/unpolarized/spacelike/as1.rs +++ b/crates/ekore/src/operator_matrix_elements/unpolarized/spacelike/as1.rs @@ -77,10 +77,9 @@ pub fn A_ns(c: &mut Cache, nf: u8, L: f64) -> [[Complex; 2]; 2] { #[cfg(test)] mod tests { + use super::*; + use crate::harmonics::cache::Cache; use crate::{assert_approx_eq_cmplx, cmplx}; - use crate::{ - harmonics::cache::Cache, operator_matrix_elements::unpolarized::spacelike::as1::*, - }; use num::complex::Complex; const NF: u8 = 5; @@ -94,11 +93,11 @@ mod tests { assert_approx_eq_cmplx!( f64, (aS1[0][2] + aS1[1][2] + aS1[2][2]), - cmplx!(0., 0.), + Complex::zero(), epsilon = 1e-10 ); // gluon momentum conservation - assert_approx_eq_cmplx!(f64, (aS1[0][0] + aS1[1][0] + aS1[2][0]), cmplx!(0., 0.)); + assert_approx_eq_cmplx!(f64, (aS1[0][0] + aS1[1][0] + aS1[2][0]), Complex::zero()); } #[test] @@ -129,7 +128,7 @@ mod tests { 3.666666667, 2.61904761905, 2.0555555556, - 1.6969696967, + 1.696969697, ]; for n in 0..4 { diff --git a/crates/ekore/src/util.rs b/crates/ekore/src/util.rs index 0d815bc98..bbe960640 100644 --- a/crates/ekore/src/util.rs +++ b/crates/ekore/src/util.rs @@ -24,4 +24,8 @@ macro_rules! assert_approx_eq_cmplx { float_cmp::assert_approx_eq!($size, $ref.re, $target.re, epsilon = $epsilon); float_cmp::assert_approx_eq!($size, $ref.im, $target.im, epsilon = $epsilon); }; + ($size:ty, $ref:expr, $target:expr, ulps=$ulps:expr, epsilon=$epsilon:expr) => { + float_cmp::assert_approx_eq!($size, $ref.re, $target.re, ulps = $ulps, epsilon = $epsilon); + float_cmp::assert_approx_eq!($size, $ref.im, $target.im, ulps = $ulps, epsilon = $epsilon); + }; }