# Determine Fixed-Point Types for Q-less QR Decomposition

This example shows how to use fixed.qlessqrFixedpointTypes to analytically determine a fixed-point type for the computation of the Q-less QR decomposition.

### Define Matrix Dimensions

Specify the number of rows and columns in matrix $A$.

m = 10; % Number of rows in matrix A
n = 3;  % Number of columns in matrix A

### Generate Matrix A

Use the helper function realUniformRandomArray to generate a random matrix $A$ such that the elements of $A$ are between $-1$ and $+1$.

rng('default')
A = fixed.example.realUniformRandomArray(-1,1,m,n);

### Select Fixed-Point Type

Use the fixed.qlessqrFixedpointTypes function to select the fixed-point data type for matrix $A$ that guarantees no overflow will occur in the transformation of $A$ in-place to $R={Q}^{\prime }A$.

max_abs_A = 1;  % Upper bound on max(abs(A(:))
precisionBits = 24;  % Number of bits of precision
T = fixed.qlessqrFixedpointTypes(m,max_abs_A,precisionBits)
T = struct with fields:
A: [0x0 embedded.fi]

T.A is the type computed for transforming $\mathit{A}$ to $\mathit{R}={\mathit{Q}}^{\prime }\mathit{A}$ in-place so that it does not overflow.

T.A
ans =

[]

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 29
FractionLength: 24

### Use the Specified Type to Compute the Q-less QR Decomposition

Cast the input to the type determined by fixed.qlessqrFixedpointTypes.

A = cast(A,'like',T.A);

Accelerate fixed.qlessQR by using fiaccel to generate a MATLAB executable (MEX) function.

fiaccel fixed.qlessQR -args {A} -o qlessQR_mex

Compute the QR decomposition.

R = qlessQR_mex(A);

### Verify that R is Upper-Triangular

$R$ is an upper-triangular matrix.

R
R =
2.2180    0.8559   -0.5607
0    2.0578   -0.4017
0         0    1.7117

DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 29
FractionLength: 24
isequal(R,triu(R))
ans = logical
1

### Verify the Accuracy of the Output

To evaluate the accuracy of the fixed.qlessQR function, compute the relative error.

$\mathit{R}={\mathit{Q}}^{\prime }\mathit{A}$, and $Q$ is orthogonal, so ${R}^{\prime }R={A}^{\prime }Q{Q}^{\prime }A={A}^{\prime }A$, within rounding error.

relative_error = norm(double(R'*R - A'*A))/norm(double(A'*A))
relative_error = 9.3865e-07

Suppress mlint warnings.

%#ok<*NOPTS>