apex-solver 1.3.0

High-performance nonlinear least squares optimization with Lie group support for SLAM and bundle adjustment
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Sparse Jacobian assembly using symbolic sparsity patterns.
//!
//! This module handles building the symbolic structure (sparsity pattern) once,
//! then efficiently filling in numerical values during each optimization iteration.
//! Uses `SparseColMat` from `faer` for the Jacobian representation.

use std::{
    collections::HashMap,
    sync::{Arc, Mutex},
};

use faer::{
    Col, Mat,
    sparse::{Argsort, Pair, SparseColMat, SymbolicSparseColMat},
};
use rayon::prelude::*;

use crate::error::ErrorLogging;
use crate::linearizer::{LinearizerError, LinearizerResult};

use super::super::linearize_block;
use crate::core::problem::{Problem, VariableEnum};

/// Symbolic structure for sparse matrix operations.
///
/// Contains the sparsity pattern (which entries are non-zero) and an ordering
/// for efficient numerical computation. This is computed once at the beginning
/// and reused throughout optimization.
///
/// # Fields
///
/// - `pattern`: The symbolic sparse column matrix structure (row/col indices of non-zeros)
/// - `order`: A fill-reducing ordering/permutation for numerical stability
pub struct SymbolicStructure {
    pub pattern: SymbolicSparseColMat<usize>,
    pub order: Argsort<usize>,
}

/// Build the symbolic sparsity structure for the Jacobian matrix.
///
/// This pre-computes which entries in the Jacobian will be non-zero based on the
/// factor graph connectivity. Each factor connecting variables contributes a dense
/// block to the Jacobian; this function records all such (row, col) pairs.
///
/// Called once before optimization begins. The resulting structure is reused for
/// efficient numerical assembly in [`assemble_sparse()`].
///
/// # Arguments
///
/// * `problem` - The optimization problem (provides residual blocks and their structure)
/// * `variables` - Current variable values (provides DOF sizes)
/// * `variable_index_map` - Maps variable names to their column offset in the Jacobian
/// * `total_dof` - Total degrees of freedom (number of columns)
pub fn build_symbolic_structure(
    problem: &Problem,
    variables: &HashMap<String, VariableEnum>,
    variable_index_map: &HashMap<String, usize>,
    total_dof: usize,
) -> LinearizerResult<SymbolicStructure> {
    let mut indices = Vec::<Pair<usize, usize>>::new();

    problem
        .residual_blocks()
        .iter()
        .for_each(|(_, residual_block)| {
            let mut variable_local_idx_size_list = Vec::<(usize, usize)>::new();
            let mut count_variable_local_idx: usize = 0;

            for var_key in &residual_block.variable_key_list {
                if let Some(variable) = variables.get(var_key) {
                    variable_local_idx_size_list
                        .push((count_variable_local_idx, variable.get_size()));
                    count_variable_local_idx += variable.get_size();
                }
            }

            for (i, var_key) in residual_block.variable_key_list.iter().enumerate() {
                if let Some(variable_global_idx) = variable_index_map.get(var_key) {
                    let (_, var_size) = variable_local_idx_size_list[i];

                    for row_idx in 0..residual_block.factor.get_dimension() {
                        for col_idx in 0..var_size {
                            let global_row_idx = residual_block.residual_row_start_idx + row_idx;
                            let global_col_idx = variable_global_idx + col_idx;
                            indices.push(Pair::new(global_row_idx, global_col_idx));
                        }
                    }
                }
            }
        });

    let (pattern, order) = SymbolicSparseColMat::try_new_from_indices(
        problem.total_residual_dimension,
        total_dof,
        &indices,
    )
    .map_err(|e| {
        LinearizerError::SymbolicStructure(
            "Failed to build symbolic sparse matrix structure".to_string(),
        )
        .log_with_source(e)
    })?;

    Ok(SymbolicStructure { pattern, order })
}

