Compare commits

...

44 commits

Author SHA1 Message Date
875109a6d6
Merge matrices 2022-07-16 17:41:47 +02:00
0fd9da4637
Merge vectors 2022-07-16 17:40:26 +02:00
999dcb96c7
Merge cnumbers 2022-07-16 17:39:13 +02:00
054a9d67aa
Merge fractions 2022-07-16 17:36:36 +02:00
330f4d289d
Now with matrix multiplication 2022-07-16 17:30:59 +02:00
408a97e93e
Add dot product 2022-07-16 16:54:10 +02:00
Micke Nordin
8a7399df48 Update 'README.md' 2022-07-16 12:12:37 +00:00
6474a64f96
Add conjugation and transposition 2022-07-16 14:10:50 +02:00
e5cd0fc7d1
Add equality operators 2022-07-16 13:51:57 +02:00
6db1b1e943
Print zeros 2022-07-16 13:02:27 +02:00
aec0995aaf
Indentation 2022-07-16 12:58:57 +02:00
c558dacb46
An example with 3x3 matrix 2022-07-16 12:58:25 +02:00
8e5bc40d6d
Start of matrices 2022-07-16 12:53:07 +02:00
ae46252c3e
Fix indentation 2022-07-16 12:24:32 +02:00
8dda4fe71b
Pragma once 2022-07-16 11:55:14 +02:00
1f1a384c35
Pragman once 2022-07-16 11:54:12 +02:00
afa1112872
Working vector, sort of 2022-07-16 10:47:29 +02:00
d291f7a330
Add copy constructor 2022-07-16 09:37:03 +02:00
Micke Nordin
acec9346eb Initial commit 2022-07-15 09:15:19 +00:00
Micke Nordin
b26517584c Initial commit 2022-07-15 09:08:22 +00:00
Mikael Nordin
b9e3db439f
Update README.md 2020-04-19 23:03:28 +02:00
Mikael Nordin
9b84ecf85c
Update README.md 2020-04-19 23:02:13 +02:00
Mikael Nordin
769df79c25
Update README.md 2020-04-19 23:01:08 +02:00
797347f442 Simplyfy a bit 2020-04-19 22:56:30 +02:00
6924ce6031 Fix output and mov sign out front 2020-04-19 22:54:13 +02:00
3849d1b833 Merge branch 'master' of https://github.com/mickenordin/fractions 2020-04-19 22:21:45 +02:00
ef7fbc5ae8 Switch to long long 2020-04-19 22:21:13 +02:00
Mikael Nordin
8fbe2e2950
Update README.md 2020-04-18 17:17:18 +02:00
2590da865a Use my own fractions 2020-04-18 16:57:06 +02:00
Mikael Nordin
b6c090f411
Update README.md 2020-04-18 16:55:33 +02:00
Mikael Nordin
641c03b0a4
Update README.md 2020-04-18 16:54:54 +02:00
d534e766a3 Update readme 2020-04-18 16:51:02 +02:00
b914c09fa6 Use my own fractions instead 2020-04-18 16:49:11 +02:00
75d8d7a152 Much better gcd, actual Euclids algorithm 2020-04-18 16:43:24 +02:00
3878684f85 More operators and better output 2020-04-18 15:43:41 +02:00
Micke Nordin
a5c5b1e955 First implemention of fraction 2020-04-18 13:22:02 +02:00
Micke Nordin
6cba84bc5a first commit 2020-04-18 12:32:24 +02:00
23d1ea1faa Add division 2020-04-18 11:16:20 +02:00
523f2fc0b6 Add subtraction 2020-04-18 10:37:53 +02:00
8892c7851f Better output for << operator 2020-04-18 10:25:05 +02:00
bdc98ba3c2 Complex conjugation 2016-06-18 21:11:21 +02:00
b2b1733fec Also multiplication 2016-06-18 20:21:18 +02:00
0f2a7c7536 more operators 2016-06-18 19:41:57 +02:00
271d186d27 First draft of complex number 2016-06-18 19:15:56 +02:00
9 changed files with 727 additions and 0 deletions

