Now with matrix multiplication

main
Micke Nordin 2 years ago
parent 8a7399df48
commit 330f4d289d
Signed by: micke
GPG Key ID: 014B273D614BE877

@ -3,34 +3,48 @@
#include "../vectors/vector.hpp" #include "../vectors/vector.hpp"
int main() { int main() {
vector a = vector(3); vector a = vector(2);
a[0] = cnumber(7, 0); a[0] = cnumber(7, 0);
a[1] = cnumber(5, 0); a[1] = cnumber(6, 0);
a[2] = cnumber(0, 1);
vector b = vector(3); vector b = vector(2);
b[0] = cnumber(0, 0); b[0] = cnumber(5, 0);
b[1] = cnumber(2, 0); b[1] = cnumber(3, 0);
b[2] = cnumber(0, 0);
vector c = vector(3); vector c = vector(2);
c[0] = cnumber(0, -1); c[0] = cnumber(2, 0);
c[1] = cnumber(0, 0); c[1] = cnumber(5, 0);
c[2] = cnumber(4, 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[0] = a;
m[1] = b; m[1] = b;
m[2] = c;
matrix n = matrix(2, 2);
n[0] = c;
n[1] = d;
cout << "The matrix m:" << endl; cout << "The matrix m:" << endl;
cout << m << endl; cout << m << endl;
cout << "The matrix m's transpose:" << endl; cout << "The matrix m's transpose:" << endl;
cout << m.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()) { if (!m.is_hermitian()) {
cout << "The matrix m is not hermitian, here is the hermitian conjugate:" cout << "The matrix m is not hermitian, here is the hermitian conjugate:"
<< endl; << endl;
cout << m.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; return 0;
} }

@ -21,8 +21,8 @@ public:
~matrix() { ~matrix() {
this->num_cols = 0; this->num_cols = 0;
this->num_rows = 0; this->num_rows = 0;
free(this->cols); // free(this->cols);
this->cols = NULL; // this->cols = NULL;
} }
const vector get_diagonal() const { const vector get_diagonal() const {
long long diag_len = this->num_rows; long long diag_len = this->num_rows;
@ -39,6 +39,20 @@ public:
} }
return v; 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 { const matrix transpose() const {
matrix n(this->num_rows, this->num_cols); matrix n(this->num_rows, this->num_cols);
for (long long i = 0; i < this->num_cols; i++) { for (long long i = 0; i < this->num_cols; i++) {
@ -64,7 +78,7 @@ public:
} }
const bool is_hermitian() const { const bool is_hermitian() const {
if (this->num_rows != this->num_cols) if (this->num_rows != this->num_cols)
return false; return false;
matrix m = this->hermitian_conjugate(); matrix m = this->hermitian_conjugate();
bool equal = true; bool equal = true;
for (long long i = 0; i < m.num_cols; i++) { for (long long i = 0; i < m.num_cols; i++) {
@ -122,6 +136,18 @@ public:
return n; 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 { const matrix operator+(const matrix &m) const {
matrix n(this->num_cols, this->num_rows); matrix n(this->num_cols, this->num_rows);

Loading…
Cancel
Save