-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.cpp
246 lines (213 loc) · 4.52 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <cmath>
#include <cassert>
#include <string>
#include <cstdlib>
#include <fstream>
#include "vector.h"
#include "binaryIO.h"
using namespace std;
///////////////////////////////////////////
//CONSTRUCTORS & DESTRUCTOR
//Copy
Vector::Vector(const Vector& otherVector){
mSize = otherVector.GetSize();
mData = new double[mSize];
for(int i=0; i<mSize; i++){
mData[i] = otherVector.mData[i];
}
}
//Size and Value
Vector::Vector(int size, double value){
mData = NULL;
assert(size>0);
mSize = size;
mData = new double[mSize];
for (int i = 0; i<mSize; i++){
mData[i] = value;
}
}
//File
Vector::Vector(string inPath, string name) {
mData = NULL;
int nR=0;
unsigned int nnz=0;
getHeaderInfo(nR, nnz, inPath, name);
mSize = nR;
mData = new double[nR];
brVector(mData, nR, inPath, name);
}
//Destructor
Vector::~Vector(){
delete[] mData;
}
///////////////////////////////////////////////////
//READING AND EVALUATING
//Size return
int Vector::GetSize() const{return mSize;}
//Assigning and reading
double& Vector::operator[](int i){return mData[i];}
//Read only
double Vector::Read(int i) const{return mData[i];}
//////////////////////////////////////////////////////
//OPERATORS OVERLOADING
//Equals
Vector& Vector::operator=(const Vector& otherVector){
assert(mSize == otherVector.mSize);
for(int i=0;i<mSize;i++){
mData[i] = otherVector.mData[i];
}
return *this;
}
//Addition
Vector Vector::operator+() const{
Vector v(mSize);
for(int i=0; i<mSize;i++){
v[i] = mData[i];
}
return v;
}
Vector Vector::operator+(const Vector& v1) const{
assert(mSize == v1.mSize);
Vector v(mSize);
for(int i=0;i<mSize;i++){
v[i] = mData[i] + v1.mData[i];
}
return v;
}
Vector Vector::operator+(double a) const{
Vector v(mSize);
for(int i=0;i<mSize;i++){
v[i] = mData[i] + a;
}
return v;
}
void Vector::operator+=(const Vector& v1){
assert(mSize == v1.mSize);
for(int i=0;i<mSize;i++){
mData[i] += v1.mData[i];
}
}
//Substraction
Vector Vector::operator-() const{
Vector v(mSize);
for(int i=0; i<mSize;i++){
v[i] = -mData[i];
}
return v;
}
Vector Vector::operator-(const Vector& v1) const{
assert(mSize == v1.mSize);
Vector v(mSize);
for(int i=0;i<mSize;i++){
v[i] = mData[i] - v1.mData[i];
}
return v;
}
Vector Vector::operator-(double a) const{
Vector v(mSize);
for(int i=0;i<mSize;i++){
v[i] = mData[i] - a;
}
return v;
}
void Vector::operator-=(const Vector& v1){
assert(mSize == v1.mSize);
for(int i=0;i<mSize;i++){
mData[i] -= v1.mData[i];
}
}
//Product
Vector Vector::operator*(double a) const{
Vector v(mSize);
for(int i=0;i<mSize;i++){
v[i]=a*mData[i];
}
return v;
}
void Vector::operator*=(double a){
for(int i=0;i<mSize;i++){
mData[i] *= a;
}
}
//Division
Vector Vector::operator/(double a) const{
Vector v(mSize);
for(int i=0;i<mSize;i++){
v[i]=mData[i]/a;
}
return v;
}
void Vector::operator/=(double a){
for(int i=0;i<mSize;i++){
mData[i] /= a;
}
}
////////////////////////////////////////////
//ADDING AND DELETING ELEMENTS
void Vector::resize(int newSize){
int diff = newSize - mSize;
int resizeSize=0;
double *newData = new double[newSize];
if(newSize >= mSize){resizeSize = mSize;}
else{resizeSize = newSize;}
for(int i = 0 ; i < resizeSize ; i++){
newData[i] = mData[i];
}
delete[] mData;
mData = newData;
mSize++;
}
void Vector::addVal(double a){
this->resize(mSize + 1);
mData[mSize-1] = a;
}
////////////////////////////////////////////
//OTHER METHODS
//Inverse
Vector Vector::inv() const{
Vector v(mSize);
for(int i=0;i<mSize;i++){
if(mData[i] != 0){
v[i] = 1.0 / mData[i];
}
}
return v;
}
//Norm
double Vector::norm() const{
double norm = 0.0;
for(int i=0;i<mSize;i++){
norm += mData[i] * mData[i];
}
norm = sqrt(norm);
return norm;
}
//Writing
void Vector::write(string fileName) const{
ofstream out;
out.open(fileName.c_str());
for(int i = 0 ; i < mSize ; i++){
out << mData[i] << endl;
}
out.close();
}
////////////////////////////////////////////
//FRIEND FUNCTIONS
//Scalar product
double scalProduct(const Vector& v1, const Vector& v2){
assert(v1.mSize == v2.mSize);
double scp = 0.0;
for(int i=0;i<v1.mSize;i++){
scp += v1.mData[i] * v2.mData[i];
}
return scp;
}
//Vectorial product (member * member)
Vector vectProduct(const Vector& v1, const Vector& v2){
assert(v1.mSize == v2.mSize);
Vector v(v1.mSize);
for(int i=0;i<v.GetSize();i++){
v[i] = v1.mData[i] * v2.mData[i];
}
return v;
}