# Givens rotation

In numerical linear algebra, a Givens rotation is a rotation in the plane spanned by two coordinates axes. Givens rotations are named after Wallace Givens, who introduced them to numerical analysts in the 1950s while he was working at Argonne National Laboratory.

## Matrix representation

A Givens rotation is represented by a matrix of the form

$G(i,j,\theta )={\begin{bmatrix}1&\cdots &0&\cdots &0&\cdots &0\\\vdots &\ddots &\vdots &&\vdots &&\vdots \\0&\cdots &c&\cdots &-s&\cdots &0\\\vdots &&\vdots &\ddots &\vdots &&\vdots \\0&\cdots &s&\cdots &c&\cdots &0\\\vdots &&\vdots &&\vdots &\ddots &\vdots \\0&\cdots &0&\cdots &0&\cdots &1\end{bmatrix}},$ where c = cos θ and s = sin θ appear at the intersections ith and jth rows and columns. That is, for fixed i > j, the non-zero elements of Givens matrix are given by:

{\begin{aligned}g_{kk}&{}=1\qquad {\text{for}}\ k\neq i,\,j\\g_{kk}&{}=c\qquad {\text{for}}\ k=i,\,j\\g_{ji}&{}=-g_{ij}=-s\\\end{aligned}} The product G(i, j, θ)x represents a counterclockwise rotation of the vector x in the (i, j) plane of θ radians, hence the name Givens rotation.

The main use of Givens rotations in numerical linear algebra is to introduce zeros[clarification needed] in vectors or matrices. This effect can, for example, be employed for computing the QR decomposition of a matrix. One advantage over Householder transformations is that they can easily be parallelised, and another is that often for very sparse matrices they have a lower operation count.

## Stable calculation

When a Givens rotation matrix, G(i, j, θ), multiplies another matrix, A, from the left, G A, only rows i and j of A are affected. Thus we restrict attention to the following counterclockwise problem. Given a and b, find c = cos θ and s = sin θ such that

${\begin{bmatrix}c&-s\\s&c\end{bmatrix}}{\begin{bmatrix}a\\b\end{bmatrix}}={\begin{bmatrix}r\\0\end{bmatrix}},$ where $r={\sqrt {a^{2}+b^{2}}}$ is the length of the vector $(a,b)$ . Explicit calculation of θ is rarely necessary or desirable. Instead we directly seek c and s. An obvious solution would be

{\begin{aligned}c&{}\leftarrow a/r\\s&{}\leftarrow -b/r.\end{aligned}} However, the computation for r may overflow or underflow. An alternative formulation avoiding this problem (Golub & Van Loan 1996, §5.1.8) is implemented as the hypot function in many programming languages.

The following Fortran code is a minimalistic implementation of Givens rotation for real numbers. If the input values 'a' or 'b' are frequently zero, the code may be optimized to handle these cases as presented here.

subroutine givens_rotation(a, b, c, s, r)

real a, b, c, s, r
real h, d

if (b.ne.0.0) then
h = hypot(a, b)
d = 1.0 / h
c = abs(a) * d
s = sign(d, a) * b
r = sign(1.0, a) * h
else
c = 1.0
s = 0.0
r = a
end if

return
end


Furthermore, as Edward Anderson discovered in improving LAPACK, a previously overlooked numerical consideration is continuity. To achieve this, we require r to be positive. The following MATLAB/GNU Octave code illustrates the algorithm.

function [c, s, r] = givens_rotation(a, b)
if b == 0;
c = sign(a);
if (c == 0);
c = 1.0; % Unlike other languages, MatLab's sign function returns 0 on input 0.
end;
s = 0;
r = abs(a);
elseif a == 0;
c = 0;
s = sign(b);
r = abs(b);
elseif abs(a) > abs(b);
t = b / a;
u = sign(a) * sqrt(1 + t * t);
c = 1 / u;
s = c * t;
r = a * u;
else
t = a / b;
u = sign(b) * sqrt(1 + t * t);
s = 1 / u;
c = s * t;
r = b * u;
end
end


The IEEE 754 copysign(x,y) function, provides a safe and cheap way to copy the sign of y to x. If that is not available, |x|⋅sgn(y), using the abs and sgn functions, is an alternative as done above.

## Triangularization

Given the following 3×3 Matrix:

$A_{1}={\begin{bmatrix}6&5&0\\5&1&4\\0&4&3\\\end{bmatrix}},$ perform two iterations of the Givens rotation (note that the Givens rotation algorithm used here differs slightly from above) to yield an upper triangular matrix in order to compute the QR decomposition.

In order to form the desired matrix, we must zero elements (2,1) and (3,2). We first select element (2,1) to zero. Using a rotation matrix of:

$G_{1}={\begin{bmatrix}c&-s&0\\s&c&0\\0&0&1\\\end{bmatrix}}.$ We have the following matrix multiplication:

{\begin{aligned}G_{1}A_{1}&{}=A_{2}\\&{}={\begin{bmatrix}c&-s&0\\s&c&0\\0&0&1\\\end{bmatrix}}{\begin{bmatrix}6&5&0\\5&1&4\\0&4&3\\\end{bmatrix}},\end{aligned}} where

{\begin{aligned}r&{}={\sqrt {6^{2}+5^{2}}}\approx 7.8102\\c&{}=6/r\approx 0.7682\\s&{}=-5/r\approx -0.6402.\end{aligned}} Plugging in these values for c and s and performing the matrix multiplication above yields A2:

$A_{2}\approx {\begin{bmatrix}7.8102&4.4813&2.5607\\0&-2.4327&3.0729\\0&4&3\\\end{bmatrix}}$ We now want to zero element (3,2) to finish off the process. Using the same idea as before, we have a rotation matrix of:

$G_{2}={\begin{bmatrix}1&0&0\\0&c&-s\\0&s&c\\\end{bmatrix}}$ We are presented with the following matrix multiplication:

{\begin{aligned}G_{2}A_{2}&{}=A_{3}\\&{}\approx {\begin{bmatrix}1&0&0\\0&c&-s\\0&s&c\\\end{bmatrix}}{\begin{bmatrix}7.8102&4.4813&2.5607\\0&-2.4327&3.0729\\0&4&3\\\end{bmatrix}},\end{aligned}} where

{\begin{aligned}r&{}\approx {\sqrt {(-2.4327)^{2}+4^{2}}}\approx 4.6817\\c&{}\approx -2.4327/r\approx -0.5196\\s&{}\approx -4/r\approx -0.8544.\end{aligned}} Plugging in these values for c and s and performing the multiplications gives us A3:

$A_{3}\approx {\begin{bmatrix}7.8102&4.4813&2.5607\\0&4.6817&0.9664\\0&0&-4.1843\\\end{bmatrix}}.$ This new matrix A3 is the upper triangular matrix needed to perform an iteration of the QR decomposition. Q is now formed using the transpose of the rotation matrices in the following manner:

$Q=G_{1}^{T}\,G_{2}^{T}.$ Performing this matrix multiplication yields:

$Q\approx {\begin{bmatrix}0.7682&0.3327&0.5470\\0.6402&-0.3992&-0.6564\\0&0.8544&-0.5196\\\end{bmatrix}}.$ This completes two iterations of the Givens Rotation and calculating the QR decomposition can now be done.

## Givens rotations in Clifford algebras

In Clifford algebras and its child structures like geometric algebra rotations are represented by bivectors. Givens rotations are represented by the exterior product of the basis vectors. Given any pair of basis vectors $\mathbf {e} _{i},\mathbf {e} _{j}$ Givens rotations bivectors are:

$B_{ij}=\mathbf {e} _{i}\wedge \mathbf {e} _{j}.$ Their action on any vector is written:

$v=e^{-(\theta /2)(\mathbf {e} _{i}\wedge \mathbf {e} _{j})}ue^{(\theta /2)(\mathbf {e} _{i}\wedge \mathbf {e} _{j})},$ where

$e^{(\theta /2)(\mathbf {e} _{i}\wedge \mathbf {e} _{j})}=\cos(\theta /2)+\sin(\theta /2)\mathbf {e} _{i}\wedge \mathbf {e} _{j}.$ ## Dimension 3

There are three Givens rotations in dimension 3:

$R_{X}(\theta )={\begin{bmatrix}1&0&0\\0&\cos \theta &-\sin \theta \\0&\sin \theta &\cos \theta \end{bmatrix}}.$ {\begin{aligned}\\R_{Y}(\theta )={\begin{bmatrix}\cos \theta &0&-\sin \theta \\0&1&0\\\sin \theta &0&\cos \theta \end{bmatrix}}\end{aligned}} [note 1]
{\begin{aligned}\\R_{Z}(\theta )={\begin{bmatrix}\cos \theta &-\sin \theta &0\\\sin \theta &\cos \theta &0\\0&0&1\end{bmatrix}}\end{aligned}} Given that they are endomorphisms they can be composed with each other as many times as desired, keeping in mind that g ∘ ff ∘ g.

These three Givens rotations composed can generate any rotation matrix according to Davenport's chained rotation theorem. This means that they can transform the standard basis of the space to any other frame in the space.[clarification needed]

When rotations are performed in the right order, the values of the rotation angles of the final frame will be equal to the Euler angles of the final frame in the corresponding convention. For example, an operator $R=R_{Y}(\theta _{3}).R_{X}(\theta _{2}).R_{Z}(\theta _{1})$ transforms the basis of the space into a frame with angles roll, pitch and yaw $YPR=(\theta _{3},\theta _{2},\theta _{1})$ in the Tait–Bryan convention z-x-y (convention in which the line of nodes is perpendicular to z and Y axes, also named Y-X′-Z″).

For the same reason, any rotation matrix in 3D can be decomposed in a product of three of these rotation operators.

The meaning of the composition of two Givens rotations g ∘ f is an operator that transforms vectors first by f and then by g, being f and g rotations about one axis of basis of the space. This is similar to the extrinsic rotation equivalence for Euler angles.

### Table of composed rotations

The following table shows the three Givens rotations equivalent to the different Euler angles conventions using extrinsic composition (composition of rotations about the basis axes) of active rotations and the right-handed rule for the positive sign of the angles.

The notation has been simplified in such a way that c1 means cos θ1 and s2 means sin θ2). The subindexes of the angles are the order in which they are applied using extrinsic composition (1 for intrinsic rotation, 2 for nutation, 3 for precession)

As rotations are applied just in the opposite order of the Euler angles table of rotations, this table is the same but swapping indexes 1 and 3 in the angles associated with the corresponding entry. An entry like zxy means to apply first the y rotation, then x, and finally z, in the basis axes.

All the compositions assume the right hand convention for the matrices that are multiplied, yielding the following results.

xzx xzy ${\begin{bmatrix}c_{2}&-c_{1}s_{2}&s_{1}s_{2}\\c_{3}s_{2}&c_{3}c_{2}c_{1}-s_{3}s_{1}&-c_{2}c_{3}s_{1}-c_{1}s_{3}\\s_{2}s_{3}&c_{3}s_{1}+c_{1}c_{2}s_{3}&c_{3}c_{1}-c_{2}s_{3}s_{1}\end{bmatrix}}$ ${\begin{bmatrix}c_{2}c_{3}&-c_{3}s_{2}c_{1}+s_{3}s_{1}&c_{3}s_{2}s_{1}+s_{3}c_{1}\\s_{2}&c_{1}c_{2}&-c_{2}s_{1}\\-s_{3}c_{2}&s_{3}s_{2}c_{1}+c_{3}s_{1}&-s_{3}s_{2}s_{1}+c_{3}c_{1}\end{bmatrix}}$ ${\begin{bmatrix}c_{2}&s_{1}s_{2}&c_{1}s_{2}\\s_{2}s_{3}&c_{3}c_{1}-c_{2}s_{3}s_{1}&-c_{3}s_{1}-c_{1}c_{2}s_{3}\\-c_{3}s_{2}&c_{3}c_{2}s_{1}+c_{1}s_{3}&c_{3}c_{2}c_{1}-s_{3}s_{1}\end{bmatrix}}$ ${\begin{bmatrix}c_{3}c_{2}&-s_{3}c_{1}+c_{3}s_{2}s_{1}&s_{3}s_{1}+c_{3}s_{2}c_{1}\\s_{3}c_{2}&c_{3}c_{1}+s_{3}s_{2}s_{1}&-c_{3}s_{1}+s_{3}s_{2}c_{1}\\-s_{2}&c_{2}s_{1}&c_{2}c_{1}\end{bmatrix}}$ ${\begin{bmatrix}c_{3}c_{1}-c_{2}s_{3}s_{1}&s_{2}s_{3}&c_{3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