/// Assemble residuals and sparse Jacobian from the current variable values.
///
/// Evaluates all residual blocks in parallel, collecting per-block Jacobian values
/// in column-major order. Uses the pre-computed symbolic structure to efficiently
/// construct the final `SparseColMat`.
///
/// # Arguments
///
/// * `problem` - The optimization problem
/// * `variables` - Current variable values
/// * `variable_index_map` - Maps variable names to their column offset in the Jacobian
/// * `symbolic_structure` - Pre-computed sparsity pattern from [`build_symbolic_structure()`]
pub fn assemble_sparse(
    problem: &Problem,
    variables: &HashMap<String, VariableEnum>,
    variable_index_map: &HashMap<String, usize>,
    symbolic_structure: &SymbolicStructure,
) -> LinearizerResult<(Mat<f64>, SparseColMat<usize, f64>)> {
    let total_residual = Arc::new(Mutex::new(Col::<f64>::zeros(
        problem.total_residual_dimension,
    )));

    let total_nnz = symbolic_structure.pattern.compute_nnz();

    // Evaluate all blocks in parallel, collecting sparse Jacobian values
    let jacobian_blocks: Result<Vec<(usize, Vec<f64>)>, LinearizerError> = problem
        .residual_blocks()
        .par_iter()
        .map(|(_, residual_block)| {
            let values = scatter_sparse_block(
                residual_block,
                variables,
                variable_index_map,
                &total_residual,
            )?;
            let size = values.len();
            Ok((size, values))
        })
        .collect();

    let jacobian_blocks = jacobian_blocks?;

    // Flatten block values into a single contiguous array
    let mut jacobian_values = Vec::with_capacity(total_nnz);
    for (_size, mut block_values) in jacobian_blocks {
        jacobian_values.append(&mut block_values);
    }

    let total_residual = Arc::try_unwrap(total_residual)
        .map_err(|_| {
            LinearizerError::ParallelComputation(
                "Failed to unwrap Arc for total residual".to_string(),
            )
            .log()
        })?
        .into_inner()
        .map_err(|e| {
            LinearizerError::ParallelComputation(
                "Failed to extract mutex inner value for total residual".to_string(),
            )
            .log_with_source(e)
        })?;

    let residual_faer = total_residual.as_ref().as_mat().to_owned();
    let jacobian_sparse = SparseColMat::new_from_argsort(
        symbolic_structure.pattern.clone(),
        &symbolic_structure.order,
        jacobian_values.as_slice(),
    )
    .map_err(|e| {
        LinearizerError::SymbolicStructure(
            "Failed to create sparse Jacobian from argsort".to_string(),
        )
        .log_with_source(e)
    })?;

    Ok((residual_faer, jacobian_sparse))
}

