holos-tda 0.9.0

Vietoris-Rips persistence and checked degree-Rips modules
Documentation
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use std::sync::Arc;

use crate::{
    Bar, CertificateLimits, ClassCorrespondence, Diagram, EdgeKey, GradedReductionCertificate,
    RelativeInterfaceCertificate, RipsParams, SparseDistanceMatrix,
};

/// Bounds and decomposition choices for a [`PersistenceIndex`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct IndexParams {
    /// Largest vertex separator considered by the deterministic search.
    pub max_separator_width: usize,
    /// Largest total candidate count across one tree compilation.
    pub separator_search_limit: usize,
    /// A scope at or below this vertex count remains a leaf.
    pub leaf_vertices: usize,
    /// How parent interfaces compose or retain reductions.
    pub interface_policy: InterfacePolicy,
}

impl Default for IndexParams {
    fn default() -> Self {
        Self {
            max_separator_width: 4,
            separator_search_limit: 100_000,
            leaf_vertices: 4,
            interface_policy: InterfacePolicy::Relative,
        }
    }
}

/// Policy for parent interfaces.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InterfacePolicy {
    /// Compose relative cores through arbitrary protected separators.
    Relative,
    /// Compose at a certified separator and materialize other parents.
    Compose,
    /// Retain a reduction over every parent scope.
    Materialize,
}

/// Structural and algebraic size of one compiled separator tree.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct IndexSummary {
    /// Highest homology dimension maintained by this index.
    pub max_dim: usize,
    /// Interface nodes in the tree, including the root.
    pub nodes: usize,
    /// Nodes without children.
    pub leaves: usize,
    /// Nodes split by a nonempty separator.
    pub separators: usize,
    /// Nodes split into disconnected components.
    pub component_splits: usize,
    /// Largest separator used by the tree.
    pub widest_separator: usize,
    /// Largest vertex scope retained by one materialized interface.
    pub largest_interface_vertices: usize,
    /// Largest listed-edge scope retained by one materialized interface.
    pub largest_interface_edges: usize,
    /// Interfaces composed without a reduction over their full scope.
    pub composed_interfaces: usize,
    /// Interfaces that retain a checked reduction over their full scope.
    pub materialized_interfaces: usize,
    /// Interfaces that retain an exact relative filtered core.
    pub relative_interfaces: usize,
    /// Cells supplied to all relative interfaces before cancellation.
    pub relative_input_cells: usize,
    /// Cells retained by all relative interfaces after cancellation.
    pub relative_core_cells: usize,
    /// Largest cell count retained by one relative interface.
    pub largest_relative_core_cells: usize,
    /// Equal-filtration pairs removed across all relative interfaces.
    pub relative_cancellations: usize,
    /// Whether the root omits a reduction over its full scope.
    pub root_composed: bool,
    /// Candidate vertex sets checked during decomposition.
    pub separator_candidates_checked: usize,
    /// Whether the bounded search visited every candidate in scope.
    pub separator_search_complete: bool,
}

/// Exact work charged to one index transition.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct IndexWork {
    /// Envelope edges compared when a full graph was supplied.
    pub edges_checked: usize,
    /// Tree nodes whose scopes contained at least one changed edge.
    pub nodes_touched: usize,
    /// Tree nodes shared with the preceding version.
    pub nodes_shared: usize,
    /// Touched reductions that retained at least one dependency column.
    pub nodes_repaired: usize,
    /// Touched reductions rebuilt without a retained dependency column.
    pub nodes_rebuilt: usize,
    /// Touched interfaces composed from their direct children.
    pub nodes_composed: usize,
    /// Touched relative leaf cores rebuilt from their local graph.
    pub relative_nodes_rebuilt: usize,
    /// Touched relative parent cores recomposed from child cores.
    pub relative_nodes_composed: usize,
    /// Cells supplied to touched relative interfaces before cancellation.
    pub relative_input_cells: usize,
    /// Cells retained by touched relative interfaces after cancellation.
    pub relative_core_cells: usize,
    /// Equal-filtration pairs removed in touched relative interfaces.
    pub relative_cancellations: usize,
    /// Reduction columns retained without another reduction pass.
    pub reduction_columns_reused: usize,
    /// Reduction columns processed by repair or rebuild.
    pub reduction_columns_reduced: usize,
    /// Sparse column additions performed during repair.
    pub reduction_column_additions: usize,
}

