Add mju_fill, mju_symmetrize and mju_eye utility functions.

PiperOrigin-RevId: 484226916
Change-Id: I79593d54e903ddacab0f859bfc6d17eccad7d3a1
This commit is contained in:
Yuval Tassa
2022-10-27 05:27:46 -07:00
committed by Copybara-Service
parent 507b573763
commit f905c7fb7f
8 changed files with 202 additions and 6 deletions
+35 -2
View File
@@ -16,7 +16,7 @@
#include <string.h>
#include <mujoco/mjmodel.h>
#include <mujoco/mjtnum.h>
#ifdef mjUSEPLATFORMSIMD
#if defined(__AVX__) && defined(mjUSEDOUBLE)
@@ -244,6 +244,15 @@ void mju_zero(mjtNum* res, int n) {
// res = val
void mju_fill(mjtNum* res, int n, mjtNum val) {
for (int i=0; i<n; i++) {
res[i] = val;
}
}
// res = vec
void mju_copy(mjtNum* res, const mjtNum* vec, int n) {
if (n>0) {
@@ -718,7 +727,7 @@ mjtNum mju_mulVecMatVec(const mjtNum* vec1, const mjtNum* mat, const mjtNum* vec
//------------------------------ matrix-matrix operations ------------------------------------------
//------------------------------ matrix operations -------------------------------------------------
// transpose matrix
void mju_transpose(mjtNum* res, const mjtNum* mat, int nr, int nc) {
@@ -731,6 +740,30 @@ void mju_transpose(mjtNum* res, const mjtNum* mat, int nr, int nc) {
// symmetrize square matrix M = (M + M')/2
void mju_symmetrize(mjtNum* mat, int n) {
for (int i=0; i<n; i++) {
for (int j=0; j<i; j++) {
mjtNum tmp = 0.5 * (mat[i*n+j] + mat[j*n+i]);
mat[i*n+j] = mat[j*n+i] = tmp;
}
}
}
// identity matrix
void mju_eye(mjtNum* mat, int n) {
mju_zero(mat, n*n);
for (int i=0; i<n; i++) {
mat[i*(n + 1)] = 1;
}
}
//------------------------------ matrix-matrix operations ------------------------------------------
// multiply matrices, exploit sparsity of mat1
void mju_mulMatMat(mjtNum* res, const mjtNum* mat1, const mjtNum* mat2,
int r1, int c1, int c2) {