-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataVector.cpp
More file actions
79 lines (62 loc) · 1.61 KB
/
DataVector.cpp
File metadata and controls
79 lines (62 loc) · 1.61 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
#include "DataVector.h"
#include <iostream>
#include <cmath>
using namespace std;
DataVector::DataVector(int dimension):v(dimension,0.0){}
DataVector::DataVector(const DataVector& other_v):v(other_v.v){}
DataVector::~DataVector(){}
DataVector & DataVector::operator=(const DataVector &other_v){
if(this!=&other_v){
v=other_v.v;
}
return *this;
}
void DataVector::setDimension(int dimension){
v.resize(dimension);
}
DataVector DataVector::operator+(const DataVector &other_v){
DataVector ans(v.size());
for(int i=0;i<v.size();i++){
ans.v[i]=v[i]+other_v.v[i];
}
}
DataVector DataVector::operator-(const DataVector &other_v){
DataVector ans(v.size());
for(int i=0;i<v.size();i++){
ans.v[i]=v[i]-other_v.v[i];
}
return ans;
}
double DataVector::operator*(const DataVector &other_v){
double ans=0.0;
for(int i=0;i<v.size();i++){
ans+=v[i]*other_v.v[i];
}
return ans;
}
double DataVector::dist(const DataVector &other_v)const{
if (v.size() != other_v.v.size()) {
cout<<"Vectors are different in size"<<endl;
}
double sum=0.0;
for(int i=0;i<v.size();i++){
double diff= v[i]-other_v.v[i];
sum += diff * diff;
}
// cout << "Inside dist function: sum = " << sum << endl;
return sqrt(sum);
}
double DataVector::norm(){
double sum=0.0;
sum=(*this)*(*this);
return sqrt(sum);
}
ostream& operator<<(ostream& os, const DataVector& object) {
for (double data : object.v) {
os << data << ' ';
}
return os;
}
void DataVector::addValue(double data){
v.push_back(data);
}