-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix.cpp
319 lines (287 loc) · 7.56 KB
/
matrix.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//
// matrix.cpp
//
// Created by Furkanicus on 12/04/15.
// Copyright (c) 2015 Furkan. All rights reserved.
//
#include "matrix.h"
using namespace std;
// Constructor for Any Matrix
Matrix::Matrix(unsigned rowSize, unsigned colSize, double initial){
m_rowSize = rowSize;
m_colSize = colSize;
m_matrix.resize(rowSize);
for (unsigned i = 0; i < m_matrix.size(); i++)
{
m_matrix[i].resize(colSize, initial);
}
}
// Constructor for Given Matrix
Matrix::Matrix(const char * fileName){
ifstream file_A(fileName); // input file stream to open the file A.txt
// Task 1
// Keeps track of the Column and row sizes
int colSize = 0;
int rowSize = 0;
// read it as a vector
string line_A;
int idx = 0;
double element_A;
double *vector_A = nullptr;
if (file_A.is_open() && file_A.good())
{
// cout << "File A.txt is open. \n";
while (getline(file_A, line_A))
{
rowSize += 1;
stringstream stream_A(line_A);
colSize = 0;
while (1)
{
stream_A >> element_A;
if (!stream_A)
break;
colSize += 1;
double *tempArr = new double[idx + 1];
copy(vector_A, vector_A + idx, tempArr);
tempArr[idx] = element_A;
vector_A = tempArr;
idx += 1;
}
}
}
else
{
cout << " WTF! failed to open. \n";
}
int j;
idx = 0;
m_matrix.resize(rowSize);
for (unsigned i = 0; i < m_matrix.size(); i++) {
m_matrix[i].resize(colSize);
}
for (int i = 0; i < rowSize; i++)
{
for (j = 0; j < colSize; j++)
{
this->m_matrix[i][j] = vector_A[idx];
idx++;
}
}
m_colSize = colSize;
m_rowSize = rowSize;
delete [] vector_A; // Tying up loose ends
}
// Copy Constructor
Matrix::Matrix(const Matrix &B)
{
this->m_colSize = B.getCols();
this->m_rowSize = B.getRows();
this->m_matrix = B.m_matrix;
}
Matrix::~Matrix(){
}
// Addition of Two Matrices
Matrix Matrix::operator+(Matrix &B){
Matrix sum(m_colSize, m_rowSize, 0.0);
unsigned i,j;
for (i = 0; i < m_rowSize; i++)
{
for (j = 0; j < m_colSize; j++)
{
sum(i,j) = this->m_matrix[i][j] + B(i,j);
}
}
return sum;
}
// Subtraction of Two Matrices
Matrix Matrix::operator-(Matrix & B){
Matrix diff(m_colSize, m_rowSize, 0.0);
unsigned i,j;
for (i = 0; i < m_rowSize; i++)
{
for (j = 0; j < m_colSize; j++)
{
diff(i,j) = this->m_matrix[i][j] - B(i,j);
}
}
return diff;
}
// Multiplication of Two Matrices
Matrix Matrix::operator*(Matrix & B){
Matrix multip(m_rowSize,B.getCols(),0.0);
if(m_colSize == B.getRows())
{
unsigned i,j,k;
double temp = 0.0;
for (i = 0; i < m_rowSize; i++)
{
for (j = 0; j < B.getCols(); j++)
{
temp = 0.0;
for (k = 0; k < m_colSize; k++)
{
temp += m_matrix[i][k] * B(k,j);
}
multip(i,j) = temp;
//cout << multip(i,j) << " ";
}
//cout << endl;
}
return multip;
}
else
{
return "Error";
}
}
// Scalar Addition
Matrix Matrix::operator+(double scalar){
Matrix result(m_rowSize,m_colSize,0.0);
unsigned i,j;
for (i = 0; i < m_rowSize; i++)
{
for (j = 0; j < m_colSize; j++)
{
result(i,j) = this->m_matrix[i][j] + scalar;
}
}
return result;
}
// Scalar Subraction
Matrix Matrix::operator-(double scalar){
Matrix result(m_rowSize,m_colSize,0.0);
unsigned i,j;
for (i = 0; i < m_rowSize; i++)
{
for (j = 0; j < m_colSize; j++)
{
result(i,j) = this->m_matrix[i][j] - scalar;
}
}
return result;
}
// Scalar Multiplication
Matrix Matrix::operator*(double scalar){
Matrix result(m_rowSize,m_colSize,0.0);
unsigned i,j;
for (i = 0; i < m_rowSize; i++)
{
for (j = 0; j < m_colSize; j++)
{
result(i,j) = this->m_matrix[i][j] * scalar;
}
}
return result;
}
// Scalar Division
Matrix Matrix::operator/(double scalar){
Matrix result(m_rowSize,m_colSize,0.0);
unsigned i,j;
for (i = 0; i < m_rowSize; i++)
{
for (j = 0; j < m_colSize; j++)
{
result(i,j) = this->m_matrix[i][j] / scalar;
}
}
return result;
}
// Returns value of given location when asked in the form A(x,y)
double& Matrix::operator()(const unsigned &rowNo, const unsigned & colNo)
{
return this->m_matrix[rowNo][colNo];
}
// No brainer - returns row #
unsigned Matrix::getRows() const
{
return this->m_rowSize;
}
// returns col #
unsigned Matrix::getCols() const
{
return this->m_colSize;
}
// Take any given matrices transpose and returns another matrix
Matrix Matrix::transpose()
{
Matrix Transpose(m_colSize,m_rowSize,0.0);
for (unsigned i = 0; i < m_colSize; i++)
{
for (unsigned j = 0; j < m_rowSize; j++) {
Transpose(i,j) = this->m_matrix[j][i];
}
}
return Transpose;
}
// Prints the matrix beautifully
void Matrix::print() const
{
cout << "Matrix: " << endl;
for (unsigned i = 0; i < m_rowSize; i++) {
for (unsigned j = 0; j < m_colSize; j++) {
cout << "[" << m_matrix[i][j] << "] ";
}
cout << endl;
}
}
// Returns 3 values
//First: Eigen Vector
//Second: Eigen Value
//Third: Flag
tuple<Matrix, double, int> Matrix::powerIter(unsigned rowNum, double tolerance){
// Picks a classic X vector
Matrix X(rowNum,1,1.0);
// Initiates X vector with values 1,2,3,4
for (unsigned i = 1; i <= rowNum; i++) {
X(i-1,0) = i;
}
int errorCode = 0;
double difference = 1.0; // Initiall value greater than tolerance
unsigned j = 0;
unsigned location;
// Defined to find the value between last two eigen values
vector<double> eigen;
double eigenvalue = 0.0;
eigen.push_back(0.0);
while(abs(difference) > tolerance) // breaks out when reached tolerance
{
j++;
// Normalize X vector with infinite norm
for (int i = 0; i < rowNum; ++i)
{
eigenvalue = X(0,0);
if (abs(X(i,0)) >= abs(eigenvalue))
{
// Take the value of the infinite norm as your eigenvalue
eigenvalue = X(i,0);
location = i;
}
}
if (j >= 5e5) {
cout << "Oops, that was a nasty complex number wasn't it?" << endl;
cout << "ERROR! Returning code black, code black!";
errorCode = -1;
return make_tuple(X,0.0,errorCode);
}
eigen.push_back(eigenvalue);
difference = eigen[j] - eigen[j-1];
// Normalize X vector with its infinite norm
X = X / eigenvalue;
// Multiply The matrix with X vector
X = (*this) * X;
}
// Take the X vector and what you've found is an eigenvector!
X = X / eigenvalue;
return make_tuple(X,eigenvalue,errorCode);
}
Matrix Matrix::deflation(Matrix &X, double &eigenvalue)
{
// Deflation formula exactly applied
double denominator = eigenvalue / (X.transpose() * X)(0,0);
Matrix Xtrans = X.transpose();
Matrix RHS = (X * Xtrans);
Matrix RHS2 = RHS * denominator;
Matrix A2 = *this - RHS2;
return A2;
}