-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cpp
More file actions
240 lines (234 loc) · 6.49 KB
/
Matrix.cpp
File metadata and controls
240 lines (234 loc) · 6.49 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
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
#include "Matrix.h"
#include <cassert>
using namespace std;
int Matrix::rows() const
{
return mNumRows;
}
int Matrix::cols() const
{
return mNumCols;
}
//constructors
Matrix::Matrix()
{
mNumRows = 0;
mNumCols = 0;
mData = nullptr;
}
Matrix::Matrix(int numRows, int numCols)
{
assert(numRows > 0 && numCols > 0);
mNumRows = numRows;
mNumCols = numCols;
mData = new double*[mNumRows];
for (int i = 0; i < mNumRows; i++)
{
mData[i] = new double[mNumCols];
for (int j = 0; j < mNumCols; j++)
{
mData[i][j] = 0.0;
}
}
}
//copy constructors
Matrix::Matrix(const Matrix& other)
{
mNumRows = other.mNumRows;
mNumCols = other.mNumCols;
mData = new double*[mNumRows];
for (int i = 0; i < mNumRows; i++)
{
mData[i] = new double[mNumCols];
for (int j = 0; j < mNumCols; j++)
{
mData[i][j] = other.mData[i][j];
}
}
}
//destructors
Matrix::~Matrix()
{
for (int i = 0; i<mNumRows; i++)
{
delete[] mData[i];
}
delete[] mData;
}
//assignment
Matrix &Matrix::Matrix::operator=(const Matrix& other)
{
if (this != &other)
{
//clean up old
for (int i = 0; i < mNumRows; i++)
{
delete[] mData[i];
}
delete[] mData;
//copy new
mNumRows = other.mNumRows;
mNumCols = other.mNumCols;
mData = new double*[mNumRows];
for (int i = 0; i < mNumRows; i++)
{
mData[i] = new double[mNumCols];
for (int j = 0; j < mNumCols; j++)
{
mData[i][j] = other.mData[i][j];
}
}
}
return *this;
}
//Matrix::operator()
double Matrix::Matrix::operator()(int i, int j) const
{
assert(i >= 1 && i <= mNumRows);
assert( j >= 1 && j <= mNumCols);
return mData[i - 1][j - 1];
}
double& Matrix::operator()(int i, int j)
{
assert(i >= 1 && i <= mNumRows);
assert( j >= 1 && j <= mNumCols);
return mData[i - 1][j - 1];
}
//add
Matrix Matrix::operator+(const Matrix &other) const
{
assert(mNumRows == other.mNumRows && mNumCols == other.mNumCols);
Matrix result(mNumRows, mNumCols);
for (int i = 1; i <= mNumRows; i++) // Change to 1-based indexing
{
for (int j = 1; j <= mNumCols; j++)
{
result(i, j) = (*this)(i, j) + other(i, j);
}
}
return result;
}
//subtract
Matrix Matrix::operator-(const Matrix &other) const
{
assert(mNumRows == other.mNumRows && mNumCols == other.mNumCols);
Matrix result(mNumRows, mNumCols);
for (int i = 1; i <= mNumRows; i++) // Changed to 1-based indexing
{
for (int j = 1; j <= mNumCols; j++)
{
result(i, j) = (*this)(i, j) - other(i, j);
}
}
return result;
}
//scalar multiply
Matrix Matrix::operator*(double scalar) const
{
Matrix result(mNumRows, mNumCols);
for (int i = 1; i <= mNumRows; i++) // Changed to 1-based indexing
{
for (int j = 1; j <= mNumCols; j++)
{
result(i, j) = (*this)(i, j) * scalar;
}
}
return result;
}
//multiply
Vector Matrix::operator*(const Vector& vec) const
{
assert(mNumCols == vec.size());
Vector result(mNumRows);
for (int i = 1; i <= mNumRows; i++)
{
double sum = 0.0;
for (int j = 1; j <= mNumCols; j++)
{
sum += (*this)(i, j) * vec(j);
}
result(i) = sum;
}
return result;
}
//print func
void Matrix::print() const
{
for (int i = 0; i < mNumRows; i++)
{
for (int j = 0; j < mNumCols; j++)
{
cout << mData[i][j] << " ";
}
cout << endl;
}
}
//inverse
[[nodiscard]] Matrix Matrix::inverse() const
{
assert(mNumRows == mNumCols);
int n = mNumRows;
Matrix A(*this);
Matrix I(n, n);
for (int i = 1; i <= n; i++)
{
I(i, i) = 1.0;
}
//Gauss-Jordan elimination
for (int k = 1; k <= n; k++)
{
double pivot = A(k, k);
assert(pivot != 0); //no zero pivot
for (int j = 1; j <= n; j++)
{
A(k, j) /= pivot;
I(k, j) /= pivot;
}
for (int i = 1; i <= n; i++)
{
if (i != k)
{
double factor = A(i, k);
for (int j = 1; j <= n; j++)
{
A(i, j) -= factor * A(k, j);
I(i,j) -= factor * I(k, j);
}
}
}
} return I;
}
//for Matrix * Matrix overload
Matrix Matrix::operator*(const Matrix& other) const
{
assert(mNumCols == other.mNumRows);
Matrix result(mNumRows, other.mNumCols);
for (int i = 1; i <= mNumRows; i++)
{
for (int j = 1; j <= other.mNumCols; j++)
{
double sum = 0.0;
for (int k = 1; k <= mNumCols; k++)
{
sum += (*this)(i, k) * other(k, j);
}
result(i, j) = sum;
}
}
return result;
}
//pseudo-inverse(Moore-Penrose inverse)
[[nodiscard]] Matrix Matrix::pseudoInverse() const
{
Matrix At(mNumCols, mNumRows); //tranpose
for (int i = 1; i <= mNumRows; i++)
{
for (int j = 1; j<=mNumCols; j++)
{
At(j, i) = (*this)(i, j);
}
}
Matrix AtA = At * (*this);
Matrix AtA_inv = AtA.inverse();
return AtA_inv * At;
}