mmathlib/vector.hpp

140 lines
3.9 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-18 23:01:56 +02:00
#include <iomanip>
2022-07-16 10:47:29 +02:00
#include <iostream>
class matrix;
2022-07-16 10:47:29 +02:00
class vector {
private:
2022-07-26 15:18:45 +02:00
uint64_t dimention = 0;
2022-07-18 13:25:41 +02:00
cnumber *entries = NULL;
bool err = true;
2022-07-27 10:38:56 +02:00
bool row = false;
2022-07-16 10:47:29 +02:00
public:
2022-07-18 13:25:41 +02:00
bool is_error() { return this->err; }
2022-07-18 23:01:56 +02:00
vector(){};
2022-07-26 15:18:45 +02:00
vector(const uint64_t 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);
2022-07-18 23:01:56 +02:00
this->entries = (cnumber *)malloc(sizeof(cnumber) * dimention);
2022-07-26 15:18:45 +02:00
for (uint64_t i = 0; i < dimention; i++) {
2022-07-16 10:47:29 +02:00
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);
2022-07-18 23:01:56 +02:00
this->entries = (cnumber *)malloc(sizeof(cnumber) * v.get_dimention());
2022-07-26 15:18:45 +02:00
for (uint64_t i = 0; i < v.get_dimention(); i++) {
2022-07-16 10:47:29 +02:00
this->set_entry(i, v.get_entry(i));
}
}
~vector() {
this->dimention = 0;
free(this->entries);
2022-07-18 23:01:56 +02:00
// this->entries = NULL;
2022-07-18 13:25:51 +02:00
this->err = true;
2022-07-16 10:47:29 +02:00
}
2022-07-26 15:18:45 +02:00
uint64_t get_dimention() const { return this->dimention; }
const cnumber *get_entries() const { return this->entries; }
2022-07-26 15:18:45 +02:00
cnumber get_entry(const uint64_t index) const {
2022-07-16 10:47:29 +02:00
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
}
2022-07-27 10:38:56 +02:00
void make_row() { this->row = true; }
void make_column() { this->row = false; }
2022-07-26 15:18:45 +02:00
void set_entry(const uint64_t index, const cnumber z) {
2022-07-16 10:47:29 +02:00
if (index < this->get_dimention()) {
this->entries[index] = z;
}
}
2022-07-26 15:18:45 +02:00
const cnumber operator[](const uint64_t index) const {
2022-07-16 10:47:29 +02:00
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-26 15:18:45 +02:00
cnumber &operator[](const uint64_t index) { return this->entries[index]; }
2022-07-16 10:47:29 +02:00
2022-07-19 13:54:24 +02:00
friend std::ostream &operator<<(std::ostream &os, const vector &v) {
2022-07-18 23:01:56 +02:00
int longest = 0;
2022-07-27 10:38:56 +02:00
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;
}
2022-07-26 15:18:45 +02:00
for (uint64_t i = 0; i < v.get_dimention(); i++) {
2022-07-27 10:38:56 +02:00
std::ostringstream oss;
2022-07-18 23:01:56 +02:00
oss << v[i];
2022-07-19 13:54:24 +02:00
std::string s = oss.str();
2022-07-18 23:01:56 +02:00
if (longest < s.length())
2022-07-27 10:38:56 +02:00
longest = s.length();
2022-07-18 23:01:56 +02:00
}
2022-07-26 15:18:45 +02:00
for (uint64_t i = 0; i < v.get_dimention(); i++) {
2022-07-27 10:38:56 +02:00
std::ostringstream oss;
2022-07-18 23:01:56 +02:00
oss << v[i];
2022-07-19 13:54:24 +02:00
std::string s = oss.str();
2022-07-27 10:38:56 +02:00
int padding = longest - s.length() + 1;
2022-07-18 23:01:56 +02:00
os << "|" << v[i] << std::setw(padding) << "|";
2022-07-16 10:47:29 +02:00
if (i != v.get_dimention() - 1)
2022-07-19 13:54:24 +02:00
os << std::endl;
2022-07-16 10:47:29 +02:00
}
return os;
}
void operator=(const vector &v) {
this->dimention = v.get_dimention();
free(this->entries);
2022-07-18 23:01:56 +02:00
this->entries = (cnumber *)malloc(sizeof(cnumber) * dimention);
2022-07-26 15:18:45 +02:00
for (uint64_t i = 0; i < v.get_dimention(); i++) {
2022-07-16 10:47:29 +02:00
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());
2022-07-26 15:18:45 +02:00
for (uint64_t i = 0; i < v.get_dimention(); i++) {
2022-07-16 10:47:29 +02:00
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());
2022-07-26 15:18:45 +02:00
for (uint64_t i = 0; i < v.get_dimention(); i++) {
2022-07-16 10:47:29 +02:00
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());
2022-07-26 15:18:45 +02:00
for (uint64_t i = 0; i < this->get_dimention(); i++) {
2022-07-16 10:47:29 +02:00
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-26 15:18:45 +02:00
for (uint64_t i = 0; i < this->get_dimention(); i++) {
2022-07-16 16:54:10 +02:00
res = res + this->get_entry(i) * v.get_entry(i);
}
return res;
}
2022-07-16 10:47:29 +02:00
};