Skip to content

Fix Inverse for LUFactorized #297

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
merged 4 commits into from
Jun 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lax/src/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ macro_rules! impl_solve {

fn inv(l: MatrixLayout, a: &mut [Self], ipiv: &Pivot) -> Result<()> {
let (n, _) = l.size();
if n == 0 {
// Do nothing for empty matrices.
return Ok(());
}

// calc work size
let mut info = 0;
Expand Down
13 changes: 10 additions & 3 deletions ndarray-linalg/src/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ pub trait Solve<A: Scalar> {
pub struct LUFactorized<S: Data + RawDataClone> {
/// The factors `L` and `U`; the unit diagonal elements of `L` are not
/// stored.
pub a: ArrayBase<S, Ix2>,
a: ArrayBase<S, Ix2>,
/// The pivot indices that define the permutation matrix `P`.
pub ipiv: Pivot,
ipiv: Pivot,
}

impl<A, S> Solve<A> for LUFactorized<S>
Expand Down Expand Up @@ -323,8 +323,15 @@ where
type Output = Array2<A>;

fn inv(&self) -> Result<Array2<A>> {
// Preserve the existing layout. This is required to obtain the correct
// result, because the result of `A::inv` is layout-dependent.
let a = if self.a.is_standard_layout() {
replicate(&self.a)
} else {
replicate(&self.a.t()).reversed_axes()
};
let f = LUFactorized {
a: replicate(&self.a),
a,
ipiv: self.ipiv.clone(),
};
f.inv_into()
Expand Down
103 changes: 93 additions & 10 deletions ndarray-linalg/tests/inv.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,103 @@
use ndarray::*;
use ndarray_linalg::*;

fn test_inv_random<A>(n: usize, set_f: bool, rtol: A::Real)
where
A: Scalar + Lapack,
{
let a: Array2<A> = random([n; 2].set_f(set_f));
let identity = Array2::eye(n);
assert_close_l2!(&a.inv().unwrap().dot(&a), &identity, rtol);
assert_close_l2!(
&a.factorize().unwrap().inv().unwrap().dot(&a),
&identity,
rtol
);
assert_close_l2!(
&a.clone().factorize_into().unwrap().inv().unwrap().dot(&a),
&identity,
rtol
);
}

fn test_inv_into_random<A>(n: usize, set_f: bool, rtol: A::Real)
where
A: Scalar + Lapack,
{
let a: Array2<A> = random([n; 2].set_f(set_f));
let identity = Array2::eye(n);
assert_close_l2!(&a.clone().inv_into().unwrap().dot(&a), &identity, rtol);
assert_close_l2!(
&a.factorize().unwrap().inv_into().unwrap().dot(&a),
&identity,
rtol
);
assert_close_l2!(
&a.clone()
.factorize_into()
.unwrap()
.inv_into()
.unwrap()
.dot(&a),
&identity,
rtol
);
}

#[test]
fn inv_empty() {
test_inv_random::<f32>(0, false, 0.);
test_inv_random::<f64>(0, false, 0.);
test_inv_random::<c32>(0, false, 0.);
test_inv_random::<c64>(0, false, 0.);
}

#[test]
fn inv_random_float() {
for n in 1..=8 {
for &set_f in &[false, true] {
test_inv_random::<f32>(n, set_f, 1e-3);
test_inv_random::<f64>(n, set_f, 1e-9);
}
}
}

#[test]
fn inv_random_complex() {
for n in 1..=8 {
for &set_f in &[false, true] {
test_inv_random::<c32>(n, set_f, 1e-3);
test_inv_random::<c64>(n, set_f, 1e-9);
}
}
}

#[test]
fn inv_into_empty() {
test_inv_into_random::<f32>(0, false, 0.);
test_inv_into_random::<f64>(0, false, 0.);
test_inv_into_random::<c32>(0, false, 0.);
test_inv_into_random::<c64>(0, false, 0.);
}

#[test]
fn inv_random() {
let a: Array2<f64> = random((3, 3));
let ai: Array2<_> = (&a).inv().unwrap();
let id = Array::eye(3);
assert_close_l2!(&ai.dot(&a), &id, 1e-7);
fn inv_into_random_float() {
for n in 1..=8 {
for &set_f in &[false, true] {
test_inv_into_random::<f32>(n, set_f, 1e-3);
test_inv_into_random::<f64>(n, set_f, 1e-9);
}
}
}

#[test]
fn inv_random_t() {
let a: Array2<f64> = random((3, 3).f());
let ai: Array2<_> = (&a).inv().unwrap();
let id = Array::eye(3);
assert_close_l2!(&ai.dot(&a), &id, 1e-7);
fn inv_into_random_complex() {
for n in 1..=8 {
for &set_f in &[false, true] {
test_inv_into_random::<c32>(n, set_f, 1e-3);
test_inv_into_random::<c64>(n, set_f, 1e-9);
}
}
}

#[test]
Expand Down