Skip to content

Commit daed8f4

Browse files
yuvaltassacopybara-github
authored andcommitted
Don't assume compressed input in mju_transposeSparse, allow NULL pointer for matrix values, to only transpose the structure.
PiperOrigin-RevId: 712472432 Change-Id: I6dd34583ab181f0c9677c55230cecdf0aed46f9e
1 parent 94230a8 commit daed8f4

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/engine/engine_util_sparse.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -552,12 +552,13 @@ void mju_transposeSparse(mjtNum* res, const mjtNum* mat, int nr, int nc,
552552
// clear number of non-zeros for each row of transposed
553553
mju_zeroInt(res_rownnz, nc);
554554

555-
// total number of non-zeros of mat
556-
int nnz = rowadr[nr-1] + rownnz[nr-1];
557-
558555
// count the number of non-zeros for each row of the transposed matrix
559-
for (int i = 0; i < nnz; i++) {
560-
res_rownnz[colind[i]]++;
556+
for (int r = 0; r < nr; r++) {
557+
int start = rowadr[r];
558+
int end = start + rownnz[r];
559+
for (int j = start; j < end; j++) {
560+
res_rownnz[colind[j]]++;
561+
}
561562
}
562563

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

569-
// r holds the current row in mat
570-
int r = 0;
571-
572570
// iterate through each non-zero entry of mat
573-
for (int i = 0; i < nnz; i++) {
574-
// iterate to get to the current row (skipping rows with all zeros)
575-
while ((i-rowadr[r]) >= rownnz[r]) r++;
576-
577-
// swap rows with columns and increment res_rowadr
578-
int c = res_rowadr[colind[i]]++;
579-
res[c] = mat[i];
580-
res_colind[c] = r;
571+
for (int r = 0; r < nr; r++) {
572+
int start = rowadr[r];
573+
int end = start + rownnz[r];
574+
for (int i = start; i < end; i++) {
575+
// swap rows with columns and increment res_rowadr
576+
int c = res_rowadr[colind[i]]++;
577+
res_colind[c] = r;
578+
if (res) {
579+
res[c] = mat[i];
580+
}
581+
}
581582
}
582583

583584
// shift back row addresses

0 commit comments

Comments
 (0)