Compare commits

...

6 Commits

@ -10,6 +10,10 @@ private:
public: public:
// Constructor // Constructor
cnumber() {
r = 0;
i = 0;
}
cnumber(const cnumber &z) { cnumber(const cnumber &z) {
r = z.r; r = z.r;
i = z.i; i = z.i;
@ -26,6 +30,7 @@ public:
r = a; r = a;
i = b; i = b;
} }
~cnumber() {}
// Member functions // Member functions
cnumber conjugate() const { cnumber conjugate() const {
fraction a(this->r.get_n(), this->r.get_d()); fraction a(this->r.get_n(), this->r.get_d());

@ -23,7 +23,7 @@ private:
s.erase(i + 1, s.length()); s.erase(i + 1, s.length());
bool point = false; bool point = false;
int count = 0; int count = 0;
for (int i = 0; i < s.length(); i++) { for (long unsigned i = 0; i < s.length(); i++) {
if (s[i] == '.') { if (s[i] == '.') {
point = true; point = true;
} else { } else {
@ -69,10 +69,11 @@ public:
n = q.n; n = q.n;
d = q.d; d = q.d;
} }
~fraction() {}
// Member functions // Member functions
int get_sign() const { return (!(n >= 0) != !(d >= 0)) ? -1 : 1; } int get_sign() const { return (!(n >= 0) != !(d >= 0)) ? -1 : 1; }
signed long long get_n() const { return n; } signed long long get_n() const { return this->n; }
signed long long get_d() const { return d; } signed long long get_d() const { return this->d; }
double to_double() const { double to_double() const {
double dec = (double)n / (double)d; double dec = (double)n / (double)d;
return dec; return dec;
@ -132,17 +133,17 @@ public:
return os; return os;
} }
void operator=(const fraction &q) { void operator=(const fraction &q) {
n = q.n; this->n = q.n;
d = q.d; this->d = q.d;
} }
void operator=(const int i) { void operator=(const int i) {
n = (signed long long)i; this->n = (signed long long)i;
d = (signed long long)1; this->d = (signed long long)1;
} }
void operator=(const double dec) { void operator=(const double dec) {
const fraction q(dec); const fraction q(dec);
n = q.n; this->n = q.n;
d = q.d; this->d = q.d;
} }
bool operator>(const fraction &q) const { bool operator>(const fraction &q) const {
signed long long hcf = gcd(d, q.d); signed long long hcf = gcd(d, q.d);
@ -155,7 +156,7 @@ public:
return (*this > q); return (*this > q);
} }
bool operator==(const fraction &q) const { bool operator==(const fraction &q) const {
return ((n == q.n) && (d == q.d)); return ((this->n == q.n) && (this->d == q.d));
} }
bool operator==(const int i) const { bool operator==(const int i) const {
fraction q(i); fraction q(i);

@ -3,7 +3,7 @@
int main() { int main() {
vector a = vector(2); vector a = vector(2);
a[0] = cnumber(7, 0); a[0] = cnumber(7, 0);
a[1] = cnumber(6, 0); a[1] = cnumber(6, 1);
vector b = vector(2); vector b = vector(2);
b[0] = cnumber(5, 0); b[0] = cnumber(5, 0);
@ -11,12 +11,12 @@ int main() {
vector c = vector(2); vector c = vector(2);
c[0] = cnumber(2, 0); c[0] = cnumber(2, 0);
c[1] = cnumber(5, 0); c[1] = cnumber(1, 1);
vector d = vector(2); vector d = vector(2);
d[0] = cnumber(1, 0); d[0] = cnumber(1, -1);
d[1] = cnumber(0, 1); d[1] = cnumber(5, 0);
matrix m = matrix(2, 2); matrix m = matrix(2, 2);
m[0] = a; m[0] = a;
@ -28,6 +28,8 @@ int main() {
cout << "The matrix m:" << endl; cout << "The matrix m:" << endl;
cout << m << endl; cout << m << endl;
cout << "The matrix m roted by one pi radian:" << endl;
cout << m.rotate_by_one_pi() << endl;
cout << "The determinant of matrix m:" << endl; cout << "The determinant of matrix m:" << endl;
cout << m.determinant() << endl; cout << m.determinant() << endl;
cout << "The matrix m's transpose:" << endl; cout << "The matrix m's transpose:" << endl;
@ -47,8 +49,8 @@ int main() {
<< endl; << endl;
cout << m.hermitian_conjugate() << endl; cout << m.hermitian_conjugate() << endl;
} }
if (!n.is_hermitian()) { if (n.is_hermitian()) {
cout << "The matrix n is not hermitian, here is the hermitian conjugate:" cout << "The matrix n is hermitian, here is the hermitian conjugate:"
<< endl; << endl;
cout << n.hermitian_conjugate() << endl; cout << n.hermitian_conjugate() << endl;
} }

@ -6,103 +6,100 @@
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);
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 (long long 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->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);
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_entry(long long index) const { return this->entries[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];
}
}; };

@ -2,26 +2,44 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() { int main() {
vector v = vector(3); vector v = vector(3);
cnumber one(1,0); cnumber one(1, 0);
cnumber two(2,0); cnumber two(2, 0);
cnumber three(3,0); cnumber three(3, 0);
cnumber four(4,0); cnumber four(4, 0);
cnumber five(5,0); cnumber five(5, 0);
cnumber i(0,1); cnumber i(0, 1);
v[0] = one; v[0] = one;
v[1] = two; v[1] = two;
v[2] = i; v[2] = i;
vector w = vector(3); vector w = vector(3);
w[0] = three; w[0] = three;
w[1] = four; w[1] = four;
w[2] = five; w[2] = five;
cout << "v:" << endl; cout << "v:" << endl;
cout << v << endl; cout << v << endl;
cout << "w:" << endl; cout << "w:" << endl;
cout << w << endl; cout << w << endl;
cout << "v * w:" << endl; cout << "v + w:" << endl;
cout << v * w << endl; cout << v + w << endl;
return 0; cout << "v - w:" << endl;
cout << v - w << endl;
cout << "v * w:" << endl;
cout << v * w << endl;
cout << "w * i:" << endl;
cout << w * i << endl;
for (long long j = 0; j < v.get_dimention(); j++) {
cout << "Element " << j << " of v:" << endl;
cout << v[j] << endl;
}
cout << "A temp vector of dimension 3" << endl << vector(3) << endl ;
vector k = vector(3);
k = w;
cout << "Assignment of w to k" << endl << k << endl ;
cout << "Manually set elements of k to elements of v" << endl;
for (long long j = 0; j < v.get_dimention(); j++)
k[j] = v[j];
cout << k << endl;
return 0;
} }

@ -4,20 +4,27 @@
using namespace std; using namespace std;
class vector { class vector {
private: private:
long long dimention; long long dimention = 0;
cnumber *entries; cnumber *entries = NULL;
bool err = true;
public: public:
bool is_error() { return this->err; }
vector() {};
vector(const long long dimention) { vector(const long long dimention) {
this->err = false;
this->dimention = dimention; this->dimention = dimention;
this->entries = (cnumber *)calloc(sizeof(cnumber), dimention); free(this->entries);
this->entries = (cnumber *)malloc(sizeof(cnumber)* dimention);
for (long long i = 0; i < dimention; i++) { for (long long i = 0; i < dimention; i++) {
this->set_entry(i, cnumber(0, 0)); this->set_entry(i, cnumber(0, 0));
} }
} }
vector(const vector &v) { vector(const vector &v) {
this->err = false;
this->dimention = v.get_dimention(); this->dimention = v.get_dimention();
this->entries = (cnumber *)calloc(sizeof(cnumber), v.get_dimention()); free(this->entries);
this->entries = (cnumber *)malloc(sizeof(cnumber)* v.get_dimention());
for (long long i = 0; i < v.get_dimention(); i++) { for (long long i = 0; i < v.get_dimention(); i++) {
this->set_entry(i, v.get_entry(i)); this->set_entry(i, v.get_entry(i));
} }
@ -25,15 +32,16 @@ public:
~vector() { ~vector() {
this->dimention = 0; this->dimention = 0;
free(this->entries); free(this->entries);
this->entries = NULL; //this->entries = NULL;
this->err = true;
} }
long long get_dimention() const { return this->dimention; } long long get_dimention() const { return this->dimention; }
cnumber *get_entries() const { return this->entries; } cnumber *get_entries() const { return this->entries; }
cnumber get_entry(const long long index) const { cnumber get_entry(const long long index) const {
if (index < this->get_dimention()) { if (index < this->get_dimention()) {
return this->entries[index]; return this->entries[index];
} }
return cnumber(0,0); return cnumber(0, 0);
} }
void set_entry(const long long index, const cnumber z) { void set_entry(const long long index, const cnumber z) {
if (index < this->get_dimention()) { if (index < this->get_dimention()) {
@ -45,7 +53,8 @@ public:
return this->get_entry(index); return this->get_entry(index);
} }
// FIXME: Figure out how to make sure you do not try to access something outside of the index // FIXME: Figure out how to make sure you do not try to access something
// outside of the index
cnumber &operator[](const long long index) { return this->entries[index]; } cnumber &operator[](const long long index) { return this->entries[index]; }
friend ostream &operator<<(ostream &os, const vector &v) { friend ostream &operator<<(ostream &os, const vector &v) {
@ -59,10 +68,11 @@ public:
void operator=(const vector &v) { void operator=(const vector &v) {
this->dimention = v.get_dimention(); this->dimention = v.get_dimention();
free(this->entries); free(this->entries);
this->entries = (cnumber *)calloc(sizeof(cnumber), dimention); this->entries = (cnumber *)malloc(sizeof(cnumber)* dimention);
for (long long i = 0; i < v.get_dimention(); i++) { for (long long i = 0; i < v.get_dimention(); i++) {
this->set_entry(i, v.get_entry(i)); this->set_entry(i, v.get_entry(i));
} }
this->err = false;
} }
const vector operator+(const vector &v) const { const vector operator+(const vector &v) const {
if (this->get_dimention() != v.get_dimention()) { if (this->get_dimention() != v.get_dimention()) {
@ -93,7 +103,7 @@ public:
return product; return product;
} }
const cnumber operator*(const vector &v) const { const cnumber operator*(const vector &v) const {
cnumber res(0,0); cnumber res(0, 0);
for (long long i = 0; i < this->get_dimention(); i++) { for (long long i = 0; i < this->get_dimention(); i++) {
res = res + this->get_entry(i) * v.get_entry(i); res = res + this->get_entry(i) * v.get_entry(i);
} }

Loading…
Cancel
Save