Add offdiagonal getter and determinant

main
Micke Nordin 2 years ago
parent 8e5231e74c
commit c0fa950cf5
Signed by: micke
GPG Key ID: 014B273D614BE877

@ -28,10 +28,18 @@ int main() {
cout << "The matrix m:" << endl; cout << "The matrix m:" << endl;
cout << m << endl; cout << m << endl;
cout << "The determinant of matrix m:" << endl;
cout << m.determinant() << 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 m's diagonal:" << endl;
cout << m.get_diagonal() << endl;
cout << "The matrix m's off diagonal:" << endl;
cout << m.get_off_diagonal() << endl;
cout << "The matrix n" << endl; cout << "The matrix n" << endl;
cout << n << endl; cout << n << endl;
cout << "The determinant of matrix n:" << endl;
cout << n.determinant() << endl;
cout << "The matrix m * n" << endl; cout << "The matrix m * n" << endl;
cout << m * n << endl; cout << m * n << endl;
if (!m.is_hermitian()) { if (!m.is_hermitian()) {

@ -22,8 +22,19 @@ 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 cnumber determinant() const {
cnumber dsum(1,0);
cnumber osum(1,0);
vector diag = this->get_diagonal();
vector odiag = this->get_off_diagonal();
for (long long i = 0; i < diag.get_dimention(); i++) {
dsum = dsum * diag[i];
osum = osum * odiag[i];
}
return dsum - osum;
} }
const vector get_diagonal() const { const vector get_diagonal() const {
long long diag_len = this->num_rows; long long diag_len = this->num_rows;
@ -40,6 +51,21 @@ public:
} }
return v; return v;
} }
const vector get_off_diagonal() const {
long long diag_len = this->num_rows;
if (this->num_cols < diag_len) {
diag_len = this->num_cols;
}
vector v(diag_len);
for (long long i = 0; i < this->num_cols; i++) {
for (long long j = 0; j < this->num_rows; j++) {
if (i + j == this->num_rows - 1) {
v[i] = this->cols[i][j];
}
}
}
return v;
}
const vector get_row(long long index) const { return this->cols[index]; } const vector get_row(long long index) const { return this->cols[index]; }
const vector get_column(long long index) const { const vector get_column(long long index) const {

Loading…
Cancel
Save