-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cpp
More file actions
228 lines (184 loc) · 5.04 KB
/
Matrix.cpp
File metadata and controls
228 lines (184 loc) · 5.04 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
#include <cmath>
#include "Matrix.h"
Matrix::Matrix(const std::vector<double>& arr, const unsigned int cols, const unsigned int rows) {
m_cols = cols;
m_rows = rows;
m_mat = Pseudo2DArray<double>(arr, m_cols, m_rows);
}
Matrix::Matrix(const Pseudo2DArray<double>& arr) {
m_mat = arr;
m_cols = m_mat.GetWidth();
m_rows = m_mat.GetHeight();
}
Matrix::Matrix(const Matrix& other) {
m_cols = other.m_cols;
m_rows = other.m_rows;
m_mat = other.m_mat;
}
Matrix& Matrix::operator=(const Matrix& other) {
if (this == &other) return *this;
m_cols = other.m_cols;
m_rows = other.m_rows;
m_mat = other.m_mat;
return *this;
}
Matrix& Matrix::operator*=(const Matrix& rhs) {
if (m_cols != rhs.m_rows) return *this;
const unsigned int newCols = rhs.m_cols;
const unsigned int newRows = m_rows;
Pseudo2DArray<double> newMat(newCols, newRows);
for (unsigned int i = 0; i < newCols; i++) {
for (unsigned int j = 0; j < newRows; j++) {
double total = 0.;
for (unsigned int k = 0; k < rhs.m_rows; k++) {
total += m_mat(k, j) * rhs.m_mat(i, k);
}
newMat(i, j) = total;
}
}
m_mat = newMat;
m_cols = newCols;
m_rows = newRows;
return *this;
}
Matrix Matrix::operator*(const Matrix& rhs) const {
Matrix lhs(*this);
lhs *= rhs;
return lhs;
}
Matrix& Matrix::operator*=(const double scalar) {
for (unsigned int i = 0; i < m_cols; i++) {
for (unsigned int j = 0; j < m_rows; j++) {
m_mat(i, j) *= scalar;
}
}
return *this;
}
Matrix Matrix::operator*(const double scalar) const {
Matrix lhs(*this);
lhs *= scalar;
return lhs;
}
double& Matrix::operator()(const unsigned int x, const unsigned int y) {
return m_mat(x, y);
}
double Matrix::operator()(const unsigned int x, const unsigned int y) const {
return m_mat(x, y);
}
void Matrix::Pow(const double p) {
for (unsigned int i = 0; i < m_cols; i++) {
for (unsigned int j = 0; j < m_rows; j++) {
m_mat(i, j) = std::pow(m_mat(i, j), p);
}
}
}
void Matrix::Cbrt() {
for (unsigned int i = 0; i < m_cols; i++) {
for (unsigned int j = 0; j < m_rows; j++) {
m_mat(i, j) = std::cbrt(m_mat(i, j));
}
}
}
void Matrix::NRoot(const double n) {
const double exp = 1. / n;
for (unsigned int i = 0; i < m_cols; i++) {
for (unsigned int j = 0; j < m_rows; j++) {
if (std::fmod(n, 1.) == 0.) {
m_mat(i, j) = std::pow(m_mat(i, j), exp);
}
else {
double absroot = std::pow(std::abs(m_mat(i, j)), exp);
if (m_mat(i, j) < 0.) absroot *= -1.;
m_mat(i, j) = absroot;
}
}
}
}
void Matrix::Transpose() {
Pseudo2DArray<double> newMat(m_rows, m_cols);
for (unsigned int i = 0; i < m_cols; i++) {
for (unsigned int j = 0; j < m_rows; j++) {
newMat(j, i) = m_mat(i, j);
}
}
m_mat = newMat;
m_cols = newMat.GetWidth();
m_rows = newMat.GetHeight();
//return true;
}
double Matrix::Determinant() const {
if (m_rows != m_cols) return 0.0;
double total = 0.0;
for (int x = 0; x < (int)m_cols; x++) {
// addition
double mult = m_mat.GetValueWrapped(x, 0);
for (int i = 1; i < (int)m_rows; i++) {
mult *= m_mat.GetValueWrapped(x + i, i);
}
total += mult;
// subtraction
mult = m_mat.GetValueWrapped(x, 0);
for (int i = 1; i < (int)m_rows; i++) {
mult *= m_mat.GetValueWrapped(x - i, i);
}
total -= mult;
}
return total;
}
bool Matrix::Invert() {
if (m_rows != m_cols) return false;
if ((*this).Determinant() == 0.) return false;
const unsigned int size = m_rows;
// pivoting
Pseudo2DArray<double> identity = (*this).Identity().m_mat;
for (unsigned int x = 0; x < size; x++) {
// row exchange if pivot == 0
// swap with value in same column with biggest abs value
if (m_mat(x, x) == 0.) {
unsigned int swap_row = x == 0 ? 1 : 0;
double chosen_abs = 0;
for (unsigned int y = 0; y < size; y++) {
if (x != y) {
double abs = std::abs(m_mat(x, y));
if (abs > chosen_abs) {
swap_row = y;
chosen_abs = abs;
}
}
}
m_mat.SwapRows(x, swap_row);
identity.SwapRows(x, swap_row);
}
// zero other values in column
for (unsigned int y = 0; y < size; y++) {
if (y != x) {
double mult = (-1. * m_mat(x, y)) / m_mat(x, x);
for (unsigned int z = 0; z < size; z++) {
m_mat(z, y) = (mult * m_mat(z, x)) + m_mat(z, y);
identity(z, y) = (mult * identity(z, x)) + identity(z, y);
}
}
}
}
// multiply
for (unsigned int y = 0; y < size; y++) {
const double div = m_mat(y, y);
for (unsigned int x = 0; x < size; x++) {
identity(x, y) /= div;
}
}
m_mat = identity;
return true;
}
Matrix Matrix::Identity() const {
Matrix id(m_mat);
for (unsigned int y = 0; y < m_rows; y++) {
for (unsigned int x = 0; x < m_cols; x++) {
id.m_mat(x, y) = x == y ? 1. : 0.;
}
}
return id;
}
void Matrix::SetValue(const unsigned int x, const unsigned int y, const double value) {
m_mat(x, y) = value;
}