Add offdiagonal getter and determinant
This commit is contained in:
parent
8e5231e74c
commit
c0fa950cf5
2 changed files with 36 additions and 2 deletions
|
@ -28,10 +28,18 @@ int main() {
|
|||
|
||||
cout << "The matrix m:" << endl;
|
||||
cout << m << endl;
|
||||
cout << "The determinant of matrix m:" << endl;
|
||||
cout << m.determinant() << endl;
|
||||
cout << "The matrix m's 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 << n << endl;
|
||||
cout << "The determinant of matrix n:" << endl;
|
||||
cout << n.determinant() << endl;
|
||||
cout << "The matrix m * n" << endl;
|
||||
cout << m * n << endl;
|
||||
if (!m.is_hermitian()) {
|
||||
|
|
30
matrix.hpp
30
matrix.hpp
|
@ -22,8 +22,19 @@ public:
|
|||
~matrix() {
|
||||
this->num_cols = 0;
|
||||
this->num_rows = 0;
|
||||
// free(this->cols);
|
||||
// this->cols = NULL;
|
||||
free(this->cols);
|
||||
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 {
|
||||
long long diag_len = this->num_rows;
|
||||
|
@ -40,6 +51,21 @@ public:
|
|||
}
|
||||
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_column(long long index) const {
|
||||
|
|
Loading…
Add table
Reference in a new issue