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 and , the number of columns in matrix , and the number of columns in matrix . This example sets to be the identity matrix the same size as the number of rows of .
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 such that the elements of are between and . Matrix 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 and that guarantee no overflow will occur in the transformation of in-place to and in-place to .
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 to 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 to 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 to and to . In this example, is the identity matrix, so is the economy-size orthogonal factor of the QR decomposition.
Q = C';
Verify That Q Is Orthogonal and R Is Upper-Triangular
is orthogonal, so 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
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.5208e-06
Suppress mlint
warnings.
%#ok<*NOPTS>