/// How an index produced a new version.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IndexUpdateMode {
    /// The supplied graph was bit-for-bit equal to the current graph.
    Unchanged,
    /// Every changed node retained at least one reduction column.
    Repaired,
    /// Changed routes required no reduction over a parent scope.
    Composed,
    /// Changed routes were rebuilt from exact local relative cores.
    Relative,
    /// At least one changed node rebuilt its complete reduction.
    Rebuilt,
    /// The listed-edge envelope changed and a new tree was compiled.
    Recompiled,
}

/// Why an index transition changed execution state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum IndexEventKind {
    /// An edge crossed the fixed filtration threshold.
    ThresholdCrossing,
    /// A reduction retained a valid dependency prefix.
    ReductionRepaired,
    /// A reduction retained no dependency prefix.
    ReductionRebuilt,
    /// A parent diagram was composed from checked child interfaces.
    InterfaceComposed,
    /// A relative leaf core was rebuilt from its local graph.
    RelativeCoreRebuilt,
    /// A relative parent core was recomposed from direct child cores.
    RelativeCoreComposed,
    /// The fixed listed-edge envelope changed.
    EnvelopeRecompiled,
}

/// One event reported by an index transition.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IndexEvent {
    /// Event kind.
    pub kind: IndexEventKind,
    /// Content identifier of the affected old node, when one exists.
    pub node: Option<[u8; 32]>,
    /// First changed edge in the node, when one exists.
    pub edge: Option<EdgeKey>,
}

/// Exact multiset difference between two persistence diagrams.
#[derive(Debug, Clone, Default)]
pub struct DiagramDelta {
    /// Intervals removed from the preceding version.
    pub removed: Vec<Bar>,
    /// Intervals added in the new version.
    pub added: Vec<Bar>,
}

/// One change to an edge already present in an index envelope.
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub enum IndexEdit {
    /// Set the edge to the supplied non-negative finite weight.
    SetWeight {
        /// Edge in the fixed listed-edge envelope.
        edge: EdgeKey,
        /// New edge weight.
        value: f64,
    },
    /// Move an envelope edge into the finite filtration.
    Activate {
        /// Edge in the fixed listed-edge envelope.
        edge: EdgeKey,
        /// New edge weight at or below the finite threshold.
        value: f64,
    },
    /// Keep the edge listed but place it above the finite threshold.
    Deactivate {
        /// Edge in the fixed listed-edge envelope.
        edge: EdgeKey,
    },
}

impl IndexEdit {
    /// Construct a weight change in canonical endpoint order.
    pub fn set_weight(u: usize, v: usize, value: f64) -> Self {
        Self::SetWeight {
            edge: EdgeKey::new(u, v),
            value,
        }
    }

    /// Construct an edge deactivation in canonical endpoint order.
    pub fn deactivate(u: usize, v: usize) -> Self {
        Self::Deactivate {
            edge: EdgeKey::new(u, v),
        }
    }

    /// Construct an edge activation in canonical endpoint order.
    pub fn activate(u: usize, v: usize, value: f64) -> Self {
        Self::Activate {
            edge: EdgeKey::new(u, v),
            value,
        }
    }

    pub(crate) fn edge(self) -> EdgeKey {
        match self {
            Self::SetWeight { edge, .. }
            | Self::Activate { edge, .. }
            | Self::Deactivate { edge } => edge,
        }
    }
}

/// One atomic active-topology patch inside a fixed edge envelope.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct TopologyPatch {
    edits: Vec<IndexEdit>,
}

impl TopologyPatch {
    /// Construct a patch from edge edits applied as one transaction.
    pub fn new(edits: Vec<IndexEdit>) -> Self {
        Self { edits }
    }

    /// Edits in this transaction.
    pub fn edits(&self) -> &[IndexEdit] {
        &self.edits
    }

    /// Consume this patch and return its edits.
    pub fn into_edits(self) -> Vec<IndexEdit> {
        self.edits
    }
}

/// Difference between two index versions.
#[derive(Debug, Clone)]
pub struct IndexDiff {
    /// Whether both versions use the same listed-edge envelope.
    pub same_envelope: bool,
    /// Tree nodes physically shared by both versions.
    pub shared_nodes: usize,
    /// Exact persistence-diagram difference.
    pub diagram: DiagramDelta,
}

/// One ordered alternative advanced from a shared index version.
#[derive(Debug, Clone)]
pub struct IndexBranch {
    /// Position of the alternative in the input batch.
    pub index: usize,
    /// Transition from the shared branch point.
    pub transition: IndexTransition,
}

