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

Make size(F.Q) == size(Matrix(F.Q))? #513

Open
andreasnoack opened this issue Mar 23, 2018 · 6 comments
Open

Make size(F.Q) == size(Matrix(F.Q))? #513

andreasnoack opened this issue Mar 23, 2018 · 6 comments
Labels
docs This change adds or pertains to documentation

Comments

@andreasnoack
Copy link
Member

Following up on the discussion in JuliaLang/julia#26392. E.g.

julia> using LinearAlgebra

julia> A = randn(4,2);

julia> F = qrfact(A);

julia> size(F.Q), size(Matrix(F.Q))
((4, 4), (4, 2))
@andreasnoack andreasnoack added docs This change adds or pertains to documentation linear algebra labels Mar 23, 2018
@StefanKarpinski
Copy link
Member

StefanKarpinski commented Mar 23, 2018

What about exposing the truncated Matrix(F.Q) as a different accessor instead? Maybe as F.Q0?

@Nosferican
Copy link

Nosferican commented Mar 27, 2018

It has been bugging me for a while that F.Q behaves like a Matrix in a is not really Q way.

using LinearAlgebra, Random
srand(0)
X = rand(100, 3)
B = rand(100, 4)
F = qrfact(X)
F.Q * B
Matrix(F.Q) * B

I prefer exposing Q more often than the compact Householder elementary reflectors.

@dlfivefifty
Copy link
Contributor

I'm a bit confused what the use of the square Q is. For example, I was surprised this fails:

julia> Q,R = qr(randn(5,3));

julia> b = randn(5);

julia> R \ (Q'b)
ERROR: DimensionMismatch("B has first dimension 5 but needs 3")
Stacktrace:
 [1] trtrs!(::Char, ::Char, ::Char, ::Array{Float64,2}, ::Array{Float64,1}) at /Users/solver/Projects/julia-1.1/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/lapack.jl:3340
 [2] ldiv! at /Users/solver/Projects/julia-1.1/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/triangular.jl:583 [inlined]
 [3] \ at /Users/solver/Projects/julia-1.1/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/triangular.jl:1854 [inlined]
 [4] \(::Array{Float64,2}, ::Array{Float64,1}) at /Users/solver/Projects/julia-1.1/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/generic.jl:903
 [5] top-level scope at none:0

@andreasnoack
Copy link
Member Author

I'm a bit confused what the use of the square Q is

The way I see it is that a compact representation of Q naturally is square. That is also how LAPACK applies a compact QR. An application where the square behavior can be useful is the statistical application of least squares where you'd also like sum of the squared residuals which can be computed without computing the residual since

norm(A*x - b)^2 = norm(Q*[R; 0]*x - b)^2 = norm([R*x; 0] - Q'*b)^2 =
  norm(R*x - Q0'*b)^2 + norm(Q1'*b)^2 = norm(Q1'*b)^2

when x is the least squares solution R\(Q0'*b).

@tkf
Copy link
Member

tkf commented Aug 20, 2019

The issue in the OP seems to be already addressed by JuliaLang/julia#28446. But is it still an option to add a different accessor as suggested by @StefanKarpinski #513 and perhaps "fix" Matrix(Q) s.t. size(F.Q) == size(Matrix(F.Q))?

@andreasnoack andreasnoack changed the title Document that size(F.Q) != size(Matrix(F.Q)) Make size(F.Q) == size(Matrix(F.Q))? Aug 20, 2019
@cortner
Copy link

cortner commented Aug 23, 2019

+1 for enabling ‘R \ (Q'b)’ or something similar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

No branches or pull requests

7 participants