Skip to main content

feynkit_cff/
orientation.rs

1use linnet::half_edge::involution::{EdgeIndex, EdgeVec, Orientation};
2use serde::{Deserialize, Serialize};
3use symbolica::atom::{Atom, AtomCore};
4
5pub type EdgeOrientation = Orientation;
6pub type GraphOrientation = EdgeVec<Orientation>;
7
8/// Structural operations on a complete Linnet graph orientation.
9pub trait GraphOrientationExt {
10    /// Compare orientations while treating undirected reference edges as wildcards.
11    fn is_compatible_with(&self, reference: &Self) -> bool;
12}
13
14impl GraphOrientationExt for GraphOrientation {
15    fn is_compatible_with(&self, reference: &Self) -> bool {
16        orientation_entries_are_compatible(
17            self.iter().map(|(edge, orientation)| (edge, *orientation)),
18            reference
19                .iter()
20                .map(|(edge, orientation)| (edge, *orientation)),
21        )
22    }
23}
24
25fn orientation_entries_are_compatible(
26    left: impl ExactSizeIterator<Item = (EdgeIndex, Orientation)>,
27    reference: impl ExactSizeIterator<Item = (EdgeIndex, Orientation)>,
28) -> bool {
29    left.len() == reference.len()
30        && left
31            .zip(reference)
32            .all(|((left_id, left), (right_id, right))| {
33                left_id == right_id && (right == Orientation::Undirected || left == right)
34            })
35}
36
37/// Stable index of an orientation within a generated expression.
38#[derive(
39    Clone,
40    Copy,
41    Debug,
42    PartialEq,
43    Eq,
44    PartialOrd,
45    Ord,
46    Hash,
47    Serialize,
48    Deserialize,
49    bincode::Encode,
50    bincode::Decode,
51)]
52pub struct OrientationId(pub usize);
53
54impl OrientationId {
55    pub const fn index(self) -> usize {
56        self.0
57    }
58}
59
60impl From<usize> for OrientationId {
61    fn from(value: usize) -> Self {
62        Self(value)
63    }
64}
65
66impl From<OrientationId> for usize {
67    fn from(value: OrientationId) -> Self {
68        value.0
69    }
70}
71
72/// Exact weight `2^-halvings` multiplying one orientation's denominator tree.
73///
74/// Each of the two closures of a self-loop carries half of the tadpole
75/// contour, as in the shared recursion of `three-dimensional-reps`, so an
76/// orientation directing `k` self-loops has weight `2^-k`. Human-readable
77/// expressions without a weight field read as the unit weight. Compact binary
78/// encodings always carry the weight, so their layout differs from encodings
79/// made before this weight existed.
80#[derive(
81    Clone,
82    Copy,
83    Debug,
84    Default,
85    PartialEq,
86    Eq,
87    PartialOrd,
88    Ord,
89    Hash,
90    Serialize,
91    Deserialize,
92    bincode::Encode,
93    bincode::Decode,
94)]
95pub struct OrientationWeight {
96    halvings: u32,
97}
98
99impl OrientationWeight {
100    pub const UNIT: Self = Self { halvings: 0 };
101
102    pub const fn from_halvings(halvings: u32) -> Self {
103        Self { halvings }
104    }
105
106    /// Number of factors of one half in this weight.
107    pub const fn halvings(self) -> u32 {
108        self.halvings
109    }
110
111    pub const fn is_unit(&self) -> bool {
112        self.halvings == 0
113    }
114
115    /// The exact rational weight as a Symbolica number.
116    pub fn to_atom(self) -> Atom {
117        Atom::num(2).pow(-i64::from(self.halvings))
118    }
119}
120
121#[cfg(test)]
122mod tests {
123    use super::*;
124
125    #[test]
126    fn undirected_reference_edges_are_wildcards() {
127        let orientation =
128            GraphOrientation::from_iter([Orientation::Default, Orientation::Reversed]);
129        let reference =
130            GraphOrientation::from_iter([Orientation::Default, Orientation::Undirected]);
131
132        assert!(orientation.is_compatible_with(&reference));
133        assert!(!reference.is_compatible_with(&orientation));
134    }
135
136    #[test]
137    fn unequal_edge_coverage_is_incompatible_in_both_directions() {
138        let short = GraphOrientation::from_iter([Orientation::Default]);
139        let long = GraphOrientation::from_iter([Orientation::Default, Orientation::Undirected]);
140
141        assert!(!short.is_compatible_with(&long));
142        assert!(!long.is_compatible_with(&short));
143    }
144
145    #[test]
146    fn different_edge_ids_are_incompatible() {
147        let left = [(EdgeIndex(0), Orientation::Default)];
148        let reference = [(EdgeIndex(1), Orientation::Default)];
149
150        assert!(!orientation_entries_are_compatible(
151            left.into_iter(),
152            reference.into_iter()
153        ));
154    }
155}