Skip to content
Snippets Groups Projects
Commit bc6943ae authored by Kevin Höllring's avatar Kevin Höllring
Browse files

Fix broken iteration in matrix class

parent 515e228a
No related branches found
No related tags found
No related merge requests found
#ifndef __MATRIX_H__ #ifndef __MATRIX_H__
#define __MATRIX_H__ #define __MATRIX_H__
#include <vector>
#include <iostream> #include <iostream>
#include <vector>
namespace magic { namespace magic {
using std::vector; using std::vector;
...@@ -9,7 +9,7 @@ namespace magic { ...@@ -9,7 +9,7 @@ namespace magic {
// This is a custom Matrix type to allow for matrix multiplication // This is a custom Matrix type to allow for matrix multiplication
class Matrix { class Matrix {
public: public:
Matrix(uint8_t rows, uint8_t columns) { Matrix(uint32_t rows, uint32_t columns) {
this->_rows = rows; this->_rows = rows;
this->_columns = columns; this->_columns = columns;
this->entries.assign(rows, vector<int32_t>(columns, 0)); this->entries.assign(rows, vector<int32_t>(columns, 0));
...@@ -19,9 +19,7 @@ namespace magic { ...@@ -19,9 +19,7 @@ namespace magic {
entries[i][j] = value; entries[i][j] = value;
} }
int32_t getValue(uint8_t i, uint8_t j) { int32_t getValue(uint8_t i, uint8_t j) { return entries[i][j]; }
return entries[i][j];
}
// Calculate the matrix product with another matrix // Calculate the matrix product with another matrix
Matrix multiply(Matrix& other) { Matrix multiply(Matrix& other) {
...@@ -29,10 +27,12 @@ namespace magic { ...@@ -29,10 +27,12 @@ namespace magic {
Matrix result(this->_rows, other._columns); Matrix result(this->_rows, other._columns);
// Calculate all entries in the resulting matrix // Calculate all entries in the resulting matrix
for (uint8_t r = 0; r < this->_rows; r++) { for (uint32_t r = 0; r < this->_rows; r++) {
for (uint8_t c = 0; c < other._columns; c++) { for (uint32_t c = 0; c < other._columns; c++) {
for (uint8_t link = 0; link < this->_columns; link++) { for (uint32_t link = 0; link < this->_columns; link++) {
int32_t res = result.getValue(r, c) + this->getValue(r, link) * other.getValue(link, c); int32_t res =
result.getValue(r, c) +
this->getValue(r, link) * other.getValue(link, c);
result.setValue(r, c, res); result.setValue(r, c, res);
} }
} }
...@@ -56,10 +56,10 @@ namespace magic { ...@@ -56,10 +56,10 @@ namespace magic {
} }
private: private:
uint8_t _rows; uint32_t _rows;
uint8_t _columns; uint32_t _columns;
vector<vector<int32_t>> entries; vector<vector<int32_t>> entries;
}; };
} } // namespace magic
#endif #endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment