You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
3.9 KiB
140 lines
3.9 KiB
#pragma once
|
|
#include "cnumber.hpp"
|
|
#include <iomanip>
|
|
#include <iostream>
|
|
class matrix;
|
|
class vector {
|
|
private:
|
|
uint64_t dimention = 0;
|
|
cnumber *entries = NULL;
|
|
bool err = true;
|
|
bool row = false;
|
|
|
|
public:
|
|
bool is_error() { return this->err; }
|
|
vector(){};
|
|
vector(const uint64_t dimention) {
|
|
this->err = false;
|
|
this->dimention = dimention;
|
|
free(this->entries);
|
|
this->entries = (cnumber *)malloc(sizeof(cnumber) * dimention);
|
|
for (uint64_t i = 0; i < dimention; i++) {
|
|
this->set_entry(i, cnumber(0, 0));
|
|
}
|
|
}
|
|
vector(const vector &v) {
|
|
this->err = false;
|
|
this->dimention = v.get_dimention();
|
|
free(this->entries);
|
|
this->entries = (cnumber *)malloc(sizeof(cnumber) * v.get_dimention());
|
|
for (uint64_t 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;
|
|
this->err = true;
|
|
}
|
|
uint64_t get_dimention() const { return this->dimention; }
|
|
const cnumber *get_entries() const { return this->entries; }
|
|
cnumber get_entry(const uint64_t index) const {
|
|
if (index < this->get_dimention()) {
|
|
return this->entries[index];
|
|
}
|
|
return cnumber(0, 0);
|
|
}
|
|
void make_row() { this->row = true; }
|
|
void make_column() { this->row = false; }
|
|
void set_entry(const uint64_t index, const cnumber z) {
|
|
if (index < this->get_dimention()) {
|
|
this->entries[index] = z;
|
|
}
|
|
}
|
|
|
|
const cnumber operator[](const uint64_t 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 uint64_t index) { return this->entries[index]; }
|
|
|
|
friend std::ostream &operator<<(std::ostream &os, const vector &v) {
|
|
int longest = 0;
|
|
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;
|
|
}
|
|
for (uint64_t i = 0; i < v.get_dimention(); i++) {
|
|
std::ostringstream oss;
|
|
oss << v[i];
|
|
std::string s = oss.str();
|
|
if (longest < s.length())
|
|
longest = s.length();
|
|
}
|
|
for (uint64_t i = 0; i < v.get_dimention(); i++) {
|
|
std::ostringstream oss;
|
|
oss << v[i];
|
|
std::string s = oss.str();
|
|
int padding = longest - s.length() + 1;
|
|
os << "|" << v[i] << std::setw(padding) << "|";
|
|
if (i != v.get_dimention() - 1)
|
|
os << std::endl;
|
|
}
|
|
return os;
|
|
}
|
|
void operator=(const vector &v) {
|
|
this->dimention = v.get_dimention();
|
|
free(this->entries);
|
|
this->entries = (cnumber *)malloc(sizeof(cnumber) * dimention);
|
|
for (uint64_t i = 0; i < v.get_dimention(); i++) {
|
|
this->set_entry(i, v.get_entry(i));
|
|
}
|
|
this->err = false;
|
|
}
|
|
const vector operator+(const vector &v) const {
|
|
if (this->get_dimention() != v.get_dimention()) {
|
|
return vector(0);
|
|
}
|
|
vector sum = vector(v.get_dimention());
|
|
for (uint64_t 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 (uint64_t i = 0; i < v.get_dimention(); i++) {
|
|
sum[i] = this->entries[i] - v[i];
|
|
}
|
|
return sum;
|
|
}
|
|
const vector operator*(const matrix &m) const;
|
|
const vector operator*(const cnumber scalar) const {
|
|
vector product = vector(this->get_dimention());
|
|
for (uint64_t 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 (uint64_t i = 0; i < this->get_dimention(); i++) {
|
|
res = res + this->get_entry(i) * v.get_entry(i);
|
|
}
|
|
return res;
|
|
}
|
|
};
|