-
Notifications
You must be signed in to change notification settings - Fork 520
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
mat: calculate Q lazily when calling QR.ToQ #1970
Open
kortschak
wants to merge
1
commit into
master
Choose a base branch
from
mat/qr_very_tall
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid this doesn't work because
q
is currently needed in theAt
method. The test passes just becauseQTo
is called in the test beforeEqualApprox
(which callsAt
).We could reconstruct the necessary row of Q in each call to
At
which is terrible but hopefully nobody usesQR
asMatrix
except in the call toSolve
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other alternative is to lazily calculate Q for
At
the same way it is forToQ
, with a warning that it may cause OoM. I think your approach is probably better. A warning in the docs that it will be extremely inefficient should be enough.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could lazily compute
q
in theAt()
method ifq
is nil or empty, in the same way it is done in theQto
.But I guess I'm also a bit confused as to why we need
q
at all inAt()
in the first place. When we factorize a matrixa
,qr
stores a copy of that matrix. Why not return directlyqr.At()
?Isn't https://github.com/gonum/gonum/blame/2ad11cabb395b96efc5b67fa1b64480762d9e703/mat/qr.go#L46 also faulty?
I'm expected we should return
Q*R
at element(i,j)
, but instead it seems we're returningQ*A
at element(i,j)
, withA = Q*R
, the matrix we factorize.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. If one element is needed, then most likely all are.
That copy is overwritten by
lapack64.Geqrf
which efficiently stores both Q and R in the same matrix.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another alternative is to indeed store
a
inFactorize
and compute Q inQTo
without storing it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we would need to copy
a
in this situation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the end I think it's best to lazily compute Q in At. QR.At should never be called in practice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking that @tvkn's suggestion of conditionally computing element of QR for
At
via the relevant row of Q if Q is not already calculated, otherwise falling through to the code that is there now.This is safer than just lazily calculating Q which has the risk of someone thinking the code is fine until a tall matrix is used down the track. At worst with this approach there will be a perf hit, but not a crash.
I don't have time to do this, so if someone wants to pick this up. I'm happy for that to happen.