-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathVector.cpp
More file actions
92 lines (79 loc) · 2.08 KB
/
MathVector.cpp
File metadata and controls
92 lines (79 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <random>
#include <chrono>
#include "MathVector.hpp"
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator(seed);
std::uniform_real_distribution<double> distribution(0,1);
double randDouble(double min, double max)
{
return (max - min) * distribution(generator) + min;
}
void MathVector::randomVector(DimensionLimits d)
{
vector.clear();
for(unsigned int i = 0; i < d.min.size(); i++)
vector.push_back(randDouble(d.min[i], d.max[i]));
}
void MathVector::fillValues(unsigned dimension, double value)
{
vector.clear();
for (unsigned i = 0; i < dimension; i++)
vector.push_back(value);
}
std::ostream &operator<<(std::ostream &out, MathVector m)
{
out << "[ ";
for(auto elem: m.vector)
out << elem << " ";
out << "]";
return out;
}
MathVector operator+(const MathVector m1, const MathVector m2)
{
if(m1.size() != m2.size())
throw std::length_error("Vectors size must match");
MathVector result = m1;
for(int i = 0; i < result.size(); i++)
result.vector[i] += m2.vector[i];
return result;
}
MathVector operator-(const MathVector m1, const MathVector m2)
{
if(m1.size() != m2.size())
throw std::length_error("Vectors size must match");
MathVector result = m1;
for(int i = 0; i < result.size(); i++)
result.vector[i] -= m2.vector[i];
return result;
}
MathVector operator*(const MathVector m1, const MathVector m2)
{
if(m1.size() != m2.size())
throw std::length_error("Vectors size must match");
MathVector result = m1;
for(int i = 0; i < result.size(); i++)
result.vector[i] *= m2.vector[i];
return result;
}
MathVector operator+(const MathVector m, const double d)
{
MathVector result = m;
for(int i = 0; i < result.size(); i++)
result.vector[i] += d;
return result;
}
MathVector operator+(const double d, const MathVector m)
{
return m + d;
}
MathVector operator*(const MathVector m, const double d)
{
MathVector result = m;
for(int i = 0; i < result.size(); i++)
result.vector[i] *= d;
return result;
}
MathVector operator*(const double d ,const MathVector m)
{
return m * d;
}