diff --git a/Matrix.h b/Matrix.h index 5ef8848..9167b5a 100644 --- a/Matrix.h +++ b/Matrix.h @@ -1,14 +1,22 @@ +#pragma once + #include #include #include +#include template class Matrix{ public: + Matrix(); + + Matrix(const std::array & array); + /** * @brief Element-wise matrix addition * @param other the other matrix to add to this one * @param result A buffer to store the result into + * @note there is no problem if result == this */ void Add(const Matrix & other, Matrix & result) const; @@ -16,6 +24,7 @@ class Matrix{ * @brief Element-wise subtract matrix * @param other the other matrix to subtract from this one * @param result A buffer to store the result into + * @note there is no problem if result == this */ void Subtract(const Matrix & other, Matrix & result) const; @@ -31,12 +40,14 @@ class Matrix{ * @brief Multiply the matrix by a scalar * @param scalar the the scalar to multiply by * @param result A buffer to store the result into + * @note there is no problem if result == this */ void Multiply(float scalar, Matrix & result) const; /** * @brief Invert this matrix * @param result A buffer to store the result into + * @warning this is super slow! Only call it if you absolutely have to!!! */ void Invert(Matrix & result) const; @@ -55,12 +66,19 @@ class Matrix{ /** * @return Get the determinant of the matrix */ - float Det(); + float Det() const; + + /** + * @brief Calculate the eigenvalues for a square matrix + * @param result a buffer to store the result into + */ + void EigenValues(Matrix & result) const; /** * @brief Element-wise multiply the two matrices * @param other the other matrix to multiply into this one * @param result A buffer to store the result into + * @note there is no problem if result == this */ void ElementMultiply(const Matrix & other, Matrix & result) const; @@ -68,6 +86,7 @@ class Matrix{ * @brief Element-wise divide the two matrices * @param other the other matrix to multiply into this one * @param result A buffer to store the result into + * @note there is no problem if result == this */ void ElementDivide(const Matrix & other, Matrix & result) const; @@ -79,19 +98,26 @@ class Matrix{ */ float & Get(uint8_t row_index, uint8_t column_index) const; + /** + * @brief get the specified row of the matrix returned as a reference to the internal array + */ + std::array & operator[](uint8_t row_index) const; + + void operator=(Matrix & other); + /** * @brief Get a row from the matrix * @param row_index the row index to get * @param row a buffer to write the row into */ - void GetRow(uint8_t row_index, Matrix & row) const; + void GetRow(uint8_t row_index, Matrix<1, columns> & row) const; /** * @brief Get a row from the matrix * @param column_index the row index to get * @param column a buffer to write the row into */ - void GetColumn(uint8_t column_index, Matrix<1, columns> & column) const; + void GetColumn(uint8_t column_index, Matrix & column) const; /** * @brief Get the number of rows in this matrix @@ -104,7 +130,6 @@ class Matrix{ constexpr uint8_t GetColumnSize(){return columns;} private: - /** * @brief take the dot product of the two vectors */ @@ -116,13 +141,42 @@ class Matrix{ */ void zeroMatrix(); - void matrixOfMinors(const Matrix & input, Matrix & result) const; + void matrixOfMinors(Matrix & result) const; - void adjugate(const Matrix & input, Matrix & result) const; + void minorMatrix(Matrix & result, uint8_t row_idx, uint8_t column_idx) const; + void adjugate(Matrix & result) const; + + /** + * @brief reduce the matrix so the sum of its elements equal 1 + * @param result a buffer to store the result into + */ + void normalize(Matrix & result) const; + + constexpr bool isSquare(){return rows==columns;} std::array, rows> matrix; }; +template +Matrix::Matrix(){ + this->zeroMatrix(); +} + +template +Matrix::Matrix(const std::array & array){ + for(uint8_t row_idx{0}; row_idx < rows; row_idx++){ + for(uint8_t column_idx{0}; column_idx < columns; column_idx++){ + uint16_t i = static_cast(row_idx) + static_cast(column_idx); + if(i < array.size()){ + this->Get(row_idx, column_idx) = array[i]; + } + else{ + this->Get(row_idx, column_idx) = 0; + } + } + } +} + template void Matrix::Add(const Matrix & other, Matrix & result) const{ for(uint8_t row{0}; row < rows; row++){ @@ -183,14 +237,15 @@ void Matrix::Invert(Matrix & result) const{ return; } + // TODO: This algorithm is really inneficient because of the matrix of minors. We should make a different algorithm // how to calculate the inverse: https://www.mathsisfun.com/algebra/matrix-inverse-minors-cofactors-adjugate.html // calculate the matrix of minors Matrix minors{}; - this->matrixOfMinors(this, minors); + this->matrixOfMinors(minors); // now adjugate the matrix and save it in our output - this->adjugate(minors, result); + minors.adjugate(result); float determinant = this->Det(); // scale the result by 1/determinant and we have our answer @@ -199,5 +254,199 @@ void Matrix::Invert(Matrix & result) const{ template void Matrix::Transpose(Matrix & result) const{ + for(uint8_t column_idx{0}; column_idx < rows; column_idx++){ + for(uint8_t row_idx{0}; row_idx < columns; row_idx++){ + result.Get(row_idx, column_idx) = this->Get(column_idx, row_idx); + } + } +} +template +void Matrix::Square(Matrix & result) const{ + static_assert(this->isSquare(), "You can't square an non-square matrix."); + + this->Multiply(this, result); +} + +template +float Matrix::Det() const{ + static_assert(this->isSquare(), "You can't take the determinant of a non-square matrix."); + Matrix<1, columns> eigenValues{}; + this->EigenValues(eigenValues); + + float determinant{1}; + for(uint8_t i{0}; i < columns; i++){ + determinant *= eigenValues.Get(0, i); + } + + return determinant; +} + +template +void Matrix::EigenValues(Matrix & eigenvalues) const{ + static_assert(rows == columns, "Eigenvalues can only be computed for square matrices."); + // I got this code from: https://www.quora.com/What-is-the-C-code-for-finding-eigenvalues + Matrix v{}; + Matrix Av{}; + Matrix z{}; + + float d = 0.0, d_old = 0.0; + constexpr float convergence_value{1e-6}; + constexpr uint16_t max_iterations{500}; + + // Initialize v as a random vector + for (int i = 0; i < rows; i++) { + v[0][i] = rand() / RAND_MAX; + } + + // run this loop until the eigenvalues converge or we give up + for (uint16_t k{0}; k < max_iterations; k++) { + /* Multiply A by v */ + for (int i = 0; i < rows; i++) { + Av[0][i] = 0.0; + for (int j = 0; j < rows; j++) { + Av[0][i] += this->Get(0, i * rows + j) * v[0][j]; + } + } + + // Calculate the eigenvalue and update v + d_old = d; + d = dot_product(v, Av, rows); + for (int i = 0; i < rows; i++) { + z[0][i] = Av[0][i] - d * v[0][i]; + } + + z.normalize(z); + + for (int i = 0; i < rows; i++) { + v[0][i] = z[0][i]; + } + + /* Check for convergence */ + if (fabs(d - d_old) < convergence_value) { + eigenvalues[0][k] = d; + k++; + d = 0.0; + for (int i = 0; i < rows; i++) { + v[0][i] = rand() / RAND_MAX; + } + } + } +} + +template +void Matrix::ElementMultiply(const Matrix & other, Matrix & result) const{ + for(uint8_t row_idx{0}; row_idx < rows; row_idx++){ + for(uint8_t column_idx{0}; column_idx < columns; column_idx++){ + result.Get(row_idx, column_idx) = this->Get(row_idx, column_idx) * other.Get(row_idx, column_idx); + } + } +} + +template +void Matrix::ElementDivide(const Matrix & other, Matrix & result) const{ + for(uint8_t row_idx{0}; row_idx < rows; row_idx++){ + for(uint8_t column_idx{0}; column_idx < columns; column_idx++){ + result.Get(row_idx, column_idx) = this->Get(row_idx, column_idx) / other.Get(row_idx, column_idx); + } + } +} + +template +float & Matrix::Get(uint8_t row_index, uint8_t column_index) const{ + return this->matrix[row_index][column_index]; +} + +template +void Matrix::GetRow(uint8_t row_index, Matrix<1, columns> & row) const{ + row = Matrix<1, columns>(this->matrix[row_index]); +} + +template +void Matrix::GetColumn(uint8_t column_index, Matrix & column) const{ + for(uint8_t row_idx{0}; row_idx < rows; row_idx++){ + column.Get(0, column_index) = this->Get(row_idx, column_index); + } +} + +template +template +float Matrix::dotProduct(const Matrix & vec1, const Matrix & vec2){ + float sum{0}; + for(uint8_t i{0}; i < vector_size; i++){ + sum += vec1.Get(i, 0) * vec2.Get(i, 0); + } + + return sum; +} + +template +void Matrix::zeroMatrix(){ + for(uint8_t row_idx{0}; row_idx < rows; row_idx++){ + for(uint8_t column_idx{0}; column_idx < columns; column_idx++){ + this->matrix[row_idx][column_idx] = 0; + } + } +} + +template +void Matrix::matrixOfMinors(Matrix & result) const{ + Matrix minorMatrix{}; + + for(uint8_t row_idx{0}; row_idx < rows; row_idx++){ + for(uint8_t column_idx{0}; column_idx < columns; column_idx++){ + this->minorMatrix(minorMatrix, row_idx, column_idx); + result.Get(row_idx, column_idx) = minorMatrix.Det(); + } + } +} + +template +void Matrix::minorMatrix(Matrix & result, uint8_t row_idx, uint8_t column_idx) const{ + std::array subArray{}; + + for(uint8_t row_iter{0}; row_iter < rows; row_iter++){ + for(uint8_t column_iter{0}; column_iter < columns; column_iter++){ + uint16_t i = static_cast(row_iter) + static_cast(column_iter); + if(row_iter == row_idx || column_iter == column_idx){ + continue; + } + subArray[i] = this->Get(row_iter, column_iter); + } + } + + result = Matrix{subArray}; +} + +template +void Matrix::adjugate(Matrix & result) const{ + for(uint8_t row_iter{0}; row_iter < rows; row_iter++){ + for(uint8_t column_iter{0}; column_iter < columns; column_iter++){ + float sign = ((row_iter + 1) % 2) ? -1 : 1; + sign *= ((column_iter + 1) % 2) ? -1 : 1; + result.Get(row_iter, column_iter) = this->Get(row_iter, column_iter) * sign; + } + } +} + +template +void Matrix::normalize(Matrix & result) const{ + float sum{0}; + for(uint8_t column_idx{0}; column_idx < rows; column_idx++){ + for(uint8_t row_idx{0}; row_idx < columns; row_idx++){ + sum += this->Get(row_idx, column_idx); + } + } + + if(sum == 0){ + // this wouldn't do anything anyways + result.zeroMatrix(); + return; + } + + for(uint8_t column_idx{0}; column_idx < rows; column_idx++){ + for(uint8_t row_idx{0}; row_idx < columns; row_idx++){ + result.Get(row_idx, column_idx) = this->Get(row_idx, column_idx) / sum; + } + } } \ No newline at end of file