View file

@ -1,2 +1,58 @@
<<<<<<< HEAD
# mmathlib # mmathlib
**Compile and run**
```bash
git clone https://code.smolnet.org/micke/mmathlib.git
cd mmathlib
g++ -o fractions fractions.cpp
./fractions
g++ -o cnumber cnumber.cpp
./cnumber
g++ -o matrix matrix.cpp
./matrix
```
**Output**
```
7/3=(7/3)
7/3=2.333333333
1/11=(1/11)
1/11=0.09090909091
(7/3)+(1/11)=(80/33)
(7/3)+(1/11)=2.424242424
(7/3)-(1/11)=(74/33)
(7/3)-(1/11)=2.242424242
(7/3)*(1/11)=(7/33)
(7/3)*(1/11)=0.2121212121
(7/3)/(1/11)=(77/3)
(7/3)/(1/11)=25.66666667
0.75=(3/4)
a = 3+2i
a* = 3-2i
a*a* = 13
b = 4-3i
b* = 4+3i
b*b* = 25
a + b = 7-i
(a + b)* = 7+i
a - b = -1+5i
(a - b)* = -1-5i
a * b = 18-i
(a * b)* = 18+i
a / b = (6/25)+(17/25)i
(a / b)* = (6/25)-(17/25)i
The matrix m:
| 7 | 5 | i |
| 0 | 2 | 0 |
| -i | 0 | 4 |
The matrix m's transpose:
| 7 | 0 | -i |
| 5 | 2 | 0 |
| i | 0 | 4 |
The matrix m is not hermitian, here is the hermitian conjugate:
| 7 | 0 | i |
| 5 | 2 | 0 |
| -i | 0 | 4 |
```

23
cnumber.cpp Normal file
View file

@ -0,0 +1,23 @@
#include <iostream>
#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;
}

90
cnumber.hpp Normal file
View file

@ -0,0 +1,90 @@
#pragma once
#include "../fractions/fractions.hpp"
#include <iostream>
using namespace std;
class cnumber {
private:
fraction r, i;
public:
// Constructor
cnumber(const cnumber &z) {
r = z.r;
i = z.i;
}
cnumber(const fraction &a, const fraction &b) {
r = a;
i = b;
}
cnumber(int a, int b) {
r = a;
i = b;
}
cnumber(double a, double b) {
r = a;
i = b;
}
// Member functions
cnumber conjugate() const {
fraction a(this->r.get_n(), this->r.get_d());
fraction b(this->i.get_n(), this->i.get_d());
cnumber z(a, b * -1);
return z;
}
// Operators
cnumber operator+(const cnumber &that) const {
cnumber z(this->r + that.r, this->i + that.i);
return z;
}
cnumber operator-(const cnumber &that) const {
cnumber z(this->r - that.r, this->i - that.i);
return z;
}
cnumber operator*(const cnumber &that) const {
cnumber z((that.r * this->r) - (that.i * this->i),
(that.r * this->i) + (this->r * that.i));
return z;
}
cnumber operator*(const fraction &q) const {
cnumber that(q, fraction(0));
return *this * that;
}
cnumber operator/(const cnumber &that) const {
cnumber numerator((this->r * that.r) - (this->i * (that.i * -1)),
(this->r * (that.i * -1)) + (that.r * this->i));
cnumber denominator((that.r * that.r) - (that.i * (that.i * -1)),
(that.r * (that.i * -1)) + (that.r * that.i));
cnumber ratio(numerator.r / denominator.r, numerator.i / denominator.r);
return ratio;
}
friend ostream &operator<<(ostream &os, const cnumber &z) {
if (z.r != 0) {
os << z.r;
if ((z.i.get_sign() == 1) && (z.i > 0)) {
os << '+';
}
}
if (z.i != 0) {
if ((z.i != 1) && (z.i != -1)) {
os << z.i;
} else if (z.i == -1) {
os << '-';
}
os << 'i';
}
if (z.r == 0 && z.i == 0) {
os << '0';
}
return os;
}
void operator=(const cnumber &z) {
r = z.r;
i = z.i;
}
const bool operator==(const cnumber &z) const { return r == z.r && i == z.i; }
const bool operator!=(const cnumber &z) const { return r != z.r || i != z.i; }
};

