Compare commits
No commits in common. "c2f65038bf5f8329532bc9430f7202ff602de977" and "881b0b44cf37629e84d9289977ff74818464f58a" have entirely different histories.
c2f65038bf
...
881b0b44cf
3 changed files with 40 additions and 102 deletions
|
@ -4,9 +4,9 @@
|
||||||
|
|
||||||
class fraction {
|
class fraction {
|
||||||
private:
|
private:
|
||||||
int64_t n = 0;
|
uint64_t n = 0;
|
||||||
int64_t d = 1;
|
uint64_t d = 1;
|
||||||
int64_t gcd(int64_t a, int64_t b) const {
|
uint64_t gcd(uint64_t a, uint64_t b) const {
|
||||||
if (b == 0) {
|
if (b == 0) {
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
@ -37,19 +37,19 @@ public:
|
||||||
// Constructors
|
// Constructors
|
||||||
fraction() {}
|
fraction() {}
|
||||||
fraction(const fraction &q) {
|
fraction(const fraction &q) {
|
||||||
int64_t hcf = gcd(q.n, q.d);
|
uint64_t hcf = gcd(q.n, q.d);
|
||||||
n = q.n / hcf;
|
n = q.n / hcf;
|
||||||
d = q.d / hcf;
|
d = q.d / hcf;
|
||||||
}
|
}
|
||||||
fraction(int64_t a, int64_t b) {
|
fraction(uint64_t a, uint64_t b) {
|
||||||
int64_t hcf = gcd(a, b);
|
uint64_t hcf = gcd(a, b);
|
||||||
n = a / hcf;
|
n = a / hcf;
|
||||||
d = b / hcf;
|
d = b / hcf;
|
||||||
}
|
}
|
||||||
fraction(int a, int b) {
|
fraction(int a, int b) {
|
||||||
int64_t hcf = gcd(a, b);
|
uint64_t hcf = gcd(a, b);
|
||||||
n = (int64_t)a / hcf;
|
n = (uint64_t)a / hcf;
|
||||||
d = (int64_t)b / hcf;
|
d = (uint64_t)b / hcf;
|
||||||
}
|
}
|
||||||
fraction(int a) {
|
fraction(int a) {
|
||||||
fraction q(a, 1);
|
fraction q(a, 1);
|
||||||
|
@ -58,11 +58,11 @@ public:
|
||||||
}
|
}
|
||||||
fraction(double dec) {
|
fraction(double dec) {
|
||||||
int precision = get_precision(dec);
|
int precision = get_precision(dec);
|
||||||
int64_t denominator = 1;
|
uint64_t denominator = 1;
|
||||||
for (int i = 0; i < precision; i++) {
|
for (int i = 0; i < precision; i++) {
|
||||||
denominator *= 10;
|
denominator *= 10;
|
||||||
}
|
}
|
||||||
int64_t numerator = dec * denominator;
|
uint64_t numerator = dec * denominator;
|
||||||
fraction q(numerator, denominator);
|
fraction q(numerator, denominator);
|
||||||
n = q.n;
|
n = q.n;
|
||||||
d = q.d;
|
d = q.d;
|
||||||
|
@ -70,34 +70,34 @@ public:
|
||||||
~fraction() {}
|
~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; }
|
||||||
int64_t get_n() const { return this->n; }
|
uint64_t get_n() const { return this->n; }
|
||||||
int64_t get_d() const { return this->d; }
|
uint64_t 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;
|
||||||
}
|
}
|
||||||
// Operators
|
// Operators
|
||||||
fraction operator+(const fraction &that) const {
|
fraction operator+(const fraction &that) const {
|
||||||
int64_t numerator = this->n * that.d + that.n * this->d;
|
uint64_t numerator = this->n * that.d + that.n * this->d;
|
||||||
int64_t denominator = this->d * that.d;
|
uint64_t denominator = this->d * that.d;
|
||||||
fraction q(numerator, denominator);
|
fraction q(numerator, denominator);
|
||||||
return q;
|
return q;
|
||||||
}
|
}
|
||||||
fraction operator-(const fraction &that) const {
|
fraction operator-(const fraction &that) const {
|
||||||
int64_t numerator = this->n * that.d - that.n * this->d;
|
uint64_t numerator = this->n * that.d - that.n * this->d;
|
||||||
int64_t denominator = this->d * that.d;
|
uint64_t denominator = this->d * that.d;
|
||||||
fraction q(numerator, denominator);
|
fraction q(numerator, denominator);
|
||||||
return q;
|
return q;
|
||||||
}
|
}
|
||||||
fraction operator*(const fraction &that) const {
|
fraction operator*(const fraction &that) const {
|
||||||
int64_t numerator = this->n * that.n;
|
uint64_t numerator = this->n * that.n;
|
||||||
int64_t denominator = this->d * that.d;
|
uint64_t denominator = this->d * that.d;
|
||||||
fraction q(numerator, denominator);
|
fraction q(numerator, denominator);
|
||||||
return q;
|
return q;
|
||||||
}
|
}
|
||||||
fraction operator*(const int i) const {
|
fraction operator*(const int i) const {
|
||||||
int64_t numerator = this->n * i;
|
uint64_t numerator = this->n * i;
|
||||||
int64_t denominator = this->d;
|
uint64_t denominator = this->d;
|
||||||
fraction q(numerator, denominator);
|
fraction q(numerator, denominator);
|
||||||
return q;
|
return q;
|
||||||
}
|
}
|
||||||
|
@ -107,8 +107,8 @@ public:
|
||||||
return a * b;
|
return a * b;
|
||||||
}
|
}
|
||||||
friend std::ostream &operator<<(std::ostream &os, const fraction &q) {
|
friend std::ostream &operator<<(std::ostream &os, const fraction &q) {
|
||||||
int64_t num = q.n;
|
uint64_t num = q.n;
|
||||||
int64_t den = q.d;
|
uint64_t den = q.d;
|
||||||
std::string s = "";
|
std::string s = "";
|
||||||
if (q.n < 0) {
|
if (q.n < 0) {
|
||||||
s = "-";
|
s = "-";
|
||||||
|
@ -135,8 +135,8 @@ public:
|
||||||
this->d = q.d;
|
this->d = q.d;
|
||||||
}
|
}
|
||||||
void operator=(const int i) {
|
void operator=(const int i) {
|
||||||
this->n = (int64_t)i;
|
this->n = (uint64_t)i;
|
||||||
this->d = (int64_t)1;
|
this->d = (uint64_t)1;
|
||||||
}
|
}
|
||||||
void operator=(const double dec) {
|
void operator=(const double dec) {
|
||||||
const fraction q(dec);
|
const fraction q(dec);
|
||||||
|
@ -144,7 +144,7 @@ public:
|
||||||
this->d = q.d;
|
this->d = q.d;
|
||||||
}
|
}
|
||||||
bool operator>(const fraction &q) const {
|
bool operator>(const fraction &q) const {
|
||||||
int64_t hcf = gcd(d, q.d);
|
uint64_t hcf = gcd(d, q.d);
|
||||||
fraction a(*this * hcf);
|
fraction a(*this * hcf);
|
||||||
fraction b(q * hcf);
|
fraction b(q * hcf);
|
||||||
return (a.n > b.n);
|
return (a.n > b.n);
|
||||||
|
|
67
matrix.hpp
67
matrix.hpp
|
@ -1,7 +1,5 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "cnumber.hpp"
|
|
||||||
#include "vector.hpp"
|
#include "vector.hpp"
|
||||||
#include <cstdint>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -11,7 +9,6 @@ private:
|
||||||
uint64_t entry_dimension;
|
uint64_t entry_dimension;
|
||||||
vector *entries;
|
vector *entries;
|
||||||
bool err;
|
bool err;
|
||||||
bool augmented = false;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
matrix(const uint64_t num_entries, const uint64_t entry_dimension) {
|
matrix(const uint64_t num_entries, const uint64_t entry_dimension) {
|
||||||
|
@ -39,24 +36,6 @@ public:
|
||||||
this->entries = NULL;
|
this->entries = NULL;
|
||||||
this->err = true;
|
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_num_entries() const { return this->num_entries; }
|
||||||
const uint64_t get_entry_dimension() const { return this->entry_dimension; }
|
const uint64_t get_entry_dimension() const { return this->entry_dimension; }
|
||||||
const cnumber determinant() const {
|
const cnumber determinant() const {
|
||||||
|
@ -94,40 +73,14 @@ public:
|
||||||
if (!m.is_invertible()) {
|
if (!m.is_invertible()) {
|
||||||
m.err = true;
|
m.err = true;
|
||||||
} else {
|
} 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;
|
return m;
|
||||||
}
|
}
|
||||||
const vector get_entry(uint64_t index) const { return this->entries[index]; }
|
const vector get_entry(uint64_t index) const { return this->entries[index]; }
|
||||||
|
|
||||||
const vector get_column(uint64_t index) const {
|
const vector get_row(uint64_t index) const {
|
||||||
vector v(this->entry_dimension);
|
vector v(this->entry_dimension);
|
||||||
for (uint64_t j = 0; j < this->entry_dimension; j++) {
|
for (uint64_t j = 0; j < this->entry_dimension; j++) {
|
||||||
v[j] = this->entries[j][index];
|
v[j] = this->entries[j][index];
|
||||||
|
@ -236,15 +189,13 @@ public:
|
||||||
oss << std::setw(padding) << "|";
|
oss << std::setw(padding) << "|";
|
||||||
symbols[1] = oss.str();
|
symbols[1] = oss.str();
|
||||||
symbols[2] = "|";
|
symbols[2] = "|";
|
||||||
bool print = true;
|
for (int i = 0; i < 3; i++) {
|
||||||
for (int k = 0; k < 3; k++) {
|
int len = symbols[i].length() - 1;
|
||||||
int len = symbols[k].length() - 1;
|
char cur = symbols[i][0];
|
||||||
char cur = symbols[k][0];
|
if (cur != last) {
|
||||||
if (cur != last || (m.augmented && j == m[i].get_dimention() - 1 && print)) {
|
os << symbols[i];
|
||||||
print = false;
|
|
||||||
os << symbols[k];
|
|
||||||
}
|
}
|
||||||
last = symbols[k][len];
|
last = symbols[i][len];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (i != m.num_entries - 1)
|
if (i != m.num_entries - 1)
|
||||||
|
@ -295,7 +246,7 @@ public:
|
||||||
}
|
}
|
||||||
for (uint64_t i = 0; i < this->entry_dimension; i++) {
|
for (uint64_t i = 0; i < this->entry_dimension; i++) {
|
||||||
for (uint64_t j = 0; j < this->entry_dimension; j++) {
|
for (uint64_t j = 0; j < this->entry_dimension; j++) {
|
||||||
n[i][j] = this->get_entry(i) * m.get_column(j);
|
n[i][j] = this->get_entry(i) * m.get_row(j);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
15
vector.hpp
15
vector.hpp
|
@ -8,7 +8,6 @@ private:
|
||||||
uint64_t dimention = 0;
|
uint64_t dimention = 0;
|
||||||
cnumber *entries = NULL;
|
cnumber *entries = NULL;
|
||||||
bool err = true;
|
bool err = true;
|
||||||
bool row = false;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool is_error() { return this->err; }
|
bool is_error() { return this->err; }
|
||||||
|
@ -45,8 +44,6 @@ public:
|
||||||
}
|
}
|
||||||
return cnumber(0, 0);
|
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) {
|
void set_entry(const uint64_t index, const cnumber z) {
|
||||||
if (index < this->get_dimention()) {
|
if (index < this->get_dimention()) {
|
||||||
this->entries[index] = z;
|
this->entries[index] = z;
|
||||||
|
@ -63,16 +60,6 @@ public:
|
||||||
|
|
||||||
friend std::ostream &operator<<(std::ostream &os, const vector &v) {
|
friend std::ostream &operator<<(std::ostream &os, const vector &v) {
|
||||||
int longest = 0;
|
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++) {
|
for (uint64_t i = 0; i < v.get_dimention(); i++) {
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << v[i];
|
oss << v[i];
|
||||||
|
@ -84,7 +71,7 @@ public:
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << v[i];
|
oss << v[i];
|
||||||
std::string s = oss.str();
|
std::string s = oss.str();
|
||||||
int padding = longest - s.length() + 1;
|
int padding = longest - s.length() +1;
|
||||||
os << "|" << v[i] << std::setw(padding) << "|";
|
os << "|" << v[i] << std::setw(padding) << "|";
|
||||||
if (i != v.get_dimention() - 1)
|
if (i != v.get_dimention() - 1)
|
||||||
os << std::endl;
|
os << std::endl;
|
||||||
|
|
Loading…
Add table
Reference in a new issue