# Determine Fixed-Point Types for QR Decomposition

This example shows how to use `fixed.qrFixedpointTypes` to analytically determine fixed-point types for the computation of the QR decomposition.

### Define Matrix Dimensions

Specify the number of rows in matrices $A$ and $B$, the number of columns in matrix $A$, and the number of columns in matrix $B$. This example sets $B$ to be the identity matrix the same size as the number of rows of $A$.

```m = 10; % Number of rows in matrices A and B n = 3; % Number of columns in matrix A```

### Generate Matrices A and B

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

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

### Select Fixed-Point Types

Use `fixed.qrFixedpointTypes` to select fixed-point data types for matrices $A$ and $B$ that guarantee no overflow will occur in the transformation of $A$ in-place to $R={Q}^{\prime }A$ and $B$ in-place to $C={Q}^{\prime }B$.

```max_abs_A = 1; % Upper bound on max(abs(A(:)) max_abs_B = 1; % Upper bound on max(abs(B(:)) precisionBits = 24; % Number of bits of precision T = fixed.qrFixedpointTypes(m,max_abs_A,max_abs_B,precisionBits)```
```T = struct with fields: A: [0x0 embedded.fi] B: [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 ```

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

`T.B`
```ans = [] DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 29 FractionLength: 24 ```

### Use the Specified Types to Compute the QR Decomposition

Cast the inputs to the types determined by `fixed.qrFixedpointTypes``.`

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

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

`fiaccel fixed.qrAB -args {A,B} -o qrAB_mex`

Compute the QR decomposition.

`[C,R] = qrAB_mex(A,B);`

### Extract the Economy-Size Q

The function `fixed.qrAB` transforms $\mathit{A}$ to $\mathit{R}={\mathit{Q}}^{\prime }\mathit{A}$ and $B$ to $C={Q}^{\prime }B$. In this example, $B$ is the identity matrix, so $Q={C}^{\prime }$ is the economy-size orthogonal factor of the QR decomposition.

`Q = C';`

### Verify that Q is Orthogonal and R is Upper-Triangular

$Q$ is orthogonal, so ${Q}^{\prime }Q$ is the identity matrix within rounding error.

`I = Q'*Q`
```I = 1.0000 -0.0000 -0.0000 -0.0000 1.0000 -0.0000 -0.0000 -0.0000 1.0000 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 62 FractionLength: 48 ```

$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.qrAB` function, compute the relative error.

`relative_error = norm(double(Q*R - A))/norm(double(A))`
```relative_error = 1.5886e-06 ```

Suppress `mlint` warnings.

`%#ok<*NOPTS>`