29
fractions.cpp Normal file
View file

@ -0,0 +1,29 @@
#include <iostream>
#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;
}

179
fractions.hpp Normal file
View file

@ -0,0 +1,179 @@
#pragma once
#include <iostream>
#include <string>
using namespace std;
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 {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int get_precision(double a) {
string s = to_string(a);
int i = s.length() - 1;
while (s[i] == '0') {
i--;
}
s.erase(i + 1, s.length());
bool point = false;
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '.') {
point = true;
} else {
if (point) {
count++;
}
}
}
return count;
}
public:
// Constructors
fraction() {}
fraction(const fraction &q) {
signed long long 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);
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;
}
fraction(int a) {
fraction q(a, 1);
n = q.n;
d = q.d;
}
fraction(double dec) {
int precision = get_precision(dec);
signed long long denominator = 1;
for (int i = 0; i < precision; i++) {
denominator *= 10;
}
signed long long numerator = dec * denominator;
fraction q(numerator, denominator);
n = q.n;
d = q.d;
}
// Member functions
int get_sign() const { return (!(n >= 0) != !(d >= 0)) ? -1 : 1; }
signed long long get_n() const { return n; }
signed long long get_d() const { return 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;
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;
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;
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;
fraction q(numerator, denominator);
return q;
}
fraction operator/(const fraction &that) const {
fraction a(this->n, this->d);
fraction b(that.d, that.n);
return a * b;
}
friend ostream &operator<<(ostream &os, const fraction &q) {
signed long long num = q.n;
signed long long den = q.d;
string s = "";
if (q.n < 0) {
s = "-";
num = num * -1;
}
if (q.d < 0) {
s = "-";
den = den * -1;
}
if (q.get_sign() == 1) {
string s = "";
}
if (q.d == q.n) {
os << s << 1;
} else if (q.d == 1) {
os << s << num;
} else {
os << s << '(' << num << '/' << den << ')';
}
return os;
}
void operator=(const fraction &q) {
n = q.n;
d = q.d;
}
void operator=(const int i) {
n = (signed long long)i;
d = (signed long long)1;
}
void operator=(const double dec) {
const fraction q(dec);
n = q.n;
d = q.d;
}
bool operator>(const fraction &q) const {
signed long long hcf = gcd(d, q.d);
fraction a(*this * hcf);
fraction b(q * hcf);
return (a.n > b.n);
}
bool operator>(const int i) const {
fraction q(i);
return (*this > q);
}
bool operator==(const fraction &q) const {
return ((n == q.n) && (d == q.d));
}
bool operator==(const int i) const {
fraction q(i);
return (*this == q);
}
bool operator==(const double dec) const {
fraction q(dec);
return (*this == q);
}
bool operator!=(const fraction &q) const {
return ((n != q.n) || (d != q.d));
}
bool operator!=(const int i) const {
fraction q(i);
return (*this != q);
}
bool operator!=(const double dec) const {
fraction q(dec);
return (*this != q);
}
};

50
matrix.cpp Normal file
View file

