1use symbolica::{
4 atom::{Atom, AtomCore, AtomView, Symbol},
5 function,
6 id::Replacement,
7 symbol,
8};
9
10use crate::{CffError, CffExpression, EdgeId, EnergySurface, HSurface, Surface, SurfaceCache};
11
12impl EnergySurface {
13 pub fn to_atom_with(
15 &self,
16 energy: impl Fn(EdgeId) -> Atom,
17 external_energy: impl Fn(EdgeId) -> Atom,
18 ) -> Atom {
19 self.energies
20 .iter()
21 .fold(Atom::Zero, |sum, edge| sum + energy(*edge))
22 + self
23 .external_shift
24 .iter()
25 .fold(Atom::Zero, |sum, (edge, sign)| {
26 sum + Atom::num(*sign) * external_energy(*edge)
27 })
28 }
29}
30
31impl HSurface {
32 pub fn to_atom_with(
34 &self,
35 energy: impl Fn(EdgeId) -> Atom,
36 external_energy: impl Fn(EdgeId) -> Atom,
37 ) -> Atom {
38 self.positive_energies
39 .iter()
40 .fold(Atom::Zero, |sum, edge| sum + energy(*edge))
41 - self
42 .negative_energies
43 .iter()
44 .fold(Atom::Zero, |sum, edge| sum + energy(*edge))
45 + self
46 .external_shift
47 .iter()
48 .fold(Atom::Zero, |sum, (edge, sign)| {
49 sum + Atom::num(*sign) * external_energy(*edge)
50 })
51 }
52}
53
54impl Surface {
55 pub fn to_atom_with(
56 &self,
57 energy: impl Fn(EdgeId) -> Atom,
58 external_energy: impl Fn(EdgeId) -> Atom,
59 ) -> Atom {
60 match self {
61 Self::Energy(surface) => surface.to_atom_with(energy, external_energy),
62 Self::H(surface) => surface.to_atom_with(energy, external_energy),
63 Self::Unit => Atom::num(1),
64 Self::Infinite => crate::SurfaceId::Infinite.into(),
65 }
66 }
67}
68
69impl SurfaceCache {
70 pub fn replacements_with(
72 &self,
73 energy: impl Fn(EdgeId) -> Atom,
74 external_energy: impl Fn(EdgeId) -> Atom,
75 ) -> Vec<Replacement> {
76 self.energy_surfaces()
77 .iter()
78 .enumerate()
79 .map(|(i, surface)| {
80 Replacement::new(
81 Atom::from(crate::EnergySurfaceId(i)),
82 surface.to_atom_with(&energy, &external_energy),
83 )
84 })
85 .chain(self.h_surfaces().iter().enumerate().map(|(i, surface)| {
86 Replacement::new(
87 Atom::from(crate::HSurfaceId(i)),
88 surface.to_atom_with(&energy, &external_energy),
89 )
90 }))
91 .collect()
92 }
93}
94
95impl CffExpression {
96 pub fn to_atom(&self) -> Atom {
98 self.orientations
99 .iter()
100 .fold(Atom::Zero, |sum, orientation| sum + orientation.to_atom())
101 }
102
103 pub fn measure_normalization(loop_number: usize) -> Atom {
105 (-Atom::i()).pow(loop_number as i64)
106 / (Atom::var(Symbol::PI) * 2).pow(3 * loop_number as i64)
107 }
108
109 pub fn inverse_energy_product(energies: impl IntoIterator<Item = Atom>) -> Atom {
111 energies
112 .into_iter()
113 .fold(Atom::num(1), |product, energy| product / (-2 * energy))
114 }
115}
116
117pub struct SurfacePole {
123 pub surface: Atom,
124 pub order: usize,
125}
126
127impl SurfacePole {
128 pub fn laurent_coefficient(
131 &self,
132 coefficient: &Atom,
133 variable: Symbol,
134 root: &Atom,
135 laurent_power: i64,
136 ) -> Result<Atom, CffError> {
137 if self.order == 0 || laurent_power >= 0 || laurent_power.unsigned_abs() > self.order as u64
138 {
139 return Err(CffError::Invariant(
140 "a Laurent power must lie between minus the positive pole order and -1".into(),
141 ));
142 }
143 if root.contains_symbol(variable) {
144 return Err(CffError::Invariant(
145 "a residue root must not depend on its integration variable".into(),
146 ));
147 }
148 let order = i64::try_from(self.order)
149 .map_err(|_| CffError::Invariant("pole order exceeds i64".into()))?;
150 let root_value = self.surface.replace(variable).with(root.to_pattern());
151 if matches!(root_value.as_view(), AtomView::Num(_)) && root_value != Atom::Zero {
152 return Err(CffError::Invariant(
153 "the supplied residue root is not a zero of the surface".into(),
154 ));
155 }
156 if self
157 .surface
158 .derivative(variable)
159 .replace(variable)
160 .with(root.to_pattern())
161 .expand()
162 == Atom::Zero
163 {
164 return Err(CffError::Invariant(
165 "the supplied residue root must be simple".into(),
166 ));
167 }
168 let expansion = self
169 .surface
170 .series(variable, root.clone(), (order, 1))
171 .map_err(|error| CffError::Invariant(format!("cannot expand pole surface: {error}")))?
172 .to_atom()
173 - root_value;
174 let delta = symbol!("feynkit_cff::residue_delta");
175 let mut regular =
176 coefficient * expansion.pow(-order) * (Atom::var(variable) - root).pow(order);
177 let derivative_order = (order + laurent_power) as usize;
178 for _ in 0..derivative_order {
179 regular = regular.derivative(variable);
180 }
181 regular = regular.replace(Atom::var(variable) - root).with(delta);
184 let mut result = regular
185 .series(delta, Atom::Zero, (0, 1))
186 .map_err(|error| CffError::Invariant(format!("cannot evaluate pole residue: {error}")))?
187 .to_atom();
188 for factor in 2..=derivative_order {
189 result /= Atom::num(factor as i64);
190 }
191 Ok(result.replace(variable).with(root.to_pattern()))
192 }
193
194 pub fn residue(
195 &self,
196 coefficient: &Atom,
197 variable: Symbol,
198 root: &Atom,
199 ) -> Result<Atom, CffError> {
200 self.laurent_coefficient(coefficient, variable, root, -1)
201 }
202}
203
204#[derive(Clone, Debug)]
211pub struct CutPropagator {
212 pub energy: Atom,
213 pub on_shell_energy: Atom,
214 pub power: usize,
215 pub orientation: i8,
216 pub prescription: i8,
217 pub normalization: Atom,
219}
220
221impl CutPropagator {
222 pub fn validate(&self) -> Result<(), CffError> {
223 if self.power == 0 || self.power > i64::MAX as usize {
224 return Err(CffError::Invariant(
225 "cut propagator power must be a positive integer representable as i64".into(),
226 ));
227 }
228 if self.on_shell_energy == Atom::Zero {
229 return Err(CffError::Invariant(
230 "cut on-shell energy must be nonzero so the energy roots are distinct".into(),
231 ));
232 }
233 if ![-1, 1].contains(&self.orientation) || ![-1, 1].contains(&self.prescription) {
234 return Err(CffError::Invariant(
235 "cut orientation and prescription must be +1 or -1".into(),
236 ));
237 }
238 Ok(())
239 }
240
241 pub fn to_atom(&self) -> Result<Atom, CffError> {
243 self.validate()?;
244 Ok(&self.normalization
245 * Atom::num(self.prescription as i64)
246 * function!(
247 symbol!("CutDelta"),
248 self.power as i64,
249 self.energy.pow(2) - self.on_shell_energy.pow(2),
250 &self.energy,
251 self.orientation as i64
252 ))
253 }
254
255 pub fn to_energy_atom(&self) -> Result<Atom, CffError> {
257 self.validate()?;
258 let root = Atom::num(self.orientation as i64) * &self.on_shell_energy;
259 Ok(&self.normalization
260 * Atom::num((self.prescription * self.orientation) as i64)
261 * function!(symbol!("Delta"), self.power as i64, &self.energy - &root)
262 / (&self.energy + root).pow(self.power as i64))
263 }
264
265 pub fn apply(&self, coefficient: &Atom, variable: Symbol) -> Result<Atom, CffError> {
269 self.validate()?;
270 if self.energy != Atom::var(variable) || self.on_shell_energy.contains_symbol(variable) {
271 return Err(CffError::Invariant("cut action requires an independent energy variable and an on-shell energy independent of it".into()));
272 }
273 let root = Atom::num(self.orientation as i64) * &self.on_shell_energy;
274 let mut regular = coefficient / (&self.energy + &root).pow(self.power as i64);
275 for order in 1..self.power {
276 regular = regular.derivative(variable) / Atom::num(order as i64);
277 }
278 Ok(&self.normalization
279 * Atom::num((self.prescription * self.orientation) as i64)
280 * regular.replace(variable).with(root.to_pattern()))
281 }
282}
283
284#[cfg(test)]
285mod tests {
286 use super::*;
287 use symbolica::parse;
288
289 #[test]
290 fn residue_rejects_invalid_orders_and_non_simple_roots() {
291 let x = symbol!("cut_test::invalid_x");
292 let pole = SurfacePole {
293 surface: Atom::var(x).pow(2),
294 order: 1,
295 };
296 assert!(pole.residue(&Atom::num(1), x, &Atom::Zero).is_err());
297 assert!(pole.residue(&Atom::num(1), x, &Atom::num(1)).is_err());
298 assert!(
299 pole.laurent_coefficient(&Atom::num(1), x, &Atom::Zero, 0)
300 .is_err()
301 );
302 }
303
304 #[test]
305 fn energy_lowering_and_normalization_preserve_runtime_signs() {
306 let h = HSurface {
307 positive_energies: vec![EdgeId::new(0)],
308 negative_energies: vec![EdgeId::new(1)],
309 external_shift: vec![(EdgeId::new(2), -1)].into(),
310 vertex_set: crate::VertexSet::default(),
311 };
312 let e = |edge: EdgeId| function!(symbol!("cut_test::E"), edge.index() as i64);
313 let p = |edge: EdgeId| function!(symbol!("cut_test::P"), edge.index() as i64);
314 assert_eq!(
315 h.to_atom_with(e, p),
316 e(EdgeId::new(0)) - e(EdgeId::new(1)) - p(EdgeId::new(2))
317 );
318 assert_eq!(
319 CffExpression::inverse_energy_product([Atom::num(2), Atom::num(3)]),
320 Atom::num(1) / 24
321 );
322 assert_eq!(CffExpression::inverse_energy_product([]), Atom::num(1));
323 assert_eq!(CffExpression::measure_normalization(0), Atom::num(1));
324 assert_eq!(
325 CffExpression::measure_normalization(1),
326 -Atom::i() / (Atom::num(2) * Atom::var(Symbol::PI)).pow(3)
327 );
328 }
329
330 #[test]
331 fn raised_cut_action_equals_energy_residue_for_both_orientations() {
332 let x = symbol!("cut_test::x");
333 let energy = parse!("sqrt(cut_test::k^2+cut_test::m^2)");
334 let coefficient = (Atom::var(x) + 3).pow(4);
335 for power in 1..=3 {
336 for orientation in [-1, 1] {
337 let cut = CutPropagator {
338 energy: Atom::var(x),
339 on_shell_energy: energy.clone(),
340 power,
341 orientation,
342 prescription: 1,
343 normalization: -2 * Atom::var(Symbol::PI) * Atom::i(),
344 };
345 let pole = SurfacePole {
346 surface: Atom::var(x).pow(2) - energy.pow(2),
347 order: power,
348 };
349 let root = Atom::num(orientation as i64) * &energy;
350 let residue = pole.residue(&coefficient, x, &root).unwrap();
351 let expected = &cut.normalization * Atom::num(orientation as i64) * residue;
352 assert_eq!(
353 (cut.apply(&coefficient, x).unwrap() - expected)
354 .together()
355 .expand(),
356 Atom::Zero
357 );
358 }
359 }
360 }
361
362 #[test]
363 fn triple_cut_differentiates_the_uncut_factor_and_coefficient() {
364 let x = symbol!("cut_test::q0");
365 let cut = CutPropagator {
366 energy: Atom::var(x),
367 on_shell_energy: Atom::num(2),
368 power: 3,
369 orientation: 1,
370 prescription: 1,
371 normalization: Atom::num(1),
372 };
373 assert_eq!(
374 cut.apply(&Atom::var(x).pow(2), x).unwrap(),
375 Atom::num(-1) / 128
376 );
377 let mut opposite = cut.clone();
378 opposite.prescription = -1;
379 assert_eq!(
380 opposite.apply(&Atom::var(x).pow(2), x).unwrap(),
381 Atom::num(1) / 128
382 );
383 }
384
385 #[test]
386 fn implicit_surface_laurent_coefficients_keep_surface_derivatives() {
387 let x = symbol!("cut_test::t");
388 let root = Atom::var(symbol!("cut_test::root"));
389 let surface = function!(symbol!("cut_test::eta"), x);
390 let coefficient = function!(symbol!("cut_test::f"), x);
391 let pole = SurfacePole {
392 surface: surface.clone(),
393 order: 2,
394 };
395 let derivative = surface.derivative(x).replace(x).with(root.to_pattern());
396 let expected = coefficient.derivative(x).replace(x).with(root.to_pattern())
397 / derivative.pow(2)
398 - coefficient.replace(x).with(root.to_pattern())
399 * surface
400 .derivative(x)
401 .derivative(x)
402 .replace(x)
403 .with(root.to_pattern())
404 / derivative.pow(3);
405 assert_eq!(
406 (pole.residue(&coefficient, x, &root).unwrap() - expected)
407 .together()
408 .expand(),
409 Atom::Zero
410 );
411 }
412}