All of these should be uint64_t

main
Micke Nordin 2 years ago
parent 24c8888bd3
commit 51b074152a
Signed by: micke
GPG Key ID: 014B273D614BE877

@ -4,9 +4,9 @@
class fraction {
private:
signed long long n = 0;
signed long long d = 1;
signed long long gcd(signed long long a, signed long long b) const {
uint64_t n = 0;
uint64_t d = 1;
uint64_t gcd(uint64_t a, uint64_t b) const {
if (b == 0) {
return a;
}
@ -37,19 +37,19 @@ public:
// Constructors
fraction() {}
fraction(const fraction &q) {
signed long long hcf = gcd(q.n, q.d);
uint64_t hcf = gcd(q.n, q.d);
n = q.n / hcf;
d = q.d / hcf;
}
fraction(signed long long a, signed long long b) {
signed long long hcf = gcd(a, b);
fraction(uint64_t a, uint64_t b) {
uint64_t hcf = gcd(a, b);
n = a / hcf;
d = b / hcf;
}
fraction(int a, int b) {
signed long long hcf = gcd(a, b);
n = (signed long long)a / hcf;
d = (signed long long)b / hcf;
uint64_t hcf = gcd(a, b);
n = (uint64_t)a / hcf;
d = (uint64_t)b / hcf;
}
fraction(int a) {
fraction q(a, 1);
@ -58,11 +58,11 @@ public:
}
fraction(double dec) {
int precision = get_precision(dec);
signed long long denominator = 1;
uint64_t denominator = 1;
for (int i = 0; i < precision; i++) {
denominator *= 10;
}
signed long long numerator = dec * denominator;
uint64_t numerator = dec * denominator;
fraction q(numerator, denominator);
n = q.n;
d = q.d;
@ -70,34 +70,34 @@ public:
~fraction() {}
// Member functions
int get_sign() const { return (!(n >= 0) != !(d >= 0)) ? -1 : 1; }
signed long long get_n() const { return this->n; }
signed long long get_d() const { return this->d; }
uint64_t get_n() const { return this->n; }
uint64_t get_d() const { return this->d; }
double to_double() const {
double dec = (double)n / (double)d;
return dec;
}
// Operators
fraction operator+(const fraction &that) const {
signed long long numerator = this->n * that.d + that.n * this->d;
signed long long denominator = this->d * that.d;
uint64_t numerator = this->n * that.d + that.n * this->d;
uint64_t denominator = this->d * that.d;
fraction q(numerator, denominator);
return q;
}
fraction operator-(const fraction &that) const {
signed long long numerator = this->n * that.d - that.n * this->d;
signed long long denominator = this->d * that.d;
uint64_t numerator = this->n * that.d - that.n * this->d;
uint64_t denominator = this->d * that.d;
fraction q(numerator, denominator);
return q;
}
fraction operator*(const fraction &that) const {
signed long long numerator = this->n * that.n;
signed long long denominator = this->d * that.d;
uint64_t numerator = this->n * that.n;
uint64_t denominator = this->d * that.d;
fraction q(numerator, denominator);
return q;
}
fraction operator*(const int i) const {
signed long long numerator = this->n * i;
signed long long denominator = this->d;
uint64_t numerator = this->n * i;
uint64_t denominator = this->d;
fraction q(numerator, denominator);
return q;
}
@ -107,8 +107,8 @@ public:
return a * b;
}
friend std::ostream &operator<<(std::ostream &os, const fraction &q) {
signed long long num = q.n;
signed long long den = q.d;
uint64_t num = q.n;
uint64_t den = q.d;
std::string s = "";
if (q.n < 0) {
s = "-";
@ -135,8 +135,8 @@ public:
this->d = q.d;
}
void operator=(const int i) {
this->n = (signed long long)i;
this->d = (signed long long)1;
this->n = (uint64_t)i;
this->d = (uint64_t)1;
}
void operator=(const double dec) {
const fraction q(dec);
@ -144,7 +144,7 @@ public:
this->d = q.d;
}
bool operator>(const fraction &q) const {
signed long long hcf = gcd(d, q.d);
uint64_t hcf = gcd(d, q.d);
fraction a(*this * hcf);
fraction b(q * hcf);
return (a.n > b.n);

@ -140,7 +140,7 @@ void vector_main() {
std::cout << v * w << std::endl;
std::cout << "w * i:" << std::endl;
std::cout << w * i << std::endl;
for (long long j = 0; j < v.get_dimention(); j++) {
for (uint64_t j = 0; j < v.get_dimention(); j++) {
std::cout << "Element " << j << " of v:" << std::endl;
std::cout << v[j] << std::endl;
}
@ -149,7 +149,7 @@ void vector_main() {
k = w;
std::cout << "Assignment of w to k" << std::endl << k << std::endl;
std::cout << "Manually set elements of k to elements of v" << std::endl;
for (long long j = 0; j < v.get_dimention(); j++)
for (uint64_t j = 0; j < v.get_dimention(); j++)
k[j] = v[j];
std::cout << k << std::endl;
}

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

@ -2,9 +2,9 @@
#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++) {
for (uint64_t i = 0; i < this->get_dimention(); i++) {
cnumber sum = 0;
for (long long j = 0; j < this->get_dimention(); j++) {
for (uint64_t j = 0; j < this->get_dimention(); j++) {
sum = sum + (m.get_entry(j)[i] * this->entries[j]);
}
res[i] = sum;

@ -5,19 +5,19 @@
class matrix;
class vector {
private:
long long dimention = 0;
uint64_t dimention = 0;
cnumber *entries = NULL;
bool err = true;
public:
bool is_error() { return this->err; }
vector(){};
vector(const long long dimention) {
vector(const uint64_t dimention) {
this->err = false;
this->dimention = dimention;
free(this->entries);
this->entries = (cnumber *)malloc(sizeof(cnumber) * dimention);
for (long long i = 0; i < dimention; i++) {
for (uint64_t i = 0; i < dimention; i++) {
this->set_entry(i, cnumber(0, 0));
}
}
@ -26,7 +26,7 @@ public:
this->dimention = 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 (uint64_t i = 0; i < v.get_dimention(); i++) {
this->set_entry(i, v.get_entry(i));
}
}
@ -36,38 +36,38 @@ public:
// this->entries = NULL;
this->err = true;
}
long long get_dimention() const { return this->dimention; }
uint64_t get_dimention() const { return this->dimention; }
const cnumber *get_entries() const { return this->entries; }
cnumber get_entry(const long long index) const {
cnumber get_entry(const uint64_t index) const {
if (index < this->get_dimention()) {
return this->entries[index];
}
return cnumber(0, 0);
}
void set_entry(const long long index, const cnumber z) {
void set_entry(const uint64_t index, const cnumber z) {
if (index < this->get_dimention()) {
this->entries[index] = z;
}
}
const cnumber operator[](const long long index) const {
const cnumber operator[](const uint64_t index) const {
return this->get_entry(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 uint64_t index) { return this->entries[index]; }
friend std::ostream &operator<<(std::ostream &os, const vector &v) {
int longest = 0;
for (long long i = 0; i < v.get_dimention(); i++) {
for (uint64_t i = 0; i < v.get_dimention(); i++) {
std::ostringstream oss;
oss << v[i];
std::string s = oss.str();
if (longest < s.length())
longest = s.length();
}
for (long long i = 0; i < v.get_dimention(); i++) {
for (uint64_t i = 0; i < v.get_dimention(); i++) {
std::ostringstream oss;
oss << v[i];
std::string s = oss.str();
@ -82,7 +82,7 @@ public:
this->dimention = v.get_dimention();
free(this->entries);
this->entries = (cnumber *)malloc(sizeof(cnumber) * dimention);
for (long long i = 0; i < v.get_dimention(); i++) {
for (uint64_t i = 0; i < v.get_dimention(); i++) {
this->set_entry(i, v.get_entry(i));
}
this->err = false;
@ -92,7 +92,7 @@ public:
return vector(0);
}
vector sum = vector(v.get_dimention());
for (long long i = 0; i < v.get_dimention(); i++) {
for (uint64_t i = 0; i < v.get_dimention(); i++) {
sum[i] = this->get_entry(i) + v[i];
}
return sum;
@ -103,7 +103,7 @@ public:
return vector(0);
}
vector sum = vector(v.get_dimention());
for (long long i = 0; i < v.get_dimention(); i++) {
for (uint64_t i = 0; i < v.get_dimention(); i++) {
sum[i] = this->entries[i] - v[i];
}
return sum;
@ -111,14 +111,14 @@ public:
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++) {
for (uint64_t i = 0; i < this->get_dimention(); i++) {
product[i] = this->entries[i] * scalar;
}
return product;
}
const cnumber operator*(const vector &v) const {
cnumber res(0, 0);
for (long long i = 0; i < this->get_dimention(); i++) {
for (uint64_t i = 0; i < this->get_dimention(); i++) {
res = res + this->get_entry(i) * v.get_entry(i);
}
return res;

Loading…
Cancel
Save