1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
pub use native::*;

// For use in Rust
//
// - Collect into a module to improve code organization, but immediately re-export.
//
// - Each function simply calls its inline analogue with the sole objective of not
//   inlining code when not necessary. The inline functions are still available to
//   the crate for more specialized use.
mod native {
    use super::inlines;
    use crate::{Num, R};

    pub fn g_sep<T: Num>(s: T, ln_s: T, ln_s_pl_1: T) -> T {
        inlines::g_sep(s, ln_s, ln_s_pl_1)
    }

    pub fn g<T: Num>(s: T) -> T {
        inlines::g(s)
    }

    pub fn g_xi_sep<T: Num>(ln_s: T) -> T {
        inlines::g_xi_sep(ln_s)
    }

    pub fn g_xi<T: Num>(s: T) -> T {
        inlines::g_xi(s)
    }

    pub fn dressing_inv_landau_sep<T: Num>(s: T, ln_s: T, ln_s_pl_1: T, g0: R) -> T {
        inlines::dressing_inv_landau_sep(s, ln_s, ln_s_pl_1, g0)
    }

    pub fn dressing_inv_sep<T: Num>(s: T, ln_s: T, ln_s_pl_1: T, g0: R, xi: R) -> T {
        inlines::dressing_inv_sep(s, ln_s, ln_s_pl_1, g0, xi)
    }
}

// For use in other languages, e.g. C/C++/Python
//
// - Re-export at crate::ffi, since symbols need to be unmangled anyway and the
//   namespace will not be preserved.
//
// - Here too each function calls its inline analogue, but the objective is to
//   switch from generic to concrete argument types so that the functions can
//   be compiled into a C dynamic library. To do so, we need to double their
//   number (one function for real arguments, another for complex arguments).
pub(crate) mod ffi {
    use super::inlines;
    use crate::{C, R};

    #[no_mangle]
    pub extern "C" fn oneloop__ghost__g_sep(s: R, ln_s: R, ln_s_pl_1: R) -> R {
        inlines::g_sep(s, ln_s, ln_s_pl_1)
    }

    #[no_mangle]
    pub extern "C" fn oneloop__ghost__g(s: R) -> R {
        inlines::g(s)
    }

    #[no_mangle]
    pub extern "C" fn oneloop__ghost__g_sep__complex(s: C, ln_s: C, ln_s_pl_1: C) -> C {
        inlines::g_sep(s, ln_s, ln_s_pl_1)
    }

    #[no_mangle]
    pub extern "C" fn oneloop__ghost__g__complex(s: C) -> C {
        inlines::g(s)
    }

    #[no_mangle]
    pub extern "C" fn oneloop__ghost__g_xi_sep(ln_s: R) -> R {
        inlines::g_xi_sep(ln_s)
    }

    #[no_mangle]
    pub extern "C" fn oneloop__ghost__g_xi(s: R) -> R {
        inlines::g_xi(s)
    }

    #[no_mangle]
    pub extern "C" fn oneloop__ghost__g_xi_sep__complex(ln_s: C) -> C {
        inlines::g_xi_sep(ln_s)
    }

    #[no_mangle]
    pub extern "C" fn oneloop__ghost__g_xi__complex(s: C) -> C {
        inlines::g_xi(s)
    }

    #[no_mangle]
    pub extern "C" fn ym__oneloop__ghost__dressing_inv_landau_sep(
        s: R,
        ln_s: R,
        ln_s_pl_1: R,
        g0: R,
    ) -> R {
        inlines::dressing_inv_landau_sep(s, ln_s, ln_s_pl_1, g0)
    }

    #[no_mangle]
    pub extern "C" fn ym__oneloop__ghost__dressing_inv_landau_sep__complex(
        s: C,
        ln_s: C,
        ln_s_pl_1: C,
        g0: R,
    ) -> C {
        inlines::dressing_inv_landau_sep(s, ln_s, ln_s_pl_1, g0)
    }

    #[no_mangle]
    pub extern "C" fn ym__oneloop__ghost__dressing_inv_sep(
        s: R,
        ln_s: R,
        ln_s_pl_1: R,
        g0: R,
        xi: R,
    ) -> R {
        inlines::dressing_inv_sep(s, ln_s, ln_s_pl_1, g0, xi)
    }

    #[no_mangle]
    pub extern "C" fn ym__oneloop__ghost__dressing_inv_sep__complex(
        s: C,
        ln_s: C,
        ln_s_pl_1: C,
        g0: R,
        xi: R,
    ) -> C {
        inlines::dressing_inv_sep(s, ln_s, ln_s_pl_1, g0, xi)
    }
}

// For internal use only
//
// - Here we define the building blocks for the other functions. This module
//   serves two purposes: to hold inlined functions and to provide a single
//   source of truth for the actual mathematical expressions
pub(crate) mod inlines {
    use crate::{Num, R};

    #[inline(always)]
    pub fn l_g<T: Num>(s: T, ln_s: T, ln_s_pl_1: T) -> T {
        let sinv = s.inv();
        let sinv2 = sinv * sinv;
        let s_pl_1 = s + 1.;
        let s_pl_1_2 = s_pl_1 * s_pl_1;
        s_pl_1_2 * (sinv * 2. - sinv2) * ln_s_pl_1 - s * 2. * ln_s
    }

