-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector_math.cpp
More file actions
72 lines (65 loc) · 2.43 KB
/
vector_math.cpp
File metadata and controls
72 lines (65 loc) · 2.43 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
#include "vector_math.h"
/*----------- element‑wise addition / subtraction -----------------------*/
std::vector<double>& operator+=(std::vector<double>& a,
const std::vector<double>& b)
{
for (std::size_t i = 0; i < a.size(); ++i) a[i] += b[i];
return a;
}
std::vector<double>& operator-=(std::vector<double>& a,
const std::vector<double>& b)
{
for (std::size_t i = 0; i < a.size(); ++i) a[i] -= b[i];
return a;
}
std::vector<double> operator+(const std::vector<double>& a,
const std::vector<double>& b)
{ std::vector<double> res = a; return res += b; }
std::vector<double> operator-(const std::vector<double>& a,
const std::vector<double>& b)
{ std::vector<double> res = a; return res -= b; }
/*----------- vector–scalar products -----------------------------------*/
std::vector<double>& operator*=(std::vector<double>& v, double s)
{
for (double& x : v) x *= s;
return v;
}
std::vector<double>& operator/=(std::vector<double>& v, double s)
{
if (s == 0.0) throw std::invalid_argument("Division by zero.");
for (double& x : v) x /= s;
return v;
}
std::vector<double> operator*(const std::vector<double>& v, double s)
{ std::vector<double> res = v; return res *= s; }
std::vector<double> operator*(double s, const std::vector<double>& v)
{ return v * s; }
std::vector<double> operator/(const std::vector<double>& v, double s)
{ std::vector<double> res = v; return res /= s; }
/*----------- Element‑wise product ---------------------------*/
std::vector<double>& operator*(std::vector<double>& a,
const std::vector<double>& b)
{
for (std::size_t i = 0; i < a.size(); ++i) a[i] *= b[i];
return a;
}
std::vector<double> operator*(const std::vector<double>& a,
const std::vector<double>& b)
{
std::vector<double> res = a;
for (std::size_t i = 0; i < a.size(); ++i) res[i] *= b[i];
return res;
}
std::vector<double>& operator/(std::vector<double>& a,
const std::vector<double>& b)
{
for (std::size_t i = 0; i < a.size(); ++i) a[i] /= b[i];
return a;
}
std::vector<double> operator/(const std::vector<double>& a,
const std::vector<double>& b)
{
std::vector<double> res = a;
for (std::size_t i = 0; i < a.size(); ++i) res[i] /= b[i];
return res;
}