Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance qr function with mode argument #114

Merged
merged 4 commits into from
Oct 31, 2023
Merged

Enhance qr function with mode argument #114

merged 4 commits into from
Oct 31, 2023

Conversation

jofrevalles
Copy link
Member

Summary

Currently, the qr decomposition on a Tensor t defaults to the "full" mode, which given a matrix (M, N) with M>N we get a Q matrix with size (M, M) and R with size (N, N). This is counter-intuitive since these two tensors share a virtual index but they do not have the same dimensions.

In this PR we fix this by adding a mode argument to the qr function (which by default is set to :reduced). With mode=:reduced, it will ensure that the dimensions of the virtual index match and with mode=:full the qr functions returns the full matrices.

Example of usage

This enhancement facilitates more intuitive operations as illustrated below:

 julia> using Tenet

 julia> A = Tensor(rand(32, 8), (:i, :j))
32×8 Tensor{Float64, 2, Matrix{Float64}}: ...

julia> Q, R = qr(A, :reduced; left_inds=(:i,))
...

julia> size(Q), size(R)
((32, 8), (8, 8))

julia> Q, R = qr(A, :full; left_inds=(:i,))
...

julia> size(Q), size(R)
((32, 32), (8, 8))

@mofeing
Copy link
Member

mofeing commented Oct 28, 2023

I see the problem but I don't think this is the solution. In the documentation of LinearAlgebra.qr function you can find the following paragraph:

...
Multiplication with respect to either full/square or non-full/square Q is allowed, i.e. both F.Q*F.R and F.Q*A are supported. A Q matrix can be converted into a regular matrix with Matrix. This operation returns the "thin" Q factor, i.e., if A is m×n with m>=n, then Matrix(F.Q) yields an m×n matrix with orthonormal columns. To retrieve the "full" Q factor, an m×m orthogonal matrix, use F.Q*I. If m<=n, then Matrix(F.Q) yields an m×m orthogonal matrix.
...

I would default to retrieving the thin Q matrix without a mode argument.

@jofrevalles
Copy link
Member Author

I would default to retrieving the thin Q matrix without a mode argument.

Okay, this also makes sense. In this way we always get size(Q, virtualind)=size(R, virtualind) which follows our implementation.

@codecov
Copy link

codecov bot commented Oct 30, 2023

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (e7e5fae) 0.23% compared to head (baf5d24) 0.23%.

Additional details and impacted files
@@           Coverage Diff           @@
##           develop    #114   +/-   ##
=======================================
  Coverage     0.23%   0.23%           
=======================================
  Files           14      14           
  Lines          862     862           
=======================================
  Hits             2       2           
  Misses         860     860           
Files Coverage Δ
src/Numerics.jl 0.00% <0.00%> (ø)

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

@mofeing
Copy link
Member

mofeing commented Oct 31, 2023

Is the PR ready?

@jofrevalles
Copy link
Member Author

Is the PR ready?

Yes!

@mofeing mofeing merged commit 2089a76 into develop Oct 31, 2023
4 of 5 checks passed
@mofeing mofeing deleted the feature/enhance-qr branch October 31, 2023 13:22
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.

2 participants