This commit is contained in:
Pascal Serrarens 2025-07-03 13:06:46 +02:00
parent c3fa3d5957
commit 205dbeb877
2 changed files with 49 additions and 21 deletions

View File

@ -27,6 +27,18 @@ Matrix1::Matrix1(float* data, int size) : data(data), size(size) {
this->externalData = true; 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() { Vector3 Matrix1::ToVector3() {
return Vector3(this->data[0], this->data[1], this->data[2]); 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) { Matrix2::Matrix2(int nRows, int nCols) : nRows(nRows), nCols(nCols) {
this->nValues = nRows * nCols; this->nValues = nRows * nCols;
//printf("Allocate %d\n", this->nValues); // printf("Allocate %d\n", this->nValues);
if (this->nValues <= 0) { if (this->nValues <= 0) {
this->data = nullptr; this->data = nullptr;
@ -156,7 +168,7 @@ Matrix2& Matrix2::operator=(const Matrix2& m) {
Matrix2::~Matrix2() { Matrix2::~Matrix2() {
if (!this->externalData) { if (!this->externalData) {
//printf("Deallocate %d\n", this->nValues); // printf("Deallocate %d\n", this->nValues);
delete[] data; delete[] data;
} }
} }
@ -174,7 +186,8 @@ Matrix2 Matrix2::Clone() const {
// nCols(other.nCols), // nCols(other.nCols),
// nValues(other.nValues), // nValues(other.nValues),
// data(other.data) { // 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 // // double deletion
// } // }
@ -275,7 +288,17 @@ Matrix2 Matrix2::operator-(const Matrix2& v) const {
return r; 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); Matrix2 r = Matrix2(this->nRows, B.nCols);
int ACols = this->nCols; int ACols = this->nCols;
@ -306,7 +329,7 @@ Matrix2 LinearAlgebra::Matrix2::operator*(const Matrix2& B) const {
Matrix2 Matrix2::GetRows(int from, int to) { Matrix2 Matrix2::GetRows(int from, int to) {
if (from < 0 || to >= this->nRows) 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); Matrix2 result = Matrix2(to - from, this->nCols);
int resultRowIx = 0; int resultRowIx = 0;
@ -330,8 +353,8 @@ Vector3 Matrix2::GetRow3(int rowIx) {
void Matrix2::SetRow(int rowIx, Matrix1 source) { void Matrix2::SetRow(int rowIx, Matrix1 source) {
int cellIx = rowIx * this->nCols; int cellIx = rowIx * this->nCols;
for (int ix = 0; ix < source.size; ix++) for (int ix = 0; ix < source.Size(); ix++)
this->data[cellIx + ix] = source.data[ix]; this->data[cellIx + ix] = source(ix);
} }
void Matrix2::SetRow3(int rowIx, Vector3 v) { void Matrix2::SetRow3(int rowIx, Vector3 v) {

View File

@ -9,13 +9,13 @@ namespace LinearAlgebra {
/// @brief A 1-dimensional matrix or vector of arbitrary size /// @brief A 1-dimensional matrix or vector of arbitrary size
class Matrix1 { class Matrix1 {
public: public:
float* data = nullptr;
int size = 0;
Matrix1(); Matrix1();
Matrix1(int size); Matrix1(int size);
Matrix1(float* data, int size); Matrix1(float* data, int size);
int Size() const;
static Matrix1 FromVector3(Vector3 v);
Vector3 ToVector3(); Vector3 ToVector3();
static Matrix1 FromQuaternion(Quaternion q); static Matrix1 FromQuaternion(Quaternion q);
@ -45,6 +45,10 @@ class Matrix1 {
this->data[ix] = source.data[sourceIx]; this->data[ix] = source.data[sourceIx];
} }
protected:
float* data = nullptr;
int size = 0;
private: private:
bool externalData = true; bool externalData = true;
}; };
@ -105,15 +109,16 @@ class Matrix2 {
return r; return r;
} }
friend Matrix1 operator*(const Matrix2& m, const Matrix1& v) { friend Matrix1 operator*(const Matrix2& m, const Matrix1& v);
Matrix1 r = Matrix1(m.nRows); // {
for (int rowIx = 0; rowIx < m.nRows; rowIx++) { // Matrix1 r = Matrix1(m.nRows);
int mRowIx = rowIx * m.nCols; // for (int rowIx = 0; rowIx < m.nRows; rowIx++) {
for (int colIx = 0; colIx < m.nCols; colIx++) // int mRowIx = rowIx * m.nCols;
r.data[rowIx] += m.data[mRowIx + colIx] * v.data[rowIx]; // for (int colIx = 0; colIx < m.nCols; colIx++)
} // r.data[rowIx] += m.data[mRowIx + colIx] * v.data[rowIx];
return r; // }
} // return r;
// }
friend Matrix2 operator/(const Matrix2& m, float f) { friend Matrix2 operator/(const Matrix2& m, float f) {
Matrix2 r = Matrix2(m.nRows, m.nCols); 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 // We don't want these because they make impliciti copies which is inefficient
// move constructor and move assignment operator // move constructor and move assignment operator
//Matrix2(Matrix2&& other) noexcept; // Matrix2(Matrix2&& other) noexcept;
//Matrix2& operator=(Matrix2&& other) noexcept; // Matrix2& operator=(Matrix2&& other) noexcept;
static Matrix2 Omega(const Vector3& v); static Matrix2 Omega(const Vector3& v);