Skip to content

Commit

Permalink
rust: Add rel to cmplx eq
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhekhorn committed Oct 11, 2024
1 parent 457e52c commit d1b6c53
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ mod test {
fn test_quark_number_conservation() {
let logs = [0., 100.];
for L in logs {
let N = cmplx![1., 0.];
let N = cmplx!(1., 0.);
let mut c = Cache::new(N);
let aNSqq2 = A_qq_ns(&mut c, NF, L);
assert_approx_eq_cmplx!(f64, aNSqq2, Complex::zero(), epsilon = 2e-11);
Expand All @@ -310,7 +310,7 @@ mod test {
fn test_momentum_conservation() {
let logs = [0., 100.];
for L in logs {
let N = cmplx![2., 0.];
let N = cmplx!(2., 0.);
let mut c = Cache::new(N);
let aS2 = A_singlet(&mut c, NF, L, false);

Expand Down Expand Up @@ -346,14 +346,14 @@ mod test {

#[test]
fn pegasus_sign() {
let ref_val = cmplx![-21133.9, 0.];
let N = cmplx![2., 0.];
let ref_val = cmplx!(-21133.9, 0.);
let N = cmplx!(2., 0.);
let mut c = Cache::new(N);
let L = 100.;

let aS2 = A_singlet(&mut c, NF, L, false);
// add macro for this?
assert_approx_eq_cmplx!(f64, aS2[0][0], ref_val, epsilon = 4e-5 * (21133.9));
assert_approx_eq_cmplx!(f64, aS2[0][0], ref_val, rel = 4e-5);
}

#[test]
Expand Down Expand Up @@ -391,7 +391,7 @@ mod test {

for n in 2..11 {
for (i, L) in [0., 10.].iter().enumerate() {
let N = cmplx![n as f64, 0.];
let N = cmplx!(n as f64, 0.);
let mut c = Cache::new(N);

let aS2 = A_singlet(&mut c, NF, *L, false);
Expand All @@ -400,33 +400,33 @@ mod test {
assert_approx_eq_cmplx!(
f64,
aS2[0][0],
cmplx![ref_val_gg[i][idx], 0.],
epsilon = 2e-6 * (-ref_val_gg[i][idx])
cmplx!(ref_val_gg[i][idx], 0.),
rel = 2e-6
);
assert_approx_eq_cmplx!(
f64,
aS2[0][1],
cmplx![ref_val_gq[i][idx], 0.],
epsilon = 4e-6 * (ref_val_gq[i][idx])
cmplx!(ref_val_gq[i][idx], 0.),
rel = 4e-6
);
assert_approx_eq_cmplx!(
f64,
aS2[2][0],
cmplx![ref_val_Hg[i][idx], 0.],
epsilon = 3e-6 * (ref_val_Hg[i][idx])
cmplx!(ref_val_Hg[i][idx], 0.),
rel = 3e-6
);
assert_approx_eq_cmplx!(
f64,
aS2[2][1],
cmplx![ref_val_Hq[i][idx], 0.],
epsilon = 3e-6 * (-ref_val_Hq[i][idx])
cmplx!(ref_val_Hq[i][idx], 0.),
rel = 3e-6
);
}
assert_approx_eq_cmplx!(
f64,
aS2[1][1],
cmplx![ref_val_qq[i][n - 2], 0.],
epsilon = 4e-6 * (-ref_val_qq[i][n - 2])
cmplx!(ref_val_qq[i][n - 2], 0.),
rel = 4e-6
);
}
}
Expand All @@ -436,7 +436,7 @@ mod test {
fn Hg2_pegasus() {
let L = 0.;
for n in 3..20 {
let N = cmplx![n as f64, 0.];
let N = cmplx!(n as f64, 0.);

let mut c = Cache::new(N);

Expand All @@ -458,15 +458,15 @@ mod test {
+ 93.68 * 1.0 / N.powu(2)
- 146.8 * E2;

assert_approx_eq_cmplx!(f64, aS2[2][0], a_hg_param, epsilon = 7e-4 * a_hg_param.re);
assert_approx_eq_cmplx!(f64, aS2[2][0], a_hg_param, rel = 7e-4);
}
}

#[test]
fn msbar_matching() {
let logs = [0., 100.];
for L in logs {
let N = cmplx![2., 0.];
let N = cmplx!(2., 0.);
let mut c = Cache::new(N);
let aS2 = A_singlet(&mut c, NF, L, false);
assert_approx_eq_cmplx!(
Expand Down
5 changes: 5 additions & 0 deletions crates/ekore/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ macro_rules! cmplx {
#[cfg(test)]
#[macro_export]
macro_rules! assert_approx_eq_cmplx {
($size:ty, $ref:expr, $target:expr, rel=$rel:expr) => {
assert!($target.norm() > 0.0, "target has norm=0!");
float_cmp::assert_approx_eq!($size, $ref.re, $target.re, epsilon = $rel * $target.norm());
float_cmp::assert_approx_eq!($size, $ref.im, $target.im, epsilon = $rel * $target.norm());
};
($size:ty, $ref:expr, $target:expr $(, $set:ident = $val:expr)*) => {
float_cmp::assert_approx_eq!($size, $ref.re, $target.re $(, $set = $val)*);
float_cmp::assert_approx_eq!($size, $ref.im, $target.im $(, $set = $val)*);
Expand Down

0 comments on commit d1b6c53

Please sign in to comment.