/// Linearize a single block and extract Jacobian values in column-major order for sparse assembly.
///
/// Uses the shared [`linearize_block()`] helper, then scatters the block Jacobian
/// into a flat `Vec<f64>` matching the symbolic structure's expected ordering.
fn scatter_sparse_block(
    residual_block: &crate::core::residual_block::ResidualBlock,
    variables: &HashMap<String, VariableEnum>,
    variable_index_map: &HashMap<String, usize>,
    total_residual: &Arc<Mutex<Col<f64>>>,
) -> LinearizerResult<Vec<f64>> {
    let block = linearize_block(residual_block, variables, total_residual)?;

    let mut local_jacobian_values = Vec::new();
    for (i, var_key) in residual_block.variable_key_list.iter().enumerate() {
        if variable_index_map.contains_key(var_key) {
            let (variable_local_idx, var_size) = block.variable_local_idx_size_list[i];
            let variable_jac = block
                .jacobian
                .view((0, variable_local_idx), (block.residual_dim, var_size));

            for row_idx in 0..block.residual_dim {
                for col_idx in 0..var_size {
                    local_jacobian_values.push(variable_jac[(row_idx, col_idx)]);
                }
            }
        } else {
            return Err(LinearizerError::Variable(format!(
                "Missing key {} in variable-to-column-index mapping",
                var_key
            ))
            .log());
        }
    }

    Ok(local_jacobian_values)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{core::problem::Problem, factors, linalg::JacobianMode, optimizer};
    use apex_manifolds::ManifoldType;
    use nalgebra::{DMatrix, DVector, dvector};
    use std::collections::HashMap;

    type TestResult = Result<(), Box<dyn std::error::Error>>;

    struct LinearFactor {
        target: f64,
    }

    impl factors::Factor for LinearFactor {
        fn linearize(
            &self,
            params: &[DVector<f64>],
            compute_jacobian: bool,
        ) -> (DVector<f64>, Option<DMatrix<f64>>) {
            let residual = dvector![params[0][0] - self.target];
            let jacobian = if compute_jacobian {
                Some(DMatrix::from_element(1, 1, 1.0))
            } else {
                None
            };
            (residual, jacobian)
        }

        fn get_dimension(&self) -> usize {
            1
        }
    }

    fn one_var_problem() -> (Problem, HashMap<String, (ManifoldType, DVector<f64>)>) {
        let mut problem = Problem::new(JacobianMode::Sparse);
        problem.add_residual_block(&["x"], Box::new(LinearFactor { target: 0.0 }), None);
        let mut init = HashMap::new();
        init.insert("x".to_string(), (ManifoldType::RN, dvector![5.0]));
        (problem, init)
    }

    // -------------------------------------------------------------------------
    // build_symbolic_structure
    // -------------------------------------------------------------------------

    #[test]
    fn test_build_symbolic_structure_nnz() -> TestResult {
        let (problem, init) = one_var_problem();
        let state = optimizer::initialize_optimization_state(&problem, &init)?;
        let sym = build_symbolic_structure(
            &problem,
            &state.variables,
            &state.variable_index_map,
            state.total_dof,
        )?;
        assert_eq!(sym.pattern.compute_nnz(), 1);
        Ok(())
    }

    #[test]
    fn test_build_symbolic_structure_dimensions() -> TestResult {
        let (problem, init) = one_var_problem();
        let state = optimizer::initialize_optimization_state(&problem, &init)?;
        let sym = build_symbolic_structure(
            &problem,
            &state.variables,
            &state.variable_index_map,
            state.total_dof,
        )?;
        assert_eq!(sym.pattern.nrows(), 1);
        assert_eq!(sym.pattern.ncols(), 1);
        Ok(())
    }

    #[test]
    fn test_build_symbolic_structure_two_factors() -> TestResult {
        let mut problem = Problem::new(JacobianMode::Sparse);
        problem.add_residual_block(&["x"], Box::new(LinearFactor { target: 0.0 }), None);
        problem.add_residual_block(&["x"], Box::new(LinearFactor { target: 1.0 }), None);
        let mut init = HashMap::new();
        init.insert("x".to_string(), (ManifoldType::RN, dvector![5.0]));
        let state = optimizer::initialize_optimization_state(&problem, &init)?;
        let sym = build_symbolic_structure(
            &problem,
            &state.variables,
            &state.variable_index_map,
            state.total_dof,
        )?;
        assert_eq!(sym.pattern.compute_nnz(), 2);
        Ok(())
    }

    // -------------------------------------------------------------------------
    // assemble_sparse
    // -------------------------------------------------------------------------

    #[test]
    fn test_assemble_sparse_basic() -> TestResult {
        let (problem, init) = one_var_problem();
        let state = optimizer::initialize_optimization_state(&problem, &init)?;
        let sym = state
            .symbolic_structure
            .ok_or("symbolic_structure is None")?;
        let (residual, _) =
            assemble_sparse(&problem, &state.variables, &state.variable_index_map, &sym)?;
        assert!((residual[(0, 0)] - 5.0).abs() < 1e-12);
        Ok(())
    }

    #[test]
    fn test_assemble_sparse_jacobian_value() -> TestResult {
        let (problem, init) = one_var_problem();
        let state = optimizer::initialize_optimization_state(&problem, &init)?;
        let sym = state
            .symbolic_structure
            .ok_or("symbolic_structure is None")?;
        let (_, jacobian) =
            assemble_sparse(&problem, &state.variables, &state.variable_index_map, &sym)?;
        let val = jacobian.as_ref().val_of_col(0)[0];
        assert!((val - 1.0).abs() < 1e-12);
        Ok(())
    }

    #[test]
    fn test_assemble_sparse_zero_residual() -> TestResult {
        let mut problem = Problem::new(JacobianMode::Sparse);
        problem.add_residual_block(&["x"], Box::new(LinearFactor { target: 3.0 }), None);
        let mut init = HashMap::new();
        init.insert("x".to_string(), (ManifoldType::RN, dvector![3.0]));
        let state = optimizer::initialize_optimization_state(&problem, &init)?;
        let sym = state
            .symbolic_structure
            .ok_or("symbolic_structure is None")?;
        let (residual, _) =
            assemble_sparse(&problem, &state.variables, &state.variable_index_map, &sym)?;
        assert!(residual[(0, 0)].abs() < 1e-12);
        Ok(())
    }

    #[test]
    fn test_assemble_sparse_dimensions() -> TestResult {
        let (problem, init) = one_var_problem();
        let state = optimizer::initialize_optimization_state(&problem, &init)?;
        let sym = state
            .symbolic_structure
            .ok_or("symbolic_structure is None")?;
        let (residual, jacobian) =
            assemble_sparse(&problem, &state.variables, &state.variable_index_map, &sym)?;
        assert_eq!(residual.nrows(), 1);
        assert_eq!(residual.ncols(), 1);
        assert_eq!(jacobian.nrows(), 1);
        assert_eq!(jacobian.ncols(), 1);
        Ok(())
    }

    #[test]
    fn test_assemble_sparse_two_variables() -> TestResult {
        let mut problem = Problem::new(JacobianMode::Sparse);
        problem.add_residual_block(&["x"], Box::new(LinearFactor { target: 0.0 }), None);
        problem.add_residual_block(&["y"], Box::new(LinearFactor { target: 0.0 }), None);
        let mut init = HashMap::new();
        init.insert("x".to_string(), (ManifoldType::RN, dvector![2.0]));
        init.insert("y".to_string(), (ManifoldType::RN, dvector![7.0]));
        let state = optimizer::initialize_optimization_state(&problem, &init)?;
        let sym = state
            .symbolic_structure
            .ok_or("symbolic_structure is None")?;
        let (residual, _) =
            assemble_sparse(&problem, &state.variables, &state.variable_index_map, &sym)?;
        assert_eq!(residual.nrows(), 2);
        let rsum = residual[(0, 0)].abs() + residual[(1, 0)].abs();
        assert!((rsum - 9.0).abs() < 1e-12);
        Ok(())
    }

    /// Exercises the `CoreError::Variable` error path inside `scatter_sparse_block()`.
    ///
    /// The symbolic structure is built correctly, but we pass an empty
    /// `variable_index_map` to `assemble_sparse` so the variable key lookup
    /// fails, triggering the `Missing key` error branch.
    #[test]
    fn test_assemble_sparse_missing_variable_key_returns_error() -> TestResult {
        let (problem, init) = one_var_problem();
        let state = optimizer::initialize_optimization_state(&problem, &init)?;
        let sym = state
            .symbolic_structure
            .ok_or("symbolic_structure is None")?;

        // Pass an empty variable_index_map: "x" is missing → should trigger CoreError::Variable
        let empty_map = HashMap::new();
        let result = assemble_sparse(&problem, &state.variables, &empty_map, &sym);
        assert!(
            result.is_err(),
            "assemble_sparse with missing variable key should return Err"
        );
        Ok(())
    }
}