# Issue arising from QR decomposition changes between 0.6 to 0.7

**URL:** <https://discourse.julialang.org/t/issue-arising-from-qr-decomposition-changes-between-0-6-to-0-7/17791>\
**Category:** General Usage\
**Tags:** linearalgebra\
**Created:** [November 20, 2018, 8:39pm UTC](https://discourse.julialang.org/t/issue-arising-from-qr-decomposition-changes-between-0-6-to-0-7/17791 "2018-11-20T20:39:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![omalled](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/omalled/32/12532_2.png) [@omalled](https://discourse.julialang.org/u/omalled)\
**Post date:** [November 20, 2018, 8:39pm UTC](https://discourse.julialang.org/t/issue-arising-from-qr-decomposition-changes-between-0-6-to-0-7/17791/1 "2018-11-20T20:39:26Z")

</div>

I have an implementation of Algorithm 4 from [this paper](https://arxiv.org/pdf/1609.00048.pdf) that works on julia 0.6, but is failing on julia 0.7. The crux of the issue can be demonstrated with these two lines of code:

```julia
U, T = qr(randn(10, 5))#analogous to line 3 in the paper's algorithm
T \ (transpose(U) * randn(10, 5))#analogous to line 4 in the paper's algorithm

```

Julia 0.7 complains about a dimension mismatch between `T` and `transpose(U) * randn(10, 5)` on the second line. This makes sense since `size(T) == (5, 5)` and `size(transpose(U) * randn(10, 5)) == (10, 5)`. However, some other operations that ostensibly involve a dimension mismatch do work (e.g., `U * T` works despite the fact that `size(U) == (10, 10)` and `size(T) == (5, 5)`). I have seen [this](https://discourse.julialang.org/t/about-qr-decomposition-linear-algebra/9606) related post, but I haven’t been able to extract a solution from it.

Is there a way to reorganize the operations (or something) so that this will work on julia 0.7? It seems like it should be possible to implement Algorithm 4 from [this paper](https://arxiv.org/pdf/1609.00048.pdf) in a few lines of julia code, but I am struggling to do that with julia 0.7 (even though I have such an implementation for julia 0.6). Anyone have ideas?

---

<div class="post-metadata">

**Author:** ![omalled](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/omalled/32/12532_2.png) [@omalled](https://discourse.julialang.org/u/omalled)\
**Post date:** [November 21, 2018, 4:54pm UTC](https://discourse.julialang.org/t/issue-arising-from-qr-decomposition-changes-between-0-6-to-0-7/17791/3 "2018-11-21T16:54:29Z")

</div>

Thanks, but running this on 0.7 doesn’t produce the same result as my original code on 0.6. Is there some reason why we should expect this to work (aside from that the sizes of the objects line up)? Here’s what I came up with for 0.7 that seems to be equivalent to my original code on 0.6:

```julia
F = qr(randn(10, 5))
x = F.R \ (Matrix(F.Q)' * randn(10, 5))

```

This note from the [documentation](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/index.html#Standard-Functions-1) (which unfortunately isn’t in the `?qr` documentation for julia 0.7) was the key to figuring this out:

> A `Q` matrix can be converted into a regular matrix with [`Matrix`](https://docs.julialang.org/en/v1/base/arrays/#Base.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.

I thought I’d make a note of it here in case someone finds it helpful in the future.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 21, 2018, 5:06pm UTC](https://discourse.julialang.org/t/issue-arising-from-qr-decomposition-changes-between-0-6-to-0-7/17791/4 "2018-11-21T17:06:40Z")

</div>

Same experience here. And the same solution

```julia
fact = qr(A); 
p = UpperTriangular(fact.R)\(transpose(Array(fact.Q))*b) # R \ (transpose(Q) * b)

```

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [November 22, 2018, 1:26pm UTC](https://discourse.julialang.org/t/issue-arising-from-qr-decomposition-changes-between-0-6-to-0-7/17791/5 "2018-11-22T13:26:12Z")

</div>

In Julia 0.6, `Q, R = qr(::Matrix)` would yield two matrices. In Julia 0.7 and on `F = qr(::Matrix)` yields a `<:Factorization`. The `Q` is kept as `QRCompactWYQ`. If you want the `Matrix` version of it, you can do `Matrix(F.Q)`. For additional information do check the [documentation](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.qr).

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 22, 2018, 1:40pm UTC](https://discourse.julialang.org/t/issue-arising-from-qr-decomposition-changes-between-0-6-to-0-7/17791/6 "2018-11-22T13:40:38Z")

</div>

If want to compute `R \ (Q'*b)` — this is the least-square solution — just do `F \ b` where `F` is the factorization object returned by `qr`.

For example, the following all compute the same thing (up to roundoff errors):

```julia
julia> A = randn(10, 5); b = randn(10);

julia> A \ b
5-element Array{Float64,1}:
 -2.1381793415186894
 -1.925894533068401 
 -0.6539752131528996
  0.2931584134939902
 -0.4190898942664864

julia> F = qr(A); F \ b
5-element Array{Float64,1}:
 -2.1381793415186863 
 -1.925894533068399  
 -0.6539752131528995 
  0.29315841349399047
 -0.41908989426648574

julia> F.R \ (Matrix(F.Q)' * b)
5-element Array{Float64,1}:
 -2.1381793415186867 
 -1.9258945330683994 
 -0.6539752131528997 
  0.29315841349399074
 -0.41908989426648574

```

---

<div class="post-metadata">

**Author:** ![omalled](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/omalled/32/12532_2.png) [@omalled](https://discourse.julialang.org/u/omalled)\
**Post date:** [November 26, 2018, 5:30pm UTC](https://discourse.julialang.org/t/issue-arising-from-qr-decomposition-changes-between-0-6-to-0-7/17791/7 "2018-11-26T17:30:18Z")

</div>

Thanks, Steven. This seems like the ideal solution, and makes me wonder “Why didn’t I think of that?”

---

<div class="post-metadata">

**Author:** ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)\
**Post date:** [April 7, 2019, 1:27am UTC](https://discourse.julialang.org/t/issue-arising-from-qr-decomposition-changes-between-0-6-to-0-7/17791/8 "2019-04-07T01:27:31Z")

</div>

> [@stevengj](#):
>
> F = qr(A); F \ b

`ERROR: UndefVarError: qr not defined`

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 7, 2019, 1:42am UTC](https://discourse.julialang.org/t/issue-arising-from-qr-decomposition-changes-between-0-6-to-0-7/17791/9 "2019-04-07T01:42:15Z")

</div>

You need `julia> using LinearAlgebra`. Please do not post on old threads.
