Left justify output

main
Micke Nordin 2 years ago
parent e125739a63
commit eeee3590c7
Signed by: micke
GPG Key ID: 014B273D614BE877

@ -42,27 +42,27 @@ a * b = 18-i
a / b = (6/25)+(17/25)i a / b = (6/25)+(17/25)i
(a / b)* = (6/25)-(17/25)i (a / b)* = (6/25)-(17/25)i
v: v:
| 1 | |1|
| 2 | |2|
| i | |i|
w: w:
| 3 | |3|
| 4 | |4|
| 5 | |5|
v + w: v + w:
| 4 | |4 |
| 6 | |6 |
| 5+i | |5+i|
v - w: v - w:
| -2 | |-2 |
| -2 | |-2 |
| -5+i | |-5+i|
v * w: v * w:
11+5i 11+5i
w * i: w * i:
| 3i | |3i|
| 4i | |4i|
| 5i | |5i|
Element 0 of v: Element 0 of v:
1 1
Element 1 of v: Element 1 of v:
@ -70,57 +70,57 @@ Element 1 of v:
Element 2 of v: Element 2 of v:
i i
A temp vector of dimension 3 A temp vector of dimension 3
| 0 | |0|
| 0 | |0|
| 0 | |0|
Assignment of w to k Assignment of w to k
| 3 | |3|
| 4 | |4|
| 5 | |5|
Manually set elements of k to elements of v Manually set elements of k to elements of v
| 1 | |1|
| 2 | |2|
| i | |i|
The matrix m The matrix m
| 1 | 1 | |1|1|
| 1 | 1 | |1|1|
The determinant of matrix m: The determinant of matrix m:
0 0
The matrix n: The matrix n:
| 2 | 1+i | |2 |1+i|
| 1-i | 5 | |1-i|5 |
The matrix n roted by one pi radian: The matrix n roted by one pi radian:
| 1+i | 5 | |1+i|5 |
| 2 | 1-i | |2 |1-i|
The determinant of matrix n: The determinant of matrix n:
8 8
The matrix n's transpose: The matrix n's transpose:
| 2 | 1-i | |2 |1-i|
| 1+i | 5 | |1+i|5 |
The matrix n's diagonal: The matrix n's diagonal:
| 2 | |2|
| 5 | |5|
The matrix n's off diagonal: The matrix n's off diagonal:
| 1+i | |1+i|
| 1-i | |1-i|
The vector d: The vector d:
| 1-i | |1-i|
| 5 | |5 |
The matrix n * the vector d The matrix n * the vector d
| 2-2i | 2 | |2-2i|2 |
| 5-5i | 25 | |5-5i|25 |
The vector d * the matrix n The vector d * the matrix n
| 7-7i | |7-7i|
| 27 | |27 |
The matrix m * n The matrix m * n
| 3-i | 6+i | |3-i|6+i|
| 3-i | 6+i | |3-i|6+i|
The matrix m + n The matrix m + n
| 3 | 2+i | |3 |2+i|
| 2-i | 6 | |2-i|6 |
Is 2 an eigenvalue of m? Is 2 an eigenvalue of m?
Yes! Yes!
The matrix n is hermitian, here is the hermitian conjugate: The matrix n is hermitian, here is the hermitian conjugate:
| 2 | 1+i | |2 |1+i|
| 1-i | 5 | |1-i|5 |
``` ```

@ -160,12 +160,27 @@ public:
} }
friend ostream &operator<<(ostream &os, const matrix &m) { friend ostream &operator<<(ostream &os, const matrix &m) {
char last = '\0'; char last = '\0';
int longest = 0;
for (long long i = 0; i < m.num_entries; i++) { for (long long i = 0; i < m.num_entries; i++) {
for (long long j = 0; j < m.entry_dimension; j++) { for (long long j = 0; j < m.entry_dimension; j++) {
ostringstream oss;
oss << m.entries[i][j];
string s = oss.str();
if (longest < s.length())
longest = s.length();
}
}
for (long long i = 0; i < m.num_entries; i++) {
for (long long j = 0; j < m.entry_dimension; j++) {
ostringstream iss;
iss << m.entries[i][j];
string s = iss.str();
int padding = longest - s.length() +1;
string symbols[3]; string symbols[3];
symbols[0] = "|"; symbols[0] = "|";
ostringstream oss; ostringstream oss;
oss << " " << m.entries[i][j] << " "; oss << m.entries[i][j] ;
oss << std::setw(padding) << "|";
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++) {
@ -206,16 +221,16 @@ public:
return n; return n;
} }
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 (long long i = 0; i < this->get_num_entries(); i++) {
for (long long j = 0; j < this->get_entry_dimension(); j++) { for (long long 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));
}
} }
}
return res; return res;
} }
const matrix operator*(const matrix m) const { const matrix operator*(const matrix m) const {
matrix n(this->num_entries, m.entry_dimension); matrix n(this->num_entries, m.entry_dimension);
if (this->num_entries != m.entry_dimension && if (this->num_entries != m.entry_dimension &&

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "cnumber.hpp" #include "cnumber.hpp"
#include <iomanip>
#include <iostream> #include <iostream>
using namespace std; using namespace std;
class matrix; class matrix;
@ -11,12 +12,12 @@ private:
public: public:
bool is_error() { return this->err; } bool is_error() { return this->err; }
vector() {}; vector(){};
vector(const long long dimention) { vector(const long long dimention) {
this->err = false; this->err = false;
this->dimention = dimention; this->dimention = dimention;
free(this->entries); free(this->entries);
this->entries = (cnumber *)malloc(sizeof(cnumber)* dimention); 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));
} }
@ -25,7 +26,7 @@ public:
this->err = false; this->err = false;
this->dimention = v.get_dimention(); this->dimention = v.get_dimention();
free(this->entries); free(this->entries);
this->entries = (cnumber *)malloc(sizeof(cnumber)* v.get_dimention()); 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));
} }
@ -33,7 +34,7 @@ public:
~vector() { ~vector() {
this->dimention = 0; this->dimention = 0;
free(this->entries); free(this->entries);
//this->entries = NULL; // this->entries = NULL;
this->err = true; this->err = true;
} }
long long get_dimention() const { return this->dimention; } long long get_dimention() const { return this->dimention; }
@ -59,8 +60,20 @@ public:
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) {
int longest = 0;
for (long long i = 0; i < v.get_dimention(); i++) { for (long long i = 0; i < v.get_dimention(); i++) {
os << "| " << v[i] << " |"; ostringstream oss;
oss << v[i];
string s = oss.str();
if (longest < s.length())
longest = s.length();
}
for (long long i = 0; i < v.get_dimention(); i++) {
ostringstream oss;
oss << v[i];
string s = oss.str();
int padding = longest - s.length() +1;
os << "|" << v[i] << std::setw(padding) << "|";
if (i != v.get_dimention() - 1) if (i != v.get_dimention() - 1)
os << endl; os << endl;
} }
@ -69,7 +82,7 @@ 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 *)malloc(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));
} }

Loading…
Cancel
Save