How to implement the lu function for fixed-point data types?
이전 댓글 표시
Hi there,
I'd like to implement fixed point functionality for lu. Unfortunately, I don't see any support in Matlab because I'd have to convert the fixed-point data to floating-point before using the lu function.
However, converting the data back to floating-point for the computation would defeat the goal of using fixed-point data types.
Here's my implementation; any help would be greatly appreciated if I could convert to fixed point.
Below is my code
% test bench
n = 5;
num_tests = 100;
testInvertMatrix(n, num_tests);
function max_error = testInvertMatrix(n, num_tests)
% Initialize the maximum error
max_error = 0;
% Run the test cases
for i = 1:num_tests
% Generate a random matrix
A = rand(n);
% Compute the inverse of A using both fixed-point and floating-point
[A_inv_fixed, A_inv_float] = invertMatrix(A);
% I am observing following error
% Error using lu
% Invalid data type. First argument must be double or single.
% Compute the error
error = norm(double(A_inv_fixed) - A_inv_float, 'fro');
% Update the maximum error
max_error = max(max_error, error);
end
% Display the maximum error
fprintf('The maximum error over %d test cases is %e\n', num_tests, max_error);
end
function [A_inv_fixed, A_inv_float] = invertMatrix(A)
% Convert the matrix to fixed-point
A_fixed = fi(A, 1, 16, 15); % 1 sign bit, 16 total bits, 15 fraction bits
% Compute the LU decomposition of A for both fixed-point and floating-point
[L_fixed, U_fixed, P_fixed] = lu(A_fixed);
[L_float, U_float, P_float] = lu(A);
% Compute the inverse of A using the custom algorithm for both fixed-point and floating-point
A_inv_fixed = mldivide(U_fixed, mldivide(L_fixed, mldivide(P_fixed, eye(size(A_fixed)))));
A_inv_float = mldivide(U_float, mldivide(L_float, mldivide(P_float, eye(size(A)))));
end
댓글 수: 2
John D'Errico
2024년 3월 4일
Yes. I know you WANT to do this. BUT my gut just screams at how bad of an idea this is.
I am sure you can find pseudo-code for an LU online. You can even find a pretty simple LU scheme in MATLAB, that you could then hack to use fixed point data types.
The probem is, subtractive cancellation and division will kill you in fixed point, especially when you are looking at very small numbers of bits. 15 bits, plus a sign bit for 16 total bits?
Life is Wonderful
2024년 3월 5일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Fixed-Point Designer에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!