Skip to content

Commit

Permalink
Don't assume compressed input in mju_transposeSparse, allow NULL po…
Browse files Browse the repository at this point in the history
…inter for matrix values, to only transpose the structure.

PiperOrigin-RevId: 712472432
Change-Id: I6dd34583ab181f0c9677c55230cecdf0aed46f9e
  • Loading branch information
yuvaltassa authored and copybara-github committed Jan 6, 2025
1 parent 94230a8 commit daed8f4
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/engine/engine_util_sparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,13 @@ void mju_transposeSparse(mjtNum* res, const mjtNum* mat, int nr, int nc,
// clear number of non-zeros for each row of transposed
mju_zeroInt(res_rownnz, nc);

// total number of non-zeros of mat
int nnz = rowadr[nr-1] + rownnz[nr-1];

// count the number of non-zeros for each row of the transposed matrix
for (int i = 0; i < nnz; i++) {
res_rownnz[colind[i]]++;
for (int r = 0; r < nr; r++) {
int start = rowadr[r];
int end = start + rownnz[r];
for (int j = start; j < end; j++) {
res_rownnz[colind[j]]++;
}
}

// compute the row addresses for the transposed matrix
Expand All @@ -566,18 +567,18 @@ void mju_transposeSparse(mjtNum* res, const mjtNum* mat, int nr, int nc,
res_rowadr[i] = res_rowadr[i-1] + res_rownnz[i-1];
}

// r holds the current row in mat
int r = 0;

// iterate through each non-zero entry of mat
for (int i = 0; i < nnz; i++) {
// iterate to get to the current row (skipping rows with all zeros)
while ((i-rowadr[r]) >= rownnz[r]) r++;

// swap rows with columns and increment res_rowadr
int c = res_rowadr[colind[i]]++;
res[c] = mat[i];
res_colind[c] = r;
for (int r = 0; r < nr; r++) {
int start = rowadr[r];
int end = start + rownnz[r];
for (int i = start; i < end; i++) {
// swap rows with columns and increment res_rowadr
int c = res_rowadr[colind[i]]++;
res_colind[c] = r;
if (res) {
res[c] = mat[i];
}
}
}

// shift back row addresses
Expand Down

0 comments on commit daed8f4

Please sign in to comment.