Main Content

This example shows how to use `fixed.qrFixedpointTypes`

to analytically determine fixed-point types for the computation of the QR decomposition.

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

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);
```

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: 28 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: 28 FractionLength: 24

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);

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';

$$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: 60 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: 28 FractionLength: 24

isequal(R,triu(R))

`ans = `*logical*
1

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.3415e-06

Suppress `mlint`

warnings.

`%#ok<*NOPTS>`