|
|
@ -5,18 +5,18 @@
|
|
|
|
#include <string>
|
|
|
|
#include <string>
|
|
|
|
class matrix {
|
|
|
|
class matrix {
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
long long num_entries;
|
|
|
|
uint64_t num_entries;
|
|
|
|
long long entry_dimension;
|
|
|
|
uint64_t entry_dimension;
|
|
|
|
vector *entries;
|
|
|
|
vector *entries;
|
|
|
|
bool err;
|
|
|
|
bool err;
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
matrix(const long long num_entries, const long long entry_dimension) {
|
|
|
|
matrix(const uint64_t num_entries, const uint64_t entry_dimension) {
|
|
|
|
this->num_entries = entry_dimension;
|
|
|
|
this->num_entries = entry_dimension;
|
|
|
|
this->entry_dimension = num_entries;
|
|
|
|
this->entry_dimension = num_entries;
|
|
|
|
this->err = false;
|
|
|
|
this->err = false;
|
|
|
|
this->entries = (vector *)malloc(sizeof(vector) * this->num_entries);
|
|
|
|
this->entries = (vector *)malloc(sizeof(vector) * this->num_entries);
|
|
|
|
for (long long i = 0; i < entry_dimension; i++) {
|
|
|
|
for (uint64_t i = 0; i < entry_dimension; i++) {
|
|
|
|
this->entries[i] = vector(this->entry_dimension);
|
|
|
|
this->entries[i] = vector(this->entry_dimension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -25,7 +25,7 @@ public:
|
|
|
|
this->entry_dimension = m.entry_dimension;
|
|
|
|
this->entry_dimension = m.entry_dimension;
|
|
|
|
this->err = m.err;
|
|
|
|
this->err = m.err;
|
|
|
|
this->entries = (vector *)malloc(sizeof(vector) * m.num_entries);
|
|
|
|
this->entries = (vector *)malloc(sizeof(vector) * m.num_entries);
|
|
|
|
for (long long i = 0; i < m.num_entries; i++) {
|
|
|
|
for (uint64_t i = 0; i < m.num_entries; i++) {
|
|
|
|
this->entries[i] = m[i];
|
|
|
|
this->entries[i] = m[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -36,27 +36,27 @@ public:
|
|
|
|
this->entries = NULL;
|
|
|
|
this->entries = NULL;
|
|
|
|
this->err = true;
|
|
|
|
this->err = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const long long get_num_entries() const { return this->num_entries; }
|
|
|
|
const uint64_t get_num_entries() const { return this->num_entries; }
|
|
|
|
const long long get_entry_dimension() const { return this->entry_dimension; }
|
|
|
|
const uint64_t get_entry_dimension() const { return this->entry_dimension; }
|
|
|
|
const cnumber determinant() const {
|
|
|
|
const cnumber determinant() const {
|
|
|
|
cnumber dsum(1, 0);
|
|
|
|
cnumber dsum(1, 0);
|
|
|
|
cnumber osum(1, 0);
|
|
|
|
cnumber osum(1, 0);
|
|
|
|
vector diag = this->get_diagonal();
|
|
|
|
vector diag = this->get_diagonal();
|
|
|
|
vector odiag = this->get_off_diagonal();
|
|
|
|
vector odiag = this->get_off_diagonal();
|
|
|
|
for (long long i = 0; i < diag.get_dimention(); i++) {
|
|
|
|
for (uint64_t i = 0; i < diag.get_dimention(); i++) {
|
|
|
|
dsum = dsum * diag[i];
|
|
|
|
dsum = dsum * diag[i];
|
|
|
|
osum = osum * odiag[i];
|
|
|
|
osum = osum * odiag[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dsum - osum;
|
|
|
|
return dsum - osum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const vector get_diagonal() const {
|
|
|
|
const vector get_diagonal() const {
|
|
|
|
long long diag_len = this->entry_dimension;
|
|
|
|
uint64_t diag_len = this->entry_dimension;
|
|
|
|
if (this->num_entries < diag_len) {
|
|
|
|
if (this->num_entries < diag_len) {
|
|
|
|
diag_len = this->num_entries;
|
|
|
|
diag_len = this->num_entries;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vector v(diag_len);
|
|
|
|
vector v(diag_len);
|
|
|
|
for (long long i = 0; i < this->num_entries; i++) {
|
|
|
|
for (uint64_t i = 0; i < this->num_entries; i++) {
|
|
|
|
for (long long j = 0; j < this->entry_dimension; j++) {
|
|
|
|
for (uint64_t j = 0; j < this->entry_dimension; j++) {
|
|
|
|
if (i == j) {
|
|
|
|
if (i == j) {
|
|
|
|
v[i] = this->entries[i][j];
|
|
|
|
v[i] = this->entries[i][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -65,22 +65,33 @@ public:
|
|
|
|
return v;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const vector get_off_diagonal() const {
|
|
|
|
const vector get_off_diagonal() const {
|
|
|
|
return this->rotate_by_one_pi().get_diagonal();
|
|
|
|
return this->rotate().get_diagonal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const vector get_entry(long long index) const { return this->entries[index]; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const vector get_row(long long index) const {
|
|
|
|
const matrix get_echelon() const {
|
|
|
|
|
|
|
|
matrix m = *this;
|
|
|
|
|
|
|
|
if (!m.is_invertible()) {
|
|
|
|
|
|
|
|
m.err = true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
for (uint64_t i = 0; i < m.get_num_entries(); i++) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return m;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const vector get_entry(uint64_t index) const { return this->entries[index]; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const vector get_row(uint64_t index) const {
|
|
|
|
vector v(this->entry_dimension);
|
|
|
|
vector v(this->entry_dimension);
|
|
|
|
for (long long j = 0; j < this->entry_dimension; j++) {
|
|
|
|
for (uint64_t j = 0; j < this->entry_dimension; j++) {
|
|
|
|
v[j] = this->entries[j][index];
|
|
|
|
v[j] = this->entries[j][index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const matrix rotate_by_one_pi() const {
|
|
|
|
const matrix rotate() const {
|
|
|
|
matrix m = this->transpose();
|
|
|
|
matrix m = this->transpose();
|
|
|
|
matrix n = matrix(m.entry_dimension, m.num_entries);
|
|
|
|
matrix n = matrix(m.entry_dimension, m.num_entries);
|
|
|
|
for (long long i = 0; i < m.entry_dimension; i++) {
|
|
|
|
for (uint64_t i = 0; i < m.entry_dimension; i++) {
|
|
|
|
int index = m.num_entries - i - 1;
|
|
|
|
int index = m.num_entries - i - 1;
|
|
|
|
n[i] = m[index];
|
|
|
|
n[i] = m[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -88,8 +99,8 @@ public:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const matrix transpose() const {
|
|
|
|
const matrix transpose() const {
|
|
|
|
matrix n = matrix(this->entry_dimension, this->num_entries);
|
|
|
|
matrix n = matrix(this->entry_dimension, this->num_entries);
|
|
|
|
for (long long i = 0; i < this->num_entries; i++) {
|
|
|
|
for (uint64_t i = 0; i < this->num_entries; i++) {
|
|
|
|
for (long long j = 0; j < this->entry_dimension; j++) {
|
|
|
|
for (uint64_t j = 0; j < this->entry_dimension; j++) {
|
|
|
|
n[j][i] = this->entries[i][j];
|
|
|
|
n[j][i] = this->entries[i][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -98,8 +109,8 @@ public:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const matrix conjugate() const {
|
|
|
|
const matrix conjugate() const {
|
|
|
|
matrix n = matrix(this->num_entries, this->entry_dimension);
|
|
|
|
matrix n = matrix(this->num_entries, this->entry_dimension);
|
|
|
|
for (long long i = 0; i < this->num_entries; i++) {
|
|
|
|
for (uint64_t i = 0; i < this->num_entries; i++) {
|
|
|
|
for (long long j = 0; j < this->entry_dimension; j++) {
|
|
|
|
for (uint64_t j = 0; j < this->entry_dimension; j++) {
|
|
|
|
n[i][j] = this->entries[i][j].conjugate();
|
|
|
|
n[i][j] = this->entries[i][j].conjugate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -111,8 +122,8 @@ public:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const matrix I() const {
|
|
|
|
const matrix I() const {
|
|
|
|
matrix m = matrix(this->num_entries, this->entry_dimension);
|
|
|
|
matrix m = matrix(this->num_entries, this->entry_dimension);
|
|
|
|
for (long long i = 0; i < this->num_entries; i++) {
|
|
|
|
for (uint64_t i = 0; i < this->num_entries; i++) {
|
|
|
|
for (long long j = 0; j < this->entry_dimension; j++) {
|
|
|
|
for (uint64_t j = 0; j < this->entry_dimension; j++) {
|
|
|
|
if (i == j) {
|
|
|
|
if (i == j) {
|
|
|
|
m[i][j] = cnumber(1, 0);
|
|
|
|
m[i][j] = cnumber(1, 0);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -126,8 +137,8 @@ public:
|
|
|
|
bool result = true;
|
|
|
|
bool result = true;
|
|
|
|
if (this->num_entries != this->entry_dimension)
|
|
|
|
if (this->num_entries != this->entry_dimension)
|
|
|
|
return !result;
|
|
|
|
return !result;
|
|
|
|
for (long long i = 0; i < this->num_entries; i++) {
|
|
|
|
for (uint64_t i = 0; i < this->num_entries; i++) {
|
|
|
|
for (long long j = 0; j < this->num_entries; j++) {
|
|
|
|
for (uint64_t j = 0; j < this->num_entries; j++) {
|
|
|
|
if (i != j && this->entries[i][j] != 0)
|
|
|
|
if (i != j && this->entries[i][j] != 0)
|
|
|
|
return !result;
|
|
|
|
return !result;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -155,8 +166,8 @@ public:
|
|
|
|
friend std::ostream &operator<<(std::ostream &os, const matrix &m) {
|
|
|
|
friend std::ostream &operator<<(std::ostream &os, const matrix &m) {
|
|
|
|
char last = '\0';
|
|
|
|
char last = '\0';
|
|
|
|
int longest = 0;
|
|
|
|
int longest = 0;
|
|
|
|
for (long long i = 0; i < m.num_entries; i++) {
|
|
|
|
for (uint64_t i = 0; i < m.num_entries; i++) {
|
|
|
|
for (long long j = 0; j < m.entry_dimension; j++) {
|
|
|
|
for (uint64_t j = 0; j < m.entry_dimension; j++) {
|
|
|
|
std::ostringstream oss;
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << m.entries[i][j];
|
|
|
|
oss << m.entries[i][j];
|
|
|
|
std::string s = oss.str();
|
|
|
|
std::string s = oss.str();
|
|
|
@ -164,16 +175,16 @@ public:
|
|
|
|
longest = s.length();
|
|
|
|
longest = s.length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (long long i = 0; i < m.num_entries; i++) {
|
|
|
|
for (uint64_t i = 0; i < m.num_entries; i++) {
|
|
|
|
for (long long j = 0; j < m.entry_dimension; j++) {
|
|
|
|
for (uint64_t j = 0; j < m.entry_dimension; j++) {
|
|
|
|
std::ostringstream iss;
|
|
|
|
std::ostringstream iss;
|
|
|
|
iss << m.entries[i][j];
|
|
|
|
iss << m.entries[i][j];
|
|
|
|
std::string s = iss.str();
|
|
|
|
std::string s = iss.str();
|
|
|
|
int padding = longest - s.length() +1;
|
|
|
|
int padding = longest - s.length() + 1;
|
|
|
|
std::string symbols[3];
|
|
|
|
std::string symbols[3];
|
|
|
|
symbols[0] = "|";
|
|
|
|
symbols[0] = "|";
|
|
|
|
std::ostringstream oss;
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << m.entries[i][j] ;
|
|
|
|
oss << m.entries[i][j];
|
|
|
|
oss << std::setw(padding) << "|";
|
|
|
|
oss << std::setw(padding) << "|";
|
|
|
|
symbols[1] = oss.str();
|
|
|
|
symbols[1] = oss.str();
|
|
|
|
symbols[2] = "|";
|
|
|
|
symbols[2] = "|";
|
|
|
@ -193,8 +204,8 @@ public:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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_entries; i++) {
|
|
|
|
for (uint64_t i = 0; i < this->num_entries; i++) {
|
|
|
|
for (long long j = 0; j < this->entry_dimension; j++) {
|
|
|
|
for (uint64_t j = 0; j < this->entry_dimension; j++) {
|
|
|
|
if (this->entries[i][j] != m[i][j]) {
|
|
|
|
if (this->entries[i][j] != m[i][j]) {
|
|
|
|
equal = false;
|
|
|
|
equal = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -202,13 +213,13 @@ public:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return equal;
|
|
|
|
return equal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vector &operator[](const long long index) { return this->entries[index]; }
|
|
|
|
vector &operator[](const uint64_t index) { return this->entries[index]; }
|
|
|
|
const vector operator[](const long long index) const {
|
|
|
|
const vector operator[](const uint64_t index) const {
|
|
|
|
return this->entries[index];
|
|
|
|
return this->entries[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const matrix operator*(const cnumber z) const {
|
|
|
|
const matrix operator*(const cnumber z) const {
|
|
|
|
matrix n(this->num_entries, this->entry_dimension);
|
|
|
|
matrix n(this->num_entries, this->entry_dimension);
|
|
|
|
for (long long i = 0; i < this->num_entries; i++) {
|
|
|
|
for (uint64_t i = 0; i < this->num_entries; i++) {
|
|
|
|
n[i] = this->entries[i] * z;
|
|
|
|
n[i] = this->entries[i] * z;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -217,8 +228,8 @@ public:
|
|
|
|
|
|
|
|
|
|
|
|
const matrix operator*(vector &v) const {
|
|
|
|
const matrix operator*(vector &v) const {
|
|
|
|
matrix res(this->get_num_entries(), this->get_entry_dimension());
|
|
|
|
matrix res(this->get_num_entries(), this->get_entry_dimension());
|
|
|
|
for (long long i = 0; i < this->get_num_entries(); i++) {
|
|
|
|
for (uint64_t i = 0; i < this->get_num_entries(); i++) {
|
|
|
|
for (long long j = 0; j < this->get_entry_dimension(); j++) {
|
|
|
|
for (uint64_t j = 0; j < this->get_entry_dimension(); j++) {
|
|
|
|
res[i][j] = (this->get_entry(i)[j] * v.get_entry(i));
|
|
|
|
res[i][j] = (this->get_entry(i)[j] * v.get_entry(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -232,8 +243,8 @@ public:
|
|
|
|
n.err = true;
|
|
|
|
n.err = true;
|
|
|
|
return n;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (long long i = 0; i < this->entry_dimension; i++) {
|
|
|
|
for (uint64_t i = 0; i < this->entry_dimension; i++) {
|
|
|
|
for (long long j = 0; j < this->entry_dimension; j++) {
|
|
|
|
for (uint64_t j = 0; j < this->entry_dimension; j++) {
|
|
|
|
n[i][j] = this->get_entry(i) * m.get_row(j);
|
|
|
|
n[i][j] = this->get_entry(i) * m.get_row(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -243,7 +254,7 @@ public:
|
|
|
|
|
|
|
|
|
|
|
|
const matrix operator+(const matrix &m) const {
|
|
|
|
const matrix operator+(const matrix &m) const {
|
|
|
|
matrix n(this->num_entries, this->entry_dimension);
|
|
|
|
matrix n(this->num_entries, this->entry_dimension);
|
|
|
|
for (long long i = 0; i < this->num_entries; i++) {
|
|
|
|
for (uint64_t i = 0; i < this->num_entries; i++) {
|
|
|
|
n[i] = this->entries[i] + m[i];
|
|
|
|
n[i] = this->entries[i] + m[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -251,7 +262,7 @@ public:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const matrix operator-(const matrix &m) const {
|
|
|
|
const matrix operator-(const matrix &m) const {
|
|
|
|
matrix n(this->num_entries, this->entry_dimension);
|
|
|
|
matrix n(this->num_entries, this->entry_dimension);
|
|
|
|
for (long long i = 0; i < this->num_entries; i++) {
|
|
|
|
for (uint64_t i = 0; i < this->num_entries; i++) {
|
|
|
|
n[i] = this->entries[i] - m[i];
|
|
|
|
n[i] = this->entries[i] - m[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -263,7 +274,7 @@ public:
|
|
|
|
this->err = m.err;
|
|
|
|
this->err = m.err;
|
|
|
|
free(this->entries);
|
|
|
|
free(this->entries);
|
|
|
|
this->entries = (vector *)malloc(sizeof(vector) * m.num_entries);
|
|
|
|
this->entries = (vector *)malloc(sizeof(vector) * m.num_entries);
|
|
|
|
for (long long i = 0; i < m.num_entries; i++)
|
|
|
|
for (uint64_t i = 0; i < m.num_entries; i++)
|
|
|
|
this->entries[i] = m[i];
|
|
|
|
this->entries[i] = m[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|