]> OCCT Git - occt.git/commitdiff
Foundation Classes - Matrix multiplied issue (#522)
authorPasukhin Dmitry <dpasukhi@opencascade.com>
Tue, 24 Jun 2025 11:38:31 +0000 (12:38 +0100)
committerdpasukhi <dpasukhi@opencascade.com>
Sat, 6 Sep 2025 19:56:50 +0000 (20:56 +0100)
Refactor multiplication to avoid self-correlation issues by using a temporary copy.

src/math/math_Matrix.cxx

index b2a61e1667742fc8f2766c1390805c8e0bfcd21f..b7fa5177ab0c424b7e0961248c03e68bcf4c87d3 100644 (file)
@@ -635,6 +635,14 @@ void math_Matrix::Multiply(const math_Matrix& Right)
     ColNumber() != Right.RowNumber(),
     "math_Matrix::Multiply() - input matrix has incompatible dimensions");
 
+  // Create a temporary copy to avoid corrupting our own data during calculation
+  math_Matrix aTemp = *this;
+  if (this == &Right)
+  {
+    Multiply(aTemp, aTemp);
+    return;
+  }
+
   Standard_Real Som;
   for (Standard_Integer I = LowerRowIndex; I <= UpperRowIndex; I++)
   {
@@ -644,7 +652,7 @@ void math_Matrix::Multiply(const math_Matrix& Right)
       Standard_Integer I2 = Right.LowerRowIndex;
       for (Standard_Integer J = LowerColIndex; J <= UpperColIndex; J++)
       {
-        Som = Som + Array(I, J) * Right.Array(I2, J2);
+        Som += aTemp.Array(I, J) * Right.Array(I2, J2);
         I2++;
       }
       Array(I, J2) = Som;