dimforge / dimforge/nalgebra

Eigen::new() can panic via unwrap() on Schur decomposition failure

Open
#1,594 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
4.8k
Forks
565
PR merge metrics
No merged PRs in 30d

Description

## Description

`Eigen::new()` returns `Option` but internally calls `.unwrap()` on `Schur::new()`, which can panic before `Eigen::new()` has a chance to return `None`:

```rust
// src/linalg/eigen.rs, line 72
let (mut eigenvectors, mut eigenvalues) = Schur::new(m, 0).unwrap().unpack();
```

`Schur::new` returns `Option>` and can fail for matrices where the Schur decomposition does not converge. When it does, the `.unwrap()` panics instead of propagating the failure as `None`.

## Impact

This is inconsistent with every other decomposition in nalgebra (`Cholesky::new`, `LU::new`, `SVD::try_new`, etc.), which return `Option`/`Result` on failure without panicking. Users relying on `Eigen::new()` returning `None` gracefully may get unexpected panics instead.

## Expected behaviour

```rust
let (mut eigenvectors, mut eigenvalues) = Schur::new(m, 0)?.unpack();
// or
let (mut eigenvectors, mut eigenvalues) = Schur::new(m, 0)
.map(|s| s.unpack())
.unwrap_or_else(|| return None);
```

## Fix

Replace `.unwrap()` with `?` (using early-return `None`) or an explicit `match`/`?` to propagate the failure as `None` from `Eigen::new()`.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.