Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# vector
Linear space element representation
[![Build status](https://ci.appveyor.com/api/projects/status/em2k9pidg5n2as0i/branch/master?svg=true)](https://ci.appveyor.com/project/Fxlr8/vector/branch/master)
[![Build Status](https://travis-ci.org/Fxlr8/vector.svg?branch=Turkin)](https://travis-ci.org/Fxlr8/vector)
107 changes: 107 additions & 0 deletions src/vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "vector.h"

Vector::Vector() {
for (unsigned int i = 0; i < n; ++i) {
coords_[i] = 0;
}
}

Vector::Vector(double a) {
for (unsigned int i = 0; i < n; ++i) {
coords_[i] = a;
}
}

Vector::Vector(const Vector &op) {
for (unsigned int i = 0; i < n; ++i) {
coords_[i] = op.coords_[i];
}
}

Vector &Vector::operator=(const Vector &op) {
for (unsigned int i = 0; i < n; ++i) {
coords_[i] = op.coords_[i];
}
return *this;
}

double Vector::operator[](unsigned long i) const {
return coords_[i];
}

double &Vector::operator[](unsigned long i) {
return coords_[i];
}

Vector &Vector::operator+=(const Vector &op) {
for (unsigned int i = 0; i < n; ++i) {
coords_[i] += op.coords_[i];
}
return *this;
}


Vector &Vector::operator-=(const Vector &op) {
for (unsigned int i = 0; i < n; ++i) {
coords_[i] -= op.coords_[i];
}
return *this;
}

Vector &Vector::operator*=(double a) {
for (unsigned int i = 0; i < n; ++i) {
coords_[i] *= a;
}
return *this;
}

Vector &Vector::operator/=(double a) {
for (unsigned int i = 0; i < n; ++i) {
coords_[i] /= a;
}
return *this;
}

bool operator==(const Vector &op1, const Vector &op2) {
for (unsigned int i = 0; i < Vector::n; i++) {
if (op1.coords_[i] != op2.coords_[i]) {
return false; }
}
return true;
}

Vector operator+(const Vector &op1, const Vector &op2) {
return Vector(op1)+=op2;
}

Vector operator-(const Vector &op1, const Vector &op2) {
return Vector(op1)-=op2;
}

Vector operator*(const Vector &op, double a) {
return Vector(op) *= a;
}
Vector operator*(double a, const Vector &op){
return Vector(op) *= a;
}

Vector operator/(const Vector &op1, double a) {
return Vector(op1) /= a;
}

double operator^(const Vector &op1, const Vector &op2) {
double res = 0;
for (unsigned int i = 0; i < Vector::n; ++i) {
res += op1.coords_[i] * op2.coords_[i];
}
return res;
}

Vector Vector::operator-() const {
return Vector(*this) *= -1;
}

bool operator!=(const Vector &op1, const Vector &op2) {
return !(op1 == op2);

}