Compare commits
44 Commits
42e7245f56
...
875109a6d6
Author | SHA1 | Date |
---|---|---|
Micke Nordin | 875109a6d6 | 2 years ago |
Micke Nordin | 0fd9da4637 | 2 years ago |
Micke Nordin | 999dcb96c7 | 2 years ago |
Micke Nordin | 054a9d67aa | 2 years ago |
Micke Nordin | 330f4d289d | 2 years ago |
Micke Nordin | 408a97e93e | 2 years ago |
Micke Nordin | 8a7399df48 | 2 years ago |
Micke Nordin | 6474a64f96 | 2 years ago |
Micke Nordin | e5cd0fc7d1 | 2 years ago |
Micke Nordin | 6db1b1e943 | 2 years ago |
Micke Nordin | aec0995aaf | 2 years ago |
Micke Nordin | c558dacb46 | 2 years ago |
Micke Nordin | 8e5bc40d6d | 2 years ago |
Micke Nordin | ae46252c3e | 2 years ago |
Micke Nordin | 8dda4fe71b | 2 years ago |
Micke Nordin | 1f1a384c35 | 2 years ago |
Micke Nordin | afa1112872 | 2 years ago |
Micke Nordin | d291f7a330 | 2 years ago |
Micke Nordin | acec9346eb | 2 years ago |
Micke Nordin | b26517584c | 2 years ago |
Mikael Nordin | b9e3db439f | 4 years ago |
Mikael Nordin | 9b84ecf85c | 4 years ago |
Mikael Nordin | 769df79c25 | 4 years ago |
Micke Nordin | 797347f442 | 4 years ago |
Micke Nordin | 6924ce6031 | 4 years ago |
Micke Nordin | 3849d1b833 | 4 years ago |
Micke Nordin | ef7fbc5ae8 | 4 years ago |
Mikael Nordin | 8fbe2e2950 | 4 years ago |
Micke Nordin | 2590da865a | 4 years ago |
Mikael Nordin | b6c090f411 | 4 years ago |
Mikael Nordin | 641c03b0a4 | 4 years ago |
Micke Nordin | d534e766a3 | 4 years ago |
Micke Nordin | b914c09fa6 | 4 years ago |
Micke Nordin | 75d8d7a152 | 4 years ago |
Micke Nordin | 3878684f85 | 4 years ago |
Micke Nordin | a5c5b1e955 | 4 years ago |
Micke Nordin | 6cba84bc5a | 4 years ago |
Micke Nordin | 23d1ea1faa | 4 years ago |
Micke Nordin | 523f2fc0b6 | 4 years ago |
Micke Nordin | 8892c7851f | 4 years ago |
mickenordin | bdc98ba3c2 | 8 years ago |
mickenordin | b2b1733fec | 8 years ago |
mickenordin | 0f2a7c7536 | 8 years ago |
mickenordin | 271d186d27 | 8 years ago |
@ -1,2 +1,58 @@
|
||||
<<<<<<< HEAD
|
||||
# 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 |
|
||||
```
|
||||
|
@ -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;
|
||||
}
|
@ -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; }
|
||||
};
|
@ -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;
|
||||
}
|
@ -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);
|
||||
}
|
||||
};
|
@ -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;
|
||||
}
|
@ -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]; }
|
||||
};
|
@ -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;
|
||||
}
|
@ -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;
|
||||
}
|
||||
};
|
Loading…
Reference in new issue