-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.h
226 lines (180 loc) · 5.34 KB
/
Vector.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#ifndef VECTOR_H
#define VECTOR_H
#include <stdexcept>
#include <iostream>
#include <iomanip>
#include <vector>
template<class T>
class abVector{
public:
abVector();
abVector(std::vector<T>& inputData);
abVector(int numdims);
~abVector();
int GetNumDims() const;
T GetElement(int ind) const;
bool SetElement(int ind, const T& val);
T Norm() const;
abVector<T> Normalized() const;
abVector<T> operator+ (const abVector<T>& rhs);
abVector<T> operator- (const abVector<T>& rhs);
abVector<T> operator* (const T& rhs);
template<class U>
friend abVector<U> operator* (const U& lhs, const abVector<U>& rhs);
template<class U>
friend std::ostream& operator<< (std::ostream& os, const abVector<U>& v);
static T dot(const abVector<T>& a, const abVector<T>& b);
static abVector<T> cross(const abVector<T>& a, const abVector<T>& b);
private:
std::vector<T> m_vectorData;
int m_nDims;
};
template<class T>
abVector<T>::abVector() {
m_nDims = 0;
m_vectorData = std::vector<T>();
}
template<class T>
abVector<T>::abVector(std::vector<T>& inputData) {
m_nDims = inputData.size();
m_vectorData = inputData;
}
template<class T>
abVector<T>::abVector(int numdims) {
m_nDims = numdims;
m_vectorData = std::vector<T>(m_nDims, 0);
}
template<class T>
abVector<T>::~abVector() {
// nothing, since vector(STL) class takes care of it
}
template<class T>
int abVector<T>::GetNumDims() const {
return m_nDims;
}
template<class T>
T abVector<T>::GetElement(int ind) const {
if(ind >= m_nDims) {
throw std::invalid_argument("Index is out of range.");
} else {
return m_vectorData[ind];
}
}
template<class T>
bool abVector<T>::SetElement(int ind, const T& val) {
if(ind >= m_nDims) {
throw std::invalid_argument("Index is out of range.");
} else {
m_vectorData[ind] = val;
return true;
}
}
template<class T>
T abVector<T>::Norm() const {
int n = GetNumDims();
T ans = 0;
for(int i = 0; i < n; i++) {
T x = GetElement(i);
ans += x * x;
}
ans = sqrt(ans);
return ans;
}
template<class T>
abVector<T> abVector<T>::Normalized() const {
T norm = Norm();
int n = GetNumDims();
std::vector<T> inputData;
for(int i = 0; i < n; i++) {
inputData.push_back(GetElement(i) / norm);
}
abVector<T> res(inputData);
return res;
}
template<class T>
abVector<T> abVector<T>::operator+ (const abVector<T>& rhs) {
if(GetNumDims() != rhs.GetNumDims()) {
throw std::invalid_argument("Dimensions of lhs and rhs should be equal for addition.");
} else {
int n = rhs.GetNumDims();
std::vector<T> res;
for(int i = 0; i < n; i++) {
res.push_back(GetElement(i) + rhs.GetElement(i));
}
abVector<T> resultData(res);
return resultData;
}
}
template<class T>
abVector<T> abVector<T>::operator- (const abVector<T>& rhs) {
if(GetNumDims() != rhs.GetNumDims()) {
throw std::invalid_argument("Dimensions of lhs and rhs should be equal for subtraction.");
} else {
int n = rhs.GetNumDims();
std::vector<T> res;
for(int i = 0; i < n; i++) {
res.push_back(GetElement(i) - rhs.GetElement(i));
}
abVector<T> resultData(res);
return resultData;
}
}
template<class T>
abVector<T> abVector<T>::operator* (const T& rhs) {
int n = GetNumDims();
std::vector<T> res;
for(int i = 0; i < n; i++) {
res.push_back(GetElement(i) * rhs);
}
abVector<T> resultData(res);
return resultData;
}
template<class T>
abVector<T> operator* (const T& lhs, const abVector<T>& rhs) {
int n = rhs.GetNumDims();
std::vector<T> res;
for(int i = 0; i < n; i++) {
res.push_back(lhs * rhs.GetElement(i));
}
abVector<T> resultData(res);
return resultData;
}
template<class T>
T abVector<T>::dot(const abVector<T>& a, const abVector<T>& b) {
if(a.GetNumDims() != b.GetNumDims()) {
throw std::invalid_argument("Number of dimensions should be equal for dot product.");
}
int n = a.GetNumDims();
T ans = 0.0;
for(int i = 0; i < n; i++) {
ans += (a.GetElement(i) * b.GetElement(i));
}
return ans;
}
template<class T>
abVector<T> abVector<T>::cross(const abVector<T>& a, const abVector<T>& b) {
if(a.GetNumDims() != b.GetNumDims()) {
throw std::invalid_argument("Number of dimensions should be equal for dot product.");
}
int n = a.GetNumDims();
if(n != 3) {
throw std::invalid_argument("Cross product can only be calculated for 3 dimensions.");
}
std::vector<T> inputData(3, 0);
inputData[0] = a.GetElement(1) * b.GetElement(2) - b.GetElement(1) * a.GetElement(2);
inputData[1] = a.GetElement(2) * b.GetElement(0) - b.GetElement(2) * a.GetElement(0);
inputData[2] = a.GetElement(0) * b.GetElement(1) - b.GetElement(0) * a.GetElement(1);
abVector<T> ans(inputData);
return ans;
}
template <class T>
std::ostream& operator<<(std::ostream& os, const abVector<T>& v) {
int n = v.GetNumDims();
os << "Vector (Dimensions = " << n << "):\n";
for (int i = 0; i < n; ++i) {
os << v.GetElement(i) << " ";
}
os << "\n";
return os;
}
#endif