Something went wrong on our end
Select Git revision
matrix.h 1.43 KiB
#ifndef __MATRIX_H__
#define __MATRIX_H__
#include <vector>
#include <iostream>
namespace magic {
using std::vector;
// This is a custom Matrix type to allow for matrix multiplication
class Matrix {
public:
Matrix(uint8_t rows, uint8_t columns) {
this->_rows = rows;
this->_columns = columns;
this->entries.assign(rows, vector<int32_t>(columns, 0));
}
void setValue(uint8_t i, uint8_t j, int32_t value) {
entries[i][j] = value;
}
int32_t getValue(uint8_t i, uint8_t j) {
return entries[i][j];
}
// Calculate the matrix product with another matrix
Matrix multiply(Matrix &other) {
// Create the result matrix
Matrix result(this->_rows, other._columns);
// Calculate all entries in the resulting matrix
for (uint8_t r = 0; r < this->_rows; r++) {
for (uint8_t c = 0; c < other._columns; c++) {
for (uint8_t link = 0; link < this->_columns; link++) {
int32_t res = result.getValue(r, c) + this->getValue(r, link) * other.getValue(link, c);
result.setValue(r, c, res);
}
}
}
// Return the resulting matrix
return result;
}
// Output the matrix to screen
void print() {
using std::cout;
using std::endl;
for (uint8_t r = 0; r < this->_rows; r++) {
for (uint8_t c = 0; c < this->_columns; c++) {
cout << entries[r][c] << '\t';
}
cout << endl;
}
}
private:
uint8_t _rows;
uint8_t _columns;
vector<vector<int32_t>> entries;
};
}
#endif