Compare commits
4 commits
881b0b44cf
...
c2f65038bf
Author | SHA1 | Date | |
---|---|---|---|
c2f65038bf | |||
2df8387cf4 | |||
10d74236e7 | |||
d88f108aec |
3 changed files with 102 additions and 40 deletions
|
@ -4,9 +4,9 @@
|
|||
|
||||
class fraction {
|
||||
private:
|
||||
uint64_t n = 0;
|
||||
uint64_t d = 1;
|
||||
uint64_t gcd(uint64_t a, uint64_t b) const {
|
||||
int64_t n = 0;
|
||||
int64_t d = 1;
|
||||
int64_t gcd(int64_t a, int64_t b) const {
|
||||
if (b == 0) {
|
||||
return a;
|
||||
}
|
||||
|
@ -37,19 +37,19 @@ public:
|
|||
// Constructors
|
||||
fraction() {}
|
||||
fraction(const fraction &q) {
|
||||
uint64_t hcf = gcd(q.n, q.d);
|
||||
int64_t hcf = gcd(q.n, q.d);
|
||||
n = q.n / hcf;
|
||||
d = q.d / hcf;
|
||||
}
|
||||
fraction(uint64_t a, uint64_t b) {
|
||||
uint64_t hcf = gcd(a, b);
|
||||
fraction(int64_t a, int64_t b) {
|
||||
int64_t hcf = gcd(a, b);
|
||||
n = a / hcf;
|
||||
d = b / hcf;
|
||||
}
|
||||
fraction(int a, int b) {
|
||||
uint64_t hcf = gcd(a, b);
|
||||
n = (uint64_t)a / hcf;
|
||||
d = (uint64_t)b / hcf;
|
||||
int64_t hcf = gcd(a, b);
|
||||
n = (int64_t)a / hcf;
|
||||
d = (int64_t)b / hcf;
|
||||
}
|
||||
fraction(int a) {
|
||||
fraction q(a, 1);
|
||||
|
@ -58,11 +58,11 @@ public:
|
|||
}
|
||||
fraction(double dec) {
|
||||
int precision = get_precision(dec);
|
||||
uint64_t denominator = 1;
|
||||
int64_t denominator = 1;
|
||||
for (int i = 0; i < precision; i++) {
|
||||
denominator *= 10;
|
||||
}
|
||||
uint64_t numerator = dec * denominator;
|
||||
int64_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; }
|
||||
uint64_t get_n() const { return this->n; }
|
||||
uint64_t get_d() const { return this->d; }
|
||||
int64_t get_n() const { return this->n; }
|
||||
int64_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 {
|
||||
uint64_t numerator = this->n * that.d + that.n * this->d;
|
||||
uint64_t denominator = this->d * that.d;
|
||||
int64_t numerator = this->n * that.d + that.n * this->d;
|
||||
int64_t denominator = this->d * that.d;
|
||||
fraction q(numerator, denominator);
|
||||
return q;
|
||||
}
|
||||
fraction operator-(const fraction &that) const {
|
||||
uint64_t numerator = this->n * that.d - that.n * this->d;
|
||||
uint64_t denominator = this->d * that.d;
|
||||
int64_t numerator = this->n * that.d - that.n * this->d;
|
||||
int64_t denominator = this->d * that.d;
|
||||
fraction q(numerator, denominator);
|
||||
return q;
|
||||
}
|
||||
fraction operator*(const fraction &that) const {
|
||||
uint64_t numerator = this->n * that.n;
|
||||
uint64_t denominator = this->d * that.d;
|
||||
int64_t numerator = this->n * that.n;
|
||||
int64_t denominator = this->d * that.d;
|
||||
fraction q(numerator, denominator);
|
||||
return q;
|
||||
}
|
||||
fraction operator*(const int i) const {
|
||||
uint64_t numerator = this->n * i;
|
||||
uint64_t denominator = this->d;
|
||||
int64_t numerator = this->n * i;
|
||||
int64_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) {
|
||||
uint64_t num = q.n;
|
||||
uint64_t den = q.d;
|
||||
int64_t num = q.n;
|
||||
int64_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 = (uint64_t)i;
|
||||
this->d = (uint64_t)1;
|
||||
this->n = (int64_t)i;
|
||||
this->d = (int64_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 {
|
||||
uint64_t hcf = gcd(d, q.d);
|
||||
int64_t hcf = gcd(d, q.d);
|
||||
fraction a(*this * hcf);
|
||||
fraction b(q * hcf);
|
||||
return (a.n > b.n);
|
||||
|
|
69
matrix.hpp
69
matrix.hpp
|
@ -1,5 +1,7 @@
|
|||
#pragma once
|
||||
#include "cnumber.hpp"
|
||||
#include "vector.hpp"
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
@ -9,6 +11,7 @@ private:
|
|||
uint64_t entry_dimension;
|
||||
vector *entries;
|
||||
bool err;
|
||||
bool augmented = false;
|
||||
|
||||
public:
|
||||
matrix(const uint64_t num_entries, const uint64_t entry_dimension) {
|
||||
|
@ -36,6 +39,24 @@ public:
|
|||
this->entries = NULL;
|
||||
this->err = true;
|
||||
}
|
||||
const matrix augment(vector v) const {
|
||||
matrix m = *this;
|
||||
if (v.get_dimention() != this->get_num_entries()) {
|
||||
m.err = true;
|
||||
return m;
|
||||
}
|
||||
for (uint64_t i = 0; i < m.get_num_entries(); i++) {
|
||||
vector old = m[i];
|
||||
vector n(old.get_dimention() + 1);
|
||||
for (uint64_t j = 0; j < old.get_dimention(); j++)
|
||||
n[j] = old[j];
|
||||
n[old.get_dimention()] = v[i];
|
||||
m[i] = n;
|
||||
}
|
||||
m.entry_dimension = m.entry_dimension + 1;
|
||||
m.augmented = true;
|
||||
return m;
|
||||
}
|
||||
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 {
|
||||
|
@ -73,14 +94,40 @@ public:
|
|||
if (!m.is_invertible()) {
|
||||
m.err = true;
|
||||
} else {
|
||||
for (uint64_t i = 0; i < m.get_num_entries(); i++) {
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
// switch row i with row j
|
||||
const matrix exchange_row(uint64_t i, uint64_t j) {
|
||||
matrix m = *this;
|
||||
vector v = m.get_entry(i);
|
||||
m[i] = m[j];
|
||||
m[j] = v;
|
||||
return m;
|
||||
}
|
||||
// subtract row i from row j
|
||||
const matrix subtract_row(uint64_t i, uint64_t j, cnumber multiplier = 1) {
|
||||
matrix m = *this;
|
||||
m[j] = m[j] - (m.multiply_row(i, multiplier)[i]);
|
||||
return m;
|
||||
}
|
||||
// add row i to row j
|
||||
const matrix add_row(uint64_t i, uint64_t j, cnumber multiplier = 1) {
|
||||
matrix m = *this;
|
||||
m[j] = m[j] + (m.multiply_row(i, multiplier)[i]);
|
||||
return m;
|
||||
}
|
||||
|
||||
// Multiply row by z
|
||||
const matrix multiply_row(uint64_t i, cnumber z) {
|
||||
matrix m = *this;
|
||||
m[i] = m[i] * z;
|
||||
return m;
|
||||
}
|
||||
const vector get_entry(uint64_t index) const { return this->entries[index]; }
|
||||
|
||||
const vector get_row(uint64_t index) const {
|
||||
const vector get_column(uint64_t index) const {
|
||||
vector v(this->entry_dimension);
|
||||
for (uint64_t j = 0; j < this->entry_dimension; j++) {
|
||||
v[j] = this->entries[j][index];
|
||||
|
@ -189,13 +236,15 @@ public:
|
|||
oss << std::setw(padding) << "|";
|
||||
symbols[1] = oss.str();
|
||||
symbols[2] = "|";
|
||||
for (int i = 0; i < 3; i++) {
|
||||
int len = symbols[i].length() - 1;
|
||||
char cur = symbols[i][0];
|
||||
if (cur != last) {
|
||||
os << symbols[i];
|
||||
bool print = true;
|
||||
for (int k = 0; k < 3; k++) {
|
||||
int len = symbols[k].length() - 1;
|
||||
char cur = symbols[k][0];
|
||||
if (cur != last || (m.augmented && j == m[i].get_dimention() - 1 && print)) {
|
||||
print = false;
|
||||
os << symbols[k];
|
||||
}
|
||||
last = symbols[i][len];
|
||||
last = symbols[k][len];
|
||||
}
|
||||
}
|
||||
if (i != m.num_entries - 1)
|
||||
|
@ -246,7 +295,7 @@ public:
|
|||
}
|
||||
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);
|
||||
n[i][j] = this->get_entry(i) * m.get_column(j);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
21
vector.hpp
21
vector.hpp
|
@ -8,6 +8,7 @@ private:
|
|||
uint64_t dimention = 0;
|
||||
cnumber *entries = NULL;
|
||||
bool err = true;
|
||||
bool row = false;
|
||||
|
||||
public:
|
||||
bool is_error() { return this->err; }
|
||||
|
@ -44,6 +45,8 @@ public:
|
|||
}
|
||||
return cnumber(0, 0);
|
||||
}
|
||||
void make_row() { this->row = true; }
|
||||
void make_column() { this->row = false; }
|
||||
void set_entry(const uint64_t index, const cnumber z) {
|
||||
if (index < this->get_dimention()) {
|
||||
this->entries[index] = z;
|
||||
|
@ -60,18 +63,28 @@ public:
|
|||
|
||||
friend std::ostream &operator<<(std::ostream &os, const vector &v) {
|
||||
int longest = 0;
|
||||
if (v.row) {
|
||||
os << '(';
|
||||
for (uint64_t i = 0; i < v.get_dimention(); i++) {
|
||||
os << v[i];
|
||||
if (i != v.get_dimention() - 1)
|
||||
os << ',';
|
||||
}
|
||||
os << ')';
|
||||
return os;
|
||||
}
|
||||
for (uint64_t i = 0; i < v.get_dimention(); i++) {
|
||||
std::ostringstream oss;
|
||||
std::ostringstream oss;
|
||||
oss << v[i];
|
||||
std::string s = oss.str();
|
||||
if (longest < s.length())
|
||||
longest = s.length();
|
||||
longest = s.length();
|
||||
}
|
||||
for (uint64_t i = 0; i < v.get_dimention(); i++) {
|
||||
std::ostringstream oss;
|
||||
std::ostringstream oss;
|
||||
oss << v[i];
|
||||
std::string s = oss.str();
|
||||
int padding = longest - s.length() +1;
|
||||
int padding = longest - s.length() + 1;
|
||||
os << "|" << v[i] << std::setw(padding) << "|";
|
||||
if (i != v.get_dimention() - 1)
|
||||
os << std::endl;
|
||||
|
|
Loading…
Add table
Reference in a new issue