-
Notifications
You must be signed in to change notification settings - Fork 83
Revise tests for least-square problems #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+356
−194
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9cf8331
Move test of least_squares into tests/, and minor cleanup
termoshtt e51f966
Rewrite test cases
termoshtt a8a07fb
Test for F-continuous and complex cases
termoshtt 05a2bcd
Fix F-contiguous case
termoshtt 6bbe87d
Add tests for multi-column `b`
termoshtt b60cfc8
Comment out Unsupported C/F-mixed cases
termoshtt f6a9c2a
Fix memory layout for overdetermined case
termoshtt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/// Solve least square problem `|b - Ax|` | ||
use approx::AbsDiffEq; | ||
use ndarray::*; | ||
use ndarray_linalg::*; | ||
|
||
/// A is square. `x = A^{-1} b`, `|b - Ax| = 0` | ||
fn test_exact<T: Scalar + Lapack>(a: Array2<T>) { | ||
let b: Array1<T> = random(3); | ||
let result = a.least_squares(&b).unwrap(); | ||
// unpack result | ||
let x = result.solution; | ||
let residual_l2_square = result.residual_sum_of_squares.unwrap()[()]; | ||
|
||
// must be full-rank | ||
assert_eq!(result.rank, 3); | ||
|
||
// |b - Ax| == 0 | ||
assert!(residual_l2_square < T::real(1.0e-4)); | ||
|
||
// b == Ax | ||
let ax = a.dot(&x); | ||
assert_close_l2!(&b, &ax, T::real(1.0e-4)); | ||
} | ||
|
||
macro_rules! impl_exact { | ||
($scalar:ty) => { | ||
paste::item! { | ||
#[test] | ||
fn [<least_squares_ $scalar _exact>]() { | ||
let a: Array2<f64> = random((3, 3)); | ||
test_exact(a) | ||
} | ||
|
||
#[test] | ||
fn [<least_squares_ $scalar _exact_t>]() { | ||
let a: Array2<f64> = random((3, 3).f()); | ||
test_exact(a) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_exact!(f32); | ||
impl_exact!(f64); | ||
impl_exact!(c32); | ||
impl_exact!(c64); | ||
|
||
/// #column < #row case. | ||
/// Linear problem is overdetermined, `|b - Ax| > 0`. | ||
fn test_overdetermined<T: Scalar + Lapack>(a: Array2<T>) | ||
where | ||
T::Real: AbsDiffEq<Epsilon = T::Real>, | ||
{ | ||
let b: Array1<T> = random(4); | ||
let result = a.least_squares(&b).unwrap(); | ||
// unpack result | ||
let x = result.solution; | ||
let residual_l2_square = result.residual_sum_of_squares.unwrap()[()]; | ||
|
||
// Must be full-rank | ||
assert_eq!(result.rank, 3); | ||
|
||
// eval `residual = b - Ax` | ||
let residual = &b - &a.dot(&x); | ||
assert!(residual_l2_square.abs_diff_eq(&residual.norm_l2().powi(2), T::real(1.0e-4))); | ||
|
||
// `|residual| < |b|` | ||
assert!(residual.norm_l2() < b.norm_l2()); | ||
} | ||
|
||
macro_rules! impl_overdetermined { | ||
($scalar:ty) => { | ||
paste::item! { | ||
#[test] | ||
fn [<least_squares_ $scalar _overdetermined>]() { | ||
let a: Array2<f64> = random((4, 3)); | ||
test_overdetermined(a) | ||
} | ||
|
||
#[test] | ||
fn [<least_squares_ $scalar _overdetermined_t>]() { | ||
let a: Array2<f64> = random((4, 3).f()); | ||
test_overdetermined(a) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_overdetermined!(f32); | ||
impl_overdetermined!(f64); | ||
impl_overdetermined!(c32); | ||
impl_overdetermined!(c64); | ||
|
||
/// #column > #row case. | ||
/// Linear problem is underdetermined, `|b - Ax| = 0` and `x` is not unique | ||
fn test_underdetermined<T: Scalar + Lapack>(a: Array2<T>) { | ||
let b: Array1<T> = random(3); | ||
let result = a.least_squares(&b).unwrap(); | ||
assert_eq!(result.rank, 3); | ||
assert!(result.residual_sum_of_squares.is_none()); | ||
|
||
// b == Ax | ||
let x = result.solution; | ||
let ax = a.dot(&x); | ||
assert_close_l2!(&b, &ax, T::real(1.0e-4)); | ||
} | ||
|
||
macro_rules! impl_underdetermined { | ||
($scalar:ty) => { | ||
paste::item! { | ||
#[test] | ||
fn [<least_squares_ $scalar _underdetermined>]() { | ||
let a: Array2<f64> = random((3, 4)); | ||
test_underdetermined(a) | ||
} | ||
|
||
#[test] | ||
fn [<least_squares_ $scalar _underdetermined_t>]() { | ||
let a: Array2<f64> = random((3, 4).f()); | ||
test_underdetermined(a) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_underdetermined!(f32); | ||
impl_underdetermined!(f64); | ||
impl_underdetermined!(c32); | ||
impl_underdetermined!(c64); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
/// Solve least square problem `|b - Ax|` with multi-column `b` | ||
use approx::AbsDiffEq; | ||
use ndarray::*; | ||
use ndarray_linalg::*; | ||
|
||
/// A is square. `x = A^{-1} b`, `|b - Ax| = 0` | ||
fn test_exact<T: Scalar + Lapack>(a: Array2<T>, b: Array2<T>) { | ||
assert_eq!(a.layout().unwrap().size(), (3, 3)); | ||
assert_eq!(b.layout().unwrap().size(), (3, 2)); | ||
|
||
let result = a.least_squares(&b).unwrap(); | ||
// unpack result | ||
let x: Array2<T> = result.solution; | ||
let residual_l2_square: Array1<T::Real> = result.residual_sum_of_squares.unwrap(); | ||
|
||
// must be full-rank | ||
assert_eq!(result.rank, 3); | ||
|
||
// |b - Ax| == 0 | ||
for &residual in &residual_l2_square { | ||
assert!(residual < T::real(1.0e-4)); | ||
} | ||
|
||
// b == Ax | ||
let ax = a.dot(&x); | ||
assert_close_l2!(&b, &ax, T::real(1.0e-4)); | ||
} | ||
|
||
macro_rules! impl_exact { | ||
($scalar:ty) => { | ||
paste::item! { | ||
#[test] | ||
fn [<least_squares_ $scalar _exact_ac_bc>]() { | ||
let a: Array2<f64> = random((3, 3)); | ||
let b: Array2<f64> = random((3, 2)); | ||
test_exact(a, b) | ||
} | ||
|
||
/* Unsupported currently. See https://github.com/rust-ndarray/ndarray-linalg/issues/234 | ||
#[test] | ||
fn [<least_squares_ $scalar _exact_ac_bf>]() { | ||
let a: Array2<f64> = random((3, 3)); | ||
let b: Array2<f64> = random((3, 2).f()); | ||
test_exact(a, b) | ||
} | ||
#[test] | ||
fn [<least_squares_ $scalar _exact_af_bc>]() { | ||
let a: Array2<f64> = random((3, 3).f()); | ||
let b: Array2<f64> = random((3, 2)); | ||
test_exact(a, b) | ||
} | ||
*/ | ||
|
||
#[test] | ||
fn [<least_squares_ $scalar _exact_af_bf>]() { | ||
let a: Array2<f64> = random((3, 3).f()); | ||
let b: Array2<f64> = random((3, 2).f()); | ||
test_exact(a, b) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_exact!(f32); | ||
impl_exact!(f64); | ||
impl_exact!(c32); | ||
impl_exact!(c64); | ||
|
||
/// #column < #row case. | ||
/// Linear problem is overdetermined, `|b - Ax| > 0`. | ||
fn test_overdetermined<T: Scalar + Lapack>(a: Array2<T>, bs: Array2<T>) | ||
where | ||
T::Real: AbsDiffEq<Epsilon = T::Real>, | ||
{ | ||
assert_eq!(a.layout().unwrap().size(), (4, 3)); | ||
assert_eq!(bs.layout().unwrap().size(), (4, 2)); | ||
|
||
let result = a.least_squares(&bs).unwrap(); | ||
// unpack result | ||
let xs = result.solution; | ||
let residual_l2_square = result.residual_sum_of_squares.unwrap(); | ||
|
||
// Must be full-rank | ||
assert_eq!(result.rank, 3); | ||
|
||
for j in 0..2 { | ||
let b = bs.index_axis(Axis(1), j); | ||
let x = xs.index_axis(Axis(1), j); | ||
let residual = &b - &a.dot(&x); | ||
let residual_l2_sq = residual_l2_square[j]; | ||
assert!(residual_l2_sq.abs_diff_eq(&residual.norm_l2().powi(2), T::real(1.0e-4))); | ||
|
||
// `|residual| < |b|` | ||
assert!(residual.norm_l2() < b.norm_l2()); | ||
} | ||
} | ||
|
||
macro_rules! impl_overdetermined { | ||
($scalar:ty) => { | ||
paste::item! { | ||
#[test] | ||
fn [<least_squares_ $scalar _overdetermined_ac_bc>]() { | ||
let a: Array2<f64> = random((4, 3)); | ||
let b: Array2<f64> = random((4, 2)); | ||
test_overdetermined(a, b) | ||
} | ||
|
||
/* Unsupported currently. See https://github.com/rust-ndarray/ndarray-linalg/issues/234 | ||
#[test] | ||
fn [<least_squares_ $scalar _overdetermined_af_bc>]() { | ||
let a: Array2<f64> = random((4, 3).f()); | ||
let b: Array2<f64> = random((4, 2)); | ||
test_overdetermined(a, b) | ||
} | ||
#[test] | ||
fn [<least_squares_ $scalar _overdetermined_ac_bf>]() { | ||
let a: Array2<f64> = random((4, 3)); | ||
let b: Array2<f64> = random((4, 2).f()); | ||
test_overdetermined(a, b) | ||
} | ||
*/ | ||
|
||
#[test] | ||
fn [<least_squares_ $scalar _overdetermined_af_bf>]() { | ||
let a: Array2<f64> = random((4, 3).f()); | ||
let b: Array2<f64> = random((4, 2).f()); | ||
test_overdetermined(a, b) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_overdetermined!(f32); | ||
impl_overdetermined!(f64); | ||
impl_overdetermined!(c32); | ||
impl_overdetermined!(c64); | ||
|
||
/// #column > #row case. | ||
/// Linear problem is underdetermined, `|b - Ax| = 0` and `x` is not unique | ||
fn test_underdetermined<T: Scalar + Lapack>(a: Array2<T>, b: Array2<T>) { | ||
assert_eq!(a.layout().unwrap().size(), (3, 4)); | ||
assert_eq!(b.layout().unwrap().size(), (3, 2)); | ||
|
||
let result = a.least_squares(&b).unwrap(); | ||
assert_eq!(result.rank, 3); | ||
assert!(result.residual_sum_of_squares.is_none()); | ||
|
||
// b == Ax | ||
let x = result.solution; | ||
let ax = a.dot(&x); | ||
assert_close_l2!(&b, &ax, T::real(1.0e-4)); | ||
} | ||
|
||
macro_rules! impl_underdetermined { | ||
($scalar:ty) => { | ||
paste::item! { | ||
#[test] | ||
fn [<least_squares_ $scalar _underdetermined_ac_bc>]() { | ||
let a: Array2<f64> = random((3, 4)); | ||
let b: Array2<f64> = random((3, 2)); | ||
test_underdetermined(a, b) | ||
} | ||
|
||
/* Unsupported currently. See https://github.com/rust-ndarray/ndarray-linalg/issues/234 | ||
#[test] | ||
fn [<least_squares_ $scalar _underdetermined_af_bc>]() { | ||
let a: Array2<f64> = random((3, 4).f()); | ||
let b: Array2<f64> = random((3, 2)); | ||
test_underdetermined(a, b) | ||
} | ||
#[test] | ||
fn [<least_squares_ $scalar _underdetermined_ac_bf>]() { | ||
let a: Array2<f64> = random((3, 4)); | ||
let b: Array2<f64> = random((3, 2).f()); | ||
test_underdetermined(a, b) | ||
} | ||
*/ | ||
|
||
#[test] | ||
fn [<least_squares_ $scalar _underdetermined_af_bf>]() { | ||
let a: Array2<f64> = random((3, 4).f()); | ||
let b: Array2<f64> = random((3, 2).f()); | ||
test_underdetermined(a, b) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_underdetermined!(f32); | ||
impl_underdetermined!(f64); | ||
impl_underdetermined!(c32); | ||
impl_underdetermined!(c64); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/f64/$scalar/g