Crate lstsq

source ·
Expand description

Return the least-squares solution to a linear matrix equation

The crate implements the linear least squares solution to a linear matrix equation.

Characteristics:

  • Linear algebra and types from the nalgebra crate.
  • Maximum compatibility with the numpy.linalg.lstsq Python library function.
  • No standard library is required (disable the default features to disable use of std) and no heap allocations. In other words, this can run on a bare-metal microcontroller with no OS.

Example:

use nalgebra::{self as na, OMatrix, OVector, U2};

let a = OMatrix::<f64, na::Dyn, U2>::from_row_slice(&[
    1.0, 1.0,
    2.0, 1.0,
    3.0, 1.0,
    4.0, 1.0,
]);

let b = OVector::<f64, na::Dyn>::from_row_slice(&[2.5, 4.4, 6.6, 8.5]);

let epsilon = 1e-14;
let results = lstsq::lstsq(&a, &b, epsilon).unwrap();

assert_eq!(results.solution.nrows(), 2);
approx::assert_relative_eq!(results.solution[0], 2.02, epsilon = epsilon);
approx::assert_relative_eq!(results.solution[1], 0.45, epsilon = epsilon);
approx::assert_relative_eq!(results.residuals, 0.018, epsilon = epsilon);
assert_eq!(results.rank, 2);

Structs§

Functions§

  • Return the least-squares solution to a linear matrix equation.