mmathlib/vector.hpp

115 lines
3.2 KiB
C++
Raw Normal View History

2022-07-16 11:54:12 +02:00
#pragma once
2022-07-16 17:44:45 +02:00
#include "cnumber.hpp"
2022-07-16 10:47:29 +02:00
#include <iostream>
using namespace std;
class matrix;
2022-07-16 10:47:29 +02:00
class vector {
private:
2022-07-18 13:25:41 +02:00
long long dimention = 0;
cnumber *entries = NULL;
bool err = true;
2022-07-16 10:47:29 +02:00
public:
2022-07-18 13:25:41 +02:00
bool is_error() { return this->err; }
vector() {};
2022-07-16 10:47:29 +02:00
vector(const long long dimention) {
2022-07-18 13:25:41 +02:00
this->err = false;
2022-07-16 10:47:29 +02:00
this->dimention = dimention;
2022-07-18 13:25:41 +02:00
free(this->entries);
this->entries = (cnumber *)malloc(sizeof(cnumber)* dimention);
2022-07-16 10:47:29 +02:00
for (long long i = 0; i < dimention; i++) {
this->set_entry(i, cnumber(0, 0));
}
}
vector(const vector &v) {
2022-07-18 13:25:41 +02:00
this->err = false;
2022-07-16 10:47:29 +02:00
this->dimention = v.get_dimention();
2022-07-18 13:25:41 +02:00
free(this->entries);
this->entries = (cnumber *)malloc(sizeof(cnumber)* v.get_dimention());
2022-07-16 10:47:29 +02:00
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);
2022-07-18 13:25:51 +02:00
//this->entries = NULL;
this->err = true;
2022-07-16 10:47:29 +02:00
}
long long get_dimention() const { return this->dimention; }
const cnumber *get_entries() const { return this->entries; }
2022-07-16 10:47:29 +02:00
cnumber get_entry(const long long index) const {
if (index < this->get_dimention()) {
2022-07-18 13:25:51 +02:00
return this->entries[index];
2022-07-16 10:47:29 +02:00
}
2022-07-18 13:25:51 +02:00
return cnumber(0, 0);
2022-07-16 10:47:29 +02:00
}
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);
}
2022-07-18 13:25:51 +02:00
// FIXME: Figure out how to make sure you do not try to access something
// outside of the index
2022-07-16 10:47:29 +02:00
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);
2022-07-18 13:25:41 +02:00
this->entries = (cnumber *)malloc(sizeof(cnumber)* dimention);
2022-07-16 10:47:29 +02:00
for (long long i = 0; i < v.get_dimention(); i++) {
this->set_entry(i, v.get_entry(i));
}
2022-07-18 13:25:41 +02:00
this->err = false;
2022-07-16 10:47:29 +02:00
}
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 matrix &m) const;
2022-07-16 10:47:29 +02:00
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;
}
2022-07-16 16:54:10 +02:00
const cnumber operator*(const vector &v) const {
2022-07-18 13:25:51 +02:00
cnumber res(0, 0);
2022-07-16 16:54:10 +02:00
for (long long i = 0; i < this->get_dimention(); i++) {
res = res + this->get_entry(i) * v.get_entry(i);
}
return res;
}
2022-07-16 10:47:29 +02:00
};