/// Summary of one filtered interface.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InterfaceSummary {
    /// Content identifier of this node.
    pub digest: [u8; 32],
    /// Depth from the root.
    pub depth: usize,
    /// Original labeled vertices in the node scope.
    pub vertices: Vec<usize>,
    /// Original labeled separator vertices shared by its children.
    pub separator: Vec<usize>,
    /// Original labeled vertices fixed for every ancestor composition.
    pub protected_vertices: Vec<usize>,
    /// Listed edges in the node scope.
    pub edges: usize,
    /// Direct child count.
    pub children: usize,
    /// Algebra used by this interface.
    pub mode: InterfaceMode,
    /// Boundary columns in the checked reduction.
    pub reduction_columns: usize,
    /// Boundary-column counts for simplex dimensions one through `max_dim + 1`.
    pub columns_by_dimension: Vec<usize>,
    /// Cells supplied before relative cancellation.
    pub relative_input_cells: usize,
    /// Cells retained by the relative interface.
    pub relative_core_cells: usize,
    /// Equal-filtration pairs removed by relative cancellation.
    pub relative_cancellations: usize,
}

/// Algebra retained by one separator interface.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InterfaceMode {
    /// The interface stores a filtered core relative to its parent boundary.
    Relative,
    /// The interface stores a reduction over its full vertex scope.
    Materialized,
    /// The scope is a disjoint union of its child scopes.
    Disjoint,
    /// Child scopes meet in one zero-filtration simplex.
    ZeroSimplex,
    /// Child scopes meet in a zero-filtration flag cone.
    ZeroCone,
}

/// Result of constructing a new index version.
#[derive(Debug, Clone)]
pub struct IndexTransition {
    /// New exact index version.
    pub index: PersistenceIndex,
    /// How the version was produced.
    pub mode: IndexUpdateMode,
    /// Exact diagram difference.
    pub delta: DiagramDelta,
    /// Transition events.
    pub events: Vec<IndexEvent>,
    /// Optional exact class relations on the common filtered subcomplex.
    pub correspondence: Vec<ClassCorrespondence>,
    /// Exact work charged to this transition.
    pub work: IndexWork,
}

#[derive(Debug, Clone)]
pub(crate) struct InterfaceNode {
    pub(crate) digest: [u8; 32],
    pub(crate) vertices: Vec<usize>,
    pub(crate) edge_positions: Vec<usize>,
    pub(crate) separator: Vec<usize>,
    pub(crate) protected_vertices: Vec<usize>,
    pub(crate) children: Vec<Arc<InterfaceNode>>,
    pub(crate) state: InterfaceState,
}

#[derive(Debug, Clone)]
pub(crate) enum InterfaceState {
    Relative(RelativeInterfaceCertificate),
    Materialized(GradedReductionCertificate),
    Composed {
        mode: InterfaceMode,
        diagram: Diagram,
    },
}

impl InterfaceNode {
    pub(crate) fn diagram(&self) -> &Diagram {
        match &self.state {
            InterfaceState::Relative(relative) => relative.diagram(),
            InterfaceState::Materialized(reduction) => reduction.diagram(),
            InterfaceState::Composed { diagram, .. } => diagram,
        }
    }

    pub(crate) fn mode(&self) -> InterfaceMode {
        match &self.state {
            InterfaceState::Relative(_) => InterfaceMode::Relative,
            InterfaceState::Materialized(_) => InterfaceMode::Materialized,
            InterfaceState::Composed { mode, .. } => *mode,
        }
    }

    pub(crate) fn reduction(&self) -> Option<&GradedReductionCertificate> {
        match &self.state {
            InterfaceState::Materialized(reduction) => Some(reduction),
            InterfaceState::Relative(_) | InterfaceState::Composed { .. } => None,
        }
    }

    pub(crate) fn relative(&self) -> Option<&RelativeInterfaceCertificate> {
        match &self.state {
            InterfaceState::Relative(relative) => Some(relative),
            InterfaceState::Materialized(_) | InterfaceState::Composed { .. } => None,
        }
    }
}

/// An immutable, versioned persistence index.
///
/// The listed vertices and edges form an envelope. Weight changes and
/// threshold crossings inside that envelope preserve the separator tree.
/// A different listed-edge set compiles a new tree and reports that event.
#[derive(Debug, Clone)]
pub struct PersistenceIndex {
    pub(crate) params: RipsParams,
    pub(crate) index_params: IndexParams,
    pub(crate) limits: CertificateLimits,
    pub(crate) graph: Arc<SparseDistanceMatrix>,
    pub(crate) topology: Arc<Vec<EdgeKey>>,
    pub(crate) root: Arc<InterfaceNode>,
    pub(crate) summary: IndexSummary,
}