Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.43 KB | None | 0 0
  1. #include <cmath>
  2. #include "Vector.h"
  3. #include "Matrix.h"
  4.  
  5. using namespace mat_vec;
  6.  
  7. Vector operator*(double k, const Vector &v) {
  8.     Vector result(v.size(), 1);
  9.     for (size_t i = 0; i < v.size(); i++) {
  10.         result[i] = k * v[i];
  11.     }
  12.     return result;
  13. }
  14.  
  15. Vector::Vector(size_t size, double value) : length(size) {
  16.     data = new double[size];
  17.     for(size_t i = 0; i < size; i++){
  18.         data[i] = value;
  19.     }
  20. }
  21.  
  22. Vector::Vector(const Vector &src) : length(src.size()) {
  23.     data = new double[length];
  24.     for (size_t i = 0; i < length; i++) {
  25.         data[i] = src.data[i];
  26.     }
  27. }
  28.  
  29. Vector& Vector::operator=(const Vector &rhs) {
  30.     delete[] data;
  31.     data = new double[rhs.length];
  32.     length = rhs.size();
  33.     for (size_t i = 0; i < rhs.length; i++) {
  34.         data[i] = rhs.data[i];
  35.     }
  36.     return *this;
  37. }
  38.  
  39. Vector::~Vector() {
  40.     delete[] data;
  41.     data = nullptr;
  42. }
  43.  
  44. size_t Vector::size() const {
  45.     return length;
  46. }
  47.  
  48. double Vector::operator[](size_t n) const {
  49.     return data[n];
  50. }
  51.  
  52. double& Vector::operator[](size_t n) {
  53.     return data[n];
  54. }
  55.  
  56. double Vector::norm() const {
  57.     double s = 0;
  58.     for (size_t i = 0; i < length; i++) {
  59.         s += data[i] * data[i];
  60.     }
  61.     return sqrt(s);
  62. }
  63.  
  64. Vector Vector::normalized() const {
  65.     Vector result = *this;
  66.     result.normalize();
  67.     return result;
  68. }
  69.  
  70. void Vector::normalize() {
  71.     double _norm = norm();
  72.     if (_norm == 0) {
  73.         // throw exception
  74.     }
  75.     for (size_t i = 0; i < length; i++) {
  76.         data[i] /= _norm;
  77.     }
  78. }
  79.  
  80. Vector Vector::operator+(const Vector &rhs) const {
  81.     if (length != rhs.length) {
  82.         // throw exception
  83.     }
  84.     Vector result = *this;
  85.     result += rhs;
  86.     return result;
  87. }
  88.  
  89. Vector& Vector::operator+=(const Vector &rhs) {
  90.     if (length != rhs.length) {
  91.         // throw exception
  92.     }
  93.     for (size_t i = 0; i < length; i++) {
  94.         data[i] += rhs.data[i];
  95.     }
  96.     return *this;
  97. }
  98.  
  99. Vector Vector::operator-(const Vector &rhs) const {
  100.     if (length != rhs.length) {
  101.         // throw exception
  102.     }
  103.     Vector result = *this;
  104.     result -= rhs;
  105.     return result;
  106. }
  107.  
  108. Vector &Vector::operator-=(const Vector &rhs) {
  109.     if (length != rhs.length) {
  110.         // throw exception
  111.     }
  112.     for (size_t i = 0; i < length; i++) {
  113.         data[i] -= rhs.data[i];
  114.     }
  115.     return *this;
  116. }
  117.  
  118. Vector Vector::operator^(const Vector &rhs) const {
  119.     if (length != rhs.length) {
  120.         // throw exception
  121.     }
  122.     Vector result = *this;
  123.     result ^= rhs;
  124.     return result;
  125. }
  126.  
  127. Vector &Vector::operator^=(const Vector &rhs) {
  128.     if (length != rhs.length) {
  129.         // throw exception
  130.     }
  131.     for (size_t i = 0; i < length; i++) {
  132.         data[i] *= rhs.data[i];
  133.     }
  134.     return *this;
  135. }
  136.  
  137. double Vector::operator*(const Vector &rhs) const {
  138.     if (length != rhs.length) {
  139.         // throw exception
  140.     }
  141.     double s = 0;
  142.     for (size_t i = 0; i < length; i++) {
  143.         s += data[i] * rhs.data[i];
  144.     }
  145.     return s;
  146. }
  147.  
  148. Vector Vector::operator*(double k) const {
  149.     Vector result = *this;
  150.     result *= k;
  151.     return result;
  152. }
  153.  
  154. Vector &Vector::operator*=(double k) {
  155.     for (size_t i = 0; i < length; i++) {
  156.         data[i] *= k;
  157.     }
  158.     return *this;
  159. }
  160.  
  161. Vector Vector::operator/(double k) const {
  162.     if (k == 0) {
  163.         // throw exception
  164.     }
  165.     Vector result = *this;
  166.     result /= k;
  167.     return result;
  168. }
  169.  
  170. Vector &Vector::operator/=(double k) {
  171.     if (k == 0) {
  172.         // throw exception
  173.     }
  174.     for (size_t i = 0; i < length; i++) {
  175.         data[i] /= k;
  176.     }
  177.     return *this;
  178. }
  179.  
  180. Vector Vector::operator*(const Matrix &mat) const {
  181.     Vector result = *this;
  182.     result *= mat;
  183.     return result;
  184. }
  185.  
  186. Vector &Vector::operator*=(const Matrix &mat) {
  187.     size_t rows = mat.shape().first;
  188.     size_t cols = mat.shape().second;
  189.     if (length != rows) {
  190.         // throw exception
  191.     }
  192.     for (size_t i = 0; i < cols; i++) {
  193.         double c = 0;
  194.         for (size_t j = 0; j < rows; j++) {
  195.             c += (data[j] + mat.get(j, i));
  196.         }
  197.         data[i] = c;
  198.     }
  199.     return *this;
  200. }
  201.  
  202. bool Vector::operator==(const Vector &rhs) const {
  203.     for (size_t i = 0; i < length; i++) {
  204.         if(fabs(data[i] - rhs.data[i]) >= 0.000001){
  205.             return false;
  206.         }
  207.     }
  208.     return true;
  209. }
  210.  
  211. bool Vector::operator!=(const Vector &rhs) const {
  212.     return !(*this == rhs);
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement