Skip to content

Conversation

geo-ant
Copy link
Collaborator

@geo-ant geo-ant commented Sep 27, 2025

This PR adds column-pivoted QR decomposition to nalgebra-lapack. This is a pretty useful feature, because while the "normal" nalgebra crate also has column-pivoted QR, it will only solve square linear systems. The nalgebra-lapack implementation can be used for both square and overdetermined linear systems, which should be a useful addition.

UPDATE:

  • This PR also updates to the newest version of the lapack-src crate, which fixes Undefined Behavior and Segfaults When Linking MKL in nalgebra-lapack #1552.
  • Because the netlib backend was updated due to the above dependency, I got a failing test because the newer netlib calculates the eigenvectors with flipped sign compared to before. This also made me spot (and fix) an error in the complex eigendecomposition logic. I also improved those tests as a drive-by fix.
  • Changed the feature flags to switch lapack backends and made the documentation clearer.

/// routine for column pivoting QR decomposition using level 3 BLAS,
/// see https://www.netlib.org/lapack/lug/node42.html
/// or https://www.intel.com/content/www/us/en/docs/onemkl/developer-reference-c/2023-0/geqp3.html
fn xgeqp3(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that all functions of ColPivQrScalar must be unsafe because underlying lapack functions do not check the lengths of slices.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Frankly, I think all the lapack trait functions in this crate must be unsafe. That is something I already have on my agenda for later, but I will definitely update the stuff inside my current PR.

xormqr = $xormqr:path $(,)?
) => {
impl ColPivQrReal for $type {
fn xormqr(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also looks unsafe...

@geo-ant geo-ant requested a review from im-0 September 28, 2025 06:04
// without pivoting. I'm not 100% sure that we can't abstract over real and
// complex behavior in the scalar trait, but I'll keep it like this for now.
#[allow(missing_docs)]
pub unsafe trait ColPivQrReal: ColPivQrScalar + QRReal {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that unsafe is not required here for a whole trait as merely implementing this trait does not make any safe code that does not use any unsafe methods of this trait itself unsafe.

Otherwise safety-related changes LGTM.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I went a bit overboard, but my thought was that I'd like to basically prevent people from implementing those traits themselves. I think what I'll actually do is make those traits sealed.

/// Utility trait to add a thin abstraction layer over lapack functionality for
/// column pivoted QR decomposition.
#[allow(missing_docs)]
pub unsafe trait ColPivQrScalar: ComplexField + QRScalar {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same.

@geo-ant geo-ant requested a review from im-0 October 6, 2025 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Undefined Behavior and Segfaults When Linking MKL in nalgebra-lapack

2 participants