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
//! rivrs-sparse — Sparse Linear Algebra
//!
//! This library heavily relies on [`faer`](https://crates.io/crates/faer) for
//! foundational data types (e.g., `SparseColMat`, `Triplet`, `Col`), dense
//! solvers, and some sparse linear algebra routines (e.g., AMD ordering).
//!
//! # Sparse Symmetric Indefinite Solver
//!
//! The library provides a sparse symmetric indefinite direct solver
//! based on the A Posteriori Threshold Pivoting (APTP) algorithm:
//!
//! - **Symbolic analysis**: Ordering, elimination tree, symbolic factorization
//! - **Numeric factorization**: LDL^T with APTP pivoting
//! - **Triangular solve**: Forward/backward substitution
//!
//! The primary references are:
//!
//! - Hogg, Duff, & Lopez (2020), "A New Sparse LDL^T Solver Using
//! A Posteriori Threshold Pivoting", SIAM J. Sci. Comput.
//! - Duff & Reid (1983), "The multifrontal solution of indefinite sparse
//! symmetric linear equations"
//! - Liu (1992), "The Multifrontal Method for Sparse Matrix Solution:
//! Theory and Practice", SIAM Review
//! - SPRAL (BSD-3-Clause) for reference implementation patterns
//!
//! See [NOTICE](../NOTICE) for complete attribution and licensing information.
//!
//! # Quick Start
//!
//! ```
//! use faer::sparse::{SparseColMat, Triplet};
//! use faer::Col;
//! use rivrs_sparse::symmetric::{SparseLDLT, SolverOptions};
//!
//! // Symmetric indefinite 2x2: A = [[2, 1], [1, -1]]
//! let triplets = vec![
//! Triplet::new(0, 0, 2.0),
//! Triplet::new(1, 0, 1.0), Triplet::new(0, 1, 1.0),
//! Triplet::new(1, 1, -1.0),
//! ];
//! let a = SparseColMat::try_new_from_triplets(2, 2, &triplets).unwrap();
//! let b = Col::from_fn(2, |i| [3.0, 0.0][i]);
//!
//! let x = SparseLDLT::solve_full(&a, &b, &SolverOptions::default()).unwrap();
//! assert!((x[0] - 1.0).abs() < 1e-12);
//! assert!((x[1] - 1.0).abs() < 1e-12);
//! ```
//!
//! # Three-Step Solver API
//!
//! For repeated solves with the same matrix or sparsity pattern, use the three-step
//! solve API:
//!
//! 1. Symbolic analysis (reusable for same sparsity pattern)
//! 2. Numeric factorization (reusable for same matrix)
//! 3. Triangular solve
//!
//! For instance, with multiple right-hand side vectors but the same matrix,
//! perform steps 1-2 once and then reuse the factorization for each vector.
//!
//! ```
//! use faer::sparse::{SparseColMat, Triplet};
//! use faer::{Col, Par};
//! use faer::dyn_stack::{MemBuffer, MemStack};
//! use rivrs_sparse::symmetric::{SparseLDLT, AnalyzeOptions, FactorOptions};
//!
//! // Build a symmetric matrix (full CSC — both triangles stored)
//! let triplets = vec![
//! Triplet::new(0, 0, 4.0),
//! Triplet::new(1, 0, 1.0), Triplet::new(0, 1, 1.0),
//! Triplet::new(1, 1, 3.0),
//! ];
//! let a = SparseColMat::try_new_from_triplets(2, 2, &triplets).unwrap();
//!
//! // Phase 1: Symbolic analysis
//! let mut solver = SparseLDLT::analyze_with_matrix(&a, &AnalyzeOptions::default())?;
//!
//! // Phase 2: Numeric factorization
//! solver.factor(&a, &FactorOptions::default())?;
//!
//! // Phase 3: Triangular solve
//! let b = Col::from_fn(2, |i| [5.0, 4.0][i]);
//! let req = solver.solve_scratch(1);
//! let mut mem = MemBuffer::new(req);
//! let x = solver.solve(&b, &mut MemStack::new(&mut mem), Par::Seq)?;
//! assert!((x[0] - 1.0).abs() < 1e-12);
//! assert!((x[1] - 1.0).abs() < 1e-12);
//! # Ok::<(), rivrs_sparse::error::SparseError>(())
//! ```
//!
//! # Ordering Strategies
//!
//! | Strategy | Best for | Notes |
//! |----------|----------|-------|
//! | [`MatchOrderMetis`](symmetric::OrderingStrategy::MatchOrderMetis) | Hard indefinite (KKT, saddle-point) | Default. MC64 matching + METIS |
//! | [`Metis`](symmetric::OrderingStrategy::Metis) | Easy indefinite (FEM, thermal) | Pure METIS nested dissection |
//! | [`Amd`](symmetric::OrderingStrategy::Amd) | Small matrices, unit tests | faer built-in AMD |
//!
//! # Feature Flags
//!
//! | Feature | Purpose |
//! |---------|---------|
//! | `diagnostic` | Per-supernode timing instrumentation. Zero overhead when disabled. |
//! | `test-util` | Test infrastructure: random matrix generators, property-based testing. |
//!
//! # Error Handling
//!
//! All fallible operations return [`Result<T, SparseError>`](error::SparseError).
//! Error variants are organized by solver phase:
//!
//! - **Analysis**: [`NotSquare`](error::SparseError::NotSquare),
//! [`AnalysisFailure`](error::SparseError::AnalysisFailure)
//! - **Factorization**: [`NumericalSingularity`](error::SparseError::NumericalSingularity),
//! [`StructurallySingular`](error::SparseError::StructurallySingular)
//! - **Solve**: [`SolveBeforeFactor`](error::SparseError::SolveBeforeFactor),
//! [`DimensionMismatch`](error::SparseError::DimensionMismatch)
//! - **I/O**: [`IoError`](error::SparseError::IoError),
//! [`ParseError`](error::SparseError::ParseError)
//!
//! # Matrix Storage Convention
//!
//! All input matrices must be stored as **full symmetric CSC** (both upper and
//! lower triangles). The [`io::mtx`] reader automatically mirrors entries from
//! Matrix Market files.
//!
//! # Parallelism
//!
//! Both factorization and solve support shared-memory parallelism via
//! `Par::Seq` (sequential) or `Par::rayon(n)` (parallel with `n` threads):
//!
//! ```no_run
//! # use faer::Par;
//! # use rivrs_sparse::symmetric::FactorOptions;
//! let opts = FactorOptions { par: Par::rayon(4), ..Default::default() };
//! ```
//!
//! Tree-level parallelism (independent subtrees via rayon) and intra-node
//! parallelism (TRSM/GEMM via faer `Par`) are both controlled by this setting.
//!
//! # Performance
//!
//! Factorization dominates total solve time for most matrices. On the
//! 65-matrix SuiteSparse benchmark suite, rivrs-sparse is competitive with
//! SPRAL (median 5% faster sequential, 10% faster at 8 threads). Tuning
//! [`FactorOptions::threshold`](symmetric::FactorOptions::threshold) (default
//! 0.01) trades off stability vs fill-in for specific problem classes.
//! See the [repository](https://github.com/pinetreelabs/rivrs-linalg/sparse) for
//! benchmarking details.
//!
//! # Examples
//!
//! See the [`examples/`](https://github.com/pinetreelabs/rivrs-linalg/tree/main/sparse/examples)
//! directory for complete programs:
//!
//! - `basic_usage.rs` — Self-contained hello world
//! - `multiple_rhs.rs` — Solve for multiple right-hand sides, reusing the factorization
//! - `refactorization.rs` — Refactorize with different values on the same sparsity pattern
//! - `solve_timing.rs` — End-to-end solve timing on SuiteSparse matrices
//! - `profile_matrix.rs` — Per-supernode profiling with Chrome Trace export
//! - `parallel_scaling.rs` — Parallel speedup measurement
use fmt;
use ;
/// Solver phases corresponding to the three-phase API: analyze → factorize → solve.
///
/// Variants are ordered to match the natural pipeline order. `Roundtrip` represents
/// the full end-to-end pipeline.