diff --git a/matrix.cpp b/matrix.cpp new file mode 100644 index 0000000..5b8fce7 --- /dev/null +++ b/matrix.cpp @@ -0,0 +1,20 @@ +#include "matrix.hpp" +#include "../complex-numbers/cnumber.hpp" +#include "../vectors/vector.hpp" + +int main() { + vector v = vector(2); + v[0] = cnumber(1, 0); + v[1] = cnumber(0, 1); + + vector w = vector(2); + w[0] = cnumber(1, 0); + w[1] = cnumber(0, -1); + + matrix m = matrix(2, 2); + m[0] = v; + m[1] = w; + + cout << m << endl; + return 0; +} diff --git a/matrix.hpp b/matrix.hpp new file mode 100644 index 0000000..bae6c0c --- /dev/null +++ b/matrix.hpp @@ -0,0 +1,58 @@ +#include "../vectors/vector.hpp" +#include +#include +#include +using namespace std; +class matrix { +private: + long long num_rows; + long long num_cols; + vector *rows; + +public: + matrix(const long long num_rows, const long long num_cols) { + this->num_rows = num_rows; + this->num_cols = num_cols; + this->rows = (vector *)calloc(sizeof(vector), num_rows); + for (long long i = 0; i < num_rows; i++) { + this->rows[i] = vector(num_cols); + } + } + ~matrix() { + this->num_rows = 0; + this->num_cols = 0; + free(this->rows); + this->rows = NULL; + } + friend ostream &operator<<(ostream &os, const matrix &m) { + char last = '\0'; + for (long long i = 0; i < m.num_rows; i++) { + for (long long j = 0; j < m.num_cols; j++) { + string symbols[3]; + symbols[0] = "|"; + ostringstream oss; + oss << " " << m.rows[i][j] << " "; + symbols[1] = oss.str(); + symbols[2] = "|"; + for (int i = 0; i < 3; i++) { + int len = symbols[i].length() - 1; + char cur = symbols[i][0]; + if (cur != last) { + os << symbols[i]; + } + last = symbols[i][len]; + } + } + if (i != m.num_rows - 1) + os << endl << "|"; + } + return os; + } + const vector operator[](const long long index) const { + return this->rows[index]; + } + + // FIXME: Figure out how to make sure you do not try to access something + // outside of the index + vector &operator[](const long long index) { return this->rows[index]; } +};