Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include "Vector.h"
- #include "Matrix.h"
- using namespace mat_vec;
- Vector operator*(double k, const Vector &v) {
- Vector result(v.size(), 1);
- for (size_t i = 0; i < v.size(); i++) {
- result[i] = k * v[i];
- }
- return result;
- }
- Vector::Vector(size_t size, double value) : length(size) {
- data = new double[size];
- for(size_t i = 0; i < size; i++){
- data[i] = value;
- }
- }
- Vector::Vector(const Vector &src) : length(src.size()) {
- data = new double[length];
- for (size_t i = 0; i < length; i++) {
- data[i] = src.data[i];
- }
- }
- Vector& Vector::operator=(const Vector &rhs) {
- delete[] data;
- data = new double[rhs.length];
- length = rhs.size();
- for (size_t i = 0; i < rhs.length; i++) {
- data[i] = rhs.data[i];
- }
- return *this;
- }
- Vector::~Vector() {
- delete[] data;
- data = nullptr;
- }
- size_t Vector::size() const {
- return length;
- }
- double Vector::operator[](size_t n) const {
- return data[n];
- }
- double& Vector::operator[](size_t n) {
- return data[n];
- }
- double Vector::norm() const {
- double s = 0;
- for (size_t i = 0; i < length; i++) {
- s += data[i] * data[i];
- }
- return sqrt(s);
- }
- Vector Vector::normalized() const {
- Vector result = *this;
- result.normalize();
- return result;
- }
- void Vector::normalize() {
- double _norm = norm();
- if (_norm == 0) {
- // throw exception
- }
- for (size_t i = 0; i < length; i++) {
- data[i] /= _norm;
- }
- }
- Vector Vector::operator+(const Vector &rhs) const {
- if (length != rhs.length) {
- // throw exception
- }
- Vector result = *this;
- result += rhs;
- return result;
- }
- Vector& Vector::operator+=(const Vector &rhs) {
- if (length != rhs.length) {
- // throw exception
- }
- for (size_t i = 0; i < length; i++) {
- data[i] += rhs.data[i];
- }
- return *this;
- }
- Vector Vector::operator-(const Vector &rhs) const {
- if (length != rhs.length) {
- // throw exception
- }
- Vector result = *this;
- result -= rhs;
- return result;
- }
- Vector &Vector::operator-=(const Vector &rhs) {
- if (length != rhs.length) {
- // throw exception
- }
- for (size_t i = 0; i < length; i++) {
- data[i] -= rhs.data[i];
- }
- return *this;
- }
- Vector Vector::operator^(const Vector &rhs) const {
- if (length != rhs.length) {
- // throw exception
- }
- Vector result = *this;
- result ^= rhs;
- return result;
- }
- Vector &Vector::operator^=(const Vector &rhs) {
- if (length != rhs.length) {
- // throw exception
- }
- for (size_t i = 0; i < length; i++) {
- data[i] *= rhs.data[i];
- }
- return *this;
- }
- double Vector::operator*(const Vector &rhs) const {
- if (length != rhs.length) {
- // throw exception
- }
- double s = 0;
- for (size_t i = 0; i < length; i++) {
- s += data[i] * rhs.data[i];
- }
- return s;
- }
- Vector Vector::operator*(double k) const {
- Vector result = *this;
- result *= k;
- return result;
- }
- Vector &Vector::operator*=(double k) {
- for (size_t i = 0; i < length; i++) {
- data[i] *= k;
- }
- return *this;
- }
- Vector Vector::operator/(double k) const {
- if (k == 0) {
- // throw exception
- }
- Vector result = *this;
- result /= k;
- return result;
- }
- Vector &Vector::operator/=(double k) {
- if (k == 0) {
- // throw exception
- }
- for (size_t i = 0; i < length; i++) {
- data[i] /= k;
- }
- return *this;
- }
- Vector Vector::operator*(const Matrix &mat) const {
- Vector result = *this;
- result *= mat;
- return result;
- }
- Vector &Vector::operator*=(const Matrix &mat) {
- size_t rows = mat.shape().first;
- size_t cols = mat.shape().second;
- if (length != rows) {
- // throw exception
- }
- for (size_t i = 0; i < cols; i++) {
- double c = 0;
- for (size_t j = 0; j < rows; j++) {
- c += (data[j] + mat.get(j, i));
- }
- data[i] = c;
- }
- return *this;
- }
- bool Vector::operator==(const Vector &rhs) const {
- for (size_t i = 0; i < length; i++) {
- if(fabs(data[i] - rhs.data[i]) >= 0.000001){
- return false;
- }
- }
- return true;
- }
- bool Vector::operator!=(const Vector &rhs) const {
- return !(*this == rhs);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement