dimforge / dimforge/nalgebra

Wrong result of nalgebra::base::Matrix::min() and ::max()

Open
#620 16 comments 0 reactions 0 assignees View on GitHub
bug good first issue P-high
Dominant language
Rust
Stars
4.8k
Forks
565
PR merge metrics
No merged PRs in 30d

Description

The methods `nalgebra::base::Matrix::min()` and `nalgebra::base::Matrix::max()` are documented as

> Returns the component with the smallest value.

and

> Returns the component with the largest value.

However, `A.min()` actually returns `N::zero().min(A[..])` (if this was a valid expression) and `A.max()` returns `N::zero().max(A[..])`. For example, this corresponds to

- `na::Vector3::new(-1.0, -2.0, -3.0).max()` returning 0.0
- `na::Vector3::new(1.0, 2.0, 3.0).min()` returning 0.0

This is due to the fact that both rely on [`xcmp`](https://github.com/rustsim/nalgebra/blob/0384268bd4c757b5b0125d63bb0599695b961c48/src/base/ops.rs#L871) which always initializes the comparison value with `N::zero()` instead of e.g. the first value of the matrix. This is either a bug in the code or in the documentation.

Currently I have to use `A[A.imin()]` instead of `A.min()`.

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.