ndarray-linalg icon indicating copy to clipboard operation
ndarray-linalg copied to clipboard

InverseHInto returns incorrect results for row major arrays

Open jturner314 opened this issue 3 years ago • 1 comments

This example

use approx::assert_abs_diff_eq;
use ndarray::*;
use ndarray_linalg::*;

fn main() {
    let arr = array![[1., 3.], [3., 1.]];
    assert_abs_diff_eq!(
        arr.invh_into().unwrap(),
        array![[-0.125, 0.375], [0.375, -0.125]],
        epsilon = 1e-9,
    );
}

fails with the following message

thread 'main' panicked at 'assert_abs_diff_eq!(arr.invh_into().unwrap(), array![[- 0.125, 0.375], [0.375, - 0.125]], epsilon = 1e-9)

    left  = [[-0.125, 3.0],
 [3.0, -0.125]], shape=[2, 2], strides=[2, 1], layout=Cc (0x5), const ndim=2
    right = [[-0.125, 0.375],
 [0.375, -0.125]], shape=[2, 2], strides=[2, 1], layout=Cc (0x5), const ndim=2

It seems that invh_into is copying the incorrect triangular portion to overwrite the other triangular portion of the matrix after calling the LAPACK routine. This bug is layout-dependent. Changing arr in the example to array![[1., 3.], [3., 1.]].reversed_axes() causes the assertion to pass.

jturner314 avatar May 10 '21 19:05 jturner314

As far as I can tell, this affects InverseH as well (i.e. the non-moving version).

felix-clark avatar Oct 15 '21 00:10 felix-clark