-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquareMatrix.cpp
More file actions
175 lines (145 loc) · 5.05 KB
/
SquareMatrix.cpp
File metadata and controls
175 lines (145 loc) · 5.05 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
164
165
166
167
168
169
170
171
172
173
174
175
#include "SquareMatrix.h"
#include "math.h"
SquareMatrix::SquareMatrix(const int dimension):
Matrix(dimension, dimension)
{
}
SquareMatrix::SquareMatrix(const int dimension, double* data) :
Matrix(dimension, dimension, data)
{
}
SquareMatrix::SquareMatrix(const SquareMatrix& input) :
Matrix(input)
{
}
SquareMatrix::SquareMatrix(const Matrix& input) :
Matrix(input)
{
if (input.GetNoOfRows() != input.GetNoOfColumns())
throw exception();
}
SquareMatrix::~SquareMatrix()
{
}
void SquareMatrix::LU(SquareMatrix& lmatrix, SquareMatrix& umatrix) const
{
// Apply the steps given in the Algorithm 2.1
umatrix = *this;
lmatrix = SquareMatrix::Identity(noOfColumns);
for (int k = 0; k < noOfColumns - 1; k++) {
for (int j = k + 1; j < noOfColumns; j++) {
lmatrix.data[GetIndex(j, k)] =
umatrix.data[GetIndex(j, k)] / umatrix.data[GetIndex(k, k)];
for (int m = k; m < noOfColumns; m++) {
umatrix.data[GetIndex(j, m)] -=
lmatrix.data[GetIndex(j, k)] * umatrix.data[GetIndex(k, m)];
}
}
}
}
void SquareMatrix::LU(SquareMatrix& lmatrix, SquareMatrix& umatrix, SquareMatrix& pivot) const
{
// Apply the steps given in the Algorithm 2.2
umatrix = *this;
lmatrix = SquareMatrix::Identity(noOfColumns);
pivot = SquareMatrix::Identity(noOfColumns);
for (int k = 0; k < noOfColumns - 1; k++) {
// Find row that has the absolute greatest element in kth column of U
double max = 0;
int index = 0;
for (int i = k; i < noOfRows; i++) {
if (max < abs(umatrix.GetEntry(i, k))) {
max = umatrix.GetEntry(i, k); // Update max if it is less than current entry.
index = i; // Keep the index since i will iterate until the end of kth row.
}
}
if (k != index) {
umatrix.ExchangeRows(k, index, k, noOfColumns - 1);
lmatrix.ExchangeRows(k, index, 0, k - 1);
pivot.ExchangeRows(k, index);
}
for (int j = k + 1; j < noOfColumns; j++) {
lmatrix.data[GetIndex(j, k)] =
umatrix.data[GetIndex(j, k)] / umatrix.data[GetIndex(k, k)];
for (int m = k; m < noOfColumns; m++) {
umatrix.data[GetIndex(j, m)] -=
lmatrix.data[GetIndex(j, k)] * umatrix.data[GetIndex(k, m)];
}
}
}
}
Matrix& SquareMatrix::Solve(const Matrix& B) const
{
// Ensure the dimensions of the B matrix
if (B.GetNoOfRows() != noOfRows || B.GetNoOfColumns() != 1)
throw exception();
// First decompose the A matrix
SquareMatrix U(noOfColumns), L(noOfColumns), P(noOfColumns);
LU(L, U, P);
// Solve the new problem LUx = Pb. First solve Ly = Pb and then Ux = y.
// Algorithm 3.1 gives the solution of Ly = Pb.
Matrix Pb(P * B);
double *y = new double[noOfRows]; // Column matrix
y[0] = Pb.GetEntry(0, 0) / L.GetEntry(0, 0);
for (int k = 1; k < noOfRows; k++) {
y[k] = Pb.GetEntry(k, 0);
for (int i = 0; i < k; i++)
y[k] -= L.GetEntry(k, i) * y[i];
y[k] /= L.GetEntry(k, k);
}
// Algorithm 3.2 gives the solution of Ux = y
double *x = new double[noOfRows]; // Column matrix
x[noOfRows - 1] = y[noOfRows - 1] / U.GetEntry(noOfRows - 1, noOfRows - 1);
for (int k = noOfRows - 2; k >= 0; k--) {
x[k] = y[k];
for (int i = 1; i < noOfRows; i++)
x[k] -= U.GetEntry(k, k + i) * x[k + i];
x[k] /= U.GetEntry(k, k);
}
return *(new Matrix(noOfRows, 1, x));
}
SquareMatrix& SquareMatrix::TriL() const
{
SquareMatrix* result(new SquareMatrix(noOfColumns));
// Copy the upper triangle with the diagonal.
for (int i = 0; i < noOfColumns; i++) {
for (int j = i; j < noOfRows; j++)
result->data[GetIndex(j, i)] = this->GetEntry(j, i);
}
return *result;
}
SquareMatrix& SquareMatrix::TriU() const
{
SquareMatrix* result(new SquareMatrix(noOfColumns));
// Copy the upper triangle with the diagonal.
for (int i = 0; i < noOfColumns; i++) {
for (int j = 0; j <= i; j++)
result->data[GetIndex(j, i)] = this->GetEntry(j, i);
}
return *result;
}
SquareMatrix& SquareMatrix::Identity(const int dimension)
{
SquareMatrix *eye(new SquareMatrix(dimension));
for (size_t i = 0; i < dimension; i++)
eye->data[i * (dimension + 1)] = 1;
return *eye;
}
SquareMatrix& SquareMatrix::Ones(const int dimension)
{
return *(new SquareMatrix(Matrix::Ones(dimension, dimension)));
}
SquareMatrix& SquareMatrix::Toeplitz(const double* column, const double* row, const int dimension)
{
Matrix m(Matrix::Toeplitz(column, dimension, row, dimension));
SquareMatrix *sm(new SquareMatrix(m));
return *sm;
}
SquareMatrix& SquareMatrix::Toeplitz(const double* vector, const int dimension)
{
return SquareMatrix::Toeplitz(vector, vector, dimension);
}
SquareMatrix& SquareMatrix::Zeros(const int dimension)
{
return *(new SquareMatrix(dimension));
}