@@ -49,6 +49,7 @@ template <sofa::Size S> static void bindSquaredMat(py::class_<Mat<S, S, double>>
4949 p.def (" transpose" , (void (MatClass::*)()) & MatClass::transpose);
5050 p.def (" inverted" , [](MatClass &mat){ return mat.inverted (); });
5151 p.def (" invert" , [](MatClass &dest, MatClass &from){ return dest.invert (from); });
52+ p.def (" trace" , [](const MatClass& mat){ return sofa::type::trace (mat); });
5253}
5354
5455template <sofa::Size R, sofa::Size C>
@@ -150,8 +151,14 @@ static void addMat(py::module & /*m*/, py::class_<Mat<R, C, double>> &p) {
150151 p.def (py::self * double ());
151152 p.def (double () * py::self);
152153 p.def (py::self / double ());
153- p.def (py::self *= double ());
154- p.def (py::self /= double ());
154+ p.def (" __imul__" , [](MatClass& self, double value) -> MatClass& {
155+ self *= value;
156+ return self;
157+ }, py::return_value_policy::reference_internal);
158+ p.def (" __itruediv__" , [](MatClass& self, double value) -> MatClass& {
159+ self /= value;
160+ return self;
161+ }, py::return_value_policy::reference_internal);
155162
156163 p.def (py::self += py::self);
157164 p.def (py::self -= py::self);
@@ -163,6 +170,16 @@ static void addMat(py::module & /*m*/, py::class_<Mat<R, C, double>> &p) {
163170 p.def (" __repr__" , [](MatClass &self) { return pyMat::__str__ (self, true ); });
164171}
165172
173+ template <sofa::Size R_1 , sofa::Size C_1 , sofa::Size R_2 , sofa::Size C_2 >
174+ static void addMatProduct (py::module & /* m*/ , py::class_<Mat<R_1 , C_1 , double >> &p)
175+ {
176+ using LHS = Mat<R_1 , C_1 , double >;
177+ using RHS = Mat<R_2 , C_2 , double >;
178+ p.def (" __mul__" , [](const LHS &self, const RHS &m) {
179+ return self * m;
180+ });
181+ }
182+
166183// Generic bindings for Matrices
167184template <sofa::Size R, sofa::Size C> struct MATRIX {
168185 static void addMat (py::module &m) {
@@ -205,6 +222,8 @@ template <> struct MATRIX<1, 1> {
205222 return std::unique_ptr<MatClass>(mat);
206223 }));
207224 ::addMat (m, p);
225+
226+ addMatProduct<1 ,1 , 1 ,1 >(m, p);
208227 }
209228};
210229
@@ -234,6 +253,8 @@ template <> struct MATRIX<2, 2> {
234253 return std::unique_ptr<MatClass>(mat);
235254 }));
236255 ::addMat (m, p);
256+
257+ addMatProduct<2 ,2 , 2 ,2 >(m, p);
237258 }
238259};
239260
@@ -263,6 +284,8 @@ template <> struct MATRIX<3, 3> {
263284 return std::unique_ptr<MatClass>(mat);
264285 }));
265286 ::addMat (m, p);
287+
288+ addMatProduct<3 ,3 , 3 ,3 >(m, p);
266289 }
267290};
268291
@@ -292,6 +315,8 @@ template <> struct MATRIX<4, 4> {
292315 return std::unique_ptr<MatClass>(mat);
293316 }));
294317 ::addMat (m, p);
318+
319+ addMatProduct<4 ,4 , 4 ,4 >(m, p);
295320 }
296321};
297322
0 commit comments