Skip to content

Commit

Permalink
Merge pull request #22 from geo-ant/feature/bump-nalgebra-to-0.33.0
Browse files Browse the repository at this point in the history
Bump nalgebra to version 0.33.0
  • Loading branch information
vadixidav authored Jul 22, 2024
2 parents 7e9feb9 + 705ca6e commit 4b5eb3d
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 51 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ default = []
minpack-compat = []

[dependencies]
nalgebra = { version = "0.32.1", default-features = false }
num-traits = { version = "0.2.14", default-features = false, features = [
nalgebra = { version = "0.33", default-features = false }
num-traits = { version = "0.2", default-features = false, features = [
"libm",
] }
cfg-if = "1.0.0"

[dev-dependencies]
arrsac = "0.10.0"
rand = { version = "0.8.4", default-features = false }
nalgebra = "0.32.1"
nalgebra = "0.33"
pcg_rand = "0.13.0"
sample-consensus = "1.0.2"
approx = "0.5.1"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
//!
//! You should try using [`differentiate_numerically`](fn.differentiate_numerically.html)
//! in a unit test to verify that your Jacobian implementation matches the residuals.
#![allow(unexpected_cfgs)]
#![no_std]
#![cfg_attr(feature = "RUSTC_IS_NIGHTLY", core_intrinsics)]

Expand Down
11 changes: 5 additions & 6 deletions src/lm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ impl<F: RealField + Float> LevenbergMarquardt<F> {
N: Dim,
M: DimMin<N> + DimMax<N>,
O: LeastSquaresProblem<F, M, N>,
DefaultAllocator:
Allocator<F, N> + Reallocator<F, M, N, DimMaximum<M, N>, N> + Allocator<usize, N>,
DefaultAllocator: Allocator<N> + Reallocator<F, M, N, DimMaximum<M, N>, N>,
{
let (mut lm, mut residuals) = match LM::new(self, target) {
Err(report) => return report,
Expand Down Expand Up @@ -309,7 +308,7 @@ where
N: Dim,
M: DimMin<N> + DimMax<N>,
O: LeastSquaresProblem<F, M, N>,
DefaultAllocator: Allocator<F, N> + Allocator<F, DimMaximum<M, N>, N>,
DefaultAllocator: Allocator<N> + Allocator<DimMaximum<M, N>, N>,
{
config: &'a LevenbergMarquardt<F>,
/// Current parameters `$\vec{x}$`
Expand Down Expand Up @@ -342,7 +341,7 @@ where
N: Dim,
M: DimMin<N> + DimMax<N>,
O: LeastSquaresProblem<F, M, N>,
DefaultAllocator: Allocator<F, N> + Allocator<F, DimMaximum<M, N>, N>,
DefaultAllocator: Allocator<N> + Allocator<DimMaximum<M, N>, N>,
{
#[allow(clippy::type_complexity)]
fn new(
Expand Down Expand Up @@ -455,7 +454,7 @@ where
lls: &mut LinearLeastSquaresDiagonalProblem<F, M, N>,
) -> Result<(), TerminationReason>
where
DefaultAllocator: Allocator<usize, N>,
DefaultAllocator: Allocator<N>,
{
// Compute norm of scaled gradient and detect degeneracy
self.gnorm = match lls.max_a_t_b_scaled(self.residuals_norm) {
Expand Down Expand Up @@ -510,7 +509,7 @@ where
param: LMParameter<F, N>,
) -> Result<Option<Vector<F, M, O::ResidualStorage>>, TerminationReason>
where
DefaultAllocator: Allocator<usize, N>,
DefaultAllocator: Allocator<N>,
{
const P1: f64 = 0.1;
const P0001: f64 = 1.0e-4;
Expand Down
6 changes: 3 additions & 3 deletions src/lm/test_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,14 +512,14 @@ impl LeastSquaresProblem<f64, U16, U3> for Meyer {
#[derive(Clone)]
struct Watson<P: DimName>
where
DefaultAllocator: Allocator<f64, P>,
DefaultAllocator: Allocator<P>,
{
params: OVector<f64, P>,
}

impl<P: DimName> Watson<P>
where
DefaultAllocator: Allocator<f64, P>,
DefaultAllocator: Allocator<P>,
{
fn new(params: OVector<f64, P>, _n: usize) -> Self {
Self { params }
Expand All @@ -528,7 +528,7 @@ where

impl<P: DimName> LeastSquaresProblem<f64, U31, P> for Watson<P>
where
DefaultAllocator: Allocator<f64, P> + Allocator<f64, U31, P>,
DefaultAllocator: Allocator<P> + Allocator<U31, P>,
{
type ParameterStorage = Owned<f64, P>;
type ResidualStorage = Owned<f64, U31>;
Expand Down
6 changes: 3 additions & 3 deletions src/lm/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum MockCall {
#[derive(Clone)]
pub struct MockProblem<N: Dim, M: Dim>
where
DefaultAllocator: Allocator<f64, N> + Allocator<f64, M> + Allocator<f64, M, N>,
DefaultAllocator: Allocator<N> + Allocator<M> + Allocator<M, N>,
{
call_history: RefCell<Vec<MockCall>>,
params: Vec<OVector<f64, N>>,
Expand All @@ -27,7 +27,7 @@ where

impl<N: Dim, M: Dim> MockProblem<N, M>
where
DefaultAllocator: Allocator<f64, N> + Allocator<f64, M> + Allocator<f64, M, N>,
DefaultAllocator: Allocator<N> + Allocator<M> + Allocator<M, N>,
{
pub fn new(initial: OVector<f64, N>, residuals: Vec<Option<OVector<f64, M>>>) -> Self {
Self {
Expand All @@ -47,7 +47,7 @@ where

impl<N: Dim, M: Dim> LeastSquaresProblem<f64, M, N> for MockProblem<N, M>
where
DefaultAllocator: Allocator<f64, N> + Allocator<f64, M> + Allocator<f64, M, N>,
DefaultAllocator: Allocator<N> + Allocator<M> + Allocator<M, N>,
{
type ResidualStorage = Owned<f64, M>;
type ParameterStorage = Owned<f64, N>;
Expand Down
22 changes: 11 additions & 11 deletions src/lm/test_init_step.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use super::test_helpers::{MockCall, MockProblem};
use super::{LevenbergMarquardt, TerminationReason, LM};
use alloc::vec;
use approx::assert_relative_eq;
#[cfg(not(feature = "minpack-compat"))]
use core::f64::{INFINITY, MIN_POSITIVE, NAN};

use nalgebra::{Dim, Dyn, OMatrix, OVector, Vector2, Vector3, U0, U2, U3};

use super::test_helpers::{MockCall, MockProblem};
use super::{LevenbergMarquardt, TerminationReason, LM};

#[test]
#[cfg(not(feature = "minpack-compat"))]
fn nan_or_inf_none_residual() {
Expand All @@ -20,8 +16,10 @@ fn nan_or_inf_none_residual() {
assert!(err.objective_function.is_nan());

// residuals return inf
let problem =
MockProblem::<U2, U3>::new(Vector2::zeros(), vec![Some(Vector3::new(1., 1., INFINITY))]);
let problem = MockProblem::<U2, U3>::new(
Vector2::zeros(),
vec![Some(Vector3::new(1., 1., f64::INFINITY))],
);
let (mut problem, err) = LM::new(&LevenbergMarquardt::new(), problem).err().unwrap();
assert_eq!(
err.termination,
Expand All @@ -33,7 +31,7 @@ fn nan_or_inf_none_residual() {

// residuals return nan
let problem =
MockProblem::<U2, U3>::new(Vector2::zeros(), vec![Some(Vector3::new(1., 1., NAN))]);
MockProblem::<U2, U3>::new(Vector2::zeros(), vec![Some(Vector3::new(1., 1., f64::NAN))]);
let (mut problem, err) = LM::new(&LevenbergMarquardt::new(), problem).err().unwrap();
assert_eq!(
err.termination,
Expand All @@ -56,8 +54,10 @@ fn already_zero() {
assert_eq!(problem.calls(), [MockCall::Residuals].as_ref());
assert!(err.objective_function.is_zero());

let problem =
MockProblem::<U1, U1>::new(Vector1::new(10.), vec![Some(Vector1::new(MIN_POSITIVE))]);
let problem = MockProblem::<U1, U1>::new(
Vector1::new(10.),
vec![Some(Vector1::new(f64::MIN_POSITIVE))],
);
let (mut problem, err) = LM::new(&LevenbergMarquardt::new(), problem).err().unwrap();
assert_eq!(err.termination, TerminationReason::ResidualsZero);
assert_eq!(err.number_of_evaluations, 1);
Expand Down
21 changes: 10 additions & 11 deletions src/lm/test_update_diag.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use ::core::f64::{INFINITY, NAN};
use alloc::vec;

use approx::assert_relative_eq;
use nalgebra::*;

use super::test_helpers::{MockCall, MockProblem};

use super::{LevenbergMarquardt, TerminationReason, LM};
use crate::qr::PivotedQR;
use alloc::vec;
use approx::assert_relative_eq;
use nalgebra::*;

#[test]
fn gnorm_and_gtol() {
Expand Down Expand Up @@ -80,11 +76,11 @@ fn nan_inf_xnorm() {
if cfg!(not(feature = "minpack-compat")) {
let jacobian = Matrix3x2::new(1., 2., 4., -2., 0.5, 0.1);
assert_eq!(
setup(Vector2::new(INFINITY, 0.), jacobian.clone()),
setup(Vector2::new(f64::INFINITY, 0.), jacobian.clone()),
TerminationReason::Numerical("subproblem x")
);
assert_eq!(
setup(Vector2::new(NAN, 0.), jacobian.clone()),
setup(Vector2::new(f64::NAN, 0.), jacobian.clone()),
TerminationReason::Numerical("subproblem x")
);
}
Expand All @@ -96,11 +92,14 @@ fn nan_inf_xnorm() {
TerminationReason::Numerical("jacobian")
};
assert_eq!(
setup(x.clone(), Matrix3x2::new(INFINITY, 2., 4., -2., 0.5, 0.1)),
setup(
x.clone(),
Matrix3x2::new(f64::INFINITY, 2., 4., -2., 0.5, 0.1)
),
termination_reason
);
assert_eq!(
setup(x.clone(), Matrix3x2::new(NAN, 2., 4., -2., 0.5, 0.1)),
setup(x.clone(), Matrix3x2::new(f64::NAN, 2., 4., -2., 0.5, 0.1)),
termination_reason
);
}
Expand Down
26 changes: 18 additions & 8 deletions src/qr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ where
M: Dim + DimMin<N>,
N: Dim,
S: RawStorageMut<F, M, N> + IsContiguous,
DefaultAllocator: Allocator<F, N> + Allocator<usize, N>,
DefaultAllocator: Allocator<N>,
{
/// The column norms of the input matrix `$\mathbf{A}$`
column_norms: OVector<F, N>,
Expand All @@ -54,7 +54,7 @@ where
M: Dim + DimMin<N> + DimMax<N>,
N: Dim,
S: RawStorageMut<F, M, N> + Storage<F, M, N> + IsContiguous,
DefaultAllocator: Allocator<F, N> + Allocator<F, DimMaximum<M, N>, N> + Allocator<usize, N>,
DefaultAllocator: Allocator<N> + Allocator<DimMaximum<M, N>, N>,
{
/// Create a pivoted QR decomposition of a matrix `$\mathbf{A}\in\R^{m\times n}$`.
pub fn new(mut a: Matrix<F, M, N, S>) -> Self {
Expand Down Expand Up @@ -203,7 +203,7 @@ where
F: nalgebra::RealField + Float + Copy,
M: Dim + DimMax<N>,
N: Dim,
DefaultAllocator: Allocator<F, N> + Allocator<F, DimMaximum<M, N>, N> + Allocator<usize, N>,
DefaultAllocator: Allocator<N> + Allocator<DimMaximum<M, N>, N>,
{
/// The first `$n$` entries of `$\mathbf{Q}^\top \vec{b}$`.
qt_b: OVector<F, N>,
Expand All @@ -224,7 +224,7 @@ where
F: nalgebra::RealField + Copy,
M: Dim + DimMax<N>,
N: Dim,
DefaultAllocator: Allocator<F, N> + Allocator<F, DimMaximum<M, N>, N> + Allocator<usize, N>,
DefaultAllocator: Allocator<N> + Allocator<DimMaximum<M, N>, N>,
{
pub permutation: &'a OVector<usize, N>,
l: &'a OMatrix<F, DimMaximum<M, N>, N>,
Expand All @@ -239,7 +239,7 @@ where
F: nalgebra::RealField + Copy,
M: Dim + DimMin<N> + DimMax<N>,
N: Dim,
DefaultAllocator: Allocator<F, N> + Allocator<F, DimMaximum<M, N>, N> + Allocator<usize, N>,
DefaultAllocator: Allocator<N> + Allocator<DimMaximum<M, N>, N> + Allocator<N>,
{
/// Solve the equation `$\mathbf{L}\vec{x} = \mathbf{P}^\top \vec{b}$`.
pub fn solve(&mut self, mut rhs: OVector<F, N>) -> OVector<F, N> {
Expand Down Expand Up @@ -295,7 +295,7 @@ where
F: nalgebra::RealField + Float + Copy,
M: Dim + DimMin<N> + DimMax<N>,
N: Dim,
DefaultAllocator: Allocator<F, N> + Allocator<F, DimMaximum<M, N>, N> + Allocator<usize, N>,
DefaultAllocator: Allocator<N> + Allocator<DimMaximum<M, N>, N>,
{
/// Compute scaled maximum of dot products between `$\vec{b}$` and the columns of `$\mathbf{A}$`.
///
Expand Down Expand Up @@ -854,7 +854,6 @@ fn test_cholesky_upper() {

#[test]
fn test_column_max_norm() {
use ::core::f64::NAN;
use nalgebra::*;
let a = Matrix4x3::from_column_slice(&[
14., -12., 20., -11., 19., 38., -4., -11., -14., 12., -20., 11.,
Expand All @@ -867,7 +866,18 @@ fn test_column_max_norm() {
assert_relative_eq!(max_at_b.unwrap(), 0.88499332, epsilon = 1e-8);

let a = Matrix4x3::from_column_slice(&[
NAN, -12., 20., -11., 19., 38., -4., -11., -14., 12., -20., 11.,
f64::NAN,
-12.,
20.,
-11.,
19.,
38.,
-4.,
-11.,
-14.,
12.,
-20.,
11.,
]);
let qr = PivotedQR::new(a);
let b = Vector4::new(1., 2., 3., 4.);
Expand Down
4 changes: 2 additions & 2 deletions src/trust_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use num_traits::Float;

pub struct LMParameter<F: RealField, N: Dim>
where
DefaultAllocator: Allocator<F, N>,
DefaultAllocator: Allocator<N>,
{
pub step: OVector<F, N>,
pub lambda: F,
Expand Down Expand Up @@ -59,7 +59,7 @@ where
F: RealField + Float,
N: Dim,
M: Dim + DimMin<N> + DimMax<N>,
DefaultAllocator: Allocator<F, N> + Allocator<F, DimMaximum<M, N>, N> + Allocator<usize, N>,
DefaultAllocator: Allocator<N> + Allocator<DimMaximum<M, N>, N>,
{
const P1: f64 = 0.1;
debug_assert!(delta.is_positive());
Expand Down
8 changes: 4 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(unexpected_cfgs)]
use crate::LeastSquaresProblem;
use alloc::{format, string::String};
use core::cell::RefCell;
Expand Down Expand Up @@ -109,7 +110,7 @@ where
M: Dim,
O: LeastSquaresProblem<F, M, N>,
O::JacobianStorage: Clone,
DefaultAllocator: Allocator<F, M, N, Buffer = O::JacobianStorage>,
DefaultAllocator: Allocator<M, N, Buffer<F> = O::JacobianStorage>,
{
let params = problem.params();
let n = params.data.shape().0;
Expand Down Expand Up @@ -217,9 +218,8 @@ where
N: Dim,
M: Dim,
O: LeastSquaresProblem<Complex<F>, M, N>,
DefaultAllocator: Allocator<Complex<F>, N, Buffer = O::ParameterStorage>
+ Allocator<F, N>
+ Allocator<F, M, N>,
DefaultAllocator:
Allocator<N, Buffer<Complex<F>> = O::ParameterStorage> + Allocator<N> + Allocator<M, N>,
{
let mut params = problem.params();
assert!(params.iter().all(|x| x.im.is_zero()), "params must be real");
Expand Down

0 comments on commit 4b5eb3d

Please sign in to comment.