-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathV.hpp
More file actions
150 lines (121 loc) · 2.63 KB
/
V.hpp
File metadata and controls
150 lines (121 loc) · 2.63 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
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
#ifndef V_HPP
#define V_HPP
#include <vector>
#include <iostream>
//Template classes cannot be initialized outside of header
template<typename T>
class V{
public:
std::vector<T> v;
V<T>()=default;
V<T>(std::vector<T> _v): v{_v} {}
V<T>(std::initializer_list<T> const & _v): v{_v} {}
V<T>(int size, T value){
v = std::vector<T>(size,value);
}
V<T>& operator += (const V<T>& rhs){
for(int i = 0; i< v.size(); i++){
this->v[i] += rhs.v[i];
}
return *this;
}
V<T> operator + (V<T> const &r){
V<T> tmp = *this;
tmp+=r;
return tmp;
}
V<T>& operator += (T a){
for(int i = 0; i< v.size(); i++){
this->v[i] += a;
}
return *this;
}
V<T> operator+ (T a){
V<T> tmp = *this;
tmp+=a;
return tmp;
}
V<T>& operator -= (const V<T>& rhs){
for(int i = 0; i< v.size(); i++){
this->v[i] -= rhs.v[i];
}
return *this;
}
V<T> operator - (V<T> const &r){
V<T> tmp = *this;
tmp-=r;
return tmp;
}
V<T>& operator-= (T a){
for(int i = 0; i< v.size(); i++){
this->v[i] -= a;
}
return *this;
}
V<T> operator - (T a){
V<T> tmp = *this;
tmp-=a;
return tmp;
}
V<T> operator* (T a){
V<T> tmp = *this;
for(int i = 0; i< v.size(); i++){
tmp.v[i] *= a;
}
return tmp;
}
V<T> operator/ (T a){
V<T> tmp = *this;
for(int i = 0; i< v.size(); i++){
tmp.v[i] /= a;
}
return tmp;
}
T & operator[] (int i){
return v[i];
}
T operator[] (int i) const{
return v[i];
}
int size(){
return v.size();
}
auto begin(){
return v.begin();
}
auto end(){
return v.end();
}
template <class... Args>
void emplace_back(Args&&... args){
v.emplace_back(args ...);
}
void clear(){
v.clear();
}
void pop_back(){
v.pop_back();
}
void print(){
for(T el : v){
std::cout << " " << el;
}
std::cout << std::endl;
}
double sum(){
double sum = 0;
for(int i = 0; i < size(); i++){
sum += v[i];
}
return sum;
}
double norm(){
double norm = 0;
for(int i = 0; i < size(); i++){
norm += v[i] * v[i];
}
return norm;
}
// I might generalize the former 2 methods with "norm_p(int p)", but it is not useful
};
#endif