Skip to content

Derived-types for matrix factorizations : QR#1092

Draft
loiseaujc wants to merge 17 commits intofortran-lang:masterfrom
loiseaujc:disciplined_linalg
Draft

Derived-types for matrix factorizations : QR#1092
loiseaujc wants to merge 17 commits intofortran-lang:masterfrom
loiseaujc:disciplined_linalg

Conversation

@loiseaujc
Copy link
Contributor

@loiseaujc loiseaujc commented Jan 21, 2026

Following #1040, this PR proposes a derived-type to store the QR factorization of a matrix. It is quite similar to what is being done in Julia and use xGEQRT driver from lapack to store the QR factorization using the compact WY format which is beneficial for performances (blocked algorithm + efficient specialized drivers matrix-matrix products).

Proposed interfaces

use stdlib_linalg, only: qr_rdp_type, qrfact
type(qr_rdp_type) :: F

! QR factorization of the matrix.
F = qrfact(A)

! Extract the Q and R components.
R = F%R()
Q = F%Q()

I still need to iterate a bit for the matrix-vector/matrix-matrix product.

Note - I don't quite like the qr_rdp_type naming for the derived-type but I ain't sure how to come with a better name. An alternative would be to use parameterized types but I'm not too confident about the actual support by all the compilers we test in the CI.

Other potential additions

This may be the subject of a different PR but I'll start the discussion here anyway as it may be relevant. Once the implementation is working/validated/integrated, I was wondering whether we should eventually defined an equivalent of the \ operator in Matlab and Julia for solving linear systems with a very simple x = A \ b which dispatch to the appropriate driver (solve for square matrix, lstsq for overdetermined ones, solve_lu if A is coming from the future lufact, specialized one for qrfact, etc). This is essentially syntactic sugar coating I'll admit but it can be a nice selling point for new comers: don't worry too much about how to get the performances out of your code, we make sure you have very descent performances by default (but if you really want to get the best perfs possible, you still need to go low-level and avoid allocations, etc).

Progress

  • Interfaces
    • qrfact : Instantiate derived-type for QR factorization.
    • qrmv/qrmm : Specialized driver for matrix-vector and matrix-matrix products.
  • Auxiliary functions
    • handle_geqrt_info : Handle info flag returned by geqrt (compact WY QR factorization).
    • handle_gemqrt_info : Handle info flag return by gemqrt (matrix-matrix product with compact WY storage).
  • Base implementation
    • qrfact
    • qrmv/qrmm
    • self%Q() : Extract the Q matrix from the Compact WY representation.
    • self%R() : Extract the R matrix from the Compact WY representation.
  • Tests
    • Random tall matrices
      • qrfact, self%Q(), self%R()
      • qrmv/qrmm
    • Random wide matrices
      • qrfact, self%Q(), self%R()
      • qrmv/qrmm
    • Random tall rank-deficient matrices
      • qrfact, self%Q(), self%R()
      • qrmv/qrmm
  • In-code documentation
    • qrfact, self%Q(), self%R()
    • qrmv/qrmm
  • Specifications
    • qrfact
    • qrmv/qrmm
  • Examples

Ping: @perazz, @jvdp1, @jalvesz

@loiseaujc loiseaujc added topic: mathematics linear algebra, sparse matrices, special functions, FFT, random numbers, statistics, ... topic: container (Abstract) data structures and containers topic: linalg Linear algebra labels Jan 21, 2026
@loiseaujc loiseaujc linked an issue Jan 21, 2026 that may be closed by this pull request
@codecov
Copy link

codecov bot commented Jan 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 68.55%. Comparing base (0ede301) to head (cdc4a9c).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1092   +/-   ##
=======================================
  Coverage   68.55%   68.55%           
=======================================
  Files         396      396           
  Lines       12746    12746           
  Branches     1376     1376           
=======================================
  Hits         8738     8738           
  Misses       4008     4008           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: container (Abstract) data structures and containers topic: linalg Linear algebra topic: mathematics linear algebra, sparse matrices, special functions, FFT, random numbers, statistics, ...

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Derived type for matrix factorizations

1 participant