# LU factorization and non-square matrices

**URL:** https://discourse.julialang.org/t/lu-factorization-and-non-square-matrices/108834
**Category:** Numerics
**Tags:** linearalgebra
**Created:** [January 15, 2024, 2:34pm UTC](https://discourse.julialang.org/t/lu-factorization-and-non-square-matrices/108834 "2024-01-15T14:34:34Z")
**Posts on this page:** 1
**Showing post:** 2

<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: [January 15, 2024, 3:12pm UTC](https://discourse.julialang.org/t/lu-factorization-and-non-square-matrices/108834/2 "2024-01-15T15:12:01Z")

</div>

> [@sijo](#):
>
> I’m curious why LU (with pivoting) is used for square matrices while QR is used for non-square ones?

For least-square solutions with a non-square or singular matrix, the LU factorization is useless — it doesn’t simplify the calculation.

QR factorization, in contrast, helps you solve least-squares problems because the R factor (in the “thin” QR factorization for matrices with full column rank) is square, invertible, and triangular, and also Q^T Q = I. (There are also more technical reasons why QR factorization works well having to do with the conditioning of the linear systems, as mentioned in [Efficient way of doing linear regression - #33 by stevengj](https://discourse.julialang.org/t/efficient-way-of-doing-linear-regression/31232/33))

In particular, the least-square solution minimizing \Vert Ax - b\Vert corresponds (in theory) to solving the normal equations A^T A \hat{x} = A^T b. If you plug in A=QR, it simplifies (Q^T Q cancels, and R^T cancels because it is invertible), whereas for A = LU it does not (L^T L does not cancel, and U is not invertible).

Similar things happen for minimum-norm solutions (with “wide” matrices / underdetermined problems), though in that case one sometimes uses the LQ factorization instead of QR.

---

_[View the full topic](https://discourse.julialang.org/t/lu-factorization-and-non-square-matrices/108834)._
