diff --git a/matrix.cpp b/matrix.cpp index 614c76c..abe8ecc 100644 --- a/matrix.cpp +++ b/matrix.cpp @@ -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()) { diff --git a/matrix.hpp b/matrix.hpp index fc943ec..20bce4a 100644 --- a/matrix.hpp +++ b/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 {