|
|
@ -6,32 +6,36 @@
|
|
|
|
using namespace std;
|
|
|
|
using namespace std;
|
|
|
|
class matrix {
|
|
|
|
class matrix {
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
long long num_cols;
|
|
|
|
long long num_entries;
|
|
|
|
long long num_rows;
|
|
|
|
long long entry_dimension;
|
|
|
|
vector *cols;
|
|
|
|
vector *entries;
|
|
|
|
|
|
|
|
bool err;
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
matrix(const long long num_rows, const long long num_cols) {
|
|
|
|
matrix(const long long num_entries, const long long entry_dimension) {
|
|
|
|
this->num_cols = num_cols;
|
|
|
|
this->num_entries = entry_dimension;
|
|
|
|
this->num_rows = num_rows;
|
|
|
|
this->entry_dimension = num_entries;
|
|
|
|
this->cols = (vector *)calloc(sizeof(vector), num_cols);
|
|
|
|
this->err = false;
|
|
|
|
for (long long i = 0; i < num_cols; i++) {
|
|
|
|
this->entries = (vector *)malloc(sizeof(vector) * this->num_entries);
|
|
|
|
this->cols[i] = vector(num_rows);
|
|
|
|
for (long long i = 0; i < entry_dimension; i++) {
|
|
|
|
|
|
|
|
this->entries[i] = vector(this->entry_dimension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
matrix(const matrix &m) {
|
|
|
|
matrix(const matrix &m) {
|
|
|
|
this->num_cols = m.num_cols;
|
|
|
|
this->num_entries = m.num_entries;
|
|
|
|
this->num_rows = m.num_rows;
|
|
|
|
this->entry_dimension = m.entry_dimension;
|
|
|
|
this->cols = (vector *)calloc(sizeof(vector), m.num_cols);
|
|
|
|
this->err = m.err;
|
|
|
|
for (long long i = 0; i < m.num_cols; i++) {
|
|
|
|
this->entries = (vector *)malloc(sizeof(vector) * m.num_entries);
|
|
|
|
this->cols[i] = m[i];
|
|
|
|
for (long long i = 0; i < m.num_entries; i++) {
|
|
|
|
|
|
|
|
this->entries[i] = m[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~matrix() {
|
|
|
|
~matrix() {
|
|
|
|
this->num_cols = 0;
|
|
|
|
this->num_entries = 0;
|
|
|
|
this->num_rows = 0;
|
|
|
|
this->entry_dimension = 0;
|
|
|
|
free(this->cols);
|
|
|
|
free(this->entries);
|
|
|
|
this->cols = NULL;
|
|
|
|
this->entries = NULL;
|
|
|
|
|
|
|
|
this->err = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const cnumber determinant() const {
|
|
|
|
const cnumber determinant() const {
|
|
|
|
cnumber dsum(1, 0);
|
|
|
|
cnumber dsum(1, 0);
|
|
|
@ -45,64 +49,57 @@ public:
|
|
|
|
return dsum - osum;
|
|
|
|
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->entry_dimension;
|
|
|
|
if (this->num_cols < diag_len) {
|
|
|
|
if (this->num_entries < diag_len) {
|
|
|
|
diag_len = this->num_cols;
|
|
|
|
diag_len = this->num_entries;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vector v(diag_len);
|
|
|
|
vector v(diag_len);
|
|
|
|
for (long long i = 0; i < this->num_cols; i++) {
|
|
|
|
for (long long i = 0; i < this->num_entries; i++) {
|
|
|
|
for (long long j = 0; j < this->num_rows; j++) {
|
|
|
|
for (long long j = 0; j < this->entry_dimension; j++) {
|
|
|
|
if (i == j) {
|
|
|
|
if (i == j) {
|
|
|
|
v[i] = this->cols[i][j];
|
|
|
|
v[i] = this->entries[i][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const vector get_off_diagonal() const {
|
|
|
|
const vector get_off_diagonal() const {
|
|
|
|
long long diag_len = this->num_rows;
|
|
|
|
return this->rotate_by_one_pi().get_diagonal();
|
|
|
|
if (this->num_cols < diag_len) {
|
|
|
|
|
|
|
|
diag_len = this->num_cols;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vector v(diag_len);
|
|
|
|
const vector get_entry(long long index) const { return this->entries[index]; }
|
|
|
|
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 {
|
|
|
|
const vector get_row(long long index) const {
|
|
|
|
vector v(this->num_rows);
|
|
|
|
vector v(this->entry_dimension);
|
|
|
|
for (long long j = 0; j < this->num_rows; j++) {
|
|
|
|
for (long long j = 0; j < this->entry_dimension; j++) {
|
|
|
|
v[j] = this->cols[j][index];
|
|
|
|
v[j] = this->entries[j][index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const matrix rotate_ninety() const {
|
|
|
|
const matrix rotate_by_one_pi() const {
|
|
|
|
matrix m(this->num_cols, this->num_rows);
|
|
|
|
matrix m = this->transpose();
|
|
|
|
return m;
|
|
|
|
matrix n = matrix(m.entry_dimension, m.num_entries);
|
|
|
|
|
|
|
|
for (long long i = 0; i < m.entry_dimension; i++) {
|
|
|
|
|
|
|
|
int index = m.num_entries - i - 1;
|
|
|
|
|
|
|
|
n[i] = m[index];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const matrix transpose() const {
|
|
|
|
const matrix transpose() const {
|
|
|
|
matrix n(this->num_rows, this->num_cols);
|
|
|
|
matrix n = matrix(this->entry_dimension, this->num_entries);
|
|
|
|
for (long long i = 0; i < this->num_cols; i++) {
|
|
|
|
for (long long i = 0; i < this->num_entries; i++) {
|
|
|
|
for (long long j = 0; j < this->num_rows; j++) {
|
|
|
|
for (long long j = 0; j < this->entry_dimension; j++) {
|
|
|
|
n[j][i] = this->cols[i][j];
|
|
|
|
n[j][i] = this->entries[i][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return n;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const matrix conjugate() const {
|
|
|
|
const matrix conjugate() const {
|
|
|
|
matrix n(this->num_cols, this->num_rows);
|
|
|
|
matrix n = matrix(this->num_entries, this->entry_dimension);
|
|
|
|
for (long long i = 0; i < this->num_cols; i++) {
|
|
|
|
for (long long i = 0; i < this->num_entries; i++) {
|
|
|
|
for (long long j = 0; j < this->num_rows; j++) {
|
|
|
|
for (long long j = 0; j < this->entry_dimension; j++) {
|
|
|
|
n[i][j] = this->cols[i][j].conjugate();
|
|
|
|
n[i][j] = this->entries[i][j].conjugate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -112,13 +109,13 @@ public:
|
|
|
|
return this->transpose().conjugate();
|
|
|
|
return this->transpose().conjugate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const bool is_hermitian() const {
|
|
|
|
const bool is_hermitian() const {
|
|
|
|
if (this->num_rows != this->num_cols)
|
|
|
|
if (this->entry_dimension != this->num_entries)
|
|
|
|
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_entries; i++) {
|
|
|
|
for (long long j = 0; j < m.num_rows; j++) {
|
|
|
|
for (long long j = 0; j < m.entry_dimension; j++) {
|
|
|
|
if (m[i][j] != this->cols[i][j]) {
|
|
|
|
if (m[i][j] != this->entries[i][j]) {
|
|
|
|
equal = false;
|
|
|
|
equal = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -127,12 +124,12 @@ public:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
friend ostream &operator<<(ostream &os, const matrix &m) {
|
|
|
|
friend ostream &operator<<(ostream &os, const matrix &m) {
|
|
|
|
char last = '\0';
|
|
|
|
char last = '\0';
|
|
|
|
for (long long i = 0; i < m.num_cols; i++) {
|
|
|
|
for (long long i = 0; i < m.num_entries; i++) {
|
|
|
|
for (long long j = 0; j < m.num_rows; j++) {
|
|
|
|
for (long long j = 0; j < m.entry_dimension; j++) {
|
|
|
|
string symbols[3];
|
|
|
|
string symbols[3];
|
|
|
|
symbols[0] = "|";
|
|
|
|
symbols[0] = "|";
|
|
|
|
ostringstream oss;
|
|
|
|
ostringstream oss;
|
|
|
|
oss << " " << m.cols[i][j] << " ";
|
|
|
|
oss << " " << m.entries[i][j] << " ";
|
|
|
|
symbols[1] = oss.str();
|
|
|
|
symbols[1] = oss.str();
|
|
|
|
symbols[2] = "|";
|
|
|
|
symbols[2] = "|";
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
@ -144,40 +141,44 @@ public:
|
|
|
|
last = symbols[i][len];
|
|
|
|
last = symbols[i][len];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i != m.num_cols - 1)
|
|
|
|
if (i != m.num_entries - 1)
|
|
|
|
os << endl << "|";
|
|
|
|
os << endl << "|";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return os;
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const bool operator==(const matrix &m) const {
|
|
|
|
const bool operator==(const matrix &m) const {
|
|
|
|
bool equal = true;
|
|
|
|
bool equal = true;
|
|
|
|
for (long long i = 0; i < this->num_cols; i++) {
|
|
|
|
for (long long i = 0; i < this->num_entries; i++) {
|
|
|
|
for (long long j = 0; j < this->num_rows; j++) {
|
|
|
|
for (long long j = 0; j < this->entry_dimension; j++) {
|
|
|
|
if (this->cols[i][j] != m[i][j]) {
|
|
|
|
if (this->entries[i][j] != m[i][j]) {
|
|
|
|
equal = false;
|
|
|
|
equal = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return equal;
|
|
|
|
return equal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vector &operator[](const long long index) { return this->entries[index]; }
|
|
|
|
const vector operator[](const long long index) const {
|
|
|
|
const vector operator[](const long long index) const {
|
|
|
|
return this->cols[index];
|
|
|
|
return this->entries[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const matrix operator*(const cnumber z) const {
|
|
|
|
const matrix operator*(const cnumber z) const {
|
|
|
|
matrix n(this->num_cols, this->num_rows);
|
|
|
|
matrix n(this->num_entries, this->entry_dimension);
|
|
|
|
for (long long i = 0; i < this->num_cols; i++) {
|
|
|
|
for (long long i = 0; i < this->num_entries; i++) {
|
|
|
|
n[i] = this->cols[i] * z;
|
|
|
|
n[i] = this->entries[i] * z;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return n;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const matrix operator*(const matrix m) const {
|
|
|
|
const matrix operator*(const matrix m) const {
|
|
|
|
matrix n(this->num_cols, m.num_rows);
|
|
|
|
matrix n(this->num_entries, m.entry_dimension);
|
|
|
|
// if (this->num_cols != m.num_rows && m.num_cols != this->num_rows)
|
|
|
|
if (this->num_entries != m.entry_dimension &&
|
|
|
|
// return n;
|
|
|
|
m.num_entries != this->entry_dimension) {
|
|
|
|
for (long long i = 0; i < this->num_rows; i++) {
|
|
|
|
n.err = true;
|
|
|
|
for (long long j = 0; j < this->num_rows; j++) {
|
|
|
|
return n;
|
|
|
|
n[i][j] = this->get_row(i) * m.get_column(j);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (long long i = 0; i < this->entry_dimension; i++) {
|
|
|
|
|
|
|
|
for (long long j = 0; j < this->entry_dimension; j++) {
|
|
|
|
|
|
|
|
n[i][j] = this->get_entry(i) * m.get_row(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -185,22 +186,28 @@ public:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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_entries, this->entry_dimension);
|
|
|
|
for (long long i = 0; i < this->num_cols; i++) {
|
|
|
|
for (long long i = 0; i < this->num_entries; i++) {
|
|
|
|
n[i] = this->cols[i] + m[i];
|
|
|
|
n[i] = this->entries[i] + m[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return n;
|
|
|
|
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_entries, this->entry_dimension);
|
|
|
|
for (long long i = 0; i < this->num_cols; i++) {
|
|
|
|
for (long long i = 0; i < this->num_entries; i++) {
|
|
|
|
n[i] = this->cols[i] - m[i];
|
|
|
|
n[i] = this->entries[i] - m[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return n;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// FIXME: Figure out how to make sure you do not try to access something
|
|
|
|
void operator=(const matrix &m) {
|
|
|
|
// outside of the index
|
|
|
|
this->num_entries = m.num_entries;
|
|
|
|
vector &operator[](const long long index) { return this->cols[index]; }
|
|
|
|
this->entry_dimension = m.entry_dimension;
|
|
|
|
|
|
|
|
this->err = m.err;
|
|
|
|
|
|
|
|
free(this->entries);
|
|
|
|
|
|
|
|
this->entries = (vector *)malloc(sizeof(vector) * m.num_entries);
|
|
|
|
|
|
|
|
for (long long i = 0; i < m.num_entries; i++)
|
|
|
|
|
|
|
|
this->entries[i] = m[i];
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|