From 5fddebd1bc9e0a82579abc9cd5d7183727e355fa Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Wed, 9 Apr 2025 12:31:46 +0200 Subject: [PATCH] Performance improvements --- LinearAlgebra/Matrix.cpp | 44 ++++++++++++++++++++++++++-------------- LinearAlgebra/Matrix.h | 1 + 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/LinearAlgebra/Matrix.cpp b/LinearAlgebra/Matrix.cpp index c18ce3a..fae5c6a 100644 --- a/LinearAlgebra/Matrix.cpp +++ b/LinearAlgebra/Matrix.cpp @@ -3,7 +3,7 @@ namespace LinearAlgebra { - #pragma region Matrix1 +#pragma region Matrix1 Matrix1::Matrix1(int size) : size(size) { if (this->size == 0) @@ -97,6 +97,11 @@ Matrix2 Matrix2::Zero(int nRows, int nCols) { return r; } +void Matrix2::Clear() { + for (int ix = 0; ix < this->nValues; ix++) + this->data[ix] = 0; +} + Matrix2 Matrix2::Identity(int size) { return Diagonal(1, size); } @@ -129,7 +134,8 @@ Matrix2 Matrix2::Transpose() const { for (uint rowIx = 0; rowIx < this->nRows; rowIx++) { for (uint colIx = 0; colIx < this->nCols; colIx++) - r.data[colIx * this->nCols + rowIx] = this->data[rowIx * this->nCols + colIx]; + r.data[colIx * this->nCols + rowIx] = + this->data[rowIx * this->nCols + colIx]; } return r; } @@ -177,10 +183,7 @@ Matrix2 LinearAlgebra::Matrix2::operator*(const Matrix2& B) const { return r; } -Matrix2 Matrix2::Slice(int rowStart, - int rowStop, - int colStart, - int colStop) { +Matrix2 Matrix2::Slice(int rowStart, int rowStop, int colStart, int colStop) { Matrix2 r = Matrix2(rowStop - rowStart, colStop - colStart); int resultRowIx = 0; @@ -194,14 +197,25 @@ Matrix2 Matrix2::Slice(int rowStart, } void Matrix2::UpdateSlice(int rowStart, - int rowStop, - int colStart, - int colStop, - const Matrix2& m) const { - for (int i = rowStart; i < rowStop; i++) { - for (int j = colStart; j < colStop; j++) - this->data[i * this->nCols + j] = - m.data[(i - rowStart) * m.nCols + (j - colStart)]; + int rowStop, + int colStart, + int colStop, + const Matrix2& m) const { + // for (int i = rowStart; i < rowStop; i++) { + // for (int j = colStart; j < colStop; j++) + // this->data[i * this->nCols + j] = + // m.data[(i - rowStart) * m.nCols + (j - colStart)]; + // } + + int rRowDataIx = rowStart * this->nCols; + int mRowDataIx = 0; + for (int rowIx = rowStart; rowIx < rowStop; rowIx++) { + rRowDataIx = rowIx * this->nCols; + // rRowDataIx += this->nCols; + mRowDataIx += m.nCols; + for (int colIx = colStart; colIx < colStop; colIx++) { + this->data[rRowDataIx + colIx] = m.data[mRowDataIx + (colIx - colStart)]; + } } } @@ -229,7 +243,7 @@ Matrix2 LinearAlgebra::Matrix2::Omega(const Vector3& v) { // Matrix2 #pragma endregion -} // namespace LinearAlgbra +} // namespace LinearAlgebra template <> MatrixOf::MatrixOf(unsigned int rows, unsigned int cols) { diff --git a/LinearAlgebra/Matrix.h b/LinearAlgebra/Matrix.h index 65d55d0..fc40055 100644 --- a/LinearAlgebra/Matrix.h +++ b/LinearAlgebra/Matrix.h @@ -39,6 +39,7 @@ class Matrix2 { Matrix2 Clone() const; static Matrix2 Zero(int nRows, int nCols); + void Clear(); static Matrix2 Identity(int size);