Create a codegen implementation of chol function where the size of the matrix is not hard-coded.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I want a C implementation for chol() function. Using MALTAB code gen app I am able to get the C implementation but in this implementation the size of the matrix is hardcoded. How can I get a C implementation in which the size of the matrix is not hard-coded and can be decided during runtime.
The script I am using to call cholesky goes like this:
rows = input('Enter the number of rows for the matrix: ');
cols = input('Enter the number of columns for the matrix: ');
% Create a random matrix of size n
A = rand(rows, cols);
A = A'*A;
%Using the cholesky function
outputMat = cholesky(A);
I have tried this approach. Will this work? Is this even possible? If yes, then how do I go about this?
0 Kommentare
Antworten (1)
Denis Gurchenkov
am 18 Sep. 2023
Yes this can be done. When you use MATLAB Coder ('codegen' command), you need to decide what are the input arrays that you feed to the entry-point function. Specifically, do you want those arrays to be fixed in size (this is the default behavior), do you want them to be variable-sized but bounded by some maximum size (say, any size up to 20x20), or variable-sized unbounded.
Depending on that decision, the C code would look different, and it would use different types to represent those arrays.
Here is a small example that shows how to generate C code for chol, with all those 3 possible input types.
First, here is the "entry point" function, mychol.m:
%#codegen
function out = mychol(a)
out = chol(a);
end
Here is the doit script that calls codegen command 3 times, with different inputs:
% a is an unbounded variable-sized 2-d matrix of doubles
% b is a variable=sized 2-d matrix bounded by 20x20
% c is a fixed-sized 2-d matrix
a = coder.typeof(1, [Inf Inf], [true true]);
b = coder.typeof(1, [20 20], [true true]);
c = coder.typeof(1, [20 20], [false false]);
codegen mychol -args {a} -d mychol_unbounded -config:lib -report
codegen mychol -args {b} -d mychol_bounded -config:lib -report
codegen mychol -args {c} -d mychol_fixsize -config:lib -report
After your run it, look at ./mychol_bounded/mychol.c, ./mychol_unbounded/mychol.c, ./mychol_fixsize/mychol.c and compare the inputs to main function. If you want to see an example on how to call the main function from your own C code (how to create those emxarray arrays), look at the "examples/main.c" in each of the folders.
Here is a link to some documentation you can use for further details: https://www.mathworks.com/help/coder/ug/what-is-variable-size-data.html
0 Kommentare
Siehe auch
Kategorien
Mehr zu MATLAB Coder finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!