commit
0fd9da4637
@ -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