From 330f4d289df9232984b59f97245eda9d629b0610 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sat, 16 Jul 2022 17:30:59 +0200 Subject: [PATCH] Now with matrix multiplication --- matrix.cpp | 40 +++++++++++++++++++++++++++------------- matrix.hpp | 32 +++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 16 deletions(-) diff --git a/matrix.cpp b/matrix.cpp index 279f52a..87eb15b 100644 --- a/matrix.cpp +++ b/matrix.cpp @@ -3,34 +3,48 @@ #include "../vectors/vector.hpp" int main() { - vector a = vector(3); + vector a = vector(2); a[0] = cnumber(7, 0); - a[1] = cnumber(5, 0); - a[2] = cnumber(0, 1); + a[1] = cnumber(6, 0); - vector b = vector(3); - b[0] = cnumber(0, 0); - b[1] = cnumber(2, 0); - b[2] = cnumber(0, 0); + vector b = vector(2); + b[0] = cnumber(5, 0); + b[1] = cnumber(3, 0); - vector c = vector(3); - c[0] = cnumber(0, -1); - c[1] = cnumber(0, 0); - c[2] = cnumber(4, 0); + vector c = vector(2); + c[0] = cnumber(2, 0); + c[1] = cnumber(5, 0); - matrix m = matrix(3, 3); + + vector d = vector(2); + d[0] = cnumber(1, 0); + d[1] = cnumber(0, 1); + + matrix m = matrix(2, 2); m[0] = a; m[1] = b; - m[2] = c; + + matrix n = matrix(2, 2); + n[0] = c; + n[1] = d; cout << "The matrix m:" << endl; cout << m << endl; cout << "The matrix m's transpose:" << endl; cout << m.transpose() << endl; + cout << "The matrix n" << endl; + cout << n << endl; + cout << "The matrix m * n" << endl; + cout << m * n << endl; if (!m.is_hermitian()) { cout << "The matrix m is not hermitian, here is the hermitian conjugate:" << endl; cout << m.hermitian_conjugate() << endl; } + if (!n.is_hermitian()) { + cout << "The matrix n is not hermitian, here is the hermitian conjugate:" + << endl; + cout << n.hermitian_conjugate() << endl; + } return 0; } diff --git a/matrix.hpp b/matrix.hpp index cc4ea15..659ee1c 100644 --- a/matrix.hpp +++ b/matrix.hpp @@ -21,8 +21,8 @@ public: ~matrix() { this->num_cols = 0; this->num_rows = 0; - free(this->cols); - this->cols = NULL; + // free(this->cols); + // this->cols = NULL; } const vector get_diagonal() const { long long diag_len = this->num_rows; @@ -39,6 +39,20 @@ public: } return v; } + const vector get_row(long long index) const { return this->cols[index]; } + + const vector get_column(long long index) const { + vector v(this->num_rows); + for (long long j = 0; j < this->num_rows; j++) { + v[j] = this->cols[j][index]; + } + return v; + } + + const matrix rotate_ninety() const { + matrix m(this->num_cols, this->num_rows); + return m; + } const matrix transpose() const { matrix n(this->num_rows, this->num_cols); for (long long i = 0; i < this->num_cols; i++) { @@ -64,7 +78,7 @@ public: } const bool is_hermitian() const { if (this->num_rows != this->num_cols) - return false; + return false; matrix m = this->hermitian_conjugate(); bool equal = true; for (long long i = 0; i < m.num_cols; i++) { @@ -122,6 +136,18 @@ public: return n; } + const matrix operator*(const matrix m) const { + matrix n(this->num_cols, m.num_rows); + // if (this->num_cols != m.num_rows && m.num_cols != this->num_rows) + // return n; + for (long long i = 0; i < this->num_rows; i++) { + for (long long j = 0; j < this->num_rows; j++) { + n[i][j] = this->get_row(i) * m.get_column(j); + } + } + + return n; + } const matrix operator+(const matrix &m) const { matrix n(this->num_cols, this->num_rows);