From 9cf1fb1d7c44ab856ba9d33e9d7d4915f81d542f Mon Sep 17 00:00:00 2001 From: Pasukhin Dmitry Date: Tue, 24 Jun 2025 12:38:31 +0100 Subject: [PATCH] Foundation Classes - Matrix multiplied issue (#522) Refactor multiplication to avoid self-correlation issues by using a temporary copy. --- src/math/math_Matrix.cxx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/math/math_Matrix.cxx b/src/math/math_Matrix.cxx index b2a61e1667..b7fa5177ab 100644 --- a/src/math/math_Matrix.cxx +++ b/src/math/math_Matrix.cxx @@ -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; -- 2.39.5