@ -0,0 +1,50 @@
#include "matrix.hpp"
#include "../complex-numbers/cnumber.hpp"
#include "../vectors/vector.hpp"
int main() {
vector a = vector(2);
a[0] = cnumber(7, 0);
a[1] = cnumber(6, 0);
vector b = vector(2);
b[0] = cnumber(5, 0);
b[1] = cnumber(3, 0);
vector c = vector(2);
c[0] = cnumber(2, 0);
c[1] = cnumber(5, 0);
vector d = vector(2);
d[0] = cnumber(1, 0);
d[1] = cnumber(0, 1);
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 matrix m's transpose:" << endl;
cout << m.transpose() << endl;
cout << "The matrix n" << endl;
cout << n << endl;
cout << "The matrix m * n" << endl;
cout << m * n << endl;
if (!m.is_hermitian()) {
cout << "The matrix m is not hermitian, here is the hermitian conjugate:"
<< endl;
cout << m.hermitian_conjugate() << endl;
}
if (!n.is_hermitian()) {
cout << "The matrix n is not hermitian, here is the hermitian conjugate:"
<< endl;
cout << n.hermitian_conjugate() << endl;
}
return 0;
}

171
matrix.hpp Normal file
View file

@ -0,0 +1,171 @@
#include "../vectors/vector.hpp"
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class matrix {
private:
long long num_cols;
long long num_rows;
vector *cols;
public:
matrix(const long long num_cols, const long long num_rows) {
this->num_cols = num_rows;
this->num_rows = num_cols;
this->cols = (vector *)calloc(sizeof(vector), num_rows);
for (long long i = 0; i < num_rows; i++) {
this->cols[i] = vector(num_cols);
}
}
~matrix() {
this->num_cols = 0;
this->num_rows = 0;
// free(this->cols);
// this->cols = NULL;
}
const vector get_diagonal() const {
long long diag_len = this->num_rows;
if (this->num_cols < diag_len) {
diag_len = this->num_cols;
}
vector v(diag_len);
for (long long i = 0; i < this->num_cols; i++) {
for (long long j = 0; j < this->num_rows; j++) {
if (i == j) {
v[i] = this->cols[i][j];
}
}
}
return v;
}
const vector get_row(long long index) const { return this->cols[index]; }
const vector get_column(long long index) const {
vector v(this->num_rows);
for (long long j = 0; j < this->num_rows; j++) {
v[j] = this->cols[j][index];
}
return v;
}
const matrix rotate_ninety() const {
matrix m(this->num_cols, this->num_rows);
return m;
}
const matrix transpose() const {
matrix n(this->num_rows, this->num_cols);
for (long long i = 0; i < this->num_cols; i++) {
for (long long j = 0; j < this->num_rows; j++) {
n[j][i] = this->cols[i][j];
}
}
return n;
}
const matrix conjugate() const {
matrix n(this->num_cols, this->num_rows);
for (long long i = 0; i < this->num_cols; i++) {
for (long long j = 0; j < this->num_rows; j++) {
n[i][j] = this->cols[i][j].conjugate();
}
}
return n;
}
const matrix hermitian_conjugate() const {
return this->transpose().conjugate();
}
const bool is_hermitian() const {
if (this->num_rows != this->num_cols)
return false;
matrix m = this->hermitian_conjugate();
bool equal = true;
for (long long i = 0; i < m.num_cols; i++) {
for (long long j = 0; j < m.num_rows; j++) {
if (m[i][j] != this->cols[i][j]) {
equal = false;
}
}
}
return equal;
}
friend ostream &operator<<(ostream &os, const matrix &m) {
char last = '\0';
for (long long i = 0; i < m.num_cols; i++) {
for (long long j = 0; j < m.num_rows; j++) {
string symbols[3];
symbols[0] = "|";
ostringstream oss;
oss << " " << m.cols[i][j] << " ";
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];
}
last = symbols[i][len];
}
}
if (i != m.num_cols - 1)
os << endl << "|";
}
return os;
}
const bool operator==(const matrix &m) const {
bool equal = true;
for (long long i = 0; i < this->num_cols; i++) {
for (long long j = 0; j < this->num_rows; j++) {
if (this->cols[i][j] != m[i][j]) {
equal = false;
}
}
}
return equal;
}
const vector operator[](const long long index) const {
return this->cols[index];
}
const matrix operator*(const cnumber z) const {
matrix n(this->num_cols, this->num_rows);
for (long long i = 0; i < this->num_cols; i++) {
n[i] = this->cols[i] * z;
}
return n;
}
const matrix operator*(const matrix m) const {
matrix n(this->num_cols, m.num_rows);
// if (this->num_cols != m.num_rows && m.num_cols != this->num_rows)
// return n;
for (long long i = 0; i < this->num_rows; i++) {
for (long long j = 0; j < this->num_rows; j++) {
n[i][j] = this->get_row(i) * m.get_column(j);
}
}
return n;
}
const matrix operator+(const matrix &m) const {
matrix n(this->num_cols, this->num_rows);
for (long long i = 0; i < this->num_cols; i++) {
n[i] = this->cols[i] + m[i];
}
return n;
}
const matrix operator-(const matrix &m) const {
matrix n(this->num_cols, this->num_rows);
for (long long i = 0; i < this->num_cols; i++) {
n[i] = this->cols[i] - m[i];
}
return n;
}
// FIXME: Figure out how to make sure you do not try to access something
// outside of the index
vector &operator[](const long long index) { return this->cols[index]; }
};

