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
where is the length of the vector .
Explicit calculation of θ is rarely necessary or desirable. Instead we directly seek c and s. An obvious solution would be
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.
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)ifb==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;elset = a/b;u=sign(b)*sqrt(1+t*t);s=1/u;c=s*t;r=b*u;end;
The IEEE 754copysign(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.
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:
We have the following matrix multiplication:
Plugging in these values for c and s and performing the matrix multiplication above yields A2:
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:
We are presented with the following matrix multiplication:
Plugging in these values for c and s and performing the multiplications gives us A3:
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:
Performing this matrix multiplication yields:
This completes two iterations of the Givens Rotation and calculating the QR decomposition can now be done.
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 Givens rotations bivectors are:
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 transforms the basis of the space into a frame with angles roll, pitch and yaw in the Tait–Bryan conventionz-x-y (convention in which the line of nodes is perpendicular to z and Y axes, also named Y-X′-Z″).
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.
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.
^The rotation matrix immediately below is not a Givens rotation. The matrix immediately below respects the right-hand rule and is this usual matrix one sees in Computer Graphics; however, a Givens rotation is simply a matrix as defined in the Matrix representation section above and does not necessarily respect the right-hand rule. The below matrix is actually the Givens rotation through an angle of -.