apex-solver 1.2.1

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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! Residual blocks that connect factors with robust loss functions.
//!
//! A `ResidualBlock` is the fundamental building block of the optimization problem. It wraps
//! a [`Factor`] (which computes residuals and Jacobians) with an optional [`Loss`] function
//! (which provides robustness to outliers). Each residual block corresponds to one measurement
//! or constraint in the factor graph.
//!
//! # Role in Optimization
//!
//! The `ResidualBlock` coordinates three key components:
//!
//! 1. **Factor**: Computes the raw residual `r(x)` and Jacobian `J = ∂r/∂x`
//! 2. **Loss function** (optional): Evaluates `ρ(||r||²)` for robust cost
//! 3. **Corrector**: Applies loss function via residual/Jacobian adjustment
//!
//! During each optimization iteration, the residual block:
//! - Evaluates the factor at current variable values
//! - Computes the squared residual norm
//! - If a loss function is present, creates a `Corrector` and applies corrections
//! - Returns the (possibly corrected) residual and Jacobian
//!
//! # Structure in the Problem
//!
//! The [`Problem`](crate::core::problem::Problem) maintains a collection of residual blocks.
//! Each block is assigned:
//! - A unique ID for identification
//! - A starting row index in the global Jacobian matrix
//! - A list of connected variable keys
//! - The factor implementation
//! - An optional loss function
//!
//! # Example
//!
//! ```
//! use apex_solver::core::residual_block::ResidualBlock;
//! use apex_solver::factors::{Factor, BetweenFactor};
//! use apex_solver::core::loss_functions::{LossFunction, HuberLoss};
//! use apex_solver::core::variable::Variable;
//! use apex_solver::manifold::se2::SE2;
//! # use apex_solver::error::ApexSolverResult;
//! # fn example() -> ApexSolverResult<()> {
//!
//! // Create a between factor (measurement between two poses)
//! let factor = Box::new(BetweenFactor::new(SE2::from_xy_angle(1.0, 0.0, 0.1)));
//!
//! // Add robust loss function for outlier rejection
//! let loss = Some(Box::new(HuberLoss::new(1.0)?) as Box<dyn LossFunction + Send>);
//!
//! // Create residual block
//! let block = ResidualBlock::new(
//!     0,                      // Block ID
//!     0,                      // Starting row in Jacobian
//!     &["x0", "x1"],          // Connected variables
//!     factor,
//!     loss,
//! );
//!
//! // Later, during optimization:
//! let var0 = Variable::new(SE2::from_xy_angle(0.0, 0.0, 0.0));
//! let var1 = Variable::new(SE2::from_xy_angle(1.1, 0.05, 0.12));
//! let variables = vec![&var0, &var1];
//!
//! let Ok((residual, jacobian)) = block.residual_and_jacobian(&variables) else { todo!() };
//! // residual and jacobian are now ready for the linear solver
//! # Ok(())
//! # }
//! # example().unwrap();
//! ```

use nalgebra::{DMatrix, DVector};

use crate::core::{
    CoreError, CoreResult, corrector::Corrector, loss_functions::LossFunction, variable::Variable,
};
use crate::factors::Factor;
use apex_manifolds::{LieGroup, Tangent};

/// A residual block that wraps a factor with an optional robust loss function.
///
/// Each residual block represents one measurement or constraint in the optimization problem.
/// It connects one or more variables through a factor, and optionally applies a robust loss
/// function for outlier rejection.
///
/// # Fields
///
/// - `residual_block_id`: Unique identifier for this block
/// - `residual_row_start_idx`: Starting row index in the global residual/Jacobian matrix
/// - `variable_key_list`: Names of the variables connected by this block
/// - `factor`: The factor that computes residuals and Jacobians
/// - `loss_func`: Optional robust loss function (e.g., Huber, Cauchy)
///
/// # Thread Safety
///
/// Residual blocks are designed for parallel evaluation. Both the `factor` and `loss_func`
/// must be `Send` to enable parallel processing across multiple residual blocks.
pub struct ResidualBlock {
    /// Unique identifier for this residual block
    pub residual_block_id: usize,

    /// Starting row index in the global residual vector and Jacobian matrix
    ///
    /// This allows the optimizer to place this block's residual and Jacobian contributions
    /// at the correct location in the full problem matrices.
    pub residual_row_start_idx: usize,

