Skip to content

Commit

Permalink
rust: Protect cache properties
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhekhorn committed Aug 22, 2024
1 parent 56ed86c commit 164c02b
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 35 deletions.
4 changes: 2 additions & 2 deletions crates/eko/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ pub unsafe extern "C" fn rust_quad_ker_qcd(u: f64, rargs: *mut c_void) -> f64 {
(args.py)(
raw.re.as_ptr(),
raw.im.as_ptr(),
c.n.re,
c.n.im,
c.n().re,
c.n().im,
jac.re,
jac.im,
args.order_qcd,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::harmonics::cache::{Cache, K};
///
/// Implements Eq. (3.4) of [\[Moch:2004pa\]][crate::bib::Moch2004pa].
pub fn gamma_ns(c: &mut Cache, _nf: u8) -> Complex<f64> {
let N = c.n;
let N = c.n();
let S1 = c.get(K::S1);
let gamma = -(3.0 - 4.0 * S1 + 2.0 / N / (N + 1.0));
CF * gamma
Expand All @@ -19,7 +19,7 @@ pub fn gamma_ns(c: &mut Cache, _nf: u8) -> Complex<f64> {
///
/// Implements Eq. (3.5) of [\[Vogt:2004mw\]](crate::bib::Vogt2004mw).
pub fn gamma_qg(c: &mut Cache, nf: u8) -> Complex<f64> {
let N = c.n;
let N = c.n();
let gamma = -(N.powu(2) + N + 2.0) / (N * (N + 1.0) * (N + 2.0));
2.0 * TR * 2.0 * (nf as f64) * gamma
}
Expand All @@ -28,7 +28,7 @@ pub fn gamma_qg(c: &mut Cache, nf: u8) -> Complex<f64> {
///
/// Implements Eq. (3.5) of [\[Vogt:2004mw\]](crate::bib::Vogt2004mw).
pub fn gamma_gq(c: &mut Cache, _nf: u8) -> Complex<f64> {
let N = c.n;
let N = c.n();
let gamma = -(N.powu(2) + N + 2.0) / (N * (N + 1.0) * (N - 1.0));
2.0 * CF * gamma
}
Expand All @@ -37,7 +37,7 @@ pub fn gamma_gq(c: &mut Cache, _nf: u8) -> Complex<f64> {
///
/// Implements Eq. (3.5) of [\[Vogt:2004mw\]](crate::bib::Vogt2004mw).
pub fn gamma_gg(c: &mut Cache, nf: u8) -> Complex<f64> {
let N = c.n;
let N = c.n();
let S1 = c.get(K::S1);
let gamma = S1 - 1.0 / N / (N - 1.0) - 1.0 / (N + 1.0) / (N + 2.0);
CA * (4.0 * gamma - 11.0 / 3.0) + 4.0 / 3.0 * TR * (nf as f64)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::harmonics::cache::{Cache, K};
///
/// Implements Eq. (3.6) of [\[Moch:2004pa\]][crate::bib::Moch2004pa].
pub fn gamma_nsm(c: &mut Cache, nf: u8) -> Complex<f64> {
let N = c.n;
let N = c.n();
let S1 = c.get(K::S1);
let S2 = c.get(K::S2);
let Sp1m = c.get(K::S1mh);
Expand Down Expand Up @@ -50,7 +50,7 @@ pub fn gamma_nsm(c: &mut Cache, nf: u8) -> Complex<f64> {
///
/// Implements Eq. (3.5) of [\[Moch:2004pa\]][crate::bib::Moch2004pa].
pub fn gamma_nsp(c: &mut Cache, nf: u8) -> Complex<f64> {
let N = c.n;
let N = c.n();
let S1 = c.get(K::S1);
let S2 = c.get(K::S2);
let Sp1p = c.get(K::S1h);
Expand Down Expand Up @@ -90,7 +90,7 @@ pub fn gamma_nsp(c: &mut Cache, nf: u8) -> Complex<f64> {
///
/// Implements Eq. (3.6) of [\[Vogt:2004mw\]][crate::bib::Vogt2004mw].
pub fn gamma_ps(c: &mut Cache, nf: u8) -> Complex<f64> {
let N = c.n;
let N = c.n();
let gqqps1_nfcf = (-4. * (2. + N * (5. + N)) * (4. + N * (4. + N * (7. + 5. * N))))
/ ((-1. + N) * N.powu(3) * (1. + N).powu(3) * (2. + N).powu(2));
2.0 * TR * (nf as f64) * CF * gqqps1_nfcf
Expand All @@ -100,7 +100,7 @@ pub fn gamma_ps(c: &mut Cache, nf: u8) -> Complex<f64> {
///
/// Implements Eq. (3.7) of [\[Vogt:2004mw\]][crate::bib::Vogt2004mw].
pub fn gamma_qg(c: &mut Cache, nf: u8) -> Complex<f64> {
let N = c.n;
let N = c.n();
let S1 = c.get(K::S1);
let S2 = c.get(K::S2);
let Sp2p = c.get(K::S2h);
Expand Down Expand Up @@ -128,7 +128,7 @@ pub fn gamma_qg(c: &mut Cache, nf: u8) -> Complex<f64> {
///
/// Implements Eq. (3.8) of [\[Vogt:2004mw\]][crate::bib::Vogt2004mw].
pub fn gamma_gq(c: &mut Cache, nf: u8) -> Complex<f64> {
let N = c.n;
let N = c.n();
let S1 = c.get(K::S1);
let S2 = c.get(K::S2);
let Sp2p = c.get(K::S2h);
Expand Down Expand Up @@ -164,7 +164,7 @@ pub fn gamma_gq(c: &mut Cache, nf: u8) -> Complex<f64> {
///
/// Implements Eq. (3.9) of [\[Vogt:2004mw\]][crate::bib::Vogt2004mw].
pub fn gamma_gg(c: &mut Cache, nf: u8) -> Complex<f64> {
let N = c.n;
let N = c.n();
let S1 = c.get(K::S1);
let Sp1p = c.get(K::S1h);
let Sp2p = c.get(K::S2h);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::harmonics::cache::{Cache, K};
///
/// Implements Eq. (3.8) of [\[Moch:2004pa\]][crate::bib::Moch2004pa].
pub fn gamma_nsm(c: &mut Cache, nf: u8) -> Complex<f64> {
let N = c.n;
let N = c.n();
let S1 = c.get(K::S1);
let S2 = c.get(K::S2);
let S3 = c.get(K::S3);
Expand Down Expand Up @@ -68,7 +68,7 @@ pub fn gamma_nsm(c: &mut Cache, nf: u8) -> Complex<f64> {
///
/// Implements Eq. (3.7) of [\[Moch:2004pa\]][crate::bib::Moch2004pa].
pub fn gamma_nsp(c: &mut Cache, nf: u8) -> Complex<f64> {
let N = c.n;
let N = c.n();
let S1 = c.get(K::S1);
let S2 = c.get(K::S2);
let S3 = c.get(K::S3);
Expand Down Expand Up @@ -125,7 +125,7 @@ pub fn gamma_nsp(c: &mut Cache, nf: u8) -> Complex<f64> {
///
/// Implements Eq. (3.9) of [\[Moch:2004pa\]][crate::bib::Moch2004pa].
pub fn gamma_nsv(c: &mut Cache, nf: u8) -> Complex<f64> {
let N = c.n;
let N = c.n();
let S1 = c.get(K::S1);
let S2 = c.get(K::S2);
let S3 = c.get(K::S3);
Expand Down Expand Up @@ -162,7 +162,7 @@ pub fn gamma_nsv(c: &mut Cache, nf: u8) -> Complex<f64> {
///
/// Implements Eq. (3.10) of [\[Vogt:2004mw\]][crate::bib::Vogt2004mw].
pub fn gamma_ps(c: &mut Cache, nf: u8) -> Complex<f64> {
let N = c.n;
let N = c.n();
let S1 = c.get(K::S1);
let S2 = c.get(K::S2);
let S3 = c.get(K::S3);
Expand Down Expand Up @@ -218,7 +218,7 @@ pub fn gamma_ps(c: &mut Cache, nf: u8) -> Complex<f64> {
///
/// Implements Eq. (3.11) of [\[Vogt:2004mw\]][crate::bib::Vogt2004mw].
pub fn gamma_qg(c: &mut Cache, nf: u8) -> Complex<f64> {
let N = c.n;
let N = c.n();
let S1 = c.get(K::S1);
let S2 = c.get(K::S2);
let S3 = c.get(K::S3);
Expand Down Expand Up @@ -275,7 +275,7 @@ pub fn gamma_qg(c: &mut Cache, nf: u8) -> Complex<f64> {
///
/// Implements Eq. (3.12) of [\[Vogt:2004mw\]][crate::bib::Vogt2004mw].
pub fn gamma_gq(c: &mut Cache, nf: u8) -> Complex<f64> {
let N = c.n;
let N = c.n();
let S1 = c.get(K::S1);
let S2 = c.get(K::S2);
let S3 = c.get(K::S3);
Expand Down Expand Up @@ -348,7 +348,7 @@ pub fn gamma_gq(c: &mut Cache, nf: u8) -> Complex<f64> {
///
/// Implements Eq. (3.13) of [\[Vogt:2004mw\]][crate::bib::Vogt2004mw].
pub fn gamma_gg(c: &mut Cache, nf: u8) -> Complex<f64> {
let N = c.n;
let N = c.n();
let S1 = c.get(K::S1);
let S2 = c.get(K::S2);
let S3 = c.get(K::S3);
Expand Down
19 changes: 17 additions & 2 deletions crates/ekore/src/harmonics/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub enum K {
/// Hold all cached values.
pub struct Cache {
/// Mellin N
pub n: Complex<f64>,
n: Complex<f64>,
/// Mapping
m: HashMap<K, Complex<f64>>,
}
Expand All @@ -49,6 +49,11 @@ impl Cache {
}
}

/// Get Mellin N.
pub fn n(&self) -> Complex<f64> {
Complex::new(self.n.re, self.n.im)
}

/// Retrieve an element.
pub fn get(&mut self, k: K) -> Complex<f64> {
let val = self.m.get(&k);
Expand Down Expand Up @@ -94,11 +99,21 @@ pub fn recursive_harmonic_sum(

#[cfg(test)]
mod tests {
use crate::harmonics::cache::recursive_harmonic_sum;
use super::*;
use crate::harmonics::{w1, w2, w3, w4};
use crate::{assert_approx_eq_cmplx, cmplx};
use num::complex::Complex;

#[test]
fn n() {
let n = cmplx!(1., 0.);
let c = Cache::new(n);
let mut m = c.n();
m += cmplx!(1., 0.);
assert_approx_eq_cmplx!(f64, c.n(), n);
assert_approx_eq_cmplx!(f64, m, cmplx!(2., 0.));
}

#[test]
fn test_recursive_harmonic_sum() {
const SX: [fn(Complex<f64>) -> Complex<f64>; 4] = [w1::S1, w2::S2, w3::S3, w4::S4];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::harmonics::cache::{Cache, K};
///
/// Implements Eq. (20a) of [\[Ball:2015tna\]](crate::bib::Ball2015tna).
pub fn A_hh(c: &mut Cache, _nf: u8, L: f64) -> Complex<f64> {
let N = c.n;
let N = c.n();
let S1m = c.get(K::S1) - 1. / N;
let S2m = c.get(K::S2) - 1. / N.powu(2);
let ahh_l = (2. + N - 3. * N.powu(2)) / (N * (1. + N)) + 4. * S1m;
Expand All @@ -30,7 +30,7 @@ pub fn A_hh(c: &mut Cache, _nf: u8, L: f64) -> Complex<f64> {
///
/// Implements Eq. (20b) of [\[Ball:2015tna\]](crate::bib::Ball2015tna).
pub fn A_gh(c: &mut Cache, _nf: u8, L: f64) -> Complex<f64> {
let N = c.n;
let N = c.n();
let agh_l1 = (2. + N + N.powu(2)) / (N * (N.powu(2) - 1.));
let agh_l0 = (-4. + 2. * N + N.powu(2) * (15. + N * (3. + N - N.powu(2))))
/ (N * (N.powu(2) - 1.)).powu(2);
Expand All @@ -41,7 +41,7 @@ pub fn A_gh(c: &mut Cache, _nf: u8, L: f64) -> Complex<f64> {
///
/// Implements Eq. (B.2) of [\[Buza:1996wv\]](crate::bib::Buza1996wv).
pub fn A_hg(c: &mut Cache, _nf: u8, L: f64) -> Complex<f64> {
let N = c.n;
let N = c.n();
let den = 1. / (N * (N + 1.) * (2. + N));
let num = 2. * (2. + N + N.powu(2));
num * den * L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::operator_matrix_elements::unpolarized::spacelike::as1;
/// |NNLO| light-light non-singlet |OME|.
/// It is given in Eq.() of
pub fn A_qq_ns(c: &mut Cache, _nf: u8, L: f64) -> Complex<f64> {
let N = c.n;
let N = c.n();
let S1 = c.get(K::S1);
let S2 = c.get(K::S2);
let S3 = c.get(K::S3);
Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn A_qq_ns(c: &mut Cache, _nf: u8, L: f64) -> Complex<f64> {
/// |NNLO| heavy-light pure-singlet |OME|
/// It is given in Eq.() of
pub fn A_hq_ps(c: &mut Cache, _nf: u8, L: f64) -> Complex<f64> {
let N = c.n;
let N = c.n();
let S2 = c.get(K::S1);
let F1M = 1.0 / (N - 1.0) * (ZETA2 - (S2 - 1.0 / N.powu(2)));
let F11 = 1.0 / (N + 1.0) * (ZETA2 - (S2 + 1.0 / (N + 1.0).powu(2)));
Expand Down
15 changes: 6 additions & 9 deletions crates/ekore/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@ macro_rules! cmplx {
#[macro_export]
macro_rules! assert_approx_eq_cmplx {
($size:ty, $ref:expr, $target:expr) => {
use float_cmp::assert_approx_eq;
assert_approx_eq!($size, $ref.re, $target.re);
assert_approx_eq!($size, $ref.im, $target.im);
float_cmp::assert_approx_eq!($size, $ref.re, $target.re);
float_cmp::assert_approx_eq!($size, $ref.im, $target.im);
};
($size:ty, $ref:expr, $target:expr, ulps=$ulps:expr) => {
use float_cmp::assert_approx_eq;
assert_approx_eq!($size, $ref.re, $target.re, ulps = $ulps);
assert_approx_eq!($size, $ref.im, $target.im, ulps = $ulps);
float_cmp::assert_approx_eq!($size, $ref.re, $target.re, ulps = $ulps);
float_cmp::assert_approx_eq!($size, $ref.im, $target.im, ulps = $ulps);
};
($size:ty, $ref:expr, $target:expr, epsilon=$epsilon:expr) => {
use float_cmp::assert_approx_eq;
assert_approx_eq!($size, $ref.re, $target.re, epsilon = $epsilon);
assert_approx_eq!($size, $ref.im, $target.im, epsilon = $epsilon);
float_cmp::assert_approx_eq!($size, $ref.re, $target.re, epsilon = $epsilon);
float_cmp::assert_approx_eq!($size, $ref.im, $target.im, epsilon = $epsilon);
};
}

0 comments on commit 164c02b

Please sign in to comment.