From 205dbeb877885f185da971e5f57254854e96cffa Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Thu, 3 Jul 2025 13:06:46 +0200 Subject: [PATCH] Cleanup --- LinearAlgebra/Matrix.cpp | 37 ++++++++++++++++++++++++++++++------- LinearAlgebra/Matrix.h | 33 +++++++++++++++++++-------------- 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/LinearAlgebra/Matrix.cpp b/LinearAlgebra/Matrix.cpp index 994b24d..5f21ac8 100644 --- a/LinearAlgebra/Matrix.cpp +++ b/LinearAlgebra/Matrix.cpp @@ -27,6 +27,18 @@ Matrix1::Matrix1(float* data, int size) : data(data), size(size) { this->externalData = true; } +int Matrix1::Size() const { + return this->size; +} + +Matrix1 Matrix1::FromVector3(Vector3 v) { + Matrix1 r(3); + r.data[0] = v.x; + r.data[1] = v.y; + r.data[2] = v.z; + return r; +} + Vector3 Matrix1::ToVector3() { return Vector3(this->data[0], this->data[1], this->data[2]); } @@ -86,7 +98,7 @@ Matrix2::Matrix2() {} Matrix2::Matrix2(int nRows, int nCols) : nRows(nRows), nCols(nCols) { this->nValues = nRows * nCols; - //printf("Allocate %d\n", this->nValues); + // printf("Allocate %d\n", this->nValues); if (this->nValues <= 0) { this->data = nullptr; @@ -156,7 +168,7 @@ Matrix2& Matrix2::operator=(const Matrix2& m) { Matrix2::~Matrix2() { if (!this->externalData) { - //printf("Deallocate %d\n", this->nValues); + // printf("Deallocate %d\n", this->nValues); delete[] data; } } @@ -174,7 +186,8 @@ Matrix2 Matrix2::Clone() const { // nCols(other.nCols), // nValues(other.nValues), // data(other.data) { -// other.data = nullptr; // Set the other object's pointer to nullptr to avoid +// other.data = nullptr; // Set the other object's pointer to nullptr to +// avoid // // double deletion // } @@ -275,7 +288,17 @@ Matrix2 Matrix2::operator-(const Matrix2& v) const { return r; } -Matrix2 LinearAlgebra::Matrix2::operator*(const Matrix2& B) const { +Matrix1 operator*(const Matrix2& m, const Matrix1& v) { + Matrix1 r = Matrix1(m.nRows); + for (int rowIx = 0; rowIx < m.nRows; rowIx++) { + int mRowIx = rowIx * m.nCols; + for (int colIx = 0; colIx < m.nCols; colIx++) + r(rowIx) += m.data[mRowIx + colIx] * v(rowIx); + } + return r; +} + +Matrix2 Matrix2::operator*(const Matrix2& B) const { Matrix2 r = Matrix2(this->nRows, B.nCols); int ACols = this->nCols; @@ -306,7 +329,7 @@ Matrix2 LinearAlgebra::Matrix2::operator*(const Matrix2& B) const { Matrix2 Matrix2::GetRows(int from, int to) { if (from < 0 || to >= this->nRows) - std::cerr << "Slice index out of range." << "std::endl"; + std::cerr << "Slice index out of range." << std::endl; Matrix2 result = Matrix2(to - from, this->nCols); int resultRowIx = 0; @@ -330,8 +353,8 @@ Vector3 Matrix2::GetRow3(int rowIx) { void Matrix2::SetRow(int rowIx, Matrix1 source) { int cellIx = rowIx * this->nCols; - for (int ix = 0; ix < source.size; ix++) - this->data[cellIx + ix] = source.data[ix]; + for (int ix = 0; ix < source.Size(); ix++) + this->data[cellIx + ix] = source(ix); } void Matrix2::SetRow3(int rowIx, Vector3 v) { diff --git a/LinearAlgebra/Matrix.h b/LinearAlgebra/Matrix.h index 8959820..b03f32e 100644 --- a/LinearAlgebra/Matrix.h +++ b/LinearAlgebra/Matrix.h @@ -9,13 +9,13 @@ namespace LinearAlgebra { /// @brief A 1-dimensional matrix or vector of arbitrary size class Matrix1 { public: - float* data = nullptr; - int size = 0; - Matrix1(); Matrix1(int size); Matrix1(float* data, int size); + int Size() const; + + static Matrix1 FromVector3(Vector3 v); Vector3 ToVector3(); static Matrix1 FromQuaternion(Quaternion q); @@ -45,6 +45,10 @@ class Matrix1 { this->data[ix] = source.data[sourceIx]; } + protected: + float* data = nullptr; + int size = 0; + private: bool externalData = true; }; @@ -105,15 +109,16 @@ class Matrix2 { return r; } - friend Matrix1 operator*(const Matrix2& m, const Matrix1& v) { - Matrix1 r = Matrix1(m.nRows); - for (int rowIx = 0; rowIx < m.nRows; rowIx++) { - int mRowIx = rowIx * m.nCols; - for (int colIx = 0; colIx < m.nCols; colIx++) - r.data[rowIx] += m.data[mRowIx + colIx] * v.data[rowIx]; - } - return r; - } + friend Matrix1 operator*(const Matrix2& m, const Matrix1& v); + // { + // Matrix1 r = Matrix1(m.nRows); + // for (int rowIx = 0; rowIx < m.nRows; rowIx++) { + // int mRowIx = rowIx * m.nCols; + // for (int colIx = 0; colIx < m.nCols; colIx++) + // r.data[rowIx] += m.data[mRowIx + colIx] * v.data[rowIx]; + // } + // return r; + // } friend Matrix2 operator/(const Matrix2& m, float f) { Matrix2 r = Matrix2(m.nRows, m.nCols); @@ -151,8 +156,8 @@ class Matrix2 { // We don't want these because they make impliciti copies which is inefficient // move constructor and move assignment operator - //Matrix2(Matrix2&& other) noexcept; - //Matrix2& operator=(Matrix2&& other) noexcept; + // Matrix2(Matrix2&& other) noexcept; + // Matrix2& operator=(Matrix2&& other) noexcept; static Matrix2 Omega(const Vector3& v);