/
matrix.cpp
302 lines (279 loc) · 6.16 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
#include "matrix.h"
#include "polynomial.h"
#include <iostream>
#include <cmath>
#include <sstream>
#include <string>
#define _USE_MATH_DEFINES
using namespace std;
void moveDownZeros(double ** mat, int maxRows, int colNum, int maxCols)
{
int dRow = 0;
while ((dRow < maxRows) && (*(*(mat + dRow) + colNum) == 0))
{
dRow++;
}
if (dRow == maxRows)
{
//If colNum < maxCols - 1, recursion for next col
if (colNum < maxCols - 1)
moveDownZeros(mat, maxRows, colNum + 1, maxCols);
}
else
{
//swap dRow with row zero
double * tempRow = *mat;
*mat = *(mat + dRow);
*(mat + dRow) = tempRow;
//if maxRows > 2 and colNum < maxCols - 1, recursion for next col + row
if ((maxRows > 2) && (colNum < maxCols - 1))
moveDownZeros(mat + 1, maxRows - 1, colNum + 1, maxCols);
}
}
//row r1 -> r1 - a * r2
void subtMultRow(double ** mat, int r1, int r2, double a, int cols)
{
for (int i = 0; i < cols; i++)
{
*(*(mat + r1) + i) -= a * *(*(mat + r2) + i);
}
}
void reduceDir(double ** mat, int dRow, int times, int col, int cols)
{
if (*(*mat + col) != 0)
{
for (int i = 1; i <= times; i++)
{
subtMultRow(mat, i * dRow, 0, *(*(mat + i * dRow) + col) /
*(*mat + col), cols);
}
}
}
//r1 -> r1 * a
void multRow (double ** mat, double a, int cols)
{
for (int i = 0; i < cols; i++)
{
*(*mat + i) *= a;
}
}
Eigenpair::Eigenpair(double val, double * vec)
{
value = val;
vector = vec;
}
Matrix::Matrix() {}
Matrix::Matrix(int rs, int cls)
{
rows = rs;
cols = cls;
//double * tmp[rows];
mat = new double * [rows];
for (int row = 0; row < rows; row++)
{
mat[row] = new double[cols];
for (int col = 0; col < cols; col++)
{
mat[row][col] = 0;
}
}
}
Matrix Matrix::copy()
{
Matrix cop;
cop.rows = rows;
cop.cols = cols;
cop.mat = new double * [rows];
for (int row = 0; row < rows; row++)
{
cop.mat[row] = new double[cols];
for (int col = 0; col < cols; col++)
{
cop.mat[row][col] = mat[row][col];
}
}
return cop;
}
void Matrix::set(int r, int c, double val)
{
mat[r][c] = val;
}
double Matrix::get(int r, int c)
{
return mat[r][c];
}
void Matrix::add(int r, int c, double val)
{
mat[r][c] += val;
}
void Matrix::print()
{
cout << "------" << endl;
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
stringstream strStr;
strStr << round(pow(10, PRINT_SIZE + 2) * mat[r][c]) / pow(10, PRINT_SIZE + 2);
string str = strStr.str();
if (str.size() > PRINT_SIZE) {
str = str.substr(0, PRINT_SIZE);
if (str.at(PRINT_SIZE - 1) == '.')
str = str.substr(0, PRINT_SIZE - 1);
}
cout << str;
for (int i = 0; i < PRINT_SIZE + 1 - str.size(); i++)
{
cout << " ";
}
}
cout << endl;
}
cout << "------" << endl;
}
void Matrix::getCharPol(int rowNum, vector<int> exCols, double * pol)
{
if (rowNum == rows - 1)
{
pol[0] += mat[rowNum][exCols.at(0)];
if (rowNum == exCols.at(0))
pol[1] += -1;
}
else
{
int pmMult = 1;
for (int colInd = 0; colInd < exCols.size(); colInd++)
{
int col = exCols.at(colInd);
if ((mat[rowNum][col] != 0) || (col == rowNum))
{
exCols.erase(exCols.begin() + colInd);
double * locPol = new double[rows + 1];
for (int i = 0; i < rows + 1; i++)
{
locPol[i] = 0;
}
getCharPol(rowNum + 1, exCols, locPol);
//write locPol to answer
for (int i = 0; i < rows + 1; i++)
{
pol[i] += locPol[i] * mat[rowNum][col] * pmMult;
if ((col == rowNum) && (i != 0))
pol[i] -= locPol[i - 1] * pmMult;
}
exCols.insert(exCols.begin() + colInd, col);
pmMult *= -1;
}
}
}
}
void Matrix::rowReduce()
{
int minDim = min(rows, cols);
int nextCol = 0;
int curRow = 0;
//get into eschelon form
while (curRow < minDim - 1)
{
//make sure all zeroes go down
moveDownZeros(mat, rows, 0, cols);
while ((nextCol < cols) && (*(*(mat + curRow) + nextCol) == 0))
nextCol++;
if (nextCol == cols)
break;
reduceDir(mat + curRow, 1, rows - curRow - 1, nextCol, cols);
curRow++;
}
curRow--;
//finish row-reduced eschelon
while (curRow >= 0)
{
nextCol = 0;
while (*(*(mat + curRow) + nextCol) == 0)
nextCol++;
//print();
//cout << "a "<< nextCol << " " << curRow << endl;
multRow(mat + curRow, 1 / *(*(mat + curRow) + nextCol), cols);
//print();
reduceDir(mat + curRow, -1, curRow, nextCol, cols);
curRow--;
}
}
//finds null vector of row-reduced eschelon-form matrix=
void findNullVector(double ** mat, int rows, int cols, double * vec)
{
//go through each column, keeping track of current pivot row
//if value at row, col is 1, then col is pivot column
// store - summ of all other entries in row into the cell, inc pivot row
//else store 1
int row = 0;
for (int col = 0; col < cols; col++)
{
if (row >= rows)
{
*vec = 1;
}
else if (abs(*(*(mat + row) + col) - 1) < ZERO_THRESH)
{
*vec = 0;
//loop through all columns in pivot row, get negative
for (int chkCol = col + 1; chkCol < cols; chkCol++)
{
*vec -= *(*(mat + row) + chkCol);
}
row++;
}
else
{
*vec = 1;
}
vec++;
}
}
void Matrix::getEigenvector(double val, double * iVec)
{
Matrix copMat = copy();
for (int i = 0; i < rows; i++)
{
copMat.add(i, i, -val);
}
copMat.rowReduce();
//copMat.print();
findNullVector(copMat.mat, rows, cols, iVec);
}
vector<Eigenpair> Matrix::getEigenpairs()
{
//assume rows = cols
double * charPol = new double[rows + 1];
for (int i = 0; i < rows + 1; i++)
{
charPol[i] = 0;
}
vector<int> exCols;
for (int i = 0; i < cols; i++)
{
exCols.push_back(i);
}
//print();
getCharPol(0, exCols, charPol);
/*cout << "Characteristic polynomial: ";
for (int i = 0; i < rows + 1; i++)
{
cout << charPol[i] << " * X ^ " << i;
if (i != rows)
cout << " + ";
}
cout << endl;
//*///cout << charPol[0] << " + " << charPol[1] << " * X + " << charPol[2] << " * X^2" << endl;
Polynomial pol(charPol, rows + 1);
vector<double> eigVals = pol.getRoots();
//cout << eigVals[0] << ", " << eigVals[1] << endl;
vector<Eigenpair> egPrs;
for (int i = 0; i < eigVals.size(); i++)
{
double * eigVec = new double[rows];
getEigenvector(eigVals.at(i) > .00000000000001 ? eigVals.at(i) : 0, eigVec);
egPrs.push_back(Eigenpair(eigVals.at(i), eigVec));
}
return egPrs;
}