Hi, I’m developing a 2D matrix multiplication extension that uses regular 2D arrays. To provide the final result matrix as another 2D array, I came up with the following strategy to create the 2D result array object in C and populate it with the custom values in vect[]
:
object *fn_makemat (object *args, object *env) {
(void) env;
int bit;
int rows, cols;
rows = 4;
cols = 4;
// Make array object
object *resdims = cons(number(rows), cons(number(cols), NULL));
object *resarray = makearray(resdims, number(0), false);
// Populate array with values
int vect[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
int index = 0;
for(int i = 0; i < rows; ++i){
for(int j = 0; j < cols; ++j) {
object *rowcol = cons(number(i), cons(number(j), NULL));
object *loc = *getarray(resarray, rowcol, 0, &bit);
loc->integer = vect[index];
++index;
}
}
return resarray;
}
But when I call the function from the uLisp REPL, I get the following array:
#2A((16 16 16 16) (16 16 16 16) (16 16 16 16) (16 16 16 16))
And the desired result is:
#2A((1 2 3 4) (5 6 7 8) (9 10 11 12) (13 14 15 16))
Apparently in every iteration the current value from the C vector: vect[index]
is being assigned to all elements in the array.
Any tips on what I could be doing wrong? Thanks in advace!