27
vector.cpp Normal file
View file

@ -0,0 +1,27 @@
#include "vector.hpp"
#include <iostream>
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;
return 0;
}

102
vector.hpp Normal file
View file

@ -0,0 +1,102 @@
#pragma once
#include "../complex-numbers/cnumber.hpp"
#include <iostream>
using namespace std;
class vector {
private:
long long dimention;
cnumber *entries;
public:
vector(const long long dimention) {
this->dimention = dimention;
this->entries = (cnumber *)calloc(sizeof(cnumber), dimention);
for (long long i = 0; i < dimention; i++) {
this->set_entry(i, cnumber(0, 0));
}
}
vector(const vector &v) {
this->dimention = v.get_dimention();
this->entries = (cnumber *)calloc(sizeof(cnumber), v.get_dimention());
for (long long i = 0; i < v.get_dimention(); i++) {
this->set_entry(i, v.get_entry(i));
}
}
~vector() {
this->dimention = 0;
free(this->entries);
this->entries = NULL;
}
long long get_dimention() const { return this->dimention; }
cnumber *get_entries() const { return this->entries; }
cnumber get_entry(const long long 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) {
if (index < this->get_dimention()) {
this->entries[index] = z;
}
}
const cnumber operator[](const long long 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]; }
friend ostream &operator<<(ostream &os, const vector &v) {
for (long long i = 0; i < v.get_dimention(); i++) {
os << "| " << v[i] << " |";
if (i != v.get_dimention() - 1)
os << endl;
}
return os;
}
void operator=(const vector &v) {
this->dimention = v.get_dimention();
free(this->entries);
this->entries = (cnumber *)calloc(sizeof(cnumber), dimention);
for (long long i = 0; i < v.get_dimention(); i++) {
this->set_entry(i, v.get_entry(i));
}
}
const vector operator+(const vector &v) const {
if (this->get_dimention() != v.get_dimention()) {
return vector(0);
}
vector sum = vector(v.get_dimention());
for (long long i = 0; i < v.get_dimention(); i++) {
sum[i] = this->get_entry(i) + v[i];
}
return sum;
}
const vector operator-(const vector &v) const {
if (this->get_dimention() != v.get_dimention()) {
return vector(0);
}
vector sum = vector(v.get_dimention());
for (long long i = 0; i < v.get_dimention(); i++) {
sum[i] = this->entries[i] - v[i];
}
return sum;
}
const vector operator*(const cnumber scalar) const {
vector product = vector(this->get_dimention());
for (long long 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++) {
res = res + this->get_entry(i) * v.get_entry(i);
}
return res;
}
};