Skip to content

Commit

Permalink
rust: Use cmplx eq in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhekhorn committed Sep 10, 2024
1 parent 17a73d5 commit 5239d8b
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 73 deletions.
31 changes: 16 additions & 15 deletions crates/ekore/src/anomalous_dimensions/unpolarized/spacelike/as1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,63 +54,64 @@ pub fn gamma_singlet(c: &mut Cache, nf: u8) -> [[Complex<f64>; 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]
fn number_conservation() {
const N: Complex<f64> = 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]
fn quark_momentum_conservation() {
const N: Complex<f64> = 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]
fn gluon_momentum_conservation() {
const N: Complex<f64> = 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]
fn gamma_qg_() {
const N: Complex<f64> = 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]
fn gamma_gq_() {
const N: Complex<f64> = 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]
fn gamma_gg_() {
const N: Complex<f64> = 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
);
}
}
124 changes: 80 additions & 44 deletions crates/ekore/src/anomalous_dimensions/unpolarized/spacelike/as2.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -216,41 +216,55 @@ pub fn gamma_singlet(c: &mut Cache, nf: u8) -> [[Complex<f64>; 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;

#[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
);

Expand All @@ -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
)
);
}
}
41 changes: 33 additions & 8 deletions crates/ekore/src/anomalous_dimensions/unpolarized/spacelike/as3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,9 @@ pub fn gamma_singlet(c: &mut Cache, nf: u8) -> [[Complex<f64>; 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;
Expand All @@ -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
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ pub fn A_ns(c: &mut Cache, nf: u8, L: f64) -> [[Complex<f64>; 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;

Expand All @@ -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]
Expand Down Expand Up @@ -129,7 +128,7 @@ mod tests {
3.666666667,
2.61904761905,
2.0555555556,
1.6969696967,
1.696969697,
];

for n in 0..4 {
Expand Down
4 changes: 4 additions & 0 deletions crates/ekore/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}

0 comments on commit 5239d8b

Please sign in to comment.