-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.cpp
More file actions
104 lines (82 loc) · 2.29 KB
/
Copy pathvector.cpp
File metadata and controls
104 lines (82 loc) · 2.29 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
#include "vector.h"
#include <stdexcept>
#include <iostream>
#include <cmath>
geom_vector::geom_vector(std::vector<double> &comps) : m_comps(comps) {};
geom_vector::geom_vector(std::initializer_list<double> comps) : m_comps(comps) {};
double& geom_vector::operator[](int index) {
if (index < 0 || index >= m_comps.size()) {
throw std::out_of_range("Indice non valido");
}
return m_comps[index];
};
double geom_vector::operator[](int index) const {
if (index < 0 || index >= m_comps.size()) {
throw std::out_of_range("Indice non valido");
}
return m_comps[index];
};
int geom_vector::size() const {
return static_cast<int>(m_comps.size());
}
geom_vector geom_vector::operator+(const geom_vector b) const {
std::vector<double> out_vec;
geom_vector a = *this;
int s = a.size();
for (int i=0; i<s; i++) {
double t = a[i] + b[i];
out_vec.push_back(t);
}
return geom_vector(out_vec);
};
void geom_vector::print() const {
std::cout << "(";
for (int i=0; i<this->size(); i++)
std::cout << (*this)[i] << " ";
std::cout << ")\n";
}
geom_vector geom_vector::operator*(const double b) const {
std::vector<double> out_vec;
geom_vector a = *this;
int s = a.size();
for (int i=0; i<s; i++) {
double t = a[i] * b;
out_vec.push_back(t);
}
return geom_vector(out_vec);
}
double geom_vector::operator*(const geom_vector b) const {
geom_vector a = *this;
int s = a.size();
double out =0;
for (int i=0; i<s; i++) {
out += a[i] * b[i];
}
return out;
}
geom_vector geom_vector::operator-(const geom_vector b) const {
std::vector<double> out_vec;
geom_vector a = *this;
int s = a.size();
for (int i=0; i<s; i++) {
double t = a[i] - b[i];
out_vec.push_back(t);
}
return geom_vector(out_vec);
};
geom_vector geom_vector::operator/(const double b) const {
std::vector<double> out_vec;
geom_vector a = *this;
int s = a.size();
for (int i=0; i<s; i++) {
double t = a[i] / b;
out_vec.push_back(t);
}
return geom_vector(out_vec);
};
double geom_vector::norm() const {
double t = 0;
for (int i=0; i<(*this).size(); i++)
t += pow((*this)[i], 2);
return pow(t, 0.5);
}