Add mju_clip, mju_fill, mju_eye and mju_symmetrize to python bindings.

- Also change signatures of `mju_fill` and `mju_symmetrize` to conform to standard.

PiperOrigin-RevId: 485613824
Change-Id: I8dd618bac92ef40aa6c76380fa73302e81968687
This commit is contained in:
Yuval Tassa
2022-11-02 09:21:38 -07:00
committed by Copybara-Service
parent 7458a6e90f
commit c83be2bc05
9 changed files with 85 additions and 24 deletions
+21
View File
@@ -1049,6 +1049,27 @@ Euler integrator, semi-implicit in velocity.
rank = mujoco.mju_boxQP(res, r, index, h, g, lower, upper)
self.assertGreater(rank, -1)
def test_mju_fill(self):
res = np.empty(3, np.float64)
mujoco.mju_fill(res, 1.5)
np.testing.assert_array_equal(res, np.full(3, 1.5))
def test_mju_eye(self):
eye4 = np.empty((4, 4), np.float64)
mujoco.mju_eye(eye4)
np.testing.assert_array_equal(eye4, np.eye(4))
def test_mju_symmetrize(self):
mat = np.linspace(0, 1, 16).reshape(4, 4)
res = np.empty((4, 4), np.float64)
mujoco.mju_symmetrize(res, mat)
np.testing.assert_array_equal(res, 0.5*(mat + mat.T))
def test_mju_clip(self):
self.assertEqual(mujoco.mju_clip(1.5, 1.0, 2.0), 1.5)
self.assertEqual(mujoco.mju_clip(1.5, 2.0, 3.0), 2.0)
self.assertEqual(mujoco.mju_clip(1.5, 0.0, 1.0), 1.0)
def test_mju_mul_vec_mat_vec(self):
vec1 = np.array([1., 2., 3.])
vec2 = np.array([3., 2., 1.])
+27
View File
@@ -632,6 +632,11 @@ PYBIND11_MODULE(_functions, pymodule) {
[](Eigen::Ref<EigenVectorX> res) {
return InterceptMjErrors(::mju_zero)(res.data(), res.size());
});
DEF_WITH_OMITTED_PY_ARGS(traits::mju_fill, "n")(
pymodule,
[](Eigen::Ref<EigenVectorX> res, mjtNum val) {
return InterceptMjErrors(::mju_fill)(res.data(), val, res.size());
});
DEF_WITH_OMITTED_PY_ARGS(traits::mju_copy, "n")(
pymodule,
[](Eigen::Ref<EigenVectorX> res,
@@ -817,6 +822,27 @@ PYBIND11_MODULE(_functions, pymodule) {
return InterceptMjErrors(::mju_transpose)(
res.data(), mat.data(), mat.rows(), mat.cols());
});
DEF_WITH_OMITTED_PY_ARGS(traits::mju_symmetrize, "n")(
pymodule,
[](Eigen::Ref<EigenArrayXX> res,
Eigen::Ref<const EigenArrayXX> mat) {
if (mat.cols() != mat.rows()) {
throw py::type_error("mat should be square");
}
if (res.cols() != mat.cols() || res.rows() != mat.rows()) {
throw py::type_error("res and mat should have the same shape");
}
return InterceptMjErrors(::mju_symmetrize)(
res.data(), mat.data(), mat.rows());
});
DEF_WITH_OMITTED_PY_ARGS(traits::mju_eye, "n")(
pymodule,
[](Eigen::Ref<EigenArrayXX> mat) {
if (mat.cols() != mat.rows()) {
throw py::type_error("mat should be square");
}
return InterceptMjErrors(::mju_eye)(mat.data(), mat.rows());
});
DEF_WITH_OMITTED_PY_ARGS(traits::mju_mulMatMat, "r1", "c1", "c2")(
pymodule,
[](Eigen::Ref<EigenArrayXX> res,
@@ -1030,6 +1056,7 @@ PYBIND11_MODULE(_functions, pymodule) {
Def<traits::mju_springDamper>(pymodule);
Def<traits::mju_min>(pymodule);
Def<traits::mju_max>(pymodule);
Def<traits::mju_clip>(pymodule);
Def<traits::mju_sign>(pymodule);
Def<traits::mju_round>(pymodule);
Def<traits::mju_type2Str>(pymodule);