nalgebra icon indicating copy to clipboard operation
nalgebra copied to clipboard

possible bug, hanging on getting eigenvalues?

Open hwchen opened this issue 6 years ago • 4 comments

Hi,

I’m trying to find the roots of a polynomial, and nalgebra has overall worked great.

Recently, I’ve found that my calculations hang on certain inputs, for which numpy immediately returns a value.

You can see my code for finding roots at https://github.com/hwchen/roots.

A pathological input is in this test case, which you can also see in the repo's unit tests:

   #[test]
    fn hanging() {
        let input = &[
            -770.9700059294701,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            0.0,
            434.3959909081459,
            0.0,
        ];

        let input: Vec<f64> = input.into_iter().rev().cloned().collect();
        dbg!(&input);
        let rs = roots(input.as_slice());
    }

Thanks for any help.

hwchen avatar Jun 22 '19 04:06 hwchen

Hi! This is a bug. It seems like the following matrix makes the Schur decomposition loop indefinitely:


  ┌                                                                                                                                                                                               ┐
  │  0 0 0 0 0 0 0 0 0 0 1.7748092111017977 │
  │  1 0 0 0 0 0 0 0 0 0 │
  │  0 1 0 0 0 0 0 0 0 0 │
  │  0 0 1 0 0 0 0 0 0 0 │
  │  0 0 0 1 0 0 0 0 0 0 │
  │  0 0 0 0 1 0 0 0 0 0 │
  │  0 0 0 0 0 1 0 0 0 0 │
  │  0 0 0 0 0 0 1 0 0 0 │
  │  0 0 0 0 0 0 0 1 0 0 │
  │  0 0 0 0 0 0 0 0 1 0 │

For what it's worth, note that you can use Schur::try_new to specify a maximum number of iterations. This does not fix your problem, but at least this gives you a way to avoid the infinite hanging (though the result will be None in those cases).

sebcrozet avatar Jun 22 '19 16:06 sebcrozet

Thanks! try_new will get me through for now, looking forward to the bugfix. Let me know if there's anything I can do to help.

hwchen avatar Jun 25 '19 00:06 hwchen

I can reproduce this issue with multiple matrices. I also tested your solution @hwchen which works great on most occasions. However, there are instances in which the function complex_eigenvalues() returns NaN while eigenvalues() returns correct eigenvalues. One such case is with:

fn test_real_roots_1() {
    dbg!(roots(&vec![-225., 0., 224., 0., 0.]));
}

This yields:

roots(p) = [
    Complex {
        re: 0.0,
        im: NaN
    },
    Complex {
        re: 0.0,
        im: NaN
    },
    Complex {
        re: 0.0,
        im: 0.0
    },
    Complex {
        re: 0.0,
        im: 0.0
    }
]

While just calling eigenvalues() yields:

Matrix {
    data: VecStorage {
        data: [
            0.9977753031397177,
            -0.9977753031397177
        ],
        nrows: Dynamic {
            value: 2
        },
        ncols: U1
    }
}

I might be wrong here but complex_eigenvalues should return all real eigenvalues in complex form right? Thanks for any help.

TimsHisProjects avatar Sep 27 '19 07:09 TimsHisProjects

trying to find the eigenvalues on the matrix that sebcrozet mentionned above, I can now find these following eigenvalues with the latest main branch :

Index Real Imaginary
0 1.0535378724316184 0.0
1 0.8862924571545396 0.5695855765718559
2 0.8862924571545396 -0.5695855765718559
3 -1.0108621860348992 0.29681591852944
4 -1.0108621860348992 -0.29681591852944
5 -0.6899205843797193 0.7962107986562793
6 -0.6899205843797193 -0.7962107986562793
7 0.4376554489741552 0.9583317570815159
8 0.4376554489741552 -0.9583317570815159
9 -0.1499340719298839 1.042814375966418
10 -0.1499340719298839 -1.042814375966418

Toucan4Life avatar Dec 08 '25 12:12 Toucan4Life