From 4309ed1e11f72f5b629f6ffd142f356c51ee6219 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Mon, 18 Jul 2022 22:09:28 +0200 Subject: [PATCH] Move things dependent on matrix out of line --- cnumber.cpp | 23 -------------------- fractions.cpp | 29 ------------------------- matrix.cpp | 60 --------------------------------------------------- matrix.hpp | 2 ++ vector.cpp | 53 ++++++++++----------------------------------- vector.hpp | 4 +++- 6 files changed, 16 insertions(+), 155 deletions(-) delete mode 100644 cnumber.cpp delete mode 100644 fractions.cpp delete mode 100644 matrix.cpp diff --git a/cnumber.cpp b/cnumber.cpp deleted file mode 100644 index 8311fce..0000000 --- a/cnumber.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include "cnumber.hpp" - - -int main() { - cnumber a(3,2); - cnumber b(4,-3); - - cout << "a = " << a << endl; - cout << "a* = " << a.conjugate() << endl; - cout << "a*a* = " << a * a.conjugate() << endl; - cout << "b = " << b << endl; - cout << "b* = " << b.conjugate() << endl; - cout << "b*b* = " << b * b.conjugate() << endl; - cout << "a + b = " << a + b << endl; - cout << "(a + b)* = " << (a + b).conjugate() << endl; - cout << "a - b = " << a - b << endl; - cout << "(a - b)* = " << (a - b).conjugate() << endl; - cout << "a * b = " << a * b << endl; - cout << "(a * b)* = " << (a * b).conjugate() << endl; - cout << "a / b = " << a / b << endl; - cout << "(a / b)* = " << (a / b).conjugate() << endl; -} diff --git a/fractions.cpp b/fractions.cpp deleted file mode 100644 index ea490af..0000000 --- a/fractions.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include "fractions.hpp" - - -int main() { - int a = 7; - int b = 3; - int c = 1; - int d = 11; - double e = 0.75; - fraction q(a,b); - fraction q2(c,d); - fraction q3(e); - cout.precision(10); - cout << a << "/" << b << '=' << q << endl; - cout << a << "/" << b << '=' << q.to_double() << endl; - cout << c << "/" << d << '=' << q2 << endl; - cout << c << "/" << d << '=' << q2.to_double() << endl; - cout << q << "+" << q2 << '=' << q + q2 << endl; - cout << q << "+" << q2 << '=' << (q + q2).to_double() << endl; - cout << q << "-" << q2 << '=' << q - q2 << endl; - cout << q << "-" << q2 << '=' << (q - q2).to_double() << endl; - cout << q << "*" << q2 << '=' << q * q2 << endl; - cout << q << "*" << q2 << '=' << (q * q2).to_double() << endl; - cout << q << "/" << q2 << '=' << q / q2 << endl; - cout << q << "/" << q2 << '=' << (q / q2).to_double() << endl; - cout << e << '=' << q3 << endl; - return 0; -} diff --git a/matrix.cpp b/matrix.cpp deleted file mode 100644 index a616ff1..0000000 --- a/matrix.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "matrix.hpp" - -int main() { - vector a = vector(2); - a[0] = 1; - a[1] = 1; - - vector b = vector(2); - b[0] = 1; - b[1] = cnumber(1, 0); - - vector c = vector(2); - c[0] = 2; - c[1] = cnumber(1, 1); - - vector d = vector(2); - d[0] = cnumber(1, -1); - d[1] = 5; - - matrix m = matrix(2, 2); - m[0] = a; - m[1] = b; - - matrix n = matrix(2, 2); - n[0] = c; - n[1] = d; - - cout << "The matrix m" << endl; - cout << m << endl; - cout << "The determinant of matrix m:" << endl; - cout << m.determinant() << endl; - cout << "The matrix n:" << endl; - cout << n << endl; - cout << "The matrix n roted by one pi radian:" << endl; - cout << n.rotate_by_one_pi() << endl; - cout << "The determinant of matrix n:" << endl; - cout << n.determinant() << endl; - cout << "The matrix n's transpose:" << endl; - cout << n.transpose() << endl; - cout << "The matrix n's diagonal:" << endl; - cout << n.get_diagonal() << endl; - cout << "The matrix n's off diagonal:" << endl; - cout << n.get_off_diagonal() << endl; - cout << "The matrix m * n" << endl; - cout << m * n << endl; - cout << "The matrix m + n" << endl; - cout << m + n << endl; - cout << "Is 2 an eigenvalue of m?" << endl; - if (m.is_eigenvalue(2)) { - cout << "Yes!" << endl; - } else { - cout << "No!" << endl; - } - if (n.is_hermitian()) { - cout << "The matrix n is hermitian, here is the hermitian conjugate:" - << endl; - cout << n.hermitian_conjugate() << endl; - } - return 0; -} diff --git a/matrix.hpp b/matrix.hpp index 62f96cd..1ab4715 100644 --- a/matrix.hpp +++ b/matrix.hpp @@ -37,6 +37,8 @@ public: this->entries = NULL; this->err = true; } + const long long get_num_entries() const { return this->num_entries; } + const long long get_entry_dimension() const { return this->entry_dimension; } const cnumber determinant() const { cnumber dsum(1, 0); cnumber osum(1, 0); diff --git a/vector.cpp b/vector.cpp index 609021d..26ca99e 100644 --- a/vector.cpp +++ b/vector.cpp @@ -1,45 +1,14 @@ #include "vector.hpp" -#include -using namespace std; -int main() { - vector v = vector(3); - cnumber one(1, 0); - cnumber two(2, 0); - cnumber three(3, 0); - cnumber four(4, 0); - cnumber five(5, 0); - cnumber i(0, 1); - v[0] = one; - v[1] = two; - v[2] = i; - - vector w = vector(3); - w[0] = three; - w[1] = four; - w[2] = five; - cout << "v:" << endl; - cout << v << endl; - cout << "w:" << endl; - cout << w << endl; - cout << "v + w:" << endl; - cout << v + w << endl; - 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; +#include "matrix.hpp" +const vector vector::operator*(const matrix &m) const { + vector res(m.get_num_entries()); + for (long long i = 0; i < this->get_dimention(); i++) { + cnumber sum = 0; + for (long long j = 0; j < this->get_dimention(); j++) { + sum = sum + (m.get_entry(j)[i] * this->entries[j]); + } + res[i] = sum; } - 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; + + return res; } diff --git a/vector.hpp b/vector.hpp index a330312..4f18535 100644 --- a/vector.hpp +++ b/vector.hpp @@ -2,6 +2,7 @@ #include "cnumber.hpp" #include using namespace std; +class matrix; class vector { private: long long dimention = 0; @@ -36,7 +37,7 @@ public: this->err = true; } long long get_dimention() const { return this->dimention; } - cnumber *get_entries() const { return this->entries; } + const cnumber *get_entries() const { return this->entries; } cnumber get_entry(const long long index) const { if (index < this->get_dimention()) { return this->entries[index]; @@ -95,6 +96,7 @@ public: } return sum; } + const vector operator*(const matrix &m) const; const vector operator*(const cnumber scalar) const { vector product = vector(this->get_dimention()); for (long long i = 0; i < this->get_dimention(); i++) {