-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvect.cpp
More file actions
71 lines (68 loc) · 2.42 KB
/
vect.cpp
File metadata and controls
71 lines (68 loc) · 2.42 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
#ifndef VECT_CPP_GUARD
#define VECT_CPP_GUARD
#include <numeric>
#include <algorithm>
#include <functional>
using namespace std;
template<typename T,int n>
class vect{
T val[n];
public:
//always constructs the zero vector
vect(){ fill_n(val,n,(T)0); }
vect(const vect<T,n>& other){ copy(other.val,other.val+n,val);}
vect<T,n>& operator=(const vect<T,n>& other){
if(&other==this) return *this;
copy(other.val,other.val+n,val);
return *this;
}
#define V_OP(op) \
vect<T,n>& operator op ## =(const vect<T,n>& other){ \
for(int i=0;i<n;i++) val[i] op ##= other.val[i]; \
return *this; \
}
V_OP(+);
V_OP(-);
#undef V_OP
vect<T,n> operator-() const{
vect<T,n> ret;
transform(val,val+n,ret.val,negate<T>());
return ret;
}
//note, this is the length squared norm
T norm() const{ return (*this)*(*this); }
bool operator==(const vect<T,n>& other) const{
return equal(val,val+n,other.val); }
bool operator!=(const vect<T,n>& other) const{ return !(this==other); }
//dot product
T operator*(const vect<T,n>& other) const{
return inner_product(val,val+n,other.val,(T)0);}
#define S_OP(op) \
vect<T,n>& operator op ## =(const T& k){ \
for(int i=0;i<n;i++) val[i] op ## = k; \
return *this; \
}
S_OP(*);
S_OP(/);
#undef S_OP
const T& operator[](int index) const { return val[index]; }
T& operator[](int index){ return val[index]; }
};
#define WRAP(op,t2) \
template<typename T,int n> \
vect<T,n> operator op (const vect<T,n>& a,t2 b){ \
vect<T,n> ret (a); \
return (ret op##= b); \
}
#define COMMA ,
WRAP(+,const vect<T COMMA n>&)
WRAP(-,const vect<T COMMA n>&)
WRAP(*,const T&)
WRAP(/,const T&)
#undef WRAP
//backwards versions
template<typename T, int n>
vect<T,n> operator*(const T& k, const vect<T,n>& a){ return a*k; }
template<typename T, int n>
vect<T,n> operator/(const T& k, const vect<T,n>& a){ return a/k; }
#endif