-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_operator.h
113 lines (96 loc) · 2.35 KB
/
vector_operator.h
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#ifndef UTK_BV_UTK_VECTOR_H
#define UTK_BV_UTK_VECTOR_H
#include <vector>
#include <cassert>
#include <cstddef>
#include <iostream>
using namespace std;
template<typename FLOAT1, typename FLOAT2>
vector<FLOAT1> operator+(const vector<FLOAT1> &v1, const vector<FLOAT2> &v2) {
assert(v1.size() == v2.size());
vector<FLOAT1> ret(v1);
for (int i = 0; i < ret.size(); ++i) {
ret[i] += v2[i];
}
return ret;
}
template<typename T>
vector<T> &operator+=(vector<T> &v1, const vector<T> &v2) {
assert(v1.size() == v2.size());
for (int i = 0; i < v1.size(); ++i) {
v1[i] += v2[i];
}
return v1;
}
template<typename T>
vector<T> operator-(const vector<T> &v1, const vector<T> &v2) {
assert(v1.size() == v2.size());
vector<T> ret(v1);
for (int i = 0; i < ret.size(); ++i) {
ret[i] -= v2[i];
}
return ret;
}
template<typename T>
vector<T> &operator-=(vector<T> &v1, const vector<T> &v2) {
assert(v1.size() == v2.size());
for (int i = 0; i < v1.size(); ++i) {
v1[i] -= v2[i];
}
return v1;
}
template<typename T>
T operator*(const vector<T> &v1, const vector<T> &v2) {
assert(v1.size() == v2.size());
T ret = T();
for (int i = 0; i < v1.size(); ++i) {
ret += v1[i] * v2[i];
}
return ret;
}
template<typename T>
vector<T> operator*(const T &num, const vector<T> &v) {
vector<T> ret(v.size());
for (int i = 0; i < v.size(); ++i) {
ret[i] = num * v[i];
}
return ret;
}
template<typename T>
vector<T> &operator*=(vector<T> &v, const T &num) {
for (int i = 0; i < v.size(); ++i) {
v[i] *= v[i];
}
return v;
}
template<typename T>
vector<T> operator*(const vector<T> &v, const T &num) {
return num * v;
}
template<typename T>
vector<T> operator/(const vector<T> &v, const T &num) {
vector<T> ret(v.size());
for (int i = 0; i < v.size(); ++i) {
ret[i] = v[i] / num;
}
return ret;
}
template<typename T>
vector<T> &operator/=(vector<T> &v, const T &num) {
for (int i = 0; i < v.size(); ++i) {
v[i] = v[i] / num;
}
return v;
}
template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
if (v.empty()) {
return out;
}
out << v[0];
for (auto i = 1; i < v.size(); ++i) {
out << ", " << v[i];
}
return out;
}
#endif //UTK_BV_UTK_VECTOR_H