    /// List of variable names (keys) that this block connects
    ///
    /// For example, a between factor connecting poses "x0" and "x1" would have
    /// `variable_key_list = ["x0", "x1"]`.
    pub variable_key_list: Vec<String>,

    /// The factor that computes residuals and Jacobians
    ///
    /// Must implement the `Factor` trait and be thread-safe (`Send`).
    pub factor: Box<dyn Factor + Send>,

    /// Optional robust loss function for outlier rejection
    ///
    /// If `None`, standard least squares is used. If `Some`, the corrector algorithm
    /// is applied to downweight outliers.
    pub loss_func: Option<Box<dyn LossFunction + Send>>,
}

impl ResidualBlock {
    /// Create a new residual block.
    ///
    /// # Arguments
    ///
    /// * `residual_block_id` - Unique identifier for this block
    /// * `residual_row_start_idx` - Starting row in the global residual vector
    /// * `variable_key_size_list` - Names of the connected variables (as string slices)
    /// * `factor` - Factor implementation (boxed trait object)
    /// * `loss_func` - Optional robust loss function (boxed trait object)
    ///
    /// # Returns
    ///
    /// A new `ResidualBlock` instance ready for use in optimization
    ///
    /// # Example
    ///
    /// ```
    /// use apex_solver::core::residual_block::ResidualBlock;
    /// use apex_solver::factors::{Factor, BetweenFactor};
    /// use apex_solver::core::loss_functions::{LossFunction, HuberLoss};
    /// use apex_solver::manifold::se2::SE2;
    /// # use apex_solver::error::ApexSolverResult;
    /// # fn example() -> ApexSolverResult<()> {
    ///
    /// let factor = Box::new(BetweenFactor::new(SE2::from_xy_angle(1.0, 0.0, 0.1)));
    /// let loss = Some(Box::new(HuberLoss::new(1.0)?) as Box<dyn LossFunction + Send>);
    ///
    /// let block = ResidualBlock::new(
    ///     0,                  // First block
    ///     0,                  // Starts at row 0
    ///     &["x0", "x1"],      // Connects two variables
    ///     factor,
    ///     loss,
    /// );
    /// # Ok(())
    /// # }
    /// # example().unwrap();
    /// ```
    pub fn new(
        residual_block_id: usize,
        residual_row_start_idx: usize,
        variable_key_size_list: &[&str],
        factor: Box<dyn Factor + Send>,
        loss_func: Option<Box<dyn LossFunction + Send>>,
    ) -> Self {
        ResidualBlock {
            residual_block_id,
            residual_row_start_idx,
            variable_key_list: variable_key_size_list
                .iter()
                .map(|s| s.to_string())
                .collect(),
            factor,
            loss_func,
        }
    }

    /// Compute residual and Jacobian for this block at the given variable values.
    ///
    /// This is the core method called during each optimization iteration. It:
    /// 1. Extracts values from the provided variables
    /// 2. Calls the factor's `linearize` method
    /// 3. If a loss function is present, applies the corrector algorithm
    /// 4. Returns the (possibly corrected) residual and Jacobian
    ///
    /// # Arguments
    ///
    /// * `variables` - References to the variables connected by this block, in order
    ///
    /// # Returns
    ///
    /// Tuple `(residual, jacobian)` where:
    /// - `residual`: N-dimensional error vector (possibly downweighted by loss function)
    /// - `jacobian`: N × M matrix of derivatives (possibly corrected by loss function)
    ///
    /// # Type Parameters
    ///
    /// * `M` - The manifold type (e.g., SE2, SE3, SO3) that implements `LieGroup`
    ///
    /// # Example
    ///
    /// ```
    /// use apex_solver::core::residual_block::ResidualBlock;
    /// use apex_solver::factors::{Factor, BetweenFactor};
    /// use apex_solver::core::variable::Variable;
    /// use apex_solver::manifold::se2::SE2;
    ///
    /// let factor = Box::new(BetweenFactor::new(SE2::from_xy_angle(1.0, 0.0, 0.1)));
    /// let block = ResidualBlock::new(0, 0, &["x0", "x1"], factor, None);
    ///
    /// let var0 = Variable::new(SE2::from_xy_angle(0.0, 0.0, 0.0));
    /// let var1 = Variable::new(SE2::from_xy_angle(1.0, 0.0, 0.1));
    /// let variables = vec![&var0, &var1];
    ///
    /// let Ok((residual, jacobian)) = block.residual_and_jacobian(&variables) else { todo!() };
    /// // Use residual and jacobian in optimization linear system
    /// ```
    ///
    /// # Implementation Details
    ///
    /// When a loss function is present:
    /// - Computes `s = ||r||²` (squared residual norm)
    /// - Creates a `Corrector` using the loss function evaluation at `s`
    /// - Applies corrections to both residual and Jacobian
    /// - This effectively converts the robust problem into weighted least squares
    ///
    /// Without a loss function:
    /// - Returns raw residual and Jacobian from the factor
    /// - Equivalent to standard (non-robust) least squares
    pub fn residual_and_jacobian<M>(
        &self,
        variables: &[&Variable<M>],
    ) -> CoreResult<(DVector<f64>, DMatrix<f64>)>
    where
        M: LieGroup + Clone + Into<DVector<f64>>,
        M::TangentVector: Tangent<M>,
    {
        // Extract variable values as DVector for the factor
        let param_vec: Vec<_> = variables.iter().map(|v| v.value.clone().into()).collect();

        // Compute raw residual and Jacobian from the factor
        let (mut residual, jacobian_opt) = self.factor.linearize(&param_vec, true);
        let mut jacobian = jacobian_opt.ok_or_else(|| {
            CoreError::FactorLinearization(
                "Factor returned None for Jacobian when compute_jacobian=true".to_string(),
            )
            .log()
        })?;

        // Apply robust loss function if present
        if let Some(loss_func) = self.loss_func.as_ref() {
            // Compute squared norm: s = ||r||²
            let squared_norm = residual.norm_squared();

            // Create corrector and apply to residual and Jacobian
            let corrector = Corrector::new(loss_func.as_ref(), squared_norm);
            corrector.correct_jacobian(&residual, &mut jacobian);
            corrector.correct_residuals(&mut residual);
        }

        Ok((residual, jacobian))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::{
        loss_functions::{HuberLoss, LossFunction},
        variable::Variable,
    };
    use crate::factors::{BetweenFactor, PriorFactor};
    use apex_manifolds::{se2::SE2, se3::SE3};
    use nalgebra::{Quaternion, dvector, vector};

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

    #[test]
    fn test_residual_block_creation() -> TestResult {
        let factor = Box::new(BetweenFactor::new(SE2::from_xy_angle(1.0, 0.0, 0.1)));
        let loss = Some(Box::new(HuberLoss::new(1.0)?) as Box<dyn LossFunction + Send>);

        let block = ResidualBlock::new(0, 0, &["x0", "x1"], factor, loss);

        assert_eq!(block.residual_block_id, 0);
        assert_eq!(block.residual_row_start_idx, 0);
        assert_eq!(block.variable_key_list, vec!["x0", "x1"]);
        assert!(block.loss_func.is_some());

        Ok(())
    }

    #[test]
    fn test_residual_block_without_loss() -> TestResult {
        let factor = Box::new(PriorFactor {
            data: dvector![0.0, 0.0, 0.0],
        });

        let block = ResidualBlock::new(1, 3, &["x0"], factor, None);

        assert_eq!(block.residual_block_id, 1);
        assert_eq!(block.residual_row_start_idx, 3);
        assert_eq!(block.variable_key_list, vec!["x0"]);
        assert!(block.loss_func.is_none());

        Ok(())
    }

    #[test]
    fn test_residual_and_jacobian_se2_between_factor() -> TestResult {
        // Create a between factor with known measurement
        let dx = 1.0;
        let dy = 0.5;
        let dtheta = 0.1;
        let factor = Box::new(BetweenFactor::new(SE2::from_xy_angle(dx, dy, dtheta)));

        let block = ResidualBlock::new(0, 0, &["x0", "x1"], factor, None);

        // Create test variables - SE2 uses [x, y, theta] ordering
        let var0 = Variable::new(SE2::from_xy_angle(0.0, 0.0, 0.0));
        let var1 = Variable::new(SE2::from_xy_angle(1.0, 0.5, 0.1));
        let variables = vec![&var0, &var1];

        let (residual, jacobian) = block.residual_and_jacobian(&variables)?;

        // Verify dimensions
        assert_eq!(residual.len(), 3);
        assert_eq!(jacobian.nrows(), 3);
        assert_eq!(jacobian.ncols(), 6); // 2 variables * 3 DOF each

        // For identity start and [0.1, 1.0, 0.5] end with measurement [1.0, 0.5, 0.1]
        // This should give very small residuals (near zero)
        assert!(
            residual.norm() < 1e-10,
            "Residual norm: {}",
            residual.norm()
        );

        // Verify Jacobian is not zero (it should have meaningful values)
        assert!(jacobian.norm() > 1e-10, "Jacobian should not be near zero");

        Ok(())
    }

    #[test]
    fn test_residual_and_jacobian_with_huber_loss() -> TestResult {
        // Create a between factor that will have non-zero residual
        let factor = Box::new(BetweenFactor::new(SE2::from_xy_angle(1.0, 0.0, 0.0)));
        let loss = Some(Box::new(HuberLoss::new(1.0)?) as Box<dyn LossFunction + Send>);

        let block = ResidualBlock::new(0, 0, &["x0", "x1"], factor, loss);

        // Create variables with significant difference to trigger loss function
        let var0 = Variable::new(SE2::from_xy_angle(0.0, 0.0, 0.0));
        let var1 = Variable::new(SE2::from_xy_angle(5.0, 5.0, 2.0)); // Very different from measurement [1.0, 0.0, 0.0]
        let variables = vec![&var0, &var1];

        let (residual_with_loss, jacobian_with_loss) = block.residual_and_jacobian(&variables)?;

        // Create same block without loss for comparison
        let factor_no_loss = Box::new(BetweenFactor::new(SE2::from_xy_angle(1.0, 0.0, 0.0)));
        let block_no_loss = ResidualBlock::new(0, 0, &["x0", "x1"], factor_no_loss, None);
        let (residual_no_loss, jacobian_no_loss) =
            block_no_loss.residual_and_jacobian(&variables)?;

        // With loss function, residuals should be different (corrected)
        let residual_diff = (residual_with_loss - residual_no_loss).norm();
        assert!(
            residual_diff > 1e-10,
            "Loss function should modify residuals"
        );

        // Jacobian should also be different
        let jacobian_diff = (jacobian_with_loss - jacobian_no_loss).norm();
        assert!(
            jacobian_diff > 1e-10,
            "Loss function should modify Jacobian"
        );

        Ok(())
    }

    #[test]
    fn test_residual_block_se3_between_factor() -> TestResult {
        // Test with SE3 - use prior factor on SE3
        let se3_data = dvector![1.0, 0.5, 0.2, 1.0, 0.0, 0.0, 0.0]; // [tx,ty,tz,qw,qx,qy,qz]
        let factor = Box::new(PriorFactor {
            data: se3_data.clone(),
        });

        let block = ResidualBlock::new(0, 0, &["x0"], factor, None);

        // Create SE3 variable
        let var0 = Variable::new(SE3::from_translation_quaternion(
            vector![1.0, 0.5, 0.2],
            Quaternion::new(1.0, 0.0, 0.0, 0.0),
        ));
        let variables = vec![&var0];

        let (residual, jacobian) = block.residual_and_jacobian(&variables)?;

        // Verify dimensions for SE3 - prior factor uses full manifold dimension
        assert_eq!(residual.len(), 7); // SE3 manifold has 7 parameters [tx,ty,tz,qw,qx,qy,qz]
        assert_eq!(jacobian.nrows(), 7);
        // For PriorFactor, Jacobian dimensions depend on implementation
        // If it's identity-based, should be 7x7; if tangent-based, should be 7x6
        // Let's be flexible and check it's one of these reasonable sizes
        assert!(jacobian.ncols() == 6 || jacobian.ncols() == 7);

        Ok(())
    }

    #[test]
    fn test_multiple_residual_blocks_different_ids() -> TestResult {
        // Test creating multiple blocks with different IDs and start indices
        let factors: Vec<Box<dyn Factor + Send>> = vec![
            Box::new(BetweenFactor::new(SE2::from_xy_angle(1.0, 0.0, 0.1))),
            Box::new(BetweenFactor::new(SE2::from_xy_angle(0.8, 0.2, -0.05))),
            Box::new(PriorFactor {
                data: dvector![0.0, 0.0, 0.0],
            }),
        ];

        let blocks: Vec<ResidualBlock> = factors
            .into_iter()
            .enumerate()
            .map(
                |(i, factor)| -> Result<ResidualBlock, Box<dyn std::error::Error>> {
                    Ok(ResidualBlock::new(
                        i,
                        i * 3, // Each block starts at different row
                        if i == 2 { &["x0"] } else { &["x0", "x1"] },
                        factor,
                        if i == 1 {
                            Some(Box::new(HuberLoss::new(0.5)?))
                        } else {
                            None
                        },
                    ))
                },
            )
            .collect::<Result<Vec<_>, _>>()?;

        // Verify each block has correct properties
        for (i, block) in blocks.iter().enumerate() {
            assert_eq!(block.residual_block_id, i);
            assert_eq!(block.residual_row_start_idx, i * 3);

            if i == 2 {
                assert_eq!(block.variable_key_list.len(), 1);
                assert!(block.loss_func.is_none());
            } else {
                assert_eq!(block.variable_key_list.len(), 2);
                assert_eq!(block.loss_func.is_some(), i == 1);
            }
        }

        Ok(())
    }

    #[test]
    fn test_residual_block_variable_ordering() -> TestResult {
        // Test that variable ordering is preserved correctly
        let factor = Box::new(BetweenFactor::new(SE2::from_xy_angle(1.0, 0.0, 0.1)));
        let block = ResidualBlock::new(0, 0, &["pose_2", "pose_1", "pose_0"], factor, None);

        let expected_order = vec!["pose_2", "pose_1", "pose_0"];
        assert_eq!(block.variable_key_list, expected_order);

        Ok(())
    }

    #[test]
    fn test_residual_block_numerical_stability() -> TestResult {
        // Test with very small values to ensure numerical stability
        let factor = Box::new(BetweenFactor::new(SE2::from_xy_angle(1e-8, 1e-8, 1e-8)));
        let block = ResidualBlock::new(0, 0, &["x0", "x1"], factor, None);

        let var0 = Variable::new(SE2::from_xy_angle(0.0, 0.0, 0.0));
        let var1 = Variable::new(SE2::from_xy_angle(1e-8, 1e-8, 1e-8));
        let variables = vec![&var0, &var1];

        let (residual, jacobian) = block.residual_and_jacobian(&variables)?;

        // Should handle small values without numerical issues
        assert!(residual.iter().all(|&x| x.is_finite()));
        assert!(jacobian.iter().all(|&x| x.is_finite()));
        assert!(residual.norm() < 1e-6);

        Ok(())
    }

    #[test]
    fn test_residual_block_large_values() -> TestResult {
        // Test with large values to ensure no overflow
        let factor = Box::new(BetweenFactor::new(SE2::from_xy_angle(100.0, -200.0, 1.5)));
        let block = ResidualBlock::new(0, 0, &["x0", "x1"], factor, None);

        let var0 = Variable::new(SE2::from_xy_angle(0.0, 0.0, 0.0));
        let var1 = Variable::new(SE2::from_xy_angle(100.0, -200.0, 1.5));
        let variables = vec![&var0, &var1];

        let (residual, jacobian) = block.residual_and_jacobian(&variables)?;

        // Should handle large values without overflow
        assert!(residual.iter().all(|&x| x.is_finite()));
        assert!(jacobian.iter().all(|&x| x.is_finite()));
        assert!(residual.norm() < 1e-10); // Should still be near zero for matching measurement

        Ok(())
    }

    #[test]
    fn test_residual_block_loss_function_switching() -> TestResult {
        // Test the same residual block with and without loss function applied
        let factor1 = Box::new(BetweenFactor::new(SE2::from_xy_angle(1.0, 0.0, 0.1)));
        let factor2 = Box::new(BetweenFactor::new(SE2::from_xy_angle(1.0, 0.0, 0.1)));

        let block_with_loss = ResidualBlock::new(
            0,
            0,
            &["x0", "x1"],
            factor1,
            Some(Box::new(HuberLoss::new(0.1)?)),
        );
        let block_without_loss = ResidualBlock::new(0, 0, &["x0", "x1"], factor2, None);

        // Create variables that will produce significant residual
        let var0 = Variable::new(SE2::from_xy_angle(0.0, 0.0, 0.0));
        let var1 = Variable::new(SE2::from_xy_angle(2.0, 1.0, 0.2)); // Far from measurement
        let variables = vec![&var0, &var1];

        let (res_with, jac_with) = block_with_loss.residual_and_jacobian(&variables)?;
        let (res_without, jac_without) = block_without_loss.residual_and_jacobian(&variables)?;

        // Loss function should modify both residual and Jacobian
        assert!((res_with.clone() - res_without.clone()).norm() > 1e-6);
        assert!((jac_with.clone() - jac_without.clone()).norm() > 1e-6);

        // With Huber loss and significant error, residual magnitude should be reduced
        assert!(res_with.norm() < res_without.norm());

        Ok(())
    }
}