Add mju_mulMatTVecSparse to multiply transposed sparse matrix and dense vector.

PiperOrigin-RevId: 688535240
Change-Id: I0bacf1e911cde99014b2dcb0a1434e61a4f41d51
This commit is contained in:
Taylor Howell
2024-10-22 07:05:35 -07:00
committed by Copybara-Service
parent 2b0629d75f
commit 94e9912d28
3 changed files with 49 additions and 2 deletions
+22
View File
@@ -206,6 +206,28 @@ void mju_mulMatVecSparse(mjtNum* res, const mjtNum* mat, const mjtNum* vec,
// multiply transposed sparse matrix and dense vector: res = mat' * vec.
void mju_mulMatTVecSparse(mjtNum* res, const mjtNum* mat, const mjtNum* vec, int nr, int nc,
const int* rownnz, const int* rowadr, const int* colind) {
// clear res
mju_zero(res, nc);
for (int i=0; i < nr; i++) {
int nnz = rownnz[i];
int adr = rowadr[i];
const int* ind = colind + adr;
const mjtNum* row = mat + adr;
mjtNum scl = vec[i];
// add row scaled by the corresponding vector element
for (int j=0; j < nnz; j++) {
res[ind[j]] += row[j] * scl;
}
}
}
// res = res*scl1 + vec*scl2
static void mju_addToSclScl(mjtNum* res, const mjtNum* vec, mjtNum scl1, mjtNum scl2, int n) {
#ifdef mjUSEAVX