diff --git a/LinearAlgebra/Matrix.cpp b/LinearAlgebra/Matrix.cpp index 5fa6fcd..78d3849 100644 --- a/LinearAlgebra/Matrix.cpp +++ b/LinearAlgebra/Matrix.cpp @@ -194,6 +194,13 @@ Matrix2 Matrix2::operator+=(const Matrix2& v) { return *this; } +Matrix2 Matrix2::operator-(const Matrix2& v) const { + Matrix2 r = Matrix2(this->nRows, this->nCols); + for (int ix = 0; ix < r.nValues; ix++) + r.data[ix] = this->data[ix] - v.data[ix]; + return r; +} + Matrix2 LinearAlgebra::Matrix2::operator*(const Matrix2& B) const { Matrix2 r = Matrix2(this->nRows, B.nCols); @@ -223,6 +230,23 @@ Matrix2 LinearAlgebra::Matrix2::operator*(const Matrix2& B) const { return r; } +Matrix2 Matrix2::GetRows(int from, int to) { + if (from < 0 || to >= this->nRows) + std::cerr << "Slice index out of range." << "std::endl"; + + Matrix2 result = Matrix2(to - from, this->nCols); + int resultRowIx = 0; + for (int rowIx = from; rowIx < to; rowIx++) { + for (int colIx = 0; colIx < this->nCols; colIx++) + result.data[resultRowIx * result.nCols + colIx] = + this->data[rowIx * this->nCols + colIx]; + + resultRowIx++; + } + + return result; +} + Matrix2 Matrix2::Slice(int rowStart, int rowStop, int colStart, int colStop) { Matrix2 r = Matrix2(rowStop - rowStart, colStop - colStart); @@ -259,6 +283,59 @@ void Matrix2::UpdateSlice(int rowStart, } } +Matrix2 Matrix2::Inverse() { + int n = this->nRows; + // // Create an identity matrix of the same size as the original matrix + Matrix2 augmentedMatrix(n, 2 * n); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + augmentedMatrix.data[i * this->nCols + j] = this->data[i, j]; + augmentedMatrix.data[i * this->nCols + (j + n)] = + (i == j) ? 1 : 0; // identity matrix; + } + } + + // Perform Gaussian elimination + for (int i = 0; i < n; i++) { + // Find the pivot row + float pivot = augmentedMatrix.data[i * augmentedMatrix.nCols + i]; + if (abs(pivot) < 1e-10) // Check for singular matrix + std::cerr << "Matrix is singular and cannot be inverted." << std::endl; + + // Normalize the pivot row + for (int j = 0; j < 2 * n; j++) + augmentedMatrix.data[i * augmentedMatrix.nCols + j] /= pivot; + + // Eliminate the column below the pivot + for (int j = i + 1; j < n; j++) { + float factor = augmentedMatrix.data[j * augmentedMatrix.nCols + i]; + for (int k = 0; k < 2 * n; k++) + augmentedMatrix.data[j * augmentedMatrix.nCols + k] -= + factor * augmentedMatrix.data[i * augmentedMatrix.nCols + k]; + } + } + + // Back substitution + for (int i = n - 1; i >= 0; i--) { + // Eliminate the column above the pivot + for (int j = i - 1; j >= 0; j--) { + float factor = augmentedMatrix.data[j * augmentedMatrix.nCols + i]; + for (int k = 0; k < 2 * n; k++) + augmentedMatrix.data[j * augmentedMatrix.nCols + k] -= + factor * augmentedMatrix.data[i * augmentedMatrix.nCols + k]; + } + } + + // Extract the inverse matrix from the augmented matrix + Matrix2 inverse(n, n); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) + inverse.data[i * inverse.nCols + j] = + augmentedMatrix.data[i * augmentedMatrix.nCols + j + n]; + } + return inverse; +} + Matrix2 Matrix2::DeleteRows(int rowStart, int rowStop) { Matrix2 r = Matrix2(this->nRows - (rowStop - rowStart), this->nCols); diff --git a/LinearAlgebra/Matrix.h b/LinearAlgebra/Matrix.h index fcb8055..5c7bcdc 100644 --- a/LinearAlgebra/Matrix.h +++ b/LinearAlgebra/Matrix.h @@ -80,6 +80,8 @@ class Matrix2 { Matrix2 operator+(const Matrix2& v) const; Matrix2 operator+=(const Matrix2& v); + Matrix2 operator-(const Matrix2& v) const; + Matrix2 operator*(const Matrix2& m) const; friend Matrix2 operator*(const Matrix2& m, float f) { Matrix2 r = Matrix2(m.nRows, m.nCols); @@ -117,22 +119,9 @@ class Matrix2 { return r; } - Matrix2 GetRows(int from, int to) { - if (from < 0 || to >= this->nRows) - std::cerr << "Slice index out of range." << "std::endl"; - - Matrix2 result = Matrix2(to-from, this->nCols); - int resultRowIx = 0; - for (int rowIx = from; rowIx < to; rowIx++) { - for (int colIx = 0; colIx < this->nCols; colIx++) - result.data[resultRowIx * result.nCols + colIx] = this->data[rowIx * this->nCols + colIx]; - - resultRowIx++; - } - - return result; - } + Matrix2 GetRows(int from, int to); + void SetRow(int rowIx, Matrix1 source) {} Matrix2 Slice(int rawStart, int rowStop, int colStart, int colStop); void UpdateSlice(int rowStart, @@ -141,6 +130,8 @@ class Matrix2 { int colStop, const Matrix2& m) const; + Matrix2 Inverse(); + Matrix2 DeleteRows(int rowStart, int rowStop); Matrix2 DeleteColumns(int colStart, int colStop);