Matrix Calculator

🌐 English
A (3×3)
Rows: 3
Cols: 3
B (3×3)
Rows: 3
Cols: 3
Supports: fractions (1/2), decimals (0.5), constants (pi, e). Empty cells are treated as 0.

Calculation Results

1. What is QR decomposition?

The QR decomposition factors a matrix \(A\) into \(A=QR\) where

  • \(Q\) has orthonormal columns (for real matrices \(Q^{T}Q = I\)) — an orthogonal matrix when square;
  • \(R\) is upper triangular.

QR is widely used for solving least-squares problems, numerical algorithms and computing orthonormal bases.

2. Formula / Gram–Schmidt procedure (brief)

Given column vectors \(a_1,a_2,\dots,a_n\) of \(A\), classical Gram–Schmidt constructs orthonormal vectors \(q_1,\dots,q_n\):

  1. \(u_1 = a_1,\quad q_1 = u_1 / \|u_1\|\).
  2. For \(k\ge2\): \(u_k = a_k - \sum_{j=1}^{k-1} \operatorname{proj}_{q_j}(a_k)\), where \(\operatorname{proj}_{q_j}(a_k) = (q_j^{T}a_k)q_j.\)
  3. \(q_k = u_k / \|u_k\|\).

Finally \(Q=[q_1\ \cdots\ q_n]\) and \(R = Q^{T}A\). For numerical stability prefer Modified Gram–Schmidt or Householder reflections for production code.

3. Worked examples

Example 1

Find QR for

\[ A = \begin{bmatrix}1 & 1\\[4pt]1 & -1\end{bmatrix} \]

Step 1. \(u_1 = [1,1]^T,\ \|u_1\|=\sqrt{2},\ q_1=\frac{1}{\sqrt2}[1,1]^T.\)

Step 2. \(q_1^{T}a_2 = \frac{1}{\sqrt2}[1,1]\cdot[1,-1] = 0\Rightarrow u_2=a_2.\)

\(q_2=\frac{1}{\sqrt2}[1,-1]^T.\)

Result:

\[ Q=\frac{1}{\sqrt2}\begin{bmatrix}1 & 1\\[4pt]1 & -1\end{bmatrix},\quad R=\begin{bmatrix}\sqrt2 & 0\\[4pt]0 & \sqrt2\end{bmatrix}. \]

Example 2

Find QR for

\[ A = \begin{bmatrix}2 & 1\\[4pt]0 & 2\end{bmatrix} \]

\(u_1=[2,0]^T,\ \|u_1\|=2,\ q_1=[1,0]^T.\)

\(q_1^{T}a_2 = [1,0]\cdot[1,2]=1,\ u_2=a_2 - (q_1^{T}a_2)q_1 = [0,2]^T,\ q_2=[0,1]^T.\)

Result:

\[ Q=\begin{bmatrix}1 & 0\\[4pt]0 & 1\end{bmatrix},\quad R=\begin{bmatrix}2 & 1\\[4pt]0 & 2\end{bmatrix}. \]

4. Common mistakes

  • Not normalizing the orthogonal vectors — forgetting the \(\|u_k\|\) step.
  • Using classical Gram–Schmidt for ill-conditioned matrices — it can be numerically unstable.
  • Mixing up order: \(R = Q^{T}A\), not \(AQ^{T}\).
  • Assuming \(Q\) must be square — for tall matrices \(Q\) has orthonormal columns (m×n) and \(R\) is n×n.
  • Forgetting to handle linear dependence among columns (zero or near-zero \(u_k\)).

5. Practice problems (answers hidden)

Try these by hand or with your QR calculator. Click Show Answer to reveal one valid QR (answers may differ by signs or ordering of orthonormal vectors).

Exercise 1

\[ A=\begin{bmatrix}1 & 2\\[4pt]0 & 2\end{bmatrix} \]
Show Answer
\[ Q=\begin{bmatrix}1 & 0\\[4pt]0 & 1\end{bmatrix},\quad R=\begin{bmatrix}1 & 2\\[4pt]0 & 2\end{bmatrix} \]

(Here first column already unit; second column orthogonalized gives [0,1].)

Exercise 2

\[ A=\begin{bmatrix}3 & 1\\[4pt]4 & 2\end{bmatrix} \]
Show Answer
\[ Q=\begin{bmatrix} \tfrac{3}{5} & \tfrac{1}{5} \\[6pt] \tfrac{4}{5} & -\tfrac{4}{5} \end{bmatrix},\quad R=\begin{bmatrix} 5 & \tfrac{11}{5} \\[6pt] 0 & \tfrac{6}{5} \end{bmatrix} \]

(Signs or exact fractions may vary; \(R=Q^{T}A\) should hold.)

Exercise 3

\[ A=\begin{bmatrix}1 & 0\\[4pt]1 & 1\end{bmatrix} \]
Show Answer
\[ Q=\begin{bmatrix}\tfrac{1}{\sqrt2} & -\tfrac{1}{\sqrt2}\\[6pt] \tfrac{1}{\sqrt2} & \tfrac{1}{\sqrt2}\end{bmatrix},\quad R=\begin{bmatrix}\sqrt2 & \tfrac{1}{\sqrt2}\\[6pt]0 & \tfrac{1}{\sqrt2}\end{bmatrix} \]

(One valid orthonormal basis; equivalent alternatives differ by column signs.)

Exercise 4

\[ A=\begin{bmatrix}2 & 2\\[4pt]2 & -2\end{bmatrix} \]
Show Answer
\[ Q=\frac{1}{\sqrt2}\begin{bmatrix}1 & 1\\[6pt]1 & -1\end{bmatrix},\quad R=\begin{bmatrix}2\sqrt2 & 0\\[6pt]0 & 2\sqrt2\end{bmatrix} \]

(Columns scaled and orthogonalized; verify \(Q^{T}Q=I\) and \(R=Q^{T}A\).)


Copyright Notice: This article is original content from the Matrix Calculator website. Please credit the source when sharing or reproducing it. For more matrix computation tools, visit matrixcalcu.com.

Learn matrix multiplication more easily in just 2 minutes with this free game.

Matrix Multiplication Game