    #[inline(always)]
    pub fn r_g<T: Num>(s: T) -> T {
        s.inv() + 2.
    }

    #[inline(always)]
    pub fn g_sep<T: Num>(s: T, ln_s: T, ln_s_pl_1: T) -> T {
        (l_g(s, ln_s, ln_s_pl_1) + r_g(s)) / 12.
    }

    #[inline(always)]
    pub fn g<T: Num>(s: T) -> T {
        let s_pl_1 = s + 1.;
        let ln_s = s.ln();
        let ln_s_pl_1 = s_pl_1.ln();
        g_sep(s, ln_s, ln_s_pl_1)
    }

    #[inline(always)]
    pub fn g_xi_sep<T: Num>(ln_s: T) -> T {
        -ln_s / 12.
    }

    #[inline(always)]
    pub fn g_xi<T: Num>(s: T) -> T {
        -s.ln() / 12.
    }

    #[inline(always)]
    pub fn dressing_inv_landau_sep<T: Num>(s: T, ln_s: T, ln_s_pl_1: T, g0: R) -> T {
        g_sep(s, ln_s, ln_s_pl_1) + g0
    }

    #[inline(always)]
    pub fn dressing_inv_sep<T: Num>(s: T, ln_s: T, ln_s_pl_1: T, g0: R, xi: R) -> T {
        dressing_inv_landau_sep(s, ln_s, ln_s_pl_1, g0) + g_xi_sep(ln_s) * xi
    }
}

#[cfg(test)]
mod tests {
    use crate::low_level::oneloop::ghost;
    use crate::{Num, C, I, R};

    const TOLERANCE: R = 1e-12;

    const REAL_TEST_VAL: [R; 4] = [1., 1.834, 2.5, 8.18];
    const COMPLEX_TEST_VAL: [C; 5] = [
        C { re: 1., im: -0.5 },
        C { re: 1., im: 0.5 },
        C { re: 1.347, im: 0. },
        C {
            re: 2.56,
            im: 0.732,
        },
        C {
            re: 7.28,
            im: 5.166,
        },
    ];

    fn assert_equal<T: Num>(lhs: T, rhs: T) {
        if rhs != T::zero() {
            assert!(
                (lhs / rhs - 1.).abs() < TOLERANCE,
                "|lhs-rhs| = |({lhs}) - ({rhs})| >= {TOLERANCE:e} rhs"
            );
        } else {
            assert!(
                (lhs - rhs).abs() < TOLERANCE,
                "|lhs-rhs| = |({lhs}) - ({rhs})| >= {TOLERANCE:e}"
            );
        }
    }

    #[test]
    fn test_g() {
        use ghost::ffi::{oneloop__ghost__g, oneloop__ghost__g__complex};
        use ghost::g;

        const REAL_RESULTS: [R; 4] = [
            0.48104906018664845,
            0.579741749632399,
            0.6366840011360758,
            0.8855899339647263,
        ];
        let complex_results: [C; 5] = [
            0.4923979959662195 - 0.07124251080687459 * I,
            0.4923979959662195 + 0.07124251080687459 * I,
            0.5272889838654602 + 0. * I,
            0.6472387703649405 + 0.053746283212455476 * I,
            0.9017966251236884 + 0.14090558554076238 * I,
        ];

        REAL_TEST_VAL
            .iter()
            .enumerate()
            .for_each(|(i, &s)| assert_equal(g(s), REAL_RESULTS[i]));

        REAL_TEST_VAL
            .iter()
            .enumerate()
            .for_each(|(i, &s)| assert_equal(oneloop__ghost__g(s), REAL_RESULTS[i]));

        COMPLEX_TEST_VAL
            .iter()
            .enumerate()
            .for_each(|(i, &s)| assert_equal(g(s), complex_results[i]));

        COMPLEX_TEST_VAL
            .iter()
            .enumerate()
            .for_each(|(i, &s)| assert_equal(oneloop__ghost__g__complex(s), complex_results[i]));
    }

    #[test]
    fn test_g_xi() {
        use ghost::ffi::{oneloop__ghost__g_xi, oneloop__ghost__g_xi__complex};
        use ghost::g_xi;

        const REAL_RESULTS: [R; 4] = [
            -0.0,
            -0.05054161448618943,
            -0.07635756098951292,
            -0.1751410125512213,
        ];
        let complex_results: [C; 5] = [
            -0.009297647971425406 + 0.038637300750067174 * I,
            -0.009297647971425406 - 0.038637300750067174 * I,
            -0.024823324785685573 + 0. * I,
            -0.08160850400245938 - 0.023208834563292327 * I,
            -0.18242055664494833 - 0.05142917783424054 * I,
        ];

        REAL_TEST_VAL
            .iter()
            .enumerate()
            .for_each(|(i, &s)| assert_equal(g_xi(s), REAL_RESULTS[i]));

        REAL_TEST_VAL
            .iter()
            .enumerate()
            .for_each(|(i, &s)| assert_equal(oneloop__ghost__g_xi(s), REAL_RESULTS[i]));

        COMPLEX_TEST_VAL
            .iter()
            .enumerate()
            .for_each(|(i, &s)| assert_equal(g_xi(s), complex_results[i]));

        COMPLEX_TEST_VAL
            .iter()
            .enumerate()
            .for_each(|(i, &s)| assert_equal(oneloop__ghost__g_xi__complex(s), complex_results[i]));
    }
}