-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.cpp
137 lines (112 loc) · 2.82 KB
/
Vector.cpp
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
/* $Rev: 250 $ */
#include "Vector.h"
#include <cmath>
#include <assert.h>
// Vector constructor
Vector::Vector(size_t size) : Matrix(size, 1) {
}
// Vector copy constructor
Vector::Vector(const Vector& vec) : Matrix(vec) {
}
// Vector from Matrix constructor
Vector::Vector(const Matrix& mat) : Matrix(mat) {
assert(cols_ == 1);
}
// Vector destructor
Vector::~Vector() {
}
// Vector assignement operator
Vector& Vector::operator=(const Vector& vec) {
if (this != &vec) {
Matrix::operator=(vec);
}
return *this;
}
// Vector element access
double& Vector::operator()(size_t ix) {
return data_[ix];
}
// Vector element access (const version)
const double& Vector::operator()(size_t ix) const {
return data_[ix];
}
// Unary minus
Vector Vector::operator-() const {
return Vector(Matrix::operator-());
}
// Vector addition
Vector operator+(const Vector& lhs, const Vector& rhs) {
Vector result(lhs);
result += rhs;
return result;
}
// Vector adidition-assignement
Vector& Vector::operator+=(const Vector& vec) {
this->Matrix::operator+=(vec);
return *this;
}
// Vector difference
Vector operator-(const Vector& lhs, const Vector& rhs) {
Vector result(lhs);
result -= rhs;
return result;
}
// Vector subtraction-assignment
Vector& Vector::operator-=(const Vector& vec) {
this->Matrix::operator-=(vec);
return *this;
}
// Vector scalar multiplication from the left
Vector operator*(double s, const Vector& vec) {
Vector result(vec);
result *= s;
return result;
}
// Vector scalar multiplication from the right
Vector operator*(const Vector& vec, double s) {
Vector result(vec);
result *= s;
return result;
}
// Vector scalar multiplication-assignment
Vector& Vector::operator*=(double s) {
this->Matrix::operator*=(s);
return *this;
}
// Vector scalar division
Vector operator/(const Vector& vec, double s) {
Vector result(vec);
result /= s;
return result;
}
// Vector scalar division-assignment
Vector& Vector::operator/=(double s) {
this->Matrix::operator/=(s);
return *this;
}
// Vector dot product
double Vector::dot(const Vector& vec) const {
assert(data_.size() == vec.data_.size());
double sum = 0;
for (size_t i = 0; i < data_.size(); ++i) {
sum += data_[i] * vec.data_[i];
}
return sum;
}
// Vector cross product
Vector Vector::cross(const Vector& vec) const {
assert(rows_ == 3 && vec.rows_ == 3);
Vector result(3);
result(0) = data_[1]*vec.data_[2] - data_[2]*vec.data_[1];
result(1) = data_[2]*vec.data_[0] - data_[0]*vec.data_[2];
result(2) = data_[0]*vec.data_[1] - data_[1]*vec.data_[0];
return result;
}
// Vector norm
double Vector::norm() const {
return std::sqrt(dot(*this));
}
// Squared Vector norm
double Vector::squaredNorm() const {
return dot(*this);
}