-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp~
249 lines (216 loc) · 4.02 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
#include "Matrix.hpp"
#define TINY 1.0e-20
//Constructor - Matrix Initializer
Matrix::Matrix(int rows_, int cols_)
{
std::vector<double> temp_vec(cols_);
m.resize(rows_,temp_vec);
rows = rows_;
cols = cols_;
temp_vec.clear();
}
// number of rows/colums
int Matrix::numRows() const
{
return rows;
}
int Matrix::numCols() const
{
return cols;
}
// acess individual matrix elements
const double& Matrix::operator()(const int& row, const int& col) const
{
return m[row][col];
}
double& Matrix::operator()(const int& row,const int& col)
{
return m[row][col];
}
//matrix matrix operations
//Addition
Matrix Matrix::operator+(const Matrix& m_RHS)
{
int rows = m_RHS.numRows();
int cols = m_RHS.numCols();
Matrix rtn(rows, cols);
for (int i =0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
rtn(i,j) = m[i][j] + m_RHS(i,j);
}
}
return rtn;
}
//Subtraction
Matrix Matrix::operator-(const Matrix& m_RHS)
{
int rows = m_RHS.numRows();
int cols = m_RHS.numCols();
Matrix rtn(rows, cols);
for (int i =0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
rtn(i,j) = m[i][j] - m_RHS(i,j);
}
}
return rtn;
}
//Multiplication (AB =/= BA)
Matrix Matrix::operator*(const Matrix& m_RHS)
{
int rows_RHS = m_RHS.numRows();
int cols_RHS = m_RHS.numCols();
Matrix rtn(rows_RHS, cols_RHS);
//fill rtn with zeros
for(int i=0; i < rows_RHS; i++)
{
for(int j=0; j < cols_RHS;j++)
{
rtn(i,j) = 0;
}
}
// Matrix Multiplication O(n^3)
for (int i=0; i<rows_RHS; i++) {
for (int j=0; j<cols_RHS; j++) {
for (int k=0; k<rows_RHS; k++) {
rtn(i,j) += m[i][k] * m_RHS(k,j);
}
}
}
// rows_RHS.clear();
// cols_RHS.clear();
return rtn;
}
// Matrix inversion
// Matrix/Scalar operation
// Addition
Matrix Matrix::operator+(const double& a)
{
Matrix rtn(rows, cols);
for (int i = 0; i< rows; i++)
{
for(int j = 0; j<cols; j++)
{
rtn(i,j) = m[i][j] + a;
}
}
return rtn;
}
//Subtration
Matrix Matrix::operator-(const double& a)
{
Matrix rtn(rows, cols);
for (int i = 0; i< rows; i++)
{
for(int j = 0; j<cols; j++)
{
rtn(i,j) = m[i][j] - a;
}
}
return rtn;
}
// Multiplication
Matrix Matrix::operator*(const double& a)
{
Matrix rtn(rows, cols);
for (int i = 0; i< rows; i++)
{
for(int j = 0; j<cols; j++)
{
rtn(i,j) = m[i][j] * a;
}
}
return rtn;
}
//Division
Matrix Matrix::operator/(const double& a)
{
Matrix rtn(rows, cols);
for (int i = 0; i< rows; i++)
{
for(int j = 0; j<cols; j++)
{
rtn(i,j) = m[i][j] / a;
}
}
return rtn;
}
// Matrix/Vector multiplication
// Column Vector
// test if algorithm is correct?
Vector Matrix::operator*(const Vector& v)
{
Vector rtn(rows);
for(int i = 0; i<rows; i++)
{
for(int j = 0; j < cols; j++)
{
rtn[i] += m[i][j] * v[j];
}
}
return rtn;
}
//Gaussian Elimination
Vector Matrix::Gauss()
{
double maxEl,maxRow,tmp;
Vector rtn(rows);
Matrix mCopy(rows,cols);
//Copy matrix so as not to modify it later on
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
mCopy(i,j) = m[i][j];
}
}
//search for maximum
for(int i = 0; i<rows; i++)
{
maxEl = std::abs(mCopy(i,i));
maxRow = i;
for (int k = i+1; k<rows; k++)
{
if(std::abs(mCopy(k,i)) > maxEl)
{
maxEl = std::abs(mCopy(k,i));
maxRow = k;
}
}
//Swap max row wit current row coum by colum
for(int k=i; k<rows+1; k++)
{
tmp = mCopy(maxRow,k);
mCopy(maxRow,k) = mCopy(i,k);
mCopy(i,k) = tmp;
}
//make all rows below this one 0 in the current column
for(int k=i+1; k<rows; k++)
{
double c = -mCopy(k,i)/mCopy(i,i);
for(int j=i; j<rows+1;j++)
{
if(i==j)
{
mCopy(k,j) =0;
}
else
{
mCopy(k,j) += c*mCopy(i,j);
}
}
}
}
for(int i = rows-1; i>=0;i--)
{
rtn[i] = mCopy(i,rows)/mCopy(i,i);
for(int k=i-1;k>=0;k--)
{
mCopy(k,rows) -= mCopy(k,i)*rtn[i];
}
}
return rtn;
}