-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.hpp
50 lines (34 loc) · 876 Bytes
/
Vector.hpp
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
#ifndef VECTOR_HPP
#define VECTOR_HPP
#include <vector>
#include <cmath>
#include <iostream>
class Vector
{
public:
Vector(int n) : x_(n) {}
int dim() const {return x_.size();}
const double& operator[](int i) const {return x_[i];}
double& operator[](int i) {return x_[i];}
Vector operator*(const double& a) const ;
double operator*(const Vector& v) const ;
Vector operator+(const Vector& v) const ;
Vector operator-(const Vector& v) const ;
Vector operator-() const ;
double norm2() const {return ::sqrt((*this)*(*this));}
private:
std::vector<double> x_;
};
inline Vector operator*(const double& a, const Vector& v) {return v*a;}
inline std::ostream& operator<<(std::ostream& os, const Vector& v)
{
os << "[";
for (int i=0; i<v.dim(); i++)
{
if (i!=0) os << ", ";
os << v[i];
}
os << "]";
return os;
}
#endif