Compare commits

...

2 Commits

@ -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()) {

@ -11,19 +11,38 @@ private:
vector *cols;
public:
matrix(const long long num_cols, const long long num_rows) {
this->num_cols = num_rows;
this->num_rows = num_cols;
this->cols = (vector *)calloc(sizeof(vector), num_rows);
for (long long i = 0; i < num_rows; i++) {
this->cols[i] = vector(num_cols);
matrix(const long long num_rows, const long long num_cols) {
this->num_cols = num_cols;
this->num_rows = num_rows;
this->cols = (vector *)calloc(sizeof(vector), num_cols);
for (long long i = 0; i < num_cols; i++) {
this->cols[i] = vector(num_rows);
}
}
matrix(const matrix &m) {
this->num_cols = m.num_cols;
this->num_rows = m.num_rows;
this->cols = (vector *)calloc(sizeof(vector), m.num_cols);
for (long long i = 0; i < m.num_cols; i++) {
this->cols[i] = m[i];
}
}
~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 +59,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…
Cancel
Save