-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMorpheus_Vector.cpp
More file actions
163 lines (121 loc) · 2.6 KB
/
Morpheus_Vector.cpp
File metadata and controls
163 lines (121 loc) · 2.6 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
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
/**
* @file
* \brief Defines a Vector class
*
* @author Alicia Klinvex
*/
#include <iostream>
#include <cassert>
#include <cmath>
#include "Morpheus_Vector.h"
namespace Morpheus {
Vector::Vector(const int numElements)
{
assert(numElements > 0);
numElements_ = numElements;
// Allocate memory for the data
data_ = new double[numElements_];
}
Vector::~Vector()
{
// Release the memory
delete[] data_;
}
double& Vector::operator[](const int subscript)
{
// Make sure the subscript is valid
// Terminate the program if it's not
assert(subscript >= 0 && subscript < numElements_);
return data_[subscript];
}
const double& Vector::operator[](const int subscript) const
{
// Make sure the subscript is valid
// Terminate the program if it's not
assert(subscript >= 0 && subscript < numElements_);
return data_[subscript];
}
int Vector::getNumElements() const
{
return numElements_;
}
void Vector::setValue(const double alpha)
{
for(int i=0; i<numElements_; i++)
{
data_[i] = alpha;
}
}
void Vector::scale(const double alpha)
{
for(int i=0; i<numElements_; i++)
{
data_[i] = alpha * data_[i];
}
}
void Vector::add(const Vector& b, Vector& sum) const
{
// Make sure all three vectors are the same size
assert(this->numElements_ == b.numElements_);
assert(this->numElements_ == sum.numElements_);
// Compute the sum of each entry
for(int i=0; i<this->numElements_; i++)
{
sum.data_[i] = this->data_[i] + b.data_[i];
}
}
double Vector::dot(const Vector& b) const
{
// Make sure the vectors are the same size
assert(this->numElements_ == b.numElements_);
double sum;
// Compute the sum of all the products
for(int i=0; i<this->numElements_; i++)
{
sum = sum + (this->data_[i] * b.data_[i]);
}
return sum;
}
double Vector::norm1() const
{
double sum = 0;
// Compute the sum of all the entries magnitudes
for(int i=0; i<numElements_; i++)
{
sum = sum + data_[i];
}
return sum;
}
double Vector::normInf() const
{
double maxVal = 0;
// Find the biggest entry
for(int i=0; i<numElements_; i++)
{
double absVal = data_[i];
if(absVal > maxVal)
{
maxVal = absVal;
}
}
return maxVal;
}
double Vector::norm2() const
{
double sum = 0;
// Compute the sum of squares
for(int i=0; i<numElements_; i++)
{
sum = sum + (data_[i]*data_[i]);
}
return sum;
}
void Vector::print() const
{
std::cout << "Vector with " << numElements_ << " entries\n";
for(int i=0; i<numElements_; i++)
{
std::cout << "data[" << i << "] = " << data_[i] << std::endl;
}
}
} /